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 MyCustomRenderer
: public wxDataViewCustomRenderer
152 wxDataViewCustomRenderer( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE
)
154 m_colour
= wxT("black");
156 bool SetValue( const wxVariant
&value
)
158 m_colour
= value
.GetString();
162 bool GetValue( wxVariant
&value
) const
168 bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
170 dc
->SetPen( *wxBLACK_PEN
);
171 if (m_colour
== wxT("red"))
172 dc
->SetBrush( *wxRED_BRUSH
);
173 else if (m_colour
== wxT("green"))
174 dc
->SetBrush( *wxGREEN_BRUSH
);
176 dc
->SetBrush( *wxBLACK_BRUSH
);
177 dc
->DrawRectangle( rect
);
180 wxSize
GetSize() const
184 bool Activate( wxRect
WXUNUSED(rect
),
185 wxDataViewListModel
*WXUNUSED(model
),
186 unsigned int WXUNUSED(col
),
187 unsigned int WXUNUSED(row
) )
196 // -------------------------------------
197 // MyUnsortedTextModel
198 // -------------------------------------
200 class MyUnsortedTextModel
: public wxDataViewListModel
203 MyUnsortedTextModel()
205 m_list
.Add( wxT("This") );
206 m_list
.Add( wxT("is") );
207 m_list
.Add( wxT("an") );
208 m_list
.Add( wxT("unsorted") );
209 m_list
.Add( wxT("list") );
210 m_list
.Add( wxT("of") );
211 m_list
.Add( wxT("words.") );
213 m_bitmap
= wxBitmap( null_xpm
);
216 virtual unsigned int GetRowCount() const
218 return m_list
.GetCount();
221 virtual unsigned int GetColumnCount() const
226 virtual wxString
GetColumnType( unsigned int WXUNUSED(col
) ) const
228 return wxT("string");
231 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
) const
235 variant
= m_list
[row
];
238 if ((col
== 2) || (col
== 3))
244 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
248 virtual bool SetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
252 m_list
[row
] = variant
.GetString();
259 void AppendRow( const wxString
&text
)
265 void PrependRow( const wxString
&text
)
267 m_list
.Insert( text
, 0 );
271 void InsertRowAt1( const wxString
&text
)
273 m_list
.Insert( text
, 1 );
277 void DeleteRow( unsigned int index
)
279 m_list
.RemoveAt( index
);
283 wxArrayString m_list
;
287 // -------------------------------------
289 // -------------------------------------
291 class MyApp
: public wxApp
298 // -------------------------------------
300 // -------------------------------------
302 class MyFrame
: public wxFrame
305 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
308 void OnQuit(wxCommandEvent
& event
);
309 void OnAbout(wxCommandEvent
& event
);
310 void OnNewSortingFrame(wxCommandEvent
& event
);
312 void OnStyleChange(wxCommandEvent
& event
);
313 void OnColumnSetting(wxCommandEvent
& event
);
316 wxDataViewCtrl
* dataview_left
;
317 wxDataViewCtrl
* dataview_right
;
318 wxSplitterWindow
*m_splitter
;
319 wxPanel
*m_panelLeft
, *m_panelRight
;
323 void CreateDataViewControls();
325 wxArrayInt
GetFlaggedColumns(int flag
);
326 wxAlignment
ChooseAlign(const wxString
&msg
, bool onlyHorizontal
);
327 void SetFlag(const wxArrayInt
&idx
, int flag
);
328 void SetAlignment(const wxArrayInt
&idx
, bool header
, wxAlignment align
);
329 void SetWidth(const wxArrayInt
&idx
, bool minwidth
, int width
);
332 DECLARE_EVENT_TABLE()
335 // -------------------------------------
337 // -------------------------------------
341 ID_APPEND_ROW_LEFT
= 1000,
351 ID_PREPEND_ROW_RIGHT
,
361 class MySortingFrame
: public wxFrame
364 MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
368 void OnQuit(wxCommandEvent
& event
);
369 void OnAbout(wxCommandEvent
& event
);
371 void OnAppendRowLeft(wxCommandEvent
& event
);
372 void OnPrependRowLeft(wxCommandEvent
& event
);
373 void OnInsertRowLeft(wxCommandEvent
& event
);
374 void OnDeleteRowLeft(wxCommandEvent
& event
);
375 void OnEditRowLeft(wxCommandEvent
& event
);
377 void OnAppendRowRight(wxCommandEvent
& event
);
378 void OnPrependRowRight(wxCommandEvent
& event
);
379 void OnInsertRowRight(wxCommandEvent
& event
);
380 void OnDeleteRowRight(wxCommandEvent
& event
);
381 void OnEditRowRight(wxCommandEvent
& event
);
383 void OnSelect(wxCommandEvent
& event
);
384 void OnUnselectAll(wxCommandEvent
& event
);
386 void OnSelectedUnsorted(wxDataViewEvent
&event
);
387 void OnSelectedSorted(wxDataViewEvent
&event
);
388 void OnActivatedUnsorted(wxDataViewEvent
&event
);
390 void OnHeaderClickSorted(wxDataViewEvent
&event
);
391 void OnHeaderClickUnsorted(wxDataViewEvent
&event
);
394 wxDataViewCtrl
* dataview_left
;
395 wxDataViewCtrl
* dataview_right
;
398 wxTextCtrl
*m_logWindow
;
400 wxObjectDataPtr
<MyUnsortedTextModel
> m_unsorted_model
;
401 wxObjectDataPtr
<wxDataViewSortedListModel
> m_sorted_model
;
403 DECLARE_EVENT_TABLE()
406 // -------------------------------------
408 // -------------------------------------
412 bool MyApp::OnInit(void)
414 if ( !wxApp::OnInit() )
417 // build the first frame
419 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
432 // -------------------------------------
434 // -------------------------------------
439 ID_ABOUT
= wxID_ABOUT
,
440 ID_NEW_SORT_FRAME
= wxID_HIGHEST
+1,
444 ID_SINGLE_SEL_MODE
= wxID_HIGHEST
+2,
445 ID_MULTIPLE_SEL_MODE
,
450 ID_RESIZEABLE_COLUMNS
,
454 ID_CHOOSE_COLUMN_ALIGNMENT
,
455 ID_CHOOSE_CONTENTS_ALIGNMENT
,
461 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
464 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
465 EVT_MENU( ID_NEW_SORT_FRAME
, MyFrame::OnNewSortingFrame
)
466 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
469 EVT_COMMAND_RANGE( ID_SINGLE_SEL_MODE
, ID_VERT_RULES_MODE
,
470 wxEVT_COMMAND_MENU_SELECTED
, MyFrame::OnStyleChange
)
472 EVT_COMMAND_RANGE( ID_RESIZEABLE_COLUMNS
, ID_SET_WIDTH
,
473 wxEVT_COMMAND_MENU_SELECTED
, MyFrame::OnColumnSetting
)
477 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
478 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
480 SetIcon(wxICON(sample
));
484 wxMenu
*file_menu
= new wxMenu
;
485 file_menu
->Append(ID_NEW_SORT_FRAME
, _T("&New sorting frame"));
486 file_menu
->AppendSeparator();
487 file_menu
->Append(ID_ABOUT
, _T("&About"));
488 file_menu
->AppendSeparator();
489 file_menu
->Append(ID_EXIT
, _T("E&xit"));
491 wxMenu
*data_menu
= new wxMenu
;
492 data_menu
->AppendRadioItem(ID_SINGLE_SEL_MODE
, _T("&Single selection mode"));
493 data_menu
->AppendRadioItem(ID_MULTIPLE_SEL_MODE
, _T("&Multiple selection mode"));
494 data_menu
->AppendSeparator();
495 data_menu
->AppendCheckItem(ID_NO_HEADER_MODE
, _T("No header mode"));
496 data_menu
->AppendCheckItem(ID_HORIZ_RULES_MODE
, _T("Horizontal rules"));
497 data_menu
->AppendCheckItem(ID_VERT_RULES_MODE
, _T("Vertical rules"));
498 data_menu
->AppendSeparator();
499 data_menu
->Append(ID_RESIZEABLE_COLUMNS
, _T("Set column(s) as resizeable..."));
500 data_menu
->Append(ID_SORTABLE_COLUMNS
, _T("Set column(s) as sortable..."));
501 data_menu
->Append(ID_HIDDEN_COLUMNS
, _T("Set column(s) as hidden..."));
502 data_menu
->AppendSeparator();
503 data_menu
->Append(ID_CHOOSE_COLUMN_ALIGNMENT
, _T("Set column(s) title alignment..."));
504 data_menu
->Append(ID_CHOOSE_CONTENTS_ALIGNMENT
, _T("Set column(s) contents alignment..."));
505 data_menu
->AppendSeparator();
506 data_menu
->Append(ID_SET_MINWIDTH
, _T("Set column(s) minimal width..."));
507 data_menu
->Append(ID_SET_WIDTH
, _T("Set column(s) width..."));
509 wxMenuBar
*menu_bar
= new wxMenuBar
;
510 menu_bar
->Append(file_menu
, _T("&File"));
511 menu_bar
->Append(data_menu
, _T("&DataViewCtrl"));
513 SetMenuBar(menu_bar
);
517 // check the menus for the default wxDataViewCtrl style
518 data_menu
->Check(ID_MULTIPLE_SEL_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_MULTIPLE
) != 0);
519 data_menu
->Check(ID_NO_HEADER_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_NO_HEADER
) != 0);
520 data_menu
->Check(ID_HORIZ_RULES_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_HORIZ_RULES
) != 0);
521 data_menu
->Check(ID_VERT_RULES_MODE
, (DATAVIEW_DEFAULT_STYLE
& wxDV_VERT_RULES
) != 0);
524 // build the other controls:
526 m_splitter
= new wxSplitterWindow( this, wxID_ANY
);
527 m_splitter
->SetSashGravity(0.5);
529 m_panelLeft
= new wxPanel( m_splitter
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
531 m_panelRight
= new wxPanel( m_splitter
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
533 wxSizer
*szLeft
= new wxBoxSizer(wxVERTICAL
);
534 wxSizer
*szRight
= new wxBoxSizer(wxVERTICAL
);
536 dataview_left
= NULL
;
537 dataview_right
= NULL
;
538 CreateDataViewControls();
541 szLeft
->Add( dataview_left
, 1, wxGROW
|wxALL
, 5 );
542 m_panelLeft
->SetSizerAndFit(szLeft
);
545 wxStaticText
*stattext
=
546 new wxStaticText(m_panelRight
, wxID_ANY
,
547 wxT("This is another wxDataViewCtrl using the same wxDataViewModel ")
548 wxT("of the wxDataViewCtrl on the left but, unlike it, this window ")
549 wxT("won't react to the style/column changes done through the ")
550 wxT("'DataViewCtrl' menu"));
551 stattext
->Wrap(GetClientSize().GetWidth() / 2);
553 szRight
->Add( stattext
, 0, wxALL
, 5 );
554 szRight
->Add( dataview_right
, 1, wxGROW
|wxALL
, 5 );
555 m_panelRight
->SetSizerAndFit(szRight
);
557 // split the two panels
558 m_splitter
->SplitVertically(m_panelLeft
, m_panelRight
);
559 this->SetMinSize(m_splitter
->GetBestSize());
562 void MyFrame::CreateDataViewControls()
564 wxDataViewCtrl
*old1
= NULL
, *old2
= NULL
;
567 old1
= dataview_left
;
569 old2
= dataview_right
;
573 if (GetMenuBar()->FindItem(ID_MULTIPLE_SEL_MODE
)->IsChecked())
574 style
|= wxDV_MULTIPLE
;
575 if (GetMenuBar()->FindItem(ID_NO_HEADER_MODE
)->IsChecked())
576 style
|= wxDV_NO_HEADER
;
577 if (GetMenuBar()->FindItem(ID_HORIZ_RULES_MODE
)->IsChecked())
578 style
|= wxDV_HORIZ_RULES
;
579 if (GetMenuBar()->FindItem(ID_VERT_RULES_MODE
)->IsChecked())
580 style
|= wxDV_VERT_RULES
;
583 // Left wxDataViewCtrl
584 dataview_left
= new wxDataViewCtrl( m_panelLeft
, wxID_ANY
, wxDefaultPosition
,
585 wxDefaultSize
, style
);
588 wxObjectDataPtr
<MyTextModel
> model(new MyTextModel
);
589 dataview_left
->AssociateModel( model
.get() );
591 dataview_left
->AppendTextColumn( wxT("First"), 0, wxDATAVIEW_CELL_INERT
, -1,
593 dataview_left
->AppendTextColumn( wxT("Second"), 1, wxDATAVIEW_CELL_INERT
, -1,
596 wxDataViewTextRenderer
*text_renderer
=
597 new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
598 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 2,
600 dataview_left
->AppendColumn( column
);
602 dataview_left
->AppendToggleColumn( wxT("fourth"), 3, wxDATAVIEW_CELL_INERT
, -1,
605 MyCustomRenderer
*custom_renderer
= new MyCustomRenderer
;
606 column
= new wxDataViewColumn( wxT("custom"), custom_renderer
, 4, -1, DEFAULT_ALIGN
);
607 dataview_left
->AppendColumn( column
);
609 dataview_left
->AppendProgressColumn( wxT("progress"), 5, wxDATAVIEW_CELL_INERT
, -1,
612 dataview_left
->AppendDateColumn( wxT("date"), 6, wxDATAVIEW_CELL_INERT
, -1, DEFAULT_ALIGN
);
615 // Right wxDataViewCtrl using the same model
616 dataview_right
= new wxDataViewCtrl( m_panelRight
, wxID_ANY
);
617 dataview_right
->AssociateModel( model
.get() );
619 text_renderer
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
620 column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 2 );
621 dataview_right
->AppendColumn( column
);
622 dataview_right
->AppendTextColumn( wxT("first"), 0 );
623 dataview_right
->AppendTextColumn( wxT("second"), 1 );
624 wxDataViewToggleRenderer
*toggle_renderer
=
625 new wxDataViewToggleRenderer( wxT("bool"), wxDATAVIEW_CELL_ACTIVATABLE
);
626 column
= new wxDataViewColumn( wxT("bool"), toggle_renderer
, 3, 30 );
627 dataview_right
->AppendColumn( column
);
629 dataview_right
->AppendDateColumn( wxT("date"), 6 );
632 // layout the new dataview controls
635 m_panelLeft
->GetSizer()->Replace(old1
, dataview_left
);
638 m_panelLeft
->Layout();
643 m_panelRight
->GetSizer()->Replace(old2
, dataview_right
);
646 m_panelRight
->Layout();
650 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
655 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
657 wxAboutDialogInfo info
;
658 info
.SetName(_("DataView sample"));
659 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
660 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));
665 void MyFrame::OnNewSortingFrame(wxCommandEvent
& WXUNUSED(event
) )
667 MySortingFrame
*frame2
=
668 new MySortingFrame(NULL
, wxT("wxDataViewCtrl sorting test"), 10, 150, 600, 500);
672 void MyFrame::OnStyleChange(wxCommandEvent
& WXUNUSED(event
) )
674 // recreate the wxDataViewCtrl:
675 CreateDataViewControls();
678 void MyFrame::OnColumnSetting(wxCommandEvent
& event
)
682 bool header
= false, minwidth
= false;
685 switch (event
.GetId())
687 case ID_RESIZEABLE_COLUMNS
:
688 flag
= wxDATAVIEW_COL_RESIZABLE
;
689 columns
= GetFlaggedColumns(flag
);
691 case ID_SORTABLE_COLUMNS
:
692 flag
= wxDATAVIEW_COL_SORTABLE
;
693 columns
= GetFlaggedColumns(flag
);
695 case ID_HIDDEN_COLUMNS
:
696 flag
= wxDATAVIEW_COL_HIDDEN
;
697 columns
= GetFlaggedColumns(flag
);
700 case ID_CHOOSE_COLUMN_ALIGNMENT
:
701 msg
= wxT("Select the columns whose headers' alignment will be modified.");
704 case ID_CHOOSE_CONTENTS_ALIGNMENT
:
705 msg
= wxT("Select the columns whose contents' alignment will be modified.");
709 case ID_SET_MINWIDTH
:
710 msg
= wxT("Please provide the new minimal width:");
714 msg
= wxT("Please provide the new width:");
719 // get column titles:
721 wxArrayString choices
;
722 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
723 choices
.Add(dataview_left
->GetColumn(i
)->GetTitle());
726 wxGetMultipleChoices(
728 wxT("Choose the columns to which apply the change."),
729 wxT("Choose the column"),
733 switch (event
.GetId())
735 case ID_RESIZEABLE_COLUMNS
:
736 case ID_SORTABLE_COLUMNS
:
737 case ID_HIDDEN_COLUMNS
:
738 SetFlag(columns
, flag
);
741 case ID_CHOOSE_COLUMN_ALIGNMENT
:
742 case ID_CHOOSE_CONTENTS_ALIGNMENT
:
743 SetAlignment(columns
, header
, ChooseAlign(msg
, header
));
746 case ID_SET_MINWIDTH
:
749 int def
= minwidth
? wxDVC_DEFAULT_MINWIDTH
: wxDVC_DEFAULT_WIDTH
;
751 msg
<< wxT("\nNOTE: all non-selected columns will be reset to a width of ")
752 << def
<< wxT(" pixels.");
755 wxGetNumberFromUser(msg
, wxT("New value:"), wxT("Modify width"),
759 SetWidth(columns
, minwidth
, ret
);
764 dataview_left
->Refresh();
767 wxAlignment
MyFrame::ChooseAlign(const wxString
&msg
, bool onlyHorizontal
)
769 const wxString choices
[] =
772 wxT("Center horizontally"),
775 wxT("Center vertically"),
780 wxAlignment flags
[] =
783 wxALIGN_CENTER_HORIZONTAL
,
786 wxALIGN_CENTER_VERTICAL
,
791 int n
= WXSIZEOF(choices
);
793 n
= 3; // show only the first three choices
795 int choice
= wxGetSingleChoiceIndex(
796 msg
+ wxT("\nNOTE: _all_ non-selected column's alignment will be reset to wxALIGN_LEFT!"),
801 if (choice
== wxNOT_FOUND
)
804 return flags
[choice
];
807 void MyFrame::SetFlag(const wxArrayInt
&idx
, int toadd
)
809 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
811 int current
= dataview_left
->GetColumn(i
)->GetFlags();
813 if (idx
.Index(i
) != wxNOT_FOUND
)
814 dataview_left
->GetColumn(i
)->SetFlags(current
| toadd
);
816 dataview_left
->GetColumn(i
)->SetFlags(current
& ~toadd
);
820 wxArrayInt
MyFrame::GetFlaggedColumns(int flag
)
823 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
824 if (dataview_left
->GetColumn(i
)->GetFlags() & flag
)
829 void MyFrame::SetAlignment(const wxArrayInt
&idx
, bool header
, wxAlignment align
)
831 // set to DEFAULT_ALIGN all columns except those
832 // contained in 'idx' which are set to 'align'
834 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
836 wxAlignment toset
= DEFAULT_ALIGN
;
837 if (idx
.Index(i
) != wxNOT_FOUND
)
841 dataview_left
->GetColumn(i
)->SetAlignment(toset
);
843 dataview_left
->GetColumn(i
)->GetRenderer()->SetAlignment(toset
);
847 void MyFrame::SetWidth(const wxArrayInt
&idx
, bool minwidth
, int width
)
849 // set to wxDVC_DEFAULT_WIDTH wide all columns except those
850 // contained in 'idx' which are set to 'width'
852 for (size_t i
=0; i
<dataview_left
->GetColumnCount(); i
++)
854 int toset
= minwidth
? wxDVC_DEFAULT_MINWIDTH
: wxDVC_DEFAULT_WIDTH
;
855 if (idx
.Index(i
) != wxNOT_FOUND
)
859 dataview_left
->GetColumn(i
)->SetMinWidth(toset
);
861 dataview_left
->GetColumn(i
)->SetWidth(toset
);
866 // -------------------------------------
868 // -------------------------------------
870 BEGIN_EVENT_TABLE(MySortingFrame
,wxFrame
)
871 EVT_BUTTON( ID_APPEND_ROW_LEFT
, MySortingFrame::OnAppendRowLeft
)
872 EVT_BUTTON( ID_PREPEND_ROW_LEFT
, MySortingFrame::OnPrependRowLeft
)
873 EVT_BUTTON( ID_INSERT_ROW_LEFT
, MySortingFrame::OnInsertRowLeft
)
874 EVT_BUTTON( ID_DELETE_ROW_LEFT
, MySortingFrame::OnDeleteRowLeft
)
875 EVT_BUTTON( ID_SELECT
, MySortingFrame::OnSelect
)
876 EVT_BUTTON( ID_UNSELECT_ALL
, MySortingFrame::OnUnselectAll
)
877 EVT_DATAVIEW_ROW_SELECTED( ID_SORTED
, MySortingFrame::OnSelectedSorted
)
878 EVT_DATAVIEW_ROW_SELECTED( ID_UNSORTED
, MySortingFrame::OnSelectedUnsorted
)
879 EVT_DATAVIEW_ROW_ACTIVATED( ID_UNSORTED
, MySortingFrame::OnActivatedUnsorted
)
880 EVT_DATAVIEW_COLUMN_HEADER_CLICK( ID_SORTED
, MySortingFrame::OnHeaderClickSorted
)
881 EVT_DATAVIEW_COLUMN_HEADER_CLICK( ID_UNSORTED
, MySortingFrame::OnHeaderClickUnsorted
)
884 MySortingFrame::MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
885 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
889 SetIcon(wxICON(sample
));
892 wxPanel
*main
= new wxPanel(this);
894 // Left wxDataViewCtrl
895 dataview_left
= new wxDataViewCtrl( main
, ID_UNSORTED
, wxDefaultPosition
,
896 wxDefaultSize
, wxDV_MULTIPLE
);
898 m_unsorted_model
.reset(new MyUnsortedTextModel
);
899 dataview_left
->AssociateModel( m_unsorted_model
.get() );
901 wxDataViewTextRenderer
*text_renderer
=
902 new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
903 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 0 );
904 dataview_left
->AppendColumn( column
);
905 dataview_left
->AppendTextColumn( wxT("second"), 1 );
906 dataview_left
->AppendColumn( new wxDataViewColumn( wxBitmap(null_xpm
),
907 new wxDataViewBitmapRenderer
, 2, 25 ) );
908 dataview_left
->AppendColumn( new wxDataViewColumn( wxT("icon"),
909 new wxDataViewBitmapRenderer
, 3, 25 ) );
911 // Right wxDataViewCtrl using the sorting model
912 dataview_right
= new wxDataViewCtrl( main
, ID_SORTED
);
914 m_sorted_model
.reset(new wxDataViewSortedListModel( m_unsorted_model
.get() ));
915 dataview_right
->AssociateModel( m_sorted_model
.get() );
917 text_renderer
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
918 column
= new wxDataViewColumn( wxT("editable"), text_renderer
, 0, -1,
920 wxDATAVIEW_COL_SORTABLE
|wxDATAVIEW_COL_RESIZABLE
);
921 dataview_right
->AppendColumn( column
);
923 dataview_right
->AppendTextColumn( wxT("second"), 1 );
925 // layout dataview controls.
927 wxBoxSizer
*top_sizer
= new wxBoxSizer( wxHORIZONTAL
);
928 top_sizer
->Add( dataview_left
, 1, wxGROW
);
929 top_sizer
->Add(10,10);
930 top_sizer
->Add( dataview_right
, 1, wxGROW
);
932 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
933 button_sizer
->Add( 10, 10, 1 );
934 wxFlexGridSizer
*left_sizer
= new wxFlexGridSizer( 2 );
935 left_sizer
->Add( new wxButton( main
, ID_APPEND_ROW_LEFT
, wxT("Append") ), 0, wxALL
, 5 );
936 left_sizer
->Add( new wxButton( main
, ID_PREPEND_ROW_LEFT
, wxT("Prepend") ), 0, wxALL
, 5 );
937 left_sizer
->Add( new wxButton( main
, ID_INSERT_ROW_LEFT
, wxT("Insert") ), 0, wxALL
, 5 );
938 left_sizer
->Add( new wxButton( main
, ID_DELETE_ROW_LEFT
, wxT("Delete second") ), 0, wxALL
, 5 );
939 left_sizer
->Add( new wxButton( main
, ID_EDIT_ROW_LEFT
, wxT("Edit") ), 0, wxALL
, 5 );
940 left_sizer
->Add( 5,5 );
941 left_sizer
->Add( new wxButton( main
, ID_SELECT
, wxT("Select third") ), 0, wxALL
, 5 );
942 left_sizer
->Add( new wxButton( main
, ID_UNSELECT_ALL
, wxT("Unselect all") ), 0, wxALL
, 5 );
943 button_sizer
->Add( left_sizer
);
944 button_sizer
->Add( 10, 10, 2 );
945 wxFlexGridSizer
*right_sizer
= new wxFlexGridSizer( 2 );
946 right_sizer
->Add( new wxButton( main
, ID_APPEND_ROW_RIGHT
, wxT("Append") ), 0, wxALL
, 5 );
947 right_sizer
->Add( new wxButton( main
, ID_PREPEND_ROW_RIGHT
, wxT("Prepend") ), 0, wxALL
, 5 );
948 right_sizer
->Add( new wxButton( main
, ID_INSERT_ROW_RIGHT
, wxT("Insert") ), 0, wxALL
, 5 );
949 right_sizer
->Add( new wxButton( main
, ID_DELETE_ROW_RIGHT
, wxT("Delete second") ), 0, wxALL
, 5 );
950 right_sizer
->Add( new wxButton( main
, ID_EDIT_ROW_RIGHT
, wxT("Edit") ), 0, wxALL
, 5 );
951 button_sizer
->Add( right_sizer
);
952 button_sizer
->Add( 10, 10, 1 );
954 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
955 main_sizer
->Add( top_sizer
, 1, wxGROW
);
956 main_sizer
->Add( button_sizer
, 0, wxGROW
);
958 m_logWindow
= new wxTextCtrl(main
, wxID_ANY
, wxEmptyString
,
959 wxDefaultPosition
, wxDefaultSize
,
960 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
961 main_sizer
->Add( 20,20 );
962 main_sizer
->Add( m_logWindow
, 1, wxGROW
);
964 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
966 main
->SetSizer( main_sizer
);
969 MySortingFrame::~MySortingFrame()
971 delete wxLog::SetActiveTarget(m_logOld
);
974 void MySortingFrame::OnSelectedUnsorted(wxDataViewEvent
&event
)
976 int row
= event
.GetRow();
977 wxLogMessage( wxT("OnSelected from unsorted list, selected %d"), row
);
979 wxLogMessage( wxT("wxDataViewCtrl::IsSelected( %d ): %d (as int)"),
980 row
, (int) dataview_right
->IsSelected( row
) );
983 void MySortingFrame::OnSelectedSorted(wxDataViewEvent
&event
)
985 wxLogMessage( wxT("OnSelected from sorted list, selected %d"), (int) event
.GetRow() );
988 void MySortingFrame::OnActivatedUnsorted(wxDataViewEvent
&event
)
990 wxLogMessage( wxT("OnActivated from unsorted list, activated %d"), (int) event
.GetRow() );
993 void MySortingFrame::OnHeaderClickSorted(wxDataViewEvent
&event
)
995 wxDataViewColumn
*col
= event
.GetDataViewColumn();
996 wxLogMessage( wxT("OnHeaderClick from sorted list, column %s"), col
->GetTitle().c_str() );
998 if (col
->GetTitle() == wxT("editable"))
1000 // this is the sorting column
1001 if (col
->IsSortOrderAscending())
1003 col
->SetSortOrder( false );
1004 m_sorted_model
->SetAscending( false );
1005 m_sorted_model
->Resort();
1009 col
->SetSortOrder( true );
1010 m_sorted_model
->SetAscending( true );
1011 m_sorted_model
->Resort();
1016 void MySortingFrame::OnHeaderClickUnsorted(wxDataViewEvent
&event
)
1018 wxLogMessage( wxT("OnHeaderClick from unsorted list, column %s"),
1019 event
.GetDataViewColumn()->GetTitle().c_str() );
1022 void MySortingFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
1027 void MySortingFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1029 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control sorting"),
1030 _T("About DataView"), wxOK
);
1035 void MySortingFrame::OnSelect(wxCommandEvent
& WXUNUSED(event
))
1037 dataview_left
->SetSelection( 2 );
1040 void MySortingFrame::OnUnselectAll(wxCommandEvent
& WXUNUSED(event
))
1042 dataview_left
->ClearSelection();
1045 void MySortingFrame::OnAppendRowLeft(wxCommandEvent
& WXUNUSED(event
))
1047 wxTextEntryDialog
dialog( this, wxT("Enter text to append") );
1048 if (dialog
.ShowModal() == wxID_OK
)
1050 wxString value
= dialog
.GetValue();
1052 m_unsorted_model
->AppendRow( value
);
1056 void MySortingFrame::OnPrependRowLeft(wxCommandEvent
& WXUNUSED(event
))
1058 wxTextEntryDialog
dialog( this, wxT("Enter text to prepend") );
1059 if (dialog
.ShowModal() == wxID_OK
)
1061 wxString value
= dialog
.GetValue();
1063 m_unsorted_model
->PrependRow( value
);
1067 void MySortingFrame::OnInsertRowLeft(wxCommandEvent
& WXUNUSED(event
))
1069 wxTextEntryDialog
dialog( this, wxT("Enter text to insert before second") );
1070 if (dialog
.ShowModal() == wxID_OK
)
1072 wxString value
= dialog
.GetValue();
1074 m_unsorted_model
->InsertRowAt1( value
);
1078 void MySortingFrame::OnDeleteRowLeft(wxCommandEvent
& WXUNUSED(event
))
1080 m_unsorted_model
->DeleteRow( 1 );
1083 void MySortingFrame::OnEditRowLeft(wxCommandEvent
& WXUNUSED(event
))
1087 void MySortingFrame::OnAppendRowRight(wxCommandEvent
& WXUNUSED(event
))
1091 void MySortingFrame::OnPrependRowRight(wxCommandEvent
& WXUNUSED(event
))
1095 void MySortingFrame::OnInsertRowRight(wxCommandEvent
& WXUNUSED(event
))
1099 void MySortingFrame::OnDeleteRowRight(wxCommandEvent
& WXUNUSED(event
))
1103 void MySortingFrame::OnEditRowRight(wxCommandEvent
& WXUNUSED(event
))