]> git.saurik.com Git - wxWidgets.git/blame - samples/clipboard/clipboard.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / samples / clipboard / clipboard.cpp
CommitLineData
e28b7c29
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: clipboard.cpp
d48b06bd 3// Purpose: clipboard wxWidgets sample
e28b7c29 4// Author: Robert Roebling
e28b7c29
RR
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"
d48b06bd 11
e28b7c29
RR
12#ifdef __BORLANDC__
13 #pragma hdrstop
14#endif
15
16// for all others, include the necessary headers (this file is usually all you
17// need because it includes almost all "standard" wxWidgets headers)
18#ifndef WX_PRECOMP
19 #include "wx/wx.h"
20#endif
21
22#include "wx/clipbrd.h"
23
e7092398 24#ifndef wxHAS_IMAGES_IN_RESOURCES
e28b7c29
RR
25 #include "../sample.xpm"
26#endif
27
311c1be9 28
d48b06bd 29#define USE_ASYNCHRONOUS_CLIPBOARD_REQUEST 0
311c1be9 30
e28b7c29
RR
31class MyApp : public wxApp
32{
33public:
34 virtual bool OnInit();
35};
36
311c1be9 37#if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
e28b7c29
RR
38enum AsyncRequestState
39{
40 Idle,
41 Waiting,
42 Finished
43};
311c1be9 44#endif
e28b7c29
RR
45
46class MyFrame : public wxFrame
47{
48public:
49 MyFrame(const wxString& title);
50
51 void OnQuit(wxCommandEvent&event);
52 void OnAbout(wxCommandEvent&event);
53 void OnWriteClipboardContents(wxCommandEvent&event);
54 void OnUpdateUI(wxUpdateUIEvent&event);
311c1be9 55#if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
e28b7c29 56 void OnClipboardChange(wxClipboardEvent&event);
311c1be9 57#endif
e28b7c29
RR
58
59private:
60 wxTextCtrl *m_textctrl;
311c1be9 61#if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
e28b7c29
RR
62 AsyncRequestState m_request;
63 bool m_clipboardSupportsText;
311c1be9 64#endif
e28b7c29
RR
65
66 DECLARE_EVENT_TABLE()
67};
68
69enum
70{
71 ID_Quit = wxID_EXIT,
72 ID_About = wxID_ABOUT,
73 ID_Write = 100,
74 ID_Text = 101
75};
76
77BEGIN_EVENT_TABLE(MyFrame, wxFrame)
78 EVT_MENU(ID_Quit, MyFrame::OnQuit)
79 EVT_MENU(ID_About, MyFrame::OnAbout)
80 EVT_BUTTON(ID_Write, MyFrame::OnWriteClipboardContents)
81 EVT_UPDATE_UI(ID_Write, MyFrame::OnUpdateUI)
311c1be9 82#if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
e28b7c29 83 EVT_CLIPBOARD_CHANGED(MyFrame::OnClipboardChange)
311c1be9 84#endif
e28b7c29
RR
85END_EVENT_TABLE()
86
87IMPLEMENT_APP(MyApp)
88
89bool MyApp::OnInit()
90{
91 if ( !wxApp::OnInit() )
92 return false;
93
94 MyFrame *frame = new MyFrame("wxClipboard sample");
95 frame->Show(true);
d48b06bd 96
e28b7c29
RR
97 return true;
98}
99
100MyFrame::MyFrame(const wxString& title)
101 : wxFrame(NULL, wxID_ANY, title)
102{
103 // set the frame icon
104 SetIcon(wxICON(sample));
d48b06bd 105
311c1be9 106#if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
e28b7c29
RR
107 m_request = Idle;
108 m_clipboardSupportsText = false;
311c1be9 109#endif
e28b7c29
RR
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;
2d143b66 117 helpMenu->Append(ID_About, "&About\tF1", "Show about dialog");
e28b7c29
RR
118
119 fileMenu->Append(ID_Quit, "E&xit\tAlt-X", "Quit this program");
120
121 // now append the freshly created menu to the menu bar...
122 wxMenuBar *menuBar = new wxMenuBar();
123 menuBar->Append(fileMenu, "&File");
124 menuBar->Append(helpMenu, "&Help");
125
126 // ... and attach this menu bar to the frame
127 SetMenuBar(menuBar);
128#endif // wxUSE_MENUS
129
130 wxPanel *panel = new wxPanel( this, -1 );
d48b06bd 131
e28b7c29 132 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
d159e964 133 main_sizer->Add( new wxButton( panel, ID_Write, "Get clipboard text" ), 0, wxALL, 5 );
e28b7c29
RR
134 m_textctrl = new wxTextCtrl( panel, ID_Text, "", wxDefaultPosition,
135 wxDefaultSize, wxTE_MULTILINE );
136 main_sizer->Add( m_textctrl, 1, wxGROW );
137 panel->SetSizer( main_sizer );
138}
139
311c1be9 140void MyFrame::OnWriteClipboardContents(wxCommandEvent& WXUNUSED(event))
e28b7c29
RR
141{
142 if (wxTheClipboard->Open())
143 {
144 if (wxTheClipboard->IsSupported( wxDF_UNICODETEXT ))
145 {
146 wxTextDataObject data;
147 wxTheClipboard->GetData( data );
148 m_textctrl->Clear();
149 m_textctrl->SetValue( data.GetText() );
d48b06bd 150
e28b7c29
RR
151 }
152 wxTheClipboard->Close();
153 }
154}
155
311c1be9 156#if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
e28b7c29
RR
157void MyFrame::OnClipboardChange(wxClipboardEvent&event)
158{
159 m_clipboardSupportsText = event.SupportsFormat( wxDF_UNICODETEXT );
160 m_request = Finished;
161}
311c1be9 162#endif
e28b7c29
RR
163
164void MyFrame::OnUpdateUI(wxUpdateUIEvent&event)
165{
311c1be9 166#if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
e28b7c29
RR
167 if (m_request == Idle)
168 {
b4705641
RR
169 if (!wxTheClipboard->IsSupportedAsync( this ))
170 {
171 // request failed, try again later
172 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
173 return;
174 }
e28b7c29
RR
175 m_request = Waiting;
176 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
d48b06bd 177 }
e28b7c29
RR
178 else if (m_request == Waiting)
179 {
180 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
181 }
182 else if (m_request == Finished)
183 {
184 event.Enable( m_clipboardSupportsText );
185 m_request = Idle;
186 }
311c1be9
RR
187#else
188 event.Enable( wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) );
189#endif
e28b7c29
RR
190}
191
192void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
193{
194 // true is to force the frame to close
195 Close(true);
196}
197
198void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
199{
d48b06bd 200 wxMessageBox("Clipboard sample", "About clipboard", wxOK|wxICON_INFORMATION, this);
e28b7c29
RR
201}
202
203