Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1050 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
RevLine 
[966]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 *
[968]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
[966]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    {
[968]37        try
[966]38        {
[968]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();
[966]48
[968]49                return true;
50            }
[966]51        }
[968]52        catch (...)
53        {
54        }
[966]55        return false;
56    }
57
58    std::string fromClipboard()
59    {
[968]60        try
[966]61        {
[968]62            if (OpenClipboard(0))
63            {
64                HANDLE hData = GetClipboardData(CF_TEXT);
65                std::string output = (char*)GlobalLock(hData);
66                GlobalUnlock(hData);
67                CloseClipboard();
[966]68
[968]69                return output;
70            }
[966]71        }
[968]72        catch (...)
73        {
74        }
[966]75        return "";
76    }
77#else
[968]78    std::string clipboard = "";
79
[966]80    bool toClipboard(std::string text)
81    {
[968]82        clipboard = text;
83        return true;
[966]84    }
[968]85
[966]86    std::string fromClipboard()
87    {
[968]88        return clipboard;
[966]89    }
90#endif
Note: See TracBrowser for help on using the repository browser.