Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Writing to/getting clipboard data

This thread is locked; no one can reply to it. rss feed Print
Writing to/getting clipboard data
Felix-The-Ghost
Member #9,729
April 2008
avatar

Hello, right now I was wanting to export a bitmap to the system clipboard, although being able to fetch this would be nice as well.
I looked and found only expired MS links. Rather than hard-coding one export function can I have a function that checks the OS and uses the right system code since Allegro has no clipboard functions?
Guys I've never used the system functions.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

Don Freeman
Member #5,110
October 2004
avatar

You would want to make that part of your build system. If they compile it on Linux...then the Linux code would be used, and so on for the other OS versions. Just look at how Allegro does this.::)

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Felix-The-Ghost
Member #9,729
April 2008
avatar

What if I've never

Quote:

make that part of your build system.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

torhu
Member #2,727
September 2002
avatar

MSDN docs for the clipboard are here:
http://msdn.microsoft.com/en-us/library/ms648709(VS.85).aspx

I've never used that part of the api myself, though.

Allegro has a function to convert an allegro bitmap into a Windows bitmap, that you can pass to SetClipboardData():
http://alleg.sourceforge.net/stabledocs/en/alleg037.html#convert_bitmap_to_hbitmap

Good luck ;)

Felix-The-Ghost
Member #9,729
April 2008
avatar

Yeah I keep hearing about that but is that only for windows?
I'll try it anyway but I really want something cross-platform with platforms that support clipboards.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

torhu
Member #2,727
September 2002
avatar

Cross-platform GUI libs often have clipboard functionality. I guess you could borrow some code from one of them. Or use one of them directly.

Otherwise, you'll just have to implement it yourself on each platform. :-/

Thomas Harte
Member #33
April 2000
avatar

There is a cross-platform clipboard access add-on for SDL, probably you could adapt what you need from that. Otherwise, I can offer this code, which should be trivial to adapt, that I use myself for copying an SDL surface to the OS X clipboard:

1void MacHostMachine::SaveScreenShot( SDL_Surface * surface )
2{
3 if ( NULL == surface )
4 {
5 DisplayError( "Unable to copy screen buffer" );
6 return;
7 }
8 printf( "Copy Surface:\nSize - %dx%d\nOffset - %d\nPitch - %d\nRefcount - %d", surface->w, surface->h, surface->offset, (int)surface->pitch, surface->refcount );
9 
10 // Create an NSImage the same size as the SDL surface
11 NSSize sz;
12 sz.width = surface->w;
13 sz.height = surface->h;
14 NSImage * img = [ [NSImage alloc ] initWithSize: sz ];
15 
16 // Create a bitmap image representation to put things in
17 NSBitmapImageRep * rep = [NSBitmapImageRep alloc ];
18 [rep initWithBitmapDataPlanes:NULL
19 pixelsWide:surface->w
20 pixelsHigh:surface->h
21 bitsPerSample:8
22 samplesPerPixel:3
23 hasAlpha:NO
24 isPlanar:NO
25 colorSpaceName:NSCalibratedRGBColorSpace
26 bytesPerRow:0
27 bitsPerPixel:0 ];
28 
29 // Copy the SDL surfaces data to the bitmap representation
30 memcpy( [rep bitmapData], surface->pixels, (surface->w * 3) * surface->h);
31 // Add the bitmap representation to the NSImage
32 <img src="addRepresentation: rep" />;
33 
34 // Add the image to the pasteboard
35 NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
36 [pasteboard
37 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
38 owner:nil ];
39 [pasteboard
40 setData:<img src="TIFFRepresentation" />
41 forType:NSTIFFPboardType ];
42 
43 // Free the image and representation
44 <img src="release" />;
45 [rep release];
46 
47 // Free the surface by calling baseclass
48 HostMachine::SaveScreenShot( surface );
49}

(edit: that's Objective-C++, put it in a file with a name ending in .mm, put the standard C or C++ function definition into an ordinary header file, include the header file from ordinary C or C++ code and call the function as you would any other; implement the Windows equivalent in a .c or .cpp file and call it through the same header — then just pick which file to include in your project according to your platform).

And for copying to the Windows clipboard:

1void WindowsHostMachine::SaveScreenShot( SDL_Surface * surface )
2{
3 // We have a 24 bit surface which we will convert to a DIB for storage on the clipboard
4 int bmpRowSize = surface -> w * 3;
5 int bmpSize = sizeof( BITMAPINFO ) + ( bmpRowSize * surface -> h );
6 HANDLE dib = ::GlobalAlloc( GMEM_MOVEABLE, bmpSize );
7 BITMAPINFO * bmp = (BITMAPINFO *)::GlobalLock( dib );
8 ::ZeroMemory( bmp, bmpSize );
9 bmp -> bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
10 bmp -> bmiHeader.biWidth = surface -> w;
11 bmp -> bmiHeader.biHeight = surface -> h;
12 bmp -> bmiHeader.biPlanes = 1;
13 bmp -> bmiHeader.biBitCount = 24;
14 bmp -> bmiHeader.biCompression = BI_RGB;
15 bmp -> bmiHeader.biSizeImage = 0;
16
17 char * bmpRow = ((char *)bmp) + sizeof( BITMAPINFOHEADER );
18 // Just to be difficult windows DIBs are stored with the pixels in inverse order.
19 for ( int row = ( surface -> h - 1 ); row > -1; row -- )
20 {
21 memcpy( bmpRow, ((char*)surface -> pixels ) + ( row * bmpRowSize ), bmpRowSize );
22 bmpRow += bmpRowSize;
23 }
24
25 ::GlobalUnlock( dib );
26
27 // This is the lazy way, we're using the old-style clipboard that dates back to win16!
28 ::OpenClipboard( NULL);
29 ::SetClipboardData( CF_DIB, dib );
30 ::CloseClipboard();
31 // The DIB memory we allocated now belongs to windows, we're not supposed to free it.
32
33 // Free the surface though...
34 HostMachine::SaveScreenShot( surface );
35}

Elias
Member #358
May 2000

Hm, someone want to make a clipboard addon for A5? :)

--
"Either help out or stop whining" - Evert

Don Freeman
Member #5,110
October 2004
avatar

I would, but I don't know how to get the clipboard info in X or on a Mac OS... Even if I did, I would not be able to provide any Mac code....I don't own one to test on.::)

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Evert
Member #794
November 2000
avatar

Quote:

someone want to make a clipboard addon for A5? :)

My thoughts exactly, after reading the first post.

Quote:

I would, but I don't know how to get the clipboard info in X or on a Mac OS... Even if I did, I would not be able to provide any Mac code....I don't own one to test on.

That's ok, I'll do the Mac port. Elias can do the X11/GTK port. That's how it went for the file picker addon too. ;)

Don Freeman
Member #5,110
October 2004
avatar

Quote:

That's ok, I'll do the Mac port. Elias can do the X11/GTK port. That's how it went for the file picker addon too. ;)

Ok...I'll try to get some work done on it tonight then. I've been battling another stupid chest cold. It really sucks when you already have breathing problems...like asthma.:-/ I'll just post something here, or in my own thread...I don't have write access to the Allegro SVN. I really need to check about that though...I've not tried to get it yet.:'( How do I go about getting write access anyway?!???

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

My thoughts exactly, after reading the first post.

I kept needing to refrain from posting a "Allegro 5 needs a clipboard addon" message :-X Considering this thread is about allegro 4.

The main issue with making such a beast is that every platform does it a little bit differently. Clipboard/D&D support in an app with a nice cross platform gui isn't exactly a cake walk..

edit:

Quote:

How do I go about getting write access anyway?!???

Make some patches, do some work, and people will give you write access when they think you are ready ;) (ie: you can be trusted not to cause svn to explode).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Don Freeman
Member #5,110
October 2004
avatar

Quote:

Make some patches, do some work, and people will give you write access when they think you are ready ;) (ie: you can be trusted not to cause svn to explode).

OK...I Got ya.8-)

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

BAF
Member #2,981
December 2002
avatar

Allegro 5 is a game library.... why the hell does a game need to use the clipboard?

Thomas Harte
Member #33
April 2000
avatar

A game probably doesn't, but it might. Isn't that why the add-on system was invented?

Don Freeman
Member #5,110
October 2004
avatar

I use Allegro for other stuff as well. Games need level editors and such. It would be nice to have a uniform approach. You could even have the editor built into the game. Another nice feature...it's multiplatform, if designed correctly.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Elias
Member #358
May 2000

Almost all network games need copy&paste for the in-game chat. Look for example at Wesnoth - it would be really annoying if you could not copy&paste there.

--
"Either help out or stop whining" - Evert

LennyLen
Member #5,313
December 2004
avatar

Quote:

I use Allegro for other stuff as well.

Only one of my Allegro projects has been a game. Most of what I've used it for has been command line image manipulation tools and graphing utilities. ;)

Don Freeman
Member #5,110
October 2004
avatar

And once again...I will bring up the network addon for a5.::) Anyone interested?

On the clipboard issue:
I'm working on a Windows version anyway. How would I go about converting an HBITMAP to the new ALLEGRO_BITMAP? Maybe we could add this to the functions available under Windows....like the 4.2/4.3 versions:
win_get_dc
win_release_dc
convert_hbitmap_to_bitmap
convert_bitmap_to_hbitmap

Here is what I am thinking of the function protos:

ALLEGRO_CLIPBOARD_DATA * al_get_clipboard_data( ALLEGRO_DISPLAY *display );
bool al_set_clipboard_data( ALLEGRO_CLIPBOARD_DATA *data );

and then probably have conversion routines to convert from a ALLEGRO_CLIPBOARD_DATA struct to the various stuff, like an ALLEGRO_BITMAP or ALLEGRO_SAMPLE...probably add the ability for user defined types as well.
That cool for ya?!???

Hey, has anyone seen SpellCaster in a while?!

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Alianix
Member #10,518
December 2008
avatar

There are times when we should draw the line in the sand, and this is such an occasion.

"Say no to copy clip board addon for AL5 !" I beg you...;D

Timorg
Member #2,028
March 2002

There is a line in the sand, but if someone is willing to develop and support an addon who are we to stop them. From the point of view of getting it included into allegro, that I agree with, I really can't see that happening.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

Go to: