1 /////////////////////////////////////////////////////////////////////////////
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 /////////////////////////////////////////////////////////////////////////////
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"
29 class MyApp
: public wxApp
32 virtual bool OnInit();
35 enum AsyncRequestState
42 class MyFrame
: public wxFrame
45 MyFrame(const wxString
& title
);
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
);
54 wxTextCtrl
*m_textctrl
;
55 AsyncRequestState m_request
;
56 bool m_clipboardSupportsText
;
64 ID_About
= wxID_ABOUT
,
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
)
81 if ( !wxApp::OnInit() )
84 MyFrame
*frame
= new MyFrame("wxClipboard sample");
90 MyFrame::MyFrame(const wxString
& title
)
91 : wxFrame(NULL
, wxID_ANY
, title
)
94 SetIcon(wxICON(sample
));
97 m_clipboardSupportsText
= false;
101 wxMenu
*fileMenu
= new wxMenu
;
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");
107 fileMenu
->Append(ID_Quit
, "E&xit\tAlt-X", "Quit this program");
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");
114 // ... and attach this menu bar to the frame
116 #endif // wxUSE_MENUS
118 wxPanel
*panel
= new wxPanel( this, -1 );
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
);
128 void MyFrame::OnWriteClipboardContents(wxCommandEvent
& event
)
130 if (wxTheClipboard
->Open())
132 if (wxTheClipboard
->IsSupported( wxDF_UNICODETEXT
))
134 wxTextDataObject data
;
135 wxTheClipboard
->GetData( data
);
137 m_textctrl
->SetValue( data
.GetText() );
140 wxTheClipboard
->Close();
144 void MyFrame::OnClipboardChange(wxClipboardEvent
&event
)
146 m_clipboardSupportsText
= event
.SupportsFormat( wxDF_UNICODETEXT
);
147 m_request
= Finished
;
150 void MyFrame::OnUpdateUI(wxUpdateUIEvent
&event
)
152 if (m_request
== Idle
)
154 wxTheClipboard
->IsSupportedAsync( this );
156 event
.Enable( m_clipboardSupportsText
); // not yet known, assume last value
158 else if (m_request
== Waiting
)
160 event
.Enable( m_clipboardSupportsText
); // not yet known, assume last value
162 else if (m_request
== Finished
)
164 event
.Enable( m_clipboardSupportsText
);
169 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
171 // true is to force the frame to close
175 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))