1 /////////////////////////////////////////////////////////////////////////////
2 // Name: clipboard.cpp
3 // Purpose: clipboard wxWidgets sample
4 // Author: Robert Roebling
5 // Copyright: (c) Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx/wx.h".
10 #include "wx/wxprec.h"
11
12
13 // for all others, include the necessary headers (this file is usually all you
14 // need because it includes almost all "standard" wxWidgets headers)
15 #ifndef WX_PRECOMP
16 #include "wx/wx.h"
17 #endif
18
19 #include "wx/clipbrd.h"
20
21 #ifndef wxHAS_IMAGES_IN_RESOURCES
22 #include "../sample.xpm"
23 #endif
24
25
26 #define USE_ASYNCHRONOUS_CLIPBOARD_REQUEST 0
27
28 class MyApp : public wxApp
29 {
30 public:
31 virtual bool OnInit() wxOVERRIDE;
32 };
33
34 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
35 enum AsyncRequestState
36 {
37 Idle,
38 Waiting,
39 Finished
40 };
41 #endif
42
43 class MyFrame : public wxFrame
44 {
45 public:
46 MyFrame(const wxString& title);
47
48 void OnQuit(wxCommandEvent&event);
49 void OnAbout(wxCommandEvent&event);
50 void OnFlush(wxCommandEvent &event);
51 void OnWriteClipboardContents(wxCommandEvent&event);
52 void OnUpdateUI(wxUpdateUIEvent&event);
53 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
54 void OnClipboardChange(wxClipboardEvent&event);
55 #endif
56
57 private:
58 wxTextCtrl *m_textctrl;
59 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
60 AsyncRequestState m_request;
61 bool m_clipboardSupportsText;
62 #endif
63
64 wxDECLARE_EVENT_TABLE();
65 };
66
67 enum
68 {
69 ID_Quit = wxID_EXIT,
70 ID_About = wxID_ABOUT,
71 ID_Write = 100,
72 ID_Text = 101,
73 ID_Flush = 102
74 };
75
76 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
77 EVT_MENU(ID_Quit, MyFrame::OnQuit)
78 EVT_MENU(ID_About, MyFrame::OnAbout)
79 EVT_MENU(ID_Flush, MyFrame::OnFlush)
80 EVT_BUTTON(ID_Write, MyFrame::OnWriteClipboardContents)
81 EVT_UPDATE_UI(ID_Write, MyFrame::OnUpdateUI)
82 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
83 EVT_CLIPBOARD_CHANGED(MyFrame::OnClipboardChange)
84 #endif
85 wxEND_EVENT_TABLE()
86
87 wxIMPLEMENT_APP(MyApp);
88
OnInit()89 bool MyApp::OnInit()
90 {
91 if ( !wxApp::OnInit() )
92 return false;
93
94 MyFrame *frame = new MyFrame("wxClipboard sample");
95 frame->Show(true);
96
97 return true;
98 }
99
MyFrame(const wxString & title)100 MyFrame::MyFrame(const wxString& title)
101 : wxFrame(NULL, wxID_ANY, title)
102 {
103 // set the frame icon
104 SetIcon(wxICON(sample));
105
106 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
107 m_request = Idle;
108 m_clipboardSupportsText = false;
109 #endif
110
111 #if wxUSE_MENUS
112 // create a menu bar
113 wxMenu *fileMenu = new wxMenu;
114
115 // the "About" item should be in the help menu
116 wxMenu *helpMenu = new wxMenu;
117 helpMenu->Append(ID_About, "&About\tF1", "Show about dialog");
118
119 fileMenu->Append(ID_Flush, "Flush the clipboard" );
120 fileMenu->Append(ID_Quit, "E&xit\tAlt-X", "Quit this program");
121
122 // now append the freshly created menu to the menu bar...
123 wxMenuBar *menuBar = new wxMenuBar();
124 menuBar->Append(fileMenu, "&File");
125 menuBar->Append(helpMenu, "&Help");
126
127 // ... and attach this menu bar to the frame
128 SetMenuBar(menuBar);
129 #endif // wxUSE_MENUS
130
131 wxPanel *panel = new wxPanel( this, -1 );
132
133 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
134 main_sizer->Add( new wxButton( panel, ID_Write, "Get clipboard text" ), 0, wxALL, 5 );
135 m_textctrl = new wxTextCtrl( panel, ID_Text, "", wxDefaultPosition,
136 wxDefaultSize, wxTE_MULTILINE );
137 main_sizer->Add( m_textctrl, 1, wxGROW );
138 panel->SetSizer( main_sizer );
139 }
140
OnFlush(wxCommandEvent & WXUNUSED (event))141 void MyFrame::OnFlush(wxCommandEvent &WXUNUSED(event))
142 {
143 wxClipboardLocker clipLock;
144
145 if ( !clipLock )
146 {
147 m_textctrl->AppendText("Failed to lock clipboard.\n");
148 return;
149 }
150
151 wxString clipData = wxString::Format("Text from wx clipboard sample at %s" , wxDateTime::Now().Format());
152 if ( !wxTheClipboard->AddData(new wxTextDataObject(clipData)) )
153 {
154 m_textctrl->AppendText("Failed to put text on clipboard.\n");
155 return;
156 }
157
158 if ( !wxTheClipboard->Flush() )
159 {
160 m_textctrl->AppendText("Failed to flush clipboard.\n");
161 return;
162 }
163
164 m_textctrl->AppendText("Clipboard flushed successfully, you should now "
165 "be able to paste text even after closing the sample.");
166 }
167
OnWriteClipboardContents(wxCommandEvent & WXUNUSED (event))168 void MyFrame::OnWriteClipboardContents(wxCommandEvent& WXUNUSED(event))
169 {
170 if (wxTheClipboard->Open())
171 {
172 if (wxTheClipboard->IsSupported( wxDF_UNICODETEXT ))
173 {
174 wxTextDataObject data;
175 wxTheClipboard->GetData( data );
176 m_textctrl->Clear();
177 m_textctrl->SetValue( data.GetText() );
178
179 }
180 wxTheClipboard->Close();
181 }
182 }
183
184 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
OnClipboardChange(wxClipboardEvent & event)185 void MyFrame::OnClipboardChange(wxClipboardEvent&event)
186 {
187 m_clipboardSupportsText = event.SupportsFormat( wxDF_UNICODETEXT );
188 m_request = Finished;
189 }
190 #endif
191
OnUpdateUI(wxUpdateUIEvent & event)192 void MyFrame::OnUpdateUI(wxUpdateUIEvent&event)
193 {
194 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
195 if (m_request == Idle)
196 {
197 if (!wxTheClipboard->IsSupportedAsync( this ))
198 {
199 // request failed, try again later
200 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
201 return;
202 }
203 m_request = Waiting;
204 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
205 }
206 else if (m_request == Waiting)
207 {
208 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
209 }
210 else if (m_request == Finished)
211 {
212 event.Enable( m_clipboardSupportsText );
213 m_request = Idle;
214 }
215 #else
216 event.Enable( wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) );
217 #endif
218 }
219
OnQuit(wxCommandEvent & WXUNUSED (event))220 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
221 {
222 // true is to force the frame to close
223 Close(true);
224 }
225
OnAbout(wxCommandEvent & WXUNUSED (event))226 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
227 {
228 wxMessageBox("Clipboard sample", "About clipboard", wxOK|wxICON_INFORMATION, this);
229 }
230
231
232