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"
29 #include "wx/spinctrl.h"
32 #include "../sample.xpm"
38 #define DEFAULT_ALIGN wxALIGN_LEFT
39 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
43 // -------------------------------------
45 // -------------------------------------
47 WX_DECLARE_OBJARRAY(wxDateTime
,wxArrayDate
);
48 #include <wx/arrimpl.cpp>
49 WX_DEFINE_OBJARRAY(wxArrayDate
)
51 class MyTextModel
: public wxDataViewListModel
57 for (i
= 0; i
< 1000; i
++)
58 m_list
.Add( wxT("Test") );
59 for (i
= 0; i
< 500; i
++)
60 { m_bools
.Add( 0 ); m_bools
.Add( 1 ); }
61 for (i
= 0; i
< 500; i
++)
62 { m_colours
.Add( wxT("red") ); m_colours
.Add( wxT("green") ); }
63 for (i
= 0; i
< 1000; i
++)
64 { m_progress
.Add( i
/10 ); }
65 for (i
= 0; i
< 1000; i
++)
67 wxDateTime
date( wxDateTime::Now() );
72 virtual unsigned int GetRowCount() const
74 virtual unsigned int GetColumnCount() const
77 // as reported by wxVariant
78 virtual wxString
GetColumnType( unsigned int col
) const
81 return wxT("datetime");
92 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
) const
96 variant
= (wxDateTime
) m_dates
[row
];
100 variant
= (long) m_progress
[row
];
104 variant
= m_colours
[row
];
108 variant
= (bool) (m_bools
[row
] != 0);
112 variant
= m_list
[row
];
117 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
121 virtual bool SetValue( const wxVariant
&value
, unsigned int col
, unsigned int row
)
125 m_dates
[row
] = value
.GetDateTime();
129 m_bools
[row
] = (int) value
.GetBool();
133 m_list
[row
] = value
.GetString();
138 virtual void GetAttr( wxListItemAttr
&attr
, unsigned int col
, unsigned int row
)
141 attr
.SetBackgroundColour( *wxGREEN
);
144 wxArrayString m_list
;
146 wxArrayString m_colours
;
147 wxArrayInt m_progress
;
151 // -------------------------------------
153 // -------------------------------------
155 class MyTextRenderer
: public wxDataViewTextRenderer
158 MyTextRenderer( const wxString varianttype
= wxT("string"),
159 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
)
160 : wxDataViewTextRenderer( varianttype
, mode
) { }
162 virtual bool Validate( wxVariant
&value
)
164 wxString str
= value
;
165 if (str
.Length() > 10)
167 wxMessageDialog
dlg( NULL
, wxT("string too long") , wxT("Error") );
177 // -------------------------------------
179 // -------------------------------------
181 class MyCustomRenderer
: public wxDataViewCustomRenderer
185 wxDataViewCustomRenderer( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE
)
187 m_colour
= wxT("black");
189 bool SetValue( const wxVariant
&value
)
191 m_colour
= value
.GetString();
195 bool GetValue( wxVariant
&value
) const
201 bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
203 dc
->SetPen( *wxBLACK_PEN
);
204 if (m_colour
== wxT("red"))
205 dc
->SetBrush( *wxRED_BRUSH
);
206 else if (m_colour
== wxT("green"))
207 dc
->SetBrush( *wxGREEN_BRUSH
);
209 dc
->SetBrush( *wxBLACK_BRUSH
);
210 dc
->DrawRectangle( rect
);
213 wxSize
GetSize() const
217 bool Activate( wxRect
WXUNUSED(rect
),
218 wxDataViewListModel
*WXUNUSED(model
),
219 unsigned int WXUNUSED(col
),
220 unsigned int WXUNUSED(row
) )
229 // -------------------------------------
230 // MySpinCtrlInPlaceRenderer
231 // -------------------------------------
233 class MySpinCtrlInPlaceRenderer
: public wxDataViewCustomRenderer
236 MySpinCtrlInPlaceRenderer() :
237 wxDataViewCustomRenderer( wxT("long"), wxDATAVIEW_CELL_EDITABLE
) { }
240 virtual bool HasEditorCtrl()
244 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
247 return new wxSpinCtrl( parent
, wxID_ANY
, wxEmptyString
,
248 labelRect
.GetTopLeft(), labelRect
.GetSize(), 0, 0, 100, l
);
250 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
252 wxSpinCtrl
*sc
= (wxSpinCtrl
*) editor
;
253 long l
= sc
->GetValue();
258 bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
261 str
.Printf( wxT("%d"), (int) m_data
);
262 dc
->SetTextForeground( *wxBLACK
);
263 dc
->DrawText( str
, rect
.x
, rect
.y
);
266 wxSize
GetSize() const
268 return wxSize(80,16);
270 bool SetValue( const wxVariant
&value
)
272 m_data
= value
.GetLong();
275 bool GetValue( wxVariant
&value
) const
286 // -------------------------------------
287 // MyUnsortedTextModel
288 // -------------------------------------
290 class MyUnsortedTextModel
: public wxDataViewListModel
293 MyUnsortedTextModel()
295 m_list
.Add( wxT("This") );
296 m_list
.Add( wxT("is") );
297 m_list
.Add( wxT("an") );
298 m_list
.Add( wxT("unsorted") );
299 m_list
.Add( wxT("list") );
300 m_list
.Add( wxT("of") );
301 m_list
.Add( wxT("words.") );
311 m_bitmap
= wxBitmap( null_xpm
);
314 virtual unsigned int GetRowCount() const
316 return m_list
.GetCount();
319 virtual unsigned int GetColumnCount() const
324 virtual wxString
GetColumnType( unsigned int WXUNUSED(col
) ) const
326 return wxT("string");
329 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
) const
333 variant
= m_list
[row
];
336 if ((col
== 2) || (col
== 3))
343 variant
= (long) m_ilist
[row
];
348 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
352 virtual bool SetValue( const wxVariant
&variant
, unsigned int col
, unsigned int row
)
356 m_list
[row
] = variant
.GetString();
361 m_ilist
[row
] = variant
.GetLong();
368 void AppendRow( const wxString
&text
)
375 void PrependRow( const wxString
&text
)
377 m_list
.Insert( text
, 0 );
378 m_ilist
.Insert( 0, 0 );
382 void InsertRowAt1( const wxString
&text
)
384 m_list
.Insert( text
, 1 );
385 m_ilist
.Insert( 0, 1 );
389 void DeleteRow( unsigned int index
)
391 m_list
.RemoveAt( index
);
392 m_ilist
.RemoveAt( index
);
396 wxArrayString m_list
;
401 // -------------------------------------
403 // -------------------------------------
405 class MyApp
: public wxApp
412 // -------------------------------------
414 // -------------------------------------
416 class MyFrame
: public wxFrame
419 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
422 void OnQuit(wxCommandEvent
& event
);
423 void OnAbout(wxCommandEvent
& event
);
424 void OnNewSortingFrame(wxCommandEvent
& event
);
426 void OnStyleChange(wxCommandEvent
& event
);
427 void OnColumnSetting(wxCommandEvent
& event
);
430 wxDataViewCtrl
* dataview_left
;
431 wxDataViewCtrl
* dataview_right
;
432 wxSplitterWindow
*m_splitter
;
433 wxPanel
*m_panelLeft
, *m_panelRight
;
437 void CreateDataViewControls();
439 wxArrayInt
GetFlaggedColumns(int flag
);
440 wxAlignment
ChooseAlign(const wxString
&msg
, bool onlyHorizontal
);
441 void SetFlag(const wxArrayInt
&idx
, int flag
);
442 void SetAlignment(const wxArrayInt
&idx
, bool header
, wxAlignment align
);
443 void SetWidth(const wxArrayInt
&idx
, bool minwidth
, int width
);
446 DECLARE_EVENT_TABLE()
449 // -------------------------------------
451 // -------------------------------------
455 ID_APPEND_ROW_LEFT
= 1000,
465 ID_PREPEND_ROW_RIGHT
,
475 class MySortingFrame
: public wxFrame
478 MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
482 void OnQuit(wxCommandEvent
& event
);
483 void OnAbout(wxCommandEvent
& event
);
485 void OnAppendRowLeft(wxCommandEvent
& event
);
486 void OnPrependRowLeft(wxCommandEvent
& event
);
487 void OnInsertRowLeft(wxCommandEvent
& event
);
488 void OnDeleteRowLeft(wxCommandEvent
& event
);
489 void OnEditRowLeft(wxCommandEvent
& event
);
491 void OnAppendRowRight(wxCommandEvent
& event
);
492 void OnPrependRowRight(wxCommandEvent
& event
);
493 void OnInsertRowRight(wxCommandEvent
& event
);
494 void OnDeleteRowRight(wxCommandEvent
& event
);
495 void OnEditRowRight(wxCommandEvent
& event
);
497 void OnSelect(wxCommandEvent
& event
);
498 void OnUnselectAll(wxCommandEvent
& event
);
500 void OnSelectedUnsorted(wxDataViewEvent
&event
);
501 void OnSelectedSorted(wxDataViewEvent
&event
);
502 void OnActivatedUnsorted(wxDataViewEvent
&event
);
504 void OnHeaderClickSorted(wxDataViewEvent
&event
);
505 void OnHeaderClickUnsorted(wxDataViewEvent
&event
);
508 wxDataViewCtrl
* dataview_left
;
509 wxDataViewCtrl
* dataview_right
;
512 wxTextCtrl
*m_logWindow
;
514 wxObjectDataPtr
<MyUnsortedTextModel
> m_unsorted_model
;
515 wxObjectDataPtr
<wxDataViewSortedListModel
> m_sorted_model
;
517 DECLARE_EVENT_TABLE()
520 // -------------------------------------
522 // -------------------------------------
526 bool MyApp::OnInit(void)
528 if ( !wxApp::OnInit() )
531 // build the first frame
533 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
546 // -------------------------------------
548 // -------------------------------------
553 ID_ABOUT
= wxID_ABOUT
,
554 ID_NEW_SORT_FRAME
= wxID_HIGHEST
+1,
558 ID_SINGLE_SEL_MODE
= wxID_HIGHEST
+2,
559 ID_MULTIPLE_SEL_MODE
,
564 ID_RESIZEABLE_COLUMNS
,
568 ID_CHOOSE_COLUMN_ALIGNMENT
,
569 ID_CHOOSE_CONTENTS_ALIGNMENT
,
575 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
578 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
579 EVT_MENU( ID_NEW_SORT_FRAME
, MyFrame::OnNewSortingFrame
)
580 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
583 EVT_COMMAND_RANGE( ID_SINGLE_SEL_MODE
, ID_VERT_RULES_MODE
,
584 wxEVT_COMMAND_MENU_SELECTED
, MyFrame::OnStyleChange
)
586 EVT_COMMAND_RANGE( ID_RESIZEABLE_COLUMNS
, ID_SET_WIDTH
,
587 wxEVT_COMMAND_MENU_SELECTED
, MyFrame::OnColumnSetting
)
591 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
592 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
594 SetIcon(wxICON(sample
));
598 wxMenu
*file_menu
= new wxMenu
;
599 file_menu
->Append(ID_NEW_SORT_FRAME
, _T("&New sorting frame"));
600 file_menu
->AppendSeparator();
601 file_menu
->Append(ID_ABOUT
, _T("&About"));
602 file_menu
->AppendSeparator();
603 file_menu
->Append(ID_EXIT
, _T("E&xit"));
605 wxMenu
*data_menu
= new wxMenu
;
606 data_menu
->AppendRadioItem(ID_SINGLE_SEL_MODE
, _T("&Single selection mode"));
607 data_menu
->AppendRadioItem(ID_MULTIPLE_SEL_MODE
, _T("&Multiple selection mode"));
608 data_menu
->AppendSeparator();
609 data_menu
->AppendCheckItem(ID_NO_HEADER_MODE
, _T("No header mode"));
610 data_menu
->AppendCheckItem(ID_HORIZ_RULES_MODE
, _T("Horizontal rules"));
611 data_menu
->AppendCheckItem(ID_VERT_RULES_MODE
, _T("Vertical rules"));
612 data_menu
->AppendSeparator();
613 data_menu
->Append(ID_RESIZEABLE_COLUMNS
, _T("Set column(s) as resizeable..."));
614 data_menu
->Append(ID_SORTABLE_COLUMNS
, _T("Set column(s) as sortable..."));
615 data_menu
->Append(ID_HIDDEN_COLUMNS
, _T("Set column(s) as hidden..."));
616 data_menu
->AppendSeparator();
617 data_menu
->Append(ID_CHOOSE_COLUMN_ALIGNMENT
, _T("Set column(s) title alignment..."));
618 data_menu
->Append(ID_CHOOSE_CONTENTS_ALIGNMENT
, _T("Set column(s) contents alignment..."));
619 data_menu
->AppendSeparator();
620 data_menu
->Append(ID_SET_MINWIDTH
, _T("Set column(s) minimal width..."));
621 data_menu
->Append(ID_SET_WIDTH
, _T("Set column(s) width..."));
623 wxMenuBar
*menu_bar
= new wxMenuBar
;
624 menu_bar
->Append(file_menu
, _T("&File"));
625 menu_bar
->Append(data_menu
, _T("&DataViewCtrl"));
627 SetMenuBar(menu_bar
);
631 // check the menus for the default wxDataViewCtrl style
632 data_menu
->Check(ID_MULTIPLE_SEL_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_MULTIPLE
) != 0);
633 data_menu
->Check(ID_NO_HEADER_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_NO_HEADER
) != 0);
634 data_menu
->Check(ID_HORIZ_RULES_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_HORIZ_RULES
) != 0);
635 data_menu
->Check(ID_VERT_RULES_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_VERT_RULES
) != 0);
638 // build the other controls:
640 m_splitter
= new wxSplitterWindow( this, wxID_ANY
);
641 m_splitter
->SetSashGravity(0.5);
643 m_panelLeft
= new wxPanel( m_splitter
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
645 m_panelRight
= new wxPanel( m_splitter
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
647 wxSizer
*szLeft
= new wxBoxSizer(wxVERTICAL
);
648 wxSizer
*szRight
= new wxBoxSizer(wxVERTICAL
);
650 dataview_left
= NULL
;
651 dataview_right
= NULL
;
652 CreateDataViewControls();
655 szLeft
->Add( dataview_left
, 1, wxGROW
|wxALL
, 5 );
656 m_panelLeft
->SetSizerAndFit(szLeft
);
659 wxStaticText
*stattext
=
660 new wxStaticText(m_panelRight
, wxID_ANY
,
661 wxT("This is another wxDataViewCtrl using the same wxDataViewModel ")
662 wxT("of the wxDataViewCtrl on the left but, unlike it, this window ")
663 wxT("won't react to the style/column changes done through the ")
664 wxT("'DataViewCtrl' menu"));
665 stattext
->Wrap(GetClientSize().GetWidth() / 2);
667 szRight
->Add( stattext
, 0, wxALL
, 5 );
668 szRight
->Add( dataview_right
, 1, wxGROW
|wxALL
, 5 );
669 m_panelRight
->SetSizerAndFit(szRight
);
671 // split the two panels
672 m_splitter
->SplitVertically(m_panelLeft
, m_panelRight
);
673 this->SetMinSize(m_splitter
->GetBestSize());
676 void MyFrame::CreateDataViewControls()
678 wxDataViewCtrl
*old1
= NULL
, *old2
= NULL
;
681 old1
= dataview_left
;
683 old2
= dataview_right
;
687 if (GetMenuBar()->FindItem(ID_MULTIPLE_SEL_MODE
)->IsChecked())
688 style
|= wxDV_MULTIPLE
;
689 if (GetMenuBar()->FindItem(ID_NO_HEADER_MODE
)->IsChecked())
690 style
|= wxDV_NO_HEADER
;
692 if (GetMenuBar()->FindItem(ID_HORIZ_RULES_MODE
)->IsChecked())
693 style
|= wxDV_HORIZ_RULES
;
694 if (GetMenuBar()->FindItem(ID_VERT_RULES_MODE
)->IsChecked())
695 style
|= wxDV_VERT_RULES
;
699 // Left wxDataViewCtrl
700 dataview_left
= new wxDataViewCtrl( m_panelLeft
, wxID_ANY
, wxDefaultPosition
,
701 wxDefaultSize
, style
);
704 wxDataViewTextRenderer
*text_renderer
;
705 wxDataViewColumn
*column
;
707 wxObjectDataPtr
<MyTextModel
> model(new MyTextModel
);
708 dataview_left
->AssociateModel( model
.get() );
710 dataview_left
->AppendTextColumn( wxT("First"), 0, wxDATAVIEW_CELL_INERT
, -1,
712 dataview_left
->AppendTextColumn( wxT("Second"), 1, wxDATAVIEW_CELL_INERT
, -1,
715 text_renderer
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
716 column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 2, -1, DEFAULT_ALIGN
);
717 dataview_left
->AppendColumn( column
);
719 dataview_left
->AppendToggleColumn( wxT("fourth"), 3, wxDATAVIEW_CELL_INERT
, -1,
722 MyCustomRenderer
*custom_renderer
= new MyCustomRenderer
;
723 column
= new wxDataViewColumn( wxT("custom"), custom_renderer
, 4, -1, DEFAULT_ALIGN
);
724 dataview_left
->AppendColumn( column
);
726 dataview_left
->AppendProgressColumn( wxT("progress"), 5, wxDATAVIEW_CELL_INERT
, -1,
729 dataview_left
->AppendDateColumn( wxT("date"), 6, wxDATAVIEW_CELL_INERT
, -1, DEFAULT_ALIGN
);
731 // Right wxDataViewCtrl using the same model
732 dataview_right
= new wxDataViewCtrl( m_panelRight
, wxID_ANY
);
733 dataview_right
->AssociateModel( model
.get() );
735 text_renderer
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
736 column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 2 );
737 dataview_right
->AppendColumn( column
);
738 dataview_right
->AppendTextColumn( wxT("first"), 0 );
739 dataview_right
->AppendTextColumn( wxT("second"), 1 );
740 wxDataViewToggleRenderer
*toggle_renderer
=
741 new wxDataViewToggleRenderer( wxT("bool"), wxDATAVIEW_CELL_ACTIVATABLE
);
742 column
= new wxDataViewColumn( wxT("bool"), toggle_renderer
, 3, 30 );
743 dataview_right
->AppendColumn( column
);
745 dataview_right
->AppendDateColumn( wxT("date"), 6 );
748 // layout the new dataview controls
751 m_panelLeft
->GetSizer()->Replace(old1
, dataview_left
);
754 m_panelLeft
->Layout();
759 m_panelRight
->GetSizer()->Replace(old2
, dataview_right
);
762 m_panelRight
->Layout();
766 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
771 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
773 wxAboutDialogInfo info
;
774 info
.SetName(_("DataView sample"));
775 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
776 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));
781 void MyFrame::OnNewSortingFrame(wxCommandEvent
& WXUNUSED(event
) )
783 MySortingFrame
*frame2
=
784 new MySortingFrame(NULL
, wxT("wxDataViewCtrl sorting test"), 10, 150, 600, 500);
788 void MyFrame::OnStyleChange(wxCommandEvent
& WXUNUSED(event
) )
790 // recreate the wxDataViewCtrl:
791 CreateDataViewControls();
794 void MyFrame::OnColumnSetting(wxCommandEvent
& event
)
798 bool header
= false, minwidth
= false;
801 switch (event
.GetId())
803 case ID_RESIZEABLE_COLUMNS
:
804 flag
= wxDATAVIEW_COL_RESIZABLE
;
805 columns
= GetFlaggedColumns(flag
);
807 case ID_SORTABLE_COLUMNS
:
808 flag
= wxDATAVIEW_COL_SORTABLE
;
809 columns
= GetFlaggedColumns(flag
);
811 case ID_HIDDEN_COLUMNS
:
812 flag
= wxDATAVIEW_COL_HIDDEN
;
813 columns
= GetFlaggedColumns(flag
);
816 case ID_CHOOSE_COLUMN_ALIGNMENT
:
817 msg
= wxT("Select the columns whose headers' alignment will be modified.");
820 case ID_CHOOSE_CONTENTS_ALIGNMENT
:
821 msg
= wxT("Select the columns whose contents' alignment will be modified.");
825 case ID_SET_MINWIDTH
:
826 msg
= wxT("Please provide the new minimal width:");
830 msg
= wxT("Please provide the new width:");
835 // get column titles:
837 wxArrayString choices
;
838 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
839 choices
.Add(dataview_left
->GetColumn(i
)->GetTitle());
842 wxGetMultipleChoices(
844 wxT("Choose the columns to which apply the change."),
845 wxT("Choose the column"),
849 switch (event
.GetId())
851 case ID_RESIZEABLE_COLUMNS
:
852 case ID_SORTABLE_COLUMNS
:
853 case ID_HIDDEN_COLUMNS
:
854 SetFlag(columns
, flag
);
857 case ID_CHOOSE_COLUMN_ALIGNMENT
:
858 case ID_CHOOSE_CONTENTS_ALIGNMENT
:
859 SetAlignment(columns
, header
, ChooseAlign(msg
, header
));
862 case ID_SET_MINWIDTH
:
865 int def
= minwidth
? wxDVC_DEFAULT_MINWIDTH
: wxDVC_DEFAULT_WIDTH
;
867 msg
<< wxT("\nNOTE: all non-selected columns will be reset to a width of ")
868 << def
<< wxT(" pixels.");
871 wxGetNumberFromUser(msg
, wxT("New value:"), wxT("Modify width"),
875 SetWidth(columns
, minwidth
, ret
);
880 dataview_left
->Refresh();
883 wxAlignment
MyFrame::ChooseAlign(const wxString
&msg
, bool onlyHorizontal
)
885 const wxString choices
[] =
888 wxT("Center horizontally"),
891 wxT("Center vertically"),
896 wxAlignment flags
[] =
899 wxALIGN_CENTER_HORIZONTAL
,
902 wxALIGN_CENTER_VERTICAL
,
907 int n
= WXSIZEOF(choices
);
909 n
= 3; // show only the first three choices
911 int choice
= wxGetSingleChoiceIndex(
912 msg
+ wxT("\nNOTE: _all_ non-selected column's alignment will be reset to wxALIGN_LEFT!"),
917 if (choice
== wxNOT_FOUND
)
920 return flags
[choice
];
923 void MyFrame::SetFlag(const wxArrayInt
&idx
, int toadd
)
925 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
927 int current
= dataview_left
->GetColumn(i
)->GetFlags();
929 if (idx
.Index(i
) != wxNOT_FOUND
)
930 dataview_left
->GetColumn(i
)->SetFlags(current
| toadd
);
932 dataview_left
->GetColumn(i
)->SetFlags(current
& ~toadd
);
936 wxArrayInt
MyFrame::GetFlaggedColumns(int flag
)
939 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
940 if (dataview_left
->GetColumn(i
)->GetFlags() & flag
)
945 void MyFrame::SetAlignment(const wxArrayInt
&idx
, bool header
, wxAlignment align
)
947 // set to DEFAULT_ALIGN all columns except those
948 // contained in 'idx' which are set to 'align'
950 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
952 wxAlignment toset
= DEFAULT_ALIGN
;
953 if (idx
.Index(i
) != wxNOT_FOUND
)
957 dataview_left
->GetColumn(i
)->SetAlignment(toset
);
959 dataview_left
->GetColumn(i
)->GetRenderer()->SetAlignment(toset
);
963 void MyFrame::SetWidth(const wxArrayInt
&idx
, bool minwidth
, int width
)
965 // set to wxDVC_DEFAULT_WIDTH wide all columns except those
966 // contained in 'idx' which are set to 'width'
968 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
970 int toset
= minwidth
? wxDVC_DEFAULT_MINWIDTH
: wxDVC_DEFAULT_WIDTH
;
971 if (idx
.Index(i
) != wxNOT_FOUND
)
975 dataview_left
->GetColumn(i
)->SetMinWidth(toset
);
977 dataview_left
->GetColumn(i
)->SetWidth(toset
);
982 // -------------------------------------
984 // -------------------------------------
986 BEGIN_EVENT_TABLE(MySortingFrame
,wxFrame
)
987 EVT_BUTTON( ID_APPEND_ROW_LEFT
, MySortingFrame::OnAppendRowLeft
)
988 EVT_BUTTON( ID_PREPEND_ROW_LEFT
, MySortingFrame::OnPrependRowLeft
)
989 EVT_BUTTON( ID_INSERT_ROW_LEFT
, MySortingFrame::OnInsertRowLeft
)
990 EVT_BUTTON( ID_DELETE_ROW_LEFT
, MySortingFrame::OnDeleteRowLeft
)
991 EVT_BUTTON( ID_SELECT
, MySortingFrame::OnSelect
)
992 EVT_BUTTON( ID_UNSELECT_ALL
, MySortingFrame::OnUnselectAll
)
993 EVT_DATAVIEW_ROW_SELECTED( ID_SORTED
, MySortingFrame::OnSelectedSorted
)
994 EVT_DATAVIEW_ROW_SELECTED( ID_UNSORTED
, MySortingFrame::OnSelectedUnsorted
)
995 EVT_DATAVIEW_ROW_ACTIVATED( ID_UNSORTED
, MySortingFrame::OnActivatedUnsorted
)
996 EVT_DATAVIEW_COLUMN_HEADER_CLICK( ID_SORTED
, MySortingFrame::OnHeaderClickSorted
)
997 EVT_DATAVIEW_COLUMN_HEADER_CLICK( ID_UNSORTED
, MySortingFrame::OnHeaderClickUnsorted
)
1000 MySortingFrame::MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
1001 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
1005 SetIcon(wxICON(sample
));
1008 wxPanel
*main
= new wxPanel(this);
1010 // Left wxDataViewCtrl
1011 dataview_left
= new wxDataViewCtrl( main
, ID_UNSORTED
, wxDefaultPosition
,
1012 wxDefaultSize
, wxDV_MULTIPLE
);
1014 m_unsorted_model
= new MyUnsortedTextModel
;
1015 dataview_left
->AssociateModel( m_unsorted_model
.get() );
1017 wxDataViewTextRenderer
*text_renderer
=
1018 new MyTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
1019 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 0 );
1020 dataview_left
->AppendColumn( column
);
1021 dataview_left
->AppendTextColumn( wxT("second"), 1 );
1022 dataview_left
->AppendColumn( new wxDataViewColumn( wxBitmap(null_xpm
),
1023 new wxDataViewBitmapRenderer
, 2, 25 ) );
1024 dataview_left
->AppendColumn( new wxDataViewColumn( wxT("icon"),
1025 new wxDataViewBitmapRenderer
, 3, 25 ) );
1027 // Right wxDataViewCtrl using the sorting model
1028 dataview_right
= new wxDataViewCtrl( main
, ID_SORTED
);
1030 m_sorted_model
= new wxDataViewSortedListModel( m_unsorted_model
.get() );
1031 dataview_right
->AssociateModel( m_sorted_model
.get() );
1033 text_renderer
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
1034 column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 0, -1,
1036 wxDATAVIEW_COL_SORTABLE
|wxDATAVIEW_COL_RESIZABLE
);
1037 dataview_right
->AppendColumn( column
);
1039 dataview_right
->AppendTextColumn( wxT("second"), 1 );
1041 MySpinCtrlInPlaceRenderer
*sr
= new MySpinCtrlInPlaceRenderer
;
1042 column
= new wxDataViewColumn( wxT("spin"), sr
, 4, -1, wxALIGN_CENTER
);
1043 dataview_right
->AppendColumn( column
);
1045 // layout dataview controls.
1047 wxBoxSizer
*top_sizer
= new wxBoxSizer( wxHORIZONTAL
);
1048 top_sizer
->Add( dataview_left
, 1, wxGROW
);
1049 top_sizer
->Add(10,10);
1050 top_sizer
->Add( dataview_right
, 1, wxGROW
);
1052 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
1053 button_sizer
->Add( 10, 10, 1 );
1054 wxFlexGridSizer
*left_sizer
= new wxFlexGridSizer( 2 );
1055 left_sizer
->Add( new wxButton( main
, ID_APPEND_ROW_LEFT
, wxT("Append") ), 0, wxALL
, 5 );
1056 left_sizer
->Add( new wxButton( main
, ID_PREPEND_ROW_LEFT
, wxT("Prepend") ), 0, wxALL
, 5 );
1057 left_sizer
->Add( new wxButton( main
, ID_INSERT_ROW_LEFT
, wxT("Insert") ), 0, wxALL
, 5 );
1058 left_sizer
->Add( new wxButton( main
, ID_DELETE_ROW_LEFT
, wxT("Delete second") ), 0, wxALL
, 5 );
1059 left_sizer
->Add( new wxButton( main
, ID_EDIT_ROW_LEFT
, wxT("Edit") ), 0, wxALL
, 5 );
1060 left_sizer
->Add( 5,5 );
1061 left_sizer
->Add( new wxButton( main
, ID_SELECT
, wxT("Select third") ), 0, wxALL
, 5 );
1062 left_sizer
->Add( new wxButton( main
, ID_UNSELECT_ALL
, wxT("Unselect all") ), 0, wxALL
, 5 );
1063 button_sizer
->Add( left_sizer
);
1064 button_sizer
->Add( 10, 10, 2 );
1065 wxFlexGridSizer
*right_sizer
= new wxFlexGridSizer( 2 );
1066 right_sizer
->Add( new wxButton( main
, ID_APPEND_ROW_RIGHT
, wxT("Append") ), 0, wxALL
, 5 );
1067 right_sizer
->Add( new wxButton( main
, ID_PREPEND_ROW_RIGHT
, wxT("Prepend") ), 0, wxALL
, 5 );
1068 right_sizer
->Add( new wxButton( main
, ID_INSERT_ROW_RIGHT
, wxT("Insert") ), 0, wxALL
, 5 );
1069 right_sizer
->Add( new wxButton( main
, ID_DELETE_ROW_RIGHT
, wxT("Delete second") ), 0, wxALL
, 5 );
1070 right_sizer
->Add( new wxButton( main
, ID_EDIT_ROW_RIGHT
, wxT("Edit") ), 0, wxALL
, 5 );
1071 button_sizer
->Add( right_sizer
);
1072 button_sizer
->Add( 10, 10, 1 );
1074 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
1075 main_sizer
->Add( top_sizer
, 1, wxGROW
);
1076 main_sizer
->Add( button_sizer
, 0, wxGROW
);
1078 m_logWindow
= new wxTextCtrl(main
, wxID_ANY
, wxEmptyString
,
1079 wxDefaultPosition
, wxDefaultSize
,
1080 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
1081 main_sizer
->Add( 20,20 );
1082 main_sizer
->Add( m_logWindow
, 1, wxGROW
);
1084 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
1086 main
->SetSizer( main_sizer
);
1089 MySortingFrame::~MySortingFrame()
1091 delete wxLog::SetActiveTarget(m_logOld
);
1094 void MySortingFrame::OnSelectedUnsorted(wxDataViewEvent
&event
)
1096 int row
= event
.GetRow();
1097 wxLogMessage( wxT("OnSelected from unsorted list, selected %d"), row
);
1099 wxLogMessage( wxT("wxDataViewCtrl::IsSelected( %d ): %d (as int)"),
1100 row
, (int) dataview_right
->IsSelected( row
) );
1103 void MySortingFrame::OnSelectedSorted(wxDataViewEvent
&event
)
1105 wxLogMessage( wxT("OnSelected from sorted list, selected %d"), (int) event
.GetRow() );
1108 void MySortingFrame::OnActivatedUnsorted(wxDataViewEvent
&event
)
1110 wxLogMessage( wxT("OnActivated from unsorted list, activated %d"), (int) event
.GetRow() );
1113 void MySortingFrame::OnHeaderClickSorted(wxDataViewEvent
&event
)
1115 wxDataViewColumn
*col
= event
.GetDataViewColumn();
1116 wxLogMessage( wxT("OnHeaderClick from sorted list, column %s"), col
->GetTitle().c_str() );
1118 if (col
->GetTitle() == wxT("editable"))
1120 // this is the sorting column
1121 if (col
->IsSortOrderAscending())
1123 col
->SetSortOrder( false );
1124 m_sorted_model
->SetAscending( false );
1125 m_sorted_model
->Resort();
1129 col
->SetSortOrder( true );
1130 m_sorted_model
->SetAscending( true );
1131 m_sorted_model
->Resort();
1136 void MySortingFrame::OnHeaderClickUnsorted(wxDataViewEvent
&event
)
1138 wxLogMessage( wxT("OnHeaderClick from unsorted list, column %s"),
1139 event
.GetDataViewColumn()->GetTitle().c_str() );
1142 void MySortingFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
1147 void MySortingFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1149 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control sorting"),
1150 _T("About DataView"), wxOK
);
1155 void MySortingFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
1157 dataview_left
->SetSelection( 2 );
1160 void MySortingFrame::OnUnselectAll(wxCommandEvent
& WXUNUSED(event
))
1162 dataview_left
->ClearSelection();
1165 void MySortingFrame::OnAppendRowLeft(wxCommandEvent
& WXUNUSED(event
))
1167 wxTextEntryDialog
dialog( this, wxT("Enter text to append") );
1168 if (dialog
.ShowModal() == wxID_OK
)
1170 wxString value
= dialog
.GetValue();
1172 m_unsorted_model
->AppendRow( value
);
1176 void MySortingFrame::OnPrependRowLeft(wxCommandEvent
& WXUNUSED(event
))
1178 wxTextEntryDialog
dialog( this, wxT("Enter text to prepend") );
1179 if (dialog
.ShowModal() == wxID_OK
)
1181 wxString value
= dialog
.GetValue();
1183 m_unsorted_model
->PrependRow( value
);
1187 void MySortingFrame::OnInsertRowLeft(wxCommandEvent
& WXUNUSED(event
))
1189 wxTextEntryDialog
dialog( this, wxT("Enter text to insert before second") );
1190 if (dialog
.ShowModal() == wxID_OK
)
1192 wxString value
= dialog
.GetValue();
1194 m_unsorted_model
->InsertRowAt1( value
);
1198 void MySortingFrame::OnDeleteRowLeft(wxCommandEvent
& WXUNUSED(event
))
1200 m_unsorted_model
->DeleteRow( 1 );
1203 void MySortingFrame::OnEditRowLeft(wxCommandEvent
& WXUNUSED(event
))
1207 void MySortingFrame::OnAppendRowRight(wxCommandEvent
& WXUNUSED(event
))
1211 void MySortingFrame::OnPrependRowRight(wxCommandEvent
& WXUNUSED(event
))
1215 void MySortingFrame::OnInsertRowRight(wxCommandEvent
& WXUNUSED(event
))
1219 void MySortingFrame::OnDeleteRowRight(wxCommandEvent
& WXUNUSED(event
))
1223 void MySortingFrame::OnEditRowRight(wxCommandEvent
& WXUNUSED(event
))