Added clipbaord sample
[wxWidgets.git] / samples / clipboard / clipboard.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: clipboard.cpp
3 // Purpose: clipbaord wxWidgets sample
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"
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 #if !defined(__WXMSW__) && !defined(__WXPM__)
26 #include "../sample.xpm"
27 #endif
28
29 class MyApp : public wxApp
30 {
31 public:
32 virtual bool OnInit();
33 };
34
35 enum AsyncRequestState
36 {
37 Idle,
38 Waiting,
39 Finished
40 };
41
42 class MyFrame : public wxFrame
43 {
44 public:
45 MyFrame(const wxString& title);
46
47 void OnQuit(wxCommandEvent&event);
48 void OnAbout(wxCommandEvent&event);
49 void OnWriteClipboardContents(wxCommandEvent&event);
50 void OnUpdateUI(wxUpdateUIEvent&event);
51 void OnClipboardChange(wxClipboardEvent&event);
52
53 private:
54 wxTextCtrl *m_textctrl;
55 AsyncRequestState m_request;
56 bool m_clipboardSupportsText;
57
58 DECLARE_EVENT_TABLE()
59 };
60
61 enum
62 {
63 ID_Quit = wxID_EXIT,
64 ID_About = wxID_ABOUT,
65 ID_Write = 100,
66 ID_Text = 101
67 };
68
69 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
70 EVT_MENU(ID_Quit, MyFrame::OnQuit)
71 EVT_MENU(ID_About, MyFrame::OnAbout)
72 EVT_BUTTON(ID_Write, MyFrame::OnWriteClipboardContents)
73 EVT_UPDATE_UI(ID_Write, MyFrame::OnUpdateUI)
74 EVT_CLIPBOARD_CHANGED(MyFrame::OnClipboardChange)
75 END_EVENT_TABLE()
76
77 IMPLEMENT_APP(MyApp)
78
79 bool MyApp::OnInit()
80 {
81 if ( !wxApp::OnInit() )
82 return false;
83
84 MyFrame *frame = new MyFrame("wxClipboard sample");
85 frame->Show(true);
86
87 return true;
88 }
89
90 MyFrame::MyFrame(const wxString& title)
91 : wxFrame(NULL, wxID_ANY, title)
92 {
93 // set the frame icon
94 SetIcon(wxICON(sample));
95
96 m_request = Idle;
97 m_clipboardSupportsText = false;
98
99 #if wxUSE_MENUS
100 // create a menu bar
101 wxMenu *fileMenu = new wxMenu;
102
103 // the "About" item should be in the help menu
104 wxMenu *helpMenu = new wxMenu;
105 helpMenu->Append(ID_About, "&About...\tF1", "Show about dialog");
106
107 fileMenu->Append(ID_Quit, "E&xit\tAlt-X", "Quit this program");
108
109 // now append the freshly created menu to the menu bar...
110 wxMenuBar *menuBar = new wxMenuBar();
111 menuBar->Append(fileMenu, "&File");
112 menuBar->Append(helpMenu, "&Help");
113
114 // ... and attach this menu bar to the frame
115 SetMenuBar(menuBar);
116 #endif // wxUSE_MENUS
117
118 wxPanel *panel = new wxPanel( this, -1 );
119
120 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
121 main_sizer->Add( new wxButton( panel, ID_Write, "Get clipboard text" ) );
122 m_textctrl = new wxTextCtrl( panel, ID_Text, "", wxDefaultPosition,
123 wxDefaultSize, wxTE_MULTILINE );
124 main_sizer->Add( m_textctrl, 1, wxGROW );
125 panel->SetSizer( main_sizer );
126 }
127
128 void MyFrame::OnWriteClipboardContents(wxCommandEvent& event)
129 {
130 if (wxTheClipboard->Open())
131 {
132 if (wxTheClipboard->IsSupported( wxDF_UNICODETEXT ))
133 {
134 wxTextDataObject data;
135 wxTheClipboard->GetData( data );
136 m_textctrl->Clear();
137 m_textctrl->SetValue( data.GetText() );
138
139 }
140 wxTheClipboard->Close();
141 }
142 }
143
144 void MyFrame::OnClipboardChange(wxClipboardEvent&event)
145 {
146 m_clipboardSupportsText = event.SupportsFormat( wxDF_UNICODETEXT );
147 m_request = Finished;
148 }
149
150 void MyFrame::OnUpdateUI(wxUpdateUIEvent&event)
151 {
152 if (m_request == Idle)
153 {
154 wxTheClipboard->IsSupportedAsync( this );
155 m_request = Waiting;
156 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
157 }
158 else if (m_request == Waiting)
159 {
160 event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
161 }
162 else if (m_request == Finished)
163 {
164 event.Enable( m_clipboardSupportsText );
165 m_request = Idle;
166 }
167 }
168
169 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
170 {
171 // true is to force the frame to close
172 Close(true);
173 }
174
175 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
176 {
177 }
178
179