]>
Commit | Line | Data |
---|---|---|
e28b7c29 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: clipboard.cpp | |
d48b06bd | 3 | // Purpose: clipboard wxWidgets sample |
e28b7c29 RR |
4 | // Author: Robert Roebling |
5 | // RCS-ID: $Id: minimal.cpp 53461 2008-05-05 23:30:33Z VZ $ | |
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" | |
d48b06bd | 12 | |
e28b7c29 RR |
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 | #if !defined(__WXMSW__) && !defined(__WXPM__) | |
26 | #include "../sample.xpm" | |
27 | #endif | |
28 | ||
311c1be9 | 29 | |
d48b06bd | 30 | #define USE_ASYNCHRONOUS_CLIPBOARD_REQUEST 0 |
311c1be9 | 31 | |
e28b7c29 RR |
32 | class MyApp : public wxApp |
33 | { | |
34 | public: | |
35 | virtual bool OnInit(); | |
36 | }; | |
37 | ||
311c1be9 | 38 | #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST |
e28b7c29 RR |
39 | enum AsyncRequestState |
40 | { | |
41 | Idle, | |
42 | Waiting, | |
43 | Finished | |
44 | }; | |
311c1be9 | 45 | #endif |
e28b7c29 RR |
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); | |
311c1be9 | 56 | #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST |
e28b7c29 | 57 | void OnClipboardChange(wxClipboardEvent&event); |
311c1be9 | 58 | #endif |
e28b7c29 RR |
59 | |
60 | private: | |
61 | wxTextCtrl *m_textctrl; | |
311c1be9 | 62 | #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST |
e28b7c29 RR |
63 | AsyncRequestState m_request; |
64 | bool m_clipboardSupportsText; | |
311c1be9 | 65 | #endif |
e28b7c29 RR |
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) | |
311c1be9 | 83 | #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST |
e28b7c29 | 84 | EVT_CLIPBOARD_CHANGED(MyFrame::OnClipboardChange) |
311c1be9 | 85 | #endif |
e28b7c29 RR |
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); | |
d48b06bd | 97 | |
e28b7c29 RR |
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)); | |
d48b06bd | 106 | |
311c1be9 | 107 | #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST |
e28b7c29 RR |
108 | m_request = Idle; |
109 | m_clipboardSupportsText = false; | |
311c1be9 | 110 | #endif |
e28b7c29 RR |
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 ); | |
d48b06bd | 132 | |
e28b7c29 RR |
133 | wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL ); |
134 | main_sizer->Add( new wxButton( panel, ID_Write, "Get clipboard text" ) ); | |
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 | ||
311c1be9 | 141 | void MyFrame::OnWriteClipboardContents(wxCommandEvent& WXUNUSED(event)) |
e28b7c29 RR |
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() ); | |
d48b06bd | 151 | |
e28b7c29 RR |
152 | } |
153 | wxTheClipboard->Close(); | |
154 | } | |
155 | } | |
156 | ||
311c1be9 | 157 | #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST |
e28b7c29 RR |
158 | void MyFrame::OnClipboardChange(wxClipboardEvent&event) |
159 | { | |
160 | m_clipboardSupportsText = event.SupportsFormat( wxDF_UNICODETEXT ); | |
161 | m_request = Finished; | |
162 | } | |
311c1be9 | 163 | #endif |
e28b7c29 RR |
164 | |
165 | void MyFrame::OnUpdateUI(wxUpdateUIEvent&event) | |
166 | { | |
311c1be9 | 167 | #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST |
e28b7c29 RR |
168 | if (m_request == Idle) |
169 | { | |
b4705641 RR |
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 | } | |
e28b7c29 RR |
176 | m_request = Waiting; |
177 | event.Enable( m_clipboardSupportsText ); // not yet known, assume last value | |
d48b06bd | 178 | } |
e28b7c29 RR |
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 | } | |
311c1be9 RR |
188 | #else |
189 | event.Enable( wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) ); | |
190 | #endif | |
e28b7c29 RR |
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 | { | |
d48b06bd | 201 | wxMessageBox("Clipboard sample", "About clipboard", wxOK|wxICON_INFORMATION, this); |
e28b7c29 RR |
202 | } |
203 | ||
204 |