clipboard.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // For compilers that support precompilation, includes "wx/wx.h".
  9. #include "wx/wxprec.h"
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. // for all others, include the necessary headers (this file is usually all you
  14. // need because it includes almost all "standard" wxWidgets headers)
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif
  18. #include "wx/clipbrd.h"
  19. #ifndef wxHAS_IMAGES_IN_RESOURCES
  20. #include "../sample.xpm"
  21. #endif
  22. #define USE_ASYNCHRONOUS_CLIPBOARD_REQUEST 0
  23. class MyApp : public wxApp
  24. {
  25. public:
  26. virtual bool OnInit();
  27. };
  28. #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
  29. enum AsyncRequestState
  30. {
  31. Idle,
  32. Waiting,
  33. Finished
  34. };
  35. #endif
  36. class MyFrame : public wxFrame
  37. {
  38. public:
  39. MyFrame(const wxString& title);
  40. void OnQuit(wxCommandEvent&event);
  41. void OnAbout(wxCommandEvent&event);
  42. void OnWriteClipboardContents(wxCommandEvent&event);
  43. void OnUpdateUI(wxUpdateUIEvent&event);
  44. #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
  45. void OnClipboardChange(wxClipboardEvent&event);
  46. #endif
  47. private:
  48. wxTextCtrl *m_textctrl;
  49. #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
  50. AsyncRequestState m_request;
  51. bool m_clipboardSupportsText;
  52. #endif
  53. wxDECLARE_EVENT_TABLE();
  54. };
  55. enum
  56. {
  57. ID_Quit = wxID_EXIT,
  58. ID_About = wxID_ABOUT,
  59. ID_Write = 100,
  60. ID_Text = 101
  61. };
  62. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  63. EVT_MENU(ID_Quit, MyFrame::OnQuit)
  64. EVT_MENU(ID_About, MyFrame::OnAbout)
  65. EVT_BUTTON(ID_Write, MyFrame::OnWriteClipboardContents)
  66. EVT_UPDATE_UI(ID_Write, MyFrame::OnUpdateUI)
  67. #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
  68. EVT_CLIPBOARD_CHANGED(MyFrame::OnClipboardChange)
  69. #endif
  70. wxEND_EVENT_TABLE()
  71. IMPLEMENT_APP(MyApp)
  72. bool MyApp::OnInit()
  73. {
  74. if ( !wxApp::OnInit() )
  75. return false;
  76. MyFrame *frame = new MyFrame("wxClipboard sample");
  77. frame->Show(true);
  78. return true;
  79. }
  80. MyFrame::MyFrame(const wxString& title)
  81. : wxFrame(NULL, wxID_ANY, title)
  82. {
  83. // set the frame icon
  84. SetIcon(wxICON(sample));
  85. #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
  86. m_request = Idle;
  87. m_clipboardSupportsText = false;
  88. #endif
  89. #if wxUSE_MENUS
  90. // create a menu bar
  91. wxMenu *fileMenu = new wxMenu;
  92. // the "About" item should be in the help menu
  93. wxMenu *helpMenu = new wxMenu;
  94. helpMenu->Append(ID_About, "&About\tF1", "Show about dialog");
  95. fileMenu->Append(ID_Quit, "E&xit\tAlt-X", "Quit this program");
  96. // now append the freshly created menu to the menu bar...
  97. wxMenuBar *menuBar = new wxMenuBar();
  98. menuBar->Append(fileMenu, "&File");
  99. menuBar->Append(helpMenu, "&Help");
  100. // ... and attach this menu bar to the frame
  101. SetMenuBar(menuBar);
  102. #endif // wxUSE_MENUS
  103. wxPanel *panel = new wxPanel( this, -1 );
  104. wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
  105. main_sizer->Add( new wxButton( panel, ID_Write, "Get clipboard text" ), 0, wxALL, 5 );
  106. m_textctrl = new wxTextCtrl( panel, ID_Text, "", wxDefaultPosition,
  107. wxDefaultSize, wxTE_MULTILINE );
  108. main_sizer->Add( m_textctrl, 1, wxGROW );
  109. panel->SetSizer( main_sizer );
  110. }
  111. void MyFrame::OnWriteClipboardContents(wxCommandEvent& WXUNUSED(event))
  112. {
  113. if (wxTheClipboard->Open())
  114. {
  115. if (wxTheClipboard->IsSupported( wxDF_UNICODETEXT ))
  116. {
  117. wxTextDataObject data;
  118. wxTheClipboard->GetData( data );
  119. m_textctrl->Clear();
  120. m_textctrl->SetValue( data.GetText() );
  121. }
  122. wxTheClipboard->Close();
  123. }
  124. }
  125. #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
  126. void MyFrame::OnClipboardChange(wxClipboardEvent&event)
  127. {
  128. m_clipboardSupportsText = event.SupportsFormat( wxDF_UNICODETEXT );
  129. m_request = Finished;
  130. }
  131. #endif
  132. void MyFrame::OnUpdateUI(wxUpdateUIEvent&event)
  133. {
  134. #if USE_ASYNCHRONOUS_CLIPBOARD_REQUEST
  135. if (m_request == Idle)
  136. {
  137. if (!wxTheClipboard->IsSupportedAsync( this ))
  138. {
  139. // request failed, try again later
  140. event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
  141. return;
  142. }
  143. m_request = Waiting;
  144. event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
  145. }
  146. else if (m_request == Waiting)
  147. {
  148. event.Enable( m_clipboardSupportsText ); // not yet known, assume last value
  149. }
  150. else if (m_request == Finished)
  151. {
  152. event.Enable( m_clipboardSupportsText );
  153. m_request = Idle;
  154. }
  155. #else
  156. event.Enable( wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) );
  157. #endif
  158. }
  159. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  160. {
  161. // true is to force the frame to close
  162. Close(true);
  163. }
  164. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  165. {
  166. wxMessageBox("Clipboard sample", "About clipboard", wxOK|wxICON_INFORMATION, this);
  167. }