1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/datetime.h"
24 #include "wx/splitter.h"
25 #include "wx/aboutdlg.h"
26 #include "wx/choicdlg.h"
27 #include "wx/numdlg.h"
28 #include "wx/dataview.h"
31 #include "../sample.xpm"
37 #define DEFAULT_ALIGN wxALIGN_LEFT
38 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
42 // -------------------------------------
44 // -------------------------------------
46 WX_DECLARE_OBJARRAY(wxDateTime
,wxArrayDate
);
47 #include <wx/arrimpl.cpp>
48 WX_DEFINE_OBJARRAY(wxArrayDate
)
50 class MyTextModel
: public wxDataViewListModel
56 for (i
= 0; i
< 1000; i
++)
57 m_list
.Add( wxT("Test") );
58 for (i
= 0; i
< 500; i
++)
59 { m_bools
.Add( 0 ); m_bools
.Add( 1 ); }
60 for (i
= 0; i
< 500; i
++)
61 { m_colours
.Add( wxT("red") ); m_colours
.Add( wxT("green") ); }
62 for (i
= 0; i
< 1000; i
++)
63 { m_progress
.Add( i
/10 ); }
64 for (i
= 0; i
< 1000; i
++)
66 wxDateTime
date( wxDateTime::Now() );
71 virtual unsigned int GetRowCount() const
73 virtual unsigned int GetColumnCount() const
76 // as reported by wxVariant
77 virtual wxString
GetColumnType( unsigned int col
) const
80 return wxT("datetime");
91 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
) const
95 variant
= (wxDateTime
) m_dates
[row
];
99 variant
= (long) m_progress
[row
];
103 variant
= m_colours
[row
];
107 variant
= (bool) (m_bools
[row
] != 0);
111 variant
= m_list
[row
];
116 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
120 virtual bool SetValue( wxVariant
&value
, unsigned int col
, unsigned int row
)
124 m_dates
[row
] = value
.GetDateTime();
128 m_bools
[row
] = (int) value
.GetBool();
132 m_list
[row
] = value
.GetString();
137 wxArrayString m_list
;
139 wxArrayString m_colours
;
140 wxArrayInt m_progress
;
144 // -------------------------------------
146 // -------------------------------------
148 class MyTextRenderer
: public wxDataViewTextRenderer
151 MyTextRenderer( const wxString varianttype
= wxT("string"),
152 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
)
153 : wxDataViewTextRenderer( varianttype
, mode
) { }
155 virtual bool Validate( wxVariant
&value
)
157 wxString str
= value
;
158 if (str
.Length() > 10)
160 wxMessageDialog
dlg( NULL
, wxT("string too long") , wxT("Error") );
171 // -------------------------------------
173 // -------------------------------------
175 class MyCustomRenderer
: public wxDataViewCustomRenderer
179 wxDataViewCustomRenderer( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE
)
181 m_colour
= wxT("black");
183 bool SetValue( const wxVariant
&value
)
185 m_colour
= value
.GetString();
189 bool GetValue( wxVariant
&value
) const
195 bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
197 dc
->SetPen( *wxBLACK_PEN
);
198 if (m_colour
== wxT("red"))
199 dc
->SetBrush( *wxRED_BRUSH
);
200 else if (m_colour
== wxT("green"))
201 dc
->SetBrush( *wxGREEN_BRUSH
);
203 dc
->SetBrush( *wxBLACK_BRUSH
);
204 dc
->DrawRectangle( rect
);
207 wxSize
GetSize() const
211 bool Activate( wxRect
WXUNUSED(rect
),
212 wxDataViewListModel
*WXUNUSED(model
),
213 unsigned int WXUNUSED(col
),
214 unsigned int WXUNUSED(row
) )
223 // -------------------------------------
224 // MyUnsortedTextModel
225 // -------------------------------------
227 class MyUnsortedTextModel
: public wxDataViewListModel
230 MyUnsortedTextModel()
232 m_list
.Add( wxT("This") );
233 m_list
.Add( wxT("is") );
234 m_list
.Add( wxT("an") );
235 m_list
.Add( wxT("unsorted") );
236 m_list
.Add( wxT("list") );
237 m_list
.Add( wxT("of") );
238 m_list
.Add( wxT("words.") );
240 m_bitmap
= wxBitmap( null_xpm
);
243 virtual unsigned int GetRowCount() const
245 return m_list
.GetCount();
248 virtual unsigned int GetColumnCount() const
253 virtual wxString
GetColumnType( unsigned int WXUNUSED(col
) ) const
255 return wxT("string");
258 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
) const
262 variant
= m_list
[row
];
265 if ((col
== 2) || (col
== 3))
271 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
275 virtual bool SetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
279 m_list
[row
] = variant
.GetString();
286 void AppendRow( const wxString
&text
)
292 void PrependRow( const wxString
&text
)
294 m_list
.Insert( text
, 0 );
298 void InsertRowAt1( const wxString
&text
)
300 m_list
.Insert( text
, 1 );
304 void DeleteRow( unsigned int index
)
306 m_list
.RemoveAt( index
);
310 wxArrayString m_list
;
314 // -------------------------------------
316 // -------------------------------------
318 class MyApp
: public wxApp
325 // -------------------------------------
327 // -------------------------------------
329 class MyFrame
: public wxFrame
332 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
335 void OnQuit(wxCommandEvent
& event
);
336 void OnAbout(wxCommandEvent
& event
);
337 void OnNewSortingFrame(wxCommandEvent
& event
);
339 void OnStyleChange(wxCommandEvent
& event
);
340 void OnColumnSetting(wxCommandEvent
& event
);
343 wxDataViewCtrl
* dataview_left
;
344 wxDataViewCtrl
* dataview_right
;
345 wxSplitterWindow
*m_splitter
;
346 wxPanel
*m_panelLeft
, *m_panelRight
;
350 void CreateDataViewControls();
352 wxArrayInt
GetFlaggedColumns(int flag
);
353 wxAlignment
ChooseAlign(const wxString
&msg
, bool onlyHorizontal
);
354 void SetFlag(const wxArrayInt
&idx
, int flag
);
355 void SetAlignment(const wxArrayInt
&idx
, bool header
, wxAlignment align
);
356 void SetWidth(const wxArrayInt
&idx
, bool minwidth
, int width
);
359 DECLARE_EVENT_TABLE()
362 // -------------------------------------
364 // -------------------------------------
368 ID_APPEND_ROW_LEFT
= 1000,
378 ID_PREPEND_ROW_RIGHT
,
388 class MySortingFrame
: public wxFrame
391 MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
395 void OnQuit(wxCommandEvent
& event
);
396 void OnAbout(wxCommandEvent
& event
);
398 void OnAppendRowLeft(wxCommandEvent
& event
);
399 void OnPrependRowLeft(wxCommandEvent
& event
);
400 void OnInsertRowLeft(wxCommandEvent
& event
);
401 void OnDeleteRowLeft(wxCommandEvent
& event
);
402 void OnEditRowLeft(wxCommandEvent
& event
);
404 void OnAppendRowRight(wxCommandEvent
& event
);
405 void OnPrependRowRight(wxCommandEvent
& event
);
406 void OnInsertRowRight(wxCommandEvent
& event
);
407 void OnDeleteRowRight(wxCommandEvent
& event
);
408 void OnEditRowRight(wxCommandEvent
& event
);
410 void OnSelect(wxCommandEvent
& event
);
411 void OnUnselectAll(wxCommandEvent
& event
);
413 void OnSelectedUnsorted(wxDataViewEvent
&event
);
414 void OnSelectedSorted(wxDataViewEvent
&event
);
415 void OnActivatedUnsorted(wxDataViewEvent
&event
);
417 void OnHeaderClickSorted(wxDataViewEvent
&event
);
418 void OnHeaderClickUnsorted(wxDataViewEvent
&event
);
421 wxDataViewCtrl
* dataview_left
;
422 wxDataViewCtrl
* dataview_right
;
425 wxTextCtrl
*m_logWindow
;
427 wxObjectDataPtr
<MyUnsortedTextModel
> m_unsorted_model
;
428 wxObjectDataPtr
<wxDataViewSortedListModel
> m_sorted_model
;
430 DECLARE_EVENT_TABLE()
433 // -------------------------------------
435 // -------------------------------------
439 bool MyApp::OnInit(void)
441 if ( !wxApp::OnInit() )
444 // build the first frame
446 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
459 // -------------------------------------
461 // -------------------------------------
466 ID_ABOUT
= wxID_ABOUT
,
467 ID_NEW_SORT_FRAME
= wxID_HIGHEST
+1,
471 ID_SINGLE_SEL_MODE
= wxID_HIGHEST
+2,
472 ID_MULTIPLE_SEL_MODE
,
477 ID_RESIZEABLE_COLUMNS
,
481 ID_CHOOSE_COLUMN_ALIGNMENT
,
482 ID_CHOOSE_CONTENTS_ALIGNMENT
,
488 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
491 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
492 EVT_MENU( ID_NEW_SORT_FRAME
, MyFrame::OnNewSortingFrame
)
493 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
496 EVT_COMMAND_RANGE( ID_SINGLE_SEL_MODE
, ID_VERT_RULES_MODE
,
497 wxEVT_COMMAND_MENU_SELECTED
, MyFrame::OnStyleChange
)
499 EVT_COMMAND_RANGE( ID_RESIZEABLE_COLUMNS
, ID_SET_WIDTH
,
500 wxEVT_COMMAND_MENU_SELECTED
, MyFrame::OnColumnSetting
)
504 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
505 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
507 SetIcon(wxICON(sample
));
511 wxMenu
*file_menu
= new wxMenu
;
512 file_menu
->Append(ID_NEW_SORT_FRAME
, _T("&New sorting frame"));
513 file_menu
->AppendSeparator();
514 file_menu
->Append(ID_ABOUT
, _T("&About"));
515 file_menu
->AppendSeparator();
516 file_menu
->Append(ID_EXIT
, _T("E&xit"));
518 wxMenu
*data_menu
= new wxMenu
;
519 data_menu
->AppendRadioItem(ID_SINGLE_SEL_MODE
, _T("&Single selection mode"));
520 data_menu
->AppendRadioItem(ID_MULTIPLE_SEL_MODE
, _T("&Multiple selection mode"));
521 data_menu
->AppendSeparator();
522 data_menu
->AppendCheckItem(ID_NO_HEADER_MODE
, _T("No header mode"));
523 data_menu
->AppendCheckItem(ID_HORIZ_RULES_MODE
, _T("Horizontal rules"));
524 data_menu
->AppendCheckItem(ID_VERT_RULES_MODE
, _T("Vertical rules"));
525 data_menu
->AppendSeparator();
526 data_menu
->Append(ID_RESIZEABLE_COLUMNS
, _T("Set column(s) as resizeable..."));
527 data_menu
->Append(ID_SORTABLE_COLUMNS
, _T("Set column(s) as sortable..."));
528 data_menu
->Append(ID_HIDDEN_COLUMNS
, _T("Set column(s) as hidden..."));
529 data_menu
->AppendSeparator();
530 data_menu
->Append(ID_CHOOSE_COLUMN_ALIGNMENT
, _T("Set column(s) title alignment..."));
531 data_menu
->Append(ID_CHOOSE_CONTENTS_ALIGNMENT
, _T("Set column(s) contents alignment..."));
532 data_menu
->AppendSeparator();
533 data_menu
->Append(ID_SET_MINWIDTH
, _T("Set column(s) minimal width..."));
534 data_menu
->Append(ID_SET_WIDTH
, _T("Set column(s) width..."));
536 wxMenuBar
*menu_bar
= new wxMenuBar
;
537 menu_bar
->Append(file_menu
, _T("&File"));
538 menu_bar
->Append(data_menu
, _T("&DataViewCtrl"));
540 SetMenuBar(menu_bar
);
544 // check the menus for the default wxDataViewCtrl style
545 data_menu
->Check(ID_MULTIPLE_SEL_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_MULTIPLE
) != 0);
546 data_menu
->Check(ID_NO_HEADER_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_NO_HEADER
) != 0);
547 data_menu
->Check(ID_HORIZ_RULES_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_HORIZ_RULES
) != 0);
548 data_menu
->Check(ID_VERT_RULES_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_VERT_RULES
) != 0);
551 // build the other controls:
553 m_splitter
= new wxSplitterWindow( this, wxID_ANY
);
554 m_splitter
->SetSashGravity(0.5);
556 m_panelLeft
= new wxPanel( m_splitter
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
558 m_panelRight
= new wxPanel( m_splitter
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
560 wxSizer
*szLeft
= new wxBoxSizer(wxVERTICAL
);
561 wxSizer
*szRight
= new wxBoxSizer(wxVERTICAL
);
563 dataview_left
= NULL
;
564 dataview_right
= NULL
;
565 CreateDataViewControls();
568 szLeft
->Add( dataview_left
, 1, wxGROW
|wxALL
, 5 );
569 m_panelLeft
->SetSizerAndFit(szLeft
);
572 wxStaticText
*stattext
=
573 new wxStaticText(m_panelRight
, wxID_ANY
,
574 wxT("This is another wxDataViewCtrl using the same wxDataViewModel ")
575 wxT("of the wxDataViewCtrl on the left but, unlike it, this window ")
576 wxT("won't react to the style/column changes done through the ")
577 wxT("'DataViewCtrl' menu"));
578 stattext
->Wrap(GetClientSize().GetWidth() / 2);
580 szRight
->Add( stattext
, 0, wxALL
, 5 );
581 szRight
->Add( dataview_right
, 1, wxGROW
|wxALL
, 5 );
582 m_panelRight
->SetSizerAndFit(szRight
);
584 // split the two panels
585 m_splitter
->SplitVertically(m_panelLeft
, m_panelRight
);
586 this->SetMinSize(m_splitter
->GetBestSize());
589 void MyFrame::CreateDataViewControls()
591 wxDataViewCtrl
*old1
= NULL
, *old2
= NULL
;
594 old1
= dataview_left
;
596 old2
= dataview_right
;
600 if (GetMenuBar()->FindItem(ID_MULTIPLE_SEL_MODE
)->IsChecked())
601 style
|= wxDV_MULTIPLE
;
602 if (GetMenuBar()->FindItem(ID_NO_HEADER_MODE
)->IsChecked())
603 style
|= wxDV_NO_HEADER
;
604 if (GetMenuBar()->FindItem(ID_HORIZ_RULES_MODE
)->IsChecked())
605 style
|= wxDV_HORIZ_RULES
;
606 if (GetMenuBar()->FindItem(ID_VERT_RULES_MODE
)->IsChecked())
607 style
|= wxDV_VERT_RULES
;
610 // Left wxDataViewCtrl
611 dataview_left
= new wxDataViewCtrl( m_panelLeft
, wxID_ANY
, wxDefaultPosition
,
612 wxDefaultSize
, style
);
615 wxObjectDataPtr
<MyTextModel
> model(new MyTextModel
);
616 dataview_left
->AssociateModel( model
.get() );
618 dataview_left
->AppendTextColumn( wxT("First"), 0, wxDATAVIEW_CELL_INERT
, -1,
620 dataview_left
->AppendTextColumn( wxT("Second"), 1, wxDATAVIEW_CELL_INERT
, -1,
623 wxDataViewTextRenderer
*text_renderer
=
624 new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
625 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 2,
627 dataview_left
->AppendColumn( column
);
629 dataview_left
->AppendToggleColumn( wxT("fourth"), 3, wxDATAVIEW_CELL_INERT
, -1,
632 MyCustomRenderer
*custom_renderer
= new MyCustomRenderer
;
633 column
= new wxDataViewColumn( wxT("custom"), custom_renderer
, 4, -1, DEFAULT_ALIGN
);
634 dataview_left
->AppendColumn( column
);
636 dataview_left
->AppendProgressColumn( wxT("progress"), 5, wxDATAVIEW_CELL_INERT
, -1,
639 dataview_left
->AppendDateColumn( wxT("date"), 6, wxDATAVIEW_CELL_INERT
, -1, DEFAULT_ALIGN
);
642 // Right wxDataViewCtrl using the same model
643 dataview_right
= new wxDataViewCtrl( m_panelRight
, wxID_ANY
);
644 dataview_right
->AssociateModel( model
.get() );
646 text_renderer
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
647 column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 2 );
648 dataview_right
->AppendColumn( column
);
649 dataview_right
->AppendTextColumn( wxT("first"), 0 );
650 dataview_right
->AppendTextColumn( wxT("second"), 1 );
651 wxDataViewToggleRenderer
*toggle_renderer
=
652 new wxDataViewToggleRenderer( wxT("bool"), wxDATAVIEW_CELL_ACTIVATABLE
);
653 column
= new wxDataViewColumn( wxT("bool"), toggle_renderer
, 3, 30 );
654 dataview_right
->AppendColumn( column
);
656 dataview_right
->AppendDateColumn( wxT("date"), 6 );
659 // layout the new dataview controls
662 m_panelLeft
->GetSizer()->Replace(old1
, dataview_left
);
665 m_panelLeft
->Layout();
670 m_panelRight
->GetSizer()->Replace(old2
, dataview_right
);
673 m_panelRight
->Layout();
677 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
682 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
684 wxAboutDialogInfo info
;
685 info
.SetName(_("DataView sample"));
686 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
687 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));
692 void MyFrame::OnNewSortingFrame(wxCommandEvent
& WXUNUSED(event
) )
694 MySortingFrame
*frame2
=
695 new MySortingFrame(NULL
, wxT("wxDataViewCtrl sorting test"), 10, 150, 600, 500);
699 void MyFrame::OnStyleChange(wxCommandEvent
& WXUNUSED(event
) )
701 // recreate the wxDataViewCtrl:
702 CreateDataViewControls();
705 void MyFrame::OnColumnSetting(wxCommandEvent
& event
)
709 bool header
= false, minwidth
= false;
712 switch (event
.GetId())
714 case ID_RESIZEABLE_COLUMNS
:
715 flag
= wxDATAVIEW_COL_RESIZABLE
;
716 columns
= GetFlaggedColumns(flag
);
718 case ID_SORTABLE_COLUMNS
:
719 flag
= wxDATAVIEW_COL_SORTABLE
;
720 columns
= GetFlaggedColumns(flag
);
722 case ID_HIDDEN_COLUMNS
:
723 flag
= wxDATAVIEW_COL_HIDDEN
;
724 columns
= GetFlaggedColumns(flag
);
727 case ID_CHOOSE_COLUMN_ALIGNMENT
:
728 msg
= wxT("Select the columns whose headers' alignment will be modified.");
731 case ID_CHOOSE_CONTENTS_ALIGNMENT
:
732 msg
= wxT("Select the columns whose contents' alignment will be modified.");
736 case ID_SET_MINWIDTH
:
737 msg
= wxT("Please provide the new minimal width:");
741 msg
= wxT("Please provide the new width:");
746 // get column titles:
748 wxArrayString choices
;
749 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
750 choices
.Add(dataview_left
->GetColumn(i
)->GetTitle());
753 wxGetMultipleChoices(
755 wxT("Choose the columns to which apply the change."),
756 wxT("Choose the column"),
760 switch (event
.GetId())
762 case ID_RESIZEABLE_COLUMNS
:
763 case ID_SORTABLE_COLUMNS
:
764 case ID_HIDDEN_COLUMNS
:
765 SetFlag(columns
, flag
);
768 case ID_CHOOSE_COLUMN_ALIGNMENT
:
769 case ID_CHOOSE_CONTENTS_ALIGNMENT
:
770 SetAlignment(columns
, header
, ChooseAlign(msg
, header
));
773 case ID_SET_MINWIDTH
:
776 int def
= minwidth
? wxDVC_DEFAULT_MINWIDTH
: wxDVC_DEFAULT_WIDTH
;
778 msg
<< wxT("\nNOTE: all non-selected columns will be reset to a width of ")
779 << def
<< wxT(" pixels.");
782 wxGetNumberFromUser(msg
, wxT("New value:"), wxT("Modify width"),
786 SetWidth(columns
, minwidth
, ret
);
791 dataview_left
->Refresh();
794 wxAlignment
MyFrame::ChooseAlign(const wxString
&msg
, bool onlyHorizontal
)
796 const wxString choices
[] =
799 wxT("Center horizontally"),
802 wxT("Center vertically"),
807 wxAlignment flags
[] =
810 wxALIGN_CENTER_HORIZONTAL
,
813 wxALIGN_CENTER_VERTICAL
,
818 int n
= WXSIZEOF(choices
);
820 n
= 3; // show only the first three choices
822 int choice
= wxGetSingleChoiceIndex(
823 msg
+ wxT("\nNOTE: _all_ non-selected column's alignment will be reset to wxALIGN_LEFT!"),
828 if (choice
== wxNOT_FOUND
)
831 return flags
[choice
];
834 void MyFrame::SetFlag(const wxArrayInt
&idx
, int toadd
)
836 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
838 int current
= dataview_left
->GetColumn(i
)->GetFlags();
840 if (idx
.Index(i
) != wxNOT_FOUND
)
841 dataview_left
->GetColumn(i
)->SetFlags(current
| toadd
);
843 dataview_left
->GetColumn(i
)->SetFlags(current
& ~toadd
);
847 wxArrayInt
MyFrame::GetFlaggedColumns(int flag
)
850 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
851 if (dataview_left
->GetColumn(i
)->GetFlags() & flag
)
856 void MyFrame::SetAlignment(const wxArrayInt
&idx
, bool header
, wxAlignment align
)
858 // set to DEFAULT_ALIGN all columns except those
859 // contained in 'idx' which are set to 'align'
861 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
863 wxAlignment toset
= DEFAULT_ALIGN
;
864 if (idx
.Index(i
) != wxNOT_FOUND
)
868 dataview_left
->GetColumn(i
)->SetAlignment(toset
);
870 dataview_left
->GetColumn(i
)->GetRenderer()->SetAlignment(toset
);
874 void MyFrame::SetWidth(const wxArrayInt
&idx
, bool minwidth
, int width
)
876 // set to wxDVC_DEFAULT_WIDTH wide all columns except those
877 // contained in 'idx' which are set to 'width'
879 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
881 int toset
= minwidth
? wxDVC_DEFAULT_MINWIDTH
: wxDVC_DEFAULT_WIDTH
;
882 if (idx
.Index(i
) != wxNOT_FOUND
)
886 dataview_left
->GetColumn(i
)->SetMinWidth(toset
);
888 dataview_left
->GetColumn(i
)->SetWidth(toset
);
893 // -------------------------------------
895 // -------------------------------------
897 BEGIN_EVENT_TABLE(MySortingFrame
,wxFrame
)
898 EVT_BUTTON( ID_APPEND_ROW_LEFT
, MySortingFrame::OnAppendRowLeft
)
899 EVT_BUTTON( ID_PREPEND_ROW_LEFT
, MySortingFrame::OnPrependRowLeft
)
900 EVT_BUTTON( ID_INSERT_ROW_LEFT
, MySortingFrame::OnInsertRowLeft
)
901 EVT_BUTTON( ID_DELETE_ROW_LEFT
, MySortingFrame::OnDeleteRowLeft
)
902 EVT_BUTTON( ID_SELECT
, MySortingFrame::OnSelect
)
903 EVT_BUTTON( ID_UNSELECT_ALL
, MySortingFrame::OnUnselectAll
)
904 EVT_DATAVIEW_ROW_SELECTED( ID_SORTED
, MySortingFrame::OnSelectedSorted
)
905 EVT_DATAVIEW_ROW_SELECTED( ID_UNSORTED
, MySortingFrame::OnSelectedUnsorted
)
906 EVT_DATAVIEW_ROW_ACTIVATED( ID_UNSORTED
, MySortingFrame::OnActivatedUnsorted
)
907 EVT_DATAVIEW_COLUMN_HEADER_CLICK( ID_SORTED
, MySortingFrame::OnHeaderClickSorted
)
908 EVT_DATAVIEW_COLUMN_HEADER_CLICK( ID_UNSORTED
, MySortingFrame::OnHeaderClickUnsorted
)
911 MySortingFrame::MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
912 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
916 SetIcon(wxICON(sample
));
919 wxPanel
*main
= new wxPanel(this);
921 // Left wxDataViewCtrl
922 dataview_left
= new wxDataViewCtrl( main
, ID_UNSORTED
, wxDefaultPosition
,
923 wxDefaultSize
, wxDV_MULTIPLE
);
925 m_unsorted_model
.reset(new MyUnsortedTextModel
);
926 dataview_left
->AssociateModel( m_unsorted_model
.get() );
928 wxDataViewTextRenderer
*text_renderer
=
929 new MyTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
930 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 0 );
931 dataview_left
->AppendColumn( column
);
932 dataview_left
->AppendTextColumn( wxT("second"), 1 );
933 dataview_left
->AppendColumn( new wxDataViewColumn( wxBitmap(null_xpm
),
934 new wxDataViewBitmapRenderer
, 2, 25 ) );
935 dataview_left
->AppendColumn( new wxDataViewColumn( wxT("icon"),
936 new wxDataViewBitmapRenderer
, 3, 25 ) );
938 // Right wxDataViewCtrl using the sorting model
939 dataview_right
= new wxDataViewCtrl( main
, ID_SORTED
);
941 m_sorted_model
.reset(new wxDataViewSortedListModel( m_unsorted_model
.get() ));
942 dataview_right
->AssociateModel( m_sorted_model
.get() );
944 text_renderer
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
945 column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 0, -1,
947 wxDATAVIEW_COL_SORTABLE
|wxDATAVIEW_COL_RESIZABLE
);
948 dataview_right
->AppendColumn( column
);
950 dataview_right
->AppendTextColumn( wxT("second"), 1 );
952 // layout dataview controls.
954 wxBoxSizer
*top_sizer
= new wxBoxSizer( wxHORIZONTAL
);
955 top_sizer
->Add( dataview_left
, 1, wxGROW
);
956 top_sizer
->Add(10,10);
957 top_sizer
->Add( dataview_right
, 1, wxGROW
);
959 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
960 button_sizer
->Add( 10, 10, 1 );
961 wxFlexGridSizer
*left_sizer
= new wxFlexGridSizer( 2 );
962 left_sizer
->Add( new wxButton( main
, ID_APPEND_ROW_LEFT
, wxT("Append") ), 0, wxALL
, 5 );
963 left_sizer
->Add( new wxButton( main
, ID_PREPEND_ROW_LEFT
, wxT("Prepend") ), 0, wxALL
, 5 );
964 left_sizer
->Add( new wxButton( main
, ID_INSERT_ROW_LEFT
, wxT("Insert") ), 0, wxALL
, 5 );
965 left_sizer
->Add( new wxButton( main
, ID_DELETE_ROW_LEFT
, wxT("Delete second") ), 0, wxALL
, 5 );
966 left_sizer
->Add( new wxButton( main
, ID_EDIT_ROW_LEFT
, wxT("Edit") ), 0, wxALL
, 5 );
967 left_sizer
->Add( 5,5 );
968 left_sizer
->Add( new wxButton( main
, ID_SELECT
, wxT("Select third") ), 0, wxALL
, 5 );
969 left_sizer
->Add( new wxButton( main
, ID_UNSELECT_ALL
, wxT("Unselect all") ), 0, wxALL
, 5 );
970 button_sizer
->Add( left_sizer
);
971 button_sizer
->Add( 10, 10, 2 );
972 wxFlexGridSizer
*right_sizer
= new wxFlexGridSizer( 2 );
973 right_sizer
->Add( new wxButton( main
, ID_APPEND_ROW_RIGHT
, wxT("Append") ), 0, wxALL
, 5 );
974 right_sizer
->Add( new wxButton( main
, ID_PREPEND_ROW_RIGHT
, wxT("Prepend") ), 0, wxALL
, 5 );
975 right_sizer
->Add( new wxButton( main
, ID_INSERT_ROW_RIGHT
, wxT("Insert") ), 0, wxALL
, 5 );
976 right_sizer
->Add( new wxButton( main
, ID_DELETE_ROW_RIGHT
, wxT("Delete second") ), 0, wxALL
, 5 );
977 right_sizer
->Add( new wxButton( main
, ID_EDIT_ROW_RIGHT
, wxT("Edit") ), 0, wxALL
, 5 );
978 button_sizer
->Add( right_sizer
);
979 button_sizer
->Add( 10, 10, 1 );
981 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
982 main_sizer
->Add( top_sizer
, 1, wxGROW
);
983 main_sizer
->Add( button_sizer
, 0, wxGROW
);
985 m_logWindow
= new wxTextCtrl(main
, wxID_ANY
, wxEmptyString
,
986 wxDefaultPosition
, wxDefaultSize
,
987 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
988 main_sizer
->Add( 20,20 );
989 main_sizer
->Add( m_logWindow
, 1, wxGROW
);
991 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
993 main
->SetSizer( main_sizer
);
996 MySortingFrame::~MySortingFrame()
998 delete wxLog::SetActiveTarget(m_logOld
);
1001 void MySortingFrame::OnSelectedUnsorted(wxDataViewEvent
&event
)
1003 int row
= event
.GetRow();
1004 wxLogMessage( wxT("OnSelected from unsorted list, selected %d"), row
);
1006 wxLogMessage( wxT("wxDataViewCtrl::IsSelected( %d ): %d (as int)"),
1007 row
, (int) dataview_right
->IsSelected( row
) );
1010 void MySortingFrame::OnSelectedSorted(wxDataViewEvent
&event
)
1012 wxLogMessage( wxT("OnSelected from sorted list, selected %d"), (int) event
.GetRow() );
1015 void MySortingFrame::OnActivatedUnsorted(wxDataViewEvent
&event
)
1017 wxLogMessage( wxT("OnActivated from unsorted list, activated %d"), (int) event
.GetRow() );
1020 void MySortingFrame::OnHeaderClickSorted(wxDataViewEvent
&event
)
1022 wxDataViewColumn
*col
= event
.GetDataViewColumn();
1023 wxLogMessage( wxT("OnHeaderClick from sorted list, column %s"), col
->GetTitle().c_str() );
1025 if (col
->GetTitle() == wxT("editable"))
1027 // this is the sorting column
1028 if (col
->IsSortOrderAscending())
1030 col
->SetSortOrder( false );
1031 m_sorted_model
->SetAscending( false );
1032 m_sorted_model
->Resort();
1036 col
->SetSortOrder( true );
1037 m_sorted_model
->SetAscending( true );
1038 m_sorted_model
->Resort();
1043 void MySortingFrame::OnHeaderClickUnsorted(wxDataViewEvent
&event
)
1045 wxLogMessage( wxT("OnHeaderClick from unsorted list, column %s"),
1046 event
.GetDataViewColumn()->GetTitle().c_str() );
1049 void MySortingFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
1054 void MySortingFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1056 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control sorting"),
1057 _T("About DataView"), wxOK
);
1062 void MySortingFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
1064 dataview_left
->SetSelection( 2 );
1067 void MySortingFrame::OnUnselectAll(wxCommandEvent
& WXUNUSED(event
))
1069 dataview_left
->ClearSelection();
1072 void MySortingFrame::OnAppendRowLeft(wxCommandEvent
& WXUNUSED(event
))
1074 wxTextEntryDialog
dialog( this, wxT("Enter text to append") );
1075 if (dialog
.ShowModal() == wxID_OK
)
1077 wxString value
= dialog
.GetValue();
1079 m_unsorted_model
->AppendRow( value
);
1083 void MySortingFrame::OnPrependRowLeft(wxCommandEvent
& WXUNUSED(event
))
1085 wxTextEntryDialog
dialog( this, wxT("Enter text to prepend") );
1086 if (dialog
.ShowModal() == wxID_OK
)
1088 wxString value
= dialog
.GetValue();
1090 m_unsorted_model
->PrependRow( value
);
1094 void MySortingFrame::OnInsertRowLeft(wxCommandEvent
& WXUNUSED(event
))
1096 wxTextEntryDialog
dialog( this, wxT("Enter text to insert before second") );
1097 if (dialog
.ShowModal() == wxID_OK
)
1099 wxString value
= dialog
.GetValue();
1101 m_unsorted_model
->InsertRowAt1( value
);
1105 void MySortingFrame::OnDeleteRowLeft(wxCommandEvent
& WXUNUSED(event
))
1107 m_unsorted_model
->DeleteRow( 1 );
1110 void MySortingFrame::OnEditRowLeft(wxCommandEvent
& WXUNUSED(event
))
1114 void MySortingFrame::OnAppendRowRight(wxCommandEvent
& WXUNUSED(event
))
1118 void MySortingFrame::OnPrependRowRight(wxCommandEvent
& WXUNUSED(event
))
1122 void MySortingFrame::OnInsertRowRight(wxCommandEvent
& WXUNUSED(event
))
1126 void MySortingFrame::OnDeleteRowRight(wxCommandEvent
& WXUNUSED(event
))
1130 void MySortingFrame::OnEditRowRight(wxCommandEvent
& WXUNUSED(event
))