1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "precomp.hpp"
49 #include "gdk/gdkkeysyms.h"
53 #include <gtk/gtkgl.h>
58 // TODO Fix the initial window size when flags=0. Right now the initial window is by default
59 // 320x240 size. A better default would be actual size of the image. Problem
60 // is determining desired window size with trackbars while still allowing resizing.
62 // Gnome Totem source may be of use here, see bacon_video_widget_set_scale_ratio
63 // in totem/src/backend/bacon-video-widget-xine.c
65 ////////////////////////////////////////////////////////////
66 // CvImageWidget GTK Widget Public API
67 ////////////////////////////////////////////////////////////
68 typedef struct _CvImageWidget CvImageWidget;
69 typedef struct _CvImageWidgetClass CvImageWidgetClass;
71 struct _CvImageWidget {
73 CvMat * original_image;
78 struct _CvImageWidgetClass
80 GtkWidgetClass parent_class;
84 /** Allocate new image viewer widget */
85 GtkWidget* cvImageWidgetNew (int flags);
87 /** Set the image to display in the widget */
88 void cvImageWidgetSetImage(CvImageWidget * widget, const CvArr *arr);
90 // standard GTK object macros
91 #define CV_IMAGE_WIDGET(obj) GTK_CHECK_CAST (obj, cvImageWidget_get_type (), CvImageWidget)
92 #define CV_IMAGE_WIDGET_CLASS(klass) GTK_CHECK_CLASS_CAST (klass, cvImageWidget_get_type (), CvImageWidgetClass)
93 #define CV_IS_IMAGE_WIDGET(obj) GTK_CHECK_TYPE (obj, cvImageWidget_get_type ())
95 /////////////////////////////////////////////////////////////////////////////
96 // Private API ////////////////////////////////////////////////////////
97 /////////////////////////////////////////////////////////////////////////////
98 GtkType cvImageWidget_get_type (void);
100 static GtkWidgetClass * parent_class = NULL;
102 // flag to help size initial window
103 #define CV_WINDOW_NO_IMAGE 2
105 void cvImageWidgetSetImage(CvImageWidget * widget, const CvArr *arr){
109 //printf("cvImageWidgetSetImage\n");
111 if( CV_IS_IMAGE_HDR( arr ))
112 origin = ((IplImage*)arr)->origin;
114 mat = cvGetMat(arr, &stub);
116 if(widget->original_image && !CV_ARE_SIZES_EQ(mat, widget->original_image)){
117 cvReleaseMat( &widget->original_image );
119 if(!widget->original_image){
120 widget->original_image = cvCreateMat( mat->rows, mat->cols, CV_8UC3 );
121 gtk_widget_queue_resize( GTK_WIDGET( widget ) );
123 cvConvertImage( mat, widget->original_image,
124 (origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB );
125 if(widget->scaled_image){
126 cvResize( widget->original_image, widget->scaled_image, CV_INTER_AREA );
129 // window does not refresh without this
130 gtk_widget_queue_draw( GTK_WIDGET(widget) );
134 cvImageWidgetNew (int flags)
136 CvImageWidget *image_widget;
138 image_widget = CV_IMAGE_WIDGET( gtk_type_new (cvImageWidget_get_type ()) );
139 image_widget->original_image = 0;
140 image_widget->scaled_image = 0;
141 image_widget->flags = flags | CV_WINDOW_NO_IMAGE;
143 return GTK_WIDGET (image_widget);
147 cvImageWidget_realize (GtkWidget *widget)
149 GdkWindowAttr attributes;
150 gint attributes_mask;
152 //printf("cvImageWidget_realize\n");
153 g_return_if_fail (widget != NULL);
154 g_return_if_fail (CV_IS_IMAGE_WIDGET (widget));
156 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
158 attributes.x = widget->allocation.x;
159 attributes.y = widget->allocation.y;
160 attributes.width = widget->allocation.width;
161 attributes.height = widget->allocation.height;
162 attributes.wclass = GDK_INPUT_OUTPUT;
163 attributes.window_type = GDK_WINDOW_CHILD;
164 attributes.event_mask = gtk_widget_get_events (widget) |
165 GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK |
166 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK;
167 attributes.visual = gtk_widget_get_visual (widget);
168 attributes.colormap = gtk_widget_get_colormap (widget);
170 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
171 widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
173 widget->style = gtk_style_attach (widget->style, widget->window);
175 gdk_window_set_user_data (widget->window, widget);
177 gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
180 static CvSize cvImageWidget_calc_size( int im_width, int im_height, int max_width, int max_height ){
181 float aspect = (float)im_width/(float)im_height;
182 float max_aspect = (float)max_width/(float)max_height;
183 if(aspect > max_aspect){
184 return cvSize( max_width, cvRound(max_width/aspect) );
186 return cvSize( cvRound(max_height*aspect), max_height );
190 cvImageWidget_size_request (GtkWidget *widget,
191 GtkRequisition *requisition)
193 CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget );
195 //printf("cvImageWidget_size_request ");
196 // the case the first time cvShowImage called or when AUTOSIZE
197 if( image_widget->original_image &&
198 ((image_widget->flags & CV_WINDOW_AUTOSIZE) ||
199 (image_widget->flags & CV_WINDOW_NO_IMAGE)))
201 //printf("original ");
202 requisition->width = image_widget->original_image->cols;
203 requisition->height = image_widget->original_image->rows;
206 else if(image_widget->scaled_image){
208 requisition->width = image_widget->scaled_image->cols;
209 requisition->height = image_widget->scaled_image->rows;
211 // the case before cvShowImage called
213 //printf("default ");
214 requisition->width = 320;
215 requisition->height = 240;
217 //printf("%d %d\n",requisition->width, requisition->height);
220 static void cvImageWidget_set_size(GtkWidget * widget, int max_width, int max_height){
221 CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget );
223 //printf("cvImageWidget_set_size %d %d\n", max_width, max_height);
225 // don't allow to set the size
226 if(image_widget->flags & CV_WINDOW_AUTOSIZE) return;
227 if(!image_widget->original_image) return;
229 CvSize scaled_image_size = cvImageWidget_calc_size( image_widget->original_image->cols,
230 image_widget->original_image->rows, max_width, max_height );
232 if( image_widget->scaled_image &&
233 ( image_widget->scaled_image->cols != scaled_image_size.width ||
234 image_widget->scaled_image->rows != scaled_image_size.height ))
236 cvReleaseMat( &image_widget->scaled_image );
238 if( !image_widget->scaled_image ){
239 image_widget->scaled_image = cvCreateMat( scaled_image_size.height, scaled_image_size.width, CV_8UC3 );
243 assert( image_widget->scaled_image );
247 cvImageWidget_size_allocate (GtkWidget *widget,
248 GtkAllocation *allocation)
250 CvImageWidget *image_widget;
252 //printf("cvImageWidget_size_allocate\n");
253 g_return_if_fail (widget != NULL);
254 g_return_if_fail (CV_IS_IMAGE_WIDGET (widget));
255 g_return_if_fail (allocation != NULL);
257 widget->allocation = *allocation;
258 image_widget = CV_IMAGE_WIDGET (widget);
261 if( (image_widget->flags & CV_WINDOW_AUTOSIZE)==0 && image_widget->original_image ){
262 // (re) allocated scaled image
263 if( image_widget->flags & CV_WINDOW_NO_IMAGE ){
264 cvImageWidget_set_size( widget, image_widget->original_image->cols,
265 image_widget->original_image->rows);
268 cvImageWidget_set_size( widget, allocation->width, allocation->height );
270 cvResize( image_widget->original_image, image_widget->scaled_image, CV_INTER_AREA );
273 if (GTK_WIDGET_REALIZED (widget))
275 image_widget = CV_IMAGE_WIDGET (widget);
277 if( image_widget->original_image &&
278 ((image_widget->flags & CV_WINDOW_AUTOSIZE) ||
279 (image_widget->flags & CV_WINDOW_NO_IMAGE)) )
281 widget->allocation.width = image_widget->original_image->cols;
282 widget->allocation.height = image_widget->original_image->rows;
283 gdk_window_move_resize( widget->window, allocation->x, allocation->y,
284 image_widget->original_image->cols, image_widget->original_image->rows );
285 if(image_widget->flags & CV_WINDOW_NO_IMAGE){
286 image_widget->flags &= ~CV_WINDOW_NO_IMAGE;
287 gtk_widget_queue_resize( GTK_WIDGET(widget) );
291 gdk_window_move_resize (widget->window,
292 allocation->x, allocation->y,
293 allocation->width, allocation->height );
300 cvImageWidget_destroy (GtkObject *object)
302 CvImageWidget *image_widget;
304 g_return_if_fail (object != NULL);
305 g_return_if_fail (CV_IS_IMAGE_WIDGET (object));
307 image_widget = CV_IMAGE_WIDGET (object);
309 cvReleaseMat( &image_widget->scaled_image );
310 cvReleaseMat( &image_widget->original_image );
312 if (GTK_OBJECT_CLASS (parent_class)->destroy)
313 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
316 static void cvImageWidget_class_init (CvImageWidgetClass * klass)
318 GtkObjectClass *object_class;
319 GtkWidgetClass *widget_class;
321 object_class = (GtkObjectClass*) klass;
322 widget_class = (GtkWidgetClass*) klass;
324 parent_class = GTK_WIDGET_CLASS( gtk_type_class (gtk_widget_get_type ()) );
326 object_class->destroy = cvImageWidget_destroy;
328 widget_class->realize = cvImageWidget_realize;
329 widget_class->size_request = cvImageWidget_size_request;
330 widget_class->size_allocate = cvImageWidget_size_allocate;
331 widget_class->button_press_event = NULL;
332 widget_class->button_release_event = NULL;
333 widget_class->motion_notify_event = NULL;
337 cvImageWidget_init (CvImageWidget *image_widget)
339 image_widget->original_image=0;
340 image_widget->scaled_image=0;
341 image_widget->flags=0;
344 GtkType cvImageWidget_get_type (void){
345 static GtkType image_type = 0;
349 static const GtkTypeInfo image_info =
351 (gchar*)"CvImageWidget",
352 sizeof (CvImageWidget),
353 sizeof (CvImageWidgetClass),
354 (GtkClassInitFunc) cvImageWidget_class_init,
355 (GtkObjectInitFunc) cvImageWidget_init,
356 /* reserved_1 */ NULL,
357 /* reserved_1 */ NULL,
358 (GtkClassInitFunc) NULL
361 image_type = gtk_type_unique (GTK_TYPE_WIDGET, &image_info);
366 /////////////////////////////////////////////////////////////////////////////
368 /////////////////////////////////////////////////////////////////////////////
373 typedef struct CvTrackbar
383 CvTrackbarCallback notify;
384 CvTrackbarCallback2 notify2;
390 typedef struct CvWindow
402 int status;//0 normal, 1 fullscreen (YV)
404 CvMouseCallback on_mouse;
405 void* on_mouse_param;
418 CvOpenGlDrawCallback glDrawCallback;
421 CvOpenGlCleanCallback glCleanCallback;
428 static gboolean icvOnClose( GtkWidget* widget, GdkEvent* event, gpointer user_data );
429 static gboolean icvOnKeyPress( GtkWidget* widget, GdkEventKey* event, gpointer user_data );
430 static void icvOnTrackbar( GtkWidget* widget, gpointer user_data );
431 static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_data );
434 int thread_started=0;
435 static gpointer icvWindowThreadLoop();
436 GMutex* last_key_mutex;
437 GCond* cond_have_key;
438 GMutex* window_mutex;
439 GThread* window_thread;
440 GtkWidget* cvTopLevelWidget = 0;
443 static int last_key = -1;
444 static CvWindow* hg_windows = 0;
446 CV_IMPL int cvInitSystem( int argc, char** argv )
448 static int wasInitialized = 0;
450 // check initialization status
451 if( !wasInitialized )
455 gtk_disable_setlocale();
456 gtk_init( &argc, &argv );
459 gtk_gl_init(&argc, &argv);
468 CV_IMPL int cvStartWindowThread(){
470 cvInitSystem(0,NULL);
471 if (!thread_started) {
472 if (!g_thread_supported ()) {
473 /* the GThread system wasn't inited, so init it */
477 // this mutex protects the window resources
478 window_mutex = g_mutex_new();
480 // protects the 'last key pressed' variable
481 last_key_mutex = g_mutex_new();
483 // conditional that indicates a key has been pressed
484 cond_have_key = g_cond_new();
486 // this is the window update thread
487 window_thread = g_thread_create((GThreadFunc) icvWindowThreadLoop,
490 thread_started = window_thread!=NULL;
491 return thread_started;
498 gpointer icvWindowThreadLoop(){
500 g_mutex_lock(window_mutex);
501 gtk_main_iteration_do(FALSE);
502 g_mutex_unlock(window_mutex);
512 #define CV_LOCK_MUTEX() \
513 if(thread_started && g_thread_self()!=window_thread){ g_mutex_lock( window_mutex ); } else { }
515 #define CV_UNLOCK_MUTEX() \
516 if(thread_started && g_thread_self()!=window_thread){ g_mutex_unlock( window_mutex); } else { }
519 #define CV_LOCK_MUTEX()
520 #define CV_UNLOCK_MUTEX()
523 static CvWindow* icvFindWindowByName( const char* name )
525 CvWindow* window = hg_windows;
526 while( window != 0 && strcmp(name, window->name) != 0 )
527 window = window->next;
532 static CvWindow* icvWindowByWidget( GtkWidget* widget )
534 CvWindow* window = hg_windows;
536 while( window != 0 && window->widget != widget &&
537 window->frame != widget && window->paned != widget )
538 window = window->next;
543 double cvGetModeWindow_GTK(const char* name)//YV
547 CV_FUNCNAME( "cvGetModeWindow_GTK" );
554 CV_ERROR( CV_StsNullPtr, "NULL name string" );
556 window = icvFindWindowByName( name );
558 CV_ERROR( CV_StsNullPtr, "NULL window" );
561 result = window->status;
569 void cvSetModeWindow_GTK( const char* name, double prop_value)//Yannick Verdie
572 CV_FUNCNAME( "cvSetModeWindow_GTK" );
579 CV_ERROR( CV_StsNullPtr, "NULL name string" );
581 window = icvFindWindowByName( name );
583 CV_ERROR( CV_StsNullPtr, "NULL window" );
585 if(window->flags & CV_WINDOW_AUTOSIZE)//if the flag CV_WINDOW_AUTOSIZE is set
588 //so easy to do fullscreen here, Linux rocks !
590 if (window->status==CV_WINDOW_FULLSCREEN && prop_value==CV_WINDOW_NORMAL)
593 gtk_window_unfullscreen(GTK_WINDOW(window->frame));
594 window->status=CV_WINDOW_NORMAL;
599 if (window->status==CV_WINDOW_NORMAL && prop_value==CV_WINDOW_FULLSCREEN)
602 gtk_window_fullscreen(GTK_WINDOW(window->frame));
603 window->status=CV_WINDOW_FULLSCREEN;
612 double cvGetPropWindowAutoSize_GTK(const char* name)
616 CV_FUNCNAME( "cvGetPropWindowAutoSize_GTK" );
623 CV_ERROR( CV_StsNullPtr, "NULL name string" );
625 window = icvFindWindowByName( name );
627 EXIT; // keep silence here
629 result = window->flags & CV_WINDOW_AUTOSIZE;
636 double cvGetRatioWindow_GTK(const char* name)
640 CV_FUNCNAME( "cvGetRatioWindow_GTK" );
647 CV_ERROR( CV_StsNullPtr, "NULL name string" );
649 window = icvFindWindowByName( name );
651 EXIT; // keep silence here
653 result = static_cast<double>(window->widget->allocation.width) / window->widget->allocation.height;
660 double cvGetOpenGlProp_GTK(const char* name)
665 CV_FUNCNAME( "cvGetOpenGlProp_GTK" );
672 CV_ERROR( CV_StsNullPtr, "NULL name string" );
674 window = icvFindWindowByName( name );
676 EXIT; // keep silence here
678 result = window->useGl;
695 class GlFuncTab_GTK : public CvOpenGlFuncTab
700 void genBuffers(int n, unsigned int* buffers) const;
701 void deleteBuffers(int n, const unsigned int* buffers) const;
703 void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const;
704 void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const;
706 void bindBuffer(unsigned int target, unsigned int buffer) const;
708 void* mapBuffer(unsigned int target, unsigned int access) const;
709 void unmapBuffer(unsigned int target) const;
711 void generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const;
713 bool isGlContextInitialized() const;
715 PFNGLGENBUFFERSPROC glGenBuffersExt;
716 PFNGLDELETEBUFFERSPROC glDeleteBuffersExt;
718 PFNGLBUFFERDATAPROC glBufferDataExt;
719 PFNGLBUFFERSUBDATAPROC glBufferSubDataExt;
721 PFNGLBINDBUFFERPROC glBindBufferExt;
723 PFNGLMAPBUFFERPROC glMapBufferExt;
724 PFNGLUNMAPBUFFERPROC glUnmapBufferExt;
729 GlFuncTab_GTK::GlFuncTab_GTK()
732 glDeleteBuffersExt = 0;
735 glBufferSubDataExt = 0;
740 glUnmapBufferExt = 0;
745 void GlFuncTab_GTK::genBuffers(int n, unsigned int* buffers) const
747 CV_FUNCNAME( "GlFuncTab_GTK::genBuffers" );
751 if (!glGenBuffersExt)
752 CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
754 glGenBuffersExt(n, buffers);
760 void GlFuncTab_GTK::deleteBuffers(int n, const unsigned int* buffers) const
762 CV_FUNCNAME( "GlFuncTab_GTK::deleteBuffers" );
766 if (!glDeleteBuffersExt)
767 CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
769 glDeleteBuffersExt(n, buffers);
775 void GlFuncTab_GTK::bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
777 CV_FUNCNAME( "GlFuncTab_GTK::bufferData" );
781 if (!glBufferDataExt)
782 CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
784 glBufferDataExt(target, size, data, usage);
790 void GlFuncTab_GTK::bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
792 CV_FUNCNAME( "GlFuncTab_GTK::bufferSubData" );
796 if (!glBufferSubDataExt)
797 CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
799 glBufferSubDataExt(target, offset, size, data);
805 void GlFuncTab_GTK::bindBuffer(unsigned int target, unsigned int buffer) const
807 CV_FUNCNAME( "GlFuncTab_GTK::bindBuffer" );
811 if (!glBindBufferExt)
812 CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
814 glBindBufferExt(target, buffer);
820 void* GlFuncTab_GTK::mapBuffer(unsigned int target, unsigned int access) const
822 CV_FUNCNAME( "GlFuncTab_GTK::mapBuffer" );
829 CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
831 res = glMapBufferExt(target, access);
839 void GlFuncTab_GTK::unmapBuffer(unsigned int target) const
841 CV_FUNCNAME( "GlFuncTab_GTK::unmapBuffer" );
845 if (!glUnmapBufferExt)
846 CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
848 glUnmapBufferExt(target);
854 void GlFuncTab_GTK::generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool /*underline*/, int start, int count, int base) const
856 PangoFontDescription* fontDecr;
857 PangoFont* pangoFont;
859 CV_FUNCNAME( "GlFuncTab_GTK::generateBitmapFont" );
863 fontDecr = pango_font_description_new();
865 pango_font_description_set_size(fontDecr, height);
867 pango_font_description_set_family_static(fontDecr, family.c_str());
869 pango_font_description_set_weight(fontDecr, static_cast<PangoWeight>(weight));
871 pango_font_description_set_style(fontDecr, italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
873 pangoFont = gdk_gl_font_use_pango_font(fontDecr, start, count, base);
875 pango_font_description_free(fontDecr);
878 CV_ERROR(CV_OpenGlApiCallError, "Can't create font");
883 bool GlFuncTab_GTK::isGlContextInitialized() const
890 static GlFuncTab_GTK glFuncTab;
891 static bool first = true;
898 func = gdk_gl_get_proc_address("glGenBuffers");
899 glFuncTab.glGenBuffersExt = (PFNGLGENBUFFERSPROC)func;
901 func = gdk_gl_get_proc_address("glDeleteBuffers");
902 glFuncTab.glDeleteBuffersExt = (PFNGLDELETEBUFFERSPROC)func;
904 func = gdk_gl_get_proc_address("glBufferData");
905 glFuncTab.glBufferDataExt = (PFNGLBUFFERDATAPROC)func;
907 func = gdk_gl_get_proc_address("glBufferSubData");
908 glFuncTab.glBufferSubDataExt = (PFNGLBUFFERSUBDATAPROC)func;
910 func = gdk_gl_get_proc_address("glBindBuffer");
911 glFuncTab.glBindBufferExt = (PFNGLBINDBUFFERPROC)func;
913 func = gdk_gl_get_proc_address("glMapBuffer");
914 glFuncTab.glMapBufferExt = (PFNGLMAPBUFFERPROC)func;
916 func = gdk_gl_get_proc_address("glUnmapBuffer");
917 glFuncTab.glUnmapBufferExt = (PFNGLUNMAPBUFFERPROC)func;
919 glFuncTab.initialized = true;
921 icvSetOpenGlFuncTab(&glFuncTab);
927 void createGlContext(CvWindow* window)
929 GdkGLConfig* glconfig;
931 CV_FUNCNAME( "createGlContext" );
935 window->useGl = false;
937 // Try double-buffered visual
938 glconfig = gdk_gl_config_new_by_mode((GdkGLConfigMode)(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE));
940 CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Device Context" );
942 // Set OpenGL-capability to the widget
943 if (!gtk_widget_set_gl_capability(window->widget, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
944 CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Device Context" );
948 window->useGl = true;
953 void releaseGlContext(CvWindow* window)
955 //CV_FUNCNAME( "releaseGlContext" );
959 window->useGl = false;
964 void drawGl(CvWindow* window)
966 CV_FUNCNAME( "drawGl" );
970 GdkGLContext* glcontext = gtk_widget_get_gl_context(window->widget);
971 GdkGLDrawable* gldrawable = gtk_widget_get_gl_drawable(window->widget);
973 if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
974 CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
976 glViewport(0, 0, window->widget->allocation.width, window->widget->allocation.height);
978 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
980 if (window->glDrawCallback)
981 window->glDrawCallback(window->glDrawData);
985 if (gdk_gl_drawable_is_double_buffered (gldrawable))
986 gdk_gl_drawable_swap_buffers(gldrawable);
990 gdk_gl_drawable_gl_end(gldrawable);
996 #endif // HAVE_OPENGL
999 static gboolean cvImageWidget_expose(GtkWidget* widget, GdkEventExpose* event, gpointer data)
1002 CvWindow* window = (CvWindow*)data;
1013 CvImageWidget *image_widget;
1015 g_return_val_if_fail (widget != NULL, FALSE);
1016 g_return_val_if_fail (CV_IS_IMAGE_WIDGET (widget), FALSE);
1017 g_return_val_if_fail (event != NULL, FALSE);
1019 if (event->count > 0)
1022 image_widget = CV_IMAGE_WIDGET (widget);
1024 gdk_window_clear_area (widget->window,
1026 widget->allocation.width,
1027 widget->allocation.height);
1028 if( image_widget->scaled_image ){
1029 // center image in available region
1030 int x0 = (widget->allocation.width - image_widget->scaled_image->cols)/2;
1031 int y0 = (widget->allocation.height - image_widget->scaled_image->rows)/2;
1033 gdk_draw_rgb_image( widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
1034 x0, y0, MIN(image_widget->scaled_image->cols, widget->allocation.width),
1035 MIN(image_widget->scaled_image->rows, widget->allocation.height),
1036 GDK_RGB_DITHER_MAX, image_widget->scaled_image->data.ptr, image_widget->scaled_image->step );
1038 else if( image_widget->original_image ){
1039 gdk_draw_rgb_image( widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
1041 MIN(image_widget->original_image->cols, widget->allocation.width),
1042 MIN(image_widget->original_image->rows, widget->allocation.height),
1043 GDK_RGB_DITHER_MAX, image_widget->original_image->data.ptr, image_widget->original_image->step );
1048 CV_IMPL int cvNamedWindow( const char* name, int flags )
1051 CV_FUNCNAME( "cvNamedWindow" );
1058 cvInitSystem(1,(char**)&name);
1060 CV_ERROR( CV_StsNullPtr, "NULL name string" );
1062 // Check the name in the storage
1063 if( icvFindWindowByName( name ) != 0 )
1070 CV_CALL( window = (CvWindow*)cvAlloc(sizeof(CvWindow) + len + 1));
1071 memset( window, 0, sizeof(*window));
1072 window->name = (char*)(window + 1);
1073 memcpy( window->name, name, len + 1 );
1074 window->flags = flags;
1075 window->signature = CV_WINDOW_MAGIC_VAL;
1076 window->last_key = 0;
1077 window->on_mouse = 0;
1078 window->on_mouse_param = 0;
1079 memset( &window->toolbar, 0, sizeof(window->toolbar));
1080 window->next = hg_windows;
1082 window->status = CV_WINDOW_NORMAL;//YV
1086 window->frame = gtk_window_new( GTK_WINDOW_TOPLEVEL );
1088 window->paned = gtk_vbox_new( FALSE, 0 );
1089 window->widget = cvImageWidgetNew( flags );
1090 gtk_box_pack_end( GTK_BOX(window->paned), window->widget, TRUE, TRUE, 0 );
1091 gtk_widget_show( window->widget );
1092 gtk_container_add( GTK_CONTAINER(window->frame), window->paned );
1093 gtk_widget_show( window->paned );
1096 if (flags & CV_WINDOW_OPENGL)
1097 CV_ERROR( CV_OpenGlNotSupported, "Library was built without OpenGL support" );
1099 if (flags & CV_WINDOW_OPENGL)
1100 createGlContext(window);
1102 window->glDrawCallback = 0;
1103 window->glDrawData = 0;
1105 window->glCleanCallback = 0;
1106 window->glCleanData = 0;
1110 // configure event handlers
1111 // TODO -- move this to CvImageWidget ?
1112 gtk_signal_connect( GTK_OBJECT(window->frame), "key-press-event",
1113 GTK_SIGNAL_FUNC(icvOnKeyPress), window );
1114 gtk_signal_connect( GTK_OBJECT(window->widget), "button-press-event",
1115 GTK_SIGNAL_FUNC(icvOnMouse), window );
1116 gtk_signal_connect( GTK_OBJECT(window->widget), "button-release-event",
1117 GTK_SIGNAL_FUNC(icvOnMouse), window );
1118 gtk_signal_connect( GTK_OBJECT(window->widget), "motion-notify-event",
1119 GTK_SIGNAL_FUNC(icvOnMouse), window );
1120 gtk_signal_connect( GTK_OBJECT(window->frame), "delete-event",
1121 GTK_SIGNAL_FUNC(icvOnClose), window );
1122 gtk_signal_connect( GTK_OBJECT(window->widget), "expose-event",
1123 GTK_SIGNAL_FUNC(cvImageWidget_expose), window );
1125 gtk_widget_add_events (window->widget, GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK) ;
1127 gtk_widget_show( window->frame );
1128 gtk_window_set_title( GTK_WINDOW(window->frame), name );
1131 hg_windows->prev = window;
1132 hg_windows = window;
1134 gtk_window_set_resizable( GTK_WINDOW(window->frame), (flags & CV_WINDOW_AUTOSIZE) == 0 );
1137 // allow window to be resized
1138 if( (flags & CV_WINDOW_AUTOSIZE)==0 ){
1139 GdkGeometry geometry;
1140 geometry.min_width = 50;
1141 geometry.min_height = 50;
1142 gtk_window_set_geometry_hints( GTK_WINDOW( window->frame ), GTK_WIDGET( window->widget ),
1143 &geometry, (GdkWindowHints) (GDK_HINT_MIN_SIZE));
1150 cvSetOpenGlContext(name);
1162 CV_IMPL void cvSetOpenGlContext(const char* name)
1165 GdkGLContext* glcontext;
1166 GdkGLDrawable* gldrawable;
1168 CV_FUNCNAME( "cvSetOpenGlContext" );
1173 CV_ERROR( CV_StsNullPtr, "NULL name string" );
1175 window = icvFindWindowByName( name );
1177 CV_ERROR( CV_StsNullPtr, "NULL window" );
1180 CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
1182 glcontext = gtk_widget_get_gl_context(window->widget);
1183 gldrawable = gtk_widget_get_gl_drawable(window->widget);
1185 if (!gdk_gl_drawable_make_current(gldrawable, glcontext))
1186 CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
1191 CV_IMPL void cvUpdateWindow(const char* name)
1193 CV_FUNCNAME( "cvUpdateWindow" );
1200 CV_ERROR( CV_StsNullPtr, "NULL name string" );
1202 window = icvFindWindowByName( name );
1206 // window does not refresh without this
1207 gtk_widget_queue_draw( GTK_WIDGET(window->widget) );
1212 CV_IMPL void cvSetOpenGlDrawCallback(const char* name, CvOpenGlDrawCallback callback, void* userdata)
1216 CV_FUNCNAME( "cvCreateOpenGLCallback" );
1221 CV_ERROR( CV_StsNullPtr, "NULL name string" );
1223 window = icvFindWindowByName( name );
1228 CV_ERROR( CV_OpenGlNotSupported, "Window was created without OpenGL context" );
1230 window->glDrawCallback = callback;
1231 window->glDrawData = userdata;
1236 void icvSetOpenGlCleanCallback(const char* name, CvOpenGlCleanCallback callback, void* userdata)
1239 GdkGLContext* glcontext;
1240 GdkGLDrawable* gldrawable;
1242 CV_FUNCNAME( "icvSetOpenGlCleanCallback" );
1247 CV_ERROR(CV_StsNullPtr, "NULL name string");
1249 window = icvFindWindowByName(name);
1254 CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
1256 glcontext = gtk_widget_get_gl_context(window->widget);
1257 gldrawable = gtk_widget_get_gl_drawable(window->widget);
1259 gdk_gl_drawable_make_current(gldrawable, glcontext);
1261 if (window->glCleanCallback)
1262 window->glCleanCallback(window->glCleanData);
1264 window->glCleanCallback = callback;
1265 window->glCleanData = userdata;
1270 #endif // HAVE_OPENGL
1275 static void icvDeleteWindow( CvWindow* window )
1277 CvTrackbar* trackbar;
1282 GdkGLContext* glcontext = gtk_widget_get_gl_context(window->widget);
1283 GdkGLDrawable* gldrawable = gtk_widget_get_gl_drawable(window->widget);
1285 gdk_gl_drawable_make_current(gldrawable, glcontext);
1287 if (window->glCleanCallback)
1289 window->glCleanCallback(window->glCleanData);
1290 window->glCleanCallback = 0;
1291 window->glCleanData = 0;
1294 releaseGlContext(window);
1299 window->prev->next = window->next;
1301 hg_windows = window->next;
1304 window->next->prev = window->prev;
1306 window->prev = window->next = 0;
1308 gtk_widget_destroy( window->frame );
1310 for( trackbar = window->toolbar.first; trackbar != 0; )
1312 CvTrackbar* next = trackbar->next;
1313 cvFree( &trackbar );
1319 // if last window, send key press signal
1320 // to jump out of any waiting cvWaitKey's
1321 if(hg_windows==0 && thread_started){
1322 g_cond_broadcast(cond_have_key);
1328 CV_IMPL void cvDestroyWindow( const char* name )
1330 CV_FUNCNAME( "cvDestroyWindow" );
1337 CV_ERROR( CV_StsNullPtr, "NULL name string" );
1339 window = icvFindWindowByName( name );
1343 // note that it is possible for the update thread to run this function
1344 // if there is a call to cvShowImage in a mouse callback
1345 // (this would produce a deadlock on window_mutex)
1348 icvDeleteWindow( window );
1357 cvDestroyAllWindows( void )
1363 CvWindow* window = hg_windows;
1364 icvDeleteWindow( window );
1369 // CvSize icvCalcOptimalWindowSize( CvWindow * window, CvSize new_image_size){
1370 // CvSize window_size;
1371 // GtkWidget * toplevel = gtk_widget_get_toplevel( window->frame );
1372 // gdk_drawable_get_size( GDK_DRAWABLE(toplevel->window),
1373 // &window_size.width, &window_size.height );
1375 // window_size.width = window_size.width + new_image_size.width - window->widget->allocation.width;
1376 // window_size.height = window_size.height + new_image_size.height - window->widget->allocation.height;
1378 // return window_size;
1382 cvShowImage( const char* name, const CvArr* arr )
1384 CV_FUNCNAME( "cvShowImage" );
1391 CV_ERROR( CV_StsNullPtr, "NULL name" );
1395 window = icvFindWindowByName(name);
1398 cvNamedWindow(name, 1);
1399 window = icvFindWindowByName(name);
1408 CvMat* mat = cvGetMat(arr, &stub);
1410 cv::imshow(name, im);
1415 CvImageWidget * image_widget = CV_IMAGE_WIDGET( window->widget );
1416 cvImageWidgetSetImage( image_widget, arr );
1424 CV_IMPL void cvResizeWindow(const char* name, int width, int height )
1426 CV_FUNCNAME( "cvResizeWindow" );
1431 CvImageWidget * image_widget;
1434 CV_ERROR( CV_StsNullPtr, "NULL name" );
1436 window = icvFindWindowByName(name);
1440 image_widget = CV_IMAGE_WIDGET( window->widget );
1441 //if(image_widget->flags & CV_WINDOW_AUTOSIZE)
1446 gtk_window_set_resizable( GTK_WINDOW(window->frame), 1 );
1447 gtk_window_resize( GTK_WINDOW(window->frame), width, height );
1449 // disable initial resize since presumably user wants to keep
1451 image_widget->flags &= ~CV_WINDOW_NO_IMAGE;
1459 CV_IMPL void cvMoveWindow( const char* name, int x, int y )
1461 CV_FUNCNAME( "cvMoveWindow" );
1468 CV_ERROR( CV_StsNullPtr, "NULL name" );
1470 window = icvFindWindowByName(name);
1476 gtk_window_move( GTK_WINDOW(window->frame), x, y );
1485 icvFindTrackbarByName( const CvWindow* window, const char* name )
1487 CvTrackbar* trackbar = window->toolbar.first;
1489 for( ; trackbar != 0 && strcmp( trackbar->name, name ) != 0; trackbar = trackbar->next )
1496 icvCreateTrackbar( const char* trackbar_name, const char* window_name,
1497 int* val, int count, CvTrackbarCallback on_notify,
1498 CvTrackbarCallback2 on_notify2, void* userdata )
1502 CV_FUNCNAME( "icvCreateTrackbar" );
1506 /*char slider_name[32];*/
1507 CvWindow* window = 0;
1508 CvTrackbar* trackbar = 0;
1510 if( !window_name || !trackbar_name )
1511 CV_ERROR( CV_StsNullPtr, "NULL window or trackbar name" );
1514 CV_ERROR( CV_StsOutOfRange, "Bad trackbar maximal value" );
1516 window = icvFindWindowByName(window_name);
1520 trackbar = icvFindTrackbarByName(window,trackbar_name);
1526 int len = strlen(trackbar_name);
1527 trackbar = (CvTrackbar*)cvAlloc(sizeof(CvTrackbar) + len + 1);
1528 memset( trackbar, 0, sizeof(*trackbar));
1529 trackbar->signature = CV_TRACKBAR_MAGIC_VAL;
1530 trackbar->name = (char*)(trackbar+1);
1531 memcpy( trackbar->name, trackbar_name, len + 1 );
1532 trackbar->parent = window;
1533 trackbar->next = window->toolbar.first;
1534 window->toolbar.first = trackbar;
1536 GtkWidget* hscale_box = gtk_hbox_new( FALSE, 10 );
1537 GtkWidget* hscale_label = gtk_label_new( trackbar_name );
1538 GtkWidget* hscale = gtk_hscale_new_with_range( 0, count, 1 );
1539 gtk_range_set_update_policy( GTK_RANGE(hscale), GTK_UPDATE_CONTINUOUS );
1540 gtk_scale_set_digits( GTK_SCALE(hscale), 0 );
1541 //gtk_scale_set_value_pos( hscale, GTK_POS_TOP );
1542 gtk_scale_set_draw_value( GTK_SCALE(hscale), TRUE );
1544 trackbar->widget = hscale;
1545 gtk_box_pack_start( GTK_BOX(hscale_box), hscale_label, FALSE, FALSE, 5 );
1546 gtk_widget_show( hscale_label );
1547 gtk_box_pack_start( GTK_BOX(hscale_box), hscale, TRUE, TRUE, 5 );
1548 gtk_widget_show( hscale );
1549 gtk_box_pack_start( GTK_BOX(window->paned), hscale_box, FALSE, FALSE, 5 );
1550 gtk_widget_show( hscale_box );
1561 gtk_range_set_value( GTK_RANGE(trackbar->widget), value );
1562 trackbar->pos = value;
1563 trackbar->data = val;
1566 trackbar->maxval = count;
1567 trackbar->notify = on_notify;
1568 trackbar->notify2 = on_notify2;
1569 trackbar->userdata = userdata;
1570 gtk_signal_connect( GTK_OBJECT(trackbar->widget), "value-changed",
1571 GTK_SIGNAL_FUNC(icvOnTrackbar), trackbar );
1573 // queue a widget resize to trigger a window resize to
1574 // compensate for the addition of trackbars
1575 gtk_widget_queue_resize( GTK_WIDGET(window->widget) );
1589 cvCreateTrackbar( const char* trackbar_name, const char* window_name,
1590 int* val, int count, CvTrackbarCallback on_notify )
1592 return icvCreateTrackbar(trackbar_name, window_name, val, count,
1598 cvCreateTrackbar2( const char* trackbar_name, const char* window_name,
1599 int* val, int count, CvTrackbarCallback2 on_notify2,
1602 return icvCreateTrackbar(trackbar_name, window_name, val, count,
1603 0, on_notify2, userdata);
1608 cvSetMouseCallback( const char* window_name, CvMouseCallback on_mouse, void* param )
1610 CV_FUNCNAME( "cvSetMouseCallback" );
1614 CvWindow* window = 0;
1617 CV_ERROR( CV_StsNullPtr, "NULL window name" );
1619 window = icvFindWindowByName(window_name);
1623 window->on_mouse = on_mouse;
1624 window->on_mouse_param = param;
1630 CV_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name )
1634 CV_FUNCNAME( "cvGetTrackbarPos" );
1639 CvTrackbar* trackbar = 0;
1641 if( trackbar_name == 0 || window_name == 0 )
1642 CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
1644 window = icvFindWindowByName( window_name );
1646 trackbar = icvFindTrackbarByName( window, trackbar_name );
1649 pos = trackbar->pos;
1657 CV_IMPL void cvSetTrackbarPos( const char* trackbar_name, const char* window_name, int pos )
1659 CV_FUNCNAME( "cvSetTrackbarPos" );
1664 CvTrackbar* trackbar = 0;
1666 if( trackbar_name == 0 || window_name == 0 )
1667 CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
1669 window = icvFindWindowByName( window_name );
1671 trackbar = icvFindTrackbarByName( window, trackbar_name );
1678 if( pos > trackbar->maxval )
1679 pos = trackbar->maxval;
1684 gtk_range_set_value( GTK_RANGE(trackbar->widget), pos );
1692 CV_IMPL void* cvGetWindowHandle( const char* window_name )
1696 CV_FUNCNAME( "cvGetWindowHandle" );
1702 if( window_name == 0 )
1703 CV_ERROR( CV_StsNullPtr, "NULL window name" );
1705 window = icvFindWindowByName( window_name );
1707 widget = (void*)window->widget;
1715 CV_IMPL const char* cvGetWindowName( void* window_handle )
1717 const char* window_name = "";
1719 CV_FUNCNAME( "cvGetWindowName" );
1725 if( window_handle == 0 )
1726 CV_ERROR( CV_StsNullPtr, "NULL window" );
1728 window = icvWindowByWidget( (GtkWidget*)window_handle );
1730 window_name = window->name;
1737 static gboolean icvOnKeyPress( GtkWidget * /*widget*/,
1738 GdkEventKey* event, gpointer /*user_data*/ )
1742 switch( event->keyval )
1755 code = event->keyval;
1758 code |= event->state << 16;
1761 if(thread_started) g_mutex_lock(last_key_mutex);
1768 // signal any waiting threads
1769 g_cond_broadcast(cond_have_key);
1770 g_mutex_unlock(last_key_mutex);
1778 static void icvOnTrackbar( GtkWidget* widget, gpointer user_data )
1780 int pos = cvRound( gtk_range_get_value(GTK_RANGE(widget)));
1781 CvTrackbar* trackbar = (CvTrackbar*)user_data;
1783 if( trackbar && trackbar->signature == CV_TRACKBAR_MAGIC_VAL &&
1784 trackbar->widget == widget )
1786 trackbar->pos = pos;
1787 if( trackbar->data )
1788 *trackbar->data = pos;
1789 if( trackbar->notify2 )
1790 trackbar->notify2(pos, trackbar->userdata);
1791 else if( trackbar->notify )
1792 trackbar->notify(pos);
1796 static gboolean icvOnClose( GtkWidget* widget, GdkEvent* /*event*/, gpointer user_data )
1798 CvWindow* window = (CvWindow*)user_data;
1799 if( window->signature == CV_WINDOW_MAGIC_VAL &&
1800 window->frame == widget )
1802 icvDeleteWindow(window);
1808 static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_data )
1810 // TODO move this logic to CvImageWidget
1811 CvWindow* window = (CvWindow*)user_data;
1812 CvPoint2D32f pt32f = {-1., -1.};
1813 CvPoint pt = {-1,-1};
1814 int cv_event = -1, state = 0;
1815 CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget );
1817 if( window->signature != CV_WINDOW_MAGIC_VAL ||
1818 window->widget != widget || !window->widget ||
1819 !window->on_mouse /*|| !image_widget->original_image*/)
1822 if( event->type == GDK_MOTION_NOTIFY )
1824 GdkEventMotion* event_motion = (GdkEventMotion*)event;
1826 cv_event = CV_EVENT_MOUSEMOVE;
1827 pt32f.x = cvRound(event_motion->x);
1828 pt32f.y = cvRound(event_motion->y);
1829 state = event_motion->state;
1831 else if( event->type == GDK_BUTTON_PRESS ||
1832 event->type == GDK_BUTTON_RELEASE ||
1833 event->type == GDK_2BUTTON_PRESS )
1835 GdkEventButton* event_button = (GdkEventButton*)event;
1836 pt32f.x = cvRound(event_button->x);
1837 pt32f.y = cvRound(event_button->y);
1840 if( event_button->type == GDK_BUTTON_PRESS )
1842 cv_event = event_button->button == 1 ? CV_EVENT_LBUTTONDOWN :
1843 event_button->button == 2 ? CV_EVENT_MBUTTONDOWN :
1844 event_button->button == 3 ? CV_EVENT_RBUTTONDOWN : 0;
1846 else if( event_button->type == GDK_BUTTON_RELEASE )
1848 cv_event = event_button->button == 1 ? CV_EVENT_LBUTTONUP :
1849 event_button->button == 2 ? CV_EVENT_MBUTTONUP :
1850 event_button->button == 3 ? CV_EVENT_RBUTTONUP : 0;
1852 else if( event_button->type == GDK_2BUTTON_PRESS )
1854 cv_event = event_button->button == 1 ? CV_EVENT_LBUTTONDBLCLK :
1855 event_button->button == 2 ? CV_EVENT_MBUTTONDBLCLK :
1856 event_button->button == 3 ? CV_EVENT_RBUTTONDBLCLK : 0;
1858 state = event_button->state;
1861 if( cv_event >= 0 ){
1862 // scale point if image is scaled
1863 if( (image_widget->flags & CV_WINDOW_AUTOSIZE)==0 &&
1864 image_widget->original_image &&
1865 image_widget->scaled_image ){
1866 // image origin is not necessarily at (0,0)
1867 int x0 = (widget->allocation.width - image_widget->scaled_image->cols)/2;
1868 int y0 = (widget->allocation.height - image_widget->scaled_image->rows)/2;
1869 pt.x = cvRound( ((pt32f.x-x0)*image_widget->original_image->cols)/
1870 image_widget->scaled_image->cols );
1871 pt.y = cvRound( ((pt32f.y-y0)*image_widget->original_image->rows)/
1872 image_widget->scaled_image->rows );
1875 pt = cvPointFrom32f( pt32f );
1878 // if((unsigned)pt.x < (unsigned)(image_widget->original_image->width) &&
1879 // (unsigned)pt.y < (unsigned)(image_widget->original_image->height) )
1881 int flags = (state & GDK_SHIFT_MASK ? CV_EVENT_FLAG_SHIFTKEY : 0) |
1882 (state & GDK_CONTROL_MASK ? CV_EVENT_FLAG_CTRLKEY : 0) |
1883 (state & (GDK_MOD1_MASK|GDK_MOD2_MASK) ? CV_EVENT_FLAG_ALTKEY : 0) |
1884 (state & GDK_BUTTON1_MASK ? CV_EVENT_FLAG_LBUTTON : 0) |
1885 (state & GDK_BUTTON2_MASK ? CV_EVENT_FLAG_MBUTTON : 0) |
1886 (state & GDK_BUTTON3_MASK ? CV_EVENT_FLAG_RBUTTON : 0);
1887 window->on_mouse( cv_event, pt.x, pt.y, flags, window->on_mouse_param );
1895 static gboolean icvAlarm( gpointer user_data )
1897 *(int*)user_data = 1;
1902 CV_IMPL int cvWaitKey( int delay )
1905 if(thread_started && g_thread_self()!=window_thread){
1909 // wait for signal or timeout if delay > 0
1912 g_get_current_time(&timer);
1913 g_time_val_add(&timer, delay*1000);
1914 expired = !g_cond_timed_wait(cond_have_key, last_key_mutex, &timer);
1917 g_cond_wait(cond_have_key, last_key_mutex);
1920 my_last_key = last_key;
1921 g_mutex_unlock(last_key_mutex);
1922 if(expired || hg_windows==0){
1932 timer = g_timeout_add( delay, icvAlarm, &expired );
1934 while( gtk_main_iteration_do(TRUE) && last_key < 0 && !expired && hg_windows != 0 )
1937 if( delay > 0 && !expired )
1938 g_source_remove(timer);