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 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
;
84 static GtkWxListStore
*wxgtk_list_store_new (void);
85 static void wxgtk_list_store_init (GtkWxListStore
*list_store
);
86 static void wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
);
87 static void wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
);
88 static void wxgtk_list_store_finalize (GObject
*object
);
89 static GtkTreeModelFlags
wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
);
90 static gint
wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
);
91 static GType
wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
93 static gboolean
wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
96 static GtkTreePath
*wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
98 static void wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
102 static gboolean
wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
104 static gboolean
wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
106 GtkTreeIter
*parent
);
107 static gboolean
wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
109 static gint
wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
111 static gboolean
wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
115 static gboolean
wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
119 static GObjectClass
*list_parent_class
= NULL
;
122 gtk_wx_list_store_get_type (void)
124 static GType list_store_type
= 0;
126 if (!list_store_type
)
128 const GTypeInfo list_store_info
=
130 sizeof (GtkWxListStoreClass
),
131 NULL
, /* base_init */
132 NULL
, /* base_finalize */
133 (GClassInitFunc
) wxgtk_list_store_class_init
,
134 NULL
, /* class_finalize */
135 NULL
, /* class_data */
136 sizeof (GtkWxListStore
),
138 (GInstanceInitFunc
) wxgtk_list_store_init
,
141 static const GInterfaceInfo tree_model_info
=
143 (GInterfaceInitFunc
) wxgtk_list_store_tree_model_init
,
148 list_store_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxListStore",
149 &list_store_info
, (GTypeFlags
)0 );
151 g_type_add_interface_static (list_store_type
,
156 return list_store_type
;
159 static GtkWxListStore
*
160 wxgtk_list_store_new(void)
162 GtkWxListStore
*retval
= (GtkWxListStore
*) g_object_new (GTK_TYPE_WX_LIST_STORE
, NULL
);
167 wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
)
169 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
170 GObjectClass
*object_class
= (GObjectClass
*) klass
;
171 object_class
->finalize
= wxgtk_list_store_finalize
;
175 wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
)
177 iface
->get_flags
= wxgtk_list_store_get_flags
;
178 iface
->get_n_columns
= wxgtk_list_store_get_n_columns
;
179 iface
->get_column_type
= wxgtk_list_store_get_column_type
;
180 iface
->get_iter
= wxgtk_list_store_get_iter
;
181 iface
->get_path
= wxgtk_list_store_get_path
;
182 iface
->get_value
= wxgtk_list_store_get_value
;
183 iface
->iter_next
= wxgtk_list_store_iter_next
;
184 iface
->iter_children
= wxgtk_list_store_iter_children
;
185 iface
->iter_has_child
= wxgtk_list_store_iter_has_child
;
186 iface
->iter_n_children
= wxgtk_list_store_iter_n_children
;
187 iface
->iter_nth_child
= wxgtk_list_store_iter_nth_child
;
188 iface
->iter_parent
= wxgtk_list_store_iter_parent
;
192 wxgtk_list_store_init (GtkWxListStore
*list_store
)
194 list_store
->model
= NULL
;
195 list_store
->stamp
= g_random_int();
199 wxgtk_list_store_finalize (GObject
*object
)
201 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
203 /* we need to sort out, which class deletes what */
204 /* delete list_store->model; */
207 (* list_parent_class
->finalize
) (object
);
212 //-----------------------------------------------------------------------------
213 // implement callbacks from wxGtkListStore class by letting
214 // them call the methods of wxWidgets' wxDataViewListModel
215 //-----------------------------------------------------------------------------
217 static GtkTreeModelFlags
218 wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
)
220 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), (GtkTreeModelFlags
)0 );
222 // GTK+ list store uses a linked list for storing the
223 // items and a pointer to a child is used as the member
224 // field of a GtkTreeIter. This means that the iter is
225 // valid in the GtkListStore as long as the child exists.
226 // We use the index of the row and since the index of a
227 // specific row will change if a row above is deleted,
228 // the iter does not persist
229 return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY
;
233 wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
)
235 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
236 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), 0);
238 return list_store
->model
->GetColumnCount();
242 wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
245 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
246 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), G_TYPE_INVALID
);
248 GType gtype
= G_TYPE_INVALID
;
250 wxString wxtype
= list_store
->model
->GetColumnType( (unsigned int) index
);
252 if (wxtype
== wxT("string"))
253 gtype
= G_TYPE_STRING
;
256 wxFAIL_MSG( _T("non-string columns not supported yet") );
263 wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
267 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
268 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
269 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
271 unsigned int i
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
273 if (i
>= list_store
->model
->GetRowCount())
276 iter
->stamp
= list_store
->stamp
;
277 // user_data is just the index
278 iter
->user_data
= (gpointer
) i
;
284 wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
287 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), NULL
);
288 g_return_val_if_fail (iter
->stamp
== GTK_WX_LIST_STORE (tree_model
)->stamp
, NULL
);
290 GtkTreePath
*retval
= gtk_tree_path_new ();
291 // user_data is just the index
292 int i
= (wxUIntPtr
) iter
->user_data
;
293 gtk_tree_path_append_index (retval
, i
);
298 wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
303 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
304 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model
) );
306 wxDataViewListModel
*model
= list_store
->model
;
307 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
308 if (mtype
== wxT("string"))
311 g_value_init( value
, G_TYPE_STRING
);
312 model
->GetValue( variant
,
313 (unsigned int) column
,
314 (unsigned int) iter
->user_data
);
316 // FIXME: we should support different encodings here
317 g_value_set_string( value
, wxGTK_CONV_SYS(variant
.GetString()) );
321 wxFAIL_MSG( _T("non-string columns not supported yet") );
326 wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
329 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
330 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
332 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, FALSE
);
334 int n
= (wxUIntPtr
) iter
->user_data
;
339 if (n
>= (int) list_store
->model
->GetRowCount()-1)
342 iter
->user_data
= (gpointer
) ++n
;
348 wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
352 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
353 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
355 // this is a list, nodes have no children
359 iter
->stamp
= list_store
->stamp
;
360 iter
->user_data
= (gpointer
) -1;
366 wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
373 wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
376 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), -1);
377 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
380 return (gint
) list_store
->model
->GetRowCount();
382 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, -1);
388 wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
393 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
394 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
402 if (n
>= (gint
) list_store
->model
->GetRowCount())
405 iter
->stamp
= list_store
->stamp
;
406 iter
->user_data
= (gpointer
) n
;
412 wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
419 //-----------------------------------------------------------------------------
420 // define new GTK+ class wxGtkRendererRenderer
421 //-----------------------------------------------------------------------------
425 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
426 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
427 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
428 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
429 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
430 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
432 GType
gtk_wx_cell_renderer_get_type (void);
434 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
435 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
437 struct _GtkWxCellRenderer
439 GtkCellRenderer parent
;
442 wxDataViewCustomRenderer
*cell
;
446 struct _GtkWxCellRendererClass
448 GtkCellRendererClass cell_parent_class
;
452 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
453 static void gtk_wx_cell_renderer_init (
454 GtkWxCellRenderer
*cell
);
455 static void gtk_wx_cell_renderer_class_init(
456 GtkWxCellRendererClass
*klass
);
457 static void gtk_wx_cell_renderer_finalize (
459 static void gtk_wx_cell_renderer_get_size (
460 GtkCellRenderer
*cell
,
462 GdkRectangle
*rectangle
,
467 static void gtk_wx_cell_renderer_render (
468 GtkCellRenderer
*cell
,
471 GdkRectangle
*background_area
,
472 GdkRectangle
*cell_area
,
473 GdkRectangle
*expose_area
,
474 GtkCellRendererState flags
);
475 static gboolean
gtk_wx_cell_renderer_activate(
476 GtkCellRenderer
*cell
,
480 GdkRectangle
*background_area
,
481 GdkRectangle
*cell_area
,
482 GtkCellRendererState flags
);
484 static GObjectClass
*cell_parent_class
= NULL
;
489 gtk_wx_cell_renderer_get_type (void)
491 static GType cell_wx_type
= 0;
495 const GTypeInfo cell_wx_info
=
497 sizeof (GtkWxCellRendererClass
),
498 NULL
, /* base_init */
499 NULL
, /* base_finalize */
500 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
501 NULL
, /* class_finalize */
502 NULL
, /* class_data */
503 sizeof (GtkWxCellRenderer
),
505 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
508 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
509 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
516 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
519 cell
->last_click
= 0;
523 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
525 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
526 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
528 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
530 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
532 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
533 cell_class
->render
= gtk_wx_cell_renderer_render
;
534 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
538 gtk_wx_cell_renderer_finalize (GObject
*object
)
541 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
545 gtk_wx_cell_renderer_new (void)
547 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
551 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
553 GdkRectangle
*cell_area
,
559 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
560 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
562 wxSize size
= cell
->GetSize();
564 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
565 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
572 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
576 *x_offset
= (gint
)((renderer
->xalign
*
577 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
578 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
582 *y_offset
= (gint
)((renderer
->yalign
*
583 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
584 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
592 *height
= calc_height
;
596 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
599 GdkRectangle
*background_area
,
600 GdkRectangle
*cell_area
,
601 GdkRectangle
*expose_area
,
602 GtkCellRendererState flags
)
605 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
606 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
609 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
615 rect
.x
+= cell_area
->x
;
616 rect
.y
+= cell_area
->y
;
617 rect
.width
-= renderer
->xpad
* 2;
618 rect
.height
-= renderer
->ypad
* 2;
621 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
623 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
624 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
625 if (dc
->m_window
== NULL
)
627 dc
->m_window
= window
;
632 if (flags
& GTK_CELL_RENDERER_SELECTED
)
633 state
|= wxDATAVIEW_CELL_SELECTED
;
634 if (flags
& GTK_CELL_RENDERER_PRELIT
)
635 state
|= wxDATAVIEW_CELL_PRELIT
;
636 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
637 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
638 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
639 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
640 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
641 state
|= wxDATAVIEW_CELL_FOCUSED
;
642 cell
->Render( renderrect
, dc
, state
);
647 gtk_wx_cell_renderer_activate(
648 GtkCellRenderer
*renderer
,
652 GdkRectangle
*background_area
,
653 GdkRectangle
*cell_area
,
654 GtkCellRendererState flags
)
656 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
657 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
660 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
666 rect
.x
+= cell_area
->x
;
667 rect
.y
+= cell_area
->y
;
668 rect
.width
-= renderer
->xpad
* 2;
669 rect
.height
-= renderer
->ypad
* 2;
671 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
673 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
675 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
676 unsigned int model_row
= (unsigned int)gtk_tree_path_get_indices (treepath
)[0];
677 gtk_tree_path_free( treepath
);
679 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
681 if (event
->type
== GDK_BUTTON_PRESS
)
683 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
684 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
685 ((int) button_event
->y
) - renderrect
.y
);
688 if (button_event
->button
== 1)
690 if (cell
->LeftClick( pt
, renderrect
, model
, model_col
, model_row
))
692 // TODO: query system double-click time
693 if (button_event
->time
- wxrenderer
->last_click
< 400)
694 if (cell
->Activate( renderrect
, model
, model_col
, model_row
))
697 if (button_event
->button
== 3)
699 if (cell
->RightClick( pt
, renderrect
, model
, model_col
, model_row
))
703 wxrenderer
->last_click
= button_event
->time
;
711 // ---------------------------------------------------------
712 // wxGtkDataViewListModelNotifier
713 // ---------------------------------------------------------
715 class wxGtkDataViewListModelNotifier
: public wxDataViewListModelNotifier
718 wxGtkDataViewListModelNotifier( GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
);
720 virtual bool RowAppended();
721 virtual bool RowPrepended();
722 virtual bool RowInserted( unsigned int before
);
723 virtual bool RowDeleted( unsigned int row
);
724 virtual bool RowChanged( unsigned int row
);
725 virtual bool ValueChanged( unsigned int col
, unsigned int row
);
726 virtual bool RowsReordered( unsigned int *new_order
);
727 virtual bool Cleared();
733 return wxDataViewListModelNotifier::Freed();
736 GtkWxListStore
*m_gtk_store
;
737 wxDataViewListModel
*m_wx_model
;
740 // ---------------------------------------------------------
741 // wxGtkDataViewListModelNotifier
742 // ---------------------------------------------------------
744 wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
745 GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
)
747 m_gtk_store
= gtk_store
;
748 m_wx_model
= wx_model
;
751 bool wxGtkDataViewListModelNotifier::RowAppended()
753 unsigned int pos
= m_wx_model
->GetRowCount()-1;
756 iter
.stamp
= m_gtk_store
->stamp
;
757 iter
.user_data
= (gpointer
) pos
;
759 GtkTreePath
*path
= gtk_tree_path_new ();
760 gtk_tree_path_append_index (path
, (gint
) pos
);
761 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
762 gtk_tree_path_free (path
);
767 bool wxGtkDataViewListModelNotifier::RowPrepended()
770 iter
.stamp
= m_gtk_store
->stamp
;
771 iter
.user_data
= (gpointer
) 0;
773 GtkTreePath
*path
= gtk_tree_path_new ();
774 gtk_tree_path_append_index (path
, (gint
) 0);
775 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
776 gtk_tree_path_free (path
);
781 bool wxGtkDataViewListModelNotifier::RowInserted( unsigned int before
)
784 iter
.stamp
= m_gtk_store
->stamp
;
785 iter
.user_data
= (gpointer
) before
;
787 GtkTreePath
*path
= gtk_tree_path_new ();
788 gtk_tree_path_append_index (path
, (gint
) before
);
789 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
790 gtk_tree_path_free (path
);
795 bool wxGtkDataViewListModelNotifier::RowDeleted( unsigned int row
)
797 GtkTreePath
*path
= gtk_tree_path_new ();
798 gtk_tree_path_append_index (path
, (gint
) row
);
799 gtk_tree_model_row_deleted (GTK_TREE_MODEL (m_gtk_store
), path
);
800 gtk_tree_path_free (path
);
805 bool wxGtkDataViewListModelNotifier::RowChanged( unsigned int row
)
808 iter
.stamp
= m_gtk_store
->stamp
;
809 iter
.user_data
= (gpointer
) row
;
810 GtkTreePath
*path
= gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store
), &iter
);
811 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
812 gtk_tree_path_free (path
);
817 bool wxGtkDataViewListModelNotifier::ValueChanged( unsigned int model_col
, unsigned int model_row
)
819 // This adds GTK+'s missing MVC logic for ValueChanged
820 wxObjectList::compatibility_iterator
821 node
= GetOwner()->m_viewingColumns
.GetFirst();
824 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
825 if (viewing_column
->m_modelColumn
== model_col
)
827 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
828 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(viewing_column
->m_viewColumn
->GetGtkHandle());
831 GtkTreePath
*path
= gtk_tree_path_new();
832 gtk_tree_path_append_index( path
, model_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
);
843 node
= node
->GetNext();
849 bool wxGtkDataViewListModelNotifier::RowsReordered( unsigned int *new_order
)
851 // Assume sizeof(unsigned int)= == sizeof(gint)
853 GtkTreePath
*path
= gtk_tree_path_new ();
854 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store
), path
, NULL
, (gint
*)new_order
);
855 gtk_tree_path_free (path
);
857 // This adds GTK+'s missing MVC logic for RowsReordered
858 wxObjectList::compatibility_iterator
859 node
= GetOwner()->m_viewingColumns
.GetFirst();
862 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
863 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
864 // Doesn't work yet...
865 gtk_widget_queue_draw( GTK_WIDGET(widget
) );
867 node
= node
->GetNext();
873 bool wxGtkDataViewListModelNotifier::Cleared()
878 // ---------------------------------------------------------
879 // wxDataViewRenderer
880 // ---------------------------------------------------------
882 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
884 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
886 wxDataViewRendererBase( varianttype
, mode
, align
)
890 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
891 // after the m_renderer pointer has been initialized
894 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
896 GtkCellRendererMode gtkMode
;
899 case wxDATAVIEW_CELL_INERT
:
900 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
902 case wxDATAVIEW_CELL_ACTIVATABLE
:
903 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
905 case wxDATAVIEW_CELL_EDITABLE
:
906 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
910 GValue gvalue
= { 0, };
911 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
912 g_value_set_enum( &gvalue
, gtkMode
);
913 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
914 g_value_unset( &gvalue
);
917 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
919 wxDataViewCellMode ret
;
922 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
924 switch (g_value_get_enum(&gvalue
))
926 case GTK_CELL_RENDERER_MODE_INERT
:
927 ret
= wxDATAVIEW_CELL_INERT
;
929 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
930 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
932 case GTK_CELL_RENDERER_MODE_EDITABLE
:
933 ret
= wxDATAVIEW_CELL_EDITABLE
;
937 g_value_unset( &gvalue
);
942 void wxDataViewRenderer::SetAlignment( int align
)
944 // horizontal alignment:
947 if (align
& wxALIGN_RIGHT
)
949 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
952 GValue gvalue
= { 0, };
953 g_value_init( &gvalue
, G_TYPE_FLOAT
);
954 g_value_set_float( &gvalue
, xalign
);
955 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
956 g_value_unset( &gvalue
);
958 // vertical alignment:
961 if (align
& wxALIGN_BOTTOM
)
963 else if (align
& wxALIGN_CENTER_VERTICAL
)
966 GValue gvalue2
= { 0, };
967 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
968 g_value_set_float( &gvalue2
, yalign
);
969 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
970 g_value_unset( &gvalue2
);
973 int wxDataViewRenderer::GetAlignment() const
978 // horizontal alignment:
980 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
981 float xalign
= g_value_get_float( &gvalue
);
984 else if (xalign
== 0.5)
985 ret
|= wxALIGN_CENTER_HORIZONTAL
;
987 ret
|= wxALIGN_RIGHT
;
988 g_value_unset( &gvalue
);
991 // vertical alignment:
993 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
994 float yalign
= g_value_get_float( &gvalue
);
997 else if (yalign
== 0.5)
998 ret
|= wxALIGN_CENTER_VERTICAL
;
1000 ret
|= wxALIGN_BOTTOM
;
1001 g_value_unset( &gvalue
);
1008 // ---------------------------------------------------------
1009 // wxDataViewTextRenderer
1010 // ---------------------------------------------------------
1013 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1014 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1017 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1018 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1020 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1022 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1023 wxVariant value
= tmp
;
1024 if (!cell
->Validate( value
))
1027 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1029 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1030 unsigned int model_row
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
1031 gtk_tree_path_free( path
);
1033 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1035 model
->SetValue( value
, model_col
, model_row
);
1036 model
->ValueChanged( model_col
, model_row
);
1039 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1041 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1043 wxDataViewRenderer( varianttype
, mode
, align
)
1045 m_renderer
= (GtkWidget
*) gtk_cell_renderer_text_new();
1047 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1049 GValue gvalue
= { 0, };
1050 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1051 g_value_set_boolean( &gvalue
, true );
1052 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1053 g_value_unset( &gvalue
);
1055 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1059 SetAlignment(align
);
1062 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1064 wxString tmp
= value
;
1066 GValue gvalue
= { 0, };
1067 g_value_init( &gvalue
, G_TYPE_STRING
);
1068 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( tmp
, GetOwner()->GetOwner()->GetFont() ) );
1069 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1070 g_value_unset( &gvalue
);
1075 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1077 GValue gvalue
= { 0, };
1078 g_value_init( &gvalue
, G_TYPE_STRING
);
1079 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1080 wxString tmp
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
),
1081 wx_const_cast(wxDataViewTextRenderer
*, this)->GetOwner()->GetOwner()->GetFont() );
1082 g_value_unset( &gvalue
);
1089 void wxDataViewTextRenderer::SetAlignment( int align
)
1091 wxDataViewRenderer::SetAlignment(align
);
1093 // horizontal alignment:
1095 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1096 if (align
& wxALIGN_RIGHT
)
1097 pangoAlign
= PANGO_ALIGN_RIGHT
;
1098 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1099 pangoAlign
= PANGO_ALIGN_CENTER
;
1101 GValue gvalue
= { 0, };
1102 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1103 g_value_set_enum( &gvalue
, pangoAlign
);
1104 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1105 g_value_unset( &gvalue
);
1108 // ---------------------------------------------------------
1109 // wxDataViewBitmapRenderer
1110 // ---------------------------------------------------------
1112 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1114 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1116 wxDataViewRenderer( varianttype
, mode
, align
)
1118 m_renderer
= (GtkWidget
*) gtk_cell_renderer_pixbuf_new();
1121 SetAlignment(align
);
1124 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1126 if (value
.GetType() == wxT("wxBitmap"))
1131 // This may create a Pixbuf representation in the
1132 // wxBitmap object (and it will stay there)
1133 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1135 GValue gvalue
= { 0, };
1136 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1137 g_value_set_object( &gvalue
, pixbuf
);
1138 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1139 g_value_unset( &gvalue
);
1144 if (value
.GetType() == wxT("wxIcon"))
1149 // This may create a Pixbuf representation in the
1150 // wxBitmap object (and it will stay there)
1151 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1153 GValue gvalue
= { 0, };
1154 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1155 g_value_set_object( &gvalue
, pixbuf
);
1156 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1157 g_value_unset( &gvalue
);
1165 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1170 // ---------------------------------------------------------
1171 // wxDataViewToggleRenderer
1172 // ---------------------------------------------------------
1175 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1176 gchar
*path
, gpointer user_data
);
1179 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1180 gchar
*path
, gpointer user_data
)
1182 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1185 GValue gvalue
= { 0, };
1186 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1187 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1188 bool tmp
= g_value_get_boolean( &gvalue
);
1189 g_value_unset( &gvalue
);
1193 wxVariant value
= tmp
;
1194 if (!cell
->Validate( value
))
1197 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1199 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1200 unsigned int model_row
= (unsigned int)gtk_tree_path_get_indices (gtk_path
)[0];
1201 gtk_tree_path_free( gtk_path
);
1203 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1205 model
->SetValue( value
, model_col
, model_row
);
1206 model
->ValueChanged( model_col
, model_row
);
1209 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1211 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1212 wxDataViewCellMode mode
, int align
) :
1213 wxDataViewRenderer( varianttype
, mode
, align
)
1215 m_renderer
= (GtkWidget
*) gtk_cell_renderer_toggle_new();
1217 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1219 g_signal_connect_after( m_renderer
, "toggled",
1220 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1224 GValue gvalue
= { 0, };
1225 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1226 g_value_set_boolean( &gvalue
, false );
1227 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1228 g_value_unset( &gvalue
);
1232 SetAlignment(align
);
1235 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1239 GValue gvalue
= { 0, };
1240 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1241 g_value_set_boolean( &gvalue
, tmp
);
1242 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1243 g_value_unset( &gvalue
);
1248 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1250 GValue gvalue
= { 0, };
1251 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1252 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1253 bool tmp
= g_value_get_boolean( &gvalue
);
1254 g_value_unset( &gvalue
);
1261 // ---------------------------------------------------------
1262 // wxDataViewCustomRenderer
1263 // ---------------------------------------------------------
1265 class wxDataViewCtrlDC
: public wxWindowDC
1268 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1270 GtkWidget
*widget
= window
->m_treeview
;
1274 m_context
= window
->GtkGetPangoDefaultContext();
1275 m_layout
= pango_layout_new( m_context
);
1276 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1278 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1280 // Set m_window later
1282 // m_owner = window;
1286 // ---------------------------------------------------------
1287 // wxDataViewCustomRenderer
1288 // ---------------------------------------------------------
1290 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1292 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1293 wxDataViewCellMode mode
, int align
,
1295 wxDataViewRenderer( varianttype
, mode
, align
)
1305 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1307 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1308 renderer
->cell
= this;
1310 m_renderer
= (GtkWidget
*) renderer
;
1313 SetAlignment(align
);
1318 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1324 wxDC
*wxDataViewCustomRenderer::GetDC()
1328 if (GetOwner() == NULL
)
1330 if (GetOwner()->GetOwner() == NULL
)
1332 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1338 // ---------------------------------------------------------
1339 // wxDataViewProgressRenderer
1340 // ---------------------------------------------------------
1342 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1344 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1345 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1346 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1352 if (!gtk_check_version(2,6,0))
1354 m_renderer
= (GtkWidget
*) gtk_cell_renderer_progress_new();
1356 GValue gvalue
= { 0, };
1357 g_value_init( &gvalue
, G_TYPE_STRING
);
1359 // FIXME: font encoding support
1360 g_value_set_string( &gvalue
, wxGTK_CONV_SYS(m_label
) );
1361 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1362 g_value_unset( &gvalue
);
1365 SetAlignment(align
);
1370 // Use custom cell code
1371 wxDataViewCustomRenderer::Init(mode
, align
);
1375 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1379 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1382 if (!gtk_check_version(2,6,0))
1384 gint tmp
= (long) value
;
1385 GValue gvalue
= { 0, };
1386 g_value_init( &gvalue
, G_TYPE_INT
);
1387 g_value_set_int( &gvalue
, tmp
);
1388 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1389 g_value_unset( &gvalue
);
1394 m_value
= (long) value
;
1396 if (m_value
< 0) m_value
= 0;
1397 if (m_value
> 100) m_value
= 100;
1403 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1408 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1410 double pct
= (double)m_value
/ 100.0;
1412 bar
.width
= (int)(cell
.width
* pct
);
1413 dc
->SetPen( *wxTRANSPARENT_PEN
);
1414 dc
->SetBrush( *wxBLUE_BRUSH
);
1415 dc
->DrawRectangle( bar
);
1417 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1418 dc
->SetPen( *wxBLACK_PEN
);
1419 dc
->DrawRectangle( cell
);
1424 wxSize
wxDataViewProgressRenderer::GetSize() const
1426 return wxSize(40,12);
1429 // ---------------------------------------------------------
1430 // wxDataViewDateRenderer
1431 // ---------------------------------------------------------
1433 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1436 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1437 wxDataViewListModel
*model
, unsigned int col
, unsigned int row
) :
1438 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1443 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1444 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1445 sizer
->Add( m_cal
, 1, wxGROW
);
1450 virtual void OnDismiss()
1454 void OnCalendar( wxCalendarEvent
&event
);
1456 wxCalendarCtrl
*m_cal
;
1457 wxDataViewListModel
*m_model
;
1462 DECLARE_EVENT_TABLE()
1465 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1466 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1469 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1471 wxDateTime date
= event
.GetDate();
1472 wxVariant value
= date
;
1473 m_model
->SetValue( value
, m_col
, m_row
);
1474 m_model
->ValueChanged( m_col
, m_row
);
1478 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1480 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1481 wxDataViewCellMode mode
, int align
) :
1482 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1485 SetAlignment(align
);
1488 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1490 m_date
= value
.GetDateTime();
1495 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1500 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1502 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1503 wxString tmp
= m_date
.FormatDate();
1504 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1509 wxSize
wxDataViewDateRenderer::GetSize() const
1511 wxString tmp
= m_date
.FormatDate();
1513 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1514 return wxSize(x
,y
+d
);
1517 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewListModel
*model
,
1518 unsigned int col
, unsigned int row
)
1521 model
->GetValue( variant
, col
, row
);
1522 wxDateTime value
= variant
.GetDateTime();
1524 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1525 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
1526 wxPoint pos
= wxGetMousePosition();
1529 popup
->Popup( popup
->m_cal
);
1534 // ---------------------------------------------------------
1536 // ---------------------------------------------------------
1540 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
1541 GdkEventButton
*gdk_event
,
1542 wxDataViewColumn
*column
)
1544 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
1547 if (gdk_event
->button
== 1)
1549 wxDataViewCtrl
*dv
= column
->GetOwner();
1550 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
1551 event
.SetDataViewColumn( column
);
1552 event
.SetModel( dv
->GetModel() );
1553 dv
->GetEventHandler()->ProcessEvent( event
);
1560 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1561 GtkCellRenderer
*cell
,
1562 GtkTreeModel
*model
,
1568 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1569 GtkCellRenderer
*renderer
,
1570 GtkTreeModel
*model
,
1574 g_return_if_fail (GTK_IS_WX_LIST_STORE (model
));
1575 GtkWxListStore
*list_store
= (GtkWxListStore
*) model
;
1577 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
1579 unsigned int model_row
= (unsigned int) iter
->user_data
;
1582 list_store
->model
->GetValue( value
, cell
->GetOwner()->GetModelColumn(), model_row
);
1584 if (value
.GetType() != cell
->GetVariantType())
1585 wxLogError( wxT("Wrong type, required: %s but: %s"),
1586 value
.GetType().c_str(),
1587 cell
->GetVariantType().c_str() );
1589 cell
->SetValue( value
);
1592 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1594 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1595 unsigned int model_column
, int width
,
1596 wxAlignment align
, int flags
) :
1597 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1599 Init( align
, flags
, width
);
1601 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
1605 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1606 unsigned int model_column
, int width
,
1607 wxAlignment align
, int flags
) :
1608 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1610 Init( align
, flags
, width
);
1612 SetBitmap( bitmap
);
1615 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
1617 m_isConnected
= false;
1619 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
1620 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1621 m_column
= (GtkWidget
*) column
;
1624 SetAlignment( align
);
1626 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1627 // as GTK+ is smart and unless explicitely told, will set the minimal
1628 // width to the title's lenght, which is a better default
1630 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1631 // that we use for the wxDataViewCtrl
1632 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1633 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
1635 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
1637 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1638 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
1641 wxDataViewColumn::~wxDataViewColumn()
1645 void wxDataViewColumn::OnInternalIdle()
1650 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
1652 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1655 g_signal_connect(column
->button
, "button_press_event",
1656 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
1658 m_isConnected
= true;
1663 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
1665 wxDataViewColumnBase::SetOwner( owner
);
1667 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1669 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1672 void wxDataViewColumn::SetTitle( const wxString
&title
)
1674 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1678 // disconnect before column->button gets recreated
1679 g_signal_handlers_disconnect_by_func( column
->button
,
1680 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
1682 m_isConnected
= false;
1685 // FIXME: can it really happen that we don't have the owner here??
1686 wxDataViewCtrl
*ctrl
= GetOwner();
1687 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
1688 : wxGTK_CONV_SYS(title
) );
1690 gtk_tree_view_column_set_widget( column
, NULL
);
1693 wxString
wxDataViewColumn::GetTitle() const
1695 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
1696 return wxConvFileName
->cMB2WX(str
);
1699 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
1701 wxDataViewColumnBase::SetBitmap( bitmap
);
1703 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1706 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
1708 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1709 if (bitmap
.GetMask())
1710 mask
= bitmap
.GetMask()->GetBitmap();
1712 if (bitmap
.HasPixbuf())
1714 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
1715 bitmap
.GetPixbuf());
1719 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
1720 bitmap
.GetPixmap(), mask
);
1722 gtk_widget_show( GTK_WIDGET(gtk_image
) );
1724 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
1728 gtk_tree_view_column_set_widget( column
, NULL
);
1732 void wxDataViewColumn::SetHidden( bool hidden
)
1734 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
1737 void wxDataViewColumn::SetResizeable( bool resizeable
)
1739 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
1742 void wxDataViewColumn::SetAlignment( wxAlignment align
)
1744 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1746 gfloat xalign
= 0.0;
1747 if (align
== wxALIGN_RIGHT
)
1749 if (align
== wxALIGN_CENTER_HORIZONTAL
||
1750 align
== wxALIGN_CENTER
)
1753 gtk_tree_view_column_set_alignment( column
, xalign
);
1756 wxAlignment
wxDataViewColumn::GetAlignment() const
1758 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
1761 return wxALIGN_RIGHT
;
1763 return wxALIGN_CENTER_HORIZONTAL
;
1765 return wxALIGN_LEFT
;
1768 void wxDataViewColumn::SetSortable( bool sortable
)
1770 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1771 gtk_tree_view_column_set_sort_indicator( column
, sortable
);
1774 bool wxDataViewColumn::IsSortable() const
1776 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1777 return gtk_tree_view_column_get_sort_indicator( column
);
1780 bool wxDataViewColumn::IsResizeable() const
1782 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1783 return gtk_tree_view_column_get_resizable( column
);
1786 bool wxDataViewColumn::IsHidden() const
1788 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1789 return !gtk_tree_view_column_get_visible( column
);
1792 void wxDataViewColumn::SetSortOrder( bool ascending
)
1794 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1797 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
1799 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
1802 bool wxDataViewColumn::IsSortOrderAscending() const
1804 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1806 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
1809 void wxDataViewColumn::SetMinWidth( int width
)
1811 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
1814 int wxDataViewColumn::GetMinWidth() const
1816 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
1819 int wxDataViewColumn::GetWidth() const
1821 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
1824 void wxDataViewColumn::SetWidth( int width
)
1826 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
1830 //-----------------------------------------------------------------------------
1831 // wxDataViewCtrl signal callbacks
1832 //-----------------------------------------------------------------------------
1835 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
1837 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
1840 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ROW_SELECTED
, dv
->GetId() );
1841 event
.SetRow( dv
->GetSelection() );
1842 event
.SetModel( dv
->GetModel() );
1843 dv
->GetEventHandler()->ProcessEvent( event
);
1847 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
1848 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
1850 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED
, dv
->GetId() );
1851 unsigned int row
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
1852 event
.SetRow( row
);
1853 event
.SetModel( dv
->GetModel() );
1854 dv
->GetEventHandler()->ProcessEvent( event
);
1857 //-----------------------------------------------------------------------------
1859 //-----------------------------------------------------------------------------
1861 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
1863 wxDataViewCtrl::~wxDataViewCtrl()
1866 GetModel()->RemoveNotifier( m_notifier
);
1868 // remove the model from the GtkTreeView before it gets destroyed by the
1869 // wxDataViewCtrlBase's dtor
1870 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
1873 void wxDataViewCtrl::Init()
1878 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
1879 const wxPoint
& pos
, const wxSize
& size
,
1880 long style
, const wxValidator
& validator
)
1884 m_needParent
= true;
1886 if (!PreCreation( parent
, pos
, size
) ||
1887 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
1889 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1893 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
1895 GtkScrolledWindowSetBorder(m_widget
, style
);
1897 m_treeview
= gtk_tree_view_new();
1898 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
1901 if (!gtk_check_version(2,6,0))
1902 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
1905 if (style
& wxDV_MULTIPLE
)
1907 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
1908 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
1911 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
1914 if (!gtk_check_version(2,10,0))
1916 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
1918 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
1919 (style
& wxDV_VERT_RULES
) != 0)
1920 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
1921 else if (style
& wxDV_VERT_RULES
)
1922 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
1923 else if (style
& wxDV_HORIZ_RULES
)
1924 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
1926 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
1930 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
1932 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
1933 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
1934 gtk_widget_show (m_treeview
);
1936 m_parent
->DoAddChild( this );
1940 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
1941 g_signal_connect_after (selection
, "changed",
1942 G_CALLBACK (wxdataview_selection_changed_callback
), this);
1943 g_signal_connect_after (m_treeview
, "row_activated",
1944 G_CALLBACK (wxdataview_row_activated_callback
), this);
1949 void wxDataViewCtrl::OnInternalIdle()
1951 wxWindow::OnInternalIdle();
1953 unsigned int cols
= GetColumnCount();
1955 for (i
= 0; i
< cols
; i
++)
1957 wxDataViewColumn
*col
= GetColumn( i
);
1958 col
->OnInternalIdle();
1962 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
1964 if (!wxDataViewCtrlBase::AssociateModel( model
))
1967 GtkWxListStore
*gtk_store
= wxgtk_list_store_new();
1968 gtk_store
->model
= model
;
1970 m_notifier
= new wxGtkDataViewListModelNotifier( gtk_store
, model
);
1972 model
->AddNotifier( m_notifier
);
1974 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_store
) );
1975 g_object_unref( gtk_store
);
1980 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
1982 if (!wxDataViewCtrlBase::AppendColumn(col
))
1985 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
1987 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
1992 void wxDataViewCtrl::SetSelection( int row
)
1994 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
1998 gtk_tree_selection_unselect_all( selection
);
2002 GtkTreePath
*path
= gtk_tree_path_new ();
2003 gtk_tree_path_append_index( path
, row
);
2005 gtk_tree_selection_select_path( selection
, path
);
2007 gtk_tree_path_free( path
);
2011 void wxDataViewCtrl::Unselect( unsigned int row
)
2013 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2015 GtkTreePath
*path
= gtk_tree_path_new ();
2016 gtk_tree_path_append_index( path
, row
);
2018 gtk_tree_selection_unselect_path( selection
, path
);
2020 gtk_tree_path_free( path
);
2023 void wxDataViewCtrl::SetSelectionRange( unsigned int from
, unsigned int to
)
2027 void wxDataViewCtrl::SetSelections( const wxArrayInt
& aSelections
)
2031 bool wxDataViewCtrl::IsSelected( unsigned int row
) const
2033 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2035 GtkTreePath
*path
= gtk_tree_path_new ();
2036 gtk_tree_path_append_index( path
, row
);
2038 gboolean ret
= gtk_tree_selection_path_is_selected( selection
, path
);
2040 gtk_tree_path_free( path
);
2045 int wxDataViewCtrl::GetSelection() const
2047 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2048 if (HasFlag(wxDV_MULTIPLE
))
2050 GtkTreeModel
*model
;
2051 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
2056 // list = g_list_nth( list, 0 ); should be a noop
2057 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
2059 unsigned int row
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
2062 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
2063 g_list_free( list
);
2071 GtkTreeModel
*model
;
2073 gboolean has_selection
= gtk_tree_selection_get_selected( selection
, &model
, &iter
);
2076 unsigned int row
= (wxUIntPtr
) iter
.user_data
;
2084 int wxDataViewCtrl::GetSelections(wxArrayInt
& aSelections
) const
2086 aSelections
.Clear();
2088 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2089 if (HasFlag(wxDV_MULTIPLE
))
2091 GtkTreeModel
*model
;
2092 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
2098 // list = g_list_nth( list, 0 ); should be a noop
2099 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
2101 unsigned int row
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
2103 aSelections
.Add( (int) row
);
2105 list
= g_list_next( list
);
2109 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
2110 g_list_free( list
);
2116 GtkTreeModel
*model
;
2118 gboolean has_selection
= gtk_tree_selection_get_selected( selection
, &model
, &iter
);
2121 unsigned int row
= (wxUIntPtr
) iter
.user_data
;
2122 aSelections
.Add( (int) row
);
2132 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2134 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
2139 // !wxUSE_GENERICDATAVIEWCTRL
2142 // wxUSE_DATAVIEWCTRL