]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/ClipboardWindows.cxx
Reset: do re-init Ghost bindings.
[flightgear.git] / src / Scripting / ClipboardWindows.cxx
1 // Windows implementation of clipboard access for Nasal
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #include "NasalClipboard.hxx"
20
21 #include <simgear/debug/logstream.hxx>
22 #include <windows.h>
23
24 /**
25  * Windows does only support on clipboard and no selection. We fake also the X11
26  * selection buffer - at least inside FlightGear
27  */
28 class ClipboardWindows:
29   public NasalClipboard
30 {
31   public:
32
33     /**
34      * Get clipboard contents as text
35      */
36     virtual std::string getText(Type type)
37     {
38       if( type == CLIPBOARD )
39       {
40         std::string data;
41
42         if( !OpenClipboard(NULL) )
43           return data;
44
45         HANDLE hData = GetClipboardData( CF_TEXT );
46         char* buff = (char*)GlobalLock( hData );
47         if (buff)
48           data = buff;
49         GlobalUnlock( hData );
50         CloseClipboard();
51
52         return data;
53       }
54       else
55         return _selection;
56     }
57
58     /**
59      * Set clipboard contents as text
60      */
61     virtual bool setText(const std::string& text, Type type)
62     {
63       if( type == CLIPBOARD )
64       {
65         if( !OpenClipboard(NULL) )
66           return false;
67
68         bool ret = true;
69         if( !EmptyClipboard() )
70           ret = false;
71         else if( !text.empty() )
72         {
73           HGLOBAL hGlob = GlobalAlloc(GMEM_MOVEABLE, text.size() + 1);
74           if( !hGlob )
75             ret = false;
76           else
77           {
78             memcpy(GlobalLock(hGlob), (char*)&text[0], text.size() + 1);
79             GlobalUnlock(hGlob);
80
81             if( !SetClipboardData(CF_TEXT, hGlob) )
82             {
83               GlobalFree(hGlob);
84               ret = false;
85             }
86           }
87         }
88
89         CloseClipboard();
90         return ret;
91       }
92       else
93       {
94         _selection = text;
95         return true;
96       }
97     }
98
99   protected:
100
101     std::string _selection;
102 };
103
104 //------------------------------------------------------------------------------
105 NasalClipboard::Ptr NasalClipboard::create()
106 {
107   return NasalClipboard::Ptr(new ClipboardWindows);
108 }