From 81d6842b8eb927ab105620a9a0716a7debe6b8c9 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 2 Oct 2013 21:16:40 +0100 Subject: [PATCH] These changes introduce Gtk3 support into the highgui module. A new option WITH_GTK3 has been added to the cmake configure system to enable compilation under Gtk version 3.The flag HAVE_GTK3 is also introduced to select the new Gtk3 code in the source files. (Gtk2 is disbled when Gtk3 is selected). window_gtk.cpp has been modified to remove obsolete (and deleted from libgtk 3) and introduce new Gtk3 code in its place when compiled for Gtk3. To compile for Gtk2, disable WITH_GTK3 in cmake. To build for Gtk3 both WITH_GTK and WITH_GTK3 must be selected. --- CMakeLists.txt | 2 + cmake/OpenCVFindLibsGUI.cmake | 8 +- cmake/templates/cvconfig.h.cmake | 3 + modules/highgui/CMakeLists.txt | 2 +- modules/highgui/src/window.cpp | 11 ++ modules/highgui/src/window_gtk.cpp | 207 ++++++++++++++++++++++++++--- 6 files changed, 211 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00c35a949..c989e81a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,6 +120,7 @@ OCV_OPTION(WITH_FFMPEG "Include FFMPEG support" ON OCV_OPTION(WITH_GSTREAMER "Include Gstreamer support" ON IF (UNIX AND NOT APPLE AND NOT ANDROID) ) OCV_OPTION(WITH_GSTREAMER_1_X "Include Gstreamer 1.x support" OFF) OCV_OPTION(WITH_GTK "Include GTK support" ON IF (UNIX AND NOT APPLE AND NOT ANDROID) ) +OCV_OPTION(WITH_GTK3 "Include GTK3 support" ON IF (UNIX AND WITH_GTK AND NOT APPLE AND NOT ANDROID) ) OCV_OPTION(WITH_IPP "Include Intel IPP support" OFF IF (MSVC OR X86 OR X86_64) ) OCV_OPTION(WITH_JASPER "Include JPEG2K support" ON IF (NOT IOS) ) OCV_OPTION(WITH_JPEG "Include JPEG support" ON) @@ -603,6 +604,7 @@ else() endif() else() status(" GTK+ 2.x:" HAVE_GTK THEN "YES (ver ${ALIASOF_gtk+-2.0_VERSION})" ELSE NO) + status(" GTK+ 3.x:" HAVE_GTK3 THEN "YES (ver ${ALIASOF_gtk+-3.0_VERSION})" ELSE NO) status(" GThread :" HAVE_GTHREAD THEN "YES (ver ${ALIASOF_gthread-2.0_VERSION})" ELSE NO) status(" GtkGlExt:" HAVE_GTKGLEXT THEN "YES (ver ${ALIASOF_gtkglext-1.0_VERSION})" ELSE NO) endif() diff --git a/cmake/OpenCVFindLibsGUI.cmake b/cmake/OpenCVFindLibsGUI.cmake index c80beca05..d09ed892b 100644 --- a/cmake/OpenCVFindLibsGUI.cmake +++ b/cmake/OpenCVFindLibsGUI.cmake @@ -39,9 +39,13 @@ if(WITH_QT) endif() # --- GTK --- -ocv_clear_vars(HAVE_GTK HAVE_GTHREAD HAVE_GTKGLEXT) +ocv_clear_vars(HAVE_GTK HAVE_GTK3 HAVE_GTHREAD HAVE_GTKGLEXT) if(WITH_GTK AND NOT HAVE_QT) - CHECK_MODULE(gtk+-2.0 HAVE_GTK) + if(WITH_GTK3) + CHECK_MODULE(gtk+-3.0 HAVE_GTK3) + else() + CHECK_MODULE(gtk+-2.0 HAVE_GTK) + endif() CHECK_MODULE(gthread-2.0 HAVE_GTHREAD) if(WITH_OPENGL) CHECK_MODULE(gtkglext-1.0 HAVE_GTKGLEXT) diff --git a/cmake/templates/cvconfig.h.cmake b/cmake/templates/cvconfig.h.cmake index 56c5d5aad..68e2d13c9 100644 --- a/cmake/templates/cvconfig.h.cmake +++ b/cmake/templates/cvconfig.h.cmake @@ -82,6 +82,9 @@ /* GTK+ 2.x toolkit */ #cmakedefine HAVE_GTK +/* GTK+ 3.x toolkit */ +#cmakedefine HAVE_GTK3 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_INTTYPES_H 1 diff --git a/modules/highgui/CMakeLists.txt b/modules/highgui/CMakeLists.txt index 011bb0dc3..e8505f416 100644 --- a/modules/highgui/CMakeLists.txt +++ b/modules/highgui/CMakeLists.txt @@ -106,7 +106,7 @@ elseif(HAVE_QT) endif() elseif(HAVE_WIN32UI) list(APPEND highgui_srcs src/window_w32.cpp) -elseif(HAVE_GTK) +elseif(HAVE_GTK OR HAVE_GTK3) list(APPEND highgui_srcs src/window_gtk.cpp) elseif(HAVE_CARBON) list(APPEND highgui_srcs src/window_carbon.cpp) diff --git a/modules/highgui/src/window.cpp b/modules/highgui/src/window.cpp index 428ef51ef..6d5047075 100644 --- a/modules/highgui/src/window.cpp +++ b/modules/highgui/src/window.cpp @@ -61,6 +61,8 @@ CV_IMPL void cvSetWindowProperty(const char* name, int prop_id, double prop_valu cvSetModeWindow_W32(name,prop_value); #elif defined (HAVE_GTK) cvSetModeWindow_GTK(name,prop_value); + #elif defined (HAVE_GTK3) + cvSetModeWindow_GTK(name,prop_value); #elif defined (HAVE_CARBON) cvSetModeWindow_CARBON(name,prop_value); #elif defined (HAVE_COCOA) @@ -100,6 +102,8 @@ CV_IMPL double cvGetWindowProperty(const char* name, int prop_id) return cvGetModeWindow_W32(name); #elif defined (HAVE_GTK) return cvGetModeWindow_GTK(name); + #elif defined (HAVE_GTK3) + return cvGetModeWindow_GTK(name); #elif defined (HAVE_CARBON) return cvGetModeWindow_CARBON(name); #elif defined (HAVE_COCOA) @@ -117,6 +121,8 @@ CV_IMPL double cvGetWindowProperty(const char* name, int prop_id) return cvGetPropWindowAutoSize_W32(name); #elif defined (HAVE_GTK) return cvGetPropWindowAutoSize_GTK(name); + #elif defined (HAVE_GTK3) + return cvGetPropWindowAutoSize_GTK(name); #else return -1; #endif @@ -130,6 +136,8 @@ CV_IMPL double cvGetWindowProperty(const char* name, int prop_id) return cvGetRatioWindow_W32(name); #elif defined (HAVE_GTK) return cvGetRatioWindow_GTK(name); + #elif defined (HAVE_GTK3) + return cvGetRatioWindow_GTK(name); #else return -1; #endif @@ -143,6 +151,8 @@ CV_IMPL double cvGetWindowProperty(const char* name, int prop_id) return cvGetOpenGlProp_W32(name); #elif defined (HAVE_GTK) return cvGetOpenGlProp_GTK(name); + #elif defined (HAVE_GTK3) + return cvGetOpenGlProp_GTK(name); #else return -1; #endif @@ -472,6 +482,7 @@ int cv::createButton(const String&, ButtonCallback, void*, int , bool ) #if defined(HAVE_WIN32UI) // see window_w32.cpp #elif defined (HAVE_GTK) // see window_gtk.cpp +#elif defined (HAVE_GTK3) // see window_gtk.cpp #elif defined (HAVE_COCOA) // see window_carbon.cpp #elif defined (HAVE_CARBON) #elif defined (HAVE_QT) //YV see window_QT.cpp diff --git a/modules/highgui/src/window_gtk.cpp b/modules/highgui/src/window_gtk.cpp index c467155a3..95fee5163 100644 --- a/modules/highgui/src/window_gtk.cpp +++ b/modules/highgui/src/window_gtk.cpp @@ -43,7 +43,7 @@ #ifndef WIN32 -#ifdef HAVE_GTK +#if defined (HAVE_GTK) || defined (HAVE_GTK3) #include "gtk/gtk.h" #include "gdk/gdkkeysyms.h" @@ -150,32 +150,77 @@ cvImageWidget_realize (GtkWidget *widget) GdkWindowAttr attributes; gint attributes_mask; +#if defined(HAVE_GTK3) + GtkAllocation allocation; + gtk_widget_get_allocation(widget, &allocation); +#endif //HAVE_GTK3 + //printf("cvImageWidget_realize\n"); 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); +#if defined(HAVE_GTK3) + attributes.x = allocation.x; + attributes.y = allocation.y; + attributes.width = allocation.width; + attributes.height = allocation.height; +#else attributes.x = widget->allocation.x; attributes.y = widget->allocation.y; attributes.width = widget->allocation.width; attributes.height = widget->allocation.height; +#endif //HAVE_GTK3 + attributes.wclass = GDK_INPUT_OUTPUT; attributes.window_type = GDK_WINDOW_CHILD; attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK; attributes.visual = gtk_widget_get_visual (widget); - attributes.colormap = gtk_widget_get_colormap (widget); +#if defined(HAVE_GTK3) + attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL; + gtk_widget_set_window( + widget, + gdk_window_new( + gtk_widget_get_parent_window(widget), + &attributes, + attributes_mask + ) + ); + + gtk_widget_set_style( + widget, + gtk_style_attach( + gtk_widget_get_style(widget), + gtk_widget_get_window(widget) + ) + ); + + gdk_window_set_user_data ( + gtk_widget_get_window(widget), + widget + ); + + gtk_style_set_background ( + gtk_widget_get_style(widget), + gtk_widget_get_window(widget), + GTK_STATE_ACTIVE + ); + #else + // The following lines are included to prevent breaking + // compatibility with older Gtk2 (window = gdk_window_new (widget->parent->window, &attributes, attributes_mask); widget->style = gtk_style_attach (widget->style, widget->window); - gdk_window_set_user_data (widget->window, widget); gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE); +#endif // HAVE_GTK3 } static CvSize cvImageWidget_calc_size( int im_width, int im_height, int max_width, int max_height ){ @@ -218,6 +263,26 @@ cvImageWidget_size_request (GtkWidget *widget, //printf("%d %d\n",requisition->width, requisition->height); } +#if defined (HAVE_GTK3) +static void +cvImageWidget_get_preferred_width (GtkWidget *widget, gint *minimal_width, gint *natural_width) +{ + GtkRequisition requisition; + + cvImageWidget_size_request (widget, &requisition); + *minimal_width = *natural_width = requisition.width; +} + +static void +cvImageWidget_get_preferred_height (GtkWidget *widget, gint *minimal_height, gint *natural_height) +{ + GtkRequisition requisition; + + cvImageWidget_size_request (widget, &requisition); + *minimal_height = *natural_height = requisition.height; +} +#endif //HAVE_GTK3 + static void cvImageWidget_set_size(GtkWidget * widget, int max_width, int max_height){ CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget ); @@ -237,7 +302,7 @@ static void cvImageWidget_set_size(GtkWidget * widget, int max_width, int max_he cvReleaseMat( &image_widget->scaled_image ); } if( !image_widget->scaled_image ){ - image_widget->scaled_image = cvCreateMat( scaled_image_size.height, scaled_image_size.width, CV_8UC3 ); + image_widget->scaled_image = cvCreateMat( scaled_image_size.height, scaled_image_size.width, CV_8UC3 ); } @@ -255,7 +320,11 @@ cvImageWidget_size_allocate (GtkWidget *widget, g_return_if_fail (CV_IS_IMAGE_WIDGET (widget)); g_return_if_fail (allocation != NULL); +#if defined (HAVE_GTK3) + gtk_widget_set_allocation(widget, allocation); +#else widget->allocation = *allocation; +#endif //HAVE_GTK3 image_widget = CV_IMAGE_WIDGET (widget); @@ -279,26 +348,37 @@ cvImageWidget_size_allocate (GtkWidget *widget, ((image_widget->flags & CV_WINDOW_AUTOSIZE) || (image_widget->flags & CV_WINDOW_NO_IMAGE)) ) { +#if defined (HAVE_GTK3) + allocation->width = image_widget->original_image->cols; + allocation->height = image_widget->original_image->rows; + gtk_widget_set_allocation(widget, allocation); +#else widget->allocation.width = image_widget->original_image->cols; widget->allocation.height = image_widget->original_image->rows; - gdk_window_move_resize( widget->window, allocation->x, allocation->y, - image_widget->original_image->cols, image_widget->original_image->rows ); +#endif //HAVE_GTK3 + gdk_window_move_resize( gtk_widget_get_window(widget), + allocation->x, allocation->y, + image_widget->original_image->cols, image_widget->original_image->rows ); if(image_widget->flags & CV_WINDOW_NO_IMAGE){ image_widget->flags &= ~CV_WINDOW_NO_IMAGE; gtk_widget_queue_resize( GTK_WIDGET(widget) ); } } else{ - gdk_window_move_resize (widget->window, + gdk_window_move_resize (gtk_widget_get_window(widget), allocation->x, allocation->y, allocation->width, allocation->height ); - } } } +#if defined (HAVE_GTK3) +static void +cvImageWidget_destroy (GtkWidget *object) +#else static void cvImageWidget_destroy (GtkObject *object) +#endif //HAVE_GTK3 { CvImageWidget *image_widget; @@ -310,24 +390,39 @@ cvImageWidget_destroy (GtkObject *object) cvReleaseMat( &image_widget->scaled_image ); cvReleaseMat( &image_widget->original_image ); +#if defined (HAVE_GTK3) + if (GTK_WIDGET_CLASS (parent_class)->destroy) + (* GTK_WIDGET_CLASS (parent_class)->destroy) (object); +#else if (GTK_OBJECT_CLASS (parent_class)->destroy) (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); +#endif //HAVE_GTK3 } static void cvImageWidget_class_init (CvImageWidgetClass * klass) { +#if defined (HAVE_GTK3) + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); +#else GtkObjectClass *object_class; GtkWidgetClass *widget_class; object_class = (GtkObjectClass*) klass; widget_class = (GtkWidgetClass*) klass; +#endif //HAVE_GTK3 parent_class = GTK_WIDGET_CLASS( g_type_class_peek (gtk_widget_get_type ()) ); +#if defined (HAVE_GTK3) + widget_class->destroy = cvImageWidget_destroy; + widget_class->get_preferred_width = cvImageWidget_get_preferred_width; + widget_class->get_preferred_height = cvImageWidget_get_preferred_height; +#else object_class->destroy = cvImageWidget_destroy; + widget_class->size_request = cvImageWidget_size_request; +#endif //HAVE_GTK3 widget_class->realize = cvImageWidget_realize; - widget_class->size_request = cvImageWidget_size_request; widget_class->size_allocate = cvImageWidget_size_allocate; widget_class->button_press_event = NULL; widget_class->button_release_event = NULL; @@ -347,13 +442,15 @@ GType cvImageWidget_get_type (void){ if (!image_type) { - 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); + 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; @@ -642,8 +739,12 @@ double cvGetRatioWindow_GTK(const char* name) if (!window) EXIT; // keep silence here +#if defined (HAVE_GTK3) + result = static_cast( + gtk_widget_get_allocated_width(window->widget)) / gtk_widget_get_allocated_height(window->widget); +#else result = static_cast(window->widget->allocation.width) / window->widget->allocation.height; - +#endif // HAVE_GTK3 __END__; return result; @@ -738,7 +839,56 @@ namespace #endif // HAVE_OPENGL +#if defined (HAVE_GTK3) +static gboolean cvImageWidget_draw(GtkWidget* widget, cairo_t *cr, gpointer data) +{ +#ifdef HAVE_OPENGL + CvWindow* window = (CvWindow*)data; + if (window->useGl) + { + drawGl(window); + return TRUE; + } +#else + (void)data; +#endif + + CvImageWidget *image_widget = NULL; + GdkPixbuf *pixbuf = NULL; + + g_return_val_if_fail (widget != NULL, FALSE); + g_return_val_if_fail (CV_IS_IMAGE_WIDGET (widget), FALSE); + + cr = gdk_cairo_create(gtk_widget_get_window(widget)); + image_widget = CV_IMAGE_WIDGET (widget); + + if( image_widget->scaled_image ){ + // center image in available region + int x0 = (gtk_widget_get_allocated_width(widget) - image_widget->scaled_image->cols)/2; + int y0 = (gtk_widget_get_allocated_height(widget) - image_widget->scaled_image->rows)/2; + + pixbuf = gdk_pixbuf_new_from_data(image_widget->scaled_image->data.ptr, GDK_COLORSPACE_RGB, false, + 8, MIN(image_widget->scaled_image->cols, gtk_widget_get_allocated_width(widget)), + MIN(image_widget->scaled_image->rows, gtk_widget_get_allocated_height(widget)), + image_widget->scaled_image->step, NULL, NULL); + + gdk_cairo_set_source_pixbuf(cr, pixbuf, x0, y0); + } + else if( image_widget->original_image ){ + pixbuf = gdk_pixbuf_new_from_data(image_widget->original_image->data.ptr, GDK_COLORSPACE_RGB, false, + 8, MIN(image_widget->original_image->cols, gtk_widget_get_allocated_width(widget)), + MIN(image_widget->original_image->rows, gtk_widget_get_allocated_height(widget)), + image_widget->original_image->step, NULL, NULL); + gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); + } + + cairo_paint(cr); + cairo_destroy(cr); + return TRUE; +} + +#else static gboolean cvImageWidget_expose(GtkWidget* widget, GdkEventExpose* event, gpointer data) { #ifdef HAVE_OPENGL @@ -776,6 +926,7 @@ static gboolean cvImageWidget_expose(GtkWidget* widget, GdkEventExpose* event, g 8, MIN(image_widget->scaled_image->cols, widget->allocation.width), MIN(image_widget->scaled_image->rows, widget->allocation.height), image_widget->scaled_image->step, NULL, NULL); + gdk_cairo_set_source_pixbuf(cr, pixbuf, x0, y0); } else if( image_widget->original_image ){ @@ -790,6 +941,7 @@ static gboolean cvImageWidget_expose(GtkWidget* widget, GdkEventExpose* event, g cairo_destroy(cr); return TRUE; } +#endif //HAVE_GTK3 CV_IMPL int cvNamedWindow( const char* name, int flags ) { @@ -862,8 +1014,13 @@ CV_IMPL int cvNamedWindow( const char* name, int flags ) G_CALLBACK(icvOnMouse), window ); g_signal_connect( window->frame, "delete-event", G_CALLBACK(icvOnClose), window ); +#if defined(HAVE_GTK3) + g_signal_connect( window->widget, "draw", + G_CALLBACK(cvImageWidget_draw), window ); +#else g_signal_connect( window->widget, "expose-event", G_CALLBACK(cvImageWidget_expose), window ); +#endif //HAVE_GTK3 gtk_widget_add_events (window->widget, GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK) ; @@ -1420,6 +1577,13 @@ CV_IMPL const char* cvGetWindowName( void* window_handle ) return window_name; } +#if defined (HAVE_GTK3) +#define GDK_Escape GDK_KEY_Escape +#define GDK_Return GDK_KEY_Return +#define GDK_Linefeed GDK_KEY_Linefeed +#define GDK_Tab GDK_KEY_Tab +#endif //HAVE_GTK3 + static gboolean icvOnKeyPress( GtkWidget * /*widget*/, GdkEventKey* event, gpointer /*user_data*/ ) { @@ -1550,8 +1714,13 @@ static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_da image_widget->original_image && image_widget->scaled_image ){ // image origin is not necessarily at (0,0) +#if defined (HAVE_GTK3) + int x0 = (gtk_widget_get_allocated_width(widget) - image_widget->scaled_image->cols)/2; + int y0 = (gtk_widget_get_allocated_height(widget) - image_widget->scaled_image->rows)/2; +#else int x0 = (widget->allocation.width - image_widget->scaled_image->cols)/2; int y0 = (widget->allocation.height - image_widget->scaled_image->rows)/2; +#endif //HAVE_GTK3 pt.x = cvRound( ((pt32f.x-x0)*image_widget->original_image->cols)/ image_widget->scaled_image->cols ); pt.y = cvRound( ((pt32f.y-y0)*image_widget->original_image->rows)/ @@ -1629,7 +1798,7 @@ CV_IMPL int cvWaitKey( int delay ) } -#endif // HAVE_GTK +#endif // HAVE_GTK || HAVE_GTK3 #endif // WIN32 /* End of file. */