ownerdrw.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: ownerdrw.cpp
  3. // Purpose: Owner-draw sample, for Windows
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 13.11.97
  7. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // headers & declarations
  12. // ============================================================================
  13. // For compilers that support precompilation, includes "wx.h".
  14. #include "wx/wxprec.h"
  15. #ifdef __BORLANDC__
  16. #pragma hdrstop
  17. #endif
  18. #ifndef WX_PRECOMP
  19. #include "wx/wx.h"
  20. #endif
  21. #include "wx/ownerdrw.h"
  22. #include "wx/menuitem.h"
  23. #include "wx/checklst.h"
  24. // Define a new application type
  25. class OwnerDrawnApp: public wxApp
  26. {
  27. public:
  28. bool OnInit();
  29. };
  30. // Define a new frame type
  31. class OwnerDrawnFrame : public wxFrame
  32. {
  33. public:
  34. // ctor & dtor
  35. OwnerDrawnFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h);
  36. ~OwnerDrawnFrame(){};
  37. // notifications
  38. void OnQuit (wxCommandEvent& event);
  39. void OnMenuToggle (wxCommandEvent& event);
  40. void OnAbout (wxCommandEvent& event);
  41. void OnListboxSelect (wxCommandEvent& event);
  42. void OnCheckboxToggle (wxCommandEvent& event);
  43. void OnListboxDblClick (wxCommandEvent& event);
  44. bool OnClose () { return true; }
  45. wxDECLARE_EVENT_TABLE();
  46. private:
  47. void InitMenu();
  48. wxCheckListBox *m_pListBox;
  49. wxMenuItem *pAboutItem;
  50. };
  51. enum
  52. {
  53. Menu_Quit = 1,
  54. Menu_First = 100,
  55. Menu_Test1, Menu_Test2, Menu_Test3,
  56. Menu_Bitmap, Menu_Bitmap2,
  57. Menu_Submenu, Menu_Sub1, Menu_Sub2, Menu_Sub3,
  58. Menu_Toggle, Menu_About,
  59. Menu_Drawn1, Menu_Drawn2, Menu_Drawn3, Menu_Drawn4, Menu_Drawn5,
  60. Menu_Native1, Menu_Native2, Menu_Native3, Menu_Native4, Menu_Native5,
  61. Control_First = 1000,
  62. Control_Listbox, Control_Listbox2
  63. };
  64. wxBEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame)
  65. EVT_MENU(Menu_Toggle, OwnerDrawnFrame::OnMenuToggle)
  66. EVT_MENU(Menu_About, OwnerDrawnFrame::OnAbout)
  67. EVT_MENU(Menu_Quit, OwnerDrawnFrame::OnQuit)
  68. EVT_LISTBOX(Control_Listbox, OwnerDrawnFrame::OnListboxSelect)
  69. EVT_CHECKLISTBOX(Control_Listbox, OwnerDrawnFrame::OnCheckboxToggle)
  70. EVT_COMMAND(Control_Listbox, wxEVT_LISTBOX_DCLICK,
  71. OwnerDrawnFrame::OnListboxDblClick)
  72. wxEND_EVENT_TABLE()
  73. IMPLEMENT_APP(OwnerDrawnApp)
  74. // init our app: create windows
  75. bool OwnerDrawnApp::OnInit(void)
  76. {
  77. if ( !wxApp::OnInit() )
  78. return false;
  79. OwnerDrawnFrame *pFrame
  80. = new OwnerDrawnFrame(NULL, wxT("wxWidgets Ownerdraw Sample"),
  81. 50, 50, 450, 340);
  82. return true;
  83. }
  84. // create the menu bar for the main frame
  85. void OwnerDrawnFrame::InitMenu()
  86. {
  87. // Make a menubar
  88. wxMenuItem *pItem;
  89. wxMenu *file_menu = new wxMenu;
  90. #ifndef __WXUNIVERSAL__
  91. wxMenu *sub_menu = new wxMenu;
  92. // vars used for menu construction
  93. wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, false),
  94. fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, true),
  95. fontItalic(12, wxMODERN, wxITALIC, wxBOLD, false),
  96. // should be at least of the size of bitmaps
  97. fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, false);
  98. // sorry for my artistic skills...
  99. wxBitmap bmpBell(wxT("bell")),
  100. bmpSound(wxT("sound")),
  101. bmpNoSound(wxT("nosound")),
  102. bmpInfo(wxT("info")),
  103. bmpInfo_mono(wxT("info_mono"));
  104. // construct submenu
  105. pItem = new wxMenuItem(sub_menu, Menu_Sub1, wxT("Submenu &first"), wxT("large"));
  106. pItem->SetFont(fontLarge);
  107. sub_menu->Append(pItem);
  108. pItem = new wxMenuItem(sub_menu, Menu_Sub2, wxT("Submenu &second"), wxT("italic"),
  109. wxITEM_CHECK);
  110. pItem->SetFont(fontItalic);
  111. sub_menu->Append(pItem);
  112. pItem = new wxMenuItem(sub_menu, Menu_Sub3, wxT("Submenu &third"), wxT("underlined"),
  113. wxITEM_CHECK);
  114. pItem->SetFont(fontUlined);
  115. sub_menu->Append(pItem);
  116. // construct menu
  117. pItem = new wxMenuItem(file_menu, Menu_Test1, wxT("&Uncheckable"), wxT("red item"));
  118. pItem->SetFont(*wxITALIC_FONT);
  119. pItem->SetTextColour(wxColor(255, 0, 0));
  120. file_menu->Append(pItem);
  121. pItem = new wxMenuItem(file_menu, Menu_Test2, wxT("&Checkable"),
  122. wxT("checkable item"), wxITEM_CHECK);
  123. pItem->SetFont(*wxSMALL_FONT);
  124. file_menu->Append(pItem);
  125. file_menu->Check(Menu_Test2, true);
  126. pItem = new wxMenuItem(file_menu, Menu_Test3, wxT("&Disabled"), wxT("disabled item"));
  127. pItem->SetFont(*wxNORMAL_FONT);
  128. file_menu->Append(pItem);
  129. file_menu->Enable(Menu_Test3, false);
  130. file_menu->AppendSeparator();
  131. pItem = new wxMenuItem(file_menu, Menu_Bitmap, wxT("&Bell"),
  132. wxT("check/uncheck me!"), wxITEM_CHECK);
  133. pItem->SetFont(fontBmp);
  134. pItem->SetBitmaps(bmpBell);
  135. file_menu->Append(pItem);
  136. pItem = new wxMenuItem(file_menu, Menu_Bitmap2, wxT("So&und"),
  137. wxT("icon changes!"), wxITEM_CHECK);
  138. pItem->SetFont(fontBmp);
  139. pItem->SetBitmaps(bmpSound, bmpNoSound);
  140. file_menu->Append(pItem);
  141. file_menu->AppendSeparator();
  142. pItem = new wxMenuItem(file_menu, Menu_Submenu, wxT("&Sub menu"), wxT(""),
  143. wxITEM_CHECK, sub_menu);
  144. pItem->SetFont(*wxSWISS_FONT);
  145. file_menu->Append(pItem);
  146. file_menu->AppendSeparator();
  147. pItem = new wxMenuItem(file_menu, Menu_Toggle, wxT("&Disable/Enable\tCtrl+D"),
  148. wxT("enables/disables the About-Item"), wxITEM_NORMAL);
  149. pItem->SetFont(*wxNORMAL_FONT);
  150. file_menu->Append(pItem);
  151. // Of course Ctrl+RatherLongAccel will not work in this example:
  152. pAboutItem = new wxMenuItem(file_menu, Menu_About, wxT("&About\tCtrl+RatherLongAccel"),
  153. wxT("display program information"), wxITEM_NORMAL);
  154. pAboutItem->SetBitmap(bmpInfo);
  155. pAboutItem->SetDisabledBitmap(bmpInfo_mono);
  156. file_menu->Append(pAboutItem);
  157. file_menu->AppendSeparator();
  158. #endif
  159. pItem = new wxMenuItem(file_menu, Menu_Quit, wxT("&Quit"), wxT("Normal item"),
  160. wxITEM_NORMAL);
  161. file_menu->Append(pItem);
  162. wxMenu* drawn_menu = new wxMenu;
  163. pItem = new wxMenuItem(drawn_menu, Menu_Drawn1, wxT("&Menu item\tCtrl+K"));
  164. drawn_menu->Append(pItem);
  165. drawn_menu->AppendSeparator();
  166. pItem = new wxMenuItem(drawn_menu, Menu_Drawn2, wxT("&Checked item"),
  167. wxT("check/uncheck me!"), wxITEM_CHECK);
  168. drawn_menu->Append(pItem);
  169. drawn_menu->Check(Menu_Drawn2, true);
  170. pItem = new wxMenuItem(drawn_menu, Menu_Drawn3, wxT("&Radio item"),
  171. wxT("check/uncheck me!"), wxITEM_RADIO);
  172. drawn_menu->Append(pItem);
  173. drawn_menu->AppendSeparator();
  174. pItem = new wxMenuItem(drawn_menu, Menu_Drawn4, wxT("&Disabled item\tCtrl+RatherLongAccel"),
  175. wxT("disabled item"));
  176. pItem->Enable(false);
  177. drawn_menu->Append(pItem);
  178. pItem = new wxMenuItem(drawn_menu, Menu_Drawn5, wxT("&Other\tCtrl+O"), wxT("other item"));
  179. pItem->SetTextColour(*wxRED);
  180. drawn_menu->Append(pItem);
  181. wxMenu* native_menu = new wxMenu;
  182. pItem = new wxMenuItem(native_menu, Menu_Native1, wxT("&Menu item\tCtrl+K"));
  183. native_menu->Append(pItem);
  184. native_menu->AppendSeparator();
  185. pItem = new wxMenuItem(native_menu, Menu_Native2, wxT("&Checked item"),
  186. wxT("check/uncheck me!"), wxITEM_CHECK);
  187. native_menu->Append(pItem);
  188. native_menu->Check(Menu_Native2, true);
  189. pItem = new wxMenuItem(native_menu, Menu_Native3, wxT("&Radio item"),
  190. wxT("check/uncheck me!"), wxITEM_RADIO);
  191. native_menu->Append(pItem);
  192. native_menu->AppendSeparator();
  193. pItem = new wxMenuItem(native_menu, Menu_Native4, wxT("&Disabled item\tCtrl+RatherLongAccel"),
  194. wxT("disabled item"));
  195. pItem->Enable(false);
  196. native_menu->Append(pItem);
  197. pItem = new wxMenuItem(native_menu, Menu_Native5, wxT("&Other\tCtrl+O"), wxT("other item"));
  198. native_menu->Append(pItem);
  199. wxMenuBar *menu_bar = new wxMenuBar;
  200. menu_bar->Append(file_menu, wxT("&File"));
  201. menu_bar->Append(drawn_menu, wxT("&Drawn"));
  202. menu_bar->Append(native_menu, wxT("&Native"));
  203. SetMenuBar(menu_bar);
  204. }
  205. // main frame constructor
  206. OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, const wxChar *title,
  207. int x, int y, int w, int h)
  208. : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
  209. {
  210. // set the icon
  211. SetIcon(wxICON(sample));
  212. // create the menu
  213. InitMenu();
  214. #if wxUSE_STATUSBAR
  215. // create the status line
  216. const int widths[] = { -1, 60 };
  217. CreateStatusBar(2);
  218. SetStatusWidths(2, widths);
  219. SetStatusText(wxT("no selection"), 0);
  220. #endif // wxUSE_STATUSBAR
  221. // make a panel with some controls
  222. wxPanel *pPanel = new wxPanel(this);
  223. // check list box
  224. static const wxChar* aszChoices[] = { wxT("Hello"), wxT("world"), wxT("and"),
  225. wxT("goodbye"), wxT("cruel"), wxT("world"),
  226. wxT("-------"), wxT("owner-drawn"), wxT("listbox") };
  227. wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
  228. unsigned int ui;
  229. for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
  230. astrChoices[ui] = aszChoices[ui];
  231. m_pListBox = new wxCheckListBox
  232. (
  233. pPanel, // parent
  234. Control_Listbox, // control id
  235. wxPoint(10, 10), // listbox position
  236. wxSize(200, 200), // listbox size
  237. WXSIZEOF(aszChoices), // number of strings
  238. astrChoices // array of strings
  239. );
  240. delete [] astrChoices;
  241. for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 )
  242. {
  243. #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
  244. m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
  245. #endif
  246. }
  247. m_pListBox->Check(2);
  248. // normal (but owner-drawn) listbox
  249. static const wxChar* aszColors[] = { wxT("Red"), wxT("Blue"), wxT("Pink"),
  250. wxT("Green"), wxT("Yellow"),
  251. wxT("Black"), wxT("Violet") };
  252. astrChoices = new wxString[WXSIZEOF(aszColors)];
  253. for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
  254. {
  255. astrChoices[ui] = aszColors[ui];
  256. }
  257. wxListBox *pListBox = new wxListBox
  258. (
  259. pPanel, // parent
  260. Control_Listbox2, // control id
  261. wxPoint(220, 10), // listbox position
  262. wxSize(200, 200), // listbox size
  263. WXSIZEOF(aszColors), // number of strings
  264. astrChoices, // array of strings
  265. wxLB_OWNERDRAW // owner-drawn
  266. );
  267. #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
  268. struct { unsigned char r, g, b; } aColors[] =
  269. {
  270. {255,0,0}, {0,0,255}, {255,128,192},
  271. {0,255,0}, {255,255,128},
  272. {0,0,0}, {128,0,255}
  273. };
  274. for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
  275. {
  276. pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r,
  277. aColors[ui].g,
  278. aColors[ui].b));
  279. // yellow on white is horrible...
  280. if ( ui == 4 )
  281. {
  282. pListBox->GetItem(ui)->SetBackgroundColour(wxColor(0, 0, 0));
  283. }
  284. }
  285. #else
  286. wxUnusedVar( pListBox );
  287. #endif
  288. delete[] astrChoices;
  289. Show(true);
  290. }
  291. void OwnerDrawnFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  292. {
  293. Close(true);
  294. }
  295. void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent& WXUNUSED(event))
  296. {
  297. // This example shows the use of bitmaps in ownerdrawn menuitems and is not a good
  298. // example on how to enable and disable menuitems - this should be done with the help of
  299. // EVT_UPDATE_UI and EVT_UPDATE_UI_RANGE !
  300. pAboutItem->Enable( pAboutItem->IsEnabled() ? false : true );
  301. }
  302. void OwnerDrawnFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  303. {
  304. wxMessageDialog dialog(this,
  305. wxT("Demo of owner-drawn controls\n"),
  306. wxT("About wxOwnerDrawn"), wxYES_NO | wxCANCEL);
  307. dialog.ShowModal();
  308. }
  309. void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event)
  310. {
  311. #if wxUSE_STATUSBAR
  312. wxString strSelection;
  313. unsigned int nSel = event.GetSelection();
  314. strSelection.Printf(wxT("item %d selected (%schecked)"), nSel,
  315. m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
  316. SetStatusText(strSelection);
  317. #else
  318. wxUnusedVar(event);
  319. #endif // wxUSE_STATUSBAR
  320. }
  321. void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
  322. {
  323. wxString strSelection;
  324. strSelection.Printf(wxT("item %d double clicked"),
  325. m_pListBox->GetSelection());
  326. wxMessageDialog dialog(this, strSelection);
  327. dialog.ShowModal();
  328. }
  329. void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event)
  330. {
  331. #if wxUSE_STATUSBAR
  332. wxString strSelection;
  333. unsigned int nItem = event.GetInt();
  334. strSelection.Printf(wxT("item %d was %schecked"), nItem,
  335. m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
  336. SetStatusText(strSelection);
  337. #else
  338. wxUnusedVar(event);
  339. #endif // wxUSE_STATUSBAR
  340. }