Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/util/Clipboard.cc @ 1696

Last change on this file since 1696 was 968, checked in by landauf, 17 years ago
  • 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 size: 2.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
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
28 */
29
30#include "Clipboard.h"
31
32#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
33    #include <windows.h>
34
35    bool toClipboard(std::string text)
36    {
37        try
38        {
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();
48
49                return true;
50            }
51        }
52        catch (...)
53        {
54        }
55        return false;
56    }
57
58    std::string fromClipboard()
59    {
60        try
61        {
62            if (OpenClipboard(0))
63            {
64                HANDLE hData = GetClipboardData(CF_TEXT);
65                std::string output = (char*)GlobalLock(hData);
66                GlobalUnlock(hData);
67                CloseClipboard();
68
69                return output;
70            }
71        }
72        catch (...)
73        {
74        }
75        return "";
76    }
77#else
78    std::string clipboard = "";
79
80    bool toClipboard(std::string text)
81    {
82        clipboard = text;
83        return true;
84    }
85
86    std::string fromClipboard()
87    {
88        return clipboard;
89    }
90#endif
Note: See TracBrowser for help on using the repository browser.