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: checklst.cpp 42469 2006-10-26 20:29:02Z RR $
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//-----------------------------------------------------------------------------
26extern "C" {
27static void gtk_checklist_toggled(GtkCellRendererToggle *renderer,
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);
34    wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED,
35                              listbox->GetId() );
36    new_event.SetEventObject( listbox );
37    new_event.SetInt( gtk_tree_path_get_indices(path)[0] );
38    gtk_tree_path_free(path);
39    listbox->Check( new_event.GetInt(), !listbox->IsChecked(new_event.GetInt()));
40    listbox->GetEventHandler()->ProcessEvent( new_event );
41}
42}
43
44//-----------------------------------------------------------------------------
45// wxCheckListBox
46//-----------------------------------------------------------------------------
47
48IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox,wxListBox)
49
50wxCheckListBox::wxCheckListBox() : wxListBox()
51{
52    m_hasCheckBoxes = true;
53}
54
55wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
56                               const wxPoint& pos,
57                               const wxSize& size,
58                               int nStrings,
59                               const wxString *choices,
60                               long style,
61                               const wxValidator& validator,
62                               const wxString& name )
63{
64    m_hasCheckBoxes = true;
65    wxListBox::Create( parent, id, pos, size, nStrings, choices, style, validator, name );
66}
67
68wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
69                               const wxPoint& pos,
70                               const wxSize& size,
71                               const wxArrayString& choices,
72                               long style,
73                               const wxValidator& validator,
74                               const wxString& name )
75{
76    m_hasCheckBoxes = true;
77    wxListBox::Create( parent, id, pos, size, choices,
78                       style, validator, name );
79}
80
81void wxCheckListBox::DoCreateCheckList()
82{
83    //Create the checklist in our treeview and set up events for it
84    GtkCellRenderer* renderer =
85        gtk_cell_renderer_toggle_new();
86    GtkTreeViewColumn* column =
87        gtk_tree_view_column_new_with_attributes( "", renderer,
88                                                  "active", 0,
89                                                  NULL );
90    gtk_tree_view_column_set_fixed_width(column, 20);
91    gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
92    gtk_tree_view_column_set_clickable(column, TRUE);
93
94    g_signal_connect (renderer, "toggled",
95                      G_CALLBACK (gtk_checklist_toggled),
96                      this);
97
98    gtk_tree_view_append_column(m_treeview, column);
99}
100
101bool wxCheckListBox::IsChecked(unsigned int index) const
102{
103    wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid checklistbox") );
104
105    GtkTreeIter iter;
106    gboolean res = gtk_tree_model_iter_nth_child(
107                        GTK_TREE_MODEL(m_liststore),
108                        &iter, NULL, //NULL = parent = get first
109                        index
110                   );
111    if(!res)
112        return false;
113
114    GValue value = {0, };
115    gtk_tree_model_get_value(GTK_TREE_MODEL(m_liststore),
116                             &iter,
117                             0, //column
118                             &value);
119
120    return g_value_get_boolean(&value) == TRUE ? true : false;
121}
122
123void wxCheckListBox::Check(unsigned int index, bool check)
124{
125    wxCHECK_RET( m_treeview != NULL, wxT("invalid checklistbox") );
126
127    GtkTreeIter iter;
128    gboolean res = gtk_tree_model_iter_nth_child(
129                        GTK_TREE_MODEL(m_liststore),
130                        &iter, NULL, //NULL = parent = get first
131                        index
132                   );
133    if(!res)
134        return;
135
136    gtk_list_store_set(m_liststore,
137                       &iter,
138                       0, //column
139                       check ? TRUE : FALSE, -1);
140}
141
142int wxCheckListBox::GetItemHeight() const
143{
144    wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox"));
145
146    gint height;
147    gtk_tree_view_column_cell_get_size(
148        gtk_tree_view_get_column(m_treeview, 0),
149                                       NULL, NULL, NULL, NULL,
150                                       &height);
151    return height;
152}
153
154#endif //wxUSE_CHECKLISTBOX
155