Applied Ryan's native wxListBox and wxCheckListBox patch
[wxWidgets.git] / src / gtk / checklst.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: checklst.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Modified by: Ryan Norton (Native GTK2.0+ checklist)
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/defs.h"
15
16 #if wxUSE_CHECKLISTBOX
17
18 #include "wx/checklst.h"
19 #include "wx/gtk/private.h"
20 #include "wx/gtk/treeentry_gtk.h"
21
22 #include <gdk/gdk.h>
23 #include <gtk/gtk.h>
24
25 //-----------------------------------------------------------------------------
26 // "toggled"
27 //-----------------------------------------------------------------------------
28 #if wxUSE_NATIVEGTKCHECKLIST
29 extern "C" {
30 static void gtk_checklist_toggled(GtkCellRendererToggle *renderer,
31 gchar *stringpath,
32 wxCheckListBox *listbox)
33 {
34 wxCHECK_RET( listbox->m_treeview != NULL, wxT("invalid listbox") );
35
36 GtkTreePath* path = gtk_tree_path_new_from_string(stringpath);
37 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED,
38 listbox->GetId() );
39 new_event.SetEventObject( listbox );
40 new_event.SetInt( gtk_tree_path_get_indices(path)[0] );
41 gtk_tree_path_free(path);
42 listbox->Check( new_event.GetInt(), !listbox->IsChecked(new_event.GetInt()));
43 listbox->GetEventHandler()->ProcessEvent( new_event );
44 }
45 }
46 #endif
47
48 //-----------------------------------------------------------------------------
49 // wxCheckListBox
50 //-----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox,wxListBox)
53
54 wxCheckListBox::wxCheckListBox() : wxListBox()
55 {
56 m_hasCheckBoxes = TRUE;
57 }
58
59 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
60 const wxPoint& pos,
61 const wxSize& size,
62 int nStrings,
63 const wxString *choices,
64 long style,
65 const wxValidator& validator,
66 const wxString& name )
67 {
68 m_hasCheckBoxes = TRUE;
69 wxListBox::Create( parent, id, pos, size, nStrings, choices, style, validator, name );
70 }
71
72 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
73 const wxPoint& pos,
74 const wxSize& size,
75 const wxArrayString& choices,
76 long style,
77 const wxValidator& validator,
78 const wxString& name )
79 {
80 m_hasCheckBoxes = TRUE;
81 wxListBox::Create( parent, id, pos, size, choices,
82 style, validator, name );
83 }
84
85 #if wxUSE_NATIVEGTKCHECKLIST
86 void wxCheckListBox::DoCreateCheckList()
87 {
88 //Create the checklist in our treeview and set up events for it
89 GtkCellRenderer* renderer =
90 gtk_cell_renderer_toggle_new();
91 GtkTreeViewColumn* column =
92 gtk_tree_view_column_new_with_attributes( "", renderer,
93 "active", 0,
94 NULL );
95 gtk_tree_view_column_set_fixed_width(column, 20);
96 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
97 gtk_tree_view_column_set_clickable(column, TRUE);
98
99 g_signal_connect (renderer, "toggled",
100 G_CALLBACK (gtk_checklist_toggled),
101 this);
102
103 gtk_tree_view_append_column(m_treeview, column);
104 }
105
106 bool wxCheckListBox::IsChecked( int index ) const
107 {
108 wxCHECK_MSG( m_treeview != NULL, FALSE, wxT("invalid checklistbox") );
109
110 GtkTreeIter iter;
111 gboolean res = gtk_tree_model_iter_nth_child(
112 GTK_TREE_MODEL(m_liststore),
113 &iter, NULL, //NULL = parent = get first
114 index
115 );
116 if(!res)
117 return false;
118
119 GValue value = {0, };
120 gtk_tree_model_get_value(GTK_TREE_MODEL(m_liststore),
121 &iter,
122 0, //column
123 &value);
124
125 return g_value_get_boolean(&value) == TRUE ? true : false;
126 }
127
128 void wxCheckListBox::Check( int index, bool check )
129 {
130 wxCHECK_RET( m_treeview != NULL, wxT("invalid checklistbox") );
131
132 GtkTreeIter iter;
133 gboolean res = gtk_tree_model_iter_nth_child(
134 GTK_TREE_MODEL(m_liststore),
135 &iter, NULL, //NULL = parent = get first
136 index
137 );
138 if(!res)
139 return;
140
141 gtk_list_store_set(m_liststore,
142 &iter,
143 0, //column
144 check ? TRUE : FALSE, -1);
145 }
146
147 int wxCheckListBox::GetItemHeight() const
148 {
149 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox"));
150
151 gint height;
152 gtk_tree_view_column_cell_get_size(
153 gtk_tree_view_get_column(m_treeview, 0),
154 NULL, NULL, NULL, NULL,
155 &height);
156 return height;
157 }
158
159 #else //NON-NATIVE
160
161 bool wxCheckListBox::IsChecked( int index ) const
162 {
163 wxCHECK_MSG( m_treeview != NULL, FALSE, wxT("invalid checklistbox") );
164
165 GtkTreeEntry* entry = GtkGetEntry(index);
166 if (entry)
167 {
168 wxString str( wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) ) );
169
170 return str.GetChar(1) == wxCHECKLBOX_CHECKED;
171 }
172
173 wxFAIL_MSG(wxT("wrong checklistbox index"));
174 return FALSE;
175 }
176
177 void wxCheckListBox::Check( int index, bool check )
178 {
179 wxCHECK_RET( m_treeview != NULL, wxT("invalid checklistbox") );
180
181 GtkTreeEntry* entry = GtkGetEntry(index);
182 if (entry)
183 {
184 wxString str( wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) ) );
185
186 if (check == (str.GetChar(1) == wxCHECKLBOX_CHECKED))
187 return;
188
189 str.SetChar( 1, check ? wxCHECKLBOX_CHECKED : wxCHECKLBOX_UNCHECKED );
190
191 gtk_tree_entry_set_label( entry, wxGTK_CONV( str ) );
192
193 return;
194 }
195
196 wxFAIL_MSG(wxT("wrong checklistbox index"));
197 }
198
199 int wxCheckListBox::GetItemHeight() const
200 {
201 // FIXME
202 return 22;
203 }
204
205 #endif //wxUSE_NATIVEGTKCHECKLIST
206
207 #endif //wxUSE_CHECKLISTBOX