dataview.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dataview.cpp
  3. // Purpose: wxDataViewCtrl wxWidgets sample
  4. // Author: Robert Roebling
  5. // Modified by: Francesco Montorsi, Bo Yang
  6. // Created: 06/01/06
  7. // Copyright: (c) Robert Roebling
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // For compilers that support precompilation, includes "wx/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/dataview.h"
  22. #include "wx/datetime.h"
  23. #include "wx/splitter.h"
  24. #include "wx/aboutdlg.h"
  25. #include "wx/colordlg.h"
  26. #include "wx/choicdlg.h"
  27. #include "wx/numdlg.h"
  28. #include "wx/spinctrl.h"
  29. #include "wx/imaglist.h"
  30. #include "wx/notebook.h"
  31. #include "mymodels.h"
  32. // ----------------------------------------------------------------------------
  33. // resources
  34. // ----------------------------------------------------------------------------
  35. #ifndef wxHAS_IMAGES_IN_RESOURCES
  36. #include "../sample.xpm"
  37. #endif
  38. #include "wx_small.xpm"
  39. // ----------------------------------------------------------------------------
  40. // MyApp
  41. // ----------------------------------------------------------------------------
  42. class MyApp: public wxApp
  43. {
  44. public:
  45. virtual bool OnInit();
  46. };
  47. // ----------------------------------------------------------------------------
  48. // MyFrame
  49. // ----------------------------------------------------------------------------
  50. class MyFrame : public wxFrame
  51. {
  52. public:
  53. MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
  54. ~MyFrame();
  55. void BuildDataViewCtrl(wxPanel* parent,
  56. unsigned int nPanel,
  57. unsigned long style = 0);
  58. private:
  59. // event handlers
  60. void OnStyleChange(wxCommandEvent& event);
  61. void OnSetBackgroundColour(wxCommandEvent& event);
  62. void OnSetForegroundColour(wxCommandEvent& event);
  63. void OnQuit(wxCommandEvent& event);
  64. void OnAbout(wxCommandEvent& event);
  65. void OnClearLog(wxCommandEvent& event);
  66. void OnPageChanged(wxBookCtrlEvent& event);
  67. void OnAddMozart(wxCommandEvent& event);
  68. void OnDeleteSelected(wxCommandEvent& event);
  69. void OnDeleteYear(wxCommandEvent& event);
  70. void OnSelectNinth(wxCommandEvent& event);
  71. void OnCollapse(wxCommandEvent& event);
  72. void OnExpand(wxCommandEvent& event);
  73. void OnShowCurrent(wxCommandEvent& event);
  74. void OnSetNinthCurrent(wxCommandEvent& event);
  75. void OnPrependList(wxCommandEvent& event);
  76. void OnDeleteList(wxCommandEvent& event);
  77. // Fourth page.
  78. void OnDeleteTreeItem(wxCommandEvent& event);
  79. void OnDeleteAllTreeItems(wxCommandEvent& event);
  80. void OnAddTreeItem(wxCommandEvent& event);
  81. void OnAddTreeContainerItem(wxCommandEvent& event);
  82. void OnValueChanged( wxDataViewEvent &event );
  83. void OnActivated( wxDataViewEvent &event );
  84. void OnExpanding( wxDataViewEvent &event );
  85. void OnExpanded( wxDataViewEvent &event );
  86. void OnCollapsing( wxDataViewEvent &event );
  87. void OnCollapsed( wxDataViewEvent &event );
  88. void OnSelectionChanged( wxDataViewEvent &event );
  89. void OnStartEditing( wxDataViewEvent &event );
  90. void OnEditingStarted( wxDataViewEvent &event );
  91. void OnEditingDone( wxDataViewEvent &event );
  92. void OnHeaderClick( wxDataViewEvent &event );
  93. void OnAttrHeaderClick( wxDataViewEvent &event );
  94. void OnHeaderRightClick( wxDataViewEvent &event );
  95. void OnSorted( wxDataViewEvent &event );
  96. void OnContextMenu( wxDataViewEvent &event );
  97. void OnGoto( wxCommandEvent &event);
  98. void OnAddMany( wxCommandEvent &event);
  99. void OnHideAttributes( wxCommandEvent &event);
  100. void OnShowAttributes( wxCommandEvent &event);
  101. #if wxUSE_DRAG_AND_DROP
  102. void OnBeginDrag( wxDataViewEvent &event );
  103. void OnDropPossible( wxDataViewEvent &event );
  104. void OnDrop( wxDataViewEvent &event );
  105. #endif // wxUSE_DRAG_AND_DROP
  106. void OnDataViewChar(wxKeyEvent& event);
  107. // helper used by both OnDeleteSelected() and OnDataViewChar()
  108. void DeleteSelectedItems();
  109. wxNotebook* m_notebook;
  110. // the controls stored in the various tabs of the main notebook:
  111. wxDataViewCtrl* m_ctrl[4];
  112. // the models associated with the first two DVC:
  113. wxObjectDataPtr<MyMusicTreeModel> m_music_model;
  114. wxObjectDataPtr<MyListModel> m_list_model;
  115. // other data:
  116. wxDataViewColumn* m_col;
  117. wxDataViewColumn* m_attributes;
  118. wxTextCtrl* m_log;
  119. wxLog *m_logOld;
  120. private:
  121. wxDECLARE_EVENT_TABLE();
  122. };
  123. // ----------------------------------------------------------------------------
  124. // MyCustomRenderer
  125. // ----------------------------------------------------------------------------
  126. class MyCustomRenderer: public wxDataViewCustomRenderer
  127. {
  128. public:
  129. MyCustomRenderer()
  130. : wxDataViewCustomRenderer("string",
  131. wxDATAVIEW_CELL_ACTIVATABLE,
  132. wxALIGN_CENTER)
  133. { }
  134. virtual bool Render( wxRect rect, wxDC *dc, int state )
  135. {
  136. dc->SetBrush( *wxLIGHT_GREY_BRUSH );
  137. dc->SetPen( *wxTRANSPARENT_PEN );
  138. rect.Deflate(2);
  139. dc->DrawRoundedRectangle( rect, 5 );
  140. RenderText(m_value,
  141. 0, // no offset
  142. wxRect(dc->GetTextExtent(m_value)).CentreIn(rect),
  143. dc,
  144. state);
  145. return true;
  146. }
  147. virtual bool ActivateCell(const wxRect& WXUNUSED(cell),
  148. wxDataViewModel *WXUNUSED(model),
  149. const wxDataViewItem &WXUNUSED(item),
  150. unsigned int WXUNUSED(col),
  151. const wxMouseEvent *mouseEvent)
  152. {
  153. wxString position;
  154. if ( mouseEvent )
  155. position = wxString::Format("via mouse at %d, %d", mouseEvent->m_x, mouseEvent->m_y);
  156. else
  157. position = "from keyboard";
  158. wxLogMessage("MyCustomRenderer ActivateCell() %s", position);
  159. return false;
  160. }
  161. virtual wxSize GetSize() const
  162. {
  163. return wxSize(60,20);
  164. }
  165. virtual bool SetValue( const wxVariant &value )
  166. {
  167. m_value = value.GetString();
  168. return true;
  169. }
  170. virtual bool GetValue( wxVariant &WXUNUSED(value) ) const { return true; }
  171. private:
  172. wxString m_value;
  173. };
  174. // ============================================================================
  175. // implementation
  176. // ============================================================================
  177. // ----------------------------------------------------------------------------
  178. // MyApp
  179. // ----------------------------------------------------------------------------
  180. IMPLEMENT_APP(MyApp)
  181. bool MyApp::OnInit()
  182. {
  183. if ( !wxApp::OnInit() )
  184. return false;
  185. MyFrame *frame =
  186. new MyFrame(NULL, "wxDataViewCtrl sample", 40, 40, 1000, 540);
  187. frame->Show(true);
  188. return true;
  189. }
  190. // ----------------------------------------------------------------------------
  191. // MyFrame
  192. // ----------------------------------------------------------------------------
  193. enum
  194. {
  195. ID_CLEARLOG = wxID_HIGHEST+1,
  196. ID_BACKGROUND_COLOUR,
  197. ID_FOREGROUND_COLOUR,
  198. ID_STYLE_MENU,
  199. // file menu
  200. //ID_SINGLE, wxDV_SINGLE==0 so it's always present
  201. ID_MULTIPLE,
  202. ID_ROW_LINES,
  203. ID_HORIZ_RULES,
  204. ID_VERT_RULES,
  205. ID_EXIT = wxID_EXIT,
  206. // about menu
  207. ID_ABOUT = wxID_ABOUT,
  208. // control IDs
  209. ID_MUSIC_CTRL = 50,
  210. ID_ATTR_CTRL = 51,
  211. ID_ADD_MOZART = 100,
  212. ID_DELETE_SEL = 101,
  213. ID_DELETE_YEAR = 102,
  214. ID_SELECT_NINTH = 103,
  215. ID_COLLAPSE = 104,
  216. ID_EXPAND = 105,
  217. ID_SHOW_CURRENT,
  218. ID_SET_NINTH_CURRENT,
  219. ID_PREPEND_LIST = 200,
  220. ID_DELETE_LIST = 201,
  221. ID_GOTO = 202,
  222. ID_ADD_MANY = 203,
  223. ID_HIDE_ATTRIBUTES = 204,
  224. ID_SHOW_ATTRIBUTES = 205,
  225. // Fourth page.
  226. ID_DELETE_TREE_ITEM = 400,
  227. ID_DELETE_ALL_TREE_ITEMS = 401,
  228. ID_ADD_TREE_ITEM = 402,
  229. ID_ADD_TREE_CONTAINER_ITEM = 403
  230. };
  231. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  232. EVT_MENU_RANGE( ID_MULTIPLE, ID_VERT_RULES, MyFrame::OnStyleChange )
  233. EVT_MENU( ID_EXIT, MyFrame::OnQuit )
  234. EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
  235. EVT_MENU( ID_CLEARLOG, MyFrame::OnClearLog )
  236. EVT_MENU( ID_FOREGROUND_COLOUR, MyFrame::OnSetForegroundColour )
  237. EVT_MENU( ID_BACKGROUND_COLOUR, MyFrame::OnSetBackgroundColour )
  238. EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY, MyFrame::OnPageChanged )
  239. EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
  240. EVT_BUTTON( ID_DELETE_SEL, MyFrame::OnDeleteSelected )
  241. EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
  242. EVT_BUTTON( ID_SELECT_NINTH, MyFrame::OnSelectNinth )
  243. EVT_BUTTON( ID_COLLAPSE, MyFrame::OnCollapse )
  244. EVT_BUTTON( ID_EXPAND, MyFrame::OnExpand )
  245. EVT_BUTTON( ID_SHOW_CURRENT, MyFrame::OnShowCurrent )
  246. EVT_BUTTON( ID_SET_NINTH_CURRENT, MyFrame::OnSetNinthCurrent )
  247. EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
  248. EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
  249. EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
  250. EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
  251. EVT_BUTTON( ID_HIDE_ATTRIBUTES, MyFrame::OnHideAttributes)
  252. EVT_BUTTON( ID_SHOW_ATTRIBUTES, MyFrame::OnShowAttributes)
  253. // Fourth page.
  254. EVT_BUTTON( ID_DELETE_TREE_ITEM, MyFrame::OnDeleteTreeItem )
  255. EVT_BUTTON( ID_DELETE_ALL_TREE_ITEMS, MyFrame::OnDeleteAllTreeItems )
  256. EVT_BUTTON( ID_ADD_TREE_ITEM, MyFrame::OnAddTreeItem )
  257. EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM, MyFrame::OnAddTreeContainerItem )
  258. EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
  259. EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
  260. EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
  261. EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
  262. EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
  263. EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
  264. EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
  265. EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL, MyFrame::OnStartEditing)
  266. EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
  267. EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
  268. EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
  269. EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
  270. EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
  271. EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
  272. #if wxUSE_DRAG_AND_DROP
  273. EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL, MyFrame::OnBeginDrag )
  274. EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL, MyFrame::OnDropPossible )
  275. EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL, MyFrame::OnDrop )
  276. #endif // wxUSE_DRAG_AND_DROP
  277. EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_ATTR_CTRL, MyFrame::OnAttrHeaderClick)
  278. wxEND_EVENT_TABLE()
  279. MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
  280. wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
  281. {
  282. m_log = NULL;
  283. m_col = NULL;
  284. m_ctrl[0] = NULL;
  285. m_ctrl[1] = NULL;
  286. m_ctrl[2] = NULL;
  287. m_ctrl[3] = NULL;
  288. SetIcon(wxICON(sample));
  289. // build the menus
  290. // ----------------
  291. wxMenu *style_menu = new wxMenu;
  292. //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
  293. style_menu->AppendCheckItem(ID_MULTIPLE, "Multiple selection");
  294. style_menu->AppendCheckItem(ID_ROW_LINES, "Alternating colours");
  295. style_menu->AppendCheckItem(ID_HORIZ_RULES, "Display horizontal rules");
  296. style_menu->AppendCheckItem(ID_VERT_RULES, "Display vertical rules");
  297. wxMenu *file_menu = new wxMenu;
  298. file_menu->Append(ID_CLEARLOG, "&Clear log\tCtrl-L");
  299. file_menu->Append(ID_FOREGROUND_COLOUR, "Set &foreground colour...\tCtrl-S");
  300. file_menu->Append(ID_BACKGROUND_COLOUR, "Set &background colour...\tCtrl-B");
  301. file_menu->Append(ID_STYLE_MENU, "&Style", style_menu);
  302. file_menu->AppendSeparator();
  303. file_menu->Append(ID_EXIT, "E&xit");
  304. wxMenu *about_menu = new wxMenu;
  305. about_menu->Append(ID_ABOUT, "&About");
  306. wxMenuBar *menu_bar = new wxMenuBar;
  307. menu_bar->Append(file_menu, "&File");
  308. menu_bar->Append(about_menu, "&About");
  309. SetMenuBar(menu_bar);
  310. CreateStatusBar();
  311. // redirect logs from our event handlers to text control
  312. m_log = new wxTextCtrl( this, wxID_ANY, wxString(), wxDefaultPosition,
  313. wxDefaultSize, wxTE_MULTILINE );
  314. m_log->SetMinSize(wxSize(-1, 100));
  315. m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
  316. wxLogMessage( "This is the log window" );
  317. // first page of the notebook
  318. // --------------------------
  319. m_notebook = new wxNotebook( this, wxID_ANY );
  320. wxPanel *firstPanel = new wxPanel( m_notebook, wxID_ANY );
  321. BuildDataViewCtrl(firstPanel, 0); // sets m_ctrl[0]
  322. const wxSizerFlags border = wxSizerFlags().DoubleBorder();
  323. wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
  324. button_sizer->Add( new wxButton( firstPanel, ID_ADD_MOZART, "Add Mozart"), border );
  325. button_sizer->Add( new wxButton( firstPanel, ID_DELETE_SEL, "Delete selected"), border );
  326. button_sizer->Add( new wxButton( firstPanel, ID_DELETE_YEAR, "Delete \"Year\" column"), border );
  327. button_sizer->Add( new wxButton( firstPanel, ID_SELECT_NINTH,"Select ninth symphony"), border );
  328. button_sizer->Add( new wxButton( firstPanel, ID_COLLAPSE, "Collapse"), border );
  329. button_sizer->Add( new wxButton( firstPanel, ID_EXPAND, "Expand"), border );
  330. wxBoxSizer *sizerCurrent = new wxBoxSizer(wxHORIZONTAL);
  331. sizerCurrent->Add(new wxButton(firstPanel, ID_SHOW_CURRENT,
  332. "&Show current"), border);
  333. sizerCurrent->Add(new wxButton(firstPanel, ID_SET_NINTH_CURRENT,
  334. "Make &ninth symphony current"), border);
  335. wxSizer *firstPanelSz = new wxBoxSizer( wxVERTICAL );
  336. m_ctrl[0]->SetMinSize(wxSize(-1, 200));
  337. firstPanelSz->Add(m_ctrl[0], 1, wxGROW|wxALL, 5);
  338. firstPanelSz->Add(
  339. new wxStaticText(firstPanel, wxID_ANY, "Most of the cells above are editable!"),
  340. 0, wxGROW|wxALL, 5);
  341. firstPanelSz->Add(button_sizer);
  342. firstPanelSz->Add(sizerCurrent);
  343. firstPanel->SetSizerAndFit(firstPanelSz);
  344. // second page of the notebook
  345. // ---------------------------
  346. wxPanel *secondPanel = new wxPanel( m_notebook, wxID_ANY );
  347. BuildDataViewCtrl(secondPanel, 1); // sets m_ctrl[1]
  348. wxBoxSizer *button_sizer2 = new wxBoxSizer( wxHORIZONTAL );
  349. button_sizer2->Add( new wxButton( secondPanel, ID_PREPEND_LIST,"Prepend"), 0, wxALL, 10 );
  350. button_sizer2->Add( new wxButton( secondPanel, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
  351. button_sizer2->Add( new wxButton( secondPanel, ID_GOTO, "Goto 50"), 0, wxALL, 10 );
  352. button_sizer2->Add( new wxButton( secondPanel, ID_ADD_MANY, "Add 1000"), 0, wxALL, 10 );
  353. button_sizer2->Add( new wxButton( secondPanel, ID_HIDE_ATTRIBUTES, "Hide attributes"), 0, wxALL, 10 );
  354. button_sizer2->Add( new wxButton( secondPanel, ID_SHOW_ATTRIBUTES, "Show attributes"), 0, wxALL, 10 );
  355. wxSizer *secondPanelSz = new wxBoxSizer( wxVERTICAL );
  356. secondPanelSz->Add(m_ctrl[1], 1, wxGROW|wxALL, 5);
  357. secondPanelSz->Add(button_sizer2);
  358. secondPanel->SetSizerAndFit(secondPanelSz);
  359. // third page of the notebook
  360. // ---------------------------
  361. wxPanel *thirdPanel = new wxPanel( m_notebook, wxID_ANY );
  362. BuildDataViewCtrl(thirdPanel, 2); // sets m_ctrl[2]
  363. wxSizer *thirdPanelSz = new wxBoxSizer( wxVERTICAL );
  364. thirdPanelSz->Add(m_ctrl[2], 1, wxGROW|wxALL, 5);
  365. thirdPanel->SetSizerAndFit(thirdPanelSz);
  366. // fourth page of the notebook
  367. // ---------------------------
  368. wxPanel *fourthPanel = new wxPanel( m_notebook, wxID_ANY );
  369. BuildDataViewCtrl(fourthPanel, 3); // sets m_ctrl[3]
  370. // Buttons
  371. wxBoxSizer *button_sizer4 = new wxBoxSizer( wxHORIZONTAL );
  372. button_sizer4->Add( new wxButton( fourthPanel, ID_DELETE_TREE_ITEM, "Delete Selected"), 0, wxALL, 10 );
  373. button_sizer4->Add( new wxButton( fourthPanel, ID_DELETE_ALL_TREE_ITEMS, "Delete All"), 0, wxALL, 10 );
  374. button_sizer4->Add( new wxButton( fourthPanel, ID_ADD_TREE_ITEM, "Add Item"), 0, wxALL, 10 );
  375. button_sizer4->Add( new wxButton( fourthPanel, ID_ADD_TREE_CONTAINER_ITEM, "Add Container"), 0, wxALL, 10 );
  376. wxSizer *fourthPanelSz = new wxBoxSizer( wxVERTICAL );
  377. fourthPanelSz->Add(m_ctrl[3], 1, wxGROW|wxALL, 5);
  378. fourthPanelSz->Add(button_sizer4);
  379. fourthPanel->SetSizerAndFit(fourthPanelSz);
  380. // complete GUI
  381. // ------------
  382. m_notebook->AddPage(firstPanel, "MyMusicTreeModel");
  383. m_notebook->AddPage(secondPanel, "MyListModel");
  384. m_notebook->AddPage(thirdPanel, "wxDataViewListCtrl");
  385. m_notebook->AddPage(fourthPanel, "wxDataViewTreeCtrl");
  386. wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
  387. mainSizer->Add( m_notebook, 1, wxGROW );
  388. mainSizer->Add( m_log, 0, wxGROW );
  389. SetSizerAndFit(mainSizer);
  390. }
  391. MyFrame::~MyFrame()
  392. {
  393. delete wxLog::SetActiveTarget(m_logOld);
  394. }
  395. void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned long style)
  396. {
  397. switch (nPanel)
  398. {
  399. case 0:
  400. {
  401. wxASSERT(!m_ctrl[0] && !m_music_model);
  402. m_ctrl[0] =
  403. new wxDataViewCtrl( parent, ID_MUSIC_CTRL, wxDefaultPosition,
  404. wxDefaultSize, style );
  405. m_ctrl[0]->Connect(wxEVT_CHAR,
  406. wxKeyEventHandler(MyFrame::OnDataViewChar),
  407. NULL, this);
  408. m_music_model = new MyMusicTreeModel;
  409. m_ctrl[0]->AssociateModel( m_music_model.get() );
  410. #if wxUSE_DRAG_AND_DROP && wxUSE_UNICODE
  411. m_ctrl[0]->EnableDragSource( wxDF_UNICODETEXT );
  412. m_ctrl[0]->EnableDropTarget( wxDF_UNICODETEXT );
  413. #endif // wxUSE_DRAG_AND_DROP && wxUSE_UNICODE
  414. // column 0 of the view control:
  415. wxDataViewTextRenderer *tr =
  416. new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT );
  417. wxDataViewColumn *column0 =
  418. new wxDataViewColumn( "title", tr, 0, 200, wxALIGN_LEFT,
  419. wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_RESIZABLE );
  420. m_ctrl[0]->AppendColumn( column0 );
  421. #if 0
  422. // Call this and sorting is enabled
  423. // immediatly upon start up.
  424. column0->SetAsSortKey();
  425. #endif
  426. // column 1 of the view control:
  427. tr = new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE );
  428. wxDataViewColumn *column1 =
  429. new wxDataViewColumn( "artist", tr, 1, 150, wxALIGN_LEFT,
  430. wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE |
  431. wxDATAVIEW_COL_RESIZABLE );
  432. column1->SetMinWidth(150); // this column can't be resized to be smaller
  433. m_ctrl[0]->AppendColumn( column1 );
  434. // column 2 of the view control:
  435. wxDataViewSpinRenderer *sr =
  436. new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
  437. wxDataViewColumn *column2 =
  438. new wxDataViewColumn( "year", sr, 2, 60, wxALIGN_LEFT,
  439. wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE );
  440. m_ctrl[0]->AppendColumn( column2 );
  441. // column 3 of the view control:
  442. wxArrayString choices;
  443. choices.Add( "good" );
  444. choices.Add( "bad" );
  445. choices.Add( "lousy" );
  446. wxDataViewChoiceRenderer *c =
  447. new wxDataViewChoiceRenderer( choices, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
  448. wxDataViewColumn *column3 =
  449. new wxDataViewColumn( "rating", c, 3, 100, wxALIGN_LEFT,
  450. wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
  451. m_ctrl[0]->AppendColumn( column3 );
  452. // column 4 of the view control:
  453. m_ctrl[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT, 80 );
  454. // column 5 of the view control:
  455. MyCustomRenderer *cr = new MyCustomRenderer;
  456. wxDataViewColumn *column5 =
  457. new wxDataViewColumn( "custom", cr, 5, -1, wxALIGN_LEFT,
  458. wxDATAVIEW_COL_RESIZABLE );
  459. m_ctrl[0]->AppendColumn( column5 );
  460. // select initially the ninth symphony:
  461. m_ctrl[0]->Select(m_music_model->GetNinthItem());
  462. }
  463. break;
  464. case 1:
  465. {
  466. wxASSERT(!m_ctrl[1] && !m_list_model);
  467. m_ctrl[1] = new wxDataViewCtrl( parent, ID_ATTR_CTRL, wxDefaultPosition,
  468. wxDefaultSize, style );
  469. m_list_model = new MyListModel;
  470. m_ctrl[1]->AssociateModel( m_list_model.get() );
  471. // the various columns
  472. m_ctrl[1]->AppendTextColumn("editable string",
  473. MyListModel::Col_EditableText,
  474. wxDATAVIEW_CELL_EDITABLE,
  475. wxCOL_WIDTH_AUTOSIZE);
  476. m_ctrl[1]->AppendIconTextColumn("icon",
  477. MyListModel::Col_IconText,
  478. wxDATAVIEW_CELL_EDITABLE,
  479. wxCOL_WIDTH_AUTOSIZE);
  480. m_attributes =
  481. new wxDataViewColumn("attributes",
  482. new wxDataViewTextRenderer,
  483. MyListModel::Col_TextWithAttr,
  484. wxCOL_WIDTH_AUTOSIZE,
  485. wxALIGN_RIGHT,
  486. wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
  487. m_ctrl[1]->AppendColumn( m_attributes );
  488. m_ctrl[1]->AppendColumn(
  489. new wxDataViewColumn("custom renderer",
  490. new MyCustomRenderer,
  491. MyListModel::Col_Custom)
  492. );
  493. }
  494. break;
  495. case 2:
  496. {
  497. wxASSERT(!m_ctrl[2]);
  498. wxDataViewListCtrl* lc =
  499. new wxDataViewListCtrl( parent, wxID_ANY, wxDefaultPosition,
  500. wxDefaultSize, style );
  501. m_ctrl[2] = lc;
  502. MyListStoreDerivedModel* page2_model = new MyListStoreDerivedModel();
  503. lc->AssociateModel(page2_model);
  504. page2_model->DecRef();
  505. lc->AppendToggleColumn( "Toggle" );
  506. lc->AppendTextColumn( "Text" );
  507. lc->AppendProgressColumn( "Progress" );
  508. wxVector<wxVariant> data;
  509. for (unsigned int i=0; i<10; i++)
  510. {
  511. data.clear();
  512. data.push_back( (i%3) == 0 );
  513. data.push_back( wxString::Format("row %d", i) );
  514. data.push_back( long(5*i) );
  515. lc->AppendItem( data );
  516. }
  517. }
  518. break;
  519. case 3:
  520. {
  521. wxASSERT(!m_ctrl[3]);
  522. wxDataViewTreeCtrl* tc =
  523. new wxDataViewTreeCtrl( parent, wxID_ANY, wxDefaultPosition,
  524. wxDefaultSize, style | wxDV_NO_HEADER );
  525. m_ctrl[3] = tc;
  526. wxImageList *ilist = new wxImageList( 16, 16 );
  527. ilist->Add( wxIcon(wx_small_xpm) );
  528. tc->AssignImageList( ilist );
  529. wxDataViewItem parent =
  530. tc->AppendContainer( wxDataViewItem(0), "The Root", 0 );
  531. tc->AppendItem( parent, "Child 1", 0 );
  532. tc->AppendItem( parent, "Child 2", 0 );
  533. tc->AppendItem( parent, "Child 3, very long, long, long, long", 0 );
  534. wxDataViewItem cont =
  535. tc->AppendContainer( parent, "Container child", 0 );
  536. tc->AppendItem( cont, "Child 4", 0 );
  537. tc->AppendItem( cont, "Child 5", 0 );
  538. tc->Expand(cont);
  539. }
  540. break;
  541. }
  542. }
  543. // ----------------------------------------------------------------------------
  544. // MyFrame - generic event handlers
  545. // ----------------------------------------------------------------------------
  546. void MyFrame::OnClearLog( wxCommandEvent& WXUNUSED(event) )
  547. {
  548. m_log->Clear();
  549. }
  550. void MyFrame::OnSetForegroundColour(wxCommandEvent& WXUNUSED(event))
  551. {
  552. wxDataViewCtrl * const dvc = m_ctrl[m_notebook->GetSelection()];
  553. wxColour col = wxGetColourFromUser(this, dvc->GetForegroundColour());
  554. if ( col.IsOk() )
  555. {
  556. dvc->SetForegroundColour(col);
  557. Refresh();
  558. }
  559. }
  560. void MyFrame::OnSetBackgroundColour(wxCommandEvent& WXUNUSED(event))
  561. {
  562. wxDataViewCtrl * const dvc = m_ctrl[m_notebook->GetSelection()];
  563. wxColour col = wxGetColourFromUser(this, dvc->GetBackgroundColour());
  564. if ( col.IsOk() )
  565. {
  566. dvc->SetBackgroundColour(col);
  567. Refresh();
  568. }
  569. }
  570. void MyFrame::OnPageChanged( wxBookCtrlEvent& WXUNUSED(event) )
  571. {
  572. unsigned int nPanel = m_notebook->GetSelection();
  573. GetMenuBar()->FindItem(ID_STYLE_MENU)->SetItemLabel(
  574. wxString::Format("Style of panel #%d", nPanel+1));
  575. for (unsigned int id = ID_MULTIPLE; id <= ID_VERT_RULES; id++)
  576. {
  577. unsigned long style = 0;
  578. switch (id)
  579. {
  580. /*case ID_SINGLE:
  581. style = wxDV_SINGLE;
  582. break;*/
  583. case ID_MULTIPLE:
  584. style = wxDV_MULTIPLE;
  585. break;
  586. case ID_ROW_LINES:
  587. style = wxDV_ROW_LINES;
  588. break;
  589. case ID_HORIZ_RULES:
  590. style = wxDV_HORIZ_RULES;
  591. break;
  592. case ID_VERT_RULES:
  593. style = wxDV_VERT_RULES;
  594. break;
  595. default:
  596. wxFAIL;
  597. }
  598. GetMenuBar()->FindItem(id)->Check( m_ctrl[nPanel]->HasFlag(style) );
  599. }
  600. }
  601. void MyFrame::OnStyleChange( wxCommandEvent& WXUNUSED(event) )
  602. {
  603. unsigned int nPanel = m_notebook->GetSelection();
  604. // build the style
  605. unsigned long style = 0;
  606. /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
  607. style |= wxDV_SINGLE;*/
  608. if (GetMenuBar()->FindItem(ID_MULTIPLE)->IsChecked())
  609. style |= wxDV_MULTIPLE;
  610. if (GetMenuBar()->FindItem(ID_ROW_LINES)->IsChecked())
  611. style |= wxDV_ROW_LINES;
  612. if (GetMenuBar()->FindItem(ID_HORIZ_RULES)->IsChecked())
  613. style |= wxDV_HORIZ_RULES;
  614. if (GetMenuBar()->FindItem(ID_VERT_RULES)->IsChecked())
  615. style |= wxDV_VERT_RULES;
  616. wxSizer* sz = m_ctrl[nPanel]->GetContainingSizer();
  617. wxASSERT(sz);
  618. sz->Detach(m_ctrl[nPanel]);
  619. wxDELETE(m_ctrl[nPanel]);
  620. m_ctrl[nPanel] = NULL;
  621. if (nPanel == 0)
  622. m_music_model.reset(NULL);
  623. else if (nPanel == 1)
  624. m_list_model.reset(NULL);
  625. // rebuild the DVC for the selected panel:
  626. BuildDataViewCtrl((wxPanel*)m_notebook->GetPage(nPanel), nPanel, style);
  627. sz->Prepend(m_ctrl[nPanel], 1, wxGROW|wxALL, 5);
  628. sz->Layout();
  629. }
  630. void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
  631. {
  632. Close(true);
  633. }
  634. void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
  635. {
  636. wxAboutDialogInfo info;
  637. info.SetName(_("DataView sample"));
  638. info.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
  639. info.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
  640. info.AddDeveloper("Robert Roebling");
  641. info.AddDeveloper("Francesco Montorsi");
  642. wxAboutBox(info);
  643. }
  644. // ----------------------------------------------------------------------------
  645. // MyFrame - event handlers for the first page
  646. // ----------------------------------------------------------------------------
  647. #if wxUSE_DRAG_AND_DROP
  648. void MyFrame::OnBeginDrag( wxDataViewEvent &event )
  649. {
  650. wxDataViewItem item( event.GetItem() );
  651. // only allow drags for item, not containers
  652. if (m_music_model->IsContainer( item ) )
  653. {
  654. event.Veto();
  655. return;
  656. }
  657. MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
  658. wxTextDataObject *obj = new wxTextDataObject;
  659. obj->SetText( node->m_title );
  660. event.SetDataObject( obj );
  661. event.SetDragFlags(wxDrag_AllowMove); // allows both copy and move
  662. }
  663. void MyFrame::OnDropPossible( wxDataViewEvent &event )
  664. {
  665. wxDataViewItem item( event.GetItem() );
  666. // only allow drags for item, not containers
  667. if (m_music_model->IsContainer( item ) )
  668. event.Veto();
  669. if (event.GetDataFormat() != wxDF_UNICODETEXT)
  670. event.Veto();
  671. }
  672. void MyFrame::OnDrop( wxDataViewEvent &event )
  673. {
  674. wxDataViewItem item( event.GetItem() );
  675. // only allow drops for item, not containers
  676. if (m_music_model->IsContainer( item ) )
  677. {
  678. event.Veto();
  679. return;
  680. }
  681. if (event.GetDataFormat() != wxDF_UNICODETEXT)
  682. {
  683. event.Veto();
  684. return;
  685. }
  686. wxTextDataObject obj;
  687. obj.SetData( wxDF_UNICODETEXT, event.GetDataSize(), event.GetDataBuffer() );
  688. wxLogMessage( "Text dropped: %s", obj.GetText() );
  689. }
  690. #endif // wxUSE_DRAG_AND_DROP
  691. void MyFrame::OnAddMozart( wxCommandEvent& WXUNUSED(event) )
  692. {
  693. m_music_model->AddToClassical( "Eine kleine Nachtmusik", "Wolfgang Mozart", 1787 );
  694. }
  695. void MyFrame::DeleteSelectedItems()
  696. {
  697. wxDataViewItemArray items;
  698. int len = m_ctrl[0]->GetSelections( items );
  699. for( int i = 0; i < len; i ++ )
  700. if (items[i].IsOk())
  701. m_music_model->Delete( items[i] );
  702. }
  703. void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) )
  704. {
  705. DeleteSelectedItems();
  706. }
  707. void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
  708. {
  709. m_ctrl[0]->DeleteColumn( m_ctrl[0]->GetColumn( 2 ) );
  710. FindWindow( ID_DELETE_YEAR )->Disable();
  711. }
  712. void MyFrame::OnSelectNinth( wxCommandEvent& WXUNUSED(event) )
  713. {
  714. if (!m_music_model->GetNinthItem().IsOk())
  715. {
  716. wxLogError( "Cannot select the ninth symphony: it was removed!" );
  717. return;
  718. }
  719. m_ctrl[0]->Select( m_music_model->GetNinthItem() );
  720. }
  721. void MyFrame::OnCollapse( wxCommandEvent& WXUNUSED(event) )
  722. {
  723. wxDataViewItem item = m_ctrl[0]->GetSelection();
  724. if (item.IsOk())
  725. m_ctrl[0]->Collapse( item );
  726. }
  727. void MyFrame::OnExpand( wxCommandEvent& WXUNUSED(event) )
  728. {
  729. wxDataViewItem item = m_ctrl[0]->GetSelection();
  730. if (item.IsOk())
  731. m_ctrl[0]->Expand( item );
  732. }
  733. void MyFrame::OnShowCurrent(wxCommandEvent& WXUNUSED(event))
  734. {
  735. wxDataViewItem item = m_ctrl[0]->GetCurrentItem();
  736. if ( item.IsOk() )
  737. {
  738. wxLogMessage("Current item: \"%s\" by %s",
  739. m_music_model->GetTitle(item),
  740. m_music_model->GetArtist(item));
  741. }
  742. else
  743. {
  744. wxLogMessage("There is no current item.");
  745. }
  746. wxDataViewColumn *col = m_ctrl[0]->GetCurrentColumn();
  747. if ( col )
  748. {
  749. wxLogMessage("Current column: %d",
  750. m_ctrl[0]->GetColumnPosition(col));
  751. }
  752. else
  753. {
  754. wxLogMessage("There is no current column.");
  755. }
  756. }
  757. void MyFrame::OnSetNinthCurrent(wxCommandEvent& WXUNUSED(event))
  758. {
  759. wxDataViewItem item(m_music_model->GetNinthItem());
  760. if ( !item.IsOk() )
  761. {
  762. wxLogError( "Cannot make the ninth symphony current: it was removed!" );
  763. return;
  764. }
  765. m_ctrl[0]->SetCurrentItem(item);
  766. }
  767. void MyFrame::OnValueChanged( wxDataViewEvent &event )
  768. {
  769. wxString title = m_music_model->GetTitle( event.GetItem() );
  770. wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %s; Column: %d",
  771. title, event.GetColumn() );
  772. }
  773. void MyFrame::OnActivated( wxDataViewEvent &event )
  774. {
  775. wxString title = m_music_model->GetTitle( event.GetItem() );
  776. wxLogMessage( "wxEVT_DATAVIEW_ITEM_ACTIVATED, Item: %s", title );
  777. if (m_ctrl[0]->IsExpanded( event.GetItem() ))
  778. {
  779. wxLogMessage( "Item: %s is expanded", title );
  780. }
  781. }
  782. void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
  783. {
  784. wxString title = m_music_model->GetTitle( event.GetItem() );
  785. if (title.empty())
  786. title = "None";
  787. wxLogMessage( "wxEVT_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title );
  788. }
  789. void MyFrame::OnExpanding( wxDataViewEvent &event )
  790. {
  791. wxString title = m_music_model->GetTitle( event.GetItem() );
  792. wxLogMessage( "wxEVT_DATAVIEW_ITEM_EXPANDING, Item: %s", title );
  793. }
  794. void MyFrame::OnStartEditing( wxDataViewEvent &event )
  795. {
  796. wxString artist = m_music_model->GetArtist( event.GetItem() );
  797. if (artist == "Ludwig van Beethoven")
  798. {
  799. event.Veto();
  800. wxLogMessage( "wxEVT_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist );
  801. }
  802. else
  803. wxLogMessage( "wxEVT_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist );
  804. }
  805. void MyFrame::OnEditingStarted( wxDataViewEvent &event )
  806. {
  807. wxString title = m_music_model->GetTitle( event.GetItem() );
  808. wxLogMessage( "wxEVT_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title );
  809. }
  810. void MyFrame::OnEditingDone( wxDataViewEvent &event )
  811. {
  812. wxString title = m_music_model->GetTitle( event.GetItem() );
  813. wxLogMessage( "wxEVT_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title );
  814. }
  815. void MyFrame::OnExpanded( wxDataViewEvent &event )
  816. {
  817. wxString title = m_music_model->GetTitle( event.GetItem() );
  818. wxLogMessage( "wxEVT_DATAVIEW_ITEM_EXPANDED, Item: %s", title );
  819. }
  820. void MyFrame::OnCollapsing( wxDataViewEvent &event )
  821. {
  822. wxString title = m_music_model->GetTitle( event.GetItem() );
  823. wxLogMessage( "wxEVT_DATAVIEW_ITEM_COLLAPSING, Item: %s", title );
  824. }
  825. void MyFrame::OnCollapsed( wxDataViewEvent &event )
  826. {
  827. wxString title = m_music_model->GetTitle( event.GetItem() );
  828. wxLogMessage( "wxEVT_DATAVIEW_ITEM_COLLAPSED, Item: %s", title );
  829. }
  830. void MyFrame::OnContextMenu( wxDataViewEvent &event )
  831. {
  832. wxString title = m_music_model->GetTitle( event.GetItem() );
  833. wxLogMessage( "wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title );
  834. wxMenu menu;
  835. menu.Append( 1, "menuitem 1" );
  836. menu.Append( 2, "menuitem 2" );
  837. menu.Append( 3, "menuitem 3" );
  838. m_ctrl[0]->PopupMenu(&menu);
  839. }
  840. void MyFrame::OnAttrHeaderClick( wxDataViewEvent &event )
  841. {
  842. // we need to skip the event to let the default behaviour of sorting by
  843. // this column when it is clicked to take place
  844. event.Skip();
  845. int pos = m_ctrl[1]->GetColumnPosition( event.GetDataViewColumn() );
  846. wxLogMessage( "wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
  847. wxLogMessage( "Column title: %s Column width: %d", event.GetDataViewColumn()->GetTitle(), event.GetDataViewColumn()->GetWidth() );
  848. }
  849. void MyFrame::OnHeaderClick( wxDataViewEvent &event )
  850. {
  851. // we need to skip the event to let the default behaviour of sorting by
  852. // this column when it is clicked to take place
  853. event.Skip();
  854. int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
  855. wxLogMessage( "wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
  856. wxLogMessage( "Column width: %d", event.GetDataViewColumn()->GetWidth() );
  857. }
  858. void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
  859. {
  860. int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
  861. wxLogMessage( "wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos );
  862. }
  863. void MyFrame::OnSorted( wxDataViewEvent &event )
  864. {
  865. int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
  866. wxLogMessage( "wxEVT_DATAVIEW_COLUMN_SORTED, Column position: %d", pos );
  867. }
  868. void MyFrame::OnDataViewChar(wxKeyEvent& event)
  869. {
  870. if ( event.GetKeyCode() == WXK_DELETE )
  871. DeleteSelectedItems();
  872. else
  873. event.Skip();
  874. }
  875. // ----------------------------------------------------------------------------
  876. // MyFrame - event handlers for the second page
  877. // ----------------------------------------------------------------------------
  878. void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
  879. {
  880. m_list_model->Prepend("Test");
  881. }
  882. void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
  883. {
  884. wxDataViewItemArray items;
  885. int len = m_ctrl[1]->GetSelections( items );
  886. if (len > 0)
  887. m_list_model->DeleteItems( items );
  888. }
  889. void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
  890. {
  891. wxDataViewItem item = m_list_model->GetItem( 50 );
  892. m_ctrl[1]->EnsureVisible(item,m_col);
  893. }
  894. void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
  895. {
  896. m_list_model->AddMany();
  897. }
  898. void MyFrame::OnHideAttributes(wxCommandEvent& WXUNUSED(event))
  899. {
  900. m_attributes->SetHidden(true);
  901. }
  902. void MyFrame::OnShowAttributes(wxCommandEvent& WXUNUSED(event))
  903. {
  904. m_attributes->SetHidden(false);
  905. }
  906. // ----------------------------------------------------------------------------
  907. // MyFrame - event handlers for the fourth page
  908. // ----------------------------------------------------------------------------
  909. void MyFrame::OnDeleteTreeItem(wxCommandEvent& WXUNUSED(event))
  910. {
  911. wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
  912. wxDataViewItem selected = ctrl->GetSelection();
  913. if (!selected.IsOk())
  914. return;
  915. ctrl->DeleteItem(selected);
  916. }
  917. void MyFrame::OnDeleteAllTreeItems(wxCommandEvent& WXUNUSED(event))
  918. {
  919. wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
  920. ctrl->DeleteAllItems();
  921. }
  922. void MyFrame::OnAddTreeItem(wxCommandEvent& WXUNUSED(event))
  923. {
  924. wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
  925. wxDataViewItem selected = ctrl->GetSelection();
  926. if (ctrl->IsContainer(selected)) {
  927. wxDataViewItem newitem = ctrl->AppendItem( selected, "Item", 0 );
  928. ctrl->Select(newitem);
  929. ctrl->EditItem(newitem, ctrl->GetColumn(0));
  930. }
  931. }
  932. void MyFrame::OnAddTreeContainerItem(wxCommandEvent& WXUNUSED(event))
  933. {
  934. wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
  935. wxDataViewItem selected = ctrl->GetSelection();
  936. if (ctrl->IsContainer(selected))
  937. ctrl->AppendContainer(selected, "Container", 0 );
  938. }