]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
6de67bf4933855ddfc150adc68b9718dcf113213
[wxWidgets.git] / src / gtk / dataview.cpp
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"
19 #include "wx/dcclient.h"
20 #include "wx/calctrl.h"
21 #include "wx/popupwin.h"
22 #include "wx/sizer.h"
23
24 #include "wx/gtk/private.h"
25 #include "wx/gtk/win_gtk.h"
26
27 #include <gobject/gvaluecollector.h>
28 #include <gtk/gtktreemodel.h>
29 #include <gtk/gtktreednd.h>
30
31 #include <gdk/gdkkeysyms.h>
32
33 //-----------------------------------------------------------------------------
34 // classes
35 //-----------------------------------------------------------------------------
36
37 class wxDataViewCtrl;
38
39 //-----------------------------------------------------------------------------
40 // idle system
41 //-----------------------------------------------------------------------------
42
43 extern void wxapp_install_idle_handler();
44 extern bool g_isIdle;
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
85 static GtkWxListStore *wxgtk_list_store_new (void);
86 static void wxgtk_list_store_init (GtkWxListStore *list_store);
87 static void wxgtk_list_store_class_init (GtkWxListStoreClass *klass);
88 static void wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface);
89 static void wxgtk_list_store_finalize (GObject *object);
90 static GtkTreeModelFlags wxgtk_list_store_get_flags (GtkTreeModel *tree_model);
91 static gint wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model);
92 static GType wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
93 gint index);
94 static gboolean wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
95 GtkTreeIter *iter,
96 GtkTreePath *path);
97 static GtkTreePath *wxgtk_list_store_get_path (GtkTreeModel *tree_model,
98 GtkTreeIter *iter);
99 static void wxgtk_list_store_get_value (GtkTreeModel *tree_model,
100 GtkTreeIter *iter,
101 gint column,
102 GValue *value);
103 static gboolean wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
104 GtkTreeIter *iter);
105 static gboolean wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
106 GtkTreeIter *iter,
107 GtkTreeIter *parent);
108 static gboolean wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
109 GtkTreeIter *iter);
110 static gint wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
111 GtkTreeIter *iter);
112 static gboolean wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
113 GtkTreeIter *iter,
114 GtkTreeIter *parent,
115 gint n);
116 static gboolean wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
117 GtkTreeIter *iter,
118 GtkTreeIter *child);
119
120 static GObjectClass *list_parent_class = NULL;
121
122 GType
123 gtk_wx_list_store_get_type (void)
124 {
125 static GType list_store_type = 0;
126
127 if (!list_store_type)
128 {
129 static const GTypeInfo list_store_info =
130 {
131 sizeof (GtkWxListStoreClass),
132 NULL, /* base_init */
133 NULL, /* base_finalize */
134 (GClassInitFunc) wxgtk_list_store_class_init,
135 NULL, /* class_finalize */
136 NULL, /* class_data */
137 sizeof (GtkWxListStore),
138 0,
139 (GInstanceInitFunc) wxgtk_list_store_init,
140 };
141
142 static const GInterfaceInfo tree_model_info =
143 {
144 (GInterfaceInitFunc) wxgtk_list_store_tree_model_init,
145 NULL,
146 NULL
147 };
148
149 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxListStore",
150 &list_store_info, (GTypeFlags)0 );
151
152 g_type_add_interface_static (list_store_type,
153 GTK_TYPE_TREE_MODEL,
154 &tree_model_info);
155 }
156
157 return list_store_type;
158 }
159
160 static GtkWxListStore *
161 wxgtk_list_store_new(void)
162 {
163 GtkWxListStore *retval = (GtkWxListStore *) g_object_new (GTK_TYPE_WX_LIST_STORE, NULL);
164 return retval;
165 }
166
167 static void
168 wxgtk_list_store_class_init (GtkWxListStoreClass *klass)
169 {
170 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
171 GObjectClass *object_class = (GObjectClass*) klass;
172 object_class->finalize = wxgtk_list_store_finalize;
173 }
174
175 static void
176 wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface)
177 {
178 iface->get_flags = wxgtk_list_store_get_flags;
179 iface->get_n_columns = wxgtk_list_store_get_n_columns;
180 iface->get_column_type = wxgtk_list_store_get_column_type;
181 iface->get_iter = wxgtk_list_store_get_iter;
182 iface->get_path = wxgtk_list_store_get_path;
183 iface->get_value = wxgtk_list_store_get_value;
184 iface->iter_next = wxgtk_list_store_iter_next;
185 iface->iter_children = wxgtk_list_store_iter_children;
186 iface->iter_has_child = wxgtk_list_store_iter_has_child;
187 iface->iter_n_children = wxgtk_list_store_iter_n_children;
188 iface->iter_nth_child = wxgtk_list_store_iter_nth_child;
189 iface->iter_parent = wxgtk_list_store_iter_parent;
190 }
191
192 static void
193 wxgtk_list_store_init (GtkWxListStore *list_store)
194 {
195 list_store->model = NULL;
196 list_store->stamp = g_random_int();
197 }
198
199 static void
200 wxgtk_list_store_finalize (GObject *object)
201 {
202 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
203
204 /* we need to sort out, which class deletes what */
205 /* delete list_store->model; */
206
207 /* must chain up */
208 (* list_parent_class->finalize) (object);
209 }
210
211 } // extern "C"
212
213 //-----------------------------------------------------------------------------
214 // implement callbacks from wxGtkListStore class by letting
215 // them call the methods of wxWidgets' wxDataViewListModel
216 //-----------------------------------------------------------------------------
217
218 static GtkTreeModelFlags
219 wxgtk_list_store_get_flags (GtkTreeModel *tree_model)
220 {
221 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), (GtkTreeModelFlags)0 );
222
223 // GTK+ list store uses a linked list for storing the
224 // items and a pointer to a child is used as the member
225 // field of a GtkTreeIter. This means that the iter is
226 // valid in the GtkListStore as long as the child exists.
227 // We use the index of the row and since the index of a
228 // specific row will change if a row above is deleted,
229 // the iter does not persist
230 return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY;
231 }
232
233 static gint
234 wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model)
235 {
236 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
237 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), 0);
238
239 return list_store->model->GetNumberOfCols();
240 }
241
242 static GType
243 wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
244 gint index)
245 {
246 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
247 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), G_TYPE_INVALID);
248
249 GType gtype = G_TYPE_INVALID;
250
251 wxString wxtype = list_store->model->GetColType( (size_t) index );
252
253 if (wxtype == wxT("string"))
254 gtype = G_TYPE_STRING;
255
256 return gtype;
257 }
258
259 static gboolean
260 wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
261 GtkTreeIter *iter,
262 GtkTreePath *path)
263 {
264 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
265 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
266 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
267
268 size_t i = (size_t)gtk_tree_path_get_indices (path)[0];
269
270 if (i >= list_store->model->GetNumberOfRows())
271 return FALSE;
272
273 iter->stamp = list_store->stamp;
274 // user_data is just the index
275 iter->user_data = (gpointer) i;
276
277 return TRUE;
278 }
279
280 static GtkTreePath *
281 wxgtk_list_store_get_path (GtkTreeModel *tree_model,
282 GtkTreeIter *iter)
283 {
284 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), NULL);
285 g_return_val_if_fail (iter->stamp == GTK_WX_LIST_STORE (tree_model)->stamp, NULL);
286
287 GtkTreePath *retval = gtk_tree_path_new ();
288 // user_data is just the index
289 int i = (int) iter->user_data;
290 gtk_tree_path_append_index (retval, i);
291 return retval;
292 }
293
294 static void
295 wxgtk_list_store_get_value (GtkTreeModel *tree_model,
296 GtkTreeIter *iter,
297 gint column,
298 GValue *value)
299 {
300 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
301 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model) );
302
303 wxDataViewListModel *model = list_store->model;
304 wxString mtype = model->GetColType( (size_t) column );
305 if (mtype == wxT("string"))
306 {
307 g_value_init( value, G_TYPE_STRING );
308 wxVariant variant = model->GetValue( (size_t) column, (size_t) 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 = (int) 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 wxGtkCellRenderer
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 wxDataViewCustomCell *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 wxDataViewCustomCell *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 wxDataViewCustomCell *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 wxDataViewCustomCell *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 size_t model_row = (size_t)gtk_tree_path_get_indices (treepath)[0];
689 gtk_tree_path_free( treepath );
690
691 size_t 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( size_t before );
735 virtual bool RowDeleted( size_t row );
736 virtual bool RowChanged( size_t row );
737 virtual bool ValueChanged( size_t col, size_t row );
738 virtual bool RowsReordered( size_t *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 size_t 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( size_t before )
787 {
788 return false;
789 }
790
791 bool wxGtkDataViewListModelNotifier::RowDeleted( size_t row )
792 {
793 return false;
794 }
795
796 bool wxGtkDataViewListModelNotifier::RowChanged( size_t row )
797 {
798 GtkTreeIter iter;
799 iter.stamp = m_gtk_store->stamp;
800 iter.user_data = (gpointer) row;
801 GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store), &iter);
802 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store), path, &iter);
803 gtk_tree_path_free (path);
804
805 return true;
806 }
807
808 bool wxGtkDataViewListModelNotifier::ValueChanged( size_t model_col, size_t model_row )
809 {
810 // This adds GTK+'s missing MVC logic for ValueChanged
811 wxNode *node = GetOwner()->m_viewingColumns.GetFirst();
812 while (node)
813 {
814 wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData();
815 if (viewing_column->m_modelColumn == model_col)
816 {
817 GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview);
818 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(viewing_column->m_viewColumn->GetGtkHandle());
819
820 // Get cell area
821 GtkTreePath *path = gtk_tree_path_new();
822 gtk_tree_path_append_index( path, model_row );
823 GdkRectangle cell_area;
824 gtk_tree_view_get_cell_area( widget, path, column, &cell_area );
825 gtk_tree_path_free( path );
826
827 int ydiff = column->button->allocation.height;
828 // Redraw
829 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
830 cell_area.x, ydiff + cell_area.y, cell_area.width, cell_area.height );
831 }
832
833 node = node->GetNext();
834 }
835
836 return true;
837 }
838
839 bool wxGtkDataViewListModelNotifier::RowsReordered( size_t *new_order )
840 {
841 // Assume sizeof(size_t)= == sizeof(gint)
842
843 GtkTreePath *path = gtk_tree_path_new ();
844 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store), path, NULL, (gint*)new_order);
845 gtk_tree_path_free (path);
846
847 // This adds GTK+'s missing MVC logic for RowsReordered
848 wxNode *node = GetOwner()->m_viewingColumns.GetFirst();
849 while (node)
850 {
851 wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData();
852 GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview);
853 // Doesn't work yet...
854 gtk_widget_queue_draw( GTK_WIDGET(widget) );
855
856 node = node->GetNext();
857 }
858
859
860 return true;
861 }
862
863 bool wxGtkDataViewListModelNotifier::Cleared()
864 {
865 return false;
866 }
867
868 // ---------------------------------------------------------
869 // wxDataViewCell
870 // ---------------------------------------------------------
871
872 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell, wxDataViewCellBase)
873
874 wxDataViewCell::wxDataViewCell( const wxString &varianttype, wxDataViewCellMode mode ) :
875 wxDataViewCellBase( varianttype, mode )
876 {
877 m_renderer = NULL;
878 }
879
880 // ---------------------------------------------------------
881 // wxDataViewTextCell
882 // ---------------------------------------------------------
883
884 extern "C" {
885 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
886 gchar *arg1, gchar *arg2, gpointer user_data );
887 }
888
889 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
890 gchar *arg1, gchar *arg2, gpointer user_data )
891 {
892 wxDataViewTextCell *cell = (wxDataViewTextCell*) user_data;
893
894 wxString tmp = wxGTK_CONV_BACK( arg2 );
895 wxVariant value = tmp;
896 if (!cell->Validate( value ))
897 return;
898
899 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
900
901 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
902 size_t model_row = (size_t)gtk_tree_path_get_indices (path)[0];
903 gtk_tree_path_free( path );
904
905 size_t model_col = cell->GetOwner()->GetModelColumn();
906
907 model->SetValue( value, model_col, model_row );
908 model->ValueChanged( model_col, model_row );
909 }
910
911 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell, wxDataViewCell)
912
913 wxDataViewTextCell::wxDataViewTextCell( const wxString &varianttype, wxDataViewCellMode mode ) :
914 wxDataViewCell( varianttype, mode )
915 {
916 m_renderer = (void*) gtk_cell_renderer_text_new();
917
918 if (m_mode & wxDATAVIEW_CELL_EDITABLE)
919 {
920 GValue gvalue = { 0, };
921 g_value_init( &gvalue, G_TYPE_BOOLEAN );
922 g_value_set_boolean( &gvalue, true );
923 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
924 g_value_unset( &gvalue );
925
926 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
927 }
928 }
929
930 bool wxDataViewTextCell::SetValue( const wxVariant &value )
931 {
932 wxString tmp = value;
933
934 GValue gvalue = { 0, };
935 g_value_init( &gvalue, G_TYPE_STRING );
936 g_value_set_string( &gvalue, wxGTK_CONV( tmp ) );
937 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
938 g_value_unset( &gvalue );
939
940 return true;
941 }
942
943 bool wxDataViewTextCell::GetValue( wxVariant &value )
944 {
945 GValue gvalue = { 0, };
946 g_value_init( &gvalue, G_TYPE_STRING );
947 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
948 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &gvalue ) );
949 g_value_unset( &gvalue );
950
951 value = tmp;
952
953 return true;
954 }
955
956 // ---------------------------------------------------------
957 // wxDataViewToggleCell
958 // ---------------------------------------------------------
959
960 extern "C" {
961 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
962 gchar *path, gpointer user_data );
963 }
964
965 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
966 gchar *path, gpointer user_data )
967 {
968 wxDataViewToggleCell *cell = (wxDataViewToggleCell*) user_data;
969
970 // get old value
971 GValue gvalue = { 0, };
972 g_value_init( &gvalue, G_TYPE_BOOLEAN );
973 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
974 bool tmp = g_value_get_boolean( &gvalue );
975 g_value_unset( &gvalue );
976 // invert it
977 tmp = !tmp;
978
979 wxVariant value = tmp;
980 if (!cell->Validate( value ))
981 return;
982
983 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
984
985 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
986 size_t model_row = (size_t)gtk_tree_path_get_indices (gtk_path)[0];
987 gtk_tree_path_free( gtk_path );
988
989 size_t model_col = cell->GetOwner()->GetModelColumn();
990
991 model->SetValue( value, model_col, model_row );
992 model->ValueChanged( model_col, model_row );
993 }
994
995 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell, wxDataViewCell)
996
997 wxDataViewToggleCell::wxDataViewToggleCell( const wxString &varianttype,
998 wxDataViewCellMode mode ) :
999 wxDataViewCell( varianttype, mode )
1000 {
1001 m_renderer = (void*) gtk_cell_renderer_toggle_new();
1002
1003 if (m_mode & wxDATAVIEW_CELL_EDITABLE)
1004 {
1005 g_signal_connect_after( m_renderer, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
1006 }
1007 else
1008 {
1009
1010 GValue gvalue = { 0, };
1011 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1012 g_value_set_boolean( &gvalue, false );
1013 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
1014 g_value_unset( &gvalue );
1015
1016 GValue gvalue2 = { 0, };
1017 g_value_init( &gvalue2, gtk_cell_renderer_mode_get_type() );
1018 g_value_set_enum( &gvalue2, GTK_CELL_RENDERER_MODE_INERT );
1019 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue2 );
1020 g_value_unset( &gvalue2 );
1021
1022 }
1023 }
1024
1025 bool wxDataViewToggleCell::SetValue( const wxVariant &value )
1026 {
1027 bool tmp = value;
1028
1029 GValue gvalue = { 0, };
1030 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1031 g_value_set_boolean( &gvalue, tmp );
1032 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
1033 g_value_unset( &gvalue );
1034
1035 return true;
1036 }
1037
1038 bool wxDataViewToggleCell::GetValue( wxVariant &value )
1039 {
1040 GValue gvalue = { 0, };
1041 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1042 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
1043 bool tmp = g_value_get_boolean( &gvalue );
1044 g_value_unset( &gvalue );
1045
1046 value = tmp;
1047
1048 return true;
1049 }
1050
1051 // ---------------------------------------------------------
1052 // wxDataViewCustomCell
1053 // ---------------------------------------------------------
1054
1055 class wxDataViewCtrlDC: public wxWindowDC
1056 {
1057 public:
1058 wxDataViewCtrlDC( wxDataViewCtrl *window )
1059 {
1060 GtkWidget *widget = window->m_treeview;
1061 // Set later
1062 m_window = NULL;
1063
1064 m_context = window->GtkGetPangoDefaultContext();
1065 m_layout = pango_layout_new( m_context );
1066 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
1067
1068 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
1069
1070 // Set m_window later
1071 // SetUpDC();
1072 // m_owner = window;
1073 }
1074 };
1075
1076 // ---------------------------------------------------------
1077 // wxDataViewCustomCell
1078 // ---------------------------------------------------------
1079
1080 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell, wxDataViewCell)
1081
1082 wxDataViewCustomCell::wxDataViewCustomCell( const wxString &varianttype,
1083 wxDataViewCellMode mode, bool no_init ) :
1084 wxDataViewCell( varianttype, mode )
1085 {
1086 m_dc = NULL;
1087
1088 if (no_init)
1089 m_renderer = NULL;
1090 else
1091 Init();
1092 }
1093
1094 bool wxDataViewCustomCell::Init()
1095 {
1096 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
1097 renderer->cell = this;
1098
1099 m_renderer = (void*) renderer;
1100
1101 if (m_mode & wxDATAVIEW_CELL_ACTIVATABLE)
1102 {
1103 GValue gvalue = { 0, };
1104 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1105 g_value_set_enum( &gvalue, GTK_CELL_RENDERER_MODE_ACTIVATABLE );
1106 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1107 g_value_unset( &gvalue );
1108 }
1109
1110 return true;
1111 }
1112
1113 wxDataViewCustomCell::~wxDataViewCustomCell()
1114 {
1115 if (m_dc)
1116 delete m_dc;
1117 }
1118
1119 wxDC *wxDataViewCustomCell::GetDC()
1120 {
1121 if (m_dc == NULL)
1122 {
1123 if (GetOwner() == NULL)
1124 return NULL;
1125 if (GetOwner()->GetOwner() == NULL)
1126 return NULL;
1127 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1128 }
1129
1130 return m_dc;
1131 }
1132
1133 // ---------------------------------------------------------
1134 // wxDataViewProgressCell
1135 // ---------------------------------------------------------
1136
1137 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell, wxDataViewCustomCell)
1138
1139 wxDataViewProgressCell::wxDataViewProgressCell( const wxString &label,
1140 const wxString &varianttype, wxDataViewCellMode mode ) :
1141 wxDataViewCustomCell( varianttype, mode, true )
1142 {
1143 m_label = label;
1144 m_value = 0;
1145
1146 #ifdef __WXGTK26__
1147 if (!gtk_check_version(2,6,0))
1148 {
1149 m_renderer = (void*) gtk_cell_renderer_progress_new();
1150
1151 GValue gvalue = { 0, };
1152 g_value_init( &gvalue, G_TYPE_STRING );
1153 g_value_set_boolean( &gvalue, wxGTK_CONV(m_label) );
1154 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1155 g_value_unset( &gvalue );
1156 }
1157 else
1158 #endif
1159 {
1160 // Use custom cell code
1161 wxDataViewCustomCell::Init();
1162 }
1163 }
1164
1165 wxDataViewProgressCell::~wxDataViewProgressCell()
1166 {
1167 }
1168
1169 bool wxDataViewProgressCell::SetValue( const wxVariant &value )
1170 {
1171 #ifdef __WXGTK26__
1172 if (!gtk_check_version(2,6,0))
1173 {
1174 gint tmp = (int) value;
1175 GValue gvalue = { 0, };
1176 g_value_init( &gvalue, G_TYPE_INT );
1177 g_value_set_boolean( &gvalue, tmp );
1178 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
1179 g_value_unset( &gvalue );
1180 }
1181 else
1182 #endif
1183 {
1184 m_value = (long) value;
1185
1186 if (m_value < 0) m_value = 0;
1187 if (m_value > 100) m_value = 100;
1188 }
1189
1190 return true;
1191 }
1192
1193 bool wxDataViewProgressCell::Render( wxRect cell, wxDC *dc, int state )
1194 {
1195 double pct = (double)m_value / 100.0;
1196 wxRect bar = cell;
1197 bar.width = (int)(cell.width * pct);
1198 dc->SetPen( *wxTRANSPARENT_PEN );
1199 dc->SetBrush( *wxBLUE_BRUSH );
1200 dc->DrawRectangle( bar );
1201
1202 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1203 dc->SetPen( *wxBLACK_PEN );
1204 dc->DrawRectangle( cell );
1205
1206 return true;
1207 }
1208
1209 wxSize wxDataViewProgressCell::GetSize()
1210 {
1211 return wxSize(40,12);
1212 }
1213
1214 // ---------------------------------------------------------
1215 // wxDataViewDateCell
1216 // ---------------------------------------------------------
1217
1218 class wxDataViewDateCellPopupTransient: public wxPopupTransientWindow
1219 {
1220 public:
1221 wxDataViewDateCellPopupTransient( wxWindow* parent, wxDateTime *value,
1222 wxDataViewListModel *model, size_t col, size_t row ) :
1223 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
1224 {
1225 m_model = model;
1226 m_col = col;
1227 m_row = row;
1228 m_cal = new wxCalendarCtrl( this, -1, *value );
1229 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
1230 sizer->Add( m_cal, 1, wxGROW );
1231 SetSizer( sizer );
1232 sizer->Fit( this );
1233 }
1234
1235 virtual void OnDismiss()
1236 {
1237 }
1238
1239 void OnCalendar( wxCalendarEvent &event );
1240
1241 wxCalendarCtrl *m_cal;
1242 wxDataViewListModel *m_model;
1243 size_t m_col;
1244 size_t m_row;
1245
1246 private:
1247 DECLARE_EVENT_TABLE()
1248 };
1249
1250 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient,wxPopupTransientWindow)
1251 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar )
1252 END_EVENT_TABLE()
1253
1254 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent &event )
1255 {
1256 wxDateTime date = event.GetDate();
1257 wxVariant value = date;
1258 m_model->SetValue( value, m_col, m_row );
1259 m_model->ValueChanged( m_col, m_row );
1260 DismissAndNotify();
1261 }
1262
1263 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell, wxDataViewCustomCell)
1264
1265 wxDataViewDateCell::wxDataViewDateCell( const wxString &varianttype,
1266 wxDataViewCellMode mode ) :
1267 wxDataViewCustomCell( varianttype, mode )
1268 {
1269 }
1270
1271 bool wxDataViewDateCell::SetValue( const wxVariant &value )
1272 {
1273 m_date = value.GetDateTime();
1274
1275 return true;
1276 }
1277
1278 bool wxDataViewDateCell::Render( wxRect cell, wxDC *dc, int state )
1279 {
1280 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
1281 wxString tmp = m_date.FormatDate();
1282 dc->DrawText( tmp, cell.x, cell.y );
1283
1284 return true;
1285 }
1286
1287 wxSize wxDataViewDateCell::GetSize()
1288 {
1289 wxDataViewCtrl* view = GetOwner()->GetOwner();
1290 wxString tmp = m_date.FormatDate();
1291 wxCoord x,y,d;
1292 view->GetTextExtent( tmp, &x, &y, &d );
1293 return wxSize(x,y+d);
1294 }
1295
1296 bool wxDataViewDateCell::Activate( wxRect cell, wxDataViewListModel *model, size_t col, size_t row )
1297 {
1298 wxVariant variant = model->GetValue( col, row );
1299 wxDateTime value = variant.GetDateTime();
1300
1301 wxDataViewDateCellPopupTransient *popup = new wxDataViewDateCellPopupTransient(
1302 GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
1303 wxPoint pos = wxGetMousePosition();
1304 popup->Move( pos );
1305 popup->Layout();
1306 popup->Popup( popup->m_cal );
1307
1308 return true;
1309 }
1310
1311 // ---------------------------------------------------------
1312 // wxDataViewColumn
1313 // ---------------------------------------------------------
1314
1315 extern "C" {
1316 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1317 GtkCellRenderer *cell,
1318 GtkTreeModel *model,
1319 GtkTreeIter *iter,
1320 gpointer data );
1321 }
1322
1323
1324 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1325 GtkCellRenderer *renderer,
1326 GtkTreeModel *model,
1327 GtkTreeIter *iter,
1328 gpointer data )
1329 {
1330 g_return_if_fail (GTK_IS_WX_LIST_STORE (model));
1331 GtkWxListStore *list_store = (GtkWxListStore *) model;
1332
1333 wxDataViewCell *cell = (wxDataViewCell*) data;
1334
1335 size_t model_row = (size_t) iter->user_data;
1336
1337 wxVariant value = list_store->model->GetValue(
1338 cell->GetOwner()->GetModelColumn(), model_row );
1339
1340 if (value.GetType() != cell->GetVariantType())
1341 wxPrintf( wxT("Wrong type\n") );
1342
1343 cell->SetValue( value );
1344 }
1345
1346 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
1347
1348 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewCell *cell,
1349 size_t model_column, int flags ) :
1350 wxDataViewColumnBase( title, cell, model_column, flags )
1351 {
1352 GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle();
1353
1354 GtkTreeViewColumn *column = gtk_tree_view_column_new();
1355
1356 gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
1357
1358 gtk_tree_view_column_pack_start( column, renderer, TRUE );
1359
1360 gtk_tree_view_column_set_cell_data_func( column, renderer,
1361 wxGtkTreeCellDataFunc, (gpointer) cell, NULL );
1362
1363 m_column = (void*) column;
1364 }
1365
1366 wxDataViewColumn::~wxDataViewColumn()
1367 {
1368 }
1369
1370 void wxDataViewColumn::SetTitle( const wxString &title )
1371 {
1372 wxDataViewColumnBase::SetTitle( title );
1373
1374 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1375 gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
1376 }
1377
1378 //-----------------------------------------------------------------------------
1379 // wxDataViewCtrl
1380 //-----------------------------------------------------------------------------
1381
1382 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
1383
1384 wxDataViewCtrl::~wxDataViewCtrl()
1385 {
1386 if (m_notifier)
1387 GetModel()->RemoveNotifier( m_notifier );
1388 }
1389
1390 void wxDataViewCtrl::Init()
1391 {
1392 m_notifier = NULL;
1393 }
1394
1395 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
1396 const wxPoint& pos, const wxSize& size,
1397 long style, const wxValidator& validator )
1398 {
1399 Init();
1400
1401 m_needParent = TRUE;
1402 m_acceptsFocus = TRUE;
1403
1404 if (!PreCreation( parent, pos, size ) ||
1405 !CreateBase( parent, id, pos, size, style, validator ))
1406 {
1407 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1408 return FALSE;
1409 }
1410
1411 m_widget = gtk_scrolled_window_new (NULL, NULL);
1412 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (m_widget), GTK_SHADOW_IN);
1413
1414 m_treeview = gtk_tree_view_new();
1415 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
1416
1417 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
1418 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
1419 gtk_widget_show (m_treeview);
1420
1421 m_parent->DoAddChild( this );
1422
1423 PostCreation(size);
1424
1425 return true;
1426 }
1427
1428 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
1429 {
1430 if (!wxDataViewCtrlBase::AssociateModel( model ))
1431 return false;
1432
1433 GtkWxListStore *gtk_store = wxgtk_list_store_new();
1434 gtk_store->model = model;
1435
1436 m_notifier = new wxGtkDataViewListModelNotifier( gtk_store, model );
1437
1438 model->AddNotifier( m_notifier );
1439
1440 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_store) );
1441 g_object_unref( gtk_store );
1442
1443 return true;
1444 }
1445
1446 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
1447 {
1448 if (!wxDataViewCtrlBase::AppendColumn(col))
1449 return false;
1450
1451 GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle();
1452
1453 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column );
1454
1455 return true;
1456 }
1457
1458
1459 #endif // wxUSE_DATAVIEWCTRL
1460