]> git.saurik.com Git - wxWidgets.git/blob - samples/clipboard/clipboard.cpp
Fix a crash in wxExecute() in wxMSW too.
[wxWidgets.git] / samples / clipboard / clipboard.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: clipboard.cpp
3 // Purpose: clipboard wxWidgets sample
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 // for all others, include the necessary headers (this file is usually all you
18 // need because it includes almost all "standard" wxWidgets headers)
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/clipbrd.h"
24
25 #ifndef wxHAS_IMAGES_IN_RESOURCES
26 #include "../sample.xpm"
27 #endif
28
29
30 #define USE_ASYNCHRONOUS_CLIPBOARD_REQUEST 0
31
32 class MyApp : public wxApp
33 {
34 public:
35 virtual bool OnInit();
36 };
37
38 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
39 enum AsyncRequestState
40 {
41 Idle,
42 Waiting,
43 Finished
44 };
45 #endif
46
47 class MyFrame : public wxFrame
48 {
49 public:
50 MyFrame(const wxString& title);
51
52 void OnQuit(wxCommandEvent&event);
53 void OnAbout(wxCommandEvent&event);
54 void OnWriteClipboardContents(wxCommandEvent&event);
55 void OnUpdateUI(wxUpdateUIEvent&event);
56 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
57 void OnClipboardChange(wxClipboardEvent&event);
58 #endif
59
60 private:
61 wxTextCtrl *m_textctrl;
62 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
63 AsyncRequestState m_request;
64 bool m_clipboardSupportsText;
65 #endif
66
67 DECLARE_EVENT_TABLE()
68 };
69
70 enum
71 {
72 ID_Quit = wxID_EXIT,
73 ID_About = wxID_ABOUT,
74 ID_Write = 100,
75 ID_Text = 101
76 };
77
78 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
79 EVT_MENU(ID_Quit, MyFrame::OnQuit)
80 EVT_MENU(ID_About, MyFrame::OnAbout)
81 EVT_BUTTON(ID_Write, MyFrame::OnWriteClipboardContents)
82 EVT_UPDATE_UI(ID_Write, MyFrame::OnUpdateUI)
83 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
84 EVT_CLIPBOARD_CHANGED(MyFrame::OnClipboardChange)
85 #endif
86 END_EVENT_TABLE()
87
88 IMPLEMENT_APP(MyApp)
89
90 bool MyApp::OnInit()
91 {
92 if ( !wxApp::OnInit() )
93 return false;
94
95 MyFrame *frame = new MyFrame("wxClipboard sample");
96 frame->Show(true);
97
98 return true;
99 }
100
101 MyFrame::MyFrame(const wxString& title)
102 : wxFrame(NULL, wxID_ANY, title)
103 {
104 // set the frame icon
105 SetIcon(wxICON(sample));
106
107 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
108 m_request = Idle;
109 m_clipboardSupportsText = false;
110 #endif
111
112 #if wxUSE_MENUS
113 // create a menu bar
114 wxMenu *fileMenu = new wxMenu;
115
116 // the "About" item should be in the help menu
117 wxMenu *helpMenu = new wxMenu;
118 helpMenu->Append(ID_About, "&About\tF1", "Show about dialog");
119
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
141 void MyFrame::OnWriteClipboardContents(wxCommandEvent& WXUNUSED(event))
142 {
143 if (wxTheClipboard->Open())
144 {
145 if (wxTheClipboard->IsSupported( wxDF_UNICODETEXT ))
146 {
147 wxTextDataObject data;
148 wxTheClipboard->GetData( data );
149 m_textctrl->Clear();
150 m_textctrl->SetValue( data.GetText() );
151
152 }
153 wxTheClipboard->Close();
154 }
155 }
156
157 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
158 void MyFrame::OnClipboardChange(wxClipboardEvent&event)
159 {
160 m_clipboardSupportsText = event.SupportsFormat( wxDF_UNICODETEXT );
161 m_request = Finished;
162 }
163 #endif
164
165 void MyFrame::OnUpdateUI(wxUpdateUIEvent&event)
166 {
167 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
168 if (m_request == Idle)
169 {
170 if (!wxTheClipboard->IsSupportedAsync( this ))
171 {
172 // request failed, try again later
173 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
174 return;
175 }
176 m_request = Waiting;
177 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
178 }
179 else if (m_request == Waiting)
180 {
181 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
182 }
183 else if (m_request == Finished)
184 {
185 event.Enable( m_clipboardSupportsText );
186 m_request = Idle;
187 }
188 #else
189 event.Enable( wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) );
190 #endif
191 }
192
193 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
194 {
195 // true is to force the frame to close
196 Close(true);
197 }
198
199 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
200 {
201 wxMessageBox("Clipboard sample", "About clipboard", wxOK|wxICON_INFORMATION, this);
202 }
203
204