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 <gtk/gtk.h>
18 #include "wx/gtk/private.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     GtkTreePath* path = gtk_tree_path_new_from_string(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     gtk_tree_path_free(path);
37     listbox->Check( new_event.GetInt(), !listbox->IsChecked(new_event.GetInt()));
38     listbox->HandleWindowEvent( new_event );
39 }
40 }
41 
42 //-----------------------------------------------------------------------------
43 // wxCheckListBox
44 //-----------------------------------------------------------------------------
45 
wxCheckListBox()46 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
47 {
48     m_hasCheckBoxes = true;
49 }
50 
wxCheckListBox(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,int nStrings,const wxString * choices,long style,const wxValidator & validator,const wxString & name)51 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
52                                const wxPoint& pos,
53                                const wxSize& size,
54                                int nStrings,
55                                const wxString *choices,
56                                long style,
57                                const wxValidator& validator,
58                                const wxString& name )
59 {
60     m_hasCheckBoxes = true;
61     wxListBox::Create( parent, id, pos, size, nStrings, choices, style, validator, name );
62 }
63 
wxCheckListBox(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,const wxArrayString & choices,long style,const wxValidator & validator,const wxString & name)64 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
65                                const wxPoint& pos,
66                                const wxSize& size,
67                                const wxArrayString& choices,
68                                long style,
69                                const wxValidator& validator,
70                                const wxString& name )
71 {
72     m_hasCheckBoxes = true;
73     wxListBox::Create( parent, id, pos, size, choices,
74                        style, validator, name );
75 }
76 
DoCreateCheckList()77 void wxCheckListBox::DoCreateCheckList()
78 {
79     //Create the checklist in our treeview and set up events for it
80     GtkCellRenderer* renderer =
81         gtk_cell_renderer_toggle_new();
82     GtkTreeViewColumn* column =
83         gtk_tree_view_column_new_with_attributes( "", renderer,
84                                                   "active", 0,
85                                                   NULL );
86 #if wxUSE_LIBHILDON2
87     gtk_tree_view_column_set_fixed_width(column, 40);
88 #else
89     gtk_tree_view_column_set_fixed_width(column, 22);
90 #endif // wxUSE_LIBHILDON2/!wxUSE_LIBHILDON2
91 
92     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
93     gtk_tree_view_column_set_clickable(column, TRUE);
94 
95     g_signal_connect (renderer, "toggled",
96                       G_CALLBACK (gtk_checklist_toggled),
97                       this);
98 
99     gtk_tree_view_append_column(m_treeview, column);
100 }
101 
IsChecked(unsigned int index) const102 bool wxCheckListBox::IsChecked(unsigned int index) const
103 {
104     wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid checklistbox") );
105 
106     GtkTreeIter iter;
107     gboolean res = gtk_tree_model_iter_nth_child(
108                         GTK_TREE_MODEL(m_liststore),
109                         &iter, NULL, //NULL = parent = get first
110                         index
111                    );
112     if(!res)
113         return false;
114 
115     GValue value = G_VALUE_INIT;
116     gtk_tree_model_get_value(GTK_TREE_MODEL(m_liststore),
117                              &iter,
118                              0, //column
119                              &value);
120 
121     return g_value_get_boolean(&value) != 0;
122 }
123 
Check(unsigned int index,bool check)124 void wxCheckListBox::Check(unsigned int index, bool check)
125 {
126     wxCHECK_RET( m_treeview != NULL, wxT("invalid checklistbox") );
127 
128     GtkTreeIter iter;
129     gboolean res = gtk_tree_model_iter_nth_child(
130                         GTK_TREE_MODEL(m_liststore),
131                         &iter, NULL, //NULL = parent = get first
132                         index
133                    );
134     if(!res)
135         return;
136 
137     gtk_list_store_set(m_liststore,
138                        &iter,
139                        0, //column
140                        check ? TRUE : FALSE, -1);
141 }
142 
GetItemHeight() const143 int wxCheckListBox::GetItemHeight() const
144 {
145     wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox"));
146 
147     gint height;
148     gtk_tree_view_column_cell_get_size(
149         gtk_tree_view_get_column(m_treeview, 0),
150                                        NULL, NULL, NULL, NULL,
151                                        &height);
152     return height;
153 }
154 
155 #endif //wxUSE_CHECKLISTBOX
156