odcombobox.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: odcombobox.cpp
  4. // Purpose: Part of the widgets sample showing wxOwnerDrawnComboBox
  5. // Author: Jaakko Salli (based on combobox page by Vadim Zeitlin)
  6. // Created: Jul-28-2006
  7. // Copyright: (c) 2006 Jaakko Salli
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // for compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #if wxUSE_ODCOMBOBOX
  22. // for all others, include the necessary headers
  23. #ifndef WX_PRECOMP
  24. #include "wx/log.h"
  25. #include "wx/bitmap.h"
  26. #include "wx/button.h"
  27. #include "wx/checkbox.h"
  28. #include "wx/combobox.h"
  29. #include "wx/radiobox.h"
  30. #include "wx/statbox.h"
  31. #include "wx/textctrl.h"
  32. #endif
  33. #include "wx/dc.h"
  34. #include "wx/dcmemory.h"
  35. #include "wx/sizer.h"
  36. #include "wx/odcombo.h"
  37. #include "itemcontainer.h"
  38. #include "widgets.h"
  39. #include "icons/odcombobox.xpm"
  40. // ----------------------------------------------------------------------------
  41. // constants
  42. // ----------------------------------------------------------------------------
  43. // control ids
  44. enum
  45. {
  46. ODComboPage_Reset = wxID_HIGHEST,
  47. ODComboPage_PopupMinWidth,
  48. ODComboPage_PopupHeight,
  49. ODComboPage_ButtonWidth,
  50. ODComboPage_ButtonHeight,
  51. ODComboPage_ButtonSpacing,
  52. ODComboPage_CurText,
  53. ODComboPage_InsertionPointText,
  54. ODComboPage_Insert,
  55. ODComboPage_InsertText,
  56. ODComboPage_Add,
  57. ODComboPage_AddText,
  58. ODComboPage_AddSeveral,
  59. ODComboPage_AddMany,
  60. ODComboPage_Clear,
  61. ODComboPage_Change,
  62. ODComboPage_ChangeText,
  63. ODComboPage_Delete,
  64. ODComboPage_DeleteText,
  65. ODComboPage_DeleteSel,
  66. ODComboPage_Combo,
  67. ODComboPage_ContainerTests
  68. };
  69. // ----------------------------------------------------------------------------
  70. // ODComboboxWidgetsPage
  71. // ----------------------------------------------------------------------------
  72. class ODComboboxWidgetsPage : public ItemContainerWidgetsPage
  73. {
  74. public:
  75. ODComboboxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  76. virtual wxControl *GetWidget() const { return m_combobox; }
  77. virtual wxTextEntryBase *GetTextEntry() const
  78. { return m_combobox ? m_combobox->GetTextCtrl() : NULL; }
  79. virtual wxItemContainer* GetContainer() const { return m_combobox; }
  80. virtual void RecreateWidget() { CreateCombo(); }
  81. // lazy creation of the content
  82. virtual void CreateContent();
  83. protected:
  84. // event handlers
  85. void OnButtonReset(wxCommandEvent& event);
  86. void OnButtonChange(wxCommandEvent& event);
  87. void OnButtonDelete(wxCommandEvent& event);
  88. void OnButtonDeleteSel(wxCommandEvent& event);
  89. void OnButtonClear(wxCommandEvent& event);
  90. void OnButtonInsert(wxCommandEvent &event);
  91. void OnButtonAdd(wxCommandEvent& event);
  92. void OnButtonAddSeveral(wxCommandEvent& event);
  93. void OnButtonAddMany(wxCommandEvent& event);
  94. void OnComboBox(wxCommandEvent& event);
  95. void OnDropDown(wxCommandEvent& event);
  96. void OnCloseUp(wxCommandEvent& event);
  97. void OnComboText(wxCommandEvent& event);
  98. void OnCheckOrRadioBox(wxCommandEvent& event);
  99. void OnTextPopupWidth(wxCommandEvent& event);
  100. void OnTextPopupHeight(wxCommandEvent& event);
  101. void OnTextButtonAll(wxCommandEvent& event);
  102. void OnUpdateUICurText(wxUpdateUIEvent& event);
  103. void OnUpdateUIInsertionPointText(wxUpdateUIEvent& event);
  104. void OnUpdateUIInsert(wxUpdateUIEvent& event);
  105. void OnUpdateUIAddSeveral(wxUpdateUIEvent& event);
  106. void OnUpdateUIClearButton(wxUpdateUIEvent& event);
  107. void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
  108. void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
  109. void OnUpdateUIResetButton(wxUpdateUIEvent& event);
  110. // reset the odcombobox parameters
  111. void Reset();
  112. // (re)create the odcombobox
  113. void CreateCombo();
  114. // helper that gets all button values from controls and calls SetButtonPosition
  115. void GetButtonPosition();
  116. // helper to create the button bitmap
  117. wxBitmap CreateBitmap(const wxColour& colour);
  118. // the controls
  119. // ------------
  120. // the checkboxes for styles
  121. wxCheckBox *m_chkSort,
  122. *m_chkReadonly,
  123. *m_chkDclickcycles,
  124. *m_chkBitmapbutton,
  125. *m_chkStdbutton;
  126. // the text entries for popup and button adjustment
  127. wxTextCtrl *m_textPopupMinWidth,
  128. *m_textPopupHeight,
  129. *m_textButtonWidth,
  130. *m_textButtonHeight,
  131. *m_textButtonSpacing;
  132. // the checkboxes for same
  133. wxCheckBox *m_chkAlignpopupright,
  134. *m_chkAlignbutleft;
  135. // the combobox itself and the sizer it is in
  136. wxOwnerDrawnComboBox *m_combobox;
  137. wxSizer *m_sizerCombo;
  138. // the text entries for "Add/change string" and "Delete" buttons
  139. wxTextCtrl *m_textInsert,
  140. *m_textAdd,
  141. *m_textChange,
  142. *m_textDelete;
  143. private:
  144. wxDECLARE_EVENT_TABLE();
  145. DECLARE_WIDGETS_PAGE(ODComboboxWidgetsPage)
  146. };
  147. // ----------------------------------------------------------------------------
  148. // event tables
  149. // ----------------------------------------------------------------------------
  150. wxBEGIN_EVENT_TABLE(ODComboboxWidgetsPage, WidgetsPage)
  151. EVT_BUTTON(ODComboPage_Reset, ODComboboxWidgetsPage::OnButtonReset)
  152. EVT_BUTTON(ODComboPage_Change, ODComboboxWidgetsPage::OnButtonChange)
  153. EVT_BUTTON(ODComboPage_Delete, ODComboboxWidgetsPage::OnButtonDelete)
  154. EVT_BUTTON(ODComboPage_DeleteSel, ODComboboxWidgetsPage::OnButtonDeleteSel)
  155. EVT_BUTTON(ODComboPage_Clear, ODComboboxWidgetsPage::OnButtonClear)
  156. EVT_BUTTON(ODComboPage_Insert, ODComboboxWidgetsPage::OnButtonInsert)
  157. EVT_BUTTON(ODComboPage_Add, ODComboboxWidgetsPage::OnButtonAdd)
  158. EVT_BUTTON(ODComboPage_AddSeveral, ODComboboxWidgetsPage::OnButtonAddSeveral)
  159. EVT_BUTTON(ODComboPage_AddMany, ODComboboxWidgetsPage::OnButtonAddMany)
  160. EVT_BUTTON(ODComboPage_ContainerTests, ItemContainerWidgetsPage::OnButtonTestItemContainer)
  161. EVT_TEXT_ENTER(ODComboPage_InsertText, ODComboboxWidgetsPage::OnButtonInsert)
  162. EVT_TEXT_ENTER(ODComboPage_AddText, ODComboboxWidgetsPage::OnButtonAdd)
  163. EVT_TEXT_ENTER(ODComboPage_DeleteText, ODComboboxWidgetsPage::OnButtonDelete)
  164. EVT_TEXT(ODComboPage_PopupMinWidth, ODComboboxWidgetsPage::OnTextPopupWidth)
  165. EVT_TEXT(ODComboPage_PopupHeight, ODComboboxWidgetsPage::OnTextPopupHeight)
  166. EVT_TEXT(ODComboPage_ButtonWidth, ODComboboxWidgetsPage::OnTextButtonAll)
  167. EVT_TEXT(ODComboPage_ButtonHeight, ODComboboxWidgetsPage::OnTextButtonAll)
  168. EVT_TEXT(ODComboPage_ButtonSpacing, ODComboboxWidgetsPage::OnTextButtonAll)
  169. EVT_UPDATE_UI(ODComboPage_CurText, ODComboboxWidgetsPage::OnUpdateUICurText)
  170. EVT_UPDATE_UI(ODComboPage_InsertionPointText, ODComboboxWidgetsPage::OnUpdateUIInsertionPointText)
  171. EVT_UPDATE_UI(ODComboPage_Reset, ODComboboxWidgetsPage::OnUpdateUIResetButton)
  172. EVT_UPDATE_UI(ODComboPage_Insert, ODComboboxWidgetsPage::OnUpdateUIInsert)
  173. EVT_UPDATE_UI(ODComboPage_AddSeveral, ODComboboxWidgetsPage::OnUpdateUIAddSeveral)
  174. EVT_UPDATE_UI(ODComboPage_Clear, ODComboboxWidgetsPage::OnUpdateUIClearButton)
  175. EVT_UPDATE_UI(ODComboPage_DeleteText, ODComboboxWidgetsPage::OnUpdateUIClearButton)
  176. EVT_UPDATE_UI(ODComboPage_Delete, ODComboboxWidgetsPage::OnUpdateUIDeleteButton)
  177. EVT_UPDATE_UI(ODComboPage_Change, ODComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
  178. EVT_UPDATE_UI(ODComboPage_ChangeText, ODComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
  179. EVT_UPDATE_UI(ODComboPage_DeleteSel, ODComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
  180. EVT_COMBOBOX_DROPDOWN(ODComboPage_Combo, ODComboboxWidgetsPage::OnDropDown)
  181. EVT_COMBOBOX_CLOSEUP(ODComboPage_Combo, ODComboboxWidgetsPage::OnCloseUp)
  182. EVT_COMBOBOX(ODComboPage_Combo, ODComboboxWidgetsPage::OnComboBox)
  183. EVT_TEXT(ODComboPage_Combo, ODComboboxWidgetsPage::OnComboText)
  184. EVT_TEXT_ENTER(ODComboPage_Combo, ODComboboxWidgetsPage::OnComboText)
  185. EVT_CHECKBOX(wxID_ANY, ODComboboxWidgetsPage::OnCheckOrRadioBox)
  186. EVT_RADIOBOX(wxID_ANY, ODComboboxWidgetsPage::OnCheckOrRadioBox)
  187. wxEND_EVENT_TABLE()
  188. // ============================================================================
  189. // implementation
  190. // ============================================================================
  191. //
  192. // wxOwnerDrawnComboBox needs to subclassed so that owner-drawing
  193. // callbacks can be implemented.
  194. class DemoODComboBox : public wxOwnerDrawnComboBox
  195. {
  196. public:
  197. virtual void OnDrawItem(wxDC& dc,
  198. const wxRect& rect,
  199. int item,
  200. int WXUNUSED(flags)) const
  201. {
  202. if ( item == wxNOT_FOUND )
  203. return;
  204. wxColour txtCol;
  205. int mod = item % 4;
  206. if ( mod == 0 )
  207. txtCol = *wxBLACK;
  208. else if ( mod == 1 )
  209. txtCol = *wxRED;
  210. else if ( mod == 2 )
  211. txtCol = *wxGREEN;
  212. else
  213. txtCol = *wxBLUE;
  214. dc.SetTextForeground(txtCol);
  215. dc.DrawText(GetString(item),
  216. rect.x + 3,
  217. rect.y + ((rect.height - dc.GetCharHeight())/2)
  218. );
  219. }
  220. virtual void OnDrawBackground(wxDC& dc, const wxRect& rect,
  221. int item, int flags ) const
  222. {
  223. // If item is selected or even, or we are painting the
  224. // combo control itself, use the default rendering.
  225. if ( (flags & (wxODCB_PAINTING_CONTROL|wxODCB_PAINTING_SELECTED)) ||
  226. (item & 1) == 0 )
  227. {
  228. wxOwnerDrawnComboBox::OnDrawBackground(dc,rect,item,flags);
  229. return;
  230. }
  231. // Otherwise, draw every other background with different colour.
  232. wxColour bgCol(240,240,250);
  233. dc.SetBrush(wxBrush(bgCol));
  234. dc.SetPen(wxPen(bgCol));
  235. dc.DrawRectangle(rect);
  236. }
  237. virtual wxCoord OnMeasureItem(size_t WXUNUSED(item)) const
  238. {
  239. return 48;
  240. }
  241. virtual wxCoord OnMeasureItemWidth(size_t WXUNUSED(item)) const
  242. {
  243. return -1; // default - will be measured from text width
  244. }
  245. };
  246. IMPLEMENT_WIDGETS_PAGE(ODComboboxWidgetsPage, wxT("OwnerDrawnCombobox"),
  247. GENERIC_CTRLS | WITH_ITEMS_CTRLS | COMBO_CTRLS
  248. );
  249. ODComboboxWidgetsPage::ODComboboxWidgetsPage(WidgetsBookCtrl *book,
  250. wxImageList *imaglist)
  251. : ItemContainerWidgetsPage(book, imaglist, odcombobox_xpm)
  252. {
  253. // init everything
  254. m_chkSort =
  255. m_chkReadonly =
  256. m_chkDclickcycles = (wxCheckBox *)NULL;
  257. m_combobox = (wxOwnerDrawnComboBox *)NULL;
  258. m_sizerCombo = (wxSizer *)NULL;
  259. }
  260. void ODComboboxWidgetsPage::CreateContent()
  261. {
  262. /*
  263. What we create here is a frame having 3 panes: style pane is the
  264. leftmost one, in the middle the pane with buttons allowing to perform
  265. miscellaneous combobox operations and the pane containing the combobox
  266. itself to the right
  267. */
  268. wxTextCtrl *text;
  269. wxSizer *sizerRow;
  270. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  271. wxSizer *sizerLeft = new wxBoxSizer(wxVERTICAL);
  272. // left pane - style box
  273. wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
  274. wxSizer *sizerStyle = new wxStaticBoxSizer(box, wxVERTICAL);
  275. m_chkSort = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("&Sort items"));
  276. m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("&Read only"));
  277. m_chkDclickcycles = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("&Double-click Cycles"));
  278. sizerStyle->AddSpacer(4);
  279. m_chkBitmapbutton = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("&Bitmap button"));
  280. m_chkStdbutton = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("B&lank button background"));
  281. wxButton *btn = new wxButton(this, ODComboPage_Reset, wxT("&Reset"));
  282. sizerStyle->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 3);
  283. sizerLeft->Add(sizerStyle, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL);
  284. // left pane - popup adjustment box
  285. box = new wxStaticBox(this, wxID_ANY, wxT("Adjust &popup"));
  286. wxSizer *sizerPopupPos = new wxStaticBoxSizer(box, wxVERTICAL);
  287. sizerRow = CreateSizerWithTextAndLabel(wxT("Min. Width:"),
  288. ODComboPage_PopupMinWidth,
  289. &m_textPopupMinWidth);
  290. m_textPopupMinWidth->SetValue(wxT("-1"));
  291. sizerPopupPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
  292. sizerRow = CreateSizerWithTextAndLabel(wxT("Max. Height:"),
  293. ODComboPage_PopupHeight,
  294. &m_textPopupHeight);
  295. m_textPopupHeight->SetValue(wxT("-1"));
  296. sizerPopupPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
  297. m_chkAlignpopupright = CreateCheckBoxAndAddToSizer(sizerPopupPos, wxT("Align Right"));
  298. sizerLeft->Add(sizerPopupPos, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL | wxTOP, 2);
  299. // left pane - button adjustment box
  300. box = new wxStaticBox(this, wxID_ANY, wxT("Adjust &button"));
  301. wxSizer *sizerButtonPos = new wxStaticBoxSizer(box, wxVERTICAL);
  302. sizerRow = CreateSizerWithTextAndLabel(wxT("Width:"),
  303. ODComboPage_ButtonWidth,
  304. &m_textButtonWidth);
  305. m_textButtonWidth->SetValue(wxT("-1"));
  306. sizerButtonPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
  307. sizerRow = CreateSizerWithTextAndLabel(wxT("VSpacing:"),
  308. ODComboPage_ButtonSpacing,
  309. &m_textButtonSpacing);
  310. m_textButtonSpacing->SetValue(wxT("0"));
  311. sizerButtonPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
  312. sizerRow = CreateSizerWithTextAndLabel(wxT("Height:"),
  313. ODComboPage_ButtonHeight,
  314. &m_textButtonHeight);
  315. m_textButtonHeight->SetValue(wxT("-1"));
  316. sizerButtonPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
  317. m_chkAlignbutleft = CreateCheckBoxAndAddToSizer(sizerButtonPos, wxT("Align Left"));
  318. sizerLeft->Add(sizerButtonPos, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL | wxTOP, 2);
  319. // middle pane
  320. wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
  321. wxT("&Change combobox contents"));
  322. wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
  323. btn = new wxButton(this, ODComboPage_ContainerTests, wxT("Run &tests"));
  324. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  325. sizerRow = CreateSizerWithTextAndLabel(wxT("Current selection"),
  326. ODComboPage_CurText,
  327. &text);
  328. text->SetEditable(false);
  329. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  330. sizerRow = CreateSizerWithTextAndLabel(wxT("Insertion Point"),
  331. ODComboPage_InsertionPointText,
  332. &text);
  333. text->SetEditable(false);
  334. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  335. sizerRow = CreateSizerWithTextAndButton(ODComboPage_Insert,
  336. wxT("&Insert this string"),
  337. ODComboPage_InsertText,
  338. &m_textInsert);
  339. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  340. sizerRow = CreateSizerWithTextAndButton(ODComboPage_Add,
  341. wxT("&Add this string"),
  342. ODComboPage_AddText,
  343. &m_textAdd);
  344. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  345. btn = new wxButton(this, ODComboPage_AddSeveral, wxT("&Append a few strings"));
  346. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  347. btn = new wxButton(this, ODComboPage_AddMany, wxT("Append &many strings"));
  348. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  349. sizerRow = CreateSizerWithTextAndButton(ODComboPage_Change,
  350. wxT("C&hange current"),
  351. ODComboPage_ChangeText,
  352. &m_textChange);
  353. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  354. sizerRow = CreateSizerWithTextAndButton(ODComboPage_Delete,
  355. wxT("&Delete this item"),
  356. ODComboPage_DeleteText,
  357. &m_textDelete);
  358. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  359. btn = new wxButton(this, ODComboPage_DeleteSel, wxT("Delete &selection"));
  360. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  361. btn = new wxButton(this, ODComboPage_Clear, wxT("&Clear"));
  362. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  363. // right pane
  364. wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
  365. m_combobox = new DemoODComboBox();
  366. m_combobox->Create(this, ODComboPage_Combo, wxEmptyString,
  367. wxDefaultPosition, wxDefaultSize,
  368. 0, NULL,
  369. 0);
  370. sizerRight->Add(m_combobox, 0, wxGROW | wxALL, 5);
  371. sizerRight->SetMinSize(150, 0);
  372. m_sizerCombo = sizerRight; // save it to modify it later
  373. // the 3 panes panes compose the window
  374. sizerTop->Add(sizerLeft, 4, wxGROW | (wxALL & ~wxLEFT), 10);
  375. sizerTop->Add(sizerMiddle, 5, wxGROW | wxALL, 10);
  376. sizerTop->Add(sizerRight, 4, wxGROW | (wxALL & ~wxRIGHT), 10);
  377. // final initializations
  378. Reset();
  379. SetSizer(sizerTop);
  380. }
  381. // ----------------------------------------------------------------------------
  382. // operations
  383. // ----------------------------------------------------------------------------
  384. void ODComboboxWidgetsPage::Reset()
  385. {
  386. m_chkSort->SetValue(false);
  387. m_chkReadonly->SetValue(false);
  388. m_chkDclickcycles->SetValue(false);
  389. m_chkDclickcycles->Enable(false);
  390. m_chkBitmapbutton->SetValue(false);
  391. m_chkStdbutton->SetValue(false);
  392. m_chkStdbutton->Enable(false);
  393. }
  394. void ODComboboxWidgetsPage::CreateCombo()
  395. {
  396. int flags = ms_defaultFlags;
  397. if ( m_chkSort->GetValue() )
  398. flags |= wxCB_SORT;
  399. if ( m_chkReadonly->GetValue() )
  400. flags |= wxCB_READONLY;
  401. if ( m_chkDclickcycles->GetValue() )
  402. flags |= wxODCB_DCLICK_CYCLES;
  403. wxArrayString items;
  404. if ( m_combobox )
  405. {
  406. unsigned int count = m_combobox->GetCount();
  407. for ( unsigned int n = 0; n < count; n++ )
  408. {
  409. items.Add(m_combobox->GetString(n));
  410. }
  411. m_sizerCombo->Detach( m_combobox );
  412. delete m_combobox;
  413. }
  414. m_combobox = new DemoODComboBox();
  415. m_combobox->Create(this, ODComboPage_Combo, wxEmptyString,
  416. wxDefaultPosition, wxDefaultSize,
  417. 0, NULL,
  418. flags);
  419. unsigned int count = items.GetCount();
  420. for ( unsigned int n = 0; n < count; n++ )
  421. {
  422. m_combobox->Append(items[n]);
  423. }
  424. // Update from controls that edit popup position etc.
  425. wxUpdateUIEvent tempEvt;
  426. OnTextPopupWidth(tempEvt);
  427. OnTextPopupHeight(tempEvt);
  428. GetButtonPosition();
  429. m_combobox->SetPopupAnchor( m_chkAlignpopupright->GetValue() ? wxRIGHT : wxLEFT );
  430. if ( m_chkBitmapbutton->GetValue() )
  431. {
  432. wxBitmap bmpNormal = CreateBitmap(*wxBLUE);
  433. wxBitmap bmpPressed = CreateBitmap(wxColour(0,0,128));
  434. wxBitmap bmpHover = CreateBitmap(wxColour(128,128,255));
  435. m_combobox->SetButtonBitmaps(bmpNormal,m_chkStdbutton->GetValue(),bmpPressed,bmpHover);
  436. }
  437. m_sizerCombo->Add(m_combobox, 0, wxGROW | wxALL, 5);
  438. m_sizerCombo->Layout();
  439. }
  440. // ----------------------------------------------------------------------------
  441. // event handlers
  442. // ----------------------------------------------------------------------------
  443. void ODComboboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  444. {
  445. Reset();
  446. CreateCombo();
  447. }
  448. void ODComboboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
  449. {
  450. int sel = m_combobox->GetSelection();
  451. if ( sel != wxNOT_FOUND )
  452. {
  453. #ifndef __WXGTK__
  454. m_combobox->SetString(sel, m_textChange->GetValue());
  455. #else
  456. wxLogMessage(wxT("Not implemented in wxGTK"));
  457. #endif
  458. }
  459. }
  460. void ODComboboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
  461. {
  462. unsigned long n;
  463. if ( !m_textDelete->GetValue().ToULong(&n) ||
  464. (n >= m_combobox->GetCount()) )
  465. {
  466. return;
  467. }
  468. m_combobox->Delete(n);
  469. }
  470. void ODComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
  471. {
  472. int sel = m_combobox->GetSelection();
  473. if ( sel != wxNOT_FOUND )
  474. {
  475. m_combobox->Delete(sel);
  476. }
  477. }
  478. void ODComboboxWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
  479. {
  480. m_combobox->Clear();
  481. }
  482. void ODComboboxWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
  483. {
  484. static unsigned int s_item = 0;
  485. wxString s = m_textInsert->GetValue();
  486. if ( !m_textInsert->IsModified() )
  487. {
  488. // update the default string
  489. m_textInsert->SetValue(wxString::Format(wxT("test item %u"), ++s_item));
  490. }
  491. if (m_combobox->GetSelection() >= 0)
  492. m_combobox->Insert(s, m_combobox->GetSelection());
  493. }
  494. void ODComboboxWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
  495. {
  496. static unsigned int s_item = 0;
  497. wxString s = m_textAdd->GetValue();
  498. if ( !m_textAdd->IsModified() )
  499. {
  500. // update the default string
  501. m_textAdd->SetValue(wxString::Format(wxT("test item %u"), ++s_item));
  502. }
  503. m_combobox->Append(s);
  504. }
  505. void ODComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
  506. {
  507. // "many" means 1000 here
  508. for ( unsigned int n = 0; n < 1000; n++ )
  509. {
  510. m_combobox->Append(wxString::Format(wxT("item #%u"), n));
  511. }
  512. }
  513. void ODComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
  514. {
  515. m_combobox->Append(wxT("First"));
  516. m_combobox->Append(wxT("another one"));
  517. m_combobox->Append(wxT("and the last (very very very very very very very very very very long) one"));
  518. }
  519. void ODComboboxWidgetsPage::OnTextPopupWidth(wxCommandEvent& WXUNUSED(event))
  520. {
  521. long l = 0;
  522. m_textPopupMinWidth->GetValue().ToLong(&l);
  523. if (m_combobox && l > 0)
  524. {
  525. m_combobox->SetPopupMinWidth(l);
  526. }
  527. }
  528. void ODComboboxWidgetsPage::OnTextPopupHeight(wxCommandEvent& WXUNUSED(event))
  529. {
  530. long l = 0;
  531. m_textPopupHeight->GetValue().ToLong(&l);
  532. if (m_combobox && l > 0)
  533. {
  534. m_combobox->SetPopupMaxHeight(l);
  535. }
  536. }
  537. void ODComboboxWidgetsPage::GetButtonPosition()
  538. {
  539. long w = -1;
  540. long h = -1;
  541. long spacing = 0;
  542. m_textButtonWidth->GetValue().ToLong(&w);
  543. m_textButtonSpacing->GetValue().ToLong(&spacing);
  544. m_textButtonHeight->GetValue().ToLong(&h);
  545. int align = m_chkAlignbutleft->GetValue() ?
  546. wxLEFT : wxRIGHT;
  547. m_combobox->SetButtonPosition(w,h,align,spacing);
  548. }
  549. void ODComboboxWidgetsPage::OnTextButtonAll(wxCommandEvent& WXUNUSED(event))
  550. {
  551. if (m_combobox)
  552. {
  553. if ( m_chkBitmapbutton->GetValue() )
  554. CreateCombo();
  555. else
  556. GetButtonPosition();
  557. }
  558. }
  559. void ODComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent& event)
  560. {
  561. if (m_combobox)
  562. event.SetText( wxString::Format(wxT("%d"), m_combobox->GetSelection()) );
  563. }
  564. void ODComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent& event)
  565. {
  566. if (m_combobox)
  567. event.SetText( wxString::Format(wxT("%ld"), m_combobox->GetInsertionPoint()) );
  568. }
  569. void ODComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
  570. {
  571. if (m_combobox)
  572. event.Enable( m_chkSort->GetValue() || m_chkReadonly->GetValue() ||
  573. m_chkBitmapbutton->GetValue() );
  574. }
  575. void ODComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent& event)
  576. {
  577. if (m_combobox)
  578. {
  579. bool enable = !(m_combobox->GetWindowStyle() & wxCB_SORT) &&
  580. (m_combobox->GetSelection() >= 0);
  581. event.Enable(enable);
  582. }
  583. }
  584. void ODComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
  585. {
  586. if (m_combobox)
  587. {
  588. unsigned long n;
  589. event.Enable(m_textDelete->GetValue().ToULong(&n) &&
  590. (n < (unsigned)m_combobox->GetCount()));
  591. }
  592. }
  593. void ODComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
  594. {
  595. if (m_combobox)
  596. event.Enable(m_combobox->GetSelection() != wxNOT_FOUND);
  597. }
  598. void ODComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
  599. {
  600. if (m_combobox)
  601. event.Enable(m_combobox->GetCount() != 0);
  602. }
  603. void ODComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
  604. {
  605. if (m_combobox)
  606. event.Enable(!(m_combobox->GetWindowStyle() & wxCB_SORT));
  607. }
  608. void ODComboboxWidgetsPage::OnComboText(wxCommandEvent& event)
  609. {
  610. if (!m_combobox)
  611. return;
  612. wxString s = event.GetString();
  613. wxASSERT_MSG( s == m_combobox->GetValue(),
  614. wxT("event and combobox values should be the same") );
  615. if (event.GetEventType() == wxEVT_TEXT_ENTER)
  616. {
  617. wxLogMessage(wxT("OwnerDrawnCombobox enter pressed (now '%s')"), s.c_str());
  618. }
  619. else
  620. {
  621. wxLogMessage(wxT("OwnerDrawnCombobox text changed (now '%s')"), s.c_str());
  622. }
  623. }
  624. void ODComboboxWidgetsPage::OnComboBox(wxCommandEvent& event)
  625. {
  626. long sel = event.GetInt();
  627. m_textDelete->SetValue(wxString::Format(wxT("%ld"), sel));
  628. wxLogMessage(wxT("OwnerDrawnCombobox item %ld selected"), sel);
  629. wxLogMessage(wxT("OwnerDrawnCombobox GetValue(): %s"), m_combobox->GetValue().c_str() );
  630. }
  631. void ODComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
  632. {
  633. wxObject* ctrl = event.GetEventObject();
  634. // Double-click cycles only applies to read-only combobox
  635. if ( ctrl == (wxObject*) m_chkReadonly )
  636. {
  637. m_chkDclickcycles->Enable( m_chkReadonly->GetValue() );
  638. }
  639. else if ( ctrl == (wxObject*) m_chkBitmapbutton )
  640. {
  641. m_chkStdbutton->Enable( m_chkBitmapbutton->GetValue() );
  642. }
  643. else if ( ctrl == (wxObject*) m_chkAlignbutleft )
  644. {
  645. wxUpdateUIEvent tempEvt;
  646. OnTextButtonAll(tempEvt);
  647. }
  648. CreateCombo();
  649. }
  650. wxBitmap ODComboboxWidgetsPage::CreateBitmap(const wxColour& colour)
  651. {
  652. int ch = m_combobox->GetClientSize().y - 1;
  653. int h0 = ch - 5;
  654. long w = -1;
  655. long h = -1;
  656. m_textButtonWidth->GetValue().ToLong(&w);
  657. m_textButtonHeight->GetValue().ToLong(&h);
  658. if ( w <= 0 )
  659. w = h0 - 1;
  660. if ( h <= 0 )
  661. h = h0;
  662. if ( h > ch )
  663. h = ch;
  664. wxMemoryDC dc;
  665. wxBitmap bmp(w,h);
  666. dc.SelectObject(bmp);
  667. // Draw transparent background
  668. wxColour magic(255,0,255);
  669. wxBrush magicBrush(magic);
  670. dc.SetBrush(magicBrush);
  671. dc.SetPen(*wxTRANSPARENT_PEN);
  672. dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
  673. // Draw image content
  674. dc.SetBrush(wxBrush(colour));
  675. dc.DrawCircle(h/2,h/2+1,(h/2));
  676. dc.SelectObject(wxNullBitmap);
  677. // Finalize transparency with a mask
  678. wxMask *mask = new wxMask(bmp, magic);
  679. bmp.SetMask(mask);
  680. return bmp;
  681. }
  682. void ODComboboxWidgetsPage::OnDropDown(wxCommandEvent& WXUNUSED(event))
  683. {
  684. wxLogMessage(wxT("Combobox dropped down"));
  685. }
  686. void ODComboboxWidgetsPage::OnCloseUp(wxCommandEvent& WXUNUSED(event))
  687. {
  688. wxLogMessage(wxT("Combobox closed up"));
  689. }
  690. #endif //wxUSE_ODCOMBOBOX