Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Text reading and writing fromt/to clipboard

This thread is locked; no one can reply to it. rss feed Print
Text reading and writing fromt/to clipboard
DanielH
Member #934
January 2001
avatar

Windows only

I've tested it a few times, but would like for someone else to test it. Also any suggestions.

// header
ALLEGRO_USTR *al_read_from_clipboard();
bool al_write_to_clipboard(ALLEGRO_USTR *str);

#SelectExpand
1// source 2#include <allegro5\allegro.h> 3#include <allegro5/allegro_windows.h> 4 5ALLEGRO_USTR *al_read_from_clipboard() 6{ 7 HWND hWnd = 0; 8 ALLEGRO_USTR *str = 0; 9 uint16_t *pchData = 0; 10 HANDLE hClipboardData = 0; 11 size_t size = 0; 12 ALLEGRO_DISPLAY *display = 0; 13 14 display = al_get_current_display(); 15 16 if (display != 0) 17 { 18 hWnd = al_get_win_window_handle(display); 19 20 if (hWnd != 0) 21 { 22 if (OpenClipboard(hWnd)) 23 { 24 if (IsClipboardFormatAvailable(CF_UNICODETEXT) || 25 IsClipboardFormatAvailable(CF_TEXT) || 26 IsClipboardFormatAvailable(CF_OEMTEXT)) 27 { 28 hClipboardData = GetClipboardData(CF_UNICODETEXT); 29 30 if (hClipboardData != 0) 31 { 32 pchData = (uint16_t*)GlobalLock(hClipboardData); 33 34 if (pchData != 0) 35 { 36 str = al_ustr_new_from_utf16(pchData); 37 38 GlobalUnlock(hClipboardData); 39 } 40 } 41 } 42 43 CloseClipboard(); 44 } 45 } 46 } 47 48 return str; 49} 50 51bool al_write_to_clipboard(ALLEGRO_USTR *str) 52{ 53 HWND hWnd = 0; 54 bool rv = false; 55 uint16_t *pchData = 0; 56 HGLOBAL hClipboardData = 0; 57 size_t size = 0; 58 int offset = 0; 59 ALLEGRO_DISPLAY *display = 0; 60 size_t i = 0; 61 62 display = al_get_current_display(); 63 64 if (display != 0) 65 { 66 hWnd = al_get_win_window_handle(display); 67 68 if (hWnd != 0) 69 { 70 if (OpenClipboard(hWnd)) 71 { 72 EmptyClipboard(); 73 74 if (str != 0) 75 { 76 size = (al_ustr_length(str) + 1) * sizeof(uint16_t); 77 78 if (size > 1) 79 { 80 hClipboardData = GlobalAlloc(GMEM_MOVEABLE, size); 81 82 if (hClipboardData != 0) 83 { 84 pchData = (uint16_t*)GlobalLock(hClipboardData); 85 86 if (pchData != 0) 87 { 88 if (size == al_ustr_encode_utf16(str, pchData, size)) 89 { 90 } 91 92 GlobalUnlock(hClipboardData); 93 94 if (SetClipboardData(CF_UNICODETEXT, hClipboardData)) 95 { 96 rv = true; 97 } 98 } 99 } 100 } 101 } 102 103 CloseClipboard(); 104 } 105 } 106 } 107 108 return rv; 109}

beoran
Member #12,636
March 2011

Good beginning, but this needs to be made portable. Copy paste under Linux is a bit ... complex as well.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Edgar Reynaldo was working on this a few years back.

https://www.allegro.cc/forums/thread/606034

They all watch too much MSNBC... they get ideas.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Yeah, but mine never supported utf16 on windows. And the linux version was a nightmare but it worked.

For Linux copy/paste your program needs to respond to SelectionRequest events, something an Allegro program could not do without hacking Allegro to expose the X11 events that it monitors. For these reasons, an external program is the best solution to manage copy/paste on Linux. Since xsel, xclip and others don't manage images, I wrote my own program that does, xcsi. Copying to the clipboard (through xcsi) also works better, because xcsi stays running as long as it is the selection owner, as opposed to closing your program and losing the selection data.

What we need is someone experienced with MacOSX and obj-C to write clipboard code for OSX.

Go to: