1 /////////////////////////////////////////////////////////////////////////////
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"
15 #if wxUSE_DATAVIEWCTRL
17 #include "wx/dataview.h"
18 #include "wx/stockitem.h"
19 #include "wx/dcclient.h"
20 #include "wx/calctrl.h"
21 #include "wx/popupwin.h"
24 #include "wx/gtk/private.h"
25 #include "wx/gtk/win_gtk.h"
27 #include <gobject/gvaluecollector.h>
28 #include <gtk/gtktreemodel.h>
29 #include <gtk/gtktreednd.h>
31 #include <gdk/gdkkeysyms.h>
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern void wxapp_install_idle_handler();
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern bool g_blockEventsOnDrag
;
52 //-----------------------------------------------------------------------------
53 // define new GTK+ class wxGtkListStore
54 //-----------------------------------------------------------------------------
58 #define GTK_TYPE_WX_LIST_STORE (gtk_wx_list_store_get_type ())
59 #define GTK_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStore))
60 #define GTK_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
61 #define GTK_IS_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_LIST_STORE))
62 #define GTK_IS_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_LIST_STORE))
63 #define GTK_WX_LIST_STORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
65 GType
gtk_wx_list_store_get_type (void);
67 typedef struct _GtkWxListStore GtkWxListStore
;
68 typedef struct _GtkWxListStoreClass GtkWxListStoreClass
;
70 struct _GtkWxListStore
76 wxDataViewListModel
*model
;
79 struct _GtkWxListStoreClass
81 GObjectClass list_parent_class
;
85 static GtkWxListStore
*wxgtk_list_store_new (void);
86 static void wxgtk_list_store_init (GtkWxListStore
*list_store
);
87 static void wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
);
88 static void wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
);
89 static void wxgtk_list_store_finalize (GObject
*object
);
90 static GtkTreeModelFlags
wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
);
91 static gint
wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
);
92 static GType
wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
94 static gboolean
wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
97 static GtkTreePath
*wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
99 static void wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
103 static gboolean
wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
105 static gboolean
wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
107 GtkTreeIter
*parent
);
108 static gboolean
wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
110 static gint
wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
112 static gboolean
wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
116 static gboolean
wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
120 static GObjectClass
*list_parent_class
= NULL
;
123 gtk_wx_list_store_get_type (void)
125 static GType list_store_type
= 0;
127 if (!list_store_type
)
129 static const GTypeInfo list_store_info
=
131 sizeof (GtkWxListStoreClass
),
132 NULL
, /* base_init */
133 NULL
, /* base_finalize */
134 (GClassInitFunc
) wxgtk_list_store_class_init
,
135 NULL
, /* class_finalize */
136 NULL
, /* class_data */
137 sizeof (GtkWxListStore
),
139 (GInstanceInitFunc
) wxgtk_list_store_init
,
142 static const GInterfaceInfo tree_model_info
=
144 (GInterfaceInitFunc
) wxgtk_list_store_tree_model_init
,
149 list_store_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxListStore",
150 &list_store_info
, (GTypeFlags
)0 );
152 g_type_add_interface_static (list_store_type
,
157 return list_store_type
;
160 static GtkWxListStore
*
161 wxgtk_list_store_new(void)
163 GtkWxListStore
*retval
= (GtkWxListStore
*) g_object_new (GTK_TYPE_WX_LIST_STORE
, NULL
);
168 wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
)
170 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
171 GObjectClass
*object_class
= (GObjectClass
*) klass
;
172 object_class
->finalize
= wxgtk_list_store_finalize
;
176 wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
)
178 iface
->get_flags
= wxgtk_list_store_get_flags
;
179 iface
->get_n_columns
= wxgtk_list_store_get_n_columns
;
180 iface
->get_column_type
= wxgtk_list_store_get_column_type
;
181 iface
->get_iter
= wxgtk_list_store_get_iter
;
182 iface
->get_path
= wxgtk_list_store_get_path
;
183 iface
->get_value
= wxgtk_list_store_get_value
;
184 iface
->iter_next
= wxgtk_list_store_iter_next
;
185 iface
->iter_children
= wxgtk_list_store_iter_children
;
186 iface
->iter_has_child
= wxgtk_list_store_iter_has_child
;
187 iface
->iter_n_children
= wxgtk_list_store_iter_n_children
;
188 iface
->iter_nth_child
= wxgtk_list_store_iter_nth_child
;
189 iface
->iter_parent
= wxgtk_list_store_iter_parent
;
193 wxgtk_list_store_init (GtkWxListStore
*list_store
)
195 list_store
->model
= NULL
;
196 list_store
->stamp
= g_random_int();
200 wxgtk_list_store_finalize (GObject
*object
)
202 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
204 /* we need to sort out, which class deletes what */
205 /* delete list_store->model; */
208 (* list_parent_class
->finalize
) (object
);
213 //-----------------------------------------------------------------------------
214 // implement callbacks from wxGtkListStore class by letting
215 // them call the methods of wxWidgets' wxDataViewListModel
216 //-----------------------------------------------------------------------------
218 static GtkTreeModelFlags
219 wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
)
221 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), (GtkTreeModelFlags
)0 );
223 // GTK+ list store uses a linked list for storing the
224 // items and a pointer to a child is used as the member
225 // field of a GtkTreeIter. This means that the iter is
226 // valid in the GtkListStore as long as the child exists.
227 // We use the index of the row and since the index of a
228 // specific row will change if a row above is deleted,
229 // the iter does not persist
230 return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY
;
234 wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
)
236 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
237 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), 0);
239 return list_store
->model
->GetNumberOfCols();
243 wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
246 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
247 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), G_TYPE_INVALID
);
249 GType gtype
= G_TYPE_INVALID
;
251 wxString wxtype
= list_store
->model
->GetColType( (size_t) index
);
253 if (wxtype
== wxT("string"))
254 gtype
= G_TYPE_STRING
;
260 wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
264 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
265 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
266 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
268 size_t i
= (size_t)gtk_tree_path_get_indices (path
)[0];
270 if (i
>= list_store
->model
->GetNumberOfRows())
273 iter
->stamp
= list_store
->stamp
;
274 // user_data is just the index
275 iter
->user_data
= (gpointer
) i
;
281 wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
284 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), NULL
);
285 g_return_val_if_fail (iter
->stamp
== GTK_WX_LIST_STORE (tree_model
)->stamp
, NULL
);
287 GtkTreePath
*retval
= gtk_tree_path_new ();
288 // user_data is just the index
289 int i
= (int) iter
->user_data
;
290 gtk_tree_path_append_index (retval
, i
);
295 wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
300 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
301 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model
) );
303 wxDataViewListModel
*model
= list_store
->model
;
304 wxString mtype
= model
->GetColType( (size_t) column
);
305 if (mtype
== wxT("string"))
307 g_value_init( value
, G_TYPE_STRING
);
308 wxVariant variant
= model
->GetValue( (size_t) column
, (size_t) iter
->user_data
);
309 g_value_set_string( value
, wxGTK_CONV(variant
.GetString()) );
316 GtkTreeDataList
*list
;
317 gint tmp_column
= column
;
319 g_return_if_fail (column
< GTK_LIST_STORE (tree_model
)->n_columns
);
320 g_return_if_fail (GTK_LIST_STORE (tree_model
)->stamp
== iter
->stamp
);
322 list
= G_SLIST (iter
->user_data
)->data
;
324 while (tmp_column
-- > 0 && list
)
328 g_value_init (value
, GTK_LIST_STORE (tree_model
)->column_headers
[column
]);
330 _gtk_tree_data_list_node_to_value (list
,
331 GTK_LIST_STORE (tree_model
)->column_headers
[column
],
338 wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
341 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
342 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
344 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, FALSE
);
346 int n
= (int) iter
->user_data
;
351 if (n
>= (int) list_store
->model
->GetNumberOfRows()-1)
354 iter
->user_data
= (gpointer
) ++n
;
360 wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
364 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
365 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
367 // this is a list, nodes have no children
371 iter
->stamp
= list_store
->stamp
;
372 iter
->user_data
= (gpointer
) -1;
378 wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
385 wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
388 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), -1);
389 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
392 return (gint
) list_store
->model
->GetNumberOfRows();
394 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, -1);
400 wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
405 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
406 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
414 if (n
>= (gint
) list_store
->model
->GetNumberOfRows())
417 iter
->stamp
= list_store
->stamp
;
418 iter
->user_data
= (gpointer
) n
;
424 wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
431 //-----------------------------------------------------------------------------
432 // define new GTK+ class wxGtkCellRenderer
433 //-----------------------------------------------------------------------------
437 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
438 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
439 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
440 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
441 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
442 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
444 GType
gtk_wx_cell_renderer_get_type (void);
446 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
447 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
449 struct _GtkWxCellRenderer
451 GtkCellRenderer parent
;
454 wxDataViewCustomCell
*cell
;
458 struct _GtkWxCellRendererClass
460 GtkCellRendererClass cell_parent_class
;
464 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
465 static void gtk_wx_cell_renderer_init (
466 GtkWxCellRenderer
*cell
);
467 static void gtk_wx_cell_renderer_class_init(
468 GtkWxCellRendererClass
*klass
);
469 static void gtk_wx_cell_renderer_finalize (
471 static void gtk_wx_cell_renderer_get_size (
472 GtkCellRenderer
*cell
,
474 GdkRectangle
*rectangle
,
479 static void gtk_wx_cell_renderer_render (
480 GtkCellRenderer
*cell
,
483 GdkRectangle
*background_area
,
484 GdkRectangle
*cell_area
,
485 GdkRectangle
*expose_area
,
486 GtkCellRendererState flags
);
487 static gboolean
gtk_wx_cell_renderer_activate(
488 GtkCellRenderer
*cell
,
492 GdkRectangle
*background_area
,
493 GdkRectangle
*cell_area
,
494 GtkCellRendererState flags
);
496 static GObjectClass
*cell_parent_class
= NULL
;
501 gtk_wx_cell_renderer_get_type (void)
503 static GType cell_wx_type
= 0;
507 static const GTypeInfo cell_wx_info
=
509 sizeof (GtkWxCellRendererClass
),
510 NULL
, /* base_init */
511 NULL
, /* base_finalize */
512 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
513 NULL
, /* class_finalize */
514 NULL
, /* class_data */
515 sizeof (GtkWxCellRenderer
),
517 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
520 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
521 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
528 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
531 cell
->last_click
= 0;
535 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
537 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
538 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
540 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
542 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
544 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
545 cell_class
->render
= gtk_wx_cell_renderer_render
;
546 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
550 gtk_wx_cell_renderer_finalize (GObject
*object
)
553 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
557 gtk_wx_cell_renderer_new (void)
559 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
563 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
565 GdkRectangle
*cell_area
,
571 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
572 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
574 wxSize size
= cell
->GetSize();
576 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
577 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
584 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
588 *x_offset
= (gint
)((renderer
->xalign
*
589 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
590 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
594 *y_offset
= (gint
)((renderer
->yalign
*
595 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
596 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
604 *height
= calc_height
;
608 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
611 GdkRectangle
*background_area
,
612 GdkRectangle
*cell_area
,
613 GdkRectangle
*expose_area
,
614 GtkCellRendererState flags
)
617 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
618 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
621 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
627 rect
.x
+= cell_area
->x
;
628 rect
.y
+= cell_area
->y
;
629 rect
.width
-= renderer
->xpad
* 2;
630 rect
.height
-= renderer
->ypad
* 2;
633 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
635 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
636 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
637 if (dc
->m_window
== NULL
)
639 dc
->m_window
= window
;
644 if (flags
& GTK_CELL_RENDERER_SELECTED
)
645 state
|= wxDATAVIEW_CELL_SELECTED
;
646 if (flags
& GTK_CELL_RENDERER_PRELIT
)
647 state
|= wxDATAVIEW_CELL_PRELIT
;
648 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
649 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
650 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
651 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
652 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
653 state
|= wxDATAVIEW_CELL_FOCUSED
;
654 cell
->Render( renderrect
, dc
, state
);
659 gtk_wx_cell_renderer_activate(
660 GtkCellRenderer
*renderer
,
664 GdkRectangle
*background_area
,
665 GdkRectangle
*cell_area
,
666 GtkCellRendererState flags
)
668 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
669 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
672 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
678 rect
.x
+= cell_area
->x
;
679 rect
.y
+= cell_area
->y
;
680 rect
.width
-= renderer
->xpad
* 2;
681 rect
.height
-= renderer
->ypad
* 2;
683 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
685 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
687 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
688 size_t model_row
= (size_t)gtk_tree_path_get_indices (treepath
)[0];
689 gtk_tree_path_free( treepath
);
691 size_t model_col
= cell
->GetOwner()->GetModelColumn();
693 if (event
->type
== GDK_BUTTON_PRESS
)
695 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
696 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
697 ((int) button_event
->y
) - renderrect
.y
);
700 if (button_event
->button
== 1)
702 if (cell
->LeftClick( pt
, renderrect
, model
, model_col
, model_row
))
704 // TODO: query system double-click time
705 if (button_event
->time
- wxrenderer
->last_click
< 400)
706 if (cell
->Activate( renderrect
, model
, model_col
, model_row
))
709 if (button_event
->button
== 3)
711 if (cell
->RightClick( pt
, renderrect
, model
, model_col
, model_row
))
715 wxrenderer
->last_click
= button_event
->time
;
723 // ---------------------------------------------------------
724 // wxGtkDataViewListModelNotifier
725 // ---------------------------------------------------------
727 class wxGtkDataViewListModelNotifier
: public wxDataViewListModelNotifier
730 wxGtkDataViewListModelNotifier( GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
);
732 virtual bool RowAppended();
733 virtual bool RowPrepended();
734 virtual bool RowInserted( size_t before
);
735 virtual bool RowDeleted( size_t row
);
736 virtual bool RowChanged( size_t row
);
737 virtual bool ValueChanged( size_t col
, size_t row
);
738 virtual bool RowsReordered( size_t *new_order
);
739 virtual bool Cleared();
741 GtkWxListStore
*m_gtk_store
;
742 wxDataViewListModel
*m_wx_model
;
745 // ---------------------------------------------------------
746 // wxGtkDataViewListModelNotifier
747 // ---------------------------------------------------------
749 wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
750 GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
)
752 m_gtk_store
= gtk_store
;
753 m_wx_model
= wx_model
;
756 bool wxGtkDataViewListModelNotifier::RowAppended()
758 size_t pos
= m_wx_model
->GetNumberOfRows()-1;
761 iter
.stamp
= m_gtk_store
->stamp
;
762 iter
.user_data
= (gpointer
) pos
;
764 GtkTreePath
*path
= gtk_tree_path_new ();
765 gtk_tree_path_append_index (path
, (gint
) pos
);
766 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
767 gtk_tree_path_free (path
);
772 bool wxGtkDataViewListModelNotifier::RowPrepended()
775 iter
.stamp
= m_gtk_store
->stamp
;
776 iter
.user_data
= (gpointer
) 0;
778 GtkTreePath
*path
= gtk_tree_path_new ();
779 gtk_tree_path_append_index (path
, (gint
) 0);
780 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
781 gtk_tree_path_free (path
);
786 bool wxGtkDataViewListModelNotifier::RowInserted( size_t before
)
791 bool wxGtkDataViewListModelNotifier::RowDeleted( size_t row
)
796 bool wxGtkDataViewListModelNotifier::RowChanged( size_t row
)
799 iter
.stamp
= m_gtk_store
->stamp
;
800 iter
.user_data
= (gpointer
) row
;
801 GtkTreePath
*path
= gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store
), &iter
);
802 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
803 gtk_tree_path_free (path
);
808 bool wxGtkDataViewListModelNotifier::ValueChanged( size_t model_col
, size_t model_row
)
810 // This adds GTK+'s missing MVC logic for ValueChanged
811 wxNode
*node
= GetOwner()->m_viewingColumns
.GetFirst();
814 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
815 if (viewing_column
->m_modelColumn
== model_col
)
817 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
818 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(viewing_column
->m_viewColumn
->GetGtkHandle());
821 GtkTreePath
*path
= gtk_tree_path_new();
822 gtk_tree_path_append_index( path
, model_row
);
823 GdkRectangle cell_area
;
824 gtk_tree_view_get_cell_area( widget
, path
, column
, &cell_area
);
825 gtk_tree_path_free( path
);
827 int ydiff
= column
->button
->allocation
.height
;
829 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
830 cell_area
.x
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
833 node
= node
->GetNext();
839 bool wxGtkDataViewListModelNotifier::RowsReordered( size_t *new_order
)
841 // Assume sizeof(size_t)= == sizeof(gint)
843 GtkTreePath
*path
= gtk_tree_path_new ();
844 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store
), path
, NULL
, (gint
*)new_order
);
845 gtk_tree_path_free (path
);
847 // This adds GTK+'s missing MVC logic for RowsReordered
848 wxNode
*node
= GetOwner()->m_viewingColumns
.GetFirst();
851 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
852 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
853 // Doesn't work yet...
854 gtk_widget_queue_draw( GTK_WIDGET(widget
) );
856 node
= node
->GetNext();
863 bool wxGtkDataViewListModelNotifier::Cleared()
868 // ---------------------------------------------------------
870 // ---------------------------------------------------------
872 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell
, wxDataViewCellBase
)
874 wxDataViewCell::wxDataViewCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
875 wxDataViewCellBase( varianttype
, mode
)
880 // ---------------------------------------------------------
881 // wxDataViewTextCell
882 // ---------------------------------------------------------
885 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
886 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
889 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
890 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
892 wxDataViewTextCell
*cell
= (wxDataViewTextCell
*) user_data
;
894 wxString tmp
= wxGTK_CONV_BACK( arg2
);
895 wxVariant value
= tmp
;
896 if (!cell
->Validate( value
))
899 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
901 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
902 size_t model_row
= (size_t)gtk_tree_path_get_indices (path
)[0];
903 gtk_tree_path_free( path
);
905 size_t model_col
= cell
->GetOwner()->GetModelColumn();
907 model
->SetValue( value
, model_col
, model_row
);
908 model
->ValueChanged( model_col
, model_row
);
911 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell
, wxDataViewCell
)
913 wxDataViewTextCell::wxDataViewTextCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
914 wxDataViewCell( varianttype
, mode
)
916 m_renderer
= (void*) gtk_cell_renderer_text_new();
918 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
920 GValue gvalue
= { 0, };
921 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
922 g_value_set_boolean( &gvalue
, true );
923 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
924 g_value_unset( &gvalue
);
926 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
930 bool wxDataViewTextCell::SetValue( const wxVariant
&value
)
932 wxString tmp
= value
;
934 GValue gvalue
= { 0, };
935 g_value_init( &gvalue
, G_TYPE_STRING
);
936 g_value_set_string( &gvalue
, wxGTK_CONV( tmp
) );
937 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
938 g_value_unset( &gvalue
);
943 bool wxDataViewTextCell::GetValue( wxVariant
&value
)
945 GValue gvalue
= { 0, };
946 g_value_init( &gvalue
, G_TYPE_STRING
);
947 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
948 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &gvalue
) );
949 g_value_unset( &gvalue
);
956 // ---------------------------------------------------------
957 // wxDataViewToggleCell
958 // ---------------------------------------------------------
961 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
962 gchar
*path
, gpointer user_data
);
965 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
966 gchar
*path
, gpointer user_data
)
968 wxDataViewToggleCell
*cell
= (wxDataViewToggleCell
*) user_data
;
971 GValue gvalue
= { 0, };
972 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
973 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
974 bool tmp
= g_value_get_boolean( &gvalue
);
975 g_value_unset( &gvalue
);
979 wxVariant value
= tmp
;
980 if (!cell
->Validate( value
))
983 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
985 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
986 size_t model_row
= (size_t)gtk_tree_path_get_indices (gtk_path
)[0];
987 gtk_tree_path_free( gtk_path
);
989 size_t model_col
= cell
->GetOwner()->GetModelColumn();
991 model
->SetValue( value
, model_col
, model_row
);
992 model
->ValueChanged( model_col
, model_row
);
995 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCell
)
997 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
998 wxDataViewCellMode mode
) :
999 wxDataViewCell( varianttype
, mode
)
1001 m_renderer
= (void*) gtk_cell_renderer_toggle_new();
1003 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
1005 g_signal_connect_after( m_renderer
, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1010 GValue gvalue
= { 0, };
1011 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1012 g_value_set_boolean( &gvalue
, false );
1013 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1014 g_value_unset( &gvalue
);
1016 GValue gvalue2
= { 0, };
1017 g_value_init( &gvalue2
, gtk_cell_renderer_mode_get_type() );
1018 g_value_set_enum( &gvalue2
, GTK_CELL_RENDERER_MODE_INERT
);
1019 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue2
);
1020 g_value_unset( &gvalue2
);
1025 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
1029 GValue gvalue
= { 0, };
1030 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1031 g_value_set_boolean( &gvalue
, tmp
);
1032 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1033 g_value_unset( &gvalue
);
1038 bool wxDataViewToggleCell::GetValue( wxVariant
&value
)
1040 GValue gvalue
= { 0, };
1041 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1042 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1043 bool tmp
= g_value_get_boolean( &gvalue
);
1044 g_value_unset( &gvalue
);
1051 // ---------------------------------------------------------
1052 // wxDataViewCustomCell
1053 // ---------------------------------------------------------
1055 class wxDataViewCtrlDC
: public wxWindowDC
1058 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1060 GtkWidget
*widget
= window
->m_treeview
;
1064 m_context
= window
->GtkGetPangoDefaultContext();
1065 m_layout
= pango_layout_new( m_context
);
1066 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1068 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1070 // Set m_window later
1072 // m_owner = window;
1076 // ---------------------------------------------------------
1077 // wxDataViewCustomCell
1078 // ---------------------------------------------------------
1080 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell
, wxDataViewCell
)
1082 wxDataViewCustomCell::wxDataViewCustomCell( const wxString
&varianttype
,
1083 wxDataViewCellMode mode
, bool no_init
) :
1084 wxDataViewCell( varianttype
, mode
)
1094 bool wxDataViewCustomCell::Init()
1096 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1097 renderer
->cell
= this;
1099 m_renderer
= (void*) renderer
;
1101 if (m_mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1103 GValue gvalue
= { 0, };
1104 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1105 g_value_set_enum( &gvalue
, GTK_CELL_RENDERER_MODE_ACTIVATABLE
);
1106 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1107 g_value_unset( &gvalue
);
1113 wxDataViewCustomCell::~wxDataViewCustomCell()
1119 wxDC
*wxDataViewCustomCell::GetDC()
1123 if (GetOwner() == NULL
)
1125 if (GetOwner()->GetOwner() == NULL
)
1127 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1133 // ---------------------------------------------------------
1134 // wxDataViewProgressCell
1135 // ---------------------------------------------------------
1137 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
1139 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
1140 const wxString
&varianttype
, wxDataViewCellMode mode
) :
1141 wxDataViewCustomCell( varianttype
, mode
, true )
1147 if (!gtk_check_version(2,6,0))
1149 m_renderer
= (void*) gtk_cell_renderer_progress_new();
1151 GValue gvalue
= { 0, };
1152 g_value_init( &gvalue
, G_TYPE_STRING
);
1153 g_value_set_boolean( &gvalue
, wxGTK_CONV(m_label
) );
1154 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1155 g_value_unset( &gvalue
);
1160 // Use custom cell code
1161 wxDataViewCustomCell::Init();
1165 wxDataViewProgressCell::~wxDataViewProgressCell()
1169 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
1172 if (!gtk_check_version(2,6,0))
1174 gint tmp
= (int) value
;
1175 GValue gvalue
= { 0, };
1176 g_value_init( &gvalue
, G_TYPE_INT
);
1177 g_value_set_boolean( &gvalue
, tmp
);
1178 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1179 g_value_unset( &gvalue
);
1184 m_value
= (long) value
;
1186 if (m_value
< 0) m_value
= 0;
1187 if (m_value
> 100) m_value
= 100;
1193 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1195 double pct
= (double)m_value
/ 100.0;
1197 bar
.width
= (int)(cell
.width
* pct
);
1198 dc
->SetPen( *wxTRANSPARENT_PEN
);
1199 dc
->SetBrush( *wxBLUE_BRUSH
);
1200 dc
->DrawRectangle( bar
);
1202 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1203 dc
->SetPen( *wxBLACK_PEN
);
1204 dc
->DrawRectangle( cell
);
1209 wxSize
wxDataViewProgressCell::GetSize()
1211 return wxSize(40,12);
1214 // ---------------------------------------------------------
1215 // wxDataViewDateCell
1216 // ---------------------------------------------------------
1218 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
1221 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1222 wxDataViewListModel
*model
, size_t col
, size_t row
) :
1223 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1228 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1229 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1230 sizer
->Add( m_cal
, 1, wxGROW
);
1235 virtual void OnDismiss()
1239 void OnCalendar( wxCalendarEvent
&event
);
1241 wxCalendarCtrl
*m_cal
;
1242 wxDataViewListModel
*m_model
;
1247 DECLARE_EVENT_TABLE()
1250 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
1251 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar
)
1254 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1256 wxDateTime date
= event
.GetDate();
1257 wxVariant value
= date
;
1258 m_model
->SetValue( value
, m_col
, m_row
);
1259 m_model
->ValueChanged( m_col
, m_row
);
1263 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
1265 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
1266 wxDataViewCellMode mode
) :
1267 wxDataViewCustomCell( varianttype
, mode
)
1271 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
1273 m_date
= value
.GetDateTime();
1278 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1280 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1281 wxString tmp
= m_date
.FormatDate();
1282 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1287 wxSize
wxDataViewDateCell::GetSize()
1289 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
1290 wxString tmp
= m_date
.FormatDate();
1292 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1293 return wxSize(x
,y
+d
);
1296 bool wxDataViewDateCell::Activate( wxRect cell
, wxDataViewListModel
*model
, size_t col
, size_t row
)
1298 wxVariant variant
= model
->GetValue( col
, row
);
1299 wxDateTime value
= variant
.GetDateTime();
1301 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
1302 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
1303 wxPoint pos
= wxGetMousePosition();
1306 popup
->Popup( popup
->m_cal
);
1311 // ---------------------------------------------------------
1313 // ---------------------------------------------------------
1316 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1317 GtkCellRenderer
*cell
,
1318 GtkTreeModel
*model
,
1324 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1325 GtkCellRenderer
*renderer
,
1326 GtkTreeModel
*model
,
1330 g_return_if_fail (GTK_IS_WX_LIST_STORE (model
));
1331 GtkWxListStore
*list_store
= (GtkWxListStore
*) model
;
1333 wxDataViewCell
*cell
= (wxDataViewCell
*) data
;
1335 size_t model_row
= (size_t) iter
->user_data
;
1337 wxVariant value
= list_store
->model
->GetValue(
1338 cell
->GetOwner()->GetModelColumn(), model_row
);
1340 if (value
.GetType() != cell
->GetVariantType())
1341 wxPrintf( wxT("Wrong type\n") );
1343 cell
->SetValue( value
);
1346 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1348 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
,
1349 size_t model_column
, int flags
) :
1350 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
1352 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) cell
->GetGtkHandle();
1354 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1356 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1358 gtk_tree_view_column_pack_start( column
, renderer
, TRUE
);
1360 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1361 wxGtkTreeCellDataFunc
, (gpointer
) cell
, NULL
);
1363 m_column
= (void*) column
;
1366 wxDataViewColumn::~wxDataViewColumn()
1370 void wxDataViewColumn::SetTitle( const wxString
&title
)
1372 wxDataViewColumnBase::SetTitle( title
);
1374 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)m_column
;
1375 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1378 //-----------------------------------------------------------------------------
1380 //-----------------------------------------------------------------------------
1382 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
1384 wxDataViewCtrl::~wxDataViewCtrl()
1387 GetModel()->RemoveNotifier( m_notifier
);
1390 void wxDataViewCtrl::Init()
1395 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
1396 const wxPoint
& pos
, const wxSize
& size
,
1397 long style
, const wxValidator
& validator
)
1401 m_needParent
= TRUE
;
1402 m_acceptsFocus
= TRUE
;
1404 if (!PreCreation( parent
, pos
, size
) ||
1405 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
1407 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1411 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
1412 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (m_widget
), GTK_SHADOW_IN
);
1414 m_treeview
= gtk_tree_view_new();
1415 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
1417 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
1418 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
1419 gtk_widget_show (m_treeview
);
1421 m_parent
->DoAddChild( this );
1428 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
1430 if (!wxDataViewCtrlBase::AssociateModel( model
))
1433 GtkWxListStore
*gtk_store
= wxgtk_list_store_new();
1434 gtk_store
->model
= model
;
1436 m_notifier
= new wxGtkDataViewListModelNotifier( gtk_store
, model
);
1438 model
->AddNotifier( m_notifier
);
1440 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_store
) );
1441 g_object_unref( gtk_store
);
1446 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
1448 if (!wxDataViewCtrlBase::AppendColumn(col
))
1451 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
1453 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
1459 #endif // wxUSE_DATAVIEWCTRL