www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - copy and paste in program

reply Joel Christensen <joelcnz gmail.com> writes:
Hi,

I've got a text program I'm working on. It has game like print. But I 
want to be able to copy to the clip board and paste from it in my 
program etc. I'm using Windows 7. I have used a bit of Ubuntu in the past.

- Joelcnz
Sep 11 2011
parent reply Jimmy Cao <jcao219 gmail.com> writes:
Well, it doesn't matter what you've used in the past :-)

Take a look:

http://msdn.microsoft.com/en-us/library/ms907128.aspx

On Sun, Sep 11, 2011 at 5:29 PM, Joel Christensen <joelcnz gmail.com> wrote:

 Hi,

 I've got a text program I'm working on. It has game like print. But I want
 to be able to copy to the clip board and paste from it in my program etc.
 I'm using Windows 7. I have used a bit of Ubuntu in the past.

 - Joelcnz
Sep 11 2011
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks for the reply Jimmy.

I was thinking more like SDL (has SDL_GetClipboardText, 
SDL_SetClipboardText, 
http://wiki.libsdl.org/moin.cgi/CategoryClipboard). I'm not sure I would 
be able to get Windows CE Clipboard stuff working for me.

- Joelcnz

On 12-Sep-11 1:23 PM, Jimmy Cao wrote:
 Well, it doesn't matter what you've used in the past :-)

 Take a look:

 http://msdn.microsoft.com/en-us/library/ms907128.aspx

 On Sun, Sep 11, 2011 at 5:29 PM, Joel Christensen <joelcnz gmail.com
 <mailto:joelcnz gmail.com>> wrote:

     Hi,

     I've got a text program I'm working on. It has game like print. But
     I want to be able to copy to the clip board and paste from it in my
     program etc. I'm using Windows 7. I have used a bit of Ubuntu in the
     past.

     - Joelcnz
Sep 11 2011
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Mon, 12 Sep 2011 04:39:52 +0300, Joel Christensen <joelcnz gmail.com>  
wrote:

 I'm not sure I would be able to get Windows CE Clipboard stuff working  
 for me.
Search engines often return Windows CE results for API searches. Here's the clipboard developer documentation for full Windows versions: http://msdn.microsoft.com/en-us/library/ms648709(v=VS.85).aspx -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Sep 11 2011
parent reply Joel Christensen <joelcnz gmail.com> writes:
So how would I got about doing it in D?

On 12-Sep-11 1:52 PM, Vladimir Panteleev wrote:
 On Mon, 12 Sep 2011 04:39:52 +0300, Joel Christensen <joelcnz gmail.com>
 wrote:

 I'm not sure I would be able to get Windows CE Clipboard stuff working
 for me.
Search engines often return Windows CE results for API searches. Here's the clipboard developer documentation for full Windows versions: http://msdn.microsoft.com/en-us/library/ms648709(v=VS.85).aspx
Sep 11 2011
parent reply Jimmy Cao <jcao219 gmail.com> writes:
Here's an example I've quickly put together for you:

import std.stdio;
import core.stdc.string;

extern(Windows) {
    void* GetClipboardData(uint);
    bool OpenClipboard(void*);
}

void main() {
    if (OpenClipboard(null)) {
        auto cstr = cast(char*)GetClipboardData(1);
        if (cstr)
            writeln(std.conv.to!string(cast(char*)cstr[0..strlen(cstr)]));
    }
}



On Sun, Sep 11, 2011 at 9:22 PM, Joel Christensen <joelcnz gmail.com> wrote:

 So how would I got about doing it in D?


 On 12-Sep-11 1:52 PM, Vladimir Panteleev wrote:

 On Mon, 12 Sep 2011 04:39:52 +0300, Joel Christensen <joelcnz gmail.com>
 wrote:

  I'm not sure I would be able to get Windows CE Clipboard stuff working
 for me.
Search engines often return Windows CE results for API searches. Here's the clipboard developer documentation for full Windows versions: http://msdn.microsoft.com/en-**us/library/ms648709(v=VS.85).**aspx<http://msdn.microsoft.com/en-us/library/ms648709(v=VS.85).aspx>
Sep 11 2011
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks Jimmy. Your example worked. Or though I haven't managed to get 
the other way to work.

[code]
import std.stdio;
//import core.stdc.string;
import std.c.string;
import std.string;
import std.conv;

extern(Windows) {
     bool OpenClipboard(void*);
     void* GetClipboardData(uint);
     void* SetClipboardData(uint, void*);
}

void main() {
     if (OpenClipboard(null)) {
         auto cstr = cast(char*)GetClipboardData( 1 );
         if (cstr)
             writeln(to!string(cast(char*)cstr[0..strlen(cstr)]));

		SetClipboardData( 1, cast(char*)toStringz( "data set" ) );
     }
}
[/code]
Sep 11 2011
next sibling parent Joel Christensen <joelcnz gmail.com> writes:
I mean, I can't copy text from my program to the clipboard.

- Joelcnz

On 12-Sep-11 3:50 PM, Joel Christensen wrote:
 Thanks Jimmy. Your example worked. Or though I haven't managed to get
 the other way to work.

 [code]
 import std.stdio;
 //import core.stdc.string;
 import std.c.string;
 import std.string;
 import std.conv;

 extern(Windows) {
 bool OpenClipboard(void*);
 void* GetClipboardData(uint);
 void* SetClipboardData(uint, void*);
 }

 void main() {
 if (OpenClipboard(null)) {
 auto cstr = cast(char*)GetClipboardData( 1 );
 if (cstr)
 writeln(to!string(cast(char*)cstr[0..strlen(cstr)]));

 SetClipboardData( 1, cast(char*)toStringz( "data set" ) );
 }
 }
 [/code]
Sep 12 2011
prev sibling parent reply Jimmy Cao <jcao219 gmail.com> writes:
The other way is a bit more complicated.
Try this:

import std.stdio;
import core.stdc.string;
import std.string;

extern(Windows) {
   bool OpenClipboard(void*);
   void* GetClipboardData(uint);
   void* SetClipboardData(uint, void*);
   bool EmptyClipboard();
   bool CloseClipboard();
   void* GlobalAlloc(uint, size_t);
   void* GlobalLock(void*);
   bool GlobalUnlock(void*);
}

void main() {
   if (OpenClipboard(null)) {
       auto cstr = cast(char*)GetClipboardData( 1 );
       if(cstr)
           writeln(cstr[0..strlen(cstr)]);
       EmptyClipboard();

       auto mystr = "my data";
       void* handle = GlobalAlloc(2, mystr.length + 1);
       void* ptr = GlobalLock(handle);
       memcpy(ptr, toStringz(mystr), mystr.length + 1);
       GlobalUnlock(handle);

       SetClipboardData( 1, handle);
       CloseClipboard();
   }
}



On Sun, Sep 11, 2011 at 10:50 PM, Joel Christensen <joelcnz gmail.com>wrote:

 Thanks Jimmy. Your example worked. Or though I haven't managed to get the
 other way to work.

 [code]

 import std.stdio;
 //import core.stdc.string;
 import std.c.string;
 import std.string;
 import std.conv;

 extern(Windows) {
    bool OpenClipboard(void*);
    void* GetClipboardData(uint);
    void* SetClipboardData(uint, void*);

 }

 void main() {
    if (OpenClipboard(null)) {
        auto cstr = cast(char*)GetClipboardData( 1 );
        if (cstr)
            writeln(to!string(cast(char*)**cstr[0..strlen(cstr)]));

                SetClipboardData( 1, cast(char*)toStringz( "data set" ) );
    }
 }
 [/code]
Sep 12 2011
parent Joel Christensen <joelcnz gmail.com> writes:
Thanks so much Jimmy. You put in a bit of effort. :-)

I just added this code to my general library:


extern(Windows) { bool OpenClipboard(void*); void* GetClipboardData(uint); void* SetClipboardData(uint, void*); bool EmptyClipboard(); bool CloseClipboard(); void* GlobalAlloc(uint, size_t); void* GlobalLock(void*); bool GlobalUnlock(void*); } string getTextClipBoard() { if (OpenClipboard(null)) { scope( exit ) CloseClipboard(); auto cstr = cast(char*)GetClipboardData( 1 ); if(cstr) return cstr[0..strlen(cstr)].idup; } return null; } string setTextClipboard( string mystr ) { if (OpenClipboard(null)) { scope( exit ) CloseClipboard(); EmptyClipboard(); void* handle = GlobalAlloc(2, mystr.length + 1); void* ptr = GlobalLock(handle); memcpy(ptr, toStringz(mystr), mystr.length + 1); GlobalUnlock(handle); SetClipboardData( 1, handle); } return mystr; } <<
Sep 12 2011