1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/checklst.cpp
3 // Purpose:
4 // Author:      Robert Roebling
5 // Modified by: Ryan Norton (Native GTK2.0+ checklist)
6 // Copyright:   (c) 1998 Robert Roebling
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12 
13 #if wxUSE_CHECKLISTBOX
14 
15 #include "wx/checklst.h"
16 
17 #include "wx/gtk/private.h"
18 #include "wx/gtk/private/treeview.h"
19 
20 //-----------------------------------------------------------------------------
21 // "toggled"
22 //-----------------------------------------------------------------------------
23 extern "C" {
gtk_checklist_toggled(GtkCellRendererToggle * WXUNUSED (renderer),gchar * stringpath,wxCheckListBox * listbox)24 static void gtk_checklist_toggled(GtkCellRendererToggle * WXUNUSED(renderer),
25                                   gchar                 *stringpath,
26                                   wxCheckListBox        *listbox)
27 {
28     wxCHECK_RET( listbox->m_treeview != NULL, wxT("invalid listbox") );
29 
30     wxGtkTreePath path(stringpath);
31     wxCommandEvent new_event( wxEVT_CHECKLISTBOX,
32                               listbox->GetId() );
33     new_event.SetEventObject( listbox );
34     new_event.SetInt( gtk_tree_path_get_indices(path)[0] );
35     new_event.SetString( listbox->GetString( new_event.GetInt() ));
36     listbox->Check( new_event.GetInt(), !listbox->IsChecked(new_event.GetInt()));
37     listbox->HandleWindowEvent( new_event );
38 }
39 }
40 
41 //-----------------------------------------------------------------------------
42 // wxCheckListBox
43 //-----------------------------------------------------------------------------
44 
wxCheckListBox()45 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
46 {
47     m_hasCheckBoxes = true;
48 }
49 
wxCheckListBox(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,int nStrings,const wxString * choices,long style,const wxValidator & validator,const wxString & name)50 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
51                                const wxPoint& pos,
52                                const wxSize& size,
53                                int nStrings,
54                                const wxString *choices,
55                                long style,
56                                const wxValidator& validator,
57                                const wxString& name )
58 {
59     m_hasCheckBoxes = true;
60     wxListBox::Create( parent, id, pos, size, nStrings, choices, style, validator, name );
61 }
62 
wxCheckListBox(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,const wxArrayString & choices,long style,const wxValidator & validator,const wxString & name)63 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
64                                const wxPoint& pos,
65                                const wxSize& size,
66                                const wxArrayString& choices,
67                                long style,
68                                const wxValidator& validator,
69                                const wxString& name )
70 {
71     m_hasCheckBoxes = true;
72     wxListBox::Create( parent, id, pos, size, choices,
73                        style, validator, name );
74 }
75 
DoCreateCheckList()76 void wxCheckListBox::DoCreateCheckList()
77 {
78     //Create the checklist in our treeview and set up events for it
79     GtkCellRenderer* renderer =
80         gtk_cell_renderer_toggle_new();
81     GtkTreeViewColumn* column =
82         gtk_tree_view_column_new_with_attributes( "", renderer,
83                                                   "active", 0,
84                                                   NULL );
85     gtk_tree_view_column_set_fixed_width(column, 22);
86 
87     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
88     gtk_tree_view_column_set_clickable(column, TRUE);
89 
90     g_signal_connect (renderer, "toggled",
91                       G_CALLBACK (gtk_checklist_toggled),
92                       this);
93 
94     gtk_tree_view_append_column(m_treeview, column);
95 }
96 
IsChecked(unsigned int index) const97 bool wxCheckListBox::IsChecked(unsigned int index) const
98 {
99     wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid checklistbox") );
100 
101     GtkTreeIter iter;
102     gboolean res = gtk_tree_model_iter_nth_child(
103                         GTK_TREE_MODEL(m_liststore),
104                         &iter, NULL, //NULL = parent = get first
105                         index
106                    );
107     if(!res)
108         return false;
109 
110     GValue value = G_VALUE_INIT;
111     gtk_tree_model_get_value(GTK_TREE_MODEL(m_liststore),
112                              &iter,
113                              0, //column
114                              &value);
115 
116     return g_value_get_boolean(&value) != 0;
117 }
118 
Check(unsigned int index,bool check)119 void wxCheckListBox::Check(unsigned int index, bool check)
120 {
121     wxCHECK_RET( m_treeview != NULL, wxT("invalid checklistbox") );
122 
123     GtkTreeIter iter;
124     gboolean res = gtk_tree_model_iter_nth_child(
125                         GTK_TREE_MODEL(m_liststore),
126                         &iter, NULL, //NULL = parent = get first
127                         index
128                    );
129     if(!res)
130         return;
131 
132     gtk_list_store_set(m_liststore,
133                        &iter,
134                        0, //column
135                        check ? TRUE : FALSE, -1);
136 }
137 
GetItemHeight() const138 int wxCheckListBox::GetItemHeight() const
139 {
140     wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox"));
141 
142     gint height;
143     gtk_tree_view_column_cell_get_size(
144         gtk_tree_view_get_column(m_treeview, 0),
145                                        NULL, NULL, NULL, NULL,
146                                        &height);
147     return height;
148 }
149 
150 #endif //wxUSE_CHECKLISTBOX
151