1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
3 // Purpose: wxDataViewCtrl GTK+2 implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if wxUSE_DATAVIEWCTRL
15 #include "wx/dataview.h"
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
21 #include "wx/dcclient.h"
25 #include "wx/stockitem.h"
26 #include "wx/calctrl.h"
27 #include "wx/popupwin.h"
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/win_gtk.h"
34 #include <gobject/gvaluecollector.h>
35 #include <gtk/gtktreemodel.h>
36 #include <gtk/gtktreednd.h>
38 #include <gdk/gdkkeysyms.h>
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern bool g_blockEventsOnDrag
;
52 //-----------------------------------------------------------------------------
53 // define new GTK+ class wxGtkTreeModel
54 //-----------------------------------------------------------------------------
58 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
59 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
60 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
61 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
62 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
63 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
65 GType
gtk_wx_tree_model_get_type (void);
67 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
68 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
70 struct _GtkWxTreeModel
76 wxDataViewModel
*model
;
79 struct _GtkWxTreeModelClass
81 GObjectClass list_parent_class
;
84 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
85 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
86 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
87 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
88 static void wxgtk_tree_model_finalize (GObject
*object
);
89 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
90 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
91 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
93 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
96 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
98 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
102 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
104 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
106 GtkTreeIter
*parent
);
107 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
109 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
111 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
115 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
119 static GObjectClass
*list_parent_class
= NULL
;
122 gtk_wx_tree_model_get_type (void)
124 static GType tree_model_type
= 0;
126 if (!tree_model_type
)
128 const GTypeInfo tree_model_info
=
130 sizeof (GtkWxTreeModelClass
),
131 NULL
, /* base_init */
132 NULL
, /* base_finalize */
133 (GClassInitFunc
) wxgtk_tree_model_class_init
,
134 NULL
, /* class_finalize */
135 NULL
, /* class_data */
136 sizeof (GtkWxTreeModel
),
138 (GInstanceInitFunc
) wxgtk_tree_model_init
,
141 static const GInterfaceInfo tree_model_iface_info
=
143 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
148 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
149 &tree_model_info
, (GTypeFlags
)0 );
151 g_type_add_interface_static (tree_model_type
,
153 &tree_model_iface_info
);
156 return tree_model_type
;
159 static GtkWxTreeModel
*
160 wxgtk_tree_model_new(void)
162 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
167 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
169 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
170 GObjectClass
*object_class
= (GObjectClass
*) klass
;
171 object_class
->finalize
= wxgtk_tree_model_finalize
;
175 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
177 iface
->get_flags
= wxgtk_tree_model_get_flags
;
178 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
179 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
180 iface
->get_iter
= wxgtk_tree_model_get_iter
;
181 iface
->get_path
= wxgtk_tree_model_get_path
;
182 iface
->get_value
= wxgtk_tree_model_get_value
;
183 iface
->iter_next
= wxgtk_tree_model_iter_next
;
184 iface
->iter_children
= wxgtk_tree_model_iter_children
;
185 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
186 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
187 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
188 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
192 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
194 tree_model
->model
= NULL
;
195 tree_model
->stamp
= g_random_int();
199 wxgtk_tree_model_finalize (GObject
*object
)
201 /* GtkWxTreeModel *tree_model = GTK_WX_LIST_STORE (object); */
203 /* we need to sort out, which class deletes what */
204 /* delete tree_model->model; */
207 (* list_parent_class
->finalize
) (object
);
212 //-----------------------------------------------------------------------------
213 // implement callbacks from wxGtkTreeModel class by letting
214 // them call the methods of wxWidgets' wxDataViewModel
215 //-----------------------------------------------------------------------------
217 static GtkTreeModelFlags
218 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
220 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model
), (GtkTreeModelFlags
)0 );
222 return GTK_TREE_MODEL_ITERS_PERSIST
;
226 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
228 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
229 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
231 return wxtree_model
->model
->GetColumnCount();
235 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
238 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
239 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
241 GType gtype
= G_TYPE_INVALID
;
243 wxString wxtype
= wxtree_model
->model
->GetColumnType( (unsigned int) index
);
245 if (wxtype
== wxT("string"))
246 gtype
= G_TYPE_STRING
;
249 wxFAIL_MSG( _T("non-string columns not supported yet") );
256 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
260 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
261 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
262 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
264 wxDataViewModel
*model
= wxtree_model
->model
;
266 int depth
= gtk_tree_path_get_depth( path
);
271 for (i
= 0; i
< depth
; i
++)
273 gint pos
= gtk_tree_path_get_indices (path
)[i
];
274 item
= model
->GetNthChild( item
, (unsigned int) pos
);
280 iter
->stamp
= wxtree_model
->stamp
;
281 iter
->user_data
= (gpointer
) item
.GetID();
287 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
290 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
291 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
292 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
294 GtkTreePath
*retval
= gtk_tree_path_new ();
295 wxDataViewItem
item( (wxUint32
) iter
->user_data
);
297 wxDataViewModel
*model
= wxtree_model
->model
;
302 wxDataViewItem parent
= model
->GetParent( item
);
305 wxPrintf( wxT("wrong parent\n") );
307 wxDataViewItem node
= model
->GetFirstChild( parent
);
309 while (node
.GetID() != item
.GetID())
311 node
= model
->GetNextSibling( node
);
314 wxPrintf( wxT("wrong node\n") );
319 gtk_tree_path_prepend_index( retval
, n
);
321 item
= model
->GetParent( item
);
328 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
333 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
334 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
336 wxDataViewModel
*model
= wxtree_model
->model
;
337 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
338 if (mtype
== wxT("string"))
341 g_value_init( value
, G_TYPE_STRING
);
342 wxDataViewItem
item( (wxUint32
) iter
->user_data
);
343 model
->GetValue( variant
, item
, (unsigned int) column
);
345 g_value_set_string( value
, variant
.GetString().utf8_str() );
349 wxFAIL_MSG( _T("non-string columns not supported yet") );
354 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
357 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
358 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
360 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
362 wxDataViewModel
*model
= wxtree_model
->model
;
364 wxDataViewItem
item( (wxUint32
) iter
->user_data
);
365 item
= model
->GetNextSibling( item
);
369 iter
->user_data
= (gpointer
) item
.GetID();
375 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
379 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
380 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
382 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
384 wxDataViewModel
*model
= wxtree_model
->model
;
386 wxDataViewItem
item( (wxUint32
) parent
->user_data
);
387 item
= model
->GetFirstChild( item
);
391 iter
->stamp
= wxtree_model
->stamp
;
392 iter
->user_data
= (gpointer
) item
.GetID();
398 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
401 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
402 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
404 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
406 wxDataViewModel
*model
= wxtree_model
->model
;
408 wxDataViewItem
item( (wxUint32
) iter
->user_data
);
410 return model
->HasChildren( item
);
414 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
417 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
418 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
420 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
422 wxDataViewModel
*model
= wxtree_model
->model
;
424 wxDataViewItem
item( (wxUint32
) iter
->user_data
);
426 return model
->GetChildCount( item
);
430 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
435 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
436 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
438 wxDataViewModel
*model
= wxtree_model
->model
;
443 item
= model
->GetNthChild( item
, n
);
448 iter
->stamp
= wxtree_model
->stamp
;
449 iter
->user_data
= (gpointer
) item
.GetID();
453 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
455 wxDataViewItem
item( (wxUint32
) parent
->user_data
);
456 item
= model
->GetNthChild( item
, n
);
461 iter
->stamp
= wxtree_model
->stamp
;
462 iter
->user_data
= (gpointer
) item
.GetID();
469 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
473 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
474 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
476 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
478 wxDataViewModel
*model
= wxtree_model
->model
;
480 wxDataViewItem
item( (wxUint32
) child
->user_data
);
481 item
= model
->GetParent( item
);
486 iter
->stamp
= wxtree_model
->stamp
;
487 iter
->user_data
= (gpointer
) item
.GetID();
492 //-----------------------------------------------------------------------------
493 // define new GTK+ class wxGtkRendererRenderer
494 //-----------------------------------------------------------------------------
498 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
499 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
500 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
501 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
502 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
503 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
505 GType
gtk_wx_cell_renderer_get_type (void);
507 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
508 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
510 struct _GtkWxCellRenderer
512 GtkCellRenderer parent
;
515 wxDataViewCustomRenderer
*cell
;
519 struct _GtkWxCellRendererClass
521 GtkCellRendererClass cell_parent_class
;
525 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
526 static void gtk_wx_cell_renderer_init (
527 GtkWxCellRenderer
*cell
);
528 static void gtk_wx_cell_renderer_class_init(
529 GtkWxCellRendererClass
*klass
);
530 static void gtk_wx_cell_renderer_finalize (
532 static void gtk_wx_cell_renderer_get_size (
533 GtkCellRenderer
*cell
,
535 GdkRectangle
*rectangle
,
540 static void gtk_wx_cell_renderer_render (
541 GtkCellRenderer
*cell
,
544 GdkRectangle
*background_area
,
545 GdkRectangle
*cell_area
,
546 GdkRectangle
*expose_area
,
547 GtkCellRendererState flags
);
548 static gboolean
gtk_wx_cell_renderer_activate(
549 GtkCellRenderer
*cell
,
553 GdkRectangle
*background_area
,
554 GdkRectangle
*cell_area
,
555 GtkCellRendererState flags
);
556 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
557 GtkCellRenderer
*cell
,
561 GdkRectangle
*background_area
,
562 GdkRectangle
*cell_area
,
563 GtkCellRendererState flags
);
566 static GObjectClass
*cell_parent_class
= NULL
;
571 gtk_wx_cell_renderer_get_type (void)
573 static GType cell_wx_type
= 0;
577 const GTypeInfo cell_wx_info
=
579 sizeof (GtkWxCellRendererClass
),
580 NULL
, /* base_init */
581 NULL
, /* base_finalize */
582 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
583 NULL
, /* class_finalize */
584 NULL
, /* class_data */
585 sizeof (GtkWxCellRenderer
),
587 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
590 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
591 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
598 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
601 cell
->last_click
= 0;
605 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
607 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
608 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
610 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
612 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
614 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
615 cell_class
->render
= gtk_wx_cell_renderer_render
;
616 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
617 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
621 gtk_wx_cell_renderer_finalize (GObject
*object
)
624 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
628 gtk_wx_cell_renderer_new (void)
630 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
635 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
636 GtkCellRenderer
*renderer
,
640 GdkRectangle
*background_area
,
641 GdkRectangle
*cell_area
,
642 GtkCellRendererState flags
)
644 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
645 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
646 if (!cell
->HasEditorCtrl())
650 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
656 rect
.x
+= cell_area
->x
;
657 rect
.y
+= cell_area
->y
;
658 // rect.width -= renderer->xpad * 2;
659 // rect.height -= renderer->ypad * 2;
661 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
662 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
664 // wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
666 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
667 unsigned int model_row
= (unsigned int)gtk_tree_path_get_indices (treepath
)[0];
668 gtk_tree_path_free( treepath
);
670 cell
->StartEditing( model_row
, renderrect
);
676 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
678 GdkRectangle
*cell_area
,
684 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
685 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
687 wxSize size
= cell
->GetSize();
689 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
690 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
697 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
701 *x_offset
= (gint
)((renderer
->xalign
*
702 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
703 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
707 *y_offset
= (gint
)((renderer
->yalign
*
708 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
709 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
717 *height
= calc_height
;
721 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
724 GdkRectangle
*background_area
,
725 GdkRectangle
*cell_area
,
726 GdkRectangle
*expose_area
,
727 GtkCellRendererState flags
)
730 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
731 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
734 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
740 rect
.x
+= cell_area
->x
;
741 rect
.y
+= cell_area
->y
;
742 rect
.width
-= renderer
->xpad
* 2;
743 rect
.height
-= renderer
->ypad
* 2;
746 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
748 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
749 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
750 if (dc
->m_window
== NULL
)
752 dc
->m_window
= window
;
757 if (flags
& GTK_CELL_RENDERER_SELECTED
)
758 state
|= wxDATAVIEW_CELL_SELECTED
;
759 if (flags
& GTK_CELL_RENDERER_PRELIT
)
760 state
|= wxDATAVIEW_CELL_PRELIT
;
761 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
762 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
763 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
764 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
765 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
766 state
|= wxDATAVIEW_CELL_FOCUSED
;
767 cell
->Render( renderrect
, dc
, state
);
772 gtk_wx_cell_renderer_activate(
773 GtkCellRenderer
*renderer
,
777 GdkRectangle
*background_area
,
778 GdkRectangle
*cell_area
,
779 GtkCellRendererState flags
)
781 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
782 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
785 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
791 rect
.x
+= cell_area
->x
;
792 rect
.y
+= cell_area
->y
;
793 rect
.width
-= renderer
->xpad
* 2;
794 rect
.height
-= renderer
->ypad
* 2;
796 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
798 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
800 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
801 unsigned int model_row
= (unsigned int)gtk_tree_path_get_indices (treepath
)[0];
802 gtk_tree_path_free( treepath
);
804 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
810 // activated by <ENTER>
811 if (cell
->Activate( renderrect
, model
, model_col
, model_row
))
816 else if (event
->type
== GDK_BUTTON_PRESS
)
818 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
819 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
820 ((int) button_event
->y
) - renderrect
.y
);
823 if (button_event
->button
== 1)
825 if (cell
->LeftClick( pt
, renderrect
, model
, model_col
, model_row
))
827 // TODO: query system double-click time
828 if (button_event
->time
- wxrenderer
->last_click
< 400)
829 if (cell
->Activate( renderrect
, model
, model_col
, model_row
))
832 if (button_event
->button
== 3)
834 if (cell
->RightClick( pt
, renderrect
, model
, model_col
, model_row
))
838 wxrenderer
->last_click
= button_event
->time
;
846 // ---------------------------------------------------------
847 // wxGtkDataViewModelNotifier
848 // ---------------------------------------------------------
850 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
853 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
854 wxDataViewModel
*wx_model
,
855 wxDataViewCtrl
*ctrl
);
856 ~wxGtkDataViewModelNotifier();
858 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
859 virtual bool ItemDeleted( const wxDataViewItem
&item
);
860 virtual bool ItemChanged( const wxDataViewItem
&item
);
861 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
862 virtual bool Cleared();
864 GtkWxTreeModel
*m_wxgtk_model
;
865 wxDataViewModel
*m_wx_model
;
866 wxDataViewCtrl
*m_owner
;
869 // ---------------------------------------------------------
870 // wxGtkDataViewListModelNotifier
871 // ---------------------------------------------------------
873 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
874 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
875 wxDataViewCtrl
*ctrl
)
877 m_wxgtk_model
= wxgtk_model
;
878 m_wx_model
= wx_model
;
882 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
885 m_wxgtk_model
= NULL
;
888 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
891 iter
.stamp
= m_wxgtk_model
->stamp
;
892 iter
.user_data
= (gpointer
) item
.GetID();
894 GtkTreePath
*path
= wxgtk_tree_model_get_path(
895 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
896 gtk_tree_model_row_inserted(
897 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
898 gtk_tree_path_free (path
);
903 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&item
)
906 iter
.stamp
= m_wxgtk_model
->stamp
;
907 iter
.user_data
= (gpointer
) item
.GetID();
909 GtkTreePath
*path
= wxgtk_tree_model_get_path(
910 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
911 gtk_tree_model_row_deleted(
912 GTK_TREE_MODEL(m_wxgtk_model
), path
);
913 gtk_tree_path_free (path
);
918 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
921 iter
.stamp
= m_wxgtk_model
->stamp
;
922 iter
.user_data
= (gpointer
) item
.GetID();
924 GtkTreePath
*path
= wxgtk_tree_model_get_path(
925 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
926 gtk_tree_model_row_changed(
927 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
928 gtk_tree_path_free (path
);
933 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
935 // This adds GTK+'s missing MVC logic for ValueChanged
937 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
939 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
940 if (column
->GetModelColumn() == model_col
)
942 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
943 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
947 iter
.stamp
= m_wxgtk_model
->stamp
;
948 iter
.user_data
= (gpointer
) item
.GetID();
949 GtkTreePath
*path
= wxgtk_tree_model_get_path(
950 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
951 GdkRectangle cell_area
;
952 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
953 gtk_tree_path_free( path
);
955 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
956 double d
= gtk_adjustment_get_value( hadjust
);
959 int ydiff
= gcolumn
->button
->allocation
.height
;
961 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
962 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
969 bool wxGtkDataViewModelNotifier::Cleared()
974 // ---------------------------------------------------------
975 // wxDataViewRenderer
976 // ---------------------------------------------------------
978 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
980 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
982 wxDataViewRendererBase( varianttype
, mode
, align
)
986 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
987 // after the m_renderer pointer has been initialized
990 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
992 GtkCellRendererMode gtkMode
;
995 case wxDATAVIEW_CELL_INERT
:
996 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
998 case wxDATAVIEW_CELL_ACTIVATABLE
:
999 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1001 case wxDATAVIEW_CELL_EDITABLE
:
1002 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1006 GValue gvalue
= { 0, };
1007 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1008 g_value_set_enum( &gvalue
, gtkMode
);
1009 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1010 g_value_unset( &gvalue
);
1013 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1015 wxDataViewCellMode ret
;
1018 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1020 switch (g_value_get_enum(&gvalue
))
1022 case GTK_CELL_RENDERER_MODE_INERT
:
1023 ret
= wxDATAVIEW_CELL_INERT
;
1025 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1026 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1028 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1029 ret
= wxDATAVIEW_CELL_EDITABLE
;
1033 g_value_unset( &gvalue
);
1038 void wxDataViewRenderer::SetAlignment( int align
)
1040 // horizontal alignment:
1042 gfloat xalign
= 0.0;
1043 if (align
& wxALIGN_RIGHT
)
1045 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1048 GValue gvalue
= { 0, };
1049 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1050 g_value_set_float( &gvalue
, xalign
);
1051 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1052 g_value_unset( &gvalue
);
1054 // vertical alignment:
1056 gfloat yalign
= 0.0;
1057 if (align
& wxALIGN_BOTTOM
)
1059 else if (align
& wxALIGN_CENTER_VERTICAL
)
1062 GValue gvalue2
= { 0, };
1063 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1064 g_value_set_float( &gvalue2
, yalign
);
1065 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1066 g_value_unset( &gvalue2
);
1069 int wxDataViewRenderer::GetAlignment() const
1074 // horizontal alignment:
1076 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1077 float xalign
= g_value_get_float( &gvalue
);
1079 ret
|= wxALIGN_LEFT
;
1080 else if (xalign
== 0.5)
1081 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1083 ret
|= wxALIGN_RIGHT
;
1084 g_value_unset( &gvalue
);
1087 // vertical alignment:
1089 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1090 float yalign
= g_value_get_float( &gvalue
);
1093 else if (yalign
== 0.5)
1094 ret
|= wxALIGN_CENTER_VERTICAL
;
1096 ret
|= wxALIGN_BOTTOM
;
1097 g_value_unset( &gvalue
);
1104 // ---------------------------------------------------------
1105 // wxDataViewTextRenderer
1106 // ---------------------------------------------------------
1109 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1110 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1113 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1114 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1116 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1118 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1119 wxVariant value
= tmp
;
1120 if (!cell
->Validate( value
))
1123 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1125 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1126 unsigned int model_row
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
1127 gtk_tree_path_free( path
);
1129 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1131 model
->SetValue( value
, model_col
, model_row
);
1132 model
->ValueChanged( model_col
, model_row
);
1135 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1137 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1139 wxDataViewRenderer( varianttype
, mode
, align
)
1141 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1143 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1145 GValue gvalue
= { 0, };
1146 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1147 g_value_set_boolean( &gvalue
, true );
1148 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1149 g_value_unset( &gvalue
);
1151 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1155 SetAlignment(align
);
1158 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1160 wxString tmp
= value
;
1162 GValue gvalue
= { 0, };
1163 g_value_init( &gvalue
, G_TYPE_STRING
);
1164 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( tmp
, GetOwner()->GetOwner()->GetFont() ) );
1165 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1166 g_value_unset( &gvalue
);
1171 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1173 GValue gvalue
= { 0, };
1174 g_value_init( &gvalue
, G_TYPE_STRING
);
1175 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1176 wxString tmp
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
),
1177 wx_const_cast(wxDataViewTextRenderer
*, this)->GetOwner()->GetOwner()->GetFont() );
1178 g_value_unset( &gvalue
);
1185 void wxDataViewTextRenderer::SetAlignment( int align
)
1187 wxDataViewRenderer::SetAlignment(align
);
1189 if (gtk_check_version(2,10,0))
1192 // horizontal alignment:
1193 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1194 if (align
& wxALIGN_RIGHT
)
1195 pangoAlign
= PANGO_ALIGN_RIGHT
;
1196 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1197 pangoAlign
= PANGO_ALIGN_CENTER
;
1199 GValue gvalue
= { 0, };
1200 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1201 g_value_set_enum( &gvalue
, pangoAlign
);
1202 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1203 g_value_unset( &gvalue
);
1206 // ---------------------------------------------------------
1207 // wxDataViewBitmapRenderer
1208 // ---------------------------------------------------------
1210 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1212 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1214 wxDataViewRenderer( varianttype
, mode
, align
)
1216 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1219 SetAlignment(align
);
1222 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1224 if (value
.GetType() == wxT("wxBitmap"))
1229 // This may create a Pixbuf representation in the
1230 // wxBitmap object (and it will stay there)
1231 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1233 GValue gvalue
= { 0, };
1234 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1235 g_value_set_object( &gvalue
, pixbuf
);
1236 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1237 g_value_unset( &gvalue
);
1242 if (value
.GetType() == wxT("wxIcon"))
1247 // This may create a Pixbuf representation in the
1248 // wxBitmap object (and it will stay there)
1249 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1251 GValue gvalue
= { 0, };
1252 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1253 g_value_set_object( &gvalue
, pixbuf
);
1254 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1255 g_value_unset( &gvalue
);
1263 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1268 // ---------------------------------------------------------
1269 // wxDataViewToggleRenderer
1270 // ---------------------------------------------------------
1273 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1274 gchar
*path
, gpointer user_data
);
1277 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1278 gchar
*path
, gpointer user_data
)
1280 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1283 GValue gvalue
= { 0, };
1284 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1285 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1286 bool tmp
= g_value_get_boolean( &gvalue
);
1287 g_value_unset( &gvalue
);
1291 wxVariant value
= tmp
;
1292 if (!cell
->Validate( value
))
1295 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1297 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1298 unsigned int model_row
= (unsigned int)gtk_tree_path_get_indices (gtk_path
)[0];
1299 gtk_tree_path_free( gtk_path
);
1301 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1303 model
->SetValue( value
, model_col
, model_row
);
1304 model
->ValueChanged( model_col
, model_row
);
1307 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1309 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1310 wxDataViewCellMode mode
, int align
) :
1311 wxDataViewRenderer( varianttype
, mode
, align
)
1313 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1315 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1317 g_signal_connect_after( m_renderer
, "toggled",
1318 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1322 GValue gvalue
= { 0, };
1323 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1324 g_value_set_boolean( &gvalue
, false );
1325 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1326 g_value_unset( &gvalue
);
1330 SetAlignment(align
);
1333 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1337 GValue gvalue
= { 0, };
1338 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1339 g_value_set_boolean( &gvalue
, tmp
);
1340 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1341 g_value_unset( &gvalue
);
1346 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1348 GValue gvalue
= { 0, };
1349 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1350 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1351 bool tmp
= g_value_get_boolean( &gvalue
);
1352 g_value_unset( &gvalue
);
1359 // ---------------------------------------------------------
1360 // wxDataViewCustomRenderer
1361 // ---------------------------------------------------------
1363 class wxDataViewCtrlDC
: public wxWindowDC
1366 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1368 GtkWidget
*widget
= window
->m_treeview
;
1372 m_context
= window
->GtkGetPangoDefaultContext();
1373 m_layout
= pango_layout_new( m_context
);
1374 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1376 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1378 // Set m_window later
1380 // m_owner = window;
1384 // ---------------------------------------------------------
1385 // wxDataViewCustomRenderer
1386 // ---------------------------------------------------------
1388 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1390 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1391 wxDataViewCellMode mode
, int align
,
1393 wxDataViewRenderer( varianttype
, mode
, align
)
1403 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1405 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1406 renderer
->cell
= this;
1408 m_renderer
= (GtkCellRenderer
*) renderer
;
1411 SetAlignment(align
);
1416 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1422 wxDC
*wxDataViewCustomRenderer::GetDC()
1426 if (GetOwner() == NULL
)
1428 if (GetOwner()->GetOwner() == NULL
)
1430 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1436 // ---------------------------------------------------------
1437 // wxDataViewProgressRenderer
1438 // ---------------------------------------------------------
1440 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1442 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1443 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1444 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1450 if (!gtk_check_version(2,6,0))
1452 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1454 GValue gvalue
= { 0, };
1455 g_value_init( &gvalue
, G_TYPE_STRING
);
1457 // FIXME: font encoding support
1458 g_value_set_string( &gvalue
, wxGTK_CONV_SYS(m_label
) );
1459 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1460 g_value_unset( &gvalue
);
1463 SetAlignment(align
);
1468 // Use custom cell code
1469 wxDataViewCustomRenderer::Init(mode
, align
);
1473 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1477 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1480 if (!gtk_check_version(2,6,0))
1482 gint tmp
= (long) value
;
1483 GValue gvalue
= { 0, };
1484 g_value_init( &gvalue
, G_TYPE_INT
);
1485 g_value_set_int( &gvalue
, tmp
);
1486 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1487 g_value_unset( &gvalue
);
1492 m_value
= (long) value
;
1494 if (m_value
< 0) m_value
= 0;
1495 if (m_value
> 100) m_value
= 100;
1501 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1506 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1508 double pct
= (double)m_value
/ 100.0;
1510 bar
.width
= (int)(cell
.width
* pct
);
1511 dc
->SetPen( *wxTRANSPARENT_PEN
);
1512 dc
->SetBrush( *wxBLUE_BRUSH
);
1513 dc
->DrawRectangle( bar
);
1515 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1516 dc
->SetPen( *wxBLACK_PEN
);
1517 dc
->DrawRectangle( cell
);
1522 wxSize
wxDataViewProgressRenderer::GetSize() const
1524 return wxSize(40,12);
1527 // ---------------------------------------------------------
1528 // wxDataViewDateRenderer
1529 // ---------------------------------------------------------
1531 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1534 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1535 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1536 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1541 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1542 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1543 sizer
->Add( m_cal
, 1, wxGROW
);
1548 virtual void OnDismiss()
1552 void OnCalendar( wxCalendarEvent
&event
);
1554 wxCalendarCtrl
*m_cal
;
1555 wxDataViewModel
*m_model
;
1556 wxDataViewItem m_item
;
1560 DECLARE_EVENT_TABLE()
1563 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1564 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1567 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1569 wxDateTime date
= event
.GetDate();
1570 wxVariant value
= date
;
1571 m_model
->SetValue( value
, m_item
, m_col
);
1572 m_model
->ValueChanged( m_item
, m_col
);
1576 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1578 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1579 wxDataViewCellMode mode
, int align
) :
1580 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1583 SetAlignment(align
);
1586 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1588 m_date
= value
.GetDateTime();
1593 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1598 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1600 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1601 wxString tmp
= m_date
.FormatDate();
1602 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1607 wxSize
wxDataViewDateRenderer::GetSize() const
1609 wxString tmp
= m_date
.FormatDate();
1611 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1612 return wxSize(x
,y
+d
);
1615 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1616 const wxDataViewItem
&item
, unsigned int col
)
1619 model
->GetValue( variant
, item
, col
);
1620 wxDateTime value
= variant
.GetDateTime();
1622 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1623 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1624 wxPoint pos
= wxGetMousePosition();
1627 popup
->Popup( popup
->m_cal
);
1632 // ---------------------------------------------------------
1634 // ---------------------------------------------------------
1638 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
1639 GdkEventButton
*gdk_event
,
1640 wxDataViewColumn
*column
)
1642 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
1645 if (gdk_event
->button
== 1)
1647 wxDataViewCtrl
*dv
= column
->GetOwner();
1648 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
1649 event
.SetDataViewColumn( column
);
1650 event
.SetModel( dv
->GetModel() );
1651 dv
->GetEventHandler()->ProcessEvent( event
);
1658 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1659 GtkCellRenderer
*cell
,
1660 GtkTreeModel
*model
,
1666 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1667 GtkCellRenderer
*renderer
,
1668 GtkTreeModel
*model
,
1672 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
1673 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
1675 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
1677 wxDataViewItem
item( (wxUint32
) iter
->user_data
);
1680 tree_model
->model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
1682 if (value
.GetType() != cell
->GetVariantType())
1683 wxLogError( wxT("Wrong type, required: %s but: %s"),
1684 value
.GetType().c_str(),
1685 cell
->GetVariantType().c_str() );
1687 cell
->SetValue( value
);
1690 wxListItemAttr attr
;
1691 tree_model
->model
->GetAttr( attr
, cell
->GetOwner()->GetModelColumn(), model_row
);
1693 if (attr
.HasBackgroundColour())
1695 wxColour colour
= attr
.GetBackgroundColour();
1696 const GdkColor
* const gcol
= colour
.GetColor();
1698 GValue gvalue
= { 0, };
1699 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
1700 g_value_set_boxed( &gvalue
, gcol
);
1701 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
1702 g_value_unset( &gvalue
);
1706 GValue gvalue
= { 0, };
1707 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1708 g_value_set_boolean( &gvalue
, FALSE
);
1709 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
1710 g_value_unset( &gvalue
);
1716 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1718 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1719 unsigned int model_column
, int width
,
1720 wxAlignment align
, int flags
) :
1721 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1723 Init( align
, flags
, width
);
1725 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
1729 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1730 unsigned int model_column
, int width
,
1731 wxAlignment align
, int flags
) :
1732 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1734 Init( align
, flags
, width
);
1736 SetBitmap( bitmap
);
1739 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
1741 m_isConnected
= false;
1743 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
1744 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1745 m_column
= (GtkWidget
*) column
;
1748 SetAlignment( align
);
1750 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1751 // as GTK+ is smart and unless explicitely told, will set the minimal
1752 // width to the title's lenght, which is a better default
1754 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1755 // that we use for the wxDataViewCtrl
1756 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1757 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
1759 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
1761 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1762 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
1765 wxDataViewColumn::~wxDataViewColumn()
1769 void wxDataViewColumn::OnInternalIdle()
1774 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
1776 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1779 g_signal_connect(column
->button
, "button_press_event",
1780 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
1782 m_isConnected
= true;
1787 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
1789 wxDataViewColumnBase::SetOwner( owner
);
1791 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1793 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1796 void wxDataViewColumn::SetTitle( const wxString
&title
)
1798 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1802 // disconnect before column->button gets recreated
1803 g_signal_handlers_disconnect_by_func( column
->button
,
1804 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
1806 m_isConnected
= false;
1809 // FIXME: can it really happen that we don't have the owner here??
1810 wxDataViewCtrl
*ctrl
= GetOwner();
1811 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
1812 : wxGTK_CONV_SYS(title
) );
1814 gtk_tree_view_column_set_widget( column
, NULL
);
1817 wxString
wxDataViewColumn::GetTitle() const
1819 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
1820 return wxConvFileName
->cMB2WX(str
);
1823 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
1825 wxDataViewColumnBase::SetBitmap( bitmap
);
1827 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1830 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
1832 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1833 if (bitmap
.GetMask())
1834 mask
= bitmap
.GetMask()->GetBitmap();
1836 if (bitmap
.HasPixbuf())
1838 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
1839 bitmap
.GetPixbuf());
1843 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
1844 bitmap
.GetPixmap(), mask
);
1846 gtk_widget_show( GTK_WIDGET(gtk_image
) );
1848 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
1852 gtk_tree_view_column_set_widget( column
, NULL
);
1856 void wxDataViewColumn::SetHidden( bool hidden
)
1858 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
1861 void wxDataViewColumn::SetResizeable( bool resizeable
)
1863 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
1866 void wxDataViewColumn::SetAlignment( wxAlignment align
)
1868 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1870 gfloat xalign
= 0.0;
1871 if (align
== wxALIGN_RIGHT
)
1873 if (align
== wxALIGN_CENTER_HORIZONTAL
||
1874 align
== wxALIGN_CENTER
)
1877 gtk_tree_view_column_set_alignment( column
, xalign
);
1880 wxAlignment
wxDataViewColumn::GetAlignment() const
1882 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
1885 return wxALIGN_RIGHT
;
1887 return wxALIGN_CENTER_HORIZONTAL
;
1889 return wxALIGN_LEFT
;
1892 void wxDataViewColumn::SetSortable( bool sortable
)
1894 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1895 gtk_tree_view_column_set_sort_indicator( column
, sortable
);
1898 bool wxDataViewColumn::IsSortable() const
1900 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1901 return gtk_tree_view_column_get_sort_indicator( column
);
1904 bool wxDataViewColumn::IsResizeable() const
1906 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1907 return gtk_tree_view_column_get_resizable( column
);
1910 bool wxDataViewColumn::IsHidden() const
1912 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1913 return !gtk_tree_view_column_get_visible( column
);
1916 void wxDataViewColumn::SetSortOrder( bool ascending
)
1918 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1921 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
1923 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
1926 bool wxDataViewColumn::IsSortOrderAscending() const
1928 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1930 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
1933 void wxDataViewColumn::SetMinWidth( int width
)
1935 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
1938 int wxDataViewColumn::GetMinWidth() const
1940 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
1943 int wxDataViewColumn::GetWidth() const
1945 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
1948 void wxDataViewColumn::SetWidth( int width
)
1950 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
1954 //-----------------------------------------------------------------------------
1955 // wxDataViewCtrl signal callbacks
1956 //-----------------------------------------------------------------------------
1959 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
1961 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
1964 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, dv
->GetId() );
1966 event
.SetModel( dv
->GetModel() );
1967 dv
->GetEventHandler()->ProcessEvent( event
);
1971 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
1972 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
1974 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
1976 event
.SetModel( dv
->GetModel() );
1977 dv
->GetEventHandler()->ProcessEvent( event
);
1980 //-----------------------------------------------------------------------------
1982 //-----------------------------------------------------------------------------
1984 //-----------------------------------------------------------------------------
1985 // InsertChild for wxDataViewCtrl
1986 //-----------------------------------------------------------------------------
1988 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
1990 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
1991 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
1993 // Insert widget in GtkTreeView
1994 if (GTK_WIDGET_REALIZED(treeview
))
1995 gtk_widget_set_parent_window( child
->m_widget
,
1996 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
1997 gtk_widget_set_parent( child
->m_widget
, treeview
);
2001 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
2002 GtkAllocation
*alloc
,
2003 wxDataViewCtrl
*win
)
2006 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
2009 wxWindow
*child
= node
->GetData();
2012 gtk_widget_size_request( child
->m_widget
, &req
);
2014 GtkAllocation alloc
;
2015 alloc
.x
= child
->m_x
;
2016 alloc
.y
= child
->m_y
;
2017 alloc
.width
= child
->m_width
;
2018 alloc
.height
= child
->m_height
;
2019 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
2021 node
= node
->GetNext();
2027 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2029 wxDataViewCtrl::~wxDataViewCtrl()
2032 GetModel()->RemoveNotifier( m_notifier
);
2034 // remove the model from the GtkTreeView before it gets destroyed by the
2035 // wxDataViewCtrlBase's dtor
2036 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
2039 void wxDataViewCtrl::Init()
2044 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2045 const wxPoint
& pos
, const wxSize
& size
,
2046 long style
, const wxValidator
& validator
)
2050 if (!PreCreation( parent
, pos
, size
) ||
2051 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
2053 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
2057 m_insertCallback
= wxInsertChildInDataViewCtrl
;
2059 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
2061 GtkScrolledWindowSetBorder(m_widget
, style
);
2063 m_treeview
= gtk_tree_view_new();
2064 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
2066 g_signal_connect (m_treeview
, "size_allocate",
2067 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
2070 if (!gtk_check_version(2,6,0))
2071 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
2074 if (style
& wxDV_MULTIPLE
)
2076 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2077 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
2080 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
2083 if (!gtk_check_version(2,10,0))
2085 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
2087 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
2088 (style
& wxDV_VERT_RULES
) != 0)
2089 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
2090 else if (style
& wxDV_VERT_RULES
)
2091 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
2092 else if (style
& wxDV_HORIZ_RULES
)
2093 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
2095 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
2100 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
2103 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
2104 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2105 gtk_widget_show (m_treeview
);
2107 m_parent
->DoAddChild( this );
2111 GtkEnableSelectionEvents();
2113 g_signal_connect_after (m_treeview
, "row_activated",
2114 G_CALLBACK (wxdataview_row_activated_callback
), this);
2119 void wxDataViewCtrl::OnInternalIdle()
2121 wxWindow::OnInternalIdle();
2123 unsigned int cols
= GetColumnCount();
2125 for (i
= 0; i
< cols
; i
++)
2127 wxDataViewColumn
*col
= GetColumn( i
);
2128 col
->OnInternalIdle();
2132 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2134 if (!wxDataViewCtrlBase::AssociateModel( model
))
2137 GtkWxTreeModel
*gtk_store
= wxgtk_tree_model_new();
2138 gtk_store
->model
= model
;
2140 m_notifier
= new wxGtkDataViewModelNotifier( gtk_store
, model
, this );
2142 model
->AddNotifier( m_notifier
);
2144 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_store
) );
2145 g_object_unref( gtk_store
);
2150 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
2152 if (!wxDataViewCtrlBase::AppendColumn(col
))
2155 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
2157 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
2162 void wxDataViewCtrl::GtkDisableSelectionEvents()
2164 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2165 g_signal_connect_after (selection
, "changed",
2166 G_CALLBACK (wxdataview_selection_changed_callback
), this);
2169 void wxDataViewCtrl::GtkEnableSelectionEvents()
2171 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2172 g_signal_handlers_disconnect_by_func( selection
,
2173 (gpointer
) (wxdataview_selection_changed_callback
), this);
2178 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2180 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
2185 // !wxUSE_GENERICDATAVIEWCTRL
2188 // wxUSE_DATAVIEWCTRL