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"
44 #include <Carbon/Carbon.h>
45 #include <Quicktime/Quicktime.h>//YV
51 //#define MS_TO_TICKS(a) a*3/50
54 #define INTERWIDGETSPACE 16
55 #define WIDGETHEIGHT 32
60 typedef struct CvTrackbar
73 int labelSize;//Yannick Verdie
74 CvTrackbarCallback notify;
75 CvTrackbarCallback2 notify2;
81 typedef struct CvWindow
90 WindowRef oldwindow;//YV
101 Ptr restoreState;//YV
103 CvMouseCallback on_mouse;
104 void* on_mouse_param;
116 static CvWindow* hg_windows = 0;
118 #define Assert(exp) \
121 printf("Assertion: %s %s: %d\n", #exp, __FILE__, __LINE__);\
125 static int wasInitialized = 0;
126 static char lastKey = NO_KEY;
127 OSStatus keyHandler(EventHandlerCallRef hcr, EventRef theEvent, void* inUserData);
128 static pascal OSStatus windowEventHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *inUserData);
130 static const EventTypeSpec applicationKeyboardEvents[] =
132 { kEventClassKeyboard, kEventRawKeyDown },
135 CV_IMPL int cvInitSystem( int argc, char** argv )
138 if( !wasInitialized )
142 err = InstallApplicationEventHandler(NewEventHandlerUPP( keyHandler),GetEventTypeCount(applicationKeyboardEvents),applicationKeyboardEvents,NULL,NULL);
145 fprintf(stderr,"InstallApplicationEventHandler was not ok\n");
153 // TODO: implement missing functionality
154 CV_IMPL int cvStartWindowThread()
159 static int icvCountTrackbarInWindow( const CvWindow* window)
161 CvTrackbar* trackbar = window->toolbar.first;
163 while (trackbar != 0) {
165 trackbar = trackbar->next;
170 static CvTrackbar* icvTrackbarByHandle( void * handle )
172 CvWindow* window = hg_windows;
173 CvTrackbar* trackbar = NULL;
174 while( window != 0 && window->window != handle) {
175 trackbar = window->toolbar.first;
176 while (trackbar != 0 && trackbar->trackbar != handle)
177 trackbar = trackbar->next;
178 if (trackbar != 0 && trackbar->trackbar == handle)
180 window = window->next;
185 static CvWindow* icvWindowByHandle( void * handle )
187 CvWindow* window = hg_windows;
189 while( window != 0 && window->window != handle)
190 window = window->next;
195 CV_IMPL CvWindow * icvFindWindowByName( const char* name)
197 CvWindow* window = hg_windows;
198 while( window != 0 && strcmp(name, window->name) != 0 )
199 window = window->next;
204 static CvTrackbar* icvFindTrackbarByName( const CvWindow* window, const char* name )
206 CvTrackbar* trackbar = window->toolbar.first;
208 while (trackbar != 0 && strcmp( trackbar->name, name ) != 0)
209 trackbar = trackbar->next;
215 /* draw image to frame */
216 static void icvDrawImage( CvWindow* window )
218 Assert( window != 0 );
219 if( window->imageRef == 0 ) return;
221 CGContextRef myContext;
224 int width = window->imageWidth;
225 int height = window->imageHeight;
227 GetWindowPortBounds(window->window, &portrect);
229 if(!( window->flags & CV_WINDOW_AUTOSIZE) ) //YV
231 CGPoint origin = {0,0};
232 CGSize size = {portrect.right-portrect.left, portrect.bottom-portrect.top-window->trackbarheight};
233 rect.origin = origin; rect.size = size;
237 CGPoint origin = {0, portrect.bottom - height - window->trackbarheight};
238 CGSize size = {width, height};
239 rect.origin = origin; rect.size = size;
242 /* To be sybnchronous we are using this, better would be to susbcribe to the draw event and process whenever requested, we might save SOME CPU cycles*/
243 SetPortWindowPort (window->window);
244 QDBeginCGContext (GetWindowPort (window->window), &myContext);
245 CGContextSetInterpolationQuality (myContext, kCGInterpolationLow);
246 CGContextDrawImage(myContext,rect,window->imageRef);
247 CGContextFlush(myContext);// 4
248 QDEndCGContext (GetWindowPort(window->window), &myContext);// 5
252 /* update imageRef */
253 static void icvPutImage( CvWindow* window )
255 Assert( window != 0 );
256 if( window->image == 0 ) return;
258 CGColorSpaceRef colorspace = NULL;
259 CGDataProviderRef provider = NULL;
260 int width = window->imageWidth = window->image->cols;
261 int height = window->imageHeight = window->image->rows;
263 colorspace = CGColorSpaceCreateDeviceRGB();
268 provider = CGDataProviderCreateWithData(NULL, window->image->data.ptr, width * height , NULL );
270 if (window->imageRef != NULL){
271 CGImageRelease(window->imageRef);
272 window->imageRef = NULL;
275 window->imageRef = CGImageCreate( width, height, size , size*nbChannels , window->image->step, colorspace, kCGImageAlphaNone , provider, NULL, true, kCGRenderingIntentDefault );
276 icvDrawImage( window );
278 /* release the provider's memory */
279 CGDataProviderRelease( provider );
282 static void icvUpdateWindowSize( const CvWindow* window )
284 int width = 0, height = 240; /* init à al taille de base de l'image*/
287 GetWindowBounds(window->window, kWindowContentRgn, &globalBounds);
291 if( window->image ) {
292 width = MAX(MAX(window->image->width, width), minWidth);
293 height = window->image->height;
297 height += window->trackbarheight;
299 //height +=WIDGETHEIGHT; /* 32 pixels are spearating tracbars from the video display */
301 globalBounds.right = globalBounds.left + width;
302 globalBounds.bottom = globalBounds.top + height;
303 SetWindowBounds(window->window, kWindowContentRgn, &globalBounds);
306 static void icvDeleteWindow( CvWindow* window )
308 CvTrackbar* trackbar;
311 window->prev->next = window->next;
313 hg_windows = window->next;
316 window->next->prev = window->prev;
318 window->prev = window->next = 0;
320 cvReleaseMat( &window->image );
321 cvReleaseMat( &window->dst_image );
323 for( trackbar = window->toolbar.first; trackbar != 0; )
325 CvTrackbar* next = trackbar->next;
326 cvFree( (void**)&trackbar );
330 if (window->imageRef != NULL)
331 CGImageRelease(window->imageRef);
333 DisposeWindow (window->window);//YV
335 cvFree( (void**)&window );
339 CV_IMPL void cvDestroyWindow( const char* name)
341 CV_FUNCNAME( "cvDestroyWindow" );
348 CV_ERROR( CV_StsNullPtr, "NULL name string" );
350 window = icvFindWindowByName( name );
354 icvDeleteWindow( window );
360 CV_IMPL void cvDestroyAllWindows( void )
364 CvWindow* window = hg_windows;
365 icvDeleteWindow( window );
370 CV_IMPL void cvShowImage( const char* name, const CvArr* arr)
372 CV_FUNCNAME( "cvShowImage" );
382 CV_ERROR( CV_StsNullPtr, "NULL name" );
384 window = icvFindWindowByName(name);
387 cvNamedWindow(name, 1);
388 window = icvFindWindowByName(name);
391 if( !window || !arr )
392 EXIT; // keep silence here.
394 if( CV_IS_IMAGE_HDR( arr ))
395 origin = ((IplImage*)arr)->origin;
397 CV_CALL( image = cvGetMat( arr, &stub ));
401 cvResizeWindow( name, image->cols, image->rows );
405 !CV_ARE_SIZES_EQ(window->image, image) ) {
406 if ( ! (window->flags & CV_WINDOW_AUTOSIZE) )//FD
408 cvReleaseMat( &window->image );
411 if( !window->image ) {
413 window->image = cvCreateMat( image->rows, image->cols, CV_8UC3 );
416 cvConvertImage( image, window->image, (origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB );
417 icvPutImage( window );
419 icvUpdateWindowSize( window );
424 CV_IMPL void cvResizeWindow( const char* name, int width, int height)
426 CV_FUNCNAME( "cvResizeWindow" );
431 //CvTrackbar* trackbar;
434 CV_ERROR( CV_StsNullPtr, "NULL name" );
436 window = icvFindWindowByName(name);
440 SizeWindow(window->window, width, height, true);
445 CV_IMPL void cvMoveWindow( const char* name, int x, int y)
447 CV_FUNCNAME( "cvMoveWindow" );
452 //CvTrackbar* trackbar;
455 CV_ERROR( CV_StsNullPtr, "NULL name" );
457 window = icvFindWindowByName(name);
461 MoveWindow(window->window, x, y, true);
466 void TrackbarActionProcPtr (ControlRef theControl, ControlPartCode partCode)
468 CvTrackbar * trackbar = icvTrackbarByHandle (theControl);
470 if (trackbar == NULL)
472 fprintf(stderr,"Error getting trackbar\n");
477 int pos = GetControl32BitValue (theControl);
478 if ( trackbar->data )
479 *trackbar->data = pos;
480 if ( trackbar->notify )
481 trackbar->notify(pos);
482 else if ( trackbar->notify2 )
483 trackbar->notify2(pos, trackbar->userdata);
485 //--------YV---------------------------
486 CFStringEncoding encoding = kCFStringEncodingASCII;
487 CFAllocatorRef alloc_default = kCFAllocatorDefault; // = NULL;
489 char valueinchar[20];
490 sprintf(valueinchar, " (%d)", *trackbar->data);
492 // create an empty CFMutableString
493 CFIndex maxLength = 256;
494 CFMutableStringRef cfstring = CFStringCreateMutable(alloc_default,maxLength);
496 // append some c strings into it.
497 CFStringAppendCString(cfstring,trackbar->name,encoding);
498 CFStringAppendCString(cfstring,valueinchar,encoding);
500 SetControlData(trackbar->label, kControlEntireControl,kControlStaticTextCFStringTag, sizeof(cfstring), &cfstring);
501 DrawControls(trackbar->parent->window);
502 //-----------------------------------------
507 static int icvCreateTrackbar (const char* trackbar_name,
508 const char* window_name,
510 CvTrackbarCallback on_notify,
511 CvTrackbarCallback2 on_notify2,
516 CV_FUNCNAME( "icvCreateTrackbar" );
519 /*char slider_name[32];*/
520 CvWindow* window = 0;
521 CvTrackbar* trackbar = 0;
523 ControlRef outControl;
524 ControlRef stoutControl;
527 if( !window_name || !trackbar_name )
528 CV_ERROR( CV_StsNullPtr, "NULL window or trackbar name" );
531 CV_ERROR( CV_StsOutOfRange, "Bad trackbar maximal value" );
533 window = icvFindWindowByName(window_name);
537 trackbar = icvFindTrackbarByName(window,trackbar_name);
540 int len = strlen(trackbar_name);
541 trackbar = (CvTrackbar*)cvAlloc(sizeof(CvTrackbar) + len + 1);
542 memset( trackbar, 0, sizeof(*trackbar));
543 trackbar->signature = CV_TRACKBAR_MAGIC_VAL;
544 trackbar->name = (char*)(trackbar+1);
545 memcpy( trackbar->name, trackbar_name, len + 1 );
546 trackbar->parent = window;
547 trackbar->next = window->toolbar.first;
548 window->toolbar.first = trackbar;
557 trackbar->pos = value;
558 trackbar->data = val;
561 trackbar->maxval = count;
563 //----------- YV ----------------------
566 while((count/=10)>10){
570 //pad size maxvalue in pixel
572 char valueinchar[strlen(trackbar_name)+1 +1 +1+nbDigit+1];//length+\n +space +(+nbDigit+)
573 sprintf(valueinchar, "%s (%d)",trackbar_name, trackbar->maxval);
575 CFStringRef text = CFStringCreateWithCString(NULL,valueinchar,kCFStringEncodingASCII);
576 GetThemeTextDimensions( text, kThemeCurrentPortFont, kThemeStateActive, false, &qdSize, &baseline );
577 trackbar->labelSize = qdSize.h;
578 //--------------------------------------
580 int c = icvCountTrackbarInWindow(window);
582 GetWindowBounds(window->window,kWindowContentRgn,&bounds);
584 stboundsRect.top = (INTERWIDGETSPACE +WIDGETHEIGHT)* (c-1)+INTERWIDGETSPACE;
585 stboundsRect.left = INTERWIDGETSPACE;
586 stboundsRect.bottom = stboundsRect.top + WIDGETHEIGHT;
587 stboundsRect.right = stboundsRect.left+LABELWIDTH;
589 //fprintf(stdout,"create trackabar bounds (%d %d %d %d)\n",stboundsRect.top,stboundsRect.left,stboundsRect.bottom,stboundsRect.right);
590 //----------- YV ----------------------
591 sprintf(valueinchar, "%s (%d)",trackbar_name, trackbar->pos);
592 CreateStaticTextControl (window->window,&stboundsRect,CFStringCreateWithCString(NULL,valueinchar,kCFStringEncodingASCII),NULL,&stoutControl);
593 //--------------------------------------
595 stboundsRect.top = (INTERWIDGETSPACE +WIDGETHEIGHT)* (c-1)+INTERWIDGETSPACE;
596 stboundsRect.left = INTERWIDGETSPACE*2+LABELWIDTH;
597 stboundsRect.bottom = stboundsRect.top + WIDGETHEIGHT;
598 stboundsRect.right = bounds.right-INTERWIDGETSPACE;
600 CreateSliderControl (window->window,&stboundsRect, trackbar->pos,0,trackbar->maxval,kControlSliderLiveFeedback,0,true,NewControlActionUPP(TrackbarActionProcPtr),&outControl);
602 bounds.bottom += INTERWIDGETSPACE + WIDGETHEIGHT;
603 SetControlVisibility (outControl,true,true);
604 SetControlVisibility (stoutControl,true,true);
606 trackbar->trackbar = outControl;
607 trackbar->label = stoutControl;
609 window->trackbarheight = INTERWIDGETSPACE*2 + WIDGETHEIGHT;
611 window->trackbarheight += INTERWIDGETSPACE + WIDGETHEIGHT;
612 icvUpdateWindowSize( window );
615 trackbar->notify = on_notify;
616 trackbar->notify2 = on_notify2;
617 trackbar->userdata = userdata;
626 CV_IMPL int cvCreateTrackbar (const char* trackbar_name,
627 const char* window_name,
629 CvTrackbarCallback on_notify)
631 return icvCreateTrackbar(trackbar_name, window_name, val, count, on_notify, 0, 0);
635 CV_IMPL int cvCreateTrackbar2(const char* trackbar_name,
636 const char* window_name,
638 CvTrackbarCallback2 on_notify2,
641 return icvCreateTrackbar(trackbar_name, window_name, val,
642 count, 0, on_notify2, userdata);
647 cvSetMouseCallback( const char* name, CvMouseCallback function, void* info)
649 CvWindow* window = icvFindWindowByName( name );
652 window->on_mouse = function;
653 window->on_mouse_param = info;
657 fprintf(stdout,"Error with cvSetMouseCallback. Window not found : %s\n",name);
661 CV_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name )
665 CV_FUNCNAME( "cvGetTrackbarPos" );
670 CvTrackbar* trackbar = 0;
672 if( trackbar_name == 0 || window_name == 0 )
673 CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
675 window = icvFindWindowByName( window_name );
677 trackbar = icvFindTrackbarByName( window, trackbar_name );
687 CV_IMPL void cvSetTrackbarPos(const char* trackbar_name, const char* window_name, int pos)
689 CV_FUNCNAME( "cvSetTrackbarPos" );
694 CvTrackbar* trackbar = 0;
696 if( trackbar_name == 0 || window_name == 0 )
697 CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
699 window = icvFindWindowByName( window_name );
701 trackbar = icvFindTrackbarByName( window, trackbar_name );
708 if( pos > trackbar->maxval )
709 pos = trackbar->maxval;
711 // Set new value and redraw the trackbar
712 SetControlValue( trackbar->trackbar, pos );
713 Draw1Control( trackbar->trackbar );
720 CV_IMPL void* cvGetWindowHandle( const char* name )
722 WindowRef result = 0;
727 window = icvFindWindowByName( name );
729 result = window->window;
739 CV_IMPL const char* cvGetWindowName( void* window_handle )
741 const char* window_name = "";
743 CV_FUNCNAME( "cvGetWindowName" );
749 if( window_handle == 0 )
750 CV_ERROR( CV_StsNullPtr, "NULL window" );
751 window = icvWindowByHandle(window_handle );
753 window_name = window->name;
760 double cvGetModeWindow_CARBON(const char* name)//YV
764 CV_FUNCNAME( "cvGetModeWindow_QT" );
771 CV_ERROR( CV_StsNullPtr, "NULL name string" );
773 window = icvFindWindowByName( name );
775 CV_ERROR( CV_StsNullPtr, "NULL window" );
777 result = window->status;
783 void cvSetModeWindow_CARBON( const char* name, double prop_value)//Yannick Verdie
785 OSStatus err = noErr;
788 CV_FUNCNAME( "cvSetModeWindow_QT" );
795 CV_ERROR( CV_StsNullPtr, "NULL name string" );
797 window = icvFindWindowByName( name );
799 CV_ERROR( CV_StsNullPtr, "NULL window" );
801 if(window->flags & CV_WINDOW_AUTOSIZE)//if the flag CV_WINDOW_AUTOSIZE is set
804 if (window->status==CV_WINDOW_FULLSCREEN && prop_value==CV_WINDOW_NORMAL)
806 err = EndFullScreen(window->restoreState,0);
808 fprintf(stdout,"Error EndFullScreen\n");
809 window->window = window->oldwindow;
810 ShowWindow( window->window );
812 window->status=CV_WINDOW_NORMAL;
816 if (window->status==CV_WINDOW_NORMAL && prop_value==CV_WINDOW_FULLSCREEN)
819 err = GetWindowGreatestAreaDevice(window->window, kWindowTitleBarRgn, &device, NULL);
821 fprintf(stdout,"Error GetWindowGreatestAreaDevice\n");
823 HideWindow(window->window);
824 window->oldwindow = window->window;
825 err = BeginFullScreen(&(window->restoreState), device, 0, 0, &window->window, 0, fullScreenAllowEvents | fullScreenDontSwitchMonitorResolution);
827 fprintf(stdout,"Error BeginFullScreen\n");
829 window->status=CV_WINDOW_FULLSCREEN;
836 void cv::setWindowTitle(const String& winname, const String& title)
838 CvWindow* window = icvFindWindowByName(winname.c_str());
842 namedWindow(winname);
843 window = icvFindWindowByName(winname.c_str());
847 CV_Error(Error::StsNullPtr, "NULL window");
849 if (noErr != SetWindowTitleWithCFString(window->window, CFStringCreateWithCString(NULL, title.c_str(), kCFStringEncodingASCII)))
850 CV_Error_(Error::StsError, ("Failed to set \"%s\" window title to \"%s\"", winname.c_str(), title.c_str()));
853 CV_IMPL int cvNamedWindow( const char* name, int flags )
856 CV_FUNCNAME( "cvNamedWindow" );
858 cvInitSystem(0, NULL);
860 // to be able to display a window, we need to be a 'faceful' application
861 // http://lists.apple.com/archives/carbon-dev/2005/Jun/msg01414.html
862 static bool switched_to_faceful = false;
863 if (! switched_to_faceful)
865 ProcessSerialNumber psn = { 0, kCurrentProcess };
866 OSStatus ret = TransformProcessType (&psn, kProcessTransformToForegroundApplication );
870 SetFrontProcess( &psn );
871 switched_to_faceful = true;
875 fprintf(stderr, "Failed to tranform process type: %d\n", (int) ret);
882 WindowRef outWindow = NULL;
883 OSStatus err = noErr;
884 Rect contentBounds = {100,100,320,440};
887 UInt wAttributes = 0;
891 const EventTypeSpec genericWindowEventHandler[] = {
892 { kEventClassMouse, kEventMouseMoved},
893 { kEventClassMouse, kEventMouseDragged},
894 { kEventClassMouse, kEventMouseUp},
895 { kEventClassMouse, kEventMouseDown},
896 { kEventClassWindow, kEventWindowClose },
897 { kEventClassWindow, kEventWindowBoundsChanged }//FD
901 CV_ERROR( CV_StsNullPtr, "NULL name string" );
903 if( icvFindWindowByName( name ) != 0 ){
908 CV_CALL( window = (CvWindow*)cvAlloc(sizeof(CvWindow) + len + 1));
909 memset( window, 0, sizeof(*window));
910 window->name = (char*)(window + 1);
911 memcpy( window->name, name, len + 1 );
912 window->flags = flags;
913 window->status = CV_WINDOW_NORMAL;//YV
914 window->signature = CV_WINDOW_MAGIC_VAL;
916 window->last_key = 0;
917 window->on_mouse = 0;
918 window->on_mouse_param = 0;
920 window->next = hg_windows;
923 hg_windows->prev = window;
925 wAttributes = kWindowStandardDocumentAttributes | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute;
928 if (window->flags & CV_WINDOW_AUTOSIZE)//Yannick verdie, remove the handler at the bottom-right position of the window in AUTORESIZE mode
931 wAttributes = kWindowCloseBoxAttribute | kWindowFullZoomAttribute | kWindowCollapseBoxAttribute | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute;
934 err = CreateNewWindow ( kDocumentWindowClass,wAttributes,&contentBounds,&outWindow);
936 fprintf(stderr,"Error while creating the window\n");
938 SetWindowTitleWithCFString(outWindow,CFStringCreateWithCString(NULL,name,kCFStringEncodingASCII));
940 fprintf(stdout,"Error SetWindowTitleWithCFString\n");
942 window->window = outWindow;
943 window->oldwindow = 0;//YV
945 err = InstallWindowEventHandler(outWindow, NewEventHandlerUPP(windowEventHandler), GetEventTypeCount(genericWindowEventHandler), genericWindowEventHandler, outWindow, NULL);
947 ShowWindow( outWindow );
954 static pascal OSStatus windowEventHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *inUserData)
956 CvWindow* window = NULL;
957 UInt32 eventKind, eventClass;
961 HIPoint point = {0,0};
962 EventMouseButton eventMouseButton = 0;//FD
963 UInt32 modifiers;//FD
965 WindowRef theWindow = (WindowRef)inUserData;
966 if (theWindow == NULL)
967 return eventNotHandledErr;
968 window = icvWindowByHandle(theWindow);
970 return eventNotHandledErr;
972 eventKind = GetEventKind(theEvent);
973 eventClass = GetEventClass(theEvent);
975 switch (eventClass) {
976 case kEventClassMouse : {
979 case kEventMouseDown :
980 case kEventMouseMoved :
981 case kEventMouseDragged : {
982 err = CallNextEventHandler(nextHandler, theEvent);
983 if (err != eventNotHandledErr)
985 err = GetEventParameter(theEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof(eventMouseButton), NULL, &eventMouseButton);
986 err = GetEventParameter(theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(modifiers), NULL, &modifiers);
987 err = GetEventParameter(theEvent,kEventParamClickCount,typeUInt32,NULL,sizeof(UInt32),NULL,&count);
989 if (count >1) event += 6;
991 event = CV_EVENT_MOUSEMOVE;
994 if (eventKind == kEventMouseUp)
996 if (eventKind == kEventMouseDown)
999 unsigned int flags = 0;
1001 err = GetEventParameter(theEvent, kEventParamWindowMouseLocation, typeHIPoint, NULL, sizeof(point), NULL, &point);
1002 if (eventKind != kEventMouseMoved){
1003 switch(eventMouseButton){
1004 case kEventMouseButtonPrimary:
1005 if (modifiers & controlKey){
1006 flags += CV_EVENT_FLAG_RBUTTON;
1009 flags += CV_EVENT_FLAG_LBUTTON;
1012 case kEventMouseButtonSecondary:
1013 flags += CV_EVENT_FLAG_RBUTTON;
1016 case kEventMouseButtonTertiary:
1017 flags += CV_EVENT_FLAG_MBUTTON;
1023 if (modifiers&controlKey) flags += CV_EVENT_FLAG_CTRLKEY;
1024 if (modifiers&shiftKey) flags += CV_EVENT_FLAG_SHIFTKEY;
1025 if (modifiers& cmdKey ) flags += CV_EVENT_FLAG_ALTKEY;
1027 if (window->on_mouse != NULL){
1029 Rect structure, content;
1030 GetWindowBounds(theWindow, kWindowStructureRgn, &structure);
1031 GetWindowBounds(theWindow, kWindowContentRgn, &content);
1032 lx = (int)point.x - content.left + structure.left;
1033 ly = (int)point.y - window->trackbarheight - content.top + structure.top; /* minus la taille des trackbars */
1034 if (window->flags & CV_WINDOW_AUTOSIZE) {//FD
1035 //printf("was %d,%d\n", lx, ly);
1036 /* scale the mouse coordinates */
1037 lx = lx * window->imageWidth / (content.right - content.left);
1038 ly = ly * window->imageHeight / (content.bottom - content.top - window->trackbarheight);
1041 if (lx>0 && ly >0){ /* a remettre dans les coordonnées locale */
1042 window->on_mouse (event, lx, ly, flags, window->on_mouse_param);
1047 default : return eventNotHandledErr;
1050 case kEventClassWindow : {//FD
1052 case kEventWindowBoundsChanged :
1054 /* resize the trackbars */
1057 GetWindowBounds(window->window,kWindowContentRgn,&bounds);
1058 for ( t = window->toolbar.first; t != 0; t = t->next )
1059 SizeControl(t->trackbar,bounds.right - bounds.left - INTERWIDGETSPACE*3 - LABELWIDTH , WIDGETHEIGHT);
1061 /* redraw the image */
1062 icvDrawImage(window);
1065 return eventNotHandledErr;
1069 return eventNotHandledErr;
1072 return eventNotHandledErr;
1075 OSStatus keyHandler(EventHandlerCallRef hcr, EventRef theEvent, void* inUserData)
1081 eventKind = GetEventKind (theEvent);
1082 eventClass = GetEventClass (theEvent);
1083 err = GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(lastKey), NULL, &lastKey);
1090 CV_IMPL int cvWaitKey (int maxWait)
1092 EventRecord theEvent;
1094 // wait at least for one event (to allow mouse, etc. processing), exit if maxWait milliseconds passed (nullEvent)
1095 UInt32 start = TickCount();
1099 // remaining time until maxWait is over
1100 UInt32 wait = EventTimeToTicks (maxWait / 1000.0) - (TickCount() - start);
1103 if( maxWait > 0 && iters > 0 )
1108 WaitNextEvent (everyEvent, &theEvent, maxWait > 0 ? wait : kDurationForever, NULL);
1110 while (lastKey == NO_KEY && theEvent.what != nullEvent);