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