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 Cleared();
739 virtual bool ValueChanged( wxDataViewColumn
*view_column
, size_t model_column
, size_t row
);
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 col
, size_t row
)
810 return RowChanged( row
);
813 bool wxGtkDataViewListModelNotifier::Cleared()
818 bool wxGtkDataViewListModelNotifier::ValueChanged( wxDataViewColumn
*view_column
, size_t model_column
, size_t row
)
820 wxDataViewCell
*cell
= view_column
->GetCell();
824 wxVariant variant
= m_wx_model
->GetValue( model_column
, row
);
825 cell
->SetValue( variant
);
827 GtkTreeView
*widget
= GTK_TREE_VIEW(view_column
->GetOwner()->m_treeview
);
828 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(view_column
->GetGtkHandle());
831 GtkTreePath
*path
= gtk_tree_path_new();
832 gtk_tree_path_append_index( path
, row
);
833 GdkRectangle cell_area
;
834 gtk_tree_view_get_cell_area( widget
, path
, column
, &cell_area
);
835 gtk_tree_path_free( path
);
837 int ydiff
= column
->button
->allocation
.height
;
839 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
840 cell_area
.x
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
845 // ---------------------------------------------------------
847 // ---------------------------------------------------------
849 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell
, wxDataViewCellBase
)
851 wxDataViewCell::wxDataViewCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
852 wxDataViewCellBase( varianttype
, mode
)
857 // ---------------------------------------------------------
858 // wxDataViewTextCell
859 // ---------------------------------------------------------
862 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
863 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
866 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
867 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
869 wxDataViewTextCell
*cell
= (wxDataViewTextCell
*) user_data
;
871 wxString tmp
= wxGTK_CONV_BACK( arg2
);
872 wxVariant value
= tmp
;
873 if (!cell
->Validate( value
))
876 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
878 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
879 size_t model_row
= (size_t)gtk_tree_path_get_indices (path
)[0];
880 gtk_tree_path_free( path
);
882 size_t model_col
= cell
->GetOwner()->GetModelColumn();
884 model
->SetValue( value
, model_col
, model_row
);
885 model
->ValueChanged( model_col
, model_row
);
888 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell
, wxDataViewCell
)
890 wxDataViewTextCell::wxDataViewTextCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
891 wxDataViewCell( varianttype
, mode
)
893 m_renderer
= (void*) gtk_cell_renderer_text_new();
895 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
897 GValue gvalue
= { 0, };
898 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
899 g_value_set_boolean( &gvalue
, true );
900 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
901 g_value_unset( &gvalue
);
903 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
907 bool wxDataViewTextCell::SetValue( const wxVariant
&value
)
909 wxString tmp
= value
;
911 GValue gvalue
= { 0, };
912 g_value_init( &gvalue
, G_TYPE_STRING
);
913 g_value_set_string( &gvalue
, wxGTK_CONV( tmp
) );
914 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
915 g_value_unset( &gvalue
);
920 bool wxDataViewTextCell::GetValue( wxVariant
&value
)
922 GValue gvalue
= { 0, };
923 g_value_init( &gvalue
, G_TYPE_STRING
);
924 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
925 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &gvalue
) );
926 g_value_unset( &gvalue
);
933 // ---------------------------------------------------------
934 // wxDataViewToggleCell
935 // ---------------------------------------------------------
938 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
939 gchar
*path
, gpointer user_data
);
942 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
943 gchar
*path
, gpointer user_data
)
945 wxDataViewToggleCell
*cell
= (wxDataViewToggleCell
*) user_data
;
948 GValue gvalue
= { 0, };
949 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
950 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
951 bool tmp
= g_value_get_boolean( &gvalue
);
952 g_value_unset( &gvalue
);
956 wxVariant value
= tmp
;
957 if (!cell
->Validate( value
))
960 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
962 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
963 size_t model_row
= (size_t)gtk_tree_path_get_indices (gtk_path
)[0];
964 gtk_tree_path_free( gtk_path
);
966 size_t model_col
= cell
->GetOwner()->GetModelColumn();
968 model
->SetValue( value
, model_col
, model_row
);
969 model
->ValueChanged( model_col
, model_row
);
972 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCell
)
974 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
975 wxDataViewCellMode mode
) :
976 wxDataViewCell( varianttype
, mode
)
978 m_renderer
= (void*) gtk_cell_renderer_toggle_new();
980 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
982 g_signal_connect_after( m_renderer
, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
987 GValue gvalue
= { 0, };
988 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
989 g_value_set_boolean( &gvalue
, false );
990 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
991 g_value_unset( &gvalue
);
993 GValue gvalue2
= { 0, };
994 g_value_init( &gvalue2
, gtk_cell_renderer_mode_get_type() );
995 g_value_set_enum( &gvalue2
, GTK_CELL_RENDERER_MODE_INERT
);
996 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue2
);
997 g_value_unset( &gvalue2
);
1002 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
1006 GValue gvalue
= { 0, };
1007 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1008 g_value_set_boolean( &gvalue
, tmp
);
1009 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1010 g_value_unset( &gvalue
);
1015 bool wxDataViewToggleCell::GetValue( wxVariant
&value
)
1017 GValue gvalue
= { 0, };
1018 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1019 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1020 bool tmp
= g_value_get_boolean( &gvalue
);
1021 g_value_unset( &gvalue
);
1028 // ---------------------------------------------------------
1029 // wxDataViewCustomCell
1030 // ---------------------------------------------------------
1032 class wxDataViewCtrlDC
: public wxWindowDC
1035 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1037 GtkWidget
*widget
= window
->m_treeview
;
1041 m_context
= window
->GtkGetPangoDefaultContext();
1042 m_layout
= pango_layout_new( m_context
);
1043 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1045 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1047 // Set m_window later
1049 // m_owner = window;
1053 // ---------------------------------------------------------
1054 // wxDataViewCustomCell
1055 // ---------------------------------------------------------
1057 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell
, wxDataViewCell
)
1059 wxDataViewCustomCell::wxDataViewCustomCell( const wxString
&varianttype
,
1060 wxDataViewCellMode mode
, bool no_init
) :
1061 wxDataViewCell( varianttype
, mode
)
1071 bool wxDataViewCustomCell::Init()
1073 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1074 renderer
->cell
= this;
1076 m_renderer
= (void*) renderer
;
1078 if (m_mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1080 GValue gvalue
= { 0, };
1081 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1082 g_value_set_enum( &gvalue
, GTK_CELL_RENDERER_MODE_ACTIVATABLE
);
1083 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1084 g_value_unset( &gvalue
);
1090 wxDataViewCustomCell::~wxDataViewCustomCell()
1096 wxDC
*wxDataViewCustomCell::GetDC()
1100 if (GetOwner() == NULL
)
1102 if (GetOwner()->GetOwner() == NULL
)
1104 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1110 // ---------------------------------------------------------
1111 // wxDataViewProgressCell
1112 // ---------------------------------------------------------
1114 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
1116 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
1117 const wxString
&varianttype
, wxDataViewCellMode mode
) :
1118 wxDataViewCustomCell( varianttype
, mode
, true )
1124 if (!gtk_check_version(2,6,0))
1126 m_renderer
= (void*) gtk_cell_renderer_progress_new();
1128 GValue gvalue
= { 0, };
1129 g_value_init( &gvalue
, G_TYPE_STRING
);
1130 g_value_set_boolean( &gvalue
, wxGTK_CONV(m_label
) );
1131 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1132 g_value_unset( &gvalue
);
1137 // Use custom cell code
1138 wxDataViewCustomCell::Init();
1142 wxDataViewProgressCell::~wxDataViewProgressCell()
1146 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
1149 if (!gtk_check_version(2,6,0))
1151 gint tmp
= (int) value
;
1152 GValue gvalue
= { 0, };
1153 g_value_init( &gvalue
, G_TYPE_INT
);
1154 g_value_set_boolean( &gvalue
, tmp
);
1155 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1156 g_value_unset( &gvalue
);
1161 m_value
= (long) value
;
1163 if (m_value
< 0) m_value
= 0;
1164 if (m_value
> 100) m_value
= 100;
1170 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1172 double pct
= (double)m_value
/ 100.0;
1174 bar
.width
= (int)(cell
.width
* pct
);
1175 dc
->SetPen( *wxTRANSPARENT_PEN
);
1176 dc
->SetBrush( *wxBLUE_BRUSH
);
1177 dc
->DrawRectangle( bar
);
1179 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1180 dc
->SetPen( *wxBLACK_PEN
);
1181 dc
->DrawRectangle( cell
);
1186 wxSize
wxDataViewProgressCell::GetSize()
1188 return wxSize(40,12);
1191 // ---------------------------------------------------------
1192 // wxDataViewDateCell
1193 // ---------------------------------------------------------
1195 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
1198 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1199 wxDataViewListModel
*model
, size_t col
, size_t row
) :
1200 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1205 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1206 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1207 sizer
->Add( m_cal
, 1, wxGROW
);
1212 virtual void OnDismiss()
1216 void OnCalendar( wxCalendarEvent
&event
);
1218 wxCalendarCtrl
*m_cal
;
1219 wxDataViewListModel
*m_model
;
1224 DECLARE_EVENT_TABLE()
1227 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
1228 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar
)
1231 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1233 wxDateTime date
= event
.GetDate();
1234 wxVariant value
= date
;
1235 m_model
->SetValue( value
, m_col
, m_row
);
1236 m_model
->ValueChanged( m_col
, m_row
);
1240 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
1242 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
1243 wxDataViewCellMode mode
) :
1244 wxDataViewCustomCell( varianttype
, mode
)
1248 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
1250 m_date
= value
.GetDateTime();
1255 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1257 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1258 wxString tmp
= m_date
.FormatDate();
1259 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1264 wxSize
wxDataViewDateCell::GetSize()
1266 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
1267 wxString tmp
= m_date
.FormatDate();
1269 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1270 return wxSize(x
,y
+d
);
1273 bool wxDataViewDateCell::Activate( wxRect cell
, wxDataViewListModel
*model
, size_t col
, size_t row
)
1275 wxVariant variant
= model
->GetValue( col
, row
);
1276 wxDateTime value
= variant
.GetDateTime();
1278 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
1279 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
1280 wxPoint pos
= wxGetMousePosition();
1283 popup
->Popup( popup
->m_cal
);
1288 // ---------------------------------------------------------
1290 // ---------------------------------------------------------
1293 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1294 GtkCellRenderer
*cell
,
1295 GtkTreeModel
*model
,
1301 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1302 GtkCellRenderer
*renderer
,
1303 GtkTreeModel
*model
,
1307 g_return_if_fail (GTK_IS_WX_LIST_STORE (model
));
1308 GtkWxListStore
*list_store
= (GtkWxListStore
*) model
;
1310 wxDataViewCell
*cell
= (wxDataViewCell
*) data
;
1312 size_t model_row
= (size_t) iter
->user_data
;
1314 wxVariant value
= list_store
->model
->GetValue(
1315 cell
->GetOwner()->GetModelColumn(), model_row
);
1317 if (value
.GetType() != cell
->GetVariantType())
1318 wxPrintf( wxT("Wrong type\n") );
1320 cell
->SetValue( value
);
1323 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1325 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
,
1326 size_t model_column
, int flags
) :
1327 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
1329 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) cell
->GetGtkHandle();
1331 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1333 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1335 gtk_tree_view_column_pack_start( column
, renderer
, TRUE
);
1337 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1338 wxGtkTreeCellDataFunc
, (gpointer
) cell
, NULL
);
1340 m_column
= (void*) column
;
1343 wxDataViewColumn::~wxDataViewColumn()
1347 void wxDataViewColumn::SetTitle( const wxString
&title
)
1349 wxDataViewColumnBase::SetTitle( title
);
1351 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)m_column
;
1352 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1355 //-----------------------------------------------------------------------------
1357 //-----------------------------------------------------------------------------
1359 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
1361 wxDataViewCtrl::~wxDataViewCtrl()
1365 void wxDataViewCtrl::Init()
1369 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
1370 const wxPoint
& pos
, const wxSize
& size
,
1371 long style
, const wxValidator
& validator
)
1375 m_needParent
= TRUE
;
1376 m_acceptsFocus
= TRUE
;
1378 if (!PreCreation( parent
, pos
, size
) ||
1379 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
1381 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1385 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
1386 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (m_widget
), GTK_SHADOW_IN
);
1388 m_treeview
= gtk_tree_view_new();
1389 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
1391 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
1392 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
1393 gtk_widget_show (m_treeview
);
1395 m_parent
->DoAddChild( this );
1402 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
1404 if (!wxDataViewCtrlBase::AssociateModel( model
))
1407 GtkWxListStore
*gtk_store
= wxgtk_list_store_new();
1408 gtk_store
->model
= model
;
1410 wxGtkDataViewListModelNotifier
*notifier
=
1411 new wxGtkDataViewListModelNotifier( gtk_store
, model
);
1413 model
->SetNotifier( notifier
);
1415 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_store
) );
1416 g_object_unref( gtk_store
);
1421 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
1423 if (!wxDataViewCtrlBase::AppendColumn(col
))
1426 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
1428 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
1434 #endif // wxUSE_DATAVIEWCTRL