]> git.saurik.com Git - wxWidgets.git/blob - samples/clipboard/clipboard.cpp
Make storing non-trivial data in wxThreadSpecificInfo possible.
[wxWidgets.git] / samples / clipboard / clipboard.cpp
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 #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
24 #ifndef wxHAS_IMAGES_IN_RESOURCES
25 #include "../sample.xpm"
26 #endif
27
28
29 #define USE_ASYNCHRONOUS_CLIPBOARD_REQUEST 0
30
31 class MyApp : public wxApp
32 {
33 public:
34 virtual bool OnInit();
35 };
36
37 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
38 enum AsyncRequestState
39 {
40 Idle,
41 Waiting,
42 Finished
43 };
44 #endif
45
46 class MyFrame : public wxFrame
47 {
48 public:
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);
55 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
56 void OnClipboardChange(wxClipboardEvent&event);
57 #endif
58
59 private:
60 wxTextCtrl *m_textctrl;
61 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
62 AsyncRequestState m_request;
63 bool m_clipboardSupportsText;
64 #endif
65
66 DECLARE_EVENT_TABLE()
67 };
68
69 enum
70 {
71 ID_Quit = wxID_EXIT,
72 ID_About = wxID_ABOUT,
73 ID_Write = 100,
74 ID_Text = 101
75 };
76
77 BEGIN_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)
82 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
83 EVT_CLIPBOARD_CHANGED(MyFrame::OnClipboardChange)
84 #endif
85 END_EVENT_TABLE()
86
87 IMPLEMENT_APP(MyApp)
88
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
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_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 );
131
132 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
133 main_sizer->Add( new wxButton( panel, ID_Write, "Get clipboard text" ), 0, wxALL, 5 );
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
140 void MyFrame::OnWriteClipboardContents(wxCommandEvent& WXUNUSED(event))
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() );
150
151 }
152 wxTheClipboard->Close();
153 }
154 }
155
156 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
157 void MyFrame::OnClipboardChange(wxClipboardEvent&event)
158 {
159 m_clipboardSupportsText = event.SupportsFormat( wxDF_UNICODETEXT );
160 m_request = Finished;
161 }
162 #endif
163
164 void MyFrame::OnUpdateUI(wxUpdateUIEvent&event)
165 {
166 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
167 if (m_request == Idle)
168 {
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 }
175 m_request = Waiting;
176 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
177 }
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 }
187 #else
188 event.Enable( wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) );
189 #endif
190 }
191
192 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
193 {
194 // true is to force the frame to close
195 Close(true);
196 }
197
198 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
199 {
200 wxMessageBox("Clipboard sample", "About clipboard", wxOK|wxICON_INFORMATION, this);
201 }
202
203