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