1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: clipboard wxWidgets sample
4 // Author: Robert Roebling
5 // Copyright: (c) Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx/wx.h".
10 #include "wx/wxprec.h"
16 // for all others, include the necessary headers (this file is usually all you
17 // need because it includes almost all "standard" wxWidgets headers)
22 #include "wx/clipbrd.h"
24 #ifndef wxHAS_IMAGES_IN_RESOURCES
25 #include "../sample.xpm"
29 #define USE_ASYNCHRONOUS_CLIPBOARD_REQUEST 0
31 class MyApp
: public wxApp
34 virtual bool OnInit();
37 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
38 enum AsyncRequestState
46 class MyFrame
: public wxFrame
49 MyFrame(const wxString
& title
);
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
);
60 wxTextCtrl
*m_textctrl
;
61 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
62 AsyncRequestState m_request
;
63 bool m_clipboardSupportsText
;
72 ID_About
= wxID_ABOUT
,
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
)
91 if ( !wxApp::OnInit() )
94 MyFrame
*frame
= new MyFrame("wxClipboard sample");
100 MyFrame::MyFrame(const wxString
& title
)
101 : wxFrame(NULL
, wxID_ANY
, title
)
103 // set the frame icon
104 SetIcon(wxICON(sample
));
106 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
108 m_clipboardSupportsText
= false;
113 wxMenu
*fileMenu
= new wxMenu
;
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");
119 fileMenu
->Append(ID_Quit
, "E&xit\tAlt-X", "Quit this program");
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");
126 // ... and attach this menu bar to the frame
128 #endif // wxUSE_MENUS
130 wxPanel
*panel
= new wxPanel( this, -1 );
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
);
140 void MyFrame::OnWriteClipboardContents(wxCommandEvent
& WXUNUSED(event
))
142 if (wxTheClipboard
->Open())
144 if (wxTheClipboard
->IsSupported( wxDF_UNICODETEXT
))
146 wxTextDataObject data
;
147 wxTheClipboard
->GetData( data
);
149 m_textctrl
->SetValue( data
.GetText() );
152 wxTheClipboard
->Close();
156 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
157 void MyFrame::OnClipboardChange(wxClipboardEvent
&event
)
159 m_clipboardSupportsText
= event
.SupportsFormat( wxDF_UNICODETEXT
);
160 m_request
= Finished
;
164 void MyFrame::OnUpdateUI(wxUpdateUIEvent
&event
)
166 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
167 if (m_request
== Idle
)
169 if (!wxTheClipboard
->IsSupportedAsync( this ))
171 // request failed, try again later
172 event
.Enable( m_clipboardSupportsText
); // not yet known, assume last value
176 event
.Enable( m_clipboardSupportsText
); // not yet known, assume last value
178 else if (m_request
== Waiting
)
180 event
.Enable( m_clipboardSupportsText
); // not yet known, assume last value
182 else if (m_request
== Finished
)
184 event
.Enable( m_clipboardSupportsText
);
188 event
.Enable( wxTheClipboard
->IsSupported( wxDF_UNICODETEXT
) );
192 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
194 // true is to force the frame to close
198 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
200 wxMessageBox("Clipboard sample", "About clipboard", wxOK
|wxICON_INFORMATION
, this);