]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/checklst.cpp
Refactor wxTranslationsLoader API.
[wxWidgets.git] / src / gtk / checklst.cpp
CommitLineData
caaa4cfd 1/////////////////////////////////////////////////////////////////////////////
caf6e6de 2// Name: src/gtk/checklst.cpp
caaa4cfd
RR
3// Purpose:
4// Author: Robert Roebling
caf6e6de 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
dcf924a3
RR
14#if wxUSE_CHECKLISTBOX
15
1e6feb95 16#include "wx/checklst.h"
fab591c5 17#include "wx/gtk/private.h"
4a46cbe8 18#include "wx/gtk/treeentry_gtk.h"
1e6feb95 19
071a2d78
RR
20#include <gdk/gdk.h>
21#include <gtk/gtk.h>
83624f79 22
4a46cbe8
RR
23//-----------------------------------------------------------------------------
24// "toggled"
25//-----------------------------------------------------------------------------
4a46cbe8 26extern "C" {
e4161a2a 27static void gtk_checklist_toggled(GtkCellRendererToggle * WXUNUSED(renderer),
4a46cbe8
RR
28 gchar *stringpath,
29 wxCheckListBox *listbox)
30{
31 wxCHECK_RET( listbox->m_treeview != NULL, wxT("invalid listbox") );
32
33 GtkTreePath* path = gtk_tree_path_new_from_string(stringpath);
caf6e6de 34 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED,
4a46cbe8
RR
35 listbox->GetId() );
36 new_event.SetEventObject( listbox );
37 new_event.SetInt( gtk_tree_path_get_indices(path)[0] );
51fbe4cc 38 new_event.SetString( listbox->GetString( new_event.GetInt() ));
4a46cbe8
RR
39 gtk_tree_path_free(path);
40 listbox->Check( new_event.GetInt(), !listbox->IsChecked(new_event.GetInt()));
937013e0 41 listbox->HandleWindowEvent( new_event );
4a46cbe8
RR
42}
43}
4a46cbe8 44
caaa4cfd
RR
45//-----------------------------------------------------------------------------
46// wxCheckListBox
47//-----------------------------------------------------------------------------
48
49IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox,wxListBox)
50
88ac883a 51wxCheckListBox::wxCheckListBox() : wxListBox()
caaa4cfd 52{
caf6e6de 53 m_hasCheckBoxes = true;
caaa4cfd 54}
bbe0af5b 55
caaa4cfd 56wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
d752a3c3
VZ
57 const wxPoint& pos,
58 const wxSize& size,
3d257b8d 59 int nStrings,
d752a3c3
VZ
60 const wxString *choices,
61 long style,
62 const wxValidator& validator,
63 const wxString& name )
caaa4cfd 64{
caf6e6de 65 m_hasCheckBoxes = true;
caaa4cfd
RR
66 wxListBox::Create( parent, id, pos, size, nStrings, choices, style, validator, name );
67}
68
584ad2a3
MB
69wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
70 const wxPoint& pos,
71 const wxSize& size,
72 const wxArrayString& choices,
73 long style,
74 const wxValidator& validator,
75 const wxString& name )
76{
caf6e6de 77 m_hasCheckBoxes = true;
584ad2a3
MB
78 wxListBox::Create( parent, id, pos, size, choices,
79 style, validator, name );
80}
81
4a46cbe8
RR
82void wxCheckListBox::DoCreateCheckList()
83{
84 //Create the checklist in our treeview and set up events for it
caf6e6de 85 GtkCellRenderer* renderer =
4a46cbe8 86 gtk_cell_renderer_toggle_new();
caf6e6de 87 GtkTreeViewColumn* column =
4a46cbe8
RR
88 gtk_tree_view_column_new_with_attributes( "", renderer,
89 "active", 0,
90 NULL );
426d19f1
JS
91#if wxUSE_LIBHILDON2
92 gtk_tree_view_column_set_fixed_width(column, 40);
93#else
302c7351 94 gtk_tree_view_column_set_fixed_width(column, 22);
426d19f1
JS
95#endif // wxUSE_LIBHILDON2/!wxUSE_LIBHILDON2
96
4a46cbe8
RR
97 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
98 gtk_tree_view_column_set_clickable(column, TRUE);
99
100 g_signal_connect (renderer, "toggled",
101 G_CALLBACK (gtk_checklist_toggled),
102 this);
103
104 gtk_tree_view_append_column(m_treeview, column);
105}
106
aa61d352 107bool wxCheckListBox::IsChecked(unsigned int index) const
caaa4cfd 108{
aa61d352 109 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid checklistbox") );
4a46cbe8
RR
110
111 GtkTreeIter iter;
112 gboolean res = gtk_tree_model_iter_nth_child(
113 GTK_TREE_MODEL(m_liststore),
114 &iter, NULL, //NULL = parent = get first
115 index
aa61d352 116 );
4a46cbe8
RR
117 if(!res)
118 return false;
119
120 GValue value = {0, };
121 gtk_tree_model_get_value(GTK_TREE_MODEL(m_liststore),
122 &iter,
123 0, //column
caf6e6de 124 &value);
4a46cbe8
RR
125
126 return g_value_get_boolean(&value) == TRUE ? true : false;
127}
ff8bfdbb 128
aa61d352 129void wxCheckListBox::Check(unsigned int index, bool check)
4a46cbe8
RR
130{
131 wxCHECK_RET( m_treeview != NULL, wxT("invalid checklistbox") );
132
133 GtkTreeIter iter;
134 gboolean res = gtk_tree_model_iter_nth_child(
135 GTK_TREE_MODEL(m_liststore),
136 &iter, NULL, //NULL = parent = get first
137 index
aa61d352 138 );
4a46cbe8
RR
139 if(!res)
140 return;
141
142 gtk_list_store_set(m_liststore,
143 &iter,
144 0, //column
caf6e6de 145 check ? TRUE : FALSE, -1);
4a46cbe8
RR
146}
147
148int wxCheckListBox::GetItemHeight() const
149{
150 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox"));
151
152 gint height;
153 gtk_tree_view_column_cell_get_size(
154 gtk_tree_view_get_column(m_treeview, 0),
155 NULL, NULL, NULL, NULL,
156 &height);
157 return height;
158}
ff8bfdbb 159
4a46cbe8 160#endif //wxUSE_CHECKLISTBOX