]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/checklst.cpp
fix for non precomp
[wxWidgets.git] / src / gtk / checklst.cpp
CommitLineData
caaa4cfd
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: checklst.cpp
3// Purpose:
4// Author: Robert Roebling
4a46cbe8 5// Modified by: Ryan Norton (Native GTK2.0+ checklist)
caaa4cfd
RR
6// Id: $Id$
7// Copyright: (c) 1998 Robert Roebling
65571936 8// Licence: wxWindows licence
caaa4cfd
RR
9/////////////////////////////////////////////////////////////////////////////
10
14f355c2
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
1e6feb95 14#include "wx/defs.h"
caaa4cfd 15
dcf924a3
RR
16#if wxUSE_CHECKLISTBOX
17
1e6feb95 18#include "wx/checklst.h"
fab591c5 19#include "wx/gtk/private.h"
4a46cbe8 20#include "wx/gtk/treeentry_gtk.h"
1e6feb95 21
071a2d78
RR
22#include <gdk/gdk.h>
23#include <gtk/gtk.h>
83624f79 24
4a46cbe8
RR
25//-----------------------------------------------------------------------------
26// "toggled"
27//-----------------------------------------------------------------------------
28#if wxUSE_NATIVEGTKCHECKLIST
29extern "C" {
30static 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
caaa4cfd
RR
48//-----------------------------------------------------------------------------
49// wxCheckListBox
50//-----------------------------------------------------------------------------
51
52IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox,wxListBox)
53
88ac883a 54wxCheckListBox::wxCheckListBox() : wxListBox()
caaa4cfd
RR
55{
56 m_hasCheckBoxes = TRUE;
57}
bbe0af5b 58
caaa4cfd 59wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
d752a3c3
VZ
60 const wxPoint& pos,
61 const wxSize& size,
3d257b8d 62 int nStrings,
d752a3c3
VZ
63 const wxString *choices,
64 long style,
65 const wxValidator& validator,
66 const wxString& name )
caaa4cfd
RR
67{
68 m_hasCheckBoxes = TRUE;
69 wxListBox::Create( parent, id, pos, size, nStrings, choices, style, validator, name );
70}
71
584ad2a3
MB
72wxCheckListBox::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
4a46cbe8
RR
85#if wxUSE_NATIVEGTKCHECKLIST
86void 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
caaa4cfd
RR
106bool wxCheckListBox::IsChecked( int index ) const
107{
4a46cbe8
RR
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}
ff8bfdbb 127
4a46cbe8
RR
128void 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
147int 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}
ff8bfdbb 158
4a46cbe8
RR
159#else //NON-NATIVE
160
161bool 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) ) );
ff8bfdbb 169
d752a3c3 170 return str.GetChar(1) == wxCHECKLBOX_CHECKED;
caaa4cfd 171 }
ff8bfdbb 172
223d09f6 173 wxFAIL_MSG(wxT("wrong checklistbox index"));
caaa4cfd
RR
174 return FALSE;
175}
176
177void wxCheckListBox::Check( int index, bool check )
178{
4a46cbe8 179 wxCHECK_RET( m_treeview != NULL, wxT("invalid checklistbox") );
ff8bfdbb 180
4a46cbe8
RR
181 GtkTreeEntry* entry = GtkGetEntry(index);
182 if (entry)
caaa4cfd 183 {
4a46cbe8 184 wxString str( wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) ) );
ff8bfdbb 185
d752a3c3
VZ
186 if (check == (str.GetChar(1) == wxCHECKLBOX_CHECKED))
187 return;
ff8bfdbb 188
d752a3c3 189 str.SetChar( 1, check ? wxCHECKLBOX_CHECKED : wxCHECKLBOX_UNCHECKED );
ff8bfdbb 190
4a46cbe8 191 gtk_tree_entry_set_label( entry, wxGTK_CONV( str ) );
ff8bfdbb
VZ
192
193 return;
caaa4cfd 194 }
ff8bfdbb 195
223d09f6 196 wxFAIL_MSG(wxT("wrong checklistbox index"));
caaa4cfd
RR
197}
198
ff8bfdbb 199int wxCheckListBox::GetItemHeight() const
4f22cf8d 200{
ff8bfdbb 201 // FIXME
4f22cf8d
RR
202 return 22;
203}
dcf924a3 204
4a46cbe8
RR
205#endif //wxUSE_NATIVEGTKCHECKLIST
206
207#endif //wxUSE_CHECKLISTBOX