]>
Commit | Line | Data |
---|---|---|
790b137e | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/gtk/dataview.cpp |
790b137e RR |
3 | // Purpose: wxDataViewCtrl GTK+2 implementation |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
790b137e RR |
13 | #if wxUSE_DATAVIEWCTRL |
14 | ||
15 | #include "wx/dataview.h" | |
4ed7af08 RR |
16 | |
17 | #ifndef wxUSE_GENERICDATAVIEWCTRL | |
18 | ||
e4db172a WS |
19 | #ifndef WX_PRECOMP |
20 | #include "wx/log.h" | |
ed4b0fdc | 21 | #include "wx/dcclient.h" |
ed2fbeb8 | 22 | #include "wx/sizer.h" |
e4db172a WS |
23 | #endif |
24 | ||
790b137e | 25 | #include "wx/stockitem.h" |
7ea3a0de RR |
26 | #include "wx/calctrl.h" |
27 | #include "wx/popupwin.h" | |
2586d4a1 RR |
28 | #include "wx/icon.h" |
29 | ||
790b137e RR |
30 | |
31 | #include "wx/gtk/private.h" | |
32 | #include "wx/gtk/win_gtk.h" | |
33 | ||
34 | #include <gobject/gvaluecollector.h> | |
1557c77b RR |
35 | #include <gtk/gtktreemodel.h> |
36 | #include <gtk/gtktreednd.h> | |
790b137e | 37 | |
4d496ecb RR |
38 | #include <gdk/gdkkeysyms.h> |
39 | ||
790b137e RR |
40 | //----------------------------------------------------------------------------- |
41 | // classes | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | class wxDataViewCtrl; | |
45 | ||
790b137e RR |
46 | //----------------------------------------------------------------------------- |
47 | // data | |
48 | //----------------------------------------------------------------------------- | |
49 | ||
50 | extern bool g_blockEventsOnDrag; | |
51 | ||
52 | //----------------------------------------------------------------------------- | |
53 | // define new GTK+ class wxGtkListStore | |
54 | //----------------------------------------------------------------------------- | |
55 | ||
56 | extern "C" { | |
57 | ||
58 | #define GTK_TYPE_WX_LIST_STORE (gtk_wx_list_store_get_type ()) | |
59 | #define GTK_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStore)) | |
60 | #define GTK_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass)) | |
61 | #define GTK_IS_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_LIST_STORE)) | |
62 | #define GTK_IS_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_LIST_STORE)) | |
63 | #define GTK_WX_LIST_STORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass)) | |
64 | ||
65 | GType gtk_wx_list_store_get_type (void); | |
66 | ||
67 | typedef struct _GtkWxListStore GtkWxListStore; | |
68 | typedef struct _GtkWxListStoreClass GtkWxListStoreClass; | |
69 | ||
70 | struct _GtkWxListStore | |
71 | { | |
72 | GObject parent; | |
73 | ||
74 | /*< private >*/ | |
75 | gint stamp; | |
76 | wxDataViewListModel *model; | |
77 | }; | |
78 | ||
79 | struct _GtkWxListStoreClass | |
80 | { | |
e152afc3 | 81 | GObjectClass list_parent_class; |
790b137e RR |
82 | }; |
83 | ||
e152afc3 | 84 | static GtkWxListStore *wxgtk_list_store_new (void); |
790b137e | 85 | static void wxgtk_list_store_init (GtkWxListStore *list_store); |
1557c77b | 86 | static void wxgtk_list_store_class_init (GtkWxListStoreClass *klass); |
790b137e RR |
87 | static void wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface); |
88 | static void wxgtk_list_store_finalize (GObject *object); | |
89 | static GtkTreeModelFlags wxgtk_list_store_get_flags (GtkTreeModel *tree_model); | |
90 | static gint wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model); | |
91 | static GType wxgtk_list_store_get_column_type (GtkTreeModel *tree_model, | |
93763ad5 | 92 | gint index); |
790b137e | 93 | static gboolean wxgtk_list_store_get_iter (GtkTreeModel *tree_model, |
93763ad5 WS |
94 | GtkTreeIter *iter, |
95 | GtkTreePath *path); | |
790b137e | 96 | static GtkTreePath *wxgtk_list_store_get_path (GtkTreeModel *tree_model, |
93763ad5 | 97 | GtkTreeIter *iter); |
790b137e | 98 | static void wxgtk_list_store_get_value (GtkTreeModel *tree_model, |
93763ad5 WS |
99 | GtkTreeIter *iter, |
100 | gint column, | |
101 | GValue *value); | |
790b137e | 102 | static gboolean wxgtk_list_store_iter_next (GtkTreeModel *tree_model, |
93763ad5 | 103 | GtkTreeIter *iter); |
790b137e | 104 | static gboolean wxgtk_list_store_iter_children (GtkTreeModel *tree_model, |
93763ad5 WS |
105 | GtkTreeIter *iter, |
106 | GtkTreeIter *parent); | |
790b137e | 107 | static gboolean wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model, |
93763ad5 | 108 | GtkTreeIter *iter); |
790b137e | 109 | static gint wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model, |
93763ad5 | 110 | GtkTreeIter *iter); |
790b137e | 111 | static gboolean wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model, |
93763ad5 WS |
112 | GtkTreeIter *iter, |
113 | GtkTreeIter *parent, | |
114 | gint n); | |
790b137e | 115 | static gboolean wxgtk_list_store_iter_parent (GtkTreeModel *tree_model, |
93763ad5 WS |
116 | GtkTreeIter *iter, |
117 | GtkTreeIter *child); | |
790b137e | 118 | |
e152afc3 | 119 | static GObjectClass *list_parent_class = NULL; |
790b137e RR |
120 | |
121 | GType | |
111f83c4 | 122 | gtk_wx_list_store_get_type (void) |
790b137e | 123 | { |
93763ad5 | 124 | static GType list_store_type = 0; |
790b137e | 125 | |
93763ad5 | 126 | if (!list_store_type) |
790b137e | 127 | { |
de4a74e2 | 128 | const GTypeInfo list_store_info = |
93763ad5 WS |
129 | { |
130 | sizeof (GtkWxListStoreClass), | |
131 | NULL, /* base_init */ | |
132 | NULL, /* base_finalize */ | |
133 | (GClassInitFunc) wxgtk_list_store_class_init, | |
134 | NULL, /* class_finalize */ | |
135 | NULL, /* class_data */ | |
136 | sizeof (GtkWxListStore), | |
137 | 0, | |
138 | (GInstanceInitFunc) wxgtk_list_store_init, | |
139 | }; | |
790b137e RR |
140 | |
141 | static const GInterfaceInfo tree_model_info = | |
142 | { | |
93763ad5 WS |
143 | (GInterfaceInitFunc) wxgtk_list_store_tree_model_init, |
144 | NULL, | |
145 | NULL | |
790b137e RR |
146 | }; |
147 | ||
148 | list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxListStore", | |
93763ad5 | 149 | &list_store_info, (GTypeFlags)0 ); |
790b137e RR |
150 | |
151 | g_type_add_interface_static (list_store_type, | |
93763ad5 WS |
152 | GTK_TYPE_TREE_MODEL, |
153 | &tree_model_info); | |
790b137e RR |
154 | } |
155 | ||
156 | return list_store_type; | |
157 | } | |
158 | ||
1557c77b | 159 | static GtkWxListStore * |
e152afc3 | 160 | wxgtk_list_store_new(void) |
1557c77b | 161 | { |
e152afc3 RR |
162 | GtkWxListStore *retval = (GtkWxListStore *) g_object_new (GTK_TYPE_WX_LIST_STORE, NULL); |
163 | return retval; | |
1557c77b RR |
164 | } |
165 | ||
790b137e | 166 | static void |
1557c77b | 167 | wxgtk_list_store_class_init (GtkWxListStoreClass *klass) |
790b137e | 168 | { |
e152afc3 RR |
169 | list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); |
170 | GObjectClass *object_class = (GObjectClass*) klass; | |
790b137e RR |
171 | object_class->finalize = wxgtk_list_store_finalize; |
172 | } | |
173 | ||
174 | static void | |
175 | wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface) | |
176 | { | |
177 | iface->get_flags = wxgtk_list_store_get_flags; | |
178 | iface->get_n_columns = wxgtk_list_store_get_n_columns; | |
179 | iface->get_column_type = wxgtk_list_store_get_column_type; | |
180 | iface->get_iter = wxgtk_list_store_get_iter; | |
181 | iface->get_path = wxgtk_list_store_get_path; | |
182 | iface->get_value = wxgtk_list_store_get_value; | |
183 | iface->iter_next = wxgtk_list_store_iter_next; | |
184 | iface->iter_children = wxgtk_list_store_iter_children; | |
185 | iface->iter_has_child = wxgtk_list_store_iter_has_child; | |
186 | iface->iter_n_children = wxgtk_list_store_iter_n_children; | |
187 | iface->iter_nth_child = wxgtk_list_store_iter_nth_child; | |
188 | iface->iter_parent = wxgtk_list_store_iter_parent; | |
189 | } | |
190 | ||
191 | static void | |
192 | wxgtk_list_store_init (GtkWxListStore *list_store) | |
193 | { | |
194 | list_store->model = NULL; | |
1557c77b | 195 | list_store->stamp = g_random_int(); |
790b137e RR |
196 | } |
197 | ||
198 | static void | |
199 | wxgtk_list_store_finalize (GObject *object) | |
200 | { | |
1557c77b | 201 | /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */ |
790b137e RR |
202 | |
203 | /* we need to sort out, which class deletes what */ | |
111f83c4 | 204 | /* delete list_store->model; */ |
790b137e RR |
205 | |
206 | /* must chain up */ | |
e152afc3 | 207 | (* list_parent_class->finalize) (object); |
790b137e | 208 | } |
93763ad5 | 209 | |
790b137e RR |
210 | } // extern "C" |
211 | ||
212 | //----------------------------------------------------------------------------- | |
213 | // implement callbacks from wxGtkListStore class by letting | |
214 | // them call the methods of wxWidgets' wxDataViewListModel | |
215 | //----------------------------------------------------------------------------- | |
216 | ||
217 | static GtkTreeModelFlags | |
218 | wxgtk_list_store_get_flags (GtkTreeModel *tree_model) | |
219 | { | |
1557c77b | 220 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), (GtkTreeModelFlags)0 ); |
790b137e | 221 | |
67cbdfc1 RR |
222 | // GTK+ list store uses a linked list for storing the |
223 | // items and a pointer to a child is used as the member | |
224 | // field of a GtkTreeIter. This means that the iter is | |
225 | // valid in the GtkListStore as long as the child exists. | |
226 | // We use the index of the row and since the index of a | |
227 | // specific row will change if a row above is deleted, | |
228 | // the iter does not persist | |
229 | return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY; | |
790b137e RR |
230 | } |
231 | ||
232 | static gint | |
233 | wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model) | |
234 | { | |
235 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
236 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), 0); | |
237 | ||
9861f022 | 238 | return list_store->model->GetColumnCount(); |
790b137e RR |
239 | } |
240 | ||
241 | static GType | |
242 | wxgtk_list_store_get_column_type (GtkTreeModel *tree_model, | |
93763ad5 | 243 | gint index) |
790b137e RR |
244 | { |
245 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
246 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), G_TYPE_INVALID); | |
247 | ||
1557c77b | 248 | GType gtype = G_TYPE_INVALID; |
93763ad5 | 249 | |
9861f022 | 250 | wxString wxtype = list_store->model->GetColumnType( (unsigned int) index ); |
93763ad5 | 251 | |
1557c77b RR |
252 | if (wxtype == wxT("string")) |
253 | gtype = G_TYPE_STRING; | |
72a3ac9b VZ |
254 | else |
255 | { | |
256 | wxFAIL_MSG( _T("non-string columns not supported yet") ); | |
257 | } | |
790b137e RR |
258 | |
259 | return gtype; | |
260 | } | |
261 | ||
262 | static gboolean | |
263 | wxgtk_list_store_get_iter (GtkTreeModel *tree_model, | |
93763ad5 WS |
264 | GtkTreeIter *iter, |
265 | GtkTreePath *path) | |
790b137e RR |
266 | { |
267 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
268 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); | |
269 | g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE); | |
270 | ||
0a71f9e9 | 271 | unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0]; |
790b137e | 272 | |
9861f022 | 273 | if (i >= list_store->model->GetRowCount()) |
239eaa41 | 274 | return FALSE; |
790b137e | 275 | |
239eaa41 RR |
276 | iter->stamp = list_store->stamp; |
277 | // user_data is just the index | |
278 | iter->user_data = (gpointer) i; | |
790b137e RR |
279 | |
280 | return TRUE; | |
281 | } | |
282 | ||
283 | static GtkTreePath * | |
284 | wxgtk_list_store_get_path (GtkTreeModel *tree_model, | |
93763ad5 | 285 | GtkTreeIter *iter) |
790b137e | 286 | { |
790b137e | 287 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), NULL); |
239eaa41 | 288 | g_return_val_if_fail (iter->stamp == GTK_WX_LIST_STORE (tree_model)->stamp, NULL); |
93763ad5 | 289 | |
239eaa41 RR |
290 | GtkTreePath *retval = gtk_tree_path_new (); |
291 | // user_data is just the index | |
7a9f5d5a | 292 | int i = (wxUIntPtr) iter->user_data; |
239eaa41 RR |
293 | gtk_tree_path_append_index (retval, i); |
294 | return retval; | |
790b137e RR |
295 | } |
296 | ||
297 | static void | |
298 | wxgtk_list_store_get_value (GtkTreeModel *tree_model, | |
93763ad5 WS |
299 | GtkTreeIter *iter, |
300 | gint column, | |
301 | GValue *value) | |
790b137e | 302 | { |
1557c77b | 303 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
790b137e | 304 | g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model) ); |
239eaa41 | 305 | |
1557c77b | 306 | wxDataViewListModel *model = list_store->model; |
9861f022 | 307 | wxString mtype = model->GetColumnType( (unsigned int) column ); |
1557c77b RR |
308 | if (mtype == wxT("string")) |
309 | { | |
3f3af7e7 | 310 | wxVariant variant; |
1557c77b | 311 | g_value_init( value, G_TYPE_STRING ); |
72a3ac9b VZ |
312 | model->GetValue( variant, |
313 | (unsigned int) column, | |
314 | (unsigned int) iter->user_data ); | |
315 | ||
316 | // FIXME: we should support different encodings here | |
317 | g_value_set_string( value, wxGTK_CONV_SYS(variant.GetString()) ); | |
1557c77b RR |
318 | } |
319 | else | |
320 | { | |
72a3ac9b | 321 | wxFAIL_MSG( _T("non-string columns not supported yet") ); |
1557c77b | 322 | } |
790b137e RR |
323 | } |
324 | ||
325 | static gboolean | |
326 | wxgtk_list_store_iter_next (GtkTreeModel *tree_model, | |
93763ad5 | 327 | GtkTreeIter *iter) |
790b137e | 328 | { |
790b137e | 329 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 330 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
93763ad5 | 331 | |
67cbdfc1 | 332 | g_return_val_if_fail (list_store->stamp == iter->stamp, FALSE); |
790b137e | 333 | |
7a9f5d5a | 334 | int n = (wxUIntPtr) iter->user_data; |
93763ad5 | 335 | |
67cbdfc1 RR |
336 | if (n == -1) |
337 | return FALSE; | |
93763ad5 | 338 | |
9861f022 | 339 | if (n >= (int) list_store->model->GetRowCount()-1) |
67cbdfc1 | 340 | return FALSE; |
93763ad5 | 341 | |
111f83c4 | 342 | iter->user_data = (gpointer) ++n; |
790b137e | 343 | |
67cbdfc1 | 344 | return TRUE; |
790b137e RR |
345 | } |
346 | ||
347 | static gboolean | |
348 | wxgtk_list_store_iter_children (GtkTreeModel *tree_model, | |
93763ad5 WS |
349 | GtkTreeIter *iter, |
350 | GtkTreeIter *parent) | |
790b137e | 351 | { |
67cbdfc1 | 352 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 353 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
93763ad5 | 354 | |
67cbdfc1 | 355 | // this is a list, nodes have no children |
790b137e RR |
356 | if (parent) |
357 | return FALSE; | |
358 | ||
67cbdfc1 RR |
359 | iter->stamp = list_store->stamp; |
360 | iter->user_data = (gpointer) -1; | |
93763ad5 | 361 | |
67cbdfc1 | 362 | return TRUE; |
790b137e RR |
363 | } |
364 | ||
365 | static gboolean | |
366 | wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model, | |
93763ad5 | 367 | GtkTreeIter *iter) |
790b137e RR |
368 | { |
369 | return FALSE; | |
370 | } | |
371 | ||
372 | static gint | |
373 | wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model, | |
93763ad5 | 374 | GtkTreeIter *iter) |
790b137e RR |
375 | { |
376 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), -1); | |
1557c77b | 377 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
93763ad5 | 378 | |
790b137e | 379 | if (iter == NULL) |
9861f022 | 380 | return (gint) list_store->model->GetRowCount(); |
790b137e | 381 | |
67cbdfc1 | 382 | g_return_val_if_fail (list_store->stamp == iter->stamp, -1); |
93763ad5 | 383 | |
790b137e RR |
384 | return 0; |
385 | } | |
386 | ||
387 | static gboolean | |
388 | wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model, | |
93763ad5 WS |
389 | GtkTreeIter *iter, |
390 | GtkTreeIter *parent, | |
391 | gint n) | |
790b137e | 392 | { |
67cbdfc1 | 393 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 394 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
93763ad5 | 395 | |
67cbdfc1 RR |
396 | if (parent) |
397 | return FALSE; | |
790b137e | 398 | |
67cbdfc1 RR |
399 | if (n < 0) |
400 | return FALSE; | |
93763ad5 | 401 | |
9861f022 | 402 | if (n >= (gint) list_store->model->GetRowCount()) |
67cbdfc1 | 403 | return FALSE; |
790b137e | 404 | |
67cbdfc1 RR |
405 | iter->stamp = list_store->stamp; |
406 | iter->user_data = (gpointer) n; | |
93763ad5 | 407 | |
67cbdfc1 | 408 | return TRUE; |
790b137e RR |
409 | } |
410 | ||
411 | static gboolean | |
412 | wxgtk_list_store_iter_parent (GtkTreeModel *tree_model, | |
93763ad5 WS |
413 | GtkTreeIter *iter, |
414 | GtkTreeIter *child) | |
790b137e RR |
415 | { |
416 | return FALSE; | |
417 | } | |
418 | ||
e152afc3 | 419 | //----------------------------------------------------------------------------- |
baa9ebc4 | 420 | // define new GTK+ class wxGtkRendererRenderer |
e152afc3 RR |
421 | //----------------------------------------------------------------------------- |
422 | ||
423 | extern "C" { | |
424 | ||
425 | #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ()) | |
426 | #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer)) | |
427 | #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass)) | |
428 | #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER)) | |
429 | #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER)) | |
430 | #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass)) | |
431 | ||
432 | GType gtk_wx_cell_renderer_get_type (void); | |
433 | ||
434 | typedef struct _GtkWxCellRenderer GtkWxCellRenderer; | |
435 | typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass; | |
436 | ||
437 | struct _GtkWxCellRenderer | |
438 | { | |
439 | GtkCellRenderer parent; | |
440 | ||
441 | /*< private >*/ | |
baa9ebc4 | 442 | wxDataViewCustomRenderer *cell; |
4d496ecb | 443 | guint32 last_click; |
e152afc3 RR |
444 | }; |
445 | ||
446 | struct _GtkWxCellRendererClass | |
447 | { | |
448 | GtkCellRendererClass cell_parent_class; | |
e152afc3 RR |
449 | }; |
450 | ||
451 | ||
452 | static GtkCellRenderer *gtk_wx_cell_renderer_new (void); | |
553f7d8f RR |
453 | static void gtk_wx_cell_renderer_init ( |
454 | GtkWxCellRenderer *cell ); | |
455 | static void gtk_wx_cell_renderer_class_init( | |
456 | GtkWxCellRendererClass *klass ); | |
457 | static void gtk_wx_cell_renderer_finalize ( | |
458 | GObject *object ); | |
459 | static void gtk_wx_cell_renderer_get_size ( | |
460 | GtkCellRenderer *cell, | |
93763ad5 WS |
461 | GtkWidget *widget, |
462 | GdkRectangle *rectangle, | |
463 | gint *x_offset, | |
464 | gint *y_offset, | |
465 | gint *width, | |
466 | gint *height ); | |
553f7d8f RR |
467 | static void gtk_wx_cell_renderer_render ( |
468 | GtkCellRenderer *cell, | |
93763ad5 WS |
469 | GdkWindow *window, |
470 | GtkWidget *widget, | |
471 | GdkRectangle *background_area, | |
472 | GdkRectangle *cell_area, | |
473 | GdkRectangle *expose_area, | |
474 | GtkCellRendererState flags ); | |
553f7d8f RR |
475 | static gboolean gtk_wx_cell_renderer_activate( |
476 | GtkCellRenderer *cell, | |
477 | GdkEvent *event, | |
478 | GtkWidget *widget, | |
479 | const gchar *path, | |
480 | GdkRectangle *background_area, | |
481 | GdkRectangle *cell_area, | |
482 | GtkCellRendererState flags ); | |
e152afc3 RR |
483 | |
484 | static GObjectClass *cell_parent_class = NULL; | |
485 | ||
486 | } // extern "C" | |
487 | ||
93763ad5 | 488 | GType |
e152afc3 RR |
489 | gtk_wx_cell_renderer_get_type (void) |
490 | { | |
553f7d8f | 491 | static GType cell_wx_type = 0; |
e152afc3 | 492 | |
553f7d8f | 493 | if (!cell_wx_type) |
e152afc3 | 494 | { |
de4a74e2 | 495 | const GTypeInfo cell_wx_info = |
553f7d8f RR |
496 | { |
497 | sizeof (GtkWxCellRendererClass), | |
93763ad5 WS |
498 | NULL, /* base_init */ |
499 | NULL, /* base_finalize */ | |
553f7d8f | 500 | (GClassInitFunc) gtk_wx_cell_renderer_class_init, |
93763ad5 WS |
501 | NULL, /* class_finalize */ |
502 | NULL, /* class_data */ | |
553f7d8f RR |
503 | sizeof (GtkWxCellRenderer), |
504 | 0, /* n_preallocs */ | |
505 | (GInstanceInitFunc) gtk_wx_cell_renderer_init, | |
506 | }; | |
507 | ||
93763ad5 | 508 | cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER, |
553f7d8f | 509 | "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 ); |
e152afc3 RR |
510 | } |
511 | ||
553f7d8f | 512 | return cell_wx_type; |
e152afc3 RR |
513 | } |
514 | ||
515 | static void | |
516 | gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell) | |
517 | { | |
518 | cell->cell = NULL; | |
4d496ecb | 519 | cell->last_click = 0; |
e152afc3 RR |
520 | } |
521 | ||
522 | static void | |
523 | gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass) | |
524 | { | |
525 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
526 | GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass); | |
527 | ||
528 | cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); | |
529 | ||
530 | object_class->finalize = gtk_wx_cell_renderer_finalize; | |
531 | ||
532 | cell_class->get_size = gtk_wx_cell_renderer_get_size; | |
533 | cell_class->render = gtk_wx_cell_renderer_render; | |
553f7d8f | 534 | cell_class->activate = gtk_wx_cell_renderer_activate; |
e152afc3 RR |
535 | } |
536 | ||
537 | static void | |
538 | gtk_wx_cell_renderer_finalize (GObject *object) | |
539 | { | |
540 | /* must chain up */ | |
541 | (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object); | |
542 | } | |
543 | ||
544 | GtkCellRenderer* | |
545 | gtk_wx_cell_renderer_new (void) | |
546 | { | |
547 | return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL); | |
548 | } | |
549 | ||
550 | static void | |
551 | gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer, | |
93763ad5 WS |
552 | GtkWidget *widget, |
553 | GdkRectangle *cell_area, | |
554 | gint *x_offset, | |
555 | gint *y_offset, | |
556 | gint *width, | |
557 | gint *height) | |
e152afc3 RR |
558 | { |
559 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
baa9ebc4 | 560 | wxDataViewCustomRenderer *cell = wxrenderer->cell; |
93763ad5 | 561 | |
e152afc3 RR |
562 | wxSize size = cell->GetSize(); |
563 | ||
564 | gint calc_width = (gint) renderer->xpad * 2 + size.x; | |
565 | gint calc_height = (gint) renderer->ypad * 2 + size.y; | |
93763ad5 WS |
566 | |
567 | if (x_offset) | |
e152afc3 | 568 | *x_offset = 0; |
93763ad5 | 569 | if (y_offset) |
e152afc3 RR |
570 | *y_offset = 0; |
571 | ||
572 | if (cell_area && size.x > 0 && size.y > 0) | |
573 | { | |
574 | if (x_offset) | |
93763ad5 | 575 | { |
e152afc3 RR |
576 | *x_offset = (gint)((renderer->xalign * |
577 | (cell_area->width - calc_width - 2 * renderer->xpad))); | |
578 | *x_offset = MAX (*x_offset, 0) + renderer->xpad; | |
93763ad5 | 579 | } |
e152afc3 RR |
580 | if (y_offset) |
581 | { | |
582 | *y_offset = (gint)((renderer->yalign * | |
583 | (cell_area->height - calc_height - 2 * renderer->ypad))); | |
584 | *y_offset = MAX (*y_offset, 0) + renderer->ypad; | |
585 | } | |
586 | } | |
587 | ||
588 | if (width) | |
589 | *width = calc_width; | |
93763ad5 | 590 | |
e152afc3 RR |
591 | if (height) |
592 | *height = calc_height; | |
593 | } | |
594 | ||
595 | static void | |
596 | gtk_wx_cell_renderer_render (GtkCellRenderer *renderer, | |
93763ad5 WS |
597 | GdkWindow *window, |
598 | GtkWidget *widget, | |
599 | GdkRectangle *background_area, | |
600 | GdkRectangle *cell_area, | |
601 | GdkRectangle *expose_area, | |
602 | GtkCellRendererState flags) | |
e152afc3 RR |
603 | |
604 | { | |
605 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
baa9ebc4 | 606 | wxDataViewCustomRenderer *cell = wxrenderer->cell; |
93763ad5 | 607 | |
e152afc3 RR |
608 | GdkRectangle rect; |
609 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
93763ad5 WS |
610 | &rect.x, |
611 | &rect.y, | |
612 | &rect.width, | |
613 | &rect.height); | |
e152afc3 RR |
614 | |
615 | rect.x += cell_area->x; | |
616 | rect.y += cell_area->y; | |
617 | rect.width -= renderer->xpad * 2; | |
618 | rect.height -= renderer->ypad * 2; | |
93763ad5 | 619 | |
e152afc3 RR |
620 | GdkRectangle dummy; |
621 | if (gdk_rectangle_intersect (expose_area, &rect, &dummy)) | |
622 | { | |
623 | wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); | |
624 | wxWindowDC* dc = (wxWindowDC*) cell->GetDC(); | |
4d496ecb RR |
625 | if (dc->m_window == NULL) |
626 | { | |
627 | dc->m_window = window; | |
628 | dc->SetUpDC(); | |
629 | } | |
93763ad5 | 630 | |
e152afc3 RR |
631 | int state = 0; |
632 | if (flags & GTK_CELL_RENDERER_SELECTED) | |
633 | state |= wxDATAVIEW_CELL_SELECTED; | |
634 | if (flags & GTK_CELL_RENDERER_PRELIT) | |
635 | state |= wxDATAVIEW_CELL_PRELIT; | |
636 | if (flags & GTK_CELL_RENDERER_INSENSITIVE) | |
637 | state |= wxDATAVIEW_CELL_INSENSITIVE; | |
638 | if (flags & GTK_CELL_RENDERER_INSENSITIVE) | |
639 | state |= wxDATAVIEW_CELL_INSENSITIVE; | |
640 | if (flags & GTK_CELL_RENDERER_FOCUSED) | |
641 | state |= wxDATAVIEW_CELL_FOCUSED; | |
642 | cell->Render( renderrect, dc, state ); | |
93763ad5 | 643 | } |
e152afc3 RR |
644 | } |
645 | ||
93763ad5 | 646 | static gboolean |
553f7d8f RR |
647 | gtk_wx_cell_renderer_activate( |
648 | GtkCellRenderer *renderer, | |
649 | GdkEvent *event, | |
650 | GtkWidget *widget, | |
651 | const gchar *path, | |
652 | GdkRectangle *background_area, | |
653 | GdkRectangle *cell_area, | |
654 | GtkCellRendererState flags ) | |
655 | { | |
656 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
baa9ebc4 | 657 | wxDataViewCustomRenderer *cell = wxrenderer->cell; |
93763ad5 | 658 | |
553f7d8f RR |
659 | GdkRectangle rect; |
660 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
93763ad5 WS |
661 | &rect.x, |
662 | &rect.y, | |
663 | &rect.width, | |
664 | &rect.height); | |
553f7d8f RR |
665 | |
666 | rect.x += cell_area->x; | |
667 | rect.y += cell_area->y; | |
668 | rect.width -= renderer->xpad * 2; | |
669 | rect.height -= renderer->ypad * 2; | |
93763ad5 | 670 | |
553f7d8f | 671 | wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); |
93763ad5 | 672 | |
4d496ecb | 673 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); |
93763ad5 | 674 | |
4d496ecb | 675 | GtkTreePath *treepath = gtk_tree_path_new_from_string( path ); |
0a71f9e9 | 676 | unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (treepath)[0]; |
4d496ecb | 677 | gtk_tree_path_free( treepath ); |
93763ad5 | 678 | |
0a71f9e9 | 679 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); |
93763ad5 | 680 | |
4d496ecb RR |
681 | if (event->type == GDK_BUTTON_PRESS) |
682 | { | |
683 | GdkEventButton *button_event = (GdkEventButton*) event; | |
93763ad5 | 684 | wxPoint pt( ((int) button_event->x) - renderrect.x, |
4d496ecb | 685 | ((int) button_event->y) - renderrect.y ); |
93763ad5 | 686 | |
4d496ecb RR |
687 | bool ret = false; |
688 | if (button_event->button == 1) | |
689 | { | |
690 | if (cell->LeftClick( pt, renderrect, model, model_col, model_row )) | |
691 | ret = true; | |
7ea3a0de | 692 | // TODO: query system double-click time |
4d496ecb RR |
693 | if (button_event->time - wxrenderer->last_click < 400) |
694 | if (cell->Activate( renderrect, model, model_col, model_row )) | |
695 | ret = true; | |
696 | } | |
697 | if (button_event->button == 3) | |
698 | { | |
699 | if (cell->RightClick( pt, renderrect, model, model_col, model_row )) | |
700 | ret = true; | |
701 | } | |
93763ad5 | 702 | |
4d496ecb | 703 | wxrenderer->last_click = button_event->time; |
93763ad5 | 704 | |
4d496ecb RR |
705 | return ret; |
706 | } | |
93763ad5 | 707 | |
4d496ecb | 708 | return false; |
553f7d8f RR |
709 | } |
710 | ||
93763ad5 | 711 | // --------------------------------------------------------- |
6e2e590f | 712 | // wxGtkDataViewListModelNotifier |
93763ad5 | 713 | // --------------------------------------------------------- |
6e2e590f RR |
714 | |
715 | class wxGtkDataViewListModelNotifier: public wxDataViewListModelNotifier | |
716 | { | |
717 | public: | |
718 | wxGtkDataViewListModelNotifier( GtkWxListStore* gtk_store, wxDataViewListModel *wx_model ); | |
93763ad5 | 719 | |
6e2e590f RR |
720 | virtual bool RowAppended(); |
721 | virtual bool RowPrepended(); | |
0a71f9e9 RR |
722 | virtual bool RowInserted( unsigned int before ); |
723 | virtual bool RowDeleted( unsigned int row ); | |
724 | virtual bool RowChanged( unsigned int row ); | |
725 | virtual bool ValueChanged( unsigned int col, unsigned int row ); | |
726 | virtual bool RowsReordered( unsigned int *new_order ); | |
6e2e590f | 727 | virtual bool Cleared(); |
93763ad5 | 728 | |
87f0efe2 | 729 | virtual bool Freed() |
9861f022 RR |
730 | { |
731 | m_wx_model = NULL; | |
732 | m_gtk_store = NULL; | |
733 | return wxDataViewListModelNotifier::Freed(); | |
734 | } | |
87f0efe2 | 735 | |
6e2e590f RR |
736 | GtkWxListStore *m_gtk_store; |
737 | wxDataViewListModel *m_wx_model; | |
738 | }; | |
739 | ||
93763ad5 | 740 | // --------------------------------------------------------- |
6e2e590f | 741 | // wxGtkDataViewListModelNotifier |
93763ad5 | 742 | // --------------------------------------------------------- |
6e2e590f | 743 | |
93763ad5 | 744 | wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier( |
6e2e590f RR |
745 | GtkWxListStore* gtk_store, wxDataViewListModel *wx_model ) |
746 | { | |
747 | m_gtk_store = gtk_store; | |
748 | m_wx_model = wx_model; | |
749 | } | |
93763ad5 | 750 | |
6e2e590f RR |
751 | bool wxGtkDataViewListModelNotifier::RowAppended() |
752 | { | |
9861f022 | 753 | unsigned int pos = m_wx_model->GetRowCount()-1; |
93763ad5 | 754 | |
6e2e590f RR |
755 | GtkTreeIter iter; |
756 | iter.stamp = m_gtk_store->stamp; | |
757 | iter.user_data = (gpointer) pos; | |
93763ad5 | 758 | |
6e2e590f RR |
759 | GtkTreePath *path = gtk_tree_path_new (); |
760 | gtk_tree_path_append_index (path, (gint) pos); | |
761 | gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
762 | gtk_tree_path_free (path); | |
93763ad5 | 763 | |
6e2e590f RR |
764 | return true; |
765 | } | |
766 | ||
767 | bool wxGtkDataViewListModelNotifier::RowPrepended() | |
768 | { | |
605c2c4a RR |
769 | GtkTreeIter iter; |
770 | iter.stamp = m_gtk_store->stamp; | |
771 | iter.user_data = (gpointer) 0; | |
93763ad5 | 772 | |
605c2c4a RR |
773 | GtkTreePath *path = gtk_tree_path_new (); |
774 | gtk_tree_path_append_index (path, (gint) 0); | |
775 | gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
776 | gtk_tree_path_free (path); | |
93763ad5 | 777 | |
605c2c4a | 778 | return true; |
6e2e590f RR |
779 | } |
780 | ||
0a71f9e9 | 781 | bool wxGtkDataViewListModelNotifier::RowInserted( unsigned int before ) |
6e2e590f | 782 | { |
4627af27 RR |
783 | GtkTreeIter iter; |
784 | iter.stamp = m_gtk_store->stamp; | |
785 | iter.user_data = (gpointer) before; | |
786 | ||
787 | GtkTreePath *path = gtk_tree_path_new (); | |
788 | gtk_tree_path_append_index (path, (gint) before); | |
789 | gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
790 | gtk_tree_path_free (path); | |
791 | ||
792 | return true; | |
6e2e590f RR |
793 | } |
794 | ||
0a71f9e9 | 795 | bool wxGtkDataViewListModelNotifier::RowDeleted( unsigned int row ) |
6e2e590f | 796 | { |
4627af27 RR |
797 | GtkTreePath *path = gtk_tree_path_new (); |
798 | gtk_tree_path_append_index (path, (gint) row); | |
799 | gtk_tree_model_row_deleted (GTK_TREE_MODEL (m_gtk_store), path); | |
800 | gtk_tree_path_free (path); | |
801 | ||
802 | return true; | |
6e2e590f RR |
803 | } |
804 | ||
0a71f9e9 | 805 | bool wxGtkDataViewListModelNotifier::RowChanged( unsigned int row ) |
6e2e590f | 806 | { |
a7f61f76 RR |
807 | GtkTreeIter iter; |
808 | iter.stamp = m_gtk_store->stamp; | |
809 | iter.user_data = (gpointer) row; | |
810 | GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store), &iter); | |
811 | gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
812 | gtk_tree_path_free (path); | |
813 | ||
814 | return true; | |
6e2e590f RR |
815 | } |
816 | ||
0a71f9e9 | 817 | bool wxGtkDataViewListModelNotifier::ValueChanged( unsigned int model_col, unsigned int model_row ) |
6e2e590f | 818 | { |
4eccd3a1 | 819 | // This adds GTK+'s missing MVC logic for ValueChanged |
0a5af992 VZ |
820 | wxObjectList::compatibility_iterator |
821 | node = GetOwner()->m_viewingColumns.GetFirst(); | |
8f850e28 RR |
822 | while (node) |
823 | { | |
824 | wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData(); | |
825 | if (viewing_column->m_modelColumn == model_col) | |
826 | { | |
827 | GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview); | |
828 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(viewing_column->m_viewColumn->GetGtkHandle()); | |
829 | ||
830 | // Get cell area | |
831 | GtkTreePath *path = gtk_tree_path_new(); | |
832 | gtk_tree_path_append_index( path, model_row ); | |
833 | GdkRectangle cell_area; | |
834 | gtk_tree_view_get_cell_area( widget, path, column, &cell_area ); | |
93763ad5 | 835 | gtk_tree_path_free( path ); |
8f850e28 RR |
836 | |
837 | int ydiff = column->button->allocation.height; | |
838 | // Redraw | |
93763ad5 | 839 | gtk_widget_queue_draw_area( GTK_WIDGET(widget), |
8f850e28 RR |
840 | cell_area.x, ydiff + cell_area.y, cell_area.width, cell_area.height ); |
841 | } | |
842 | ||
843 | node = node->GetNext(); | |
844 | } | |
93763ad5 | 845 | |
8f850e28 | 846 | return true; |
6e2e590f RR |
847 | } |
848 | ||
0a71f9e9 | 849 | bool wxGtkDataViewListModelNotifier::RowsReordered( unsigned int *new_order ) |
4eccd3a1 | 850 | { |
0a71f9e9 | 851 | // Assume sizeof(unsigned int)= == sizeof(gint) |
4eccd3a1 RR |
852 | |
853 | GtkTreePath *path = gtk_tree_path_new (); | |
854 | gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store), path, NULL, (gint*)new_order); | |
855 | gtk_tree_path_free (path); | |
856 | ||
857 | // This adds GTK+'s missing MVC logic for RowsReordered | |
0a5af992 VZ |
858 | wxObjectList::compatibility_iterator |
859 | node = GetOwner()->m_viewingColumns.GetFirst(); | |
4eccd3a1 RR |
860 | while (node) |
861 | { | |
862 | wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData(); | |
863 | GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview); | |
864 | // Doesn't work yet... | |
93763ad5 | 865 | gtk_widget_queue_draw( GTK_WIDGET(widget) ); |
4eccd3a1 RR |
866 | |
867 | node = node->GetNext(); | |
868 | } | |
93763ad5 | 869 | |
4eccd3a1 RR |
870 | return true; |
871 | } | |
872 | ||
6e2e590f RR |
873 | bool wxGtkDataViewListModelNotifier::Cleared() |
874 | { | |
875 | return false; | |
876 | } | |
877 | ||
93763ad5 | 878 | // --------------------------------------------------------- |
baa9ebc4 | 879 | // wxDataViewRenderer |
93763ad5 | 880 | // --------------------------------------------------------- |
6842a71a | 881 | |
baa9ebc4 | 882 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase) |
6842a71a | 883 | |
9861f022 RR |
884 | wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode, |
885 | int align ) : | |
886 | wxDataViewRendererBase( varianttype, mode, align ) | |
6842a71a RR |
887 | { |
888 | m_renderer = NULL; | |
9861f022 RR |
889 | |
890 | // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor, | |
891 | // after the m_renderer pointer has been initialized | |
892 | } | |
893 | ||
894 | void wxDataViewRenderer::SetMode( wxDataViewCellMode mode ) | |
895 | { | |
896 | GtkCellRendererMode gtkMode; | |
897 | switch (mode) | |
898 | { | |
899 | case wxDATAVIEW_CELL_INERT: | |
900 | gtkMode = GTK_CELL_RENDERER_MODE_INERT; | |
901 | break; | |
902 | case wxDATAVIEW_CELL_ACTIVATABLE: | |
903 | gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE; | |
904 | break; | |
905 | case wxDATAVIEW_CELL_EDITABLE: | |
906 | gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE; | |
907 | break; | |
908 | } | |
909 | ||
910 | GValue gvalue = { 0, }; | |
911 | g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() ); | |
912 | g_value_set_enum( &gvalue, gtkMode ); | |
913 | g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue ); | |
914 | g_value_unset( &gvalue ); | |
915 | } | |
916 | ||
917 | wxDataViewCellMode wxDataViewRenderer::GetMode() const | |
918 | { | |
919 | wxDataViewCellMode ret; | |
920 | ||
921 | GValue gvalue; | |
922 | g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL); | |
923 | ||
924 | switch (g_value_get_enum(&gvalue)) | |
925 | { | |
926 | case GTK_CELL_RENDERER_MODE_INERT: | |
927 | ret = wxDATAVIEW_CELL_INERT; | |
928 | break; | |
929 | case GTK_CELL_RENDERER_MODE_ACTIVATABLE: | |
930 | ret = wxDATAVIEW_CELL_ACTIVATABLE; | |
931 | break; | |
932 | case GTK_CELL_RENDERER_MODE_EDITABLE: | |
933 | ret = wxDATAVIEW_CELL_EDITABLE; | |
934 | break; | |
935 | } | |
936 | ||
937 | g_value_unset( &gvalue ); | |
938 | ||
939 | return ret; | |
940 | } | |
941 | ||
942 | void wxDataViewRenderer::SetAlignment( int align ) | |
943 | { | |
944 | // horizontal alignment: | |
945 | ||
946 | gfloat xalign = 0.0; | |
947 | if (align & wxALIGN_RIGHT) | |
948 | xalign = 1.0; | |
949 | else if (align & wxALIGN_CENTER_HORIZONTAL) | |
950 | xalign = 0.5; | |
951 | ||
952 | GValue gvalue = { 0, }; | |
953 | g_value_init( &gvalue, G_TYPE_FLOAT ); | |
954 | g_value_set_float( &gvalue, xalign ); | |
955 | g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue ); | |
956 | g_value_unset( &gvalue ); | |
957 | ||
958 | // vertical alignment: | |
959 | ||
960 | gfloat yalign = 0.0; | |
961 | if (align & wxALIGN_BOTTOM) | |
962 | yalign = 1.0; | |
963 | else if (align & wxALIGN_CENTER_VERTICAL) | |
964 | yalign = 0.5; | |
965 | ||
966 | GValue gvalue2 = { 0, }; | |
967 | g_value_init( &gvalue2, G_TYPE_FLOAT ); | |
968 | g_value_set_float( &gvalue2, yalign ); | |
969 | g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 ); | |
970 | g_value_unset( &gvalue2 ); | |
6842a71a RR |
971 | } |
972 | ||
9861f022 RR |
973 | int wxDataViewRenderer::GetAlignment() const |
974 | { | |
975 | int ret = 0; | |
976 | GValue gvalue; | |
977 | ||
978 | // horizontal alignment: | |
979 | ||
980 | g_object_get( G_OBJECT(m_renderer), "xalign", &gvalue, NULL ); | |
981 | float xalign = g_value_get_float( &gvalue ); | |
982 | if (xalign < 0.5) | |
983 | ret |= wxALIGN_LEFT; | |
984 | else if (xalign == 0.5) | |
985 | ret |= wxALIGN_CENTER_HORIZONTAL; | |
986 | else | |
987 | ret |= wxALIGN_RIGHT; | |
988 | g_value_unset( &gvalue ); | |
989 | ||
990 | ||
991 | // vertical alignment: | |
992 | ||
993 | g_object_get( G_OBJECT(m_renderer), "yalign", &gvalue, NULL ); | |
994 | float yalign = g_value_get_float( &gvalue ); | |
995 | if (yalign < 0.5) | |
996 | ret |= wxALIGN_TOP; | |
997 | else if (yalign == 0.5) | |
998 | ret |= wxALIGN_CENTER_VERTICAL; | |
999 | else | |
1000 | ret |= wxALIGN_BOTTOM; | |
1001 | g_value_unset( &gvalue ); | |
1002 | ||
1003 | return ret; | |
1004 | } | |
1005 | ||
1006 | ||
1007 | ||
93763ad5 | 1008 | // --------------------------------------------------------- |
baa9ebc4 | 1009 | // wxDataViewTextRenderer |
93763ad5 | 1010 | // --------------------------------------------------------- |
6842a71a | 1011 | |
a7f61f76 | 1012 | extern "C" { |
93763ad5 | 1013 | static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer, |
a7f61f76 RR |
1014 | gchar *arg1, gchar *arg2, gpointer user_data ); |
1015 | } | |
1016 | ||
93763ad5 | 1017 | static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer, |
a7f61f76 RR |
1018 | gchar *arg1, gchar *arg2, gpointer user_data ) |
1019 | { | |
baa9ebc4 | 1020 | wxDataViewTextRenderer *cell = (wxDataViewTextRenderer*) user_data; |
93763ad5 | 1021 | |
1a74f561 | 1022 | wxString tmp = wxGTK_CONV_BACK_FONT(arg2, cell->GetOwner()->GetOwner()->GetFont()); |
a7f61f76 RR |
1023 | wxVariant value = tmp; |
1024 | if (!cell->Validate( value )) | |
1025 | return; | |
93763ad5 | 1026 | |
a7f61f76 | 1027 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); |
93763ad5 | 1028 | |
a7f61f76 | 1029 | GtkTreePath *path = gtk_tree_path_new_from_string( arg1 ); |
0a71f9e9 | 1030 | unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (path)[0]; |
a7f61f76 | 1031 | gtk_tree_path_free( path ); |
93763ad5 | 1032 | |
0a71f9e9 | 1033 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); |
93763ad5 | 1034 | |
a7f61f76 RR |
1035 | model->SetValue( value, model_col, model_row ); |
1036 | model->ValueChanged( model_col, model_row ); | |
1037 | } | |
1038 | ||
baa9ebc4 | 1039 | IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer) |
6842a71a | 1040 | |
9861f022 RR |
1041 | wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode, |
1042 | int align ) : | |
1043 | wxDataViewRenderer( varianttype, mode, align ) | |
6842a71a | 1044 | { |
9861f022 | 1045 | m_renderer = (GtkWidget*) gtk_cell_renderer_text_new(); |
93763ad5 | 1046 | |
9861f022 | 1047 | if (mode & wxDATAVIEW_CELL_EDITABLE) |
a7f61f76 RR |
1048 | { |
1049 | GValue gvalue = { 0, }; | |
1050 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1051 | g_value_set_boolean( &gvalue, true ); | |
1052 | g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue ); | |
1053 | g_value_unset( &gvalue ); | |
93763ad5 | 1054 | |
a7f61f76 RR |
1055 | g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this ); |
1056 | } | |
9861f022 RR |
1057 | |
1058 | SetMode(mode); | |
1059 | SetAlignment(align); | |
6842a71a | 1060 | } |
790b137e | 1061 | |
baa9ebc4 | 1062 | bool wxDataViewTextRenderer::SetValue( const wxVariant &value ) |
7b4fde82 RR |
1063 | { |
1064 | wxString tmp = value; | |
93763ad5 | 1065 | |
7b4fde82 RR |
1066 | GValue gvalue = { 0, }; |
1067 | g_value_init( &gvalue, G_TYPE_STRING ); | |
b94db696 | 1068 | g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) ); |
7b4fde82 RR |
1069 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); |
1070 | g_value_unset( &gvalue ); | |
93763ad5 | 1071 | |
7b4fde82 RR |
1072 | return true; |
1073 | } | |
1074 | ||
9861f022 | 1075 | bool wxDataViewTextRenderer::GetValue( wxVariant &value ) const |
a7f61f76 RR |
1076 | { |
1077 | GValue gvalue = { 0, }; | |
1078 | g_value_init( &gvalue, G_TYPE_STRING ); | |
1079 | g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
1a74f561 VZ |
1080 | wxString tmp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), |
1081 | wx_const_cast(wxDataViewTextRenderer*, this)->GetOwner()->GetOwner()->GetFont() ); | |
a7f61f76 | 1082 | g_value_unset( &gvalue ); |
93763ad5 | 1083 | |
a7f61f76 RR |
1084 | value = tmp; |
1085 | ||
1086 | return true; | |
1087 | } | |
1088 | ||
9861f022 RR |
1089 | void wxDataViewTextRenderer::SetAlignment( int align ) |
1090 | { | |
1091 | wxDataViewRenderer::SetAlignment(align); | |
1092 | ||
1093 | // horizontal alignment: | |
1094 | ||
1095 | PangoAlignment pangoAlign = PANGO_ALIGN_LEFT; | |
1096 | if (align & wxALIGN_RIGHT) | |
1097 | pangoAlign = PANGO_ALIGN_RIGHT; | |
1098 | else if (align & wxALIGN_CENTER_HORIZONTAL) | |
1099 | pangoAlign = PANGO_ALIGN_CENTER; | |
1100 | ||
1101 | GValue gvalue = { 0, }; | |
1102 | g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() ); | |
1103 | g_value_set_enum( &gvalue, pangoAlign ); | |
1104 | g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue ); | |
1105 | g_value_unset( &gvalue ); | |
1106 | } | |
1107 | ||
f4322df6 | 1108 | // --------------------------------------------------------- |
baa9ebc4 | 1109 | // wxDataViewBitmapRenderer |
f4322df6 | 1110 | // --------------------------------------------------------- |
cbc9145c | 1111 | |
baa9ebc4 | 1112 | IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer) |
cbc9145c | 1113 | |
9861f022 RR |
1114 | wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode, |
1115 | int align ) : | |
1116 | wxDataViewRenderer( varianttype, mode, align ) | |
cbc9145c | 1117 | { |
9861f022 RR |
1118 | m_renderer = (GtkWidget*) gtk_cell_renderer_pixbuf_new(); |
1119 | ||
1120 | SetMode(mode); | |
1121 | SetAlignment(align); | |
cbc9145c RR |
1122 | } |
1123 | ||
baa9ebc4 | 1124 | bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value ) |
cbc9145c RR |
1125 | { |
1126 | if (value.GetType() == wxT("wxBitmap")) | |
1127 | { | |
2586d4a1 RR |
1128 | wxBitmap bitmap; |
1129 | bitmap << value; | |
f4322df6 | 1130 | |
2586d4a1 RR |
1131 | // This may create a Pixbuf representation in the |
1132 | // wxBitmap object (and it will stay there) | |
1133 | GdkPixbuf *pixbuf = bitmap.GetPixbuf(); | |
f4322df6 | 1134 | |
2586d4a1 RR |
1135 | GValue gvalue = { 0, }; |
1136 | g_value_init( &gvalue, G_TYPE_OBJECT ); | |
1137 | g_value_set_object( &gvalue, pixbuf ); | |
1138 | g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue ); | |
1139 | g_value_unset( &gvalue ); | |
f4322df6 | 1140 | |
2586d4a1 RR |
1141 | return true; |
1142 | } | |
f4322df6 | 1143 | |
2586d4a1 RR |
1144 | if (value.GetType() == wxT("wxIcon")) |
1145 | { | |
1146 | wxIcon bitmap; | |
1147 | bitmap << value; | |
f4322df6 | 1148 | |
cbc9145c RR |
1149 | // This may create a Pixbuf representation in the |
1150 | // wxBitmap object (and it will stay there) | |
2586d4a1 | 1151 | GdkPixbuf *pixbuf = bitmap.GetPixbuf(); |
f4322df6 | 1152 | |
cbc9145c RR |
1153 | GValue gvalue = { 0, }; |
1154 | g_value_init( &gvalue, G_TYPE_OBJECT ); | |
1155 | g_value_set_object( &gvalue, pixbuf ); | |
1156 | g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue ); | |
1157 | g_value_unset( &gvalue ); | |
f4322df6 | 1158 | |
cbc9145c RR |
1159 | return true; |
1160 | } | |
f4322df6 | 1161 | |
cbc9145c RR |
1162 | return false; |
1163 | } | |
1164 | ||
9861f022 | 1165 | bool wxDataViewBitmapRenderer::GetValue( wxVariant &value ) const |
cbc9145c RR |
1166 | { |
1167 | return false; | |
1168 | } | |
f4322df6 | 1169 | |
93763ad5 | 1170 | // --------------------------------------------------------- |
baa9ebc4 | 1171 | // wxDataViewToggleRenderer |
93763ad5 | 1172 | // --------------------------------------------------------- |
fa28826d | 1173 | |
605c2c4a | 1174 | extern "C" { |
93763ad5 | 1175 | static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, |
605c2c4a RR |
1176 | gchar *path, gpointer user_data ); |
1177 | } | |
1178 | ||
93763ad5 | 1179 | static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, |
605c2c4a RR |
1180 | gchar *path, gpointer user_data ) |
1181 | { | |
baa9ebc4 | 1182 | wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data; |
605c2c4a | 1183 | |
93763ad5 | 1184 | // get old value |
605c2c4a RR |
1185 | GValue gvalue = { 0, }; |
1186 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1187 | g_object_get_property( G_OBJECT(renderer), "active", &gvalue ); | |
93763ad5 | 1188 | bool tmp = g_value_get_boolean( &gvalue ); |
605c2c4a RR |
1189 | g_value_unset( &gvalue ); |
1190 | // invert it | |
1191 | tmp = !tmp; | |
93763ad5 | 1192 | |
605c2c4a RR |
1193 | wxVariant value = tmp; |
1194 | if (!cell->Validate( value )) | |
1195 | return; | |
93763ad5 | 1196 | |
605c2c4a | 1197 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); |
93763ad5 | 1198 | |
605c2c4a | 1199 | GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path ); |
0a71f9e9 | 1200 | unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (gtk_path)[0]; |
605c2c4a | 1201 | gtk_tree_path_free( gtk_path ); |
93763ad5 | 1202 | |
0a71f9e9 | 1203 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); |
93763ad5 | 1204 | |
605c2c4a RR |
1205 | model->SetValue( value, model_col, model_row ); |
1206 | model->ValueChanged( model_col, model_row ); | |
1207 | } | |
1208 | ||
baa9ebc4 | 1209 | IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer) |
605c2c4a | 1210 | |
baa9ebc4 | 1211 | wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype, |
9861f022 RR |
1212 | wxDataViewCellMode mode, int align ) : |
1213 | wxDataViewRenderer( varianttype, mode, align ) | |
605c2c4a | 1214 | { |
9861f022 | 1215 | m_renderer = (GtkWidget*) gtk_cell_renderer_toggle_new(); |
93763ad5 | 1216 | |
9861f022 | 1217 | if (mode & wxDATAVIEW_CELL_ACTIVATABLE) |
605c2c4a | 1218 | { |
9861f022 RR |
1219 | g_signal_connect_after( m_renderer, "toggled", |
1220 | G_CALLBACK(wxGtkToggleRendererToggledCallback), this ); | |
553f7d8f RR |
1221 | } |
1222 | else | |
1223 | { | |
605c2c4a RR |
1224 | GValue gvalue = { 0, }; |
1225 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
553f7d8f | 1226 | g_value_set_boolean( &gvalue, false ); |
605c2c4a RR |
1227 | g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue ); |
1228 | g_value_unset( &gvalue ); | |
605c2c4a | 1229 | } |
9861f022 RR |
1230 | |
1231 | SetMode(mode); | |
1232 | SetAlignment(align); | |
605c2c4a RR |
1233 | } |
1234 | ||
baa9ebc4 | 1235 | bool wxDataViewToggleRenderer::SetValue( const wxVariant &value ) |
605c2c4a RR |
1236 | { |
1237 | bool tmp = value; | |
93763ad5 | 1238 | |
605c2c4a RR |
1239 | GValue gvalue = { 0, }; |
1240 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1241 | g_value_set_boolean( &gvalue, tmp ); | |
1242 | g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue ); | |
1243 | g_value_unset( &gvalue ); | |
93763ad5 | 1244 | |
605c2c4a RR |
1245 | return true; |
1246 | } | |
1247 | ||
9861f022 | 1248 | bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const |
605c2c4a RR |
1249 | { |
1250 | GValue gvalue = { 0, }; | |
1251 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1252 | g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue ); | |
93763ad5 | 1253 | bool tmp = g_value_get_boolean( &gvalue ); |
605c2c4a | 1254 | g_value_unset( &gvalue ); |
93763ad5 | 1255 | |
605c2c4a RR |
1256 | value = tmp; |
1257 | ||
1258 | return true; | |
1259 | } | |
93763ad5 WS |
1260 | |
1261 | // --------------------------------------------------------- | |
baa9ebc4 | 1262 | // wxDataViewCustomRenderer |
93763ad5 | 1263 | // --------------------------------------------------------- |
e152afc3 RR |
1264 | |
1265 | class wxDataViewCtrlDC: public wxWindowDC | |
1266 | { | |
1267 | public: | |
1268 | wxDataViewCtrlDC( wxDataViewCtrl *window ) | |
1269 | { | |
1a367564 | 1270 | GtkWidget *widget = window->m_treeview; |
e152afc3 RR |
1271 | // Set later |
1272 | m_window = NULL; | |
4d496ecb | 1273 | |
e152afc3 RR |
1274 | m_context = window->GtkGetPangoDefaultContext(); |
1275 | m_layout = pango_layout_new( m_context ); | |
1276 | m_fontdesc = pango_font_description_copy( widget->style->font_desc ); | |
1277 | ||
1278 | m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget ); | |
1279 | ||
4d496ecb RR |
1280 | // Set m_window later |
1281 | // SetUpDC(); | |
1282 | // m_owner = window; | |
e152afc3 RR |
1283 | } |
1284 | }; | |
1285 | ||
93763ad5 | 1286 | // --------------------------------------------------------- |
baa9ebc4 | 1287 | // wxDataViewCustomRenderer |
93763ad5 | 1288 | // --------------------------------------------------------- |
e152afc3 | 1289 | |
baa9ebc4 | 1290 | IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer) |
e152afc3 | 1291 | |
baa9ebc4 | 1292 | wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype, |
9861f022 RR |
1293 | wxDataViewCellMode mode, int align, |
1294 | bool no_init ) : | |
1295 | wxDataViewRenderer( varianttype, mode, align ) | |
e152afc3 RR |
1296 | { |
1297 | m_dc = NULL; | |
93763ad5 | 1298 | |
ad63bf41 RR |
1299 | if (no_init) |
1300 | m_renderer = NULL; | |
1301 | else | |
9861f022 | 1302 | Init(mode, align); |
ad63bf41 RR |
1303 | } |
1304 | ||
9861f022 | 1305 | bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align) |
ad63bf41 | 1306 | { |
e152afc3 RR |
1307 | GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new(); |
1308 | renderer->cell = this; | |
93763ad5 | 1309 | |
9861f022 | 1310 | m_renderer = (GtkWidget*) renderer; |
93763ad5 | 1311 | |
9861f022 RR |
1312 | SetMode(mode); |
1313 | SetAlignment(align); | |
93763ad5 | 1314 | |
ad63bf41 | 1315 | return true; |
e152afc3 RR |
1316 | } |
1317 | ||
baa9ebc4 | 1318 | wxDataViewCustomRenderer::~wxDataViewCustomRenderer() |
e152afc3 RR |
1319 | { |
1320 | if (m_dc) | |
1321 | delete m_dc; | |
1322 | } | |
1323 | ||
baa9ebc4 | 1324 | wxDC *wxDataViewCustomRenderer::GetDC() |
e152afc3 RR |
1325 | { |
1326 | if (m_dc == NULL) | |
4d496ecb RR |
1327 | { |
1328 | if (GetOwner() == NULL) | |
1329 | return NULL; | |
1330 | if (GetOwner()->GetOwner() == NULL) | |
1331 | return NULL; | |
e152afc3 | 1332 | m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() ); |
4d496ecb | 1333 | } |
93763ad5 | 1334 | |
e152afc3 RR |
1335 | return m_dc; |
1336 | } | |
93763ad5 WS |
1337 | |
1338 | // --------------------------------------------------------- | |
baa9ebc4 | 1339 | // wxDataViewProgressRenderer |
93763ad5 | 1340 | // --------------------------------------------------------- |
ad63bf41 | 1341 | |
baa9ebc4 | 1342 | IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer) |
ad63bf41 | 1343 | |
baa9ebc4 | 1344 | wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label, |
9861f022 RR |
1345 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : |
1346 | wxDataViewCustomRenderer( varianttype, mode, align, true ) | |
ad63bf41 RR |
1347 | { |
1348 | m_label = label; | |
1349 | m_value = 0; | |
93763ad5 | 1350 | |
ad63bf41 RR |
1351 | #ifdef __WXGTK26__ |
1352 | if (!gtk_check_version(2,6,0)) | |
1353 | { | |
9861f022 | 1354 | m_renderer = (GtkWidget*) gtk_cell_renderer_progress_new(); |
93763ad5 | 1355 | |
ad63bf41 RR |
1356 | GValue gvalue = { 0, }; |
1357 | g_value_init( &gvalue, G_TYPE_STRING ); | |
09dfa6a0 VZ |
1358 | |
1359 | // FIXME: font encoding support | |
b94db696 | 1360 | g_value_set_string( &gvalue, wxGTK_CONV_SYS(m_label) ); |
ad63bf41 RR |
1361 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); |
1362 | g_value_unset( &gvalue ); | |
9861f022 RR |
1363 | |
1364 | SetMode(mode); | |
1365 | SetAlignment(align); | |
ad63bf41 RR |
1366 | } |
1367 | else | |
1368 | #endif | |
1369 | { | |
1370 | // Use custom cell code | |
9861f022 | 1371 | wxDataViewCustomRenderer::Init(mode, align); |
ad63bf41 RR |
1372 | } |
1373 | } | |
1374 | ||
baa9ebc4 | 1375 | wxDataViewProgressRenderer::~wxDataViewProgressRenderer() |
ad63bf41 RR |
1376 | { |
1377 | } | |
1378 | ||
baa9ebc4 | 1379 | bool wxDataViewProgressRenderer::SetValue( const wxVariant &value ) |
ad63bf41 RR |
1380 | { |
1381 | #ifdef __WXGTK26__ | |
1382 | if (!gtk_check_version(2,6,0)) | |
1383 | { | |
7226118b | 1384 | gint tmp = (long) value; |
ad63bf41 RR |
1385 | GValue gvalue = { 0, }; |
1386 | g_value_init( &gvalue, G_TYPE_INT ); | |
7226118b | 1387 | g_value_set_int( &gvalue, tmp ); |
ad63bf41 RR |
1388 | g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue ); |
1389 | g_value_unset( &gvalue ); | |
1390 | } | |
1391 | else | |
1392 | #endif | |
1393 | { | |
1394 | m_value = (long) value; | |
93763ad5 | 1395 | |
ad63bf41 RR |
1396 | if (m_value < 0) m_value = 0; |
1397 | if (m_value > 100) m_value = 100; | |
1398 | } | |
93763ad5 | 1399 | |
ad63bf41 RR |
1400 | return true; |
1401 | } | |
93763ad5 | 1402 | |
9861f022 RR |
1403 | bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const |
1404 | { | |
1405 | return false; | |
1406 | } | |
1407 | ||
baa9ebc4 | 1408 | bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int state ) |
ad63bf41 RR |
1409 | { |
1410 | double pct = (double)m_value / 100.0; | |
1411 | wxRect bar = cell; | |
1412 | bar.width = (int)(cell.width * pct); | |
1413 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
1414 | dc->SetBrush( *wxBLUE_BRUSH ); | |
1415 | dc->DrawRectangle( bar ); | |
1416 | ||
1417 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); | |
1418 | dc->SetPen( *wxBLACK_PEN ); | |
1419 | dc->DrawRectangle( cell ); | |
93763ad5 | 1420 | |
ad63bf41 RR |
1421 | return true; |
1422 | } | |
1423 | ||
9861f022 | 1424 | wxSize wxDataViewProgressRenderer::GetSize() const |
ad63bf41 RR |
1425 | { |
1426 | return wxSize(40,12); | |
1427 | } | |
93763ad5 WS |
1428 | |
1429 | // --------------------------------------------------------- | |
baa9ebc4 | 1430 | // wxDataViewDateRenderer |
93763ad5 | 1431 | // --------------------------------------------------------- |
4d496ecb | 1432 | |
baa9ebc4 | 1433 | class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow |
7ea3a0de | 1434 | { |
93763ad5 | 1435 | public: |
baa9ebc4 | 1436 | wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value, |
0a71f9e9 | 1437 | wxDataViewListModel *model, unsigned int col, unsigned int row ) : |
7ea3a0de RR |
1438 | wxPopupTransientWindow( parent, wxBORDER_SIMPLE ) |
1439 | { | |
1440 | m_model = model; | |
1441 | m_col = col; | |
1442 | m_row = row; | |
1443 | m_cal = new wxCalendarCtrl( this, -1, *value ); | |
1444 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); | |
1445 | sizer->Add( m_cal, 1, wxGROW ); | |
1446 | SetSizer( sizer ); | |
1447 | sizer->Fit( this ); | |
1448 | } | |
93763ad5 | 1449 | |
7ea3a0de RR |
1450 | virtual void OnDismiss() |
1451 | { | |
1452 | } | |
93763ad5 | 1453 | |
7ea3a0de | 1454 | void OnCalendar( wxCalendarEvent &event ); |
93763ad5 | 1455 | |
7ea3a0de | 1456 | wxCalendarCtrl *m_cal; |
93763ad5 | 1457 | wxDataViewListModel *m_model; |
0a71f9e9 RR |
1458 | unsigned int m_col; |
1459 | unsigned int m_row; | |
93763ad5 | 1460 | |
7ea3a0de RR |
1461 | private: |
1462 | DECLARE_EVENT_TABLE() | |
1463 | }; | |
1464 | ||
baa9ebc4 RR |
1465 | BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow) |
1466 | EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar ) | |
7ea3a0de RR |
1467 | END_EVENT_TABLE() |
1468 | ||
baa9ebc4 | 1469 | void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) |
7ea3a0de RR |
1470 | { |
1471 | wxDateTime date = event.GetDate(); | |
1472 | wxVariant value = date; | |
1473 | m_model->SetValue( value, m_col, m_row ); | |
1474 | m_model->ValueChanged( m_col, m_row ); | |
1475 | DismissAndNotify(); | |
1476 | } | |
1477 | ||
baa9ebc4 | 1478 | IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer) |
4d496ecb | 1479 | |
baa9ebc4 | 1480 | wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype, |
9861f022 RR |
1481 | wxDataViewCellMode mode, int align ) : |
1482 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
4d496ecb | 1483 | { |
9861f022 RR |
1484 | SetMode(mode); |
1485 | SetAlignment(align); | |
4d496ecb | 1486 | } |
93763ad5 | 1487 | |
baa9ebc4 | 1488 | bool wxDataViewDateRenderer::SetValue( const wxVariant &value ) |
4d496ecb RR |
1489 | { |
1490 | m_date = value.GetDateTime(); | |
93763ad5 | 1491 | |
4d496ecb RR |
1492 | return true; |
1493 | } | |
1494 | ||
9861f022 RR |
1495 | bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const |
1496 | { | |
1497 | return false; | |
1498 | } | |
1499 | ||
baa9ebc4 | 1500 | bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state ) |
4d496ecb RR |
1501 | { |
1502 | dc->SetFont( GetOwner()->GetOwner()->GetFont() ); | |
1503 | wxString tmp = m_date.FormatDate(); | |
1504 | dc->DrawText( tmp, cell.x, cell.y ); | |
1505 | ||
1506 | return true; | |
1507 | } | |
1508 | ||
9861f022 | 1509 | wxSize wxDataViewDateRenderer::GetSize() const |
4d496ecb | 1510 | { |
4d496ecb RR |
1511 | wxString tmp = m_date.FormatDate(); |
1512 | wxCoord x,y,d; | |
9861f022 | 1513 | GetView()->GetTextExtent( tmp, &x, &y, &d ); |
4d496ecb RR |
1514 | return wxSize(x,y+d); |
1515 | } | |
1516 | ||
9861f022 RR |
1517 | bool wxDataViewDateRenderer::Activate( wxRect cell, wxDataViewListModel *model, |
1518 | unsigned int col, unsigned int row ) | |
4d496ecb | 1519 | { |
3f3af7e7 RR |
1520 | wxVariant variant; |
1521 | model->GetValue( variant, col, row ); | |
7ea3a0de RR |
1522 | wxDateTime value = variant.GetDateTime(); |
1523 | ||
baa9ebc4 | 1524 | wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient( |
7ea3a0de RR |
1525 | GetOwner()->GetOwner()->GetParent(), &value, model, col, row ); |
1526 | wxPoint pos = wxGetMousePosition(); | |
1527 | popup->Move( pos ); | |
1528 | popup->Layout(); | |
1529 | popup->Popup( popup->m_cal ); | |
4d496ecb RR |
1530 | |
1531 | return true; | |
1532 | } | |
1533 | ||
93763ad5 | 1534 | // --------------------------------------------------------- |
605c2c4a | 1535 | // wxDataViewColumn |
93763ad5 | 1536 | // --------------------------------------------------------- |
7b4fde82 | 1537 | |
31fb32e1 RR |
1538 | |
1539 | static gboolean | |
1540 | gtk_dataview_header_button_press_callback( GtkWidget *widget, | |
1541 | GdkEventButton *gdk_event, | |
1542 | wxDataViewColumn *column ) | |
1543 | { | |
1544 | if (gdk_event->type != GDK_BUTTON_PRESS) | |
1545 | return TRUE; | |
f4322df6 | 1546 | |
31fb32e1 RR |
1547 | if (gdk_event->button == 1) |
1548 | { | |
1549 | wxDataViewCtrl *dv = column->GetOwner(); | |
1550 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() ); | |
1551 | event.SetDataViewColumn( column ); | |
1552 | event.SetModel( dv->GetModel() ); | |
1553 | dv->GetEventHandler()->ProcessEvent( event ); | |
1554 | } | |
f4322df6 | 1555 | |
31fb32e1 RR |
1556 | return TRUE; |
1557 | } | |
1558 | ||
7b4fde82 RR |
1559 | extern "C" { |
1560 | static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, | |
1561 | GtkCellRenderer *cell, | |
1562 | GtkTreeModel *model, | |
1563 | GtkTreeIter *iter, | |
1564 | gpointer data ); | |
1565 | } | |
1566 | ||
1567 | ||
1568 | static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, | |
1569 | GtkCellRenderer *renderer, | |
1570 | GtkTreeModel *model, | |
1571 | GtkTreeIter *iter, | |
1572 | gpointer data ) | |
1573 | { | |
1574 | g_return_if_fail (GTK_IS_WX_LIST_STORE (model)); | |
1575 | GtkWxListStore *list_store = (GtkWxListStore *) model; | |
93763ad5 | 1576 | |
baa9ebc4 | 1577 | wxDataViewRenderer *cell = (wxDataViewRenderer*) data; |
a7f61f76 | 1578 | |
0a71f9e9 | 1579 | unsigned int model_row = (unsigned int) iter->user_data; |
93763ad5 | 1580 | |
3f3af7e7 RR |
1581 | wxVariant value; |
1582 | list_store->model->GetValue( value, cell->GetOwner()->GetModelColumn(), model_row ); | |
7b4fde82 RR |
1583 | |
1584 | if (value.GetType() != cell->GetVariantType()) | |
f4322df6 VZ |
1585 | wxLogError( wxT("Wrong type, required: %s but: %s"), |
1586 | value.GetType().c_str(), | |
cbc9145c | 1587 | cell->GetVariantType().c_str() ); |
93763ad5 | 1588 | |
7b4fde82 RR |
1589 | cell->SetValue( value ); |
1590 | } | |
1591 | ||
cbc9145c | 1592 | IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase) |
fa28826d | 1593 | |
f4322df6 VZ |
1594 | wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell, |
1595 | unsigned int model_column, int width, | |
87f0efe2 RR |
1596 | wxAlignment align, int flags ) : |
1597 | wxDataViewColumnBase( title, cell, model_column, width, align, flags ) | |
fa28826d | 1598 | { |
9861f022 | 1599 | Init( align, flags, width ); |
31fb32e1 | 1600 | |
9861f022 | 1601 | gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column), TRUE ); |
31fb32e1 | 1602 | SetTitle( title ); |
fa28826d RR |
1603 | } |
1604 | ||
f4322df6 VZ |
1605 | wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell, |
1606 | unsigned int model_column, int width, | |
87f0efe2 RR |
1607 | wxAlignment align, int flags ) : |
1608 | wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags ) | |
9861f022 RR |
1609 | { |
1610 | Init( align, flags, width ); | |
1611 | ||
1612 | SetBitmap( bitmap ); | |
1613 | } | |
1614 | ||
1615 | void wxDataViewColumn::Init(wxAlignment align, int flags, int width) | |
07a84e7b | 1616 | { |
31fb32e1 | 1617 | m_isConnected = false; |
07a84e7b | 1618 | |
9861f022 | 1619 | GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle(); |
07a84e7b | 1620 | GtkTreeViewColumn *column = gtk_tree_view_column_new(); |
9861f022 | 1621 | m_column = (GtkWidget*) column; |
07a84e7b | 1622 | |
9861f022 RR |
1623 | SetFlags( flags ); |
1624 | SetAlignment( align ); | |
07a84e7b | 1625 | |
9861f022 RR |
1626 | // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH); |
1627 | // as GTK+ is smart and unless explicitely told, will set the minimal | |
1628 | // width to the title's lenght, which is a better default | |
07a84e7b | 1629 | |
9861f022 RR |
1630 | // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode |
1631 | // that we use for the wxDataViewCtrl | |
1632 | gtk_tree_view_column_set_fixed_width( column, width < 0 ? wxDVC_DEFAULT_WIDTH : width ); | |
b94db696 | 1633 | gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED ); |
07a84e7b | 1634 | |
9861f022 | 1635 | gtk_tree_view_column_pack_end( column, renderer, TRUE ); |
07a84e7b RR |
1636 | |
1637 | gtk_tree_view_column_set_cell_data_func( column, renderer, | |
9861f022 | 1638 | wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL ); |
07a84e7b RR |
1639 | } |
1640 | ||
fa28826d RR |
1641 | wxDataViewColumn::~wxDataViewColumn() |
1642 | { | |
1643 | } | |
1644 | ||
31fb32e1 RR |
1645 | void wxDataViewColumn::OnInternalIdle() |
1646 | { | |
1647 | if (m_isConnected) | |
1648 | return; | |
f4322df6 | 1649 | |
31fb32e1 RR |
1650 | if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview)) |
1651 | { | |
9861f022 | 1652 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
31fb32e1 RR |
1653 | if (column->button) |
1654 | { | |
1655 | g_signal_connect(column->button, "button_press_event", | |
1656 | G_CALLBACK (gtk_dataview_header_button_press_callback), this); | |
f4322df6 | 1657 | |
31fb32e1 RR |
1658 | m_isConnected = true; |
1659 | } | |
1660 | } | |
1661 | } | |
1662 | ||
b94db696 RR |
1663 | void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner ) |
1664 | { | |
1665 | wxDataViewColumnBase::SetOwner( owner ); | |
f4322df6 | 1666 | |
9861f022 | 1667 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1668 | |
b94db696 | 1669 | gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) ); |
b94db696 RR |
1670 | } |
1671 | ||
fa28826d RR |
1672 | void wxDataViewColumn::SetTitle( const wxString &title ) |
1673 | { | |
9861f022 | 1674 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1675 | |
31fb32e1 RR |
1676 | if (m_isConnected) |
1677 | { | |
1678 | // disconnect before column->button gets recreated | |
f4322df6 | 1679 | g_signal_handlers_disconnect_by_func( column->button, |
9861f022 | 1680 | (GtkWidget*) gtk_dataview_header_button_press_callback, this); |
f4322df6 | 1681 | |
31fb32e1 RR |
1682 | m_isConnected = false; |
1683 | } | |
1684 | ||
09dfa6a0 VZ |
1685 | // FIXME: can it really happen that we don't have the owner here?? |
1686 | wxDataViewCtrl *ctrl = GetOwner(); | |
1687 | gtk_tree_view_column_set_title( column, ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont()) | |
1688 | : wxGTK_CONV_SYS(title) ); | |
31fb32e1 | 1689 | |
07a84e7b RR |
1690 | gtk_tree_view_column_set_widget( column, NULL ); |
1691 | } | |
1692 | ||
9861f022 RR |
1693 | wxString wxDataViewColumn::GetTitle() const |
1694 | { | |
1695 | const gchar *str = gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column) ); | |
1696 | return wxConvFileName->cMB2WX(str); | |
1697 | } | |
1698 | ||
07a84e7b RR |
1699 | void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap ) |
1700 | { | |
1701 | wxDataViewColumnBase::SetBitmap( bitmap ); | |
1702 | ||
9861f022 | 1703 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
07a84e7b RR |
1704 | if (bitmap.Ok()) |
1705 | { | |
1706 | GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() ); | |
f4322df6 | 1707 | |
07a84e7b RR |
1708 | GdkBitmap *mask = (GdkBitmap *) NULL; |
1709 | if (bitmap.GetMask()) | |
1710 | mask = bitmap.GetMask()->GetBitmap(); | |
1711 | ||
1712 | if (bitmap.HasPixbuf()) | |
1713 | { | |
1714 | gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image), | |
1715 | bitmap.GetPixbuf()); | |
1716 | } | |
1717 | else | |
1718 | { | |
1719 | gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image), | |
1720 | bitmap.GetPixmap(), mask); | |
1721 | } | |
1722 | gtk_widget_show( GTK_WIDGET(gtk_image) ); | |
f4322df6 | 1723 | |
07a84e7b RR |
1724 | gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) ); |
1725 | } | |
1726 | else | |
1727 | { | |
1728 | gtk_tree_view_column_set_widget( column, NULL ); | |
1729 | } | |
fa28826d RR |
1730 | } |
1731 | ||
9861f022 RR |
1732 | void wxDataViewColumn::SetHidden( bool hidden ) |
1733 | { | |
1734 | gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden ); | |
1735 | } | |
1736 | ||
1737 | void wxDataViewColumn::SetResizeable( bool resizeable ) | |
1738 | { | |
1739 | gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable ); | |
1740 | } | |
1741 | ||
47cef10f RR |
1742 | void wxDataViewColumn::SetAlignment( wxAlignment align ) |
1743 | { | |
9861f022 | 1744 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1745 | |
47cef10f RR |
1746 | gfloat xalign = 0.0; |
1747 | if (align == wxALIGN_RIGHT) | |
1748 | xalign = 1.0; | |
9861f022 RR |
1749 | if (align == wxALIGN_CENTER_HORIZONTAL || |
1750 | align == wxALIGN_CENTER) | |
47cef10f | 1751 | xalign = 0.5; |
f4322df6 | 1752 | |
9861f022 RR |
1753 | gtk_tree_view_column_set_alignment( column, xalign ); |
1754 | } | |
1755 | ||
1756 | wxAlignment wxDataViewColumn::GetAlignment() const | |
1757 | { | |
1758 | gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) ); | |
1759 | ||
1760 | if (xalign == 1.0) | |
1761 | return wxALIGN_RIGHT; | |
1762 | if (xalign == 0.5) | |
1763 | return wxALIGN_CENTER_HORIZONTAL; | |
f4322df6 | 1764 | |
9861f022 | 1765 | return wxALIGN_LEFT; |
47cef10f RR |
1766 | } |
1767 | ||
31fb32e1 RR |
1768 | void wxDataViewColumn::SetSortable( bool sortable ) |
1769 | { | |
9861f022 | 1770 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
31fb32e1 RR |
1771 | gtk_tree_view_column_set_sort_indicator( column, sortable ); |
1772 | } | |
1773 | ||
87f0efe2 | 1774 | bool wxDataViewColumn::IsSortable() const |
31fb32e1 | 1775 | { |
9861f022 | 1776 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
31fb32e1 RR |
1777 | return gtk_tree_view_column_get_sort_indicator( column ); |
1778 | } | |
1779 | ||
9861f022 RR |
1780 | bool wxDataViewColumn::IsResizeable() const |
1781 | { | |
1782 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
1783 | return gtk_tree_view_column_get_resizable( column ); | |
1784 | } | |
1785 | ||
1786 | bool wxDataViewColumn::IsHidden() const | |
1787 | { | |
1788 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
1789 | return !gtk_tree_view_column_get_visible( column ); | |
1790 | } | |
1791 | ||
47cef10f RR |
1792 | void wxDataViewColumn::SetSortOrder( bool ascending ) |
1793 | { | |
9861f022 | 1794 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1795 | |
47cef10f RR |
1796 | if (ascending) |
1797 | gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING ); | |
1798 | else | |
1799 | gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING ); | |
1800 | } | |
1801 | ||
87f0efe2 | 1802 | bool wxDataViewColumn::IsSortOrderAscending() const |
31fb32e1 | 1803 | { |
9861f022 | 1804 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1805 | |
31fb32e1 RR |
1806 | return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING); |
1807 | } | |
1808 | ||
9861f022 | 1809 | void wxDataViewColumn::SetMinWidth( int width ) |
533544f2 | 1810 | { |
9861f022 | 1811 | gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width ); |
533544f2 RR |
1812 | } |
1813 | ||
9861f022 RR |
1814 | int wxDataViewColumn::GetMinWidth() const |
1815 | { | |
1816 | return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) ); | |
1817 | } | |
1818 | ||
1819 | int wxDataViewColumn::GetWidth() const | |
533544f2 | 1820 | { |
9861f022 | 1821 | return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) ); |
533544f2 RR |
1822 | } |
1823 | ||
9861f022 | 1824 | void wxDataViewColumn::SetWidth( int width ) |
533544f2 | 1825 | { |
9861f022 | 1826 | gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width ); |
533544f2 RR |
1827 | } |
1828 | ||
9861f022 | 1829 | |
eb7f97f8 RR |
1830 | //----------------------------------------------------------------------------- |
1831 | // wxDataViewCtrl signal callbacks | |
1832 | //----------------------------------------------------------------------------- | |
1833 | ||
1834 | static void | |
1835 | wxdataview_selection_changed_callback( GtkTreeSelection* selection, wxDataViewCtrl *dv ) | |
1836 | { | |
b94db696 RR |
1837 | if (!GTK_WIDGET_REALIZED(dv->m_widget)) |
1838 | return; | |
f4322df6 | 1839 | |
eb7f97f8 | 1840 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_SELECTED, dv->GetId() ); |
6ee2c58b | 1841 | event.SetRow( dv->GetSelection() ); |
eb7f97f8 RR |
1842 | event.SetModel( dv->GetModel() ); |
1843 | dv->GetEventHandler()->ProcessEvent( event ); | |
1844 | } | |
1845 | ||
f828871d | 1846 | static void |
f4322df6 | 1847 | wxdataview_row_activated_callback( GtkTreeView* treeview, GtkTreePath *path, |
f828871d RR |
1848 | GtkTreeViewColumn *column, wxDataViewCtrl *dv ) |
1849 | { | |
1850 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED, dv->GetId() ); | |
1851 | unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0]; | |
1852 | event.SetRow( row ); | |
1853 | event.SetModel( dv->GetModel() ); | |
1854 | dv->GetEventHandler()->ProcessEvent( event ); | |
1855 | } | |
1856 | ||
790b137e RR |
1857 | //----------------------------------------------------------------------------- |
1858 | // wxDataViewCtrl | |
1859 | //----------------------------------------------------------------------------- | |
1860 | ||
239eaa41 RR |
1861 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) |
1862 | ||
1863 | wxDataViewCtrl::~wxDataViewCtrl() | |
1864 | { | |
8f850e28 RR |
1865 | if (m_notifier) |
1866 | GetModel()->RemoveNotifier( m_notifier ); | |
87f0efe2 RR |
1867 | |
1868 | // remove the model from the GtkTreeView before it gets destroyed by the | |
1869 | // wxDataViewCtrlBase's dtor | |
1870 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL ); | |
239eaa41 RR |
1871 | } |
1872 | ||
1873 | void wxDataViewCtrl::Init() | |
1874 | { | |
8f850e28 | 1875 | m_notifier = NULL; |
239eaa41 RR |
1876 | } |
1877 | ||
1878 | bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id, | |
93763ad5 | 1879 | const wxPoint& pos, const wxSize& size, |
239eaa41 RR |
1880 | long style, const wxValidator& validator ) |
1881 | { | |
1882 | Init(); | |
93763ad5 WS |
1883 | |
1884 | m_needParent = true; | |
1885 | m_acceptsFocus = true; | |
239eaa41 RR |
1886 | |
1887 | if (!PreCreation( parent, pos, size ) || | |
1888 | !CreateBase( parent, id, pos, size, style, validator )) | |
1889 | { | |
1890 | wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") ); | |
93763ad5 | 1891 | return false; |
239eaa41 | 1892 | } |
1a367564 RR |
1893 | |
1894 | m_widget = gtk_scrolled_window_new (NULL, NULL); | |
6493aaca VZ |
1895 | |
1896 | GtkScrolledWindowSetBorder(m_widget, style); | |
1a367564 RR |
1897 | |
1898 | m_treeview = gtk_tree_view_new(); | |
1899 | gtk_container_add (GTK_CONTAINER (m_widget), m_treeview); | |
ed4b0fdc | 1900 | |
b94db696 RR |
1901 | #ifdef __WXGTK26__ |
1902 | if (!gtk_check_version(2,6,0)) | |
1903 | gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), TRUE ); | |
1904 | #endif | |
1905 | ||
daebb44c RR |
1906 | if (style & wxDV_MULTIPLE) |
1907 | { | |
1908 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
1909 | gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE ); | |
1910 | } | |
93763ad5 | 1911 | |
9861f022 RR |
1912 | gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 ); |
1913 | ||
1914 | #ifdef __WXGTK210__ | |
1915 | if (!gtk_check_version(2,10,0)) | |
1916 | { | |
1917 | GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE; | |
f4322df6 VZ |
1918 | |
1919 | if ((style & wxDV_HORIZ_RULES) != 0 && | |
9861f022 RR |
1920 | (style & wxDV_VERT_RULES) != 0) |
1921 | grid = GTK_TREE_VIEW_GRID_LINES_BOTH; | |
1922 | else if (style & wxDV_VERT_RULES) | |
1923 | grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL; | |
1924 | else if (style & wxDV_HORIZ_RULES) | |
1925 | grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL; | |
1926 | ||
1927 | gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid ); | |
1928 | } | |
1929 | else | |
1930 | #endif | |
1931 | gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_HORIZ_RULES) != 0 ); | |
1932 | ||
1a367564 RR |
1933 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget), |
1934 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
1935 | gtk_widget_show (m_treeview); | |
93763ad5 | 1936 | |
239eaa41 RR |
1937 | m_parent->DoAddChild( this ); |
1938 | ||
b94db696 RR |
1939 | PostCreation(size); |
1940 | ||
eb7f97f8 RR |
1941 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); |
1942 | g_signal_connect_after (selection, "changed", | |
1943 | G_CALLBACK (wxdataview_selection_changed_callback), this); | |
f828871d RR |
1944 | g_signal_connect_after (m_treeview, "row_activated", |
1945 | G_CALLBACK (wxdataview_row_activated_callback), this); | |
eb7f97f8 | 1946 | |
239eaa41 RR |
1947 | return true; |
1948 | } | |
1949 | ||
31fb32e1 RR |
1950 | void wxDataViewCtrl::OnInternalIdle() |
1951 | { | |
1952 | wxWindow::OnInternalIdle(); | |
f4322df6 | 1953 | |
9861f022 | 1954 | unsigned int cols = GetColumnCount(); |
31fb32e1 RR |
1955 | unsigned int i; |
1956 | for (i = 0; i < cols; i++) | |
1957 | { | |
1958 | wxDataViewColumn *col = GetColumn( i ); | |
1959 | col->OnInternalIdle(); | |
1960 | } | |
1961 | } | |
1962 | ||
6e2e590f | 1963 | bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model ) |
239eaa41 RR |
1964 | { |
1965 | if (!wxDataViewCtrlBase::AssociateModel( model )) | |
1966 | return false; | |
1967 | ||
6e2e590f RR |
1968 | GtkWxListStore *gtk_store = wxgtk_list_store_new(); |
1969 | gtk_store->model = model; | |
1970 | ||
8f850e28 | 1971 | m_notifier = new wxGtkDataViewListModelNotifier( gtk_store, model ); |
6e2e590f | 1972 | |
93763ad5 | 1973 | model->AddNotifier( m_notifier ); |
1557c77b | 1974 | |
1a367564 RR |
1975 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_store) ); |
1976 | g_object_unref( gtk_store ); | |
93763ad5 | 1977 | |
239eaa41 RR |
1978 | return true; |
1979 | } | |
790b137e | 1980 | |
fa28826d RR |
1981 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) |
1982 | { | |
1983 | if (!wxDataViewCtrlBase::AppendColumn(col)) | |
1984 | return false; | |
93763ad5 | 1985 | |
fa28826d RR |
1986 | GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle(); |
1987 | ||
1a367564 | 1988 | gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column ); |
fa28826d RR |
1989 | |
1990 | return true; | |
1991 | } | |
1992 | ||
6ff7eee7 RR |
1993 | void wxDataViewCtrl::SetSelection( int row ) |
1994 | { | |
fc211fe5 | 1995 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); |
f4322df6 | 1996 | |
fc211fe5 RR |
1997 | if (row < 0) |
1998 | { | |
1999 | gtk_tree_selection_unselect_all( selection ); | |
2000 | } | |
2001 | else | |
2002 | { | |
2003 | GtkTreePath *path = gtk_tree_path_new (); | |
2004 | gtk_tree_path_append_index( path, row ); | |
f4322df6 | 2005 | |
fc211fe5 | 2006 | gtk_tree_selection_select_path( selection, path ); |
f4322df6 | 2007 | |
fc211fe5 RR |
2008 | gtk_tree_path_free( path ); |
2009 | } | |
2010 | } | |
2011 | ||
2012 | void wxDataViewCtrl::Unselect( unsigned int row ) | |
2013 | { | |
2014 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
f4322df6 | 2015 | |
fc211fe5 RR |
2016 | GtkTreePath *path = gtk_tree_path_new (); |
2017 | gtk_tree_path_append_index( path, row ); | |
f4322df6 | 2018 | |
fc211fe5 | 2019 | gtk_tree_selection_unselect_path( selection, path ); |
f4322df6 | 2020 | |
fc211fe5 | 2021 | gtk_tree_path_free( path ); |
6ff7eee7 RR |
2022 | } |
2023 | ||
2024 | void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to ) | |
2025 | { | |
2026 | } | |
2027 | ||
2028 | void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections) | |
2029 | { | |
2030 | } | |
f4322df6 | 2031 | |
6ff7eee7 RR |
2032 | bool wxDataViewCtrl::IsSelected( unsigned int row ) const |
2033 | { | |
fc211fe5 | 2034 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); |
f4322df6 | 2035 | |
fc211fe5 RR |
2036 | GtkTreePath *path = gtk_tree_path_new (); |
2037 | gtk_tree_path_append_index( path, row ); | |
f4322df6 | 2038 | |
fc211fe5 | 2039 | gboolean ret = gtk_tree_selection_path_is_selected( selection, path ); |
f4322df6 | 2040 | |
fc211fe5 | 2041 | gtk_tree_path_free( path ); |
f4322df6 | 2042 | |
fc211fe5 | 2043 | return ret; |
6ff7eee7 RR |
2044 | } |
2045 | ||
2046 | int wxDataViewCtrl::GetSelection() const | |
2047 | { | |
6ee2c58b RR |
2048 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); |
2049 | if (HasFlag(wxDV_MULTIPLE)) | |
2050 | { | |
2051 | GtkTreeModel *model; | |
2052 | GList *list = gtk_tree_selection_get_selected_rows( selection, &model ); | |
f4322df6 | 2053 | |
6ee2c58b RR |
2054 | // do something |
2055 | if (list) | |
2056 | { | |
2057 | // list = g_list_nth( list, 0 ); should be a noop | |
2058 | GtkTreePath *path = (GtkTreePath*) list->data; | |
f4322df6 | 2059 | |
6ee2c58b | 2060 | unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0]; |
f4322df6 | 2061 | |
6ee2c58b RR |
2062 | // delete list |
2063 | g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL ); | |
2064 | g_list_free( list ); | |
f4322df6 | 2065 | |
6ee2c58b RR |
2066 | return (int) row; |
2067 | } | |
2068 | } | |
2069 | else | |
2070 | { | |
f4322df6 | 2071 | |
6ee2c58b RR |
2072 | GtkTreeModel *model; |
2073 | GtkTreeIter iter; | |
2074 | gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter ); | |
2075 | if (has_selection) | |
2076 | { | |
2077 | unsigned int row = (wxUIntPtr) iter.user_data; | |
2078 | return (int) row; | |
2079 | } | |
2080 | } | |
f4322df6 | 2081 | |
6ff7eee7 RR |
2082 | return -1; |
2083 | } | |
2084 | ||
2085 | int wxDataViewCtrl::GetSelections(wxArrayInt& aSelections) const | |
2086 | { | |
6ee2c58b RR |
2087 | aSelections.Clear(); |
2088 | ||
2089 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
2090 | if (HasFlag(wxDV_MULTIPLE)) | |
2091 | { | |
2092 | GtkTreeModel *model; | |
2093 | GList *list = gtk_tree_selection_get_selected_rows( selection, &model ); | |
f4322df6 | 2094 | |
6ee2c58b RR |
2095 | int count = 0; |
2096 | while (list) | |
2097 | { | |
f4322df6 | 2098 | |
6ee2c58b RR |
2099 | // list = g_list_nth( list, 0 ); should be a noop |
2100 | GtkTreePath *path = (GtkTreePath*) list->data; | |
f4322df6 | 2101 | |
6ee2c58b | 2102 | unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0]; |
f4322df6 | 2103 | |
6ee2c58b | 2104 | aSelections.Add( (int) row ); |
f4322df6 VZ |
2105 | |
2106 | list = g_list_next( list ); | |
6ee2c58b | 2107 | } |
f4322df6 | 2108 | |
6ee2c58b RR |
2109 | // delete list |
2110 | g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL ); | |
2111 | g_list_free( list ); | |
f4322df6 | 2112 | |
6ee2c58b RR |
2113 | return count; |
2114 | } | |
2115 | else | |
2116 | { | |
2117 | GtkTreeModel *model; | |
2118 | GtkTreeIter iter; | |
2119 | gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter ); | |
2120 | if (has_selection) | |
2121 | { | |
2122 | unsigned int row = (wxUIntPtr) iter.user_data; | |
2123 | aSelections.Add( (int) row ); | |
2124 | return 1; | |
2125 | } | |
2126 | } | |
f4322df6 | 2127 | |
6ff7eee7 RR |
2128 | return 0; |
2129 | } | |
2130 | ||
b94db696 RR |
2131 | // static |
2132 | wxVisualAttributes | |
2133 | wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
2134 | { | |
2135 | return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new); | |
2136 | } | |
2137 | ||
6ff7eee7 | 2138 | |
93763ad5 | 2139 | #endif |
4ed7af08 | 2140 | // !wxUSE_GENERICDATAVIEWCTRL |
790b137e | 2141 | |
93763ad5 | 2142 | #endif |
4ed7af08 | 2143 | // wxUSE_DATAVIEWCTRL |