1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/checklst.cpp
4 // Author: Robert Roebling
5 // Modified by: Ryan Norton (Native GTK2.0+ checklist)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #if wxUSE_CHECKLISTBOX
16 #include "wx/checklst.h"
17 #include "wx/gtk/private.h"
18 #include "wx/gtk/treeentry_gtk.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
26 #if wxUSE_NATIVEGTKCHECKLIST
28 static void gtk_checklist_toggled(GtkCellRendererToggle
*renderer
,
30 wxCheckListBox
*listbox
)
32 wxCHECK_RET( listbox
->m_treeview
!= NULL
, wxT("invalid listbox") );
34 GtkTreePath
* path
= gtk_tree_path_new_from_string(stringpath
);
35 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
,
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
);
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
,wxListBox
)
52 wxCheckListBox::wxCheckListBox() : wxListBox()
54 m_hasCheckBoxes
= true;
57 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
61 const wxString
*choices
,
63 const wxValidator
& validator
,
64 const wxString
& name
)
66 m_hasCheckBoxes
= true;
67 wxListBox::Create( parent
, id
, pos
, size
, nStrings
, choices
, style
, validator
, name
);
70 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
73 const wxArrayString
& choices
,
75 const wxValidator
& validator
,
76 const wxString
& name
)
78 m_hasCheckBoxes
= true;
79 wxListBox::Create( parent
, id
, pos
, size
, choices
,
80 style
, validator
, name
);
83 #if wxUSE_NATIVEGTKCHECKLIST
84 void wxCheckListBox::DoCreateCheckList()
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
,
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
);
97 g_signal_connect (renderer
, "toggled",
98 G_CALLBACK (gtk_checklist_toggled
),
101 gtk_tree_view_append_column(m_treeview
, column
);
104 bool wxCheckListBox::IsChecked(unsigned int index
) const
106 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid checklistbox") );
109 gboolean res
= gtk_tree_model_iter_nth_child(
110 GTK_TREE_MODEL(m_liststore
),
111 &iter
, NULL
, //NULL = parent = get first
117 GValue value
= {0, };
118 gtk_tree_model_get_value(GTK_TREE_MODEL(m_liststore
),
123 return g_value_get_boolean(&value
) == TRUE
? true : false;
126 void wxCheckListBox::Check(unsigned int index
, bool check
)
128 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid checklistbox") );
131 gboolean res
= gtk_tree_model_iter_nth_child(
132 GTK_TREE_MODEL(m_liststore
),
133 &iter
, NULL
, //NULL = parent = get first
139 gtk_list_store_set(m_liststore
,
142 check
? TRUE
: FALSE
, -1);
145 int wxCheckListBox::GetItemHeight() const
147 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox"));
150 gtk_tree_view_column_cell_get_size(
151 gtk_tree_view_get_column(m_treeview
, 0),
152 NULL
, NULL
, NULL
, NULL
,
159 bool wxCheckListBox::IsChecked( int index
) const
161 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid checklistbox") );
163 GtkTreeEntry
* entry
= GtkGetEntry(index
);
166 wxString
str( wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) ) );
168 return str
.GetChar(1) == wxCHECKLBOX_CHECKED
;
171 wxFAIL_MSG(wxT("wrong checklistbox index"));
175 void wxCheckListBox::Check( int index
, bool check
)
177 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid checklistbox") );
179 GtkTreeEntry
* entry
= GtkGetEntry(index
);
182 wxString
str( wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) ) );
184 if (check
== (str
.GetChar(1) == wxCHECKLBOX_CHECKED
))
187 str
.SetChar( 1, check
? wxCHECKLBOX_CHECKED
: wxCHECKLBOX_UNCHECKED
);
189 gtk_tree_entry_set_label( entry
, wxGTK_CONV( str
) );
194 wxFAIL_MSG(wxT("wrong checklistbox index"));
197 int wxCheckListBox::GetItemHeight() const
203 #endif //wxUSE_NATIVEGTKCHECKLIST
205 #endif //wxUSE_CHECKLISTBOX