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