1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: clipboard wxWidgets sample
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
17 // for all others, include the necessary headers (this file is usually all you
18 // need because it includes almost all "standard" wxWidgets headers)
23 #include "wx/clipbrd.h"
25 #if !defined(__WXMSW__) && !defined(__WXPM__)
26 #include "../sample.xpm"
30 #define USE_ASYNCHRONOUS_CLIPBOARD_REQUEST 0
32 class MyApp
: public wxApp
35 virtual bool OnInit();
38 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
39 enum AsyncRequestState
47 class MyFrame
: public wxFrame
50 MyFrame(const wxString
& title
);
52 void OnQuit(wxCommandEvent
&event
);
53 void OnAbout(wxCommandEvent
&event
);
54 void OnWriteClipboardContents(wxCommandEvent
&event
);
55 void OnUpdateUI(wxUpdateUIEvent
&event
);
56 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
57 void OnClipboardChange(wxClipboardEvent
&event
);
61 wxTextCtrl
*m_textctrl
;
62 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
63 AsyncRequestState m_request
;
64 bool m_clipboardSupportsText
;
73 ID_About
= wxID_ABOUT
,
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
)
83 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
84 EVT_CLIPBOARD_CHANGED(MyFrame::OnClipboardChange
)
92 if ( !wxApp::OnInit() )
95 MyFrame
*frame
= new MyFrame("wxClipboard sample");
101 MyFrame::MyFrame(const wxString
& title
)
102 : wxFrame(NULL
, wxID_ANY
, title
)
104 // set the frame icon
105 SetIcon(wxICON(sample
));
107 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
109 m_clipboardSupportsText
= false;
114 wxMenu
*fileMenu
= new wxMenu
;
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");
120 fileMenu
->Append(ID_Quit
, "E&xit\tAlt-X", "Quit this program");
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");
127 // ... and attach this menu bar to the frame
129 #endif // wxUSE_MENUS
131 wxPanel
*panel
= new wxPanel( this, -1 );
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
);
141 void MyFrame::OnWriteClipboardContents(wxCommandEvent
& WXUNUSED(event
))
143 if (wxTheClipboard
->Open())
145 if (wxTheClipboard
->IsSupported( wxDF_UNICODETEXT
))
147 wxTextDataObject data
;
148 wxTheClipboard
->GetData( data
);
150 m_textctrl
->SetValue( data
.GetText() );
153 wxTheClipboard
->Close();
157 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
158 void MyFrame::OnClipboardChange(wxClipboardEvent
&event
)
160 m_clipboardSupportsText
= event
.SupportsFormat( wxDF_UNICODETEXT
);
161 m_request
= Finished
;
165 void MyFrame::OnUpdateUI(wxUpdateUIEvent
&event
)
167 #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
168 if (m_request
== Idle
)
170 if (!wxTheClipboard
->IsSupportedAsync( this ))
172 // request failed, try again later
173 event
.Enable( m_clipboardSupportsText
); // not yet known, assume last value
177 event
.Enable( m_clipboardSupportsText
); // not yet known, assume last value
179 else if (m_request
== Waiting
)
181 event
.Enable( m_clipboardSupportsText
); // not yet known, assume last value
183 else if (m_request
== Finished
)
185 event
.Enable( m_clipboardSupportsText
);
189 event
.Enable( wxTheClipboard
->IsSupported( wxDF_UNICODETEXT
) );
193 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
195 // true is to force the frame to close
199 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
201 wxMessageBox("Clipboard sample", "About clipboard", wxOK
|wxICON_INFORMATION
, this);