]>
Commit | Line | Data |
---|---|---|
790b137e RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dataview.cpp | |
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 | ||
13 | #include "wx/defs.h" | |
14 | ||
15 | #if wxUSE_DATAVIEWCTRL | |
16 | ||
17 | #include "wx/dataview.h" | |
18 | #include "wx/stockitem.h" | |
e152afc3 | 19 | #include "wx/dcclient.h" |
7ea3a0de RR |
20 | #include "wx/calctrl.h" |
21 | #include "wx/popupwin.h" | |
22 | #include "wx/sizer.h" | |
3f3af7e7 | 23 | #include "wx/log.h" |
790b137e RR |
24 | |
25 | #include "wx/gtk/private.h" | |
26 | #include "wx/gtk/win_gtk.h" | |
27 | ||
28 | #include <gobject/gvaluecollector.h> | |
1557c77b RR |
29 | #include <gtk/gtktreemodel.h> |
30 | #include <gtk/gtktreednd.h> | |
790b137e | 31 | |
4d496ecb RR |
32 | #include <gdk/gdkkeysyms.h> |
33 | ||
790b137e RR |
34 | //----------------------------------------------------------------------------- |
35 | // classes | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | class wxDataViewCtrl; | |
39 | ||
40 | //----------------------------------------------------------------------------- | |
41 | // idle system | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | extern void wxapp_install_idle_handler(); | |
45 | extern bool g_isIdle; | |
46 | ||
47 | //----------------------------------------------------------------------------- | |
48 | // data | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | extern bool g_blockEventsOnDrag; | |
52 | ||
53 | //----------------------------------------------------------------------------- | |
54 | // define new GTK+ class wxGtkListStore | |
55 | //----------------------------------------------------------------------------- | |
56 | ||
57 | extern "C" { | |
58 | ||
59 | #define GTK_TYPE_WX_LIST_STORE (gtk_wx_list_store_get_type ()) | |
60 | #define GTK_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStore)) | |
61 | #define GTK_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass)) | |
62 | #define GTK_IS_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_LIST_STORE)) | |
63 | #define GTK_IS_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_LIST_STORE)) | |
64 | #define GTK_WX_LIST_STORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass)) | |
65 | ||
66 | GType gtk_wx_list_store_get_type (void); | |
67 | ||
68 | typedef struct _GtkWxListStore GtkWxListStore; | |
69 | typedef struct _GtkWxListStoreClass GtkWxListStoreClass; | |
70 | ||
71 | struct _GtkWxListStore | |
72 | { | |
73 | GObject parent; | |
74 | ||
75 | /*< private >*/ | |
76 | gint stamp; | |
77 | wxDataViewListModel *model; | |
78 | }; | |
79 | ||
80 | struct _GtkWxListStoreClass | |
81 | { | |
e152afc3 | 82 | GObjectClass list_parent_class; |
790b137e RR |
83 | |
84 | }; | |
85 | ||
e152afc3 | 86 | static GtkWxListStore *wxgtk_list_store_new (void); |
790b137e | 87 | static void wxgtk_list_store_init (GtkWxListStore *list_store); |
1557c77b | 88 | static void wxgtk_list_store_class_init (GtkWxListStoreClass *klass); |
790b137e RR |
89 | static void wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface); |
90 | static void wxgtk_list_store_finalize (GObject *object); | |
91 | static GtkTreeModelFlags wxgtk_list_store_get_flags (GtkTreeModel *tree_model); | |
92 | static gint wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model); | |
93 | static GType wxgtk_list_store_get_column_type (GtkTreeModel *tree_model, | |
94 | gint index); | |
95 | static gboolean wxgtk_list_store_get_iter (GtkTreeModel *tree_model, | |
96 | GtkTreeIter *iter, | |
97 | GtkTreePath *path); | |
98 | static GtkTreePath *wxgtk_list_store_get_path (GtkTreeModel *tree_model, | |
99 | GtkTreeIter *iter); | |
100 | static void wxgtk_list_store_get_value (GtkTreeModel *tree_model, | |
101 | GtkTreeIter *iter, | |
102 | gint column, | |
103 | GValue *value); | |
104 | static gboolean wxgtk_list_store_iter_next (GtkTreeModel *tree_model, | |
105 | GtkTreeIter *iter); | |
106 | static gboolean wxgtk_list_store_iter_children (GtkTreeModel *tree_model, | |
107 | GtkTreeIter *iter, | |
108 | GtkTreeIter *parent); | |
109 | static gboolean wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model, | |
110 | GtkTreeIter *iter); | |
111 | static gint wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model, | |
112 | GtkTreeIter *iter); | |
113 | static gboolean wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model, | |
114 | GtkTreeIter *iter, | |
115 | GtkTreeIter *parent, | |
116 | gint n); | |
117 | static gboolean wxgtk_list_store_iter_parent (GtkTreeModel *tree_model, | |
118 | GtkTreeIter *iter, | |
119 | GtkTreeIter *child); | |
120 | ||
e152afc3 | 121 | static GObjectClass *list_parent_class = NULL; |
790b137e RR |
122 | |
123 | GType | |
111f83c4 | 124 | gtk_wx_list_store_get_type (void) |
790b137e RR |
125 | { |
126 | static GType list_store_type = 0; | |
127 | ||
128 | if (!list_store_type) | |
129 | { | |
130 | static const GTypeInfo list_store_info = | |
131 | { | |
132 | sizeof (GtkWxListStoreClass), | |
133 | NULL, /* base_init */ | |
134 | NULL, /* base_finalize */ | |
135 | (GClassInitFunc) wxgtk_list_store_class_init, | |
136 | NULL, /* class_finalize */ | |
137 | NULL, /* class_data */ | |
138 | sizeof (GtkWxListStore), | |
139 | 0, | |
140 | (GInstanceInitFunc) wxgtk_list_store_init, | |
141 | }; | |
142 | ||
143 | static const GInterfaceInfo tree_model_info = | |
144 | { | |
145 | (GInterfaceInitFunc) wxgtk_list_store_tree_model_init, | |
146 | NULL, | |
147 | NULL | |
148 | }; | |
149 | ||
150 | list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxListStore", | |
1557c77b | 151 | &list_store_info, (GTypeFlags)0 ); |
790b137e RR |
152 | |
153 | g_type_add_interface_static (list_store_type, | |
154 | GTK_TYPE_TREE_MODEL, | |
155 | &tree_model_info); | |
156 | } | |
157 | ||
158 | return list_store_type; | |
159 | } | |
160 | ||
1557c77b | 161 | static GtkWxListStore * |
e152afc3 | 162 | wxgtk_list_store_new(void) |
1557c77b | 163 | { |
e152afc3 RR |
164 | GtkWxListStore *retval = (GtkWxListStore *) g_object_new (GTK_TYPE_WX_LIST_STORE, NULL); |
165 | return retval; | |
1557c77b RR |
166 | } |
167 | ||
790b137e | 168 | static void |
1557c77b | 169 | wxgtk_list_store_class_init (GtkWxListStoreClass *klass) |
790b137e | 170 | { |
e152afc3 RR |
171 | list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); |
172 | GObjectClass *object_class = (GObjectClass*) klass; | |
790b137e RR |
173 | object_class->finalize = wxgtk_list_store_finalize; |
174 | } | |
175 | ||
176 | static void | |
177 | wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface) | |
178 | { | |
179 | iface->get_flags = wxgtk_list_store_get_flags; | |
180 | iface->get_n_columns = wxgtk_list_store_get_n_columns; | |
181 | iface->get_column_type = wxgtk_list_store_get_column_type; | |
182 | iface->get_iter = wxgtk_list_store_get_iter; | |
183 | iface->get_path = wxgtk_list_store_get_path; | |
184 | iface->get_value = wxgtk_list_store_get_value; | |
185 | iface->iter_next = wxgtk_list_store_iter_next; | |
186 | iface->iter_children = wxgtk_list_store_iter_children; | |
187 | iface->iter_has_child = wxgtk_list_store_iter_has_child; | |
188 | iface->iter_n_children = wxgtk_list_store_iter_n_children; | |
189 | iface->iter_nth_child = wxgtk_list_store_iter_nth_child; | |
190 | iface->iter_parent = wxgtk_list_store_iter_parent; | |
191 | } | |
192 | ||
193 | static void | |
194 | wxgtk_list_store_init (GtkWxListStore *list_store) | |
195 | { | |
196 | list_store->model = NULL; | |
1557c77b | 197 | list_store->stamp = g_random_int(); |
790b137e RR |
198 | } |
199 | ||
200 | static void | |
201 | wxgtk_list_store_finalize (GObject *object) | |
202 | { | |
1557c77b | 203 | /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */ |
790b137e RR |
204 | |
205 | /* we need to sort out, which class deletes what */ | |
111f83c4 | 206 | /* delete list_store->model; */ |
790b137e RR |
207 | |
208 | /* must chain up */ | |
e152afc3 | 209 | (* list_parent_class->finalize) (object); |
790b137e RR |
210 | } |
211 | ||
212 | } // extern "C" | |
213 | ||
214 | //----------------------------------------------------------------------------- | |
215 | // implement callbacks from wxGtkListStore class by letting | |
216 | // them call the methods of wxWidgets' wxDataViewListModel | |
217 | //----------------------------------------------------------------------------- | |
218 | ||
219 | static GtkTreeModelFlags | |
220 | wxgtk_list_store_get_flags (GtkTreeModel *tree_model) | |
221 | { | |
1557c77b | 222 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), (GtkTreeModelFlags)0 ); |
790b137e | 223 | |
67cbdfc1 RR |
224 | // GTK+ list store uses a linked list for storing the |
225 | // items and a pointer to a child is used as the member | |
226 | // field of a GtkTreeIter. This means that the iter is | |
227 | // valid in the GtkListStore as long as the child exists. | |
228 | // We use the index of the row and since the index of a | |
229 | // specific row will change if a row above is deleted, | |
230 | // the iter does not persist | |
231 | return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY; | |
790b137e RR |
232 | } |
233 | ||
234 | static gint | |
235 | wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model) | |
236 | { | |
237 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
238 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), 0); | |
239 | ||
239eaa41 | 240 | return list_store->model->GetNumberOfCols(); |
790b137e RR |
241 | } |
242 | ||
243 | static GType | |
244 | wxgtk_list_store_get_column_type (GtkTreeModel *tree_model, | |
245 | gint index) | |
246 | { | |
247 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
248 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), G_TYPE_INVALID); | |
249 | ||
1557c77b | 250 | GType gtype = G_TYPE_INVALID; |
239eaa41 | 251 | |
1557c77b RR |
252 | wxString wxtype = list_store->model->GetColType( (size_t) index ); |
253 | ||
254 | if (wxtype == wxT("string")) | |
255 | gtype = G_TYPE_STRING; | |
790b137e RR |
256 | |
257 | return gtype; | |
258 | } | |
259 | ||
260 | static gboolean | |
261 | wxgtk_list_store_get_iter (GtkTreeModel *tree_model, | |
262 | GtkTreeIter *iter, | |
263 | GtkTreePath *path) | |
264 | { | |
265 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
266 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); | |
267 | g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE); | |
268 | ||
1557c77b | 269 | size_t i = (size_t)gtk_tree_path_get_indices (path)[0]; |
790b137e | 270 | |
1557c77b | 271 | if (i >= list_store->model->GetNumberOfRows()) |
239eaa41 | 272 | return FALSE; |
790b137e | 273 | |
239eaa41 RR |
274 | iter->stamp = list_store->stamp; |
275 | // user_data is just the index | |
276 | iter->user_data = (gpointer) i; | |
790b137e RR |
277 | |
278 | return TRUE; | |
279 | } | |
280 | ||
281 | static GtkTreePath * | |
282 | wxgtk_list_store_get_path (GtkTreeModel *tree_model, | |
283 | GtkTreeIter *iter) | |
284 | { | |
790b137e | 285 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), NULL); |
239eaa41 | 286 | g_return_val_if_fail (iter->stamp == GTK_WX_LIST_STORE (tree_model)->stamp, NULL); |
790b137e | 287 | |
239eaa41 RR |
288 | GtkTreePath *retval = gtk_tree_path_new (); |
289 | // user_data is just the index | |
1557c77b | 290 | int i = (int) iter->user_data; |
239eaa41 RR |
291 | gtk_tree_path_append_index (retval, i); |
292 | return retval; | |
790b137e RR |
293 | } |
294 | ||
295 | static void | |
296 | wxgtk_list_store_get_value (GtkTreeModel *tree_model, | |
297 | GtkTreeIter *iter, | |
298 | gint column, | |
299 | GValue *value) | |
300 | { | |
1557c77b | 301 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
790b137e | 302 | g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model) ); |
239eaa41 | 303 | |
1557c77b RR |
304 | wxDataViewListModel *model = list_store->model; |
305 | wxString mtype = model->GetColType( (size_t) column ); | |
306 | if (mtype == wxT("string")) | |
307 | { | |
3f3af7e7 | 308 | wxVariant variant; |
1557c77b | 309 | g_value_init( value, G_TYPE_STRING ); |
3f3af7e7 | 310 | model->GetValue( variant, (size_t) column, (size_t) iter->user_data ); |
1557c77b RR |
311 | g_value_set_string( value, wxGTK_CONV(variant.GetString()) ); |
312 | } | |
313 | else | |
314 | { | |
315 | } | |
790b137e RR |
316 | |
317 | #if 0 | |
318 | GtkTreeDataList *list; | |
319 | gint tmp_column = column; | |
320 | ||
321 | g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns); | |
322 | g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp); | |
323 | ||
324 | list = G_SLIST (iter->user_data)->data; | |
325 | ||
326 | while (tmp_column-- > 0 && list) | |
327 | list = list->next; | |
328 | ||
329 | if (list == NULL) | |
330 | g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]); | |
331 | else | |
332 | _gtk_tree_data_list_node_to_value (list, | |
333 | GTK_LIST_STORE (tree_model)->column_headers[column], | |
334 | value); | |
335 | #endif | |
336 | ||
337 | } | |
338 | ||
339 | static gboolean | |
340 | wxgtk_list_store_iter_next (GtkTreeModel *tree_model, | |
341 | GtkTreeIter *iter) | |
342 | { | |
790b137e | 343 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 344 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
790b137e | 345 | |
67cbdfc1 | 346 | g_return_val_if_fail (list_store->stamp == iter->stamp, FALSE); |
790b137e | 347 | |
67cbdfc1 RR |
348 | int n = (int) iter->user_data; |
349 | ||
350 | if (n == -1) | |
351 | return FALSE; | |
352 | ||
1a367564 | 353 | if (n >= (int) list_store->model->GetNumberOfRows()-1) |
67cbdfc1 RR |
354 | return FALSE; |
355 | ||
111f83c4 | 356 | iter->user_data = (gpointer) ++n; |
790b137e | 357 | |
67cbdfc1 | 358 | return TRUE; |
790b137e RR |
359 | } |
360 | ||
361 | static gboolean | |
362 | wxgtk_list_store_iter_children (GtkTreeModel *tree_model, | |
363 | GtkTreeIter *iter, | |
364 | GtkTreeIter *parent) | |
365 | { | |
67cbdfc1 | 366 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 367 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
67cbdfc1 RR |
368 | |
369 | // this is a list, nodes have no children | |
790b137e RR |
370 | if (parent) |
371 | return FALSE; | |
372 | ||
67cbdfc1 RR |
373 | iter->stamp = list_store->stamp; |
374 | iter->user_data = (gpointer) -1; | |
375 | ||
376 | return TRUE; | |
790b137e RR |
377 | } |
378 | ||
379 | static gboolean | |
380 | wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model, | |
381 | GtkTreeIter *iter) | |
382 | { | |
383 | return FALSE; | |
384 | } | |
385 | ||
386 | static gint | |
387 | wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model, | |
388 | GtkTreeIter *iter) | |
389 | { | |
390 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), -1); | |
1557c77b | 391 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
790b137e RR |
392 | |
393 | if (iter == NULL) | |
1557c77b | 394 | return (gint) list_store->model->GetNumberOfRows(); |
790b137e | 395 | |
67cbdfc1 | 396 | g_return_val_if_fail (list_store->stamp == iter->stamp, -1); |
790b137e RR |
397 | |
398 | return 0; | |
399 | } | |
400 | ||
401 | static gboolean | |
402 | wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model, | |
403 | GtkTreeIter *iter, | |
404 | GtkTreeIter *parent, | |
405 | gint n) | |
406 | { | |
67cbdfc1 | 407 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 408 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
67cbdfc1 RR |
409 | |
410 | if (parent) | |
411 | return FALSE; | |
790b137e | 412 | |
67cbdfc1 RR |
413 | if (n < 0) |
414 | return FALSE; | |
415 | ||
1557c77b | 416 | if (n >= (gint) list_store->model->GetNumberOfRows()) |
67cbdfc1 | 417 | return FALSE; |
790b137e | 418 | |
67cbdfc1 RR |
419 | iter->stamp = list_store->stamp; |
420 | iter->user_data = (gpointer) n; | |
421 | ||
422 | return TRUE; | |
790b137e RR |
423 | } |
424 | ||
425 | static gboolean | |
426 | wxgtk_list_store_iter_parent (GtkTreeModel *tree_model, | |
427 | GtkTreeIter *iter, | |
428 | GtkTreeIter *child) | |
429 | { | |
430 | return FALSE; | |
431 | } | |
432 | ||
e152afc3 RR |
433 | //----------------------------------------------------------------------------- |
434 | // define new GTK+ class wxGtkCellRenderer | |
435 | //----------------------------------------------------------------------------- | |
436 | ||
437 | extern "C" { | |
438 | ||
439 | #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ()) | |
440 | #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer)) | |
441 | #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass)) | |
442 | #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER)) | |
443 | #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER)) | |
444 | #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass)) | |
445 | ||
446 | GType gtk_wx_cell_renderer_get_type (void); | |
447 | ||
448 | typedef struct _GtkWxCellRenderer GtkWxCellRenderer; | |
449 | typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass; | |
450 | ||
451 | struct _GtkWxCellRenderer | |
452 | { | |
453 | GtkCellRenderer parent; | |
454 | ||
455 | /*< private >*/ | |
456 | wxDataViewCustomCell *cell; | |
4d496ecb | 457 | guint32 last_click; |
e152afc3 RR |
458 | }; |
459 | ||
460 | struct _GtkWxCellRendererClass | |
461 | { | |
462 | GtkCellRendererClass cell_parent_class; | |
e152afc3 RR |
463 | }; |
464 | ||
465 | ||
466 | static GtkCellRenderer *gtk_wx_cell_renderer_new (void); | |
553f7d8f RR |
467 | static void gtk_wx_cell_renderer_init ( |
468 | GtkWxCellRenderer *cell ); | |
469 | static void gtk_wx_cell_renderer_class_init( | |
470 | GtkWxCellRendererClass *klass ); | |
471 | static void gtk_wx_cell_renderer_finalize ( | |
472 | GObject *object ); | |
473 | static void gtk_wx_cell_renderer_get_size ( | |
474 | GtkCellRenderer *cell, | |
475 | GtkWidget *widget, | |
476 | GdkRectangle *rectangle, | |
477 | gint *x_offset, | |
478 | gint *y_offset, | |
479 | gint *width, | |
480 | gint *height ); | |
481 | static void gtk_wx_cell_renderer_render ( | |
482 | GtkCellRenderer *cell, | |
483 | GdkWindow *window, | |
484 | GtkWidget *widget, | |
485 | GdkRectangle *background_area, | |
486 | GdkRectangle *cell_area, | |
487 | GdkRectangle *expose_area, | |
488 | GtkCellRendererState flags ); | |
489 | static gboolean gtk_wx_cell_renderer_activate( | |
490 | GtkCellRenderer *cell, | |
491 | GdkEvent *event, | |
492 | GtkWidget *widget, | |
493 | const gchar *path, | |
494 | GdkRectangle *background_area, | |
495 | GdkRectangle *cell_area, | |
496 | GtkCellRendererState flags ); | |
e152afc3 RR |
497 | |
498 | static GObjectClass *cell_parent_class = NULL; | |
499 | ||
500 | } // extern "C" | |
501 | ||
502 | GType | |
503 | gtk_wx_cell_renderer_get_type (void) | |
504 | { | |
553f7d8f | 505 | static GType cell_wx_type = 0; |
e152afc3 | 506 | |
553f7d8f | 507 | if (!cell_wx_type) |
e152afc3 | 508 | { |
553f7d8f RR |
509 | static const GTypeInfo cell_wx_info = |
510 | { | |
511 | sizeof (GtkWxCellRendererClass), | |
512 | NULL, /* base_init */ | |
513 | NULL, /* base_finalize */ | |
514 | (GClassInitFunc) gtk_wx_cell_renderer_class_init, | |
515 | NULL, /* class_finalize */ | |
516 | NULL, /* class_data */ | |
517 | sizeof (GtkWxCellRenderer), | |
518 | 0, /* n_preallocs */ | |
519 | (GInstanceInitFunc) gtk_wx_cell_renderer_init, | |
520 | }; | |
521 | ||
522 | cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER, | |
523 | "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 ); | |
e152afc3 RR |
524 | } |
525 | ||
553f7d8f | 526 | return cell_wx_type; |
e152afc3 RR |
527 | } |
528 | ||
529 | static void | |
530 | gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell) | |
531 | { | |
532 | cell->cell = NULL; | |
4d496ecb | 533 | cell->last_click = 0; |
e152afc3 RR |
534 | } |
535 | ||
536 | static void | |
537 | gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass) | |
538 | { | |
539 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
540 | GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass); | |
541 | ||
542 | cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); | |
543 | ||
544 | object_class->finalize = gtk_wx_cell_renderer_finalize; | |
545 | ||
546 | cell_class->get_size = gtk_wx_cell_renderer_get_size; | |
547 | cell_class->render = gtk_wx_cell_renderer_render; | |
553f7d8f | 548 | cell_class->activate = gtk_wx_cell_renderer_activate; |
e152afc3 RR |
549 | } |
550 | ||
551 | static void | |
552 | gtk_wx_cell_renderer_finalize (GObject *object) | |
553 | { | |
554 | /* must chain up */ | |
555 | (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object); | |
556 | } | |
557 | ||
558 | GtkCellRenderer* | |
559 | gtk_wx_cell_renderer_new (void) | |
560 | { | |
561 | return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL); | |
562 | } | |
563 | ||
564 | static void | |
565 | gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer, | |
566 | GtkWidget *widget, | |
567 | GdkRectangle *cell_area, | |
568 | gint *x_offset, | |
569 | gint *y_offset, | |
570 | gint *width, | |
571 | gint *height) | |
572 | { | |
573 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
574 | wxDataViewCustomCell *cell = wxrenderer->cell; | |
575 | ||
576 | wxSize size = cell->GetSize(); | |
577 | ||
578 | gint calc_width = (gint) renderer->xpad * 2 + size.x; | |
579 | gint calc_height = (gint) renderer->ypad * 2 + size.y; | |
580 | ||
581 | if (x_offset) | |
582 | *x_offset = 0; | |
583 | if (y_offset) | |
584 | *y_offset = 0; | |
585 | ||
586 | if (cell_area && size.x > 0 && size.y > 0) | |
587 | { | |
588 | if (x_offset) | |
589 | { | |
590 | *x_offset = (gint)((renderer->xalign * | |
591 | (cell_area->width - calc_width - 2 * renderer->xpad))); | |
592 | *x_offset = MAX (*x_offset, 0) + renderer->xpad; | |
593 | } | |
594 | if (y_offset) | |
595 | { | |
596 | *y_offset = (gint)((renderer->yalign * | |
597 | (cell_area->height - calc_height - 2 * renderer->ypad))); | |
598 | *y_offset = MAX (*y_offset, 0) + renderer->ypad; | |
599 | } | |
600 | } | |
601 | ||
602 | if (width) | |
603 | *width = calc_width; | |
604 | ||
605 | if (height) | |
606 | *height = calc_height; | |
607 | } | |
608 | ||
609 | static void | |
610 | gtk_wx_cell_renderer_render (GtkCellRenderer *renderer, | |
611 | GdkWindow *window, | |
612 | GtkWidget *widget, | |
613 | GdkRectangle *background_area, | |
614 | GdkRectangle *cell_area, | |
615 | GdkRectangle *expose_area, | |
616 | GtkCellRendererState flags) | |
617 | ||
618 | { | |
619 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
620 | wxDataViewCustomCell *cell = wxrenderer->cell; | |
621 | ||
622 | GdkRectangle rect; | |
623 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
624 | &rect.x, | |
625 | &rect.y, | |
626 | &rect.width, | |
627 | &rect.height); | |
628 | ||
629 | rect.x += cell_area->x; | |
630 | rect.y += cell_area->y; | |
631 | rect.width -= renderer->xpad * 2; | |
632 | rect.height -= renderer->ypad * 2; | |
633 | ||
634 | GdkRectangle dummy; | |
635 | if (gdk_rectangle_intersect (expose_area, &rect, &dummy)) | |
636 | { | |
637 | wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); | |
638 | wxWindowDC* dc = (wxWindowDC*) cell->GetDC(); | |
4d496ecb RR |
639 | if (dc->m_window == NULL) |
640 | { | |
641 | dc->m_window = window; | |
642 | dc->SetUpDC(); | |
643 | } | |
e152afc3 RR |
644 | |
645 | int state = 0; | |
646 | if (flags & GTK_CELL_RENDERER_SELECTED) | |
647 | state |= wxDATAVIEW_CELL_SELECTED; | |
648 | if (flags & GTK_CELL_RENDERER_PRELIT) | |
649 | state |= wxDATAVIEW_CELL_PRELIT; | |
650 | if (flags & GTK_CELL_RENDERER_INSENSITIVE) | |
651 | state |= wxDATAVIEW_CELL_INSENSITIVE; | |
652 | if (flags & GTK_CELL_RENDERER_INSENSITIVE) | |
653 | state |= wxDATAVIEW_CELL_INSENSITIVE; | |
654 | if (flags & GTK_CELL_RENDERER_FOCUSED) | |
655 | state |= wxDATAVIEW_CELL_FOCUSED; | |
656 | cell->Render( renderrect, dc, state ); | |
657 | } | |
658 | } | |
659 | ||
553f7d8f RR |
660 | static gboolean |
661 | gtk_wx_cell_renderer_activate( | |
662 | GtkCellRenderer *renderer, | |
663 | GdkEvent *event, | |
664 | GtkWidget *widget, | |
665 | const gchar *path, | |
666 | GdkRectangle *background_area, | |
667 | GdkRectangle *cell_area, | |
668 | GtkCellRendererState flags ) | |
669 | { | |
670 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
671 | wxDataViewCustomCell *cell = wxrenderer->cell; | |
672 | ||
673 | GdkRectangle rect; | |
674 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
675 | &rect.x, | |
676 | &rect.y, | |
677 | &rect.width, | |
678 | &rect.height); | |
679 | ||
680 | rect.x += cell_area->x; | |
681 | rect.y += cell_area->y; | |
682 | rect.width -= renderer->xpad * 2; | |
683 | rect.height -= renderer->ypad * 2; | |
684 | ||
685 | wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); | |
4d496ecb RR |
686 | |
687 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); | |
688 | ||
689 | GtkTreePath *treepath = gtk_tree_path_new_from_string( path ); | |
690 | size_t model_row = (size_t)gtk_tree_path_get_indices (treepath)[0]; | |
691 | gtk_tree_path_free( treepath ); | |
692 | ||
693 | size_t model_col = cell->GetOwner()->GetModelColumn(); | |
694 | ||
695 | if (event->type == GDK_BUTTON_PRESS) | |
696 | { | |
697 | GdkEventButton *button_event = (GdkEventButton*) event; | |
698 | wxPoint pt( ((int) button_event->x) - renderrect.x, | |
699 | ((int) button_event->y) - renderrect.y ); | |
553f7d8f | 700 | |
4d496ecb RR |
701 | bool ret = false; |
702 | if (button_event->button == 1) | |
703 | { | |
704 | if (cell->LeftClick( pt, renderrect, model, model_col, model_row )) | |
705 | ret = true; | |
7ea3a0de | 706 | // TODO: query system double-click time |
4d496ecb RR |
707 | if (button_event->time - wxrenderer->last_click < 400) |
708 | if (cell->Activate( renderrect, model, model_col, model_row )) | |
709 | ret = true; | |
710 | } | |
711 | if (button_event->button == 3) | |
712 | { | |
713 | if (cell->RightClick( pt, renderrect, model, model_col, model_row )) | |
714 | ret = true; | |
715 | } | |
716 | ||
717 | wxrenderer->last_click = button_event->time; | |
718 | ||
719 | return ret; | |
720 | } | |
721 | ||
4d496ecb | 722 | return false; |
553f7d8f RR |
723 | } |
724 | ||
6e2e590f RR |
725 | // --------------------------------------------------------- |
726 | // wxGtkDataViewListModelNotifier | |
727 | // --------------------------------------------------------- | |
728 | ||
729 | class wxGtkDataViewListModelNotifier: public wxDataViewListModelNotifier | |
730 | { | |
731 | public: | |
732 | wxGtkDataViewListModelNotifier( GtkWxListStore* gtk_store, wxDataViewListModel *wx_model ); | |
733 | ||
734 | virtual bool RowAppended(); | |
735 | virtual bool RowPrepended(); | |
736 | virtual bool RowInserted( size_t before ); | |
737 | virtual bool RowDeleted( size_t row ); | |
738 | virtual bool RowChanged( size_t row ); | |
a7f61f76 | 739 | virtual bool ValueChanged( size_t col, size_t row ); |
4eccd3a1 | 740 | virtual bool RowsReordered( size_t *new_order ); |
6e2e590f RR |
741 | virtual bool Cleared(); |
742 | ||
743 | GtkWxListStore *m_gtk_store; | |
744 | wxDataViewListModel *m_wx_model; | |
745 | }; | |
746 | ||
747 | // --------------------------------------------------------- | |
748 | // wxGtkDataViewListModelNotifier | |
749 | // --------------------------------------------------------- | |
750 | ||
751 | wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier( | |
752 | GtkWxListStore* gtk_store, wxDataViewListModel *wx_model ) | |
753 | { | |
754 | m_gtk_store = gtk_store; | |
755 | m_wx_model = wx_model; | |
756 | } | |
757 | ||
758 | bool wxGtkDataViewListModelNotifier::RowAppended() | |
759 | { | |
760 | size_t pos = m_wx_model->GetNumberOfRows()-1; | |
761 | ||
762 | GtkTreeIter iter; | |
763 | iter.stamp = m_gtk_store->stamp; | |
764 | iter.user_data = (gpointer) pos; | |
765 | ||
766 | GtkTreePath *path = gtk_tree_path_new (); | |
767 | gtk_tree_path_append_index (path, (gint) pos); | |
768 | gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
769 | gtk_tree_path_free (path); | |
770 | ||
771 | return true; | |
772 | } | |
773 | ||
774 | bool wxGtkDataViewListModelNotifier::RowPrepended() | |
775 | { | |
605c2c4a RR |
776 | GtkTreeIter iter; |
777 | iter.stamp = m_gtk_store->stamp; | |
778 | iter.user_data = (gpointer) 0; | |
779 | ||
780 | GtkTreePath *path = gtk_tree_path_new (); | |
781 | gtk_tree_path_append_index (path, (gint) 0); | |
782 | gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
783 | gtk_tree_path_free (path); | |
784 | ||
785 | return true; | |
6e2e590f RR |
786 | } |
787 | ||
788 | bool wxGtkDataViewListModelNotifier::RowInserted( size_t before ) | |
789 | { | |
790 | return false; | |
791 | } | |
792 | ||
793 | bool wxGtkDataViewListModelNotifier::RowDeleted( size_t row ) | |
794 | { | |
795 | return false; | |
796 | } | |
797 | ||
798 | bool wxGtkDataViewListModelNotifier::RowChanged( size_t row ) | |
799 | { | |
a7f61f76 RR |
800 | GtkTreeIter iter; |
801 | iter.stamp = m_gtk_store->stamp; | |
802 | iter.user_data = (gpointer) row; | |
803 | GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store), &iter); | |
804 | gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
805 | gtk_tree_path_free (path); | |
806 | ||
807 | return true; | |
6e2e590f RR |
808 | } |
809 | ||
8f850e28 | 810 | bool wxGtkDataViewListModelNotifier::ValueChanged( size_t model_col, size_t model_row ) |
6e2e590f | 811 | { |
4eccd3a1 | 812 | // This adds GTK+'s missing MVC logic for ValueChanged |
8f850e28 RR |
813 | wxNode *node = GetOwner()->m_viewingColumns.GetFirst(); |
814 | while (node) | |
815 | { | |
816 | wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData(); | |
817 | if (viewing_column->m_modelColumn == model_col) | |
818 | { | |
819 | GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview); | |
820 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(viewing_column->m_viewColumn->GetGtkHandle()); | |
821 | ||
822 | // Get cell area | |
823 | GtkTreePath *path = gtk_tree_path_new(); | |
824 | gtk_tree_path_append_index( path, model_row ); | |
825 | GdkRectangle cell_area; | |
826 | gtk_tree_view_get_cell_area( widget, path, column, &cell_area ); | |
827 | gtk_tree_path_free( path ); | |
828 | ||
829 | int ydiff = column->button->allocation.height; | |
830 | // Redraw | |
831 | gtk_widget_queue_draw_area( GTK_WIDGET(widget), | |
832 | cell_area.x, ydiff + cell_area.y, cell_area.width, cell_area.height ); | |
833 | } | |
834 | ||
835 | node = node->GetNext(); | |
836 | } | |
837 | ||
838 | return true; | |
6e2e590f RR |
839 | } |
840 | ||
4eccd3a1 RR |
841 | bool wxGtkDataViewListModelNotifier::RowsReordered( size_t *new_order ) |
842 | { | |
843 | // Assume sizeof(size_t)= == sizeof(gint) | |
844 | ||
845 | GtkTreePath *path = gtk_tree_path_new (); | |
846 | gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store), path, NULL, (gint*)new_order); | |
847 | gtk_tree_path_free (path); | |
848 | ||
849 | // This adds GTK+'s missing MVC logic for RowsReordered | |
850 | wxNode *node = GetOwner()->m_viewingColumns.GetFirst(); | |
851 | while (node) | |
852 | { | |
853 | wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData(); | |
854 | GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview); | |
855 | // Doesn't work yet... | |
856 | gtk_widget_queue_draw( GTK_WIDGET(widget) ); | |
857 | ||
858 | node = node->GetNext(); | |
859 | } | |
4eccd3a1 RR |
860 | |
861 | return true; | |
862 | } | |
863 | ||
6e2e590f RR |
864 | bool wxGtkDataViewListModelNotifier::Cleared() |
865 | { | |
866 | return false; | |
867 | } | |
868 | ||
6842a71a RR |
869 | // --------------------------------------------------------- |
870 | // wxDataViewCell | |
871 | // --------------------------------------------------------- | |
872 | ||
873 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell, wxDataViewCellBase) | |
874 | ||
875 | wxDataViewCell::wxDataViewCell( const wxString &varianttype, wxDataViewCellMode mode ) : | |
876 | wxDataViewCellBase( varianttype, mode ) | |
877 | { | |
878 | m_renderer = NULL; | |
879 | } | |
880 | ||
881 | // --------------------------------------------------------- | |
882 | // wxDataViewTextCell | |
883 | // --------------------------------------------------------- | |
884 | ||
a7f61f76 RR |
885 | extern "C" { |
886 | static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer, | |
887 | gchar *arg1, gchar *arg2, gpointer user_data ); | |
888 | } | |
889 | ||
890 | static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer, | |
891 | gchar *arg1, gchar *arg2, gpointer user_data ) | |
892 | { | |
893 | wxDataViewTextCell *cell = (wxDataViewTextCell*) user_data; | |
894 | ||
895 | wxString tmp = wxGTK_CONV_BACK( arg2 ); | |
896 | wxVariant value = tmp; | |
897 | if (!cell->Validate( value )) | |
898 | return; | |
899 | ||
900 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); | |
901 | ||
902 | GtkTreePath *path = gtk_tree_path_new_from_string( arg1 ); | |
903 | size_t model_row = (size_t)gtk_tree_path_get_indices (path)[0]; | |
904 | gtk_tree_path_free( path ); | |
905 | ||
906 | size_t model_col = cell->GetOwner()->GetModelColumn(); | |
907 | ||
908 | model->SetValue( value, model_col, model_row ); | |
909 | model->ValueChanged( model_col, model_row ); | |
910 | } | |
911 | ||
6842a71a RR |
912 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell, wxDataViewCell) |
913 | ||
914 | wxDataViewTextCell::wxDataViewTextCell( const wxString &varianttype, wxDataViewCellMode mode ) : | |
915 | wxDataViewCell( varianttype, mode ) | |
916 | { | |
917 | m_renderer = (void*) gtk_cell_renderer_text_new(); | |
a7f61f76 RR |
918 | |
919 | if (m_mode & wxDATAVIEW_CELL_EDITABLE) | |
920 | { | |
921 | GValue gvalue = { 0, }; | |
922 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
923 | g_value_set_boolean( &gvalue, true ); | |
924 | g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue ); | |
925 | g_value_unset( &gvalue ); | |
926 | ||
927 | g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this ); | |
928 | } | |
6842a71a | 929 | } |
790b137e | 930 | |
7b4fde82 RR |
931 | bool wxDataViewTextCell::SetValue( const wxVariant &value ) |
932 | { | |
933 | wxString tmp = value; | |
934 | ||
935 | GValue gvalue = { 0, }; | |
936 | g_value_init( &gvalue, G_TYPE_STRING ); | |
937 | g_value_set_string( &gvalue, wxGTK_CONV( tmp ) ); | |
938 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
939 | g_value_unset( &gvalue ); | |
940 | ||
941 | return true; | |
942 | } | |
943 | ||
a7f61f76 RR |
944 | bool wxDataViewTextCell::GetValue( wxVariant &value ) |
945 | { | |
946 | GValue gvalue = { 0, }; | |
947 | g_value_init( &gvalue, G_TYPE_STRING ); | |
948 | g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
949 | wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &gvalue ) ); | |
950 | g_value_unset( &gvalue ); | |
951 | ||
952 | value = tmp; | |
953 | ||
954 | return true; | |
955 | } | |
956 | ||
fa28826d | 957 | // --------------------------------------------------------- |
605c2c4a | 958 | // wxDataViewToggleCell |
fa28826d RR |
959 | // --------------------------------------------------------- |
960 | ||
605c2c4a RR |
961 | extern "C" { |
962 | static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, | |
963 | gchar *path, gpointer user_data ); | |
964 | } | |
965 | ||
966 | static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, | |
967 | gchar *path, gpointer user_data ) | |
968 | { | |
969 | wxDataViewToggleCell *cell = (wxDataViewToggleCell*) user_data; | |
970 | ||
971 | // get old value | |
972 | GValue gvalue = { 0, }; | |
973 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
974 | g_object_get_property( G_OBJECT(renderer), "active", &gvalue ); | |
975 | bool tmp = g_value_get_boolean( &gvalue ); | |
976 | g_value_unset( &gvalue ); | |
977 | // invert it | |
978 | tmp = !tmp; | |
979 | ||
980 | wxVariant value = tmp; | |
981 | if (!cell->Validate( value )) | |
982 | return; | |
983 | ||
984 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); | |
985 | ||
986 | GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path ); | |
987 | size_t model_row = (size_t)gtk_tree_path_get_indices (gtk_path)[0]; | |
988 | gtk_tree_path_free( gtk_path ); | |
989 | ||
990 | size_t model_col = cell->GetOwner()->GetModelColumn(); | |
991 | ||
992 | model->SetValue( value, model_col, model_row ); | |
993 | model->ValueChanged( model_col, model_row ); | |
994 | } | |
995 | ||
996 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell, wxDataViewCell) | |
997 | ||
998 | wxDataViewToggleCell::wxDataViewToggleCell( const wxString &varianttype, | |
999 | wxDataViewCellMode mode ) : | |
1000 | wxDataViewCell( varianttype, mode ) | |
1001 | { | |
1002 | m_renderer = (void*) gtk_cell_renderer_toggle_new(); | |
1003 | ||
1004 | if (m_mode & wxDATAVIEW_CELL_EDITABLE) | |
1005 | { | |
553f7d8f RR |
1006 | g_signal_connect_after( m_renderer, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback), this ); |
1007 | } | |
1008 | else | |
1009 | { | |
1010 | ||
605c2c4a RR |
1011 | GValue gvalue = { 0, }; |
1012 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
553f7d8f | 1013 | g_value_set_boolean( &gvalue, false ); |
605c2c4a RR |
1014 | g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue ); |
1015 | g_value_unset( &gvalue ); | |
553f7d8f RR |
1016 | |
1017 | GValue gvalue2 = { 0, }; | |
1018 | g_value_init( &gvalue2, gtk_cell_renderer_mode_get_type() ); | |
1019 | g_value_set_enum( &gvalue2, GTK_CELL_RENDERER_MODE_INERT ); | |
1020 | g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue2 ); | |
1021 | g_value_unset( &gvalue2 ); | |
1022 | ||
605c2c4a RR |
1023 | } |
1024 | } | |
1025 | ||
1026 | bool wxDataViewToggleCell::SetValue( const wxVariant &value ) | |
1027 | { | |
1028 | bool tmp = value; | |
1029 | ||
1030 | GValue gvalue = { 0, }; | |
1031 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1032 | g_value_set_boolean( &gvalue, tmp ); | |
1033 | g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue ); | |
1034 | g_value_unset( &gvalue ); | |
1035 | ||
1036 | return true; | |
1037 | } | |
1038 | ||
1039 | bool wxDataViewToggleCell::GetValue( wxVariant &value ) | |
1040 | { | |
1041 | GValue gvalue = { 0, }; | |
1042 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1043 | g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue ); | |
1044 | bool tmp = g_value_get_boolean( &gvalue ); | |
1045 | g_value_unset( &gvalue ); | |
1046 | ||
1047 | value = tmp; | |
1048 | ||
1049 | return true; | |
1050 | } | |
1051 | ||
e152afc3 RR |
1052 | // --------------------------------------------------------- |
1053 | // wxDataViewCustomCell | |
1054 | // --------------------------------------------------------- | |
1055 | ||
1056 | class wxDataViewCtrlDC: public wxWindowDC | |
1057 | { | |
1058 | public: | |
1059 | wxDataViewCtrlDC( wxDataViewCtrl *window ) | |
1060 | { | |
1a367564 | 1061 | GtkWidget *widget = window->m_treeview; |
e152afc3 RR |
1062 | // Set later |
1063 | m_window = NULL; | |
4d496ecb | 1064 | |
e152afc3 RR |
1065 | m_context = window->GtkGetPangoDefaultContext(); |
1066 | m_layout = pango_layout_new( m_context ); | |
1067 | m_fontdesc = pango_font_description_copy( widget->style->font_desc ); | |
1068 | ||
1069 | m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget ); | |
1070 | ||
4d496ecb RR |
1071 | // Set m_window later |
1072 | // SetUpDC(); | |
1073 | // m_owner = window; | |
e152afc3 RR |
1074 | } |
1075 | }; | |
1076 | ||
1077 | // --------------------------------------------------------- | |
1078 | // wxDataViewCustomCell | |
1079 | // --------------------------------------------------------- | |
1080 | ||
1081 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell, wxDataViewCell) | |
1082 | ||
1083 | wxDataViewCustomCell::wxDataViewCustomCell( const wxString &varianttype, | |
ad63bf41 | 1084 | wxDataViewCellMode mode, bool no_init ) : |
e152afc3 RR |
1085 | wxDataViewCell( varianttype, mode ) |
1086 | { | |
1087 | m_dc = NULL; | |
1088 | ||
ad63bf41 RR |
1089 | if (no_init) |
1090 | m_renderer = NULL; | |
1091 | else | |
1092 | Init(); | |
1093 | } | |
1094 | ||
1095 | bool wxDataViewCustomCell::Init() | |
1096 | { | |
e152afc3 RR |
1097 | GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new(); |
1098 | renderer->cell = this; | |
1099 | ||
1100 | m_renderer = (void*) renderer; | |
ad63bf41 | 1101 | |
553f7d8f RR |
1102 | if (m_mode & wxDATAVIEW_CELL_ACTIVATABLE) |
1103 | { | |
1104 | GValue gvalue = { 0, }; | |
1105 | g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() ); | |
1106 | g_value_set_enum( &gvalue, GTK_CELL_RENDERER_MODE_ACTIVATABLE ); | |
1107 | g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue ); | |
1108 | g_value_unset( &gvalue ); | |
1109 | } | |
1110 | ||
ad63bf41 | 1111 | return true; |
e152afc3 RR |
1112 | } |
1113 | ||
1114 | wxDataViewCustomCell::~wxDataViewCustomCell() | |
1115 | { | |
1116 | if (m_dc) | |
1117 | delete m_dc; | |
1118 | } | |
1119 | ||
1120 | wxDC *wxDataViewCustomCell::GetDC() | |
1121 | { | |
1122 | if (m_dc == NULL) | |
4d496ecb RR |
1123 | { |
1124 | if (GetOwner() == NULL) | |
1125 | return NULL; | |
1126 | if (GetOwner()->GetOwner() == NULL) | |
1127 | return NULL; | |
e152afc3 | 1128 | m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() ); |
4d496ecb | 1129 | } |
e152afc3 RR |
1130 | |
1131 | return m_dc; | |
1132 | } | |
1133 | ||
ad63bf41 RR |
1134 | // --------------------------------------------------------- |
1135 | // wxDataViewProgressCell | |
1136 | // --------------------------------------------------------- | |
1137 | ||
1138 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell, wxDataViewCustomCell) | |
1139 | ||
1140 | wxDataViewProgressCell::wxDataViewProgressCell( const wxString &label, | |
1141 | const wxString &varianttype, wxDataViewCellMode mode ) : | |
1142 | wxDataViewCustomCell( varianttype, mode, true ) | |
1143 | { | |
1144 | m_label = label; | |
1145 | m_value = 0; | |
1146 | ||
1147 | #ifdef __WXGTK26__ | |
1148 | if (!gtk_check_version(2,6,0)) | |
1149 | { | |
1150 | m_renderer = (void*) gtk_cell_renderer_progress_new(); | |
1151 | ||
1152 | GValue gvalue = { 0, }; | |
1153 | g_value_init( &gvalue, G_TYPE_STRING ); | |
1154 | g_value_set_boolean( &gvalue, wxGTK_CONV(m_label) ); | |
1155 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
1156 | g_value_unset( &gvalue ); | |
1157 | } | |
1158 | else | |
1159 | #endif | |
1160 | { | |
1161 | // Use custom cell code | |
1162 | wxDataViewCustomCell::Init(); | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | wxDataViewProgressCell::~wxDataViewProgressCell() | |
1167 | { | |
1168 | } | |
1169 | ||
1170 | bool wxDataViewProgressCell::SetValue( const wxVariant &value ) | |
1171 | { | |
1172 | #ifdef __WXGTK26__ | |
1173 | if (!gtk_check_version(2,6,0)) | |
1174 | { | |
1175 | gint tmp = (int) value; | |
1176 | GValue gvalue = { 0, }; | |
1177 | g_value_init( &gvalue, G_TYPE_INT ); | |
1178 | g_value_set_boolean( &gvalue, tmp ); | |
1179 | g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue ); | |
1180 | g_value_unset( &gvalue ); | |
1181 | } | |
1182 | else | |
1183 | #endif | |
1184 | { | |
1185 | m_value = (long) value; | |
1186 | ||
1187 | if (m_value < 0) m_value = 0; | |
1188 | if (m_value > 100) m_value = 100; | |
1189 | } | |
1190 | ||
1191 | return true; | |
1192 | } | |
1193 | ||
1194 | bool wxDataViewProgressCell::Render( wxRect cell, wxDC *dc, int state ) | |
1195 | { | |
1196 | double pct = (double)m_value / 100.0; | |
1197 | wxRect bar = cell; | |
1198 | bar.width = (int)(cell.width * pct); | |
1199 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
1200 | dc->SetBrush( *wxBLUE_BRUSH ); | |
1201 | dc->DrawRectangle( bar ); | |
1202 | ||
1203 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); | |
1204 | dc->SetPen( *wxBLACK_PEN ); | |
1205 | dc->DrawRectangle( cell ); | |
1206 | ||
1207 | return true; | |
1208 | } | |
1209 | ||
1210 | wxSize wxDataViewProgressCell::GetSize() | |
1211 | { | |
1212 | return wxSize(40,12); | |
1213 | } | |
1214 | ||
4d496ecb RR |
1215 | // --------------------------------------------------------- |
1216 | // wxDataViewDateCell | |
1217 | // --------------------------------------------------------- | |
1218 | ||
7ea3a0de RR |
1219 | class wxDataViewDateCellPopupTransient: public wxPopupTransientWindow |
1220 | { | |
1221 | public: | |
1222 | wxDataViewDateCellPopupTransient( wxWindow* parent, wxDateTime *value, | |
1223 | wxDataViewListModel *model, size_t col, size_t row ) : | |
1224 | wxPopupTransientWindow( parent, wxBORDER_SIMPLE ) | |
1225 | { | |
1226 | m_model = model; | |
1227 | m_col = col; | |
1228 | m_row = row; | |
1229 | m_cal = new wxCalendarCtrl( this, -1, *value ); | |
1230 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); | |
1231 | sizer->Add( m_cal, 1, wxGROW ); | |
1232 | SetSizer( sizer ); | |
1233 | sizer->Fit( this ); | |
1234 | } | |
1235 | ||
1236 | virtual void OnDismiss() | |
1237 | { | |
1238 | } | |
1239 | ||
1240 | void OnCalendar( wxCalendarEvent &event ); | |
1241 | ||
1242 | wxCalendarCtrl *m_cal; | |
1243 | wxDataViewListModel *m_model; | |
1244 | size_t m_col; | |
1245 | size_t m_row; | |
1246 | ||
1247 | private: | |
1248 | DECLARE_EVENT_TABLE() | |
1249 | }; | |
1250 | ||
1251 | BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient,wxPopupTransientWindow) | |
1252 | EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar ) | |
1253 | END_EVENT_TABLE() | |
1254 | ||
1255 | void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent &event ) | |
1256 | { | |
1257 | wxDateTime date = event.GetDate(); | |
1258 | wxVariant value = date; | |
1259 | m_model->SetValue( value, m_col, m_row ); | |
1260 | m_model->ValueChanged( m_col, m_row ); | |
1261 | DismissAndNotify(); | |
1262 | } | |
1263 | ||
4d496ecb RR |
1264 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell, wxDataViewCustomCell) |
1265 | ||
1266 | wxDataViewDateCell::wxDataViewDateCell( const wxString &varianttype, | |
1267 | wxDataViewCellMode mode ) : | |
1268 | wxDataViewCustomCell( varianttype, mode ) | |
1269 | { | |
1270 | } | |
1271 | ||
1272 | bool wxDataViewDateCell::SetValue( const wxVariant &value ) | |
1273 | { | |
1274 | m_date = value.GetDateTime(); | |
1275 | ||
1276 | return true; | |
1277 | } | |
1278 | ||
1279 | bool wxDataViewDateCell::Render( wxRect cell, wxDC *dc, int state ) | |
1280 | { | |
1281 | dc->SetFont( GetOwner()->GetOwner()->GetFont() ); | |
1282 | wxString tmp = m_date.FormatDate(); | |
1283 | dc->DrawText( tmp, cell.x, cell.y ); | |
1284 | ||
1285 | return true; | |
1286 | } | |
1287 | ||
1288 | wxSize wxDataViewDateCell::GetSize() | |
1289 | { | |
1290 | wxDataViewCtrl* view = GetOwner()->GetOwner(); | |
1291 | wxString tmp = m_date.FormatDate(); | |
1292 | wxCoord x,y,d; | |
1293 | view->GetTextExtent( tmp, &x, &y, &d ); | |
1294 | return wxSize(x,y+d); | |
1295 | } | |
1296 | ||
1297 | bool wxDataViewDateCell::Activate( wxRect cell, wxDataViewListModel *model, size_t col, size_t row ) | |
1298 | { | |
3f3af7e7 RR |
1299 | wxVariant variant; |
1300 | model->GetValue( variant, col, row ); | |
7ea3a0de RR |
1301 | wxDateTime value = variant.GetDateTime(); |
1302 | ||
1303 | wxDataViewDateCellPopupTransient *popup = new wxDataViewDateCellPopupTransient( | |
1304 | GetOwner()->GetOwner()->GetParent(), &value, model, col, row ); | |
1305 | wxPoint pos = wxGetMousePosition(); | |
1306 | popup->Move( pos ); | |
1307 | popup->Layout(); | |
1308 | popup->Popup( popup->m_cal ); | |
4d496ecb RR |
1309 | |
1310 | return true; | |
1311 | } | |
1312 | ||
605c2c4a RR |
1313 | // --------------------------------------------------------- |
1314 | // wxDataViewColumn | |
1315 | // --------------------------------------------------------- | |
7b4fde82 RR |
1316 | |
1317 | extern "C" { | |
1318 | static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, | |
1319 | GtkCellRenderer *cell, | |
1320 | GtkTreeModel *model, | |
1321 | GtkTreeIter *iter, | |
1322 | gpointer data ); | |
1323 | } | |
1324 | ||
1325 | ||
1326 | static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, | |
1327 | GtkCellRenderer *renderer, | |
1328 | GtkTreeModel *model, | |
1329 | GtkTreeIter *iter, | |
1330 | gpointer data ) | |
1331 | { | |
1332 | g_return_if_fail (GTK_IS_WX_LIST_STORE (model)); | |
1333 | GtkWxListStore *list_store = (GtkWxListStore *) model; | |
1334 | ||
1335 | wxDataViewCell *cell = (wxDataViewCell*) data; | |
a7f61f76 RR |
1336 | |
1337 | size_t model_row = (size_t) iter->user_data; | |
7b4fde82 | 1338 | |
3f3af7e7 RR |
1339 | wxVariant value; |
1340 | list_store->model->GetValue( value, cell->GetOwner()->GetModelColumn(), model_row ); | |
7b4fde82 RR |
1341 | |
1342 | if (value.GetType() != cell->GetVariantType()) | |
3f3af7e7 | 1343 | wxLogError( wxT("Wrong type\n") ); |
7b4fde82 RR |
1344 | |
1345 | cell->SetValue( value ); | |
1346 | } | |
1347 | ||
fa28826d RR |
1348 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase) |
1349 | ||
6842a71a RR |
1350 | wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewCell *cell, |
1351 | size_t model_column, int flags ) : | |
1352 | wxDataViewColumnBase( title, cell, model_column, flags ) | |
fa28826d | 1353 | { |
6842a71a | 1354 | GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle(); |
fa28826d | 1355 | |
6842a71a RR |
1356 | GtkTreeViewColumn *column = gtk_tree_view_column_new(); |
1357 | ||
1358 | gtk_tree_view_column_set_title( column, wxGTK_CONV(title) ); | |
fa28826d | 1359 | |
6842a71a RR |
1360 | gtk_tree_view_column_pack_start( column, renderer, TRUE ); |
1361 | ||
7b4fde82 RR |
1362 | gtk_tree_view_column_set_cell_data_func( column, renderer, |
1363 | wxGtkTreeCellDataFunc, (gpointer) cell, NULL ); | |
6842a71a | 1364 | |
fa28826d RR |
1365 | m_column = (void*) column; |
1366 | } | |
1367 | ||
1368 | wxDataViewColumn::~wxDataViewColumn() | |
1369 | { | |
1370 | } | |
1371 | ||
1372 | void wxDataViewColumn::SetTitle( const wxString &title ) | |
1373 | { | |
1374 | wxDataViewColumnBase::SetTitle( title ); | |
1375 | ||
1376 | GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column; | |
1377 | gtk_tree_view_column_set_title( column, wxGTK_CONV(title) ); | |
1378 | } | |
1379 | ||
790b137e RR |
1380 | //----------------------------------------------------------------------------- |
1381 | // wxDataViewCtrl | |
1382 | //----------------------------------------------------------------------------- | |
1383 | ||
239eaa41 RR |
1384 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) |
1385 | ||
1386 | wxDataViewCtrl::~wxDataViewCtrl() | |
1387 | { | |
8f850e28 RR |
1388 | if (m_notifier) |
1389 | GetModel()->RemoveNotifier( m_notifier ); | |
239eaa41 RR |
1390 | } |
1391 | ||
1392 | void wxDataViewCtrl::Init() | |
1393 | { | |
8f850e28 | 1394 | m_notifier = NULL; |
239eaa41 RR |
1395 | } |
1396 | ||
1397 | bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id, | |
1398 | const wxPoint& pos, const wxSize& size, | |
1399 | long style, const wxValidator& validator ) | |
1400 | { | |
1401 | Init(); | |
1402 | ||
1403 | m_needParent = TRUE; | |
1404 | m_acceptsFocus = TRUE; | |
1405 | ||
1406 | if (!PreCreation( parent, pos, size ) || | |
1407 | !CreateBase( parent, id, pos, size, style, validator )) | |
1408 | { | |
1409 | wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") ); | |
1410 | return FALSE; | |
1411 | } | |
1a367564 RR |
1412 | |
1413 | m_widget = gtk_scrolled_window_new (NULL, NULL); | |
1414 | gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (m_widget), GTK_SHADOW_IN); | |
1415 | ||
1416 | m_treeview = gtk_tree_view_new(); | |
1417 | gtk_container_add (GTK_CONTAINER (m_widget), m_treeview); | |
239eaa41 | 1418 | |
1a367564 RR |
1419 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget), |
1420 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
1421 | gtk_widget_show (m_treeview); | |
239eaa41 RR |
1422 | |
1423 | m_parent->DoAddChild( this ); | |
1424 | ||
1425 | PostCreation(size); | |
1426 | ||
1427 | return true; | |
1428 | } | |
1429 | ||
6e2e590f | 1430 | bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model ) |
239eaa41 RR |
1431 | { |
1432 | if (!wxDataViewCtrlBase::AssociateModel( model )) | |
1433 | return false; | |
1434 | ||
6e2e590f RR |
1435 | GtkWxListStore *gtk_store = wxgtk_list_store_new(); |
1436 | gtk_store->model = model; | |
1437 | ||
8f850e28 | 1438 | m_notifier = new wxGtkDataViewListModelNotifier( gtk_store, model ); |
6e2e590f | 1439 | |
8f850e28 | 1440 | model->AddNotifier( m_notifier ); |
1557c77b | 1441 | |
1a367564 RR |
1442 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_store) ); |
1443 | g_object_unref( gtk_store ); | |
239eaa41 RR |
1444 | |
1445 | return true; | |
1446 | } | |
790b137e | 1447 | |
fa28826d RR |
1448 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) |
1449 | { | |
1450 | if (!wxDataViewCtrlBase::AppendColumn(col)) | |
1451 | return false; | |
1452 | ||
1453 | GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle(); | |
1454 | ||
1a367564 | 1455 | gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column ); |
fa28826d RR |
1456 | |
1457 | return true; | |
1458 | } | |
1459 | ||
790b137e RR |
1460 | |
1461 | #endif // wxUSE_DATAVIEWCTRL | |
1462 |