Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 968


Ignore:
Timestamp:
Mar 31, 2008, 9:22:54 PM (16 years ago)
Author:
landauf
Message:
  • added a try-catch block to avoid crashes when something strange (like a file) is in the clipboard.
  • added a static variable to allow at least a local ingame clipboard for unsupported systems
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core2/src/util/Clipboard.cc

    r966 r968  
    2424 *      ...
    2525 *
     26 *   Windows version inspired by "Copy Text To Clipboard" by Laszlo Szathmary, 2007
     27 *      http://www.loria.fr/~szathmar/off/projects/C/CopyTextToClipboard/index.php
    2628 */
    2729
     
    3335    bool toClipboard(std::string text)
    3436    {
    35         if (OpenClipboard(0))
     37        try
    3638        {
    37             EmptyClipboard();
    38             HGLOBAL clipbuffer = GlobalAlloc(GMEM_DDESHARE, text.size() + 1);
    39             char* buffer = (char*)GlobalLock(clipbuffer);
    40             strcpy(buffer, text.c_str());
    41             GlobalUnlock(clipbuffer);
    42             SetClipboardData(CF_TEXT, clipbuffer);
    43             CloseClipboard();
     39            if (OpenClipboard(0))
     40            {
     41                EmptyClipboard();
     42                HGLOBAL clipbuffer = GlobalAlloc(GMEM_DDESHARE, text.size() + 1);
     43                char* buffer = (char*)GlobalLock(clipbuffer);
     44                strcpy(buffer, text.c_str());
     45                GlobalUnlock(clipbuffer);
     46                SetClipboardData(CF_TEXT, clipbuffer);
     47                CloseClipboard();
    4448
    45             return true;
     49                return true;
     50            }
     51        }
     52        catch (...)
     53        {
    4654        }
    4755        return false;
     
    5058    std::string fromClipboard()
    5159    {
    52         if (OpenClipboard(0))
     60        try
    5361        {
    54             HANDLE hData = GetClipboardData(CF_TEXT);
    55             std::string output = (char*)GlobalLock(hData);
    56             GlobalUnlock(hData);
    57             CloseClipboard();
     62            if (OpenClipboard(0))
     63            {
     64                HANDLE hData = GetClipboardData(CF_TEXT);
     65                std::string output = (char*)GlobalLock(hData);
     66                GlobalUnlock(hData);
     67                CloseClipboard();
    5868
    59             return output;
     69                return output;
     70            }
     71        }
     72        catch (...)
     73        {
    6074        }
    6175        return "";
    6276    }
    6377#else
     78    std::string clipboard = "";
     79
    6480    bool toClipboard(std::string text)
    6581    {
    66         return false;
     82        clipboard = text;
     83        return true;
    6784    }
     85
    6886    std::string fromClipboard()
    6987    {
    70         return "";
     88        return clipboard;
    7189    }
    7290#endif
Note: See TracChangeset for help on using the changeset viewer.