From 97fc1e10a717d55e7837bb0c90001600f575c1ff Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 26 Sep 2013 18:31:44 +0100 Subject: [PATCH 1/2] Replace obsolete Gtk symbols This update replaces depreciated Gtk1.x symbols with Gtk2.x symbols in preparation for adding Gtk3 support. These edits aim to be fully compatible with Gtk2.x, allowing backward compatibility. The following symbols have been replaced in this commit: Gtk symbols that have been moved to the Gdk library: - GtkType -> Gtype - GTK_CHECK_CAST -> G_TYPE_CHECK_INSTANCE_CAST - GTK_CHECK_TYPE -> G_TYPE_CHECK_INSTANCE_TYPE - gtk_signal_connect -> g_signal_connect - GTK_SIGNAL_FUNC -> G_CALLBACK - gtk_type_class -> g_type_class_peek - gtk_type_new-> g_object_new - GtkTypeInfo -> GtypeInfo (Not required for g_type_register_static_simple) - gtk_type_unique -> g_type_register_static_simple - gdk_draw_image_rgb -> replaced by gdk_cairo_source_pixbuf NB: This requires porting to use cairo contexts and GdkPixbufs. The following gtk symbols are changed for their modern equivalent: - GTK_WIDGET_REALIZED -> gtk_widget_get_realized - GTK_OBJECT_FLAGS -> gtk_widget_set_realized Symbols without replacement: - gtk_range_set_update_policy -> No replacement; as default policy is set to continuous we can delete this line. --- modules/highgui/src/window_gtk.cpp | 97 +++++++++++++++--------------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/modules/highgui/src/window_gtk.cpp b/modules/highgui/src/window_gtk.cpp index 82c49770c..c467155a3 100644 --- a/modules/highgui/src/window_gtk.cpp +++ b/modules/highgui/src/window_gtk.cpp @@ -47,6 +47,7 @@ #include "gtk/gtk.h" #include "gdk/gdkkeysyms.h" +#include #include #ifdef HAVE_OPENGL @@ -88,14 +89,14 @@ GtkWidget* cvImageWidgetNew (int flags); void cvImageWidgetSetImage(CvImageWidget * widget, const CvArr *arr); // standard GTK object macros -#define CV_IMAGE_WIDGET(obj) GTK_CHECK_CAST (obj, cvImageWidget_get_type (), CvImageWidget) +#define CV_IMAGE_WIDGET(obj) G_TYPE_CHECK_INSTANCE_CAST (obj, cvImageWidget_get_type (), CvImageWidget) #define CV_IMAGE_WIDGET_CLASS(klass) GTK_CHECK_CLASS_CAST (klass, cvImageWidget_get_type (), CvImageWidgetClass) -#define CV_IS_IMAGE_WIDGET(obj) GTK_CHECK_TYPE (obj, cvImageWidget_get_type ()) +#define CV_IS_IMAGE_WIDGET(obj) G_TYPE_CHECK_INSTANCE_TYPE (obj, cvImageWidget_get_type ()) ///////////////////////////////////////////////////////////////////////////// // Private API //////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// -GtkType cvImageWidget_get_type (void); +GType cvImageWidget_get_type (void); static GtkWidgetClass * parent_class = NULL; @@ -135,7 +136,7 @@ cvImageWidgetNew (int flags) { CvImageWidget *image_widget; - image_widget = CV_IMAGE_WIDGET( gtk_type_new (cvImageWidget_get_type ()) ); + image_widget = CV_IMAGE_WIDGET( gtk_widget_new (cvImageWidget_get_type (), NULL) ); image_widget->original_image = 0; image_widget->scaled_image = 0; image_widget->flags = flags | CV_WINDOW_NO_IMAGE; @@ -153,7 +154,7 @@ cvImageWidget_realize (GtkWidget *widget) g_return_if_fail (widget != NULL); g_return_if_fail (CV_IS_IMAGE_WIDGET (widget)); - GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); + gtk_widget_set_realized(widget, true); attributes.x = widget->allocation.x; attributes.y = widget->allocation.y; @@ -270,7 +271,7 @@ cvImageWidget_size_allocate (GtkWidget *widget, cvResize( image_widget->original_image, image_widget->scaled_image, CV_INTER_AREA ); } - if (GTK_WIDGET_REALIZED (widget)) + if (gtk_widget_get_realized (widget)) { image_widget = CV_IMAGE_WIDGET (widget); @@ -321,7 +322,7 @@ static void cvImageWidget_class_init (CvImageWidgetClass * klass) object_class = (GtkObjectClass*) klass; widget_class = (GtkWidgetClass*) klass; - parent_class = GTK_WIDGET_CLASS( gtk_type_class (gtk_widget_get_type ()) ); + parent_class = GTK_WIDGET_CLASS( g_type_class_peek (gtk_widget_get_type ()) ); object_class->destroy = cvImageWidget_destroy; @@ -341,24 +342,18 @@ cvImageWidget_init (CvImageWidget *image_widget) image_widget->flags=0; } -GtkType cvImageWidget_get_type (void){ - static GtkType image_type = 0; +GType cvImageWidget_get_type (void){ + static GType image_type = 0; if (!image_type) { - static const GtkTypeInfo image_info = - { - (gchar*)"CvImageWidget", - sizeof (CvImageWidget), - sizeof (CvImageWidgetClass), - (GtkClassInitFunc) cvImageWidget_class_init, - (GtkObjectInitFunc) cvImageWidget_init, - /* reserved_1 */ NULL, - /* reserved_1 */ NULL, - (GtkClassInitFunc) NULL - }; - - image_type = gtk_type_unique (GTK_TYPE_WIDGET, &image_info); + image_type = g_type_register_static_simple( GTK_TYPE_WIDGET, + (gchar*) "CvImageWidget", + sizeof(CvImageWidgetClass), + (GClassInitFunc) cvImageWidget_class_init, + sizeof(CvImageWidget), + (GInstanceInitFunc) cvImageWidget_init, + (GTypeFlags)NULL); } return image_type; @@ -758,7 +753,9 @@ static gboolean cvImageWidget_expose(GtkWidget* widget, GdkEventExpose* event, g (void)data; #endif - CvImageWidget *image_widget; + CvImageWidget *image_widget = NULL; + cairo_t *cr = NULL; + GdkPixbuf *pixbuf = NULL; g_return_val_if_fail (widget != NULL, FALSE); g_return_val_if_fail (CV_IS_IMAGE_WIDGET (widget), FALSE); @@ -767,29 +764,30 @@ static gboolean cvImageWidget_expose(GtkWidget* widget, GdkEventExpose* event, g if (event->count > 0) return FALSE; + cr = gdk_cairo_create(widget->window); image_widget = CV_IMAGE_WIDGET (widget); - gdk_window_clear_area (widget->window, - 0, 0, - widget->allocation.width, - widget->allocation.height); if( image_widget->scaled_image ){ // center image in available region int x0 = (widget->allocation.width - image_widget->scaled_image->cols)/2; int y0 = (widget->allocation.height - image_widget->scaled_image->rows)/2; - gdk_draw_rgb_image( widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], - x0, y0, MIN(image_widget->scaled_image->cols, widget->allocation.width), + pixbuf = gdk_pixbuf_new_from_data(image_widget->scaled_image->data.ptr, GDK_COLORSPACE_RGB, false, + 8, MIN(image_widget->scaled_image->cols, widget->allocation.width), MIN(image_widget->scaled_image->rows, widget->allocation.height), - GDK_RGB_DITHER_MAX, image_widget->scaled_image->data.ptr, image_widget->scaled_image->step ); + image_widget->scaled_image->step, NULL, NULL); + gdk_cairo_set_source_pixbuf(cr, pixbuf, x0, y0); } else if( image_widget->original_image ){ - gdk_draw_rgb_image( widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], - 0, 0, - MIN(image_widget->original_image->cols, widget->allocation.width), - MIN(image_widget->original_image->rows, widget->allocation.height), - GDK_RGB_DITHER_MAX, image_widget->original_image->data.ptr, image_widget->original_image->step ); + pixbuf = gdk_pixbuf_new_from_data(image_widget->original_image->data.ptr, GDK_COLORSPACE_RGB, false, + 8, MIN(image_widget->original_image->cols, widget->allocation.width), + MIN(image_widget->original_image->rows, widget->allocation.height), + image_widget->original_image->step, NULL, NULL); + gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); } + + cairo_paint(cr); + cairo_destroy(cr); return TRUE; } @@ -854,18 +852,18 @@ CV_IMPL int cvNamedWindow( const char* name, int flags ) // // configure event handlers // TODO -- move this to CvImageWidget ? - gtk_signal_connect( GTK_OBJECT(window->frame), "key-press-event", - GTK_SIGNAL_FUNC(icvOnKeyPress), window ); - gtk_signal_connect( GTK_OBJECT(window->widget), "button-press-event", - GTK_SIGNAL_FUNC(icvOnMouse), window ); - gtk_signal_connect( GTK_OBJECT(window->widget), "button-release-event", - GTK_SIGNAL_FUNC(icvOnMouse), window ); - gtk_signal_connect( GTK_OBJECT(window->widget), "motion-notify-event", - GTK_SIGNAL_FUNC(icvOnMouse), window ); - gtk_signal_connect( GTK_OBJECT(window->frame), "delete-event", - GTK_SIGNAL_FUNC(icvOnClose), window ); - gtk_signal_connect( GTK_OBJECT(window->widget), "expose-event", - GTK_SIGNAL_FUNC(cvImageWidget_expose), window ); + g_signal_connect( window->frame, "key-press-event", + G_CALLBACK(icvOnKeyPress), window ); + g_signal_connect( window->widget, "button-press-event", + G_CALLBACK(icvOnMouse), window ); + g_signal_connect( window->widget, "button-release-event", + G_CALLBACK(icvOnMouse), window ); + g_signal_connect( window->widget, "motion-notify-event", + G_CALLBACK(icvOnMouse), window ); + g_signal_connect( window->frame, "delete-event", + G_CALLBACK(icvOnClose), window ); + g_signal_connect( window->widget, "expose-event", + G_CALLBACK(cvImageWidget_expose), window ); gtk_widget_add_events (window->widget, GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK) ; @@ -1225,7 +1223,6 @@ icvCreateTrackbar( const char* trackbar_name, const char* window_name, GtkWidget* hscale_box = gtk_hbox_new( FALSE, 10 ); GtkWidget* hscale_label = gtk_label_new( trackbar_name ); GtkWidget* hscale = gtk_hscale_new_with_range( 0, count, 1 ); - gtk_range_set_update_policy( GTK_RANGE(hscale), GTK_UPDATE_CONTINUOUS ); gtk_scale_set_digits( GTK_SCALE(hscale), 0 ); //gtk_scale_set_value_pos( hscale, GTK_POS_TOP ); gtk_scale_set_draw_value( GTK_SCALE(hscale), TRUE ); @@ -1256,8 +1253,8 @@ icvCreateTrackbar( const char* trackbar_name, const char* window_name, trackbar->notify = on_notify; trackbar->notify2 = on_notify2; trackbar->userdata = userdata; - gtk_signal_connect( GTK_OBJECT(trackbar->widget), "value-changed", - GTK_SIGNAL_FUNC(icvOnTrackbar), trackbar ); + g_signal_connect( trackbar->widget, "value-changed", + G_CALLBACK(icvOnTrackbar), trackbar ); // queue a widget resize to trigger a window resize to // compensate for the addition of trackbars From 0d06b1dced4fedb5d1b43396bcc86e4b7d5511f5 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 30 Sep 2013 21:04:43 +0100 Subject: [PATCH 2/2] Change true for TRUE The second parameter of gtk_widget_set_realized is of type gboolean (gint). This is to ensure compatibility on alternative compilers and platforms. --- modules/highgui/src/window_gtk.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/highgui/src/window_gtk.cpp b/modules/highgui/src/window_gtk.cpp index c467155a3..79ae638cf 100644 --- a/modules/highgui/src/window_gtk.cpp +++ b/modules/highgui/src/window_gtk.cpp @@ -154,7 +154,7 @@ cvImageWidget_realize (GtkWidget *widget) g_return_if_fail (widget != NULL); g_return_if_fail (CV_IS_IMAGE_WIDGET (widget)); - gtk_widget_set_realized(widget, true); + gtk_widget_set_realized(widget, TRUE); attributes.x = widget->allocation.x; attributes.y = widget->allocation.y;