]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
avoiding nesting dcs on the same window concurrently
[wxWidgets.git] / src / gtk / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 #if wxUSE_DATAVIEWCTRL
14
15 #include "wx/dataview.h"
16
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
18
19 #ifndef WX_PRECOMP
20 #include "wx/log.h"
21 #include "wx/dcclient.h"
22 #include "wx/sizer.h"
23 #endif
24
25 #include "wx/stockitem.h"
26 #include "wx/calctrl.h"
27 #include "wx/popupwin.h"
28 #include "wx/icon.h"
29
30
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/win_gtk.h"
33
34 #include <gobject/gvaluecollector.h>
35 #include <gtk/gtktreemodel.h>
36 #include <gtk/gtktreednd.h>
37
38 #include <gdk/gdkkeysyms.h>
39
40 //-----------------------------------------------------------------------------
41 // classes
42 //-----------------------------------------------------------------------------
43
44 class wxDataViewCtrl;
45
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 {
81 GObjectClass list_parent_class;
82 };
83
84 static GtkWxListStore *wxgtk_list_store_new (void);
85 static void wxgtk_list_store_init (GtkWxListStore *list_store);
86 static void wxgtk_list_store_class_init (GtkWxListStoreClass *klass);
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,
92 gint index);
93 static gboolean wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
94 GtkTreeIter *iter,
95 GtkTreePath *path);
96 static GtkTreePath *wxgtk_list_store_get_path (GtkTreeModel *tree_model,
97 GtkTreeIter *iter);
98 static void wxgtk_list_store_get_value (GtkTreeModel *tree_model,
99 GtkTreeIter *iter,
100 gint column,
101 GValue *value);
102 static gboolean wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
103 GtkTreeIter *iter);
104 static gboolean wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
105 GtkTreeIter *iter,
106 GtkTreeIter *parent);
107 static gboolean wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
108 GtkTreeIter *iter);
109 static gint wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
110 GtkTreeIter *iter);
111 static gboolean wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
112 GtkTreeIter *iter,
113 GtkTreeIter *parent,
114 gint n);
115 static gboolean wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
116 GtkTreeIter *iter,
117 GtkTreeIter *child);
118
119 static GObjectClass *list_parent_class = NULL;
120
121 GType
122 gtk_wx_list_store_get_type (void)
123 {
124 static GType list_store_type = 0;
125
126 if (!list_store_type)
127 {
128 static const GTypeInfo list_store_info =
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 };
140
141 static const GInterfaceInfo tree_model_info =
142 {
143 (GInterfaceInitFunc) wxgtk_list_store_tree_model_init,
144 NULL,
145 NULL
146 };
147
148 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxListStore",
149 &list_store_info, (GTypeFlags)0 );
150
151 g_type_add_interface_static (list_store_type,
152 GTK_TYPE_TREE_MODEL,
153 &tree_model_info);
154 }
155
156 return list_store_type;
157 }
158
159 static GtkWxListStore *
160 wxgtk_list_store_new(void)
161 {
162 GtkWxListStore *retval = (GtkWxListStore *) g_object_new (GTK_TYPE_WX_LIST_STORE, NULL);
163 return retval;
164 }
165
166 static void
167 wxgtk_list_store_class_init (GtkWxListStoreClass *klass)
168 {
169 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
170 GObjectClass *object_class = (GObjectClass*) klass;
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;
195 list_store->stamp = g_random_int();
196 }
197
198 static void
199 wxgtk_list_store_finalize (GObject *object)
200 {
201 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
202
203 /* we need to sort out, which class deletes what */
204 /* delete list_store->model; */
205
206 /* must chain up */
207 (* list_parent_class->finalize) (object);
208 }
209
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 {
220 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), (GtkTreeModelFlags)0 );
221
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;
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
238 return list_store->model->GetNumberOfCols();
239 }
240
241 static GType
242 wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
243 gint index)
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
248 GType gtype = G_TYPE_INVALID;
249
250 wxString wxtype = list_store->model->GetColType( (unsigned int) index );
251
252 if (wxtype == wxT("string"))
253 gtype = G_TYPE_STRING;
254
255 return gtype;
256 }
257
258 static gboolean
259 wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
260 GtkTreeIter *iter,
261 GtkTreePath *path)
262 {
263 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
264 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
265 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
266
267 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
268
269 if (i >= list_store->model->GetNumberOfRows())
270 return FALSE;
271
272 iter->stamp = list_store->stamp;
273 // user_data is just the index
274 iter->user_data = (gpointer) i;
275
276 return TRUE;
277 }
278
279 static GtkTreePath *
280 wxgtk_list_store_get_path (GtkTreeModel *tree_model,
281 GtkTreeIter *iter)
282 {
283 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), NULL);
284 g_return_val_if_fail (iter->stamp == GTK_WX_LIST_STORE (tree_model)->stamp, NULL);
285
286 GtkTreePath *retval = gtk_tree_path_new ();
287 // user_data is just the index
288 int i = (wxUIntPtr) iter->user_data;
289 gtk_tree_path_append_index (retval, i);
290 return retval;
291 }
292
293 static void
294 wxgtk_list_store_get_value (GtkTreeModel *tree_model,
295 GtkTreeIter *iter,
296 gint column,
297 GValue *value)
298 {
299 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
300 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model) );
301
302 wxDataViewListModel *model = list_store->model;
303 wxString mtype = model->GetColType( (unsigned int) column );
304 if (mtype == wxT("string"))
305 {
306 wxVariant variant;
307 g_value_init( value, G_TYPE_STRING );
308 model->GetValue( variant, (unsigned int) column, (unsigned int) iter->user_data );
309 g_value_set_string( value, wxGTK_CONV(variant.GetString()) );
310 }
311 else
312 {
313 }
314
315 #if 0
316 GtkTreeDataList *list;
317 gint tmp_column = column;
318
319 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
320 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
321
322 list = G_SLIST (iter->user_data)->data;
323
324 while (tmp_column-- > 0 && list)
325 list = list->next;
326
327 if (list == NULL)
328 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
329 else
330 _gtk_tree_data_list_node_to_value (list,
331 GTK_LIST_STORE (tree_model)->column_headers[column],
332 value);
333 #endif
334
335 }
336
337 static gboolean
338 wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
339 GtkTreeIter *iter)
340 {
341 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
342 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
343
344 g_return_val_if_fail (list_store->stamp == iter->stamp, FALSE);
345
346 int n = (wxUIntPtr) iter->user_data;
347
348 if (n == -1)
349 return FALSE;
350
351 if (n >= (int) list_store->model->GetNumberOfRows()-1)
352 return FALSE;
353
354 iter->user_data = (gpointer) ++n;
355
356 return TRUE;
357 }
358
359 static gboolean
360 wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
361 GtkTreeIter *iter,
362 GtkTreeIter *parent)
363 {
364 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
365 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
366
367 // this is a list, nodes have no children
368 if (parent)
369 return FALSE;
370
371 iter->stamp = list_store->stamp;
372 iter->user_data = (gpointer) -1;
373
374 return TRUE;
375 }
376
377 static gboolean
378 wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
379 GtkTreeIter *iter)
380 {
381 return FALSE;
382 }
383
384 static gint
385 wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
386 GtkTreeIter *iter)
387 {
388 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), -1);
389 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
390
391 if (iter == NULL)
392 return (gint) list_store->model->GetNumberOfRows();
393
394 g_return_val_if_fail (list_store->stamp == iter->stamp, -1);
395
396 return 0;
397 }
398
399 static gboolean
400 wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
401 GtkTreeIter *iter,
402 GtkTreeIter *parent,
403 gint n)
404 {
405 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
406 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
407
408 if (parent)
409 return FALSE;
410
411 if (n < 0)
412 return FALSE;
413
414 if (n >= (gint) list_store->model->GetNumberOfRows())
415 return FALSE;
416
417 iter->stamp = list_store->stamp;
418 iter->user_data = (gpointer) n;
419
420 return TRUE;
421 }
422
423 static gboolean
424 wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
425 GtkTreeIter *iter,
426 GtkTreeIter *child)
427 {
428 return FALSE;
429 }
430
431 //-----------------------------------------------------------------------------
432 // define new GTK+ class wxGtkRendererRenderer
433 //-----------------------------------------------------------------------------
434
435 extern "C" {
436
437 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
438 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
439 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
440 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
441 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
442 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
443
444 GType gtk_wx_cell_renderer_get_type (void);
445
446 typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
447 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
448
449 struct _GtkWxCellRenderer
450 {
451 GtkCellRenderer parent;
452
453 /*< private >*/
454 wxDataViewCustomRenderer *cell;
455 guint32 last_click;
456 };
457
458 struct _GtkWxCellRendererClass
459 {
460 GtkCellRendererClass cell_parent_class;
461 };
462
463
464 static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
465 static void gtk_wx_cell_renderer_init (
466 GtkWxCellRenderer *cell );
467 static void gtk_wx_cell_renderer_class_init(
468 GtkWxCellRendererClass *klass );
469 static void gtk_wx_cell_renderer_finalize (
470 GObject *object );
471 static void gtk_wx_cell_renderer_get_size (
472 GtkCellRenderer *cell,
473 GtkWidget *widget,
474 GdkRectangle *rectangle,
475 gint *x_offset,
476 gint *y_offset,
477 gint *width,
478 gint *height );
479 static void gtk_wx_cell_renderer_render (
480 GtkCellRenderer *cell,
481 GdkWindow *window,
482 GtkWidget *widget,
483 GdkRectangle *background_area,
484 GdkRectangle *cell_area,
485 GdkRectangle *expose_area,
486 GtkCellRendererState flags );
487 static gboolean gtk_wx_cell_renderer_activate(
488 GtkCellRenderer *cell,
489 GdkEvent *event,
490 GtkWidget *widget,
491 const gchar *path,
492 GdkRectangle *background_area,
493 GdkRectangle *cell_area,
494 GtkCellRendererState flags );
495
496 static GObjectClass *cell_parent_class = NULL;
497
498 } // extern "C"
499
500 GType
501 gtk_wx_cell_renderer_get_type (void)
502 {
503 static GType cell_wx_type = 0;
504
505 if (!cell_wx_type)
506 {
507 static const GTypeInfo cell_wx_info =
508 {
509 sizeof (GtkWxCellRendererClass),
510 NULL, /* base_init */
511 NULL, /* base_finalize */
512 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
513 NULL, /* class_finalize */
514 NULL, /* class_data */
515 sizeof (GtkWxCellRenderer),
516 0, /* n_preallocs */
517 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
518 };
519
520 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
521 "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
522 }
523
524 return cell_wx_type;
525 }
526
527 static void
528 gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
529 {
530 cell->cell = NULL;
531 cell->last_click = 0;
532 }
533
534 static void
535 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
536 {
537 GObjectClass *object_class = G_OBJECT_CLASS (klass);
538 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
539
540 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
541
542 object_class->finalize = gtk_wx_cell_renderer_finalize;
543
544 cell_class->get_size = gtk_wx_cell_renderer_get_size;
545 cell_class->render = gtk_wx_cell_renderer_render;
546 cell_class->activate = gtk_wx_cell_renderer_activate;
547 }
548
549 static void
550 gtk_wx_cell_renderer_finalize (GObject *object)
551 {
552 /* must chain up */
553 (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
554 }
555
556 GtkCellRenderer*
557 gtk_wx_cell_renderer_new (void)
558 {
559 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
560 }
561
562 static void
563 gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
564 GtkWidget *widget,
565 GdkRectangle *cell_area,
566 gint *x_offset,
567 gint *y_offset,
568 gint *width,
569 gint *height)
570 {
571 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
572 wxDataViewCustomRenderer *cell = wxrenderer->cell;
573
574 wxSize size = cell->GetSize();
575
576 gint calc_width = (gint) renderer->xpad * 2 + size.x;
577 gint calc_height = (gint) renderer->ypad * 2 + size.y;
578
579 if (x_offset)
580 *x_offset = 0;
581 if (y_offset)
582 *y_offset = 0;
583
584 if (cell_area && size.x > 0 && size.y > 0)
585 {
586 if (x_offset)
587 {
588 *x_offset = (gint)((renderer->xalign *
589 (cell_area->width - calc_width - 2 * renderer->xpad)));
590 *x_offset = MAX (*x_offset, 0) + renderer->xpad;
591 }
592 if (y_offset)
593 {
594 *y_offset = (gint)((renderer->yalign *
595 (cell_area->height - calc_height - 2 * renderer->ypad)));
596 *y_offset = MAX (*y_offset, 0) + renderer->ypad;
597 }
598 }
599
600 if (width)
601 *width = calc_width;
602
603 if (height)
604 *height = calc_height;
605 }
606
607 static void
608 gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
609 GdkWindow *window,
610 GtkWidget *widget,
611 GdkRectangle *background_area,
612 GdkRectangle *cell_area,
613 GdkRectangle *expose_area,
614 GtkCellRendererState flags)
615
616 {
617 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
618 wxDataViewCustomRenderer *cell = wxrenderer->cell;
619
620 GdkRectangle rect;
621 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
622 &rect.x,
623 &rect.y,
624 &rect.width,
625 &rect.height);
626
627 rect.x += cell_area->x;
628 rect.y += cell_area->y;
629 rect.width -= renderer->xpad * 2;
630 rect.height -= renderer->ypad * 2;
631
632 GdkRectangle dummy;
633 if (gdk_rectangle_intersect (expose_area, &rect, &dummy))
634 {
635 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
636 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
637 if (dc->m_window == NULL)
638 {
639 dc->m_window = window;
640 dc->SetUpDC();
641 }
642
643 int state = 0;
644 if (flags & GTK_CELL_RENDERER_SELECTED)
645 state |= wxDATAVIEW_CELL_SELECTED;
646 if (flags & GTK_CELL_RENDERER_PRELIT)
647 state |= wxDATAVIEW_CELL_PRELIT;
648 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
649 state |= wxDATAVIEW_CELL_INSENSITIVE;
650 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
651 state |= wxDATAVIEW_CELL_INSENSITIVE;
652 if (flags & GTK_CELL_RENDERER_FOCUSED)
653 state |= wxDATAVIEW_CELL_FOCUSED;
654 cell->Render( renderrect, dc, state );
655 }
656 }
657
658 static gboolean
659 gtk_wx_cell_renderer_activate(
660 GtkCellRenderer *renderer,
661 GdkEvent *event,
662 GtkWidget *widget,
663 const gchar *path,
664 GdkRectangle *background_area,
665 GdkRectangle *cell_area,
666 GtkCellRendererState flags )
667 {
668 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
669 wxDataViewCustomRenderer *cell = wxrenderer->cell;
670
671 GdkRectangle rect;
672 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
673 &rect.x,
674 &rect.y,
675 &rect.width,
676 &rect.height);
677
678 rect.x += cell_area->x;
679 rect.y += cell_area->y;
680 rect.width -= renderer->xpad * 2;
681 rect.height -= renderer->ypad * 2;
682
683 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
684
685 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
686
687 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
688 unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (treepath)[0];
689 gtk_tree_path_free( treepath );
690
691 unsigned int model_col = cell->GetOwner()->GetModelColumn();
692
693 if (event->type == GDK_BUTTON_PRESS)
694 {
695 GdkEventButton *button_event = (GdkEventButton*) event;
696 wxPoint pt( ((int) button_event->x) - renderrect.x,
697 ((int) button_event->y) - renderrect.y );
698
699 bool ret = false;
700 if (button_event->button == 1)
701 {
702 if (cell->LeftClick( pt, renderrect, model, model_col, model_row ))
703 ret = true;
704 // TODO: query system double-click time
705 if (button_event->time - wxrenderer->last_click < 400)
706 if (cell->Activate( renderrect, model, model_col, model_row ))
707 ret = true;
708 }
709 if (button_event->button == 3)
710 {
711 if (cell->RightClick( pt, renderrect, model, model_col, model_row ))
712 ret = true;
713 }
714
715 wxrenderer->last_click = button_event->time;
716
717 return ret;
718 }
719
720 return false;
721 }
722
723 // ---------------------------------------------------------
724 // wxGtkDataViewListModelNotifier
725 // ---------------------------------------------------------
726
727 class wxGtkDataViewListModelNotifier: public wxDataViewListModelNotifier
728 {
729 public:
730 wxGtkDataViewListModelNotifier( GtkWxListStore* gtk_store, wxDataViewListModel *wx_model );
731
732 virtual bool RowAppended();
733 virtual bool RowPrepended();
734 virtual bool RowInserted( unsigned int before );
735 virtual bool RowDeleted( unsigned int row );
736 virtual bool RowChanged( unsigned int row );
737 virtual bool ValueChanged( unsigned int col, unsigned int row );
738 virtual bool RowsReordered( unsigned int *new_order );
739 virtual bool Cleared();
740
741 GtkWxListStore *m_gtk_store;
742 wxDataViewListModel *m_wx_model;
743 };
744
745 // ---------------------------------------------------------
746 // wxGtkDataViewListModelNotifier
747 // ---------------------------------------------------------
748
749 wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
750 GtkWxListStore* gtk_store, wxDataViewListModel *wx_model )
751 {
752 m_gtk_store = gtk_store;
753 m_wx_model = wx_model;
754 }
755
756 bool wxGtkDataViewListModelNotifier::RowAppended()
757 {
758 unsigned int pos = m_wx_model->GetNumberOfRows()-1;
759
760 GtkTreeIter iter;
761 iter.stamp = m_gtk_store->stamp;
762 iter.user_data = (gpointer) pos;
763
764 GtkTreePath *path = gtk_tree_path_new ();
765 gtk_tree_path_append_index (path, (gint) pos);
766 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
767 gtk_tree_path_free (path);
768
769 return true;
770 }
771
772 bool wxGtkDataViewListModelNotifier::RowPrepended()
773 {
774 GtkTreeIter iter;
775 iter.stamp = m_gtk_store->stamp;
776 iter.user_data = (gpointer) 0;
777
778 GtkTreePath *path = gtk_tree_path_new ();
779 gtk_tree_path_append_index (path, (gint) 0);
780 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
781 gtk_tree_path_free (path);
782
783 return true;
784 }
785
786 bool wxGtkDataViewListModelNotifier::RowInserted( unsigned int before )
787 {
788 GtkTreeIter iter;
789 iter.stamp = m_gtk_store->stamp;
790 iter.user_data = (gpointer) before;
791
792 GtkTreePath *path = gtk_tree_path_new ();
793 gtk_tree_path_append_index (path, (gint) before);
794 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
795 gtk_tree_path_free (path);
796
797 return true;
798 }
799
800 bool wxGtkDataViewListModelNotifier::RowDeleted( unsigned int row )
801 {
802 GtkTreePath *path = gtk_tree_path_new ();
803 gtk_tree_path_append_index (path, (gint) row);
804 gtk_tree_model_row_deleted (GTK_TREE_MODEL (m_gtk_store), path);
805 gtk_tree_path_free (path);
806
807 return true;
808 }
809
810 bool wxGtkDataViewListModelNotifier::RowChanged( unsigned int row )
811 {
812 GtkTreeIter iter;
813 iter.stamp = m_gtk_store->stamp;
814 iter.user_data = (gpointer) row;
815 GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store), &iter);
816 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store), path, &iter);
817 gtk_tree_path_free (path);
818
819 return true;
820 }
821
822 bool wxGtkDataViewListModelNotifier::ValueChanged( unsigned int model_col, unsigned int model_row )
823 {
824 // This adds GTK+'s missing MVC logic for ValueChanged
825 wxNode *node = GetOwner()->m_viewingColumns.GetFirst();
826 while (node)
827 {
828 wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData();
829 if (viewing_column->m_modelColumn == model_col)
830 {
831 GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview);
832 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(viewing_column->m_viewColumn->GetGtkHandle());
833
834 // Get cell area
835 GtkTreePath *path = gtk_tree_path_new();
836 gtk_tree_path_append_index( path, model_row );
837 GdkRectangle cell_area;
838 gtk_tree_view_get_cell_area( widget, path, column, &cell_area );
839 gtk_tree_path_free( path );
840
841 int ydiff = column->button->allocation.height;
842 // Redraw
843 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
844 cell_area.x, ydiff + cell_area.y, cell_area.width, cell_area.height );
845 }
846
847 node = node->GetNext();
848 }
849
850 return true;
851 }
852
853 bool wxGtkDataViewListModelNotifier::RowsReordered( unsigned int *new_order )
854 {
855 // Assume sizeof(unsigned int)= == sizeof(gint)
856
857 GtkTreePath *path = gtk_tree_path_new ();
858 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store), path, NULL, (gint*)new_order);
859 gtk_tree_path_free (path);
860
861 // This adds GTK+'s missing MVC logic for RowsReordered
862 wxNode *node = GetOwner()->m_viewingColumns.GetFirst();
863 while (node)
864 {
865 wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData();
866 GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview);
867 // Doesn't work yet...
868 gtk_widget_queue_draw( GTK_WIDGET(widget) );
869
870 node = node->GetNext();
871 }
872
873 return true;
874 }
875
876 bool wxGtkDataViewListModelNotifier::Cleared()
877 {
878 return false;
879 }
880
881 // ---------------------------------------------------------
882 // wxDataViewRenderer
883 // ---------------------------------------------------------
884
885 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
886
887 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode ) :
888 wxDataViewRendererBase( varianttype, mode )
889 {
890 m_renderer = NULL;
891 }
892
893 // ---------------------------------------------------------
894 // wxDataViewTextRenderer
895 // ---------------------------------------------------------
896
897 extern "C" {
898 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
899 gchar *arg1, gchar *arg2, gpointer user_data );
900 }
901
902 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
903 gchar *arg1, gchar *arg2, gpointer user_data )
904 {
905 wxDataViewTextRenderer *cell = (wxDataViewTextRenderer*) user_data;
906
907 wxString tmp = wxGTK_CONV_BACK( arg2 );
908 wxVariant value = tmp;
909 if (!cell->Validate( value ))
910 return;
911
912 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
913
914 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
915 unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (path)[0];
916 gtk_tree_path_free( path );
917
918 unsigned int model_col = cell->GetOwner()->GetModelColumn();
919
920 model->SetValue( value, model_col, model_row );
921 model->ValueChanged( model_col, model_row );
922 }
923
924 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
925
926 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode ) :
927 wxDataViewRenderer( varianttype, mode )
928 {
929 m_renderer = (void*) gtk_cell_renderer_text_new();
930
931 if (m_mode & wxDATAVIEW_CELL_EDITABLE)
932 {
933 GValue gvalue = { 0, };
934 g_value_init( &gvalue, G_TYPE_BOOLEAN );
935 g_value_set_boolean( &gvalue, true );
936 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
937 g_value_unset( &gvalue );
938
939 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
940 }
941 }
942
943 bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
944 {
945 wxString tmp = value;
946
947 GValue gvalue = { 0, };
948 g_value_init( &gvalue, G_TYPE_STRING );
949 g_value_set_string( &gvalue, wxGTK_CONV( tmp ) );
950 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
951 g_value_unset( &gvalue );
952
953 return true;
954 }
955
956 bool wxDataViewTextRenderer::GetValue( wxVariant &value )
957 {
958 GValue gvalue = { 0, };
959 g_value_init( &gvalue, G_TYPE_STRING );
960 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
961 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &gvalue ) );
962 g_value_unset( &gvalue );
963
964 value = tmp;
965
966 return true;
967 }
968
969 // ---------------------------------------------------------
970 // wxDataViewBitmapRenderer
971 // ---------------------------------------------------------
972
973 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
974
975 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode ) :
976 wxDataViewRenderer( varianttype, mode )
977 {
978 m_renderer = (void*) gtk_cell_renderer_pixbuf_new();
979 }
980
981 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
982 {
983 if (value.GetType() == wxT("wxBitmap"))
984 {
985 wxBitmap bitmap;
986 bitmap << value;
987
988 // This may create a Pixbuf representation in the
989 // wxBitmap object (and it will stay there)
990 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
991
992 GValue gvalue = { 0, };
993 g_value_init( &gvalue, G_TYPE_OBJECT );
994 g_value_set_object( &gvalue, pixbuf );
995 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
996 g_value_unset( &gvalue );
997
998 return true;
999 }
1000
1001 if (value.GetType() == wxT("wxIcon"))
1002 {
1003 wxIcon bitmap;
1004 bitmap << value;
1005
1006 // This may create a Pixbuf representation in the
1007 // wxBitmap object (and it will stay there)
1008 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
1009
1010 GValue gvalue = { 0, };
1011 g_value_init( &gvalue, G_TYPE_OBJECT );
1012 g_value_set_object( &gvalue, pixbuf );
1013 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1014 g_value_unset( &gvalue );
1015
1016 return true;
1017 }
1018
1019 return false;
1020 }
1021
1022 bool wxDataViewBitmapRenderer::GetValue( wxVariant &value )
1023 {
1024 return false;
1025 }
1026
1027 // ---------------------------------------------------------
1028 // wxDataViewToggleRenderer
1029 // ---------------------------------------------------------
1030
1031 extern "C" {
1032 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
1033 gchar *path, gpointer user_data );
1034 }
1035
1036 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
1037 gchar *path, gpointer user_data )
1038 {
1039 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
1040
1041 // get old value
1042 GValue gvalue = { 0, };
1043 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1044 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
1045 bool tmp = g_value_get_boolean( &gvalue );
1046 g_value_unset( &gvalue );
1047 // invert it
1048 tmp = !tmp;
1049
1050 wxVariant value = tmp;
1051 if (!cell->Validate( value ))
1052 return;
1053
1054 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
1055
1056 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
1057 unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (gtk_path)[0];
1058 gtk_tree_path_free( gtk_path );
1059
1060 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1061
1062 model->SetValue( value, model_col, model_row );
1063 model->ValueChanged( model_col, model_row );
1064 }
1065
1066 IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
1067
1068 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
1069 wxDataViewCellMode mode ) :
1070 wxDataViewRenderer( varianttype, mode )
1071 {
1072 m_renderer = (void*) gtk_cell_renderer_toggle_new();
1073
1074 if (m_mode & wxDATAVIEW_CELL_ACTIVATABLE)
1075 {
1076 g_signal_connect_after( m_renderer, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
1077 }
1078 else
1079 {
1080
1081 GValue gvalue = { 0, };
1082 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1083 g_value_set_boolean( &gvalue, false );
1084 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
1085 g_value_unset( &gvalue );
1086
1087 GValue gvalue2 = { 0, };
1088 g_value_init( &gvalue2, gtk_cell_renderer_mode_get_type() );
1089 g_value_set_enum( &gvalue2, GTK_CELL_RENDERER_MODE_INERT );
1090 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue2 );
1091 g_value_unset( &gvalue2 );
1092
1093 }
1094 }
1095
1096 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
1097 {
1098 bool tmp = value;
1099
1100 GValue gvalue = { 0, };
1101 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1102 g_value_set_boolean( &gvalue, tmp );
1103 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
1104 g_value_unset( &gvalue );
1105
1106 return true;
1107 }
1108
1109 bool wxDataViewToggleRenderer::GetValue( wxVariant &value )
1110 {
1111 GValue gvalue = { 0, };
1112 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1113 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
1114 bool tmp = g_value_get_boolean( &gvalue );
1115 g_value_unset( &gvalue );
1116
1117 value = tmp;
1118
1119 return true;
1120 }
1121
1122 // ---------------------------------------------------------
1123 // wxDataViewCustomRenderer
1124 // ---------------------------------------------------------
1125
1126 class wxDataViewCtrlDC: public wxWindowDC
1127 {
1128 public:
1129 wxDataViewCtrlDC( wxDataViewCtrl *window )
1130 {
1131 GtkWidget *widget = window->m_treeview;
1132 // Set later
1133 m_window = NULL;
1134
1135 m_context = window->GtkGetPangoDefaultContext();
1136 m_layout = pango_layout_new( m_context );
1137 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
1138
1139 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
1140
1141 // Set m_window later
1142 // SetUpDC();
1143 // m_owner = window;
1144 }
1145 };
1146
1147 // ---------------------------------------------------------
1148 // wxDataViewCustomRenderer
1149 // ---------------------------------------------------------
1150
1151 IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
1152
1153 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
1154 wxDataViewCellMode mode, bool no_init ) :
1155 wxDataViewRenderer( varianttype, mode )
1156 {
1157 m_dc = NULL;
1158
1159 if (no_init)
1160 m_renderer = NULL;
1161 else
1162 Init();
1163 }
1164
1165 bool wxDataViewCustomRenderer::Init()
1166 {
1167 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
1168 renderer->cell = this;
1169
1170 m_renderer = (void*) renderer;
1171
1172 if (m_mode & wxDATAVIEW_CELL_ACTIVATABLE)
1173 {
1174 GValue gvalue = { 0, };
1175 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1176 g_value_set_enum( &gvalue, GTK_CELL_RENDERER_MODE_ACTIVATABLE );
1177 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1178 g_value_unset( &gvalue );
1179 }
1180
1181 return true;
1182 }
1183
1184 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1185 {
1186 if (m_dc)
1187 delete m_dc;
1188 }
1189
1190 wxDC *wxDataViewCustomRenderer::GetDC()
1191 {
1192 if (m_dc == NULL)
1193 {
1194 if (GetOwner() == NULL)
1195 return NULL;
1196 if (GetOwner()->GetOwner() == NULL)
1197 return NULL;
1198 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1199 }
1200
1201 return m_dc;
1202 }
1203
1204 // ---------------------------------------------------------
1205 // wxDataViewProgressRenderer
1206 // ---------------------------------------------------------
1207
1208 IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
1209
1210 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
1211 const wxString &varianttype, wxDataViewCellMode mode ) :
1212 wxDataViewCustomRenderer( varianttype, mode, true )
1213 {
1214 m_label = label;
1215 m_value = 0;
1216
1217 #ifdef __WXGTK26__
1218 if (!gtk_check_version(2,6,0))
1219 {
1220 m_renderer = (void*) gtk_cell_renderer_progress_new();
1221
1222 GValue gvalue = { 0, };
1223 g_value_init( &gvalue, G_TYPE_STRING );
1224 g_value_set_boolean( &gvalue, wxGTK_CONV(m_label) );
1225 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1226 g_value_unset( &gvalue );
1227 }
1228 else
1229 #endif
1230 {
1231 // Use custom cell code
1232 wxDataViewCustomRenderer::Init();
1233 }
1234 }
1235
1236 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1237 {
1238 }
1239
1240 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
1241 {
1242 #ifdef __WXGTK26__
1243 if (!gtk_check_version(2,6,0))
1244 {
1245 gint tmp = (int) value;
1246 GValue gvalue = { 0, };
1247 g_value_init( &gvalue, G_TYPE_INT );
1248 g_value_set_boolean( &gvalue, tmp );
1249 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
1250 g_value_unset( &gvalue );
1251 }
1252 else
1253 #endif
1254 {
1255 m_value = (long) value;
1256
1257 if (m_value < 0) m_value = 0;
1258 if (m_value > 100) m_value = 100;
1259 }
1260
1261 return true;
1262 }
1263
1264 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int state )
1265 {
1266 double pct = (double)m_value / 100.0;
1267 wxRect bar = cell;
1268 bar.width = (int)(cell.width * pct);
1269 dc->SetPen( *wxTRANSPARENT_PEN );
1270 dc->SetBrush( *wxBLUE_BRUSH );
1271 dc->DrawRectangle( bar );
1272
1273 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1274 dc->SetPen( *wxBLACK_PEN );
1275 dc->DrawRectangle( cell );
1276
1277 return true;
1278 }
1279
1280 wxSize wxDataViewProgressRenderer::GetSize()
1281 {
1282 return wxSize(40,12);
1283 }
1284
1285 // ---------------------------------------------------------
1286 // wxDataViewDateRenderer
1287 // ---------------------------------------------------------
1288
1289 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
1290 {
1291 public:
1292 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
1293 wxDataViewListModel *model, unsigned int col, unsigned int row ) :
1294 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
1295 {
1296 m_model = model;
1297 m_col = col;
1298 m_row = row;
1299 m_cal = new wxCalendarCtrl( this, -1, *value );
1300 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
1301 sizer->Add( m_cal, 1, wxGROW );
1302 SetSizer( sizer );
1303 sizer->Fit( this );
1304 }
1305
1306 virtual void OnDismiss()
1307 {
1308 }
1309
1310 void OnCalendar( wxCalendarEvent &event );
1311
1312 wxCalendarCtrl *m_cal;
1313 wxDataViewListModel *m_model;
1314 unsigned int m_col;
1315 unsigned int m_row;
1316
1317 private:
1318 DECLARE_EVENT_TABLE()
1319 };
1320
1321 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
1322 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
1323 END_EVENT_TABLE()
1324
1325 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
1326 {
1327 wxDateTime date = event.GetDate();
1328 wxVariant value = date;
1329 m_model->SetValue( value, m_col, m_row );
1330 m_model->ValueChanged( m_col, m_row );
1331 DismissAndNotify();
1332 }
1333
1334 IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
1335
1336 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
1337 wxDataViewCellMode mode ) :
1338 wxDataViewCustomRenderer( varianttype, mode )
1339 {
1340 }
1341
1342 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
1343 {
1344 m_date = value.GetDateTime();
1345
1346 return true;
1347 }
1348
1349 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
1350 {
1351 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
1352 wxString tmp = m_date.FormatDate();
1353 dc->DrawText( tmp, cell.x, cell.y );
1354
1355 return true;
1356 }
1357
1358 wxSize wxDataViewDateRenderer::GetSize()
1359 {
1360 wxDataViewCtrl* view = GetOwner()->GetOwner();
1361 wxString tmp = m_date.FormatDate();
1362 wxCoord x,y,d;
1363 view->GetTextExtent( tmp, &x, &y, &d );
1364 return wxSize(x,y+d);
1365 }
1366
1367 bool wxDataViewDateRenderer::Activate( wxRect cell, wxDataViewListModel *model, unsigned int col, unsigned int row )
1368 {
1369 wxVariant variant;
1370 model->GetValue( variant, col, row );
1371 wxDateTime value = variant.GetDateTime();
1372
1373 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
1374 GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
1375 wxPoint pos = wxGetMousePosition();
1376 popup->Move( pos );
1377 popup->Layout();
1378 popup->Popup( popup->m_cal );
1379
1380 return true;
1381 }
1382
1383 // ---------------------------------------------------------
1384 // wxDataViewColumn
1385 // ---------------------------------------------------------
1386
1387
1388 static gboolean
1389 gtk_dataview_header_button_press_callback( GtkWidget *widget,
1390 GdkEventButton *gdk_event,
1391 wxDataViewColumn *column )
1392 {
1393 if (gdk_event->type != GDK_BUTTON_PRESS)
1394 return TRUE;
1395
1396 if (gdk_event->button == 1)
1397 {
1398 wxDataViewCtrl *dv = column->GetOwner();
1399 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
1400 event.SetDataViewColumn( column );
1401 event.SetModel( dv->GetModel() );
1402 dv->GetEventHandler()->ProcessEvent( event );
1403 }
1404
1405 return TRUE;
1406 }
1407
1408 extern "C" {
1409 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1410 GtkCellRenderer *cell,
1411 GtkTreeModel *model,
1412 GtkTreeIter *iter,
1413 gpointer data );
1414 }
1415
1416
1417 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1418 GtkCellRenderer *renderer,
1419 GtkTreeModel *model,
1420 GtkTreeIter *iter,
1421 gpointer data )
1422 {
1423 g_return_if_fail (GTK_IS_WX_LIST_STORE (model));
1424 GtkWxListStore *list_store = (GtkWxListStore *) model;
1425
1426 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
1427
1428 unsigned int model_row = (unsigned int) iter->user_data;
1429
1430 wxVariant value;
1431 list_store->model->GetValue( value, cell->GetOwner()->GetModelColumn(), model_row );
1432
1433 if (value.GetType() != cell->GetVariantType())
1434 wxLogError( wxT("Wrong type, required: %s but: %s"),
1435 value.GetType().c_str(),
1436 cell->GetVariantType().c_str() );
1437
1438 cell->SetValue( value );
1439 }
1440
1441 IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
1442
1443 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell, unsigned int model_column,
1444 int width, int flags ) :
1445 wxDataViewColumnBase( title, cell, model_column, width, flags )
1446 {
1447 m_isConnected = false;
1448
1449 GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle();
1450
1451 GtkTreeViewColumn *column = gtk_tree_view_column_new();
1452 m_column = (void*) column;
1453
1454 gtk_tree_view_column_set_clickable( column, true );
1455
1456 SetTitle( title );
1457
1458 if (flags & wxDATAVIEW_COL_RESIZABLE)
1459 gtk_tree_view_column_set_resizable( column, true );
1460 if (flags & wxDATAVIEW_COL_HIDDEN)
1461 gtk_tree_view_column_set_visible( column, false );
1462 if (flags & wxDATAVIEW_COL_SORTABLE)
1463 gtk_tree_view_column_set_sort_indicator( column, true );
1464
1465 if (width > 0)
1466 {
1467 gtk_tree_view_column_set_fixed_width( column, width );
1468 gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
1469 }
1470
1471 gtk_tree_view_column_pack_end( column, renderer, FALSE );
1472
1473 gtk_tree_view_column_set_cell_data_func( column, renderer,
1474 wxGtkTreeCellDataFunc, (gpointer) cell, NULL );
1475
1476 }
1477
1478 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell, unsigned int model_column,
1479 int width, int flags ) :
1480 wxDataViewColumnBase( bitmap, cell, model_column, width, flags )
1481 {
1482 m_isConnected = false;
1483
1484 GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle();
1485
1486 GtkTreeViewColumn *column = gtk_tree_view_column_new();
1487 m_column = (void*) column;
1488
1489 SetBitmap( bitmap );
1490
1491 if (flags & wxDATAVIEW_COL_RESIZABLE)
1492 gtk_tree_view_column_set_resizable( column, true );
1493 if (flags & wxDATAVIEW_COL_HIDDEN)
1494 gtk_tree_view_column_set_visible( column, false );
1495 if (flags & wxDATAVIEW_COL_SORTABLE)
1496 gtk_tree_view_column_set_sort_indicator( column, true );
1497
1498 if (width > 0)
1499 {
1500 gtk_tree_view_column_set_fixed_width( column, width );
1501 gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
1502 }
1503
1504 gtk_tree_view_column_pack_end( column, renderer, FALSE );
1505
1506 gtk_tree_view_column_set_cell_data_func( column, renderer,
1507 wxGtkTreeCellDataFunc, (gpointer) cell, NULL );
1508 }
1509
1510 wxDataViewColumn::~wxDataViewColumn()
1511 {
1512 }
1513
1514 void wxDataViewColumn::OnInternalIdle()
1515 {
1516 if (m_isConnected)
1517 return;
1518
1519 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
1520 {
1521 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1522 if (column->button)
1523 {
1524 g_signal_connect(column->button, "button_press_event",
1525 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
1526
1527 m_isConnected = true;
1528 }
1529 }
1530 }
1531
1532 void wxDataViewColumn::SetTitle( const wxString &title )
1533 {
1534 wxDataViewColumnBase::SetTitle( title );
1535
1536 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1537
1538 if (m_isConnected)
1539 {
1540 // disconnect before column->button gets recreated
1541 g_signal_handlers_disconnect_by_func( column->button,
1542 (void*) gtk_dataview_header_button_press_callback, this);
1543
1544 m_isConnected = false;
1545 }
1546
1547 gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
1548
1549 gtk_tree_view_column_set_widget( column, NULL );
1550 }
1551
1552 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
1553 {
1554 wxDataViewColumnBase::SetBitmap( bitmap );
1555
1556 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1557 if (bitmap.Ok())
1558 {
1559 GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() );
1560
1561 GdkBitmap *mask = (GdkBitmap *) NULL;
1562 if (bitmap.GetMask())
1563 mask = bitmap.GetMask()->GetBitmap();
1564
1565 if (bitmap.HasPixbuf())
1566 {
1567 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
1568 bitmap.GetPixbuf());
1569 }
1570 else
1571 {
1572 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
1573 bitmap.GetPixmap(), mask);
1574 }
1575 gtk_widget_show( GTK_WIDGET(gtk_image) );
1576
1577 gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) );
1578 }
1579 else
1580 {
1581 gtk_tree_view_column_set_widget( column, NULL );
1582 }
1583 }
1584
1585 void wxDataViewColumn::SetAlignment( wxAlignment align )
1586 {
1587 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1588
1589 gfloat xalign = 0.0;
1590 if (align == wxALIGN_RIGHT)
1591 xalign = 1.0;
1592 if (align == wxALIGN_CENTER)
1593 xalign = 0.5;
1594
1595 gtk_tree_view_column_set_alignment( column, xalign );
1596 }
1597
1598 void wxDataViewColumn::SetSortable( bool sortable )
1599 {
1600 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1601 gtk_tree_view_column_set_sort_indicator( column, sortable );
1602 }
1603
1604 bool wxDataViewColumn::GetSortable()
1605 {
1606 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1607 return gtk_tree_view_column_get_sort_indicator( column );
1608 }
1609
1610 void wxDataViewColumn::SetSortOrder( bool ascending )
1611 {
1612 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1613
1614 if (ascending)
1615 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
1616 else
1617 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
1618 }
1619
1620 bool wxDataViewColumn::IsSortOrderAscending()
1621 {
1622 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1623
1624 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
1625 }
1626
1627 int wxDataViewColumn::GetWidth()
1628 {
1629 return gtk_tree_view_column_get_width( (GtkTreeViewColumn *)m_column );
1630 }
1631
1632 void wxDataViewColumn::SetFixedWidth( int width )
1633 {
1634 gtk_tree_view_column_set_fixed_width( (GtkTreeViewColumn *)m_column, width );
1635 }
1636
1637 int wxDataViewColumn::GetFixedWidth()
1638 {
1639 return gtk_tree_view_column_get_fixed_width( (GtkTreeViewColumn *)m_column );
1640 }
1641
1642 //-----------------------------------------------------------------------------
1643 // wxDataViewCtrl signal callbacks
1644 //-----------------------------------------------------------------------------
1645
1646 static void
1647 wxdataview_selection_changed_callback( GtkTreeSelection* selection, wxDataViewCtrl *dv )
1648 {
1649 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_SELECTED, dv->GetId() );
1650 event.SetRow( dv->GetSelection() );
1651 event.SetModel( dv->GetModel() );
1652 dv->GetEventHandler()->ProcessEvent( event );
1653 }
1654
1655 static void
1656 wxdataview_row_activated_callback( GtkTreeView* treeview, GtkTreePath *path,
1657 GtkTreeViewColumn *column, wxDataViewCtrl *dv )
1658 {
1659 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED, dv->GetId() );
1660 unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0];
1661 event.SetRow( row );
1662 event.SetModel( dv->GetModel() );
1663 dv->GetEventHandler()->ProcessEvent( event );
1664 }
1665
1666 //-----------------------------------------------------------------------------
1667 // wxDataViewCtrl
1668 //-----------------------------------------------------------------------------
1669
1670 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
1671
1672 wxDataViewCtrl::~wxDataViewCtrl()
1673 {
1674 if (m_notifier)
1675 GetModel()->RemoveNotifier( m_notifier );
1676 }
1677
1678 void wxDataViewCtrl::Init()
1679 {
1680 m_notifier = NULL;
1681 }
1682
1683 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
1684 const wxPoint& pos, const wxSize& size,
1685 long style, const wxValidator& validator )
1686 {
1687 Init();
1688
1689 m_needParent = true;
1690 m_acceptsFocus = true;
1691
1692 if (!PreCreation( parent, pos, size ) ||
1693 !CreateBase( parent, id, pos, size, style, validator ))
1694 {
1695 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1696 return false;
1697 }
1698
1699 m_widget = gtk_scrolled_window_new (NULL, NULL);
1700
1701 GtkScrolledWindowSetBorder(m_widget, style);
1702
1703 m_treeview = gtk_tree_view_new();
1704 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
1705
1706 if (style & wxDV_MULTIPLE)
1707 {
1708 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
1709 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
1710 }
1711
1712 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
1713 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
1714 gtk_widget_show (m_treeview);
1715
1716 m_parent->DoAddChild( this );
1717
1718 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
1719 g_signal_connect_after (selection, "changed",
1720 G_CALLBACK (wxdataview_selection_changed_callback), this);
1721 g_signal_connect_after (m_treeview, "row_activated",
1722 G_CALLBACK (wxdataview_row_activated_callback), this);
1723
1724 PostCreation(size);
1725
1726 return true;
1727 }
1728
1729 void wxDataViewCtrl::OnInternalIdle()
1730 {
1731 wxWindow::OnInternalIdle();
1732
1733 unsigned int cols = GetNumberOfColumns();
1734 unsigned int i;
1735 for (i = 0; i < cols; i++)
1736 {
1737 wxDataViewColumn *col = GetColumn( i );
1738 col->OnInternalIdle();
1739 }
1740 }
1741
1742 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
1743 {
1744 if (!wxDataViewCtrlBase::AssociateModel( model ))
1745 return false;
1746
1747 GtkWxListStore *gtk_store = wxgtk_list_store_new();
1748 gtk_store->model = model;
1749
1750 m_notifier = new wxGtkDataViewListModelNotifier( gtk_store, model );
1751
1752 model->AddNotifier( m_notifier );
1753
1754 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_store) );
1755 g_object_unref( gtk_store );
1756
1757 return true;
1758 }
1759
1760 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
1761 {
1762 if (!wxDataViewCtrlBase::AppendColumn(col))
1763 return false;
1764
1765 GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle();
1766
1767 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column );
1768
1769 return true;
1770 }
1771
1772 void wxDataViewCtrl::SetSelection( int row )
1773 {
1774 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
1775
1776 if (row < 0)
1777 {
1778 gtk_tree_selection_unselect_all( selection );
1779 }
1780 else
1781 {
1782 GtkTreePath *path = gtk_tree_path_new ();
1783 gtk_tree_path_append_index( path, row );
1784
1785 gtk_tree_selection_select_path( selection, path );
1786
1787 gtk_tree_path_free( path );
1788 }
1789 }
1790
1791 void wxDataViewCtrl::Unselect( unsigned int row )
1792 {
1793 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
1794
1795 GtkTreePath *path = gtk_tree_path_new ();
1796 gtk_tree_path_append_index( path, row );
1797
1798 gtk_tree_selection_unselect_path( selection, path );
1799
1800 gtk_tree_path_free( path );
1801 }
1802
1803 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
1804 {
1805 }
1806
1807 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections)
1808 {
1809 }
1810
1811 bool wxDataViewCtrl::IsSelected( unsigned int row ) const
1812 {
1813 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
1814
1815 GtkTreePath *path = gtk_tree_path_new ();
1816 gtk_tree_path_append_index( path, row );
1817
1818 gboolean ret = gtk_tree_selection_path_is_selected( selection, path );
1819
1820 gtk_tree_path_free( path );
1821
1822 return ret;
1823 }
1824
1825 int wxDataViewCtrl::GetSelection() const
1826 {
1827 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
1828 if (HasFlag(wxDV_MULTIPLE))
1829 {
1830 GtkTreeModel *model;
1831 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
1832
1833 // do something
1834 if (list)
1835 {
1836 // list = g_list_nth( list, 0 ); should be a noop
1837 GtkTreePath *path = (GtkTreePath*) list->data;
1838
1839 unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0];
1840
1841 // delete list
1842 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
1843 g_list_free( list );
1844
1845 return (int) row;
1846 }
1847 }
1848 else
1849 {
1850
1851 GtkTreeModel *model;
1852 GtkTreeIter iter;
1853 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
1854 if (has_selection)
1855 {
1856 unsigned int row = (wxUIntPtr) iter.user_data;
1857 return (int) row;
1858 }
1859 }
1860
1861 return -1;
1862 }
1863
1864 int wxDataViewCtrl::GetSelections(wxArrayInt& aSelections) const
1865 {
1866 aSelections.Clear();
1867
1868 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
1869 if (HasFlag(wxDV_MULTIPLE))
1870 {
1871 GtkTreeModel *model;
1872 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
1873
1874 int count = 0;
1875 while (list)
1876 {
1877
1878 // list = g_list_nth( list, 0 ); should be a noop
1879 GtkTreePath *path = (GtkTreePath*) list->data;
1880
1881 unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0];
1882
1883 aSelections.Add( (int) row );
1884
1885 list = g_list_next( list );
1886 }
1887
1888 // delete list
1889 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
1890 g_list_free( list );
1891
1892 return count;
1893 }
1894 else
1895 {
1896 GtkTreeModel *model;
1897 GtkTreeIter iter;
1898 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
1899 if (has_selection)
1900 {
1901 unsigned int row = (wxUIntPtr) iter.user_data;
1902 aSelections.Add( (int) row );
1903 return 1;
1904 }
1905 }
1906
1907 return 0;
1908 }
1909
1910
1911 #endif
1912 // !wxUSE_GENERICDATAVIEWCTRL
1913
1914 #endif
1915 // wxUSE_DATAVIEWCTRL