Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/highgui/src/window_carbon.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
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.
8
//
9
//
10
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
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.
25
//
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.
28
//
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.
39
//
40
//M*/
41
42
#include "precomp.hpp"
43
44
#include <Carbon/Carbon.h>
45
#include <Quicktime/Quicktime.h>//YV
46
47
#include <unistd.h>
48
#include <cstdio>
49
#include <cmath>
50
51
//#define MS_TO_TICKS(a) a*3/50
52
53
#define LABELWIDTH 64
54
#define INTERWIDGETSPACE 16
55
#define WIDGETHEIGHT 32
56
#define NO_KEY -1
57
58
struct CvWindow;
59
60
typedef struct CvTrackbar
61
{
62
int signature;
63
64
ControlRef trackbar;
65
ControlRef label;
66
67
char* name;
68
CvTrackbar* next;
69
CvWindow* parent;
70
int* data;
71
int pos;
72
int maxval;
73
int labelSize;//Yannick Verdie
74
CvTrackbarCallback notify;
75
CvTrackbarCallback2 notify2;
76
void* userdata;
77
}
78
CvTrackbar;
79
80
81
typedef struct CvWindow
82
{
83
int signature;
84
85
char* name;
86
CvWindow* prev;
87
CvWindow* next;
88
89
WindowRef window;
90
WindowRef oldwindow;//YV
91
CGImageRef imageRef;
92
int imageWidth;//FD
93
int imageHeight;//FD
94
95
CvMat* image;
96
CvMat* dst_image;
97
int converted;
98
int last_key;
99
int flags;
100
int status;//YV
101
Ptr restoreState;//YV
102
103
CvMouseCallback on_mouse;
104
void* on_mouse_param;
105
106
struct {
107
int pos;
108
int rows;
109
CvTrackbar* first;
110
}
111
toolbar;
112
int trackbarheight;
113
}
114
CvWindow;
115
116
static CvWindow* hg_windows = 0;
117
118
#define Assert(exp) \
119
if( !(exp) ) \
120
{ \
121
printf("Assertion: %s %s: %d\n", #exp, __FILE__, __LINE__);\
122
assert(exp); \
123
}
124
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);
129
130
static const EventTypeSpec applicationKeyboardEvents[] =
131
{
132
{ kEventClassKeyboard, kEventRawKeyDown },
133
};
134
135
CV_IMPL int cvInitSystem( int argc, char** argv )
136
{
137
OSErr err = noErr;
138
if( !wasInitialized )
139
{
140
141
hg_windows = 0;
142
err = InstallApplicationEventHandler(NewEventHandlerUPP( keyHandler),GetEventTypeCount(applicationKeyboardEvents),applicationKeyboardEvents,NULL,NULL);
143
if (err != noErr)
144
{
145
fprintf(stderr,"InstallApplicationEventHandler was not ok\n");
146
}
147
wasInitialized = 1;
148
}
149
setlocale(LC_NUMERIC,"C");
150
151
return 0;
152
}
153
154
// TODO: implement missing functionality
155
CV_IMPL int cvStartWindowThread()
156
{
157
return 0;
158
}
159
160
static int icvCountTrackbarInWindow( const CvWindow* window)
161
{
162
CvTrackbar* trackbar = window->toolbar.first;
163
int count = 0;
164
while (trackbar != 0) {
165
count++;
166
trackbar = trackbar->next;
167
}
168
return count;
169
}
170
171
static CvTrackbar* icvTrackbarByHandle( void * handle )
172
{
173
CvWindow* window = hg_windows;
174
CvTrackbar* trackbar = NULL;
175
while( window != 0 && window->window != handle) {
176
trackbar = window->toolbar.first;
177
while (trackbar != 0 && trackbar->trackbar != handle)
178
trackbar = trackbar->next;
179
if (trackbar != 0 && trackbar->trackbar == handle)
180
break;
181
window = window->next;
182
}
183
return trackbar;
184
}
185
186
static CvWindow* icvWindowByHandle( void * handle )
187
{
188
CvWindow* window = hg_windows;
189
190
while( window != 0 && window->window != handle)
191
window = window->next;
192
193
return window;
194
}
195
196
CV_IMPL CvWindow * icvFindWindowByName( const char* name)
197
{
198
CvWindow* window = hg_windows;
199
while( window != 0 && strcmp(name, window->name) != 0 )
200
window = window->next;
201
202
return window;
203
}
204
205
static CvTrackbar* icvFindTrackbarByName( const CvWindow* window, const char* name )
206
{
207
CvTrackbar* trackbar = window->toolbar.first;
208
209
while (trackbar != 0 && strcmp( trackbar->name, name ) != 0)
210
trackbar = trackbar->next;
211
212
return trackbar;
213
}
214
215
//FD
216
/* draw image to frame */
217
static void icvDrawImage( CvWindow* window )
218
{
219
Assert( window != 0 );
220
if( window->imageRef == 0 ) return;
221
222
CGContextRef myContext;
223
CGRect rect;
224
Rect portrect;
225
int width = window->imageWidth;
226
int height = window->imageHeight;
227
228
GetWindowPortBounds(window->window, &portrect);
229
230
if(!( window->flags & CV_WINDOW_AUTOSIZE) ) //YV
231
{
232
CGPoint origin = {0,0};
233
CGSize size = {portrect.right-portrect.left, portrect.bottom-portrect.top-window->trackbarheight};
234
rect.origin = origin; rect.size = size;
235
}
236
else
237
{
238
CGPoint origin = {0, portrect.bottom - height - window->trackbarheight};
239
CGSize size = {width, height};
240
rect.origin = origin; rect.size = size;
241
}
242
243
/* 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*/
244
SetPortWindowPort (window->window);
245
QDBeginCGContext (GetWindowPort (window->window), &myContext);
246
CGContextSetInterpolationQuality (myContext, kCGInterpolationLow);
247
CGContextDrawImage(myContext,rect,window->imageRef);
248
CGContextFlush(myContext);// 4
249
QDEndCGContext (GetWindowPort(window->window), &myContext);// 5
250
}
251
252
//FD
253
/* update imageRef */
254
static void icvPutImage( CvWindow* window )
255
{
256
Assert( window != 0 );
257
if( window->image == 0 ) return;
258
259
CGColorSpaceRef colorspace = NULL;
260
CGDataProviderRef provider = NULL;
261
int width = window->imageWidth = window->image->cols;
262
int height = window->imageHeight = window->image->rows;
263
264
colorspace = CGColorSpaceCreateDeviceRGB();
265
266
int size = 8;
267
int nbChannels = 3;
268
269
provider = CGDataProviderCreateWithData(NULL, window->image->data.ptr, width * height , NULL );
270
271
if (window->imageRef != NULL){
272
CGImageRelease(window->imageRef);
273
window->imageRef = NULL;
274
}
275
276
window->imageRef = CGImageCreate( width, height, size , size*nbChannels , window->image->step, colorspace, kCGImageAlphaNone , provider, NULL, true, kCGRenderingIntentDefault );
277
icvDrawImage( window );
278
279
/* release the provider's memory */
280
CGDataProviderRelease( provider );
281
}
282
283
static void icvUpdateWindowSize( const CvWindow* window )
284
{
285
int width = 0, height = 240;
286
Rect globalBounds;
287
288
GetWindowBounds(window->window, kWindowContentRgn, &globalBounds);
289
290
int minWidth = 320;
291
292
if( window->image ) {
293
width = MAX(MAX(window->image->width, width), minWidth);
294
height = window->image->height;
295
} else
296
width = minWidth;
297
298
height += window->trackbarheight;
299
300
//height +=WIDGETHEIGHT; /* 32 pixels are spearating tracbars from the video display */
301
302
globalBounds.right = globalBounds.left + width;
303
globalBounds.bottom = globalBounds.top + height;
304
SetWindowBounds(window->window, kWindowContentRgn, &globalBounds);
305
}
306
307
static void icvDeleteWindow( CvWindow* window )
308
{
309
CvTrackbar* trackbar;
310
311
if( window->prev )
312
window->prev->next = window->next;
313
else
314
hg_windows = window->next;
315
316
if( window->next )
317
window->next->prev = window->prev;
318
319
window->prev = window->next = 0;
320
321
cvReleaseMat( &window->image );
322
cvReleaseMat( &window->dst_image );
323
324
for( trackbar = window->toolbar.first; trackbar != 0; )
325
{
326
CvTrackbar* next = trackbar->next;
327
cvFree( (void**)&trackbar );
328
trackbar = next;
329
}
330
331
if (window->imageRef != NULL)
332
CGImageRelease(window->imageRef);
333
334
DisposeWindow (window->window);//YV
335
336
cvFree( (void**)&window );
337
}
338
339
340
CV_IMPL void cvDestroyWindow( const char* name)
341
{
342
CV_FUNCNAME( "cvDestroyWindow" );
343
344
__BEGIN__;
345
346
CvWindow* window;
347
348
if(!name)
349
CV_ERROR( CV_StsNullPtr, "NULL name string" );
350
351
window = icvFindWindowByName( name );
352
if( !window )
353
EXIT;
354
355
icvDeleteWindow( window );
356
357
__END__;
358
}
359
360
361
CV_IMPL void cvDestroyAllWindows( void )
362
{
363
while( hg_windows )
364
{
365
CvWindow* window = hg_windows;
366
icvDeleteWindow( window );
367
}
368
}
369
370
371
CV_IMPL void cvShowImage( const char* name, const CvArr* arr)
372
{
373
CV_FUNCNAME( "cvShowImage" );
374
375
__BEGIN__;
376
377
CvWindow* window;
378
int origin = 0;
379
int resize = 0;
380
CvMat stub, *image;
381
382
if( !name )
383
CV_ERROR( CV_StsNullPtr, "NULL name" );
384
385
window = icvFindWindowByName(name);
386
if(!window)
387
{
388
cvNamedWindow(name, 1);
389
window = icvFindWindowByName(name);
390
}
391
392
if( !window || !arr )
393
EXIT; // keep silence here.
394
395
if( CV_IS_IMAGE_HDR( arr ))
396
origin = ((IplImage*)arr)->origin;
397
398
CV_CALL( image = cvGetMat( arr, &stub ));
399
400
/*
401
if( !window->image )
402
cvResizeWindow( name, image->cols, image->rows );
403
*/
404
405
if( window->image &&
406
!CV_ARE_SIZES_EQ(window->image, image) ) {
407
if ( ! (window->flags & CV_WINDOW_AUTOSIZE) )//FD
408
resize = 1;
409
cvReleaseMat( &window->image );
410
}
411
412
if( !window->image ) {
413
resize = 1;//FD
414
window->image = cvCreateMat( image->rows, image->cols, CV_8UC3 );
415
}
416
417
cvConvertImage( image, window->image, (origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB );
418
icvPutImage( window );
419
if ( resize )//FD
420
icvUpdateWindowSize( window );
421
422
__END__;
423
}
424
425
CV_IMPL void cvResizeWindow( const char* name, int width, int height)
426
{
427
CV_FUNCNAME( "cvResizeWindow" );
428
429
__BEGIN__;
430
431
CvWindow* window;
432
//CvTrackbar* trackbar;
433
434
if( !name )
435
CV_ERROR( CV_StsNullPtr, "NULL name" );
436
437
window = icvFindWindowByName(name);
438
if(!window)
439
EXIT;
440
441
SizeWindow(window->window, width, height, true);
442
443
__END__;
444
}
445
446
CV_IMPL void cvMoveWindow( const char* name, int x, int y)
447
{
448
CV_FUNCNAME( "cvMoveWindow" );
449
450
__BEGIN__;
451
452
CvWindow* window;
453
//CvTrackbar* trackbar;
454
455
if( !name )
456
CV_ERROR( CV_StsNullPtr, "NULL name" );
457
458
window = icvFindWindowByName(name);
459
if(!window)
460
EXIT;
461
462
MoveWindow(window->window, x, y, true);
463
464
__END__;
465
}
466
467
void TrackbarActionProcPtr (ControlRef theControl, ControlPartCode partCode)
468
{
469
CvTrackbar * trackbar = icvTrackbarByHandle (theControl);
470
471
if (trackbar == NULL)
472
{
473
fprintf(stderr,"Error getting trackbar\n");
474
return;
475
}
476
else
477
{
478
int pos = GetControl32BitValue (theControl);
479
if ( trackbar->data )
480
*trackbar->data = pos;
481
if ( trackbar->notify )
482
trackbar->notify(pos);
483
else if ( trackbar->notify2 )
484
trackbar->notify2(pos, trackbar->userdata);
485
486
//--------YV---------------------------
487
CFStringEncoding encoding = kCFStringEncodingASCII;
488
CFAllocatorRef alloc_default = kCFAllocatorDefault; // = NULL;
489
490
char valueinchar[20];
491
sprintf(valueinchar, " (%d)", *trackbar->data);
492
493
// create an empty CFMutableString
494
CFIndex maxLength = 256;
495
CFMutableStringRef cfstring = CFStringCreateMutable(alloc_default,maxLength);
496
497
// append some c strings into it.
498
CFStringAppendCString(cfstring,trackbar->name,encoding);
499
CFStringAppendCString(cfstring,valueinchar,encoding);
500
501
SetControlData(trackbar->label, kControlEntireControl,kControlStaticTextCFStringTag, sizeof(cfstring), &cfstring);
502
DrawControls(trackbar->parent->window);
503
//-----------------------------------------
504
}
505
}
506
507
508
static int icvCreateTrackbar (const char* trackbar_name,
509
const char* window_name,
510
int* val, int count,
511
CvTrackbarCallback on_notify,
512
CvTrackbarCallback2 on_notify2,
513
void* userdata)
514
{
515
int result = 0;
516
517
CV_FUNCNAME( "icvCreateTrackbar" );
518
__BEGIN__;
519
520
/*char slider_name[32];*/
521
CvWindow* window = 0;
522
CvTrackbar* trackbar = 0;
523
Rect stboundsRect;
524
ControlRef outControl;
525
ControlRef stoutControl;
526
Rect bounds;
527
528
if( !window_name || !trackbar_name )
529
CV_ERROR( CV_StsNullPtr, "NULL window or trackbar name" );
530
531
if( count <= 0 )
532
CV_ERROR( CV_StsOutOfRange, "Bad trackbar maximal value" );
533
534
window = icvFindWindowByName(window_name);
535
if( !window )
536
EXIT;
537
538
trackbar = icvFindTrackbarByName(window,trackbar_name);
539
if( !trackbar )
540
{
541
int len = strlen(trackbar_name);
542
trackbar = (CvTrackbar*)cvAlloc(sizeof(CvTrackbar) + len + 1);
543
memset( trackbar, 0, sizeof(*trackbar));
544
trackbar->signature = CV_TRACKBAR_MAGIC_VAL;
545
trackbar->name = (char*)(trackbar+1);
546
memcpy( trackbar->name, trackbar_name, len + 1 );
547
trackbar->parent = window;
548
trackbar->next = window->toolbar.first;
549
window->toolbar.first = trackbar;
550
551
if( val )
552
{
553
int value = *val;
554
if( value < 0 )
555
value = 0;
556
if( value > count )
557
value = count;
558
trackbar->pos = value;
559
trackbar->data = val;
560
}
561
562
trackbar->maxval = count;
563
564
//----------- YV ----------------------
565
//get nb of digits
566
int nbDigit = 0;
567
while((count/=10)>10){
568
nbDigit++;
569
}
570
571
//pad size maxvalue in pixel
572
Point qdSize;
573
char valueinchar[strlen(trackbar_name)+1 +1 +1+nbDigit+1];//length+\n +space +(+nbDigit+)
574
sprintf(valueinchar, "%s (%d)",trackbar_name, trackbar->maxval);
575
SInt16 baseline;
576
CFStringRef text = CFStringCreateWithCString(NULL,valueinchar,kCFStringEncodingASCII);
577
GetThemeTextDimensions( text, kThemeCurrentPortFont, kThemeStateActive, false, &qdSize, &baseline );
578
trackbar->labelSize = qdSize.h;
579
//--------------------------------------
580
581
int c = icvCountTrackbarInWindow(window);
582
583
GetWindowBounds(window->window,kWindowContentRgn,&bounds);
584
585
stboundsRect.top = (INTERWIDGETSPACE +WIDGETHEIGHT)* (c-1)+INTERWIDGETSPACE;
586
stboundsRect.left = INTERWIDGETSPACE;
587
stboundsRect.bottom = stboundsRect.top + WIDGETHEIGHT;
588
stboundsRect.right = stboundsRect.left+LABELWIDTH;
589
590
//fprintf(stdout,"create trackabar bounds (%d %d %d %d)\n",stboundsRect.top,stboundsRect.left,stboundsRect.bottom,stboundsRect.right);
591
//----------- YV ----------------------
592
sprintf(valueinchar, "%s (%d)",trackbar_name, trackbar->pos);
593
CreateStaticTextControl (window->window,&stboundsRect,CFStringCreateWithCString(NULL,valueinchar,kCFStringEncodingASCII),NULL,&stoutControl);
594
//--------------------------------------
595
596
stboundsRect.top = (INTERWIDGETSPACE +WIDGETHEIGHT)* (c-1)+INTERWIDGETSPACE;
597
stboundsRect.left = INTERWIDGETSPACE*2+LABELWIDTH;
598
stboundsRect.bottom = stboundsRect.top + WIDGETHEIGHT;
599
stboundsRect.right = bounds.right-INTERWIDGETSPACE;
600
601
CreateSliderControl (window->window,&stboundsRect, trackbar->pos,0,trackbar->maxval,kControlSliderLiveFeedback,0,true,NewControlActionUPP(TrackbarActionProcPtr),&outControl);
602
603
bounds.bottom += INTERWIDGETSPACE + WIDGETHEIGHT;
604
SetControlVisibility (outControl,true,true);
605
SetControlVisibility (stoutControl,true,true);
606
607
trackbar->trackbar = outControl;
608
trackbar->label = stoutControl;
609
if (c == 1)
610
window->trackbarheight = INTERWIDGETSPACE*2 + WIDGETHEIGHT;
611
else
612
window->trackbarheight += INTERWIDGETSPACE + WIDGETHEIGHT;
613
icvUpdateWindowSize( window );
614
}
615
616
trackbar->notify = on_notify;
617
trackbar->notify2 = on_notify2;
618
trackbar->userdata = userdata;
619
620
result = 1;
621
622
__END__;
623
return result;
624
}
625
626
627
CV_IMPL int cvCreateTrackbar (const char* trackbar_name,
628
const char* window_name,
629
int* val, int count,
630
CvTrackbarCallback on_notify)
631
{
632
return icvCreateTrackbar(trackbar_name, window_name, val, count, on_notify, 0, 0);
633
}
634
635
636
CV_IMPL int cvCreateTrackbar2(const char* trackbar_name,
637
const char* window_name,
638
int* val, int count,
639
CvTrackbarCallback2 on_notify2,
640
void* userdata)
641
{
642
return icvCreateTrackbar(trackbar_name, window_name, val,
643
count, 0, on_notify2, userdata);
644
}
645
646
647
CV_IMPL void
648
cvSetMouseCallback( const char* name, CvMouseCallback function, void* info)
649
{
650
CvWindow* window = icvFindWindowByName( name );
651
if (window != NULL)
652
{
653
window->on_mouse = function;
654
window->on_mouse_param = info;
655
}
656
else
657
{
658
fprintf(stdout,"Error with cvSetMouseCallback. Window not found : %s\n",name);
659
}
660
}
661
662
CV_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name )
663
{
664
int pos = -1;
665
666
CV_FUNCNAME( "cvGetTrackbarPos" );
667
668
__BEGIN__;
669
670
CvWindow* window;
671
CvTrackbar* trackbar = 0;
672
673
if( trackbar_name == 0 || window_name == 0 )
674
CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
675
676
window = icvFindWindowByName( window_name );
677
if( window )
678
trackbar = icvFindTrackbarByName( window, trackbar_name );
679
680
if( trackbar )
681
pos = trackbar->pos;
682
683
__END__;
684
685
return pos;
686
}
687
688
CV_IMPL void cvSetTrackbarPos(const char* trackbar_name, const char* window_name, int pos)
689
{
690
CV_FUNCNAME( "cvSetTrackbarPos" );
691
692
__BEGIN__;
693
694
CvWindow* window;
695
CvTrackbar* trackbar = 0;
696
697
if( trackbar_name == 0 || window_name == 0 )
698
CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
699
700
window = icvFindWindowByName( window_name );
701
if( window )
702
trackbar = icvFindTrackbarByName( window, trackbar_name );
703
704
if( trackbar )
705
{
706
if( pos < 0 )
707
pos = 0;
708
709
if( pos > trackbar->maxval )
710
pos = trackbar->maxval;
711
712
// Set new value and redraw the trackbar
713
SetControlValue( trackbar->trackbar, pos );
714
Draw1Control( trackbar->trackbar );
715
}
716
717
__END__;
718
return ;
719
}
720
721
CvRect cvGetWindowRect_CARBON(const char* name)
722
{
723
CvRect result = cvRect(-1, -1, -1, -1);
724
725
CV_FUNCNAME( "cvGetWindowRect_QT" );
726
727
__BEGIN__;
728
729
CvWindow* window;
730
731
if(!name)
732
CV_ERROR( CV_StsNullPtr, "NULL name string" );
733
734
window = icvFindWindowByName( name );
735
if( !window )
736
CV_ERROR( CV_StsNullPtr, "NULL window" );
737
738
739
Rect portrect;
740
GetWindowPortBounds(window->window, &portrect);
741
LocalToGlobal(&topLeft(portrect));
742
LocalToGlobal(&botRight(portrect));
743
if(!( window->flags & CV_WINDOW_AUTOSIZE) )
744
{
745
result = cvRect(portrect.left, portrect.top, portrect.right-portrect.left,
746
portrect.bottom-portrect.top-window->trackbarheight);
747
}
748
else
749
{
750
result = cvRect(portrect.left, portrect.bottom - height - window->trackbarheight,
751
window->imageWidth, window->imageHeight);
752
}
753
754
__END__;
755
return result;
756
}
757
758
CV_IMPL void* cvGetWindowHandle( const char* name )
759
{
760
WindowRef result = 0;
761
762
__BEGIN__;
763
764
CvWindow* window;
765
window = icvFindWindowByName( name );
766
if (window != NULL)
767
result = window->window;
768
else
769
result = NULL;
770
771
__END__;
772
773
return result;
774
}
775
776
777
CV_IMPL const char* cvGetWindowName( void* window_handle )
778
{
779
const char* window_name = "";
780
781
CV_FUNCNAME( "cvGetWindowName" );
782
783
__BEGIN__;
784
785
CvWindow* window;
786
787
if( window_handle == 0 )
788
CV_ERROR( CV_StsNullPtr, "NULL window" );
789
window = icvWindowByHandle(window_handle );
790
if( window )
791
window_name = window->name;
792
793
__END__;
794
795
return window_name;
796
}
797
798
double cvGetModeWindow_CARBON(const char* name)//YV
799
{
800
double result = -1;
801
802
CV_FUNCNAME( "cvGetModeWindow_QT" );
803
804
__BEGIN__;
805
806
CvWindow* window;
807
808
if(!name)
809
CV_ERROR( CV_StsNullPtr, "NULL name string" );
810
811
window = icvFindWindowByName( name );
812
if( !window )
813
CV_ERROR( CV_StsNullPtr, "NULL window" );
814
815
result = window->status;
816
817
__END__;
818
return result;
819
}
820
821
void cvSetModeWindow_CARBON( const char* name, double prop_value)//Yannick Verdie
822
{
823
OSStatus err = noErr;
824
825
826
CV_FUNCNAME( "cvSetModeWindow_QT" );
827
828
__BEGIN__;
829
830
CvWindow* window;
831
832
if(!name)
833
CV_ERROR( CV_StsNullPtr, "NULL name string" );
834
835
window = icvFindWindowByName( name );
836
if( !window )
837
CV_ERROR( CV_StsNullPtr, "NULL window" );
838
839
if(window->flags & CV_WINDOW_AUTOSIZE)//if the flag CV_WINDOW_AUTOSIZE is set
840
EXIT;
841
842
if (window->status==CV_WINDOW_FULLSCREEN && prop_value==CV_WINDOW_NORMAL)
843
{
844
err = EndFullScreen(window->restoreState,0);
845
if (err != noErr)
846
fprintf(stdout,"Error EndFullScreen\n");
847
window->window = window->oldwindow;
848
ShowWindow( window->window );
849
850
window->status=CV_WINDOW_NORMAL;
851
EXIT;
852
}
853
854
if (window->status==CV_WINDOW_NORMAL && prop_value==CV_WINDOW_FULLSCREEN)
855
{
856
GDHandle device;
857
err = GetWindowGreatestAreaDevice(window->window, kWindowTitleBarRgn, &device, NULL);
858
if (err != noErr)
859
fprintf(stdout,"Error GetWindowGreatestAreaDevice\n");
860
861
HideWindow(window->window);
862
window->oldwindow = window->window;
863
err = BeginFullScreen(&(window->restoreState), device, 0, 0, &window->window, 0, fullScreenAllowEvents | fullScreenDontSwitchMonitorResolution);
864
if (err != noErr)
865
fprintf(stdout,"Error BeginFullScreen\n");
866
867
window->status=CV_WINDOW_FULLSCREEN;
868
EXIT;
869
}
870
871
__END__;
872
}
873
874
void cv::setWindowTitle(const String& winname, const String& title)
875
{
876
CvWindow* window = icvFindWindowByName(winname.c_str());
877
878
if (!window)
879
{
880
namedWindow(winname);
881
window = icvFindWindowByName(winname.c_str());
882
}
883
884
if (!window)
885
CV_Error(Error::StsNullPtr, "NULL window");
886
887
if (noErr != SetWindowTitleWithCFString(window->window, CFStringCreateWithCString(NULL, title.c_str(), kCFStringEncodingASCII)))
888
CV_Error_(Error::StsError, ("Failed to set \"%s\" window title to \"%s\"", winname.c_str(), title.c_str()));
889
}
890
891
CV_IMPL int cvNamedWindow( const char* name, int flags )
892
{
893
int result = 0;
894
CV_FUNCNAME( "cvNamedWindow" );
895
if (!wasInitialized)
896
cvInitSystem(0, NULL);
897
898
// to be able to display a window, we need to be a 'faceful' application
899
// http://lists.apple.com/archives/carbon-dev/2005/Jun/msg01414.html
900
static bool switched_to_faceful = false;
901
if (! switched_to_faceful)
902
{
903
ProcessSerialNumber psn = { 0, kCurrentProcess };
904
OSStatus ret = TransformProcessType (&psn, kProcessTransformToForegroundApplication );
905
906
if (ret == noErr)
907
{
908
SetFrontProcess( &psn );
909
switched_to_faceful = true;
910
}
911
else
912
{
913
fprintf(stderr, "Failed to transform process type: %d\n", (int) ret);
914
fflush (stderr);
915
}
916
}
917
918
__BEGIN__;
919
920
WindowRef outWindow = NULL;
921
OSStatus err = noErr;
922
Rect contentBounds = {100,100,320,440};
923
924
CvWindow* window;
925
UInt wAttributes = 0;
926
927
int len;
928
929
const EventTypeSpec genericWindowEventHandler[] = {
930
{ kEventClassMouse, kEventMouseMoved},
931
{ kEventClassMouse, kEventMouseDragged},
932
{ kEventClassMouse, kEventMouseUp},
933
{ kEventClassMouse, kEventMouseDown},
934
{ kEventClassWindow, kEventWindowClose },
935
{ kEventClassWindow, kEventWindowBoundsChanged }//FD
936
};
937
938
if( !name )
939
CV_ERROR( CV_StsNullPtr, "NULL name string" );
940
941
if( icvFindWindowByName( name ) != 0 ){
942
result = 1;
943
EXIT;
944
}
945
len = strlen(name);
946
CV_CALL( window = (CvWindow*)cvAlloc(sizeof(CvWindow) + len + 1));
947
memset( window, 0, sizeof(*window));
948
window->name = (char*)(window + 1);
949
memcpy( window->name, name, len + 1 );
950
window->flags = flags;
951
window->status = CV_WINDOW_NORMAL;//YV
952
window->signature = CV_WINDOW_MAGIC_VAL;
953
window->image = 0;
954
window->last_key = 0;
955
window->on_mouse = 0;
956
window->on_mouse_param = 0;
957
958
window->next = hg_windows;
959
window->prev = 0;
960
if( hg_windows )
961
hg_windows->prev = window;
962
hg_windows = window;
963
wAttributes = kWindowStandardDocumentAttributes | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute;
964
965
966
if (window->flags & CV_WINDOW_AUTOSIZE)//Yannick verdie, remove the handler at the bottom-right position of the window in AUTORESIZE mode
967
{
968
wAttributes = 0;
969
wAttributes = kWindowCloseBoxAttribute | kWindowFullZoomAttribute | kWindowCollapseBoxAttribute | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute;
970
}
971
972
err = CreateNewWindow ( kDocumentWindowClass,wAttributes,&contentBounds,&outWindow);
973
if (err != noErr)
974
fprintf(stderr,"Error while creating the window\n");
975
976
SetWindowTitleWithCFString(outWindow,CFStringCreateWithCString(NULL,name,kCFStringEncodingASCII));
977
if (err != noErr)
978
fprintf(stdout,"Error SetWindowTitleWithCFString\n");
979
980
window->window = outWindow;
981
window->oldwindow = 0;//YV
982
983
err = InstallWindowEventHandler(outWindow, NewEventHandlerUPP(windowEventHandler), GetEventTypeCount(genericWindowEventHandler), genericWindowEventHandler, outWindow, NULL);
984
985
ShowWindow( outWindow );
986
result = 1;
987
988
__END__;
989
return result;
990
}
991
992
static pascal OSStatus windowEventHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *inUserData)
993
{
994
CvWindow* window = NULL;
995
UInt32 eventKind, eventClass;
996
OSErr err = noErr;
997
int event = 0;
998
UInt32 count = 0;
999
HIPoint point = {0,0};
1000
EventMouseButton eventMouseButton = 0;//FD
1001
UInt32 modifiers;//FD
1002
1003
WindowRef theWindow = (WindowRef)inUserData;
1004
if (theWindow == NULL)
1005
return eventNotHandledErr;
1006
window = icvWindowByHandle(theWindow);
1007
if ( window == NULL)
1008
return eventNotHandledErr;
1009
1010
eventKind = GetEventKind(theEvent);
1011
eventClass = GetEventClass(theEvent);
1012
1013
switch (eventClass) {
1014
case kEventClassMouse : {
1015
switch (eventKind){
1016
case kEventMouseUp :
1017
case kEventMouseDown :
1018
case kEventMouseMoved :
1019
case kEventMouseDragged : {
1020
err = CallNextEventHandler(nextHandler, theEvent);
1021
if (err != eventNotHandledErr)
1022
return err;
1023
err = GetEventParameter(theEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof(eventMouseButton), NULL, &eventMouseButton);
1024
err = GetEventParameter(theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(modifiers), NULL, &modifiers);
1025
err = GetEventParameter(theEvent,kEventParamClickCount,typeUInt32,NULL,sizeof(UInt32),NULL,&count);
1026
if (err == noErr){
1027
if (count >1) event += 6;
1028
} else {
1029
event = CV_EVENT_MOUSEMOVE;
1030
}
1031
1032
if (eventKind == kEventMouseUp)
1033
event +=4;
1034
if (eventKind == kEventMouseDown)
1035
event +=1;
1036
1037
unsigned int flags = 0;
1038
1039
err = GetEventParameter(theEvent, kEventParamWindowMouseLocation, typeHIPoint, NULL, sizeof(point), NULL, &point);
1040
if (eventKind != kEventMouseMoved){
1041
switch(eventMouseButton){
1042
case kEventMouseButtonPrimary:
1043
if (modifiers & controlKey){
1044
flags += CV_EVENT_FLAG_RBUTTON;
1045
event += 1;
1046
} else {
1047
flags += CV_EVENT_FLAG_LBUTTON;
1048
}
1049
break;
1050
case kEventMouseButtonSecondary:
1051
flags += CV_EVENT_FLAG_RBUTTON;
1052
event += 1;
1053
break;
1054
case kEventMouseButtonTertiary:
1055
flags += CV_EVENT_FLAG_MBUTTON;
1056
event += 2;
1057
break;
1058
}
1059
}
1060
1061
if (modifiers&controlKey) flags += CV_EVENT_FLAG_CTRLKEY;
1062
if (modifiers&shiftKey) flags += CV_EVENT_FLAG_SHIFTKEY;
1063
if (modifiers& cmdKey ) flags += CV_EVENT_FLAG_ALTKEY;
1064
1065
if (window->on_mouse != NULL){
1066
int lx,ly;
1067
Rect structure, content;
1068
GetWindowBounds(theWindow, kWindowStructureRgn, &structure);
1069
GetWindowBounds(theWindow, kWindowContentRgn, &content);
1070
lx = (int)point.x - content.left + structure.left;
1071
ly = (int)point.y - window->trackbarheight - content.top + structure.top;
1072
if (window->flags & CV_WINDOW_AUTOSIZE) {//FD
1073
//printf("was %d,%d\n", lx, ly);
1074
/* scale the mouse coordinates */
1075
lx = lx * window->imageWidth / (content.right - content.left);
1076
ly = ly * window->imageHeight / (content.bottom - content.top - window->trackbarheight);
1077
}
1078
1079
if (lx>0 && ly >0){
1080
window->on_mouse (event, lx, ly, flags, window->on_mouse_param);
1081
return noErr;
1082
}
1083
}
1084
}
1085
default : return eventNotHandledErr;
1086
}
1087
}
1088
case kEventClassWindow : {//FD
1089
switch (eventKind){
1090
case kEventWindowBoundsChanged :
1091
{
1092
/* resize the trackbars */
1093
CvTrackbar *t;
1094
Rect bounds;
1095
GetWindowBounds(window->window,kWindowContentRgn,&bounds);
1096
for ( t = window->toolbar.first; t != 0; t = t->next )
1097
SizeControl(t->trackbar,bounds.right - bounds.left - INTERWIDGETSPACE*3 - LABELWIDTH , WIDGETHEIGHT);
1098
}
1099
/* redraw the image */
1100
icvDrawImage(window);
1101
break;
1102
default :
1103
return eventNotHandledErr;
1104
}
1105
}
1106
default:
1107
return eventNotHandledErr;
1108
}
1109
1110
return eventNotHandledErr;
1111
}
1112
1113
OSStatus keyHandler(EventHandlerCallRef hcr, EventRef theEvent, void* inUserData)
1114
{
1115
UInt32 eventKind;
1116
UInt32 eventClass;
1117
OSErr err = noErr;
1118
1119
eventKind = GetEventKind (theEvent);
1120
eventClass = GetEventClass (theEvent);
1121
err = GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(lastKey), NULL, &lastKey);
1122
if (err != noErr)
1123
lastKey = NO_KEY;
1124
1125
return noErr;
1126
}
1127
1128
CV_IMPL int cvWaitKey (int maxWait)
1129
{
1130
EventRecord theEvent;
1131
1132
// wait at least for one event (to allow mouse, etc. processing), exit if maxWait milliseconds passed (nullEvent)
1133
UInt32 start = TickCount();
1134
int iters=0;
1135
do
1136
{
1137
// remaining time until maxWait is over
1138
UInt32 wait = EventTimeToTicks (maxWait / 1000.0) - (TickCount() - start);
1139
if ((int)wait <= 0)
1140
{
1141
if( maxWait > 0 && iters > 0 )
1142
break;
1143
wait = 1;
1144
}
1145
iters++;
1146
WaitNextEvent (everyEvent, &theEvent, maxWait > 0 ? wait : kDurationForever, NULL);
1147
}
1148
while (lastKey == NO_KEY && theEvent.what != nullEvent);
1149
1150
int key = lastKey;
1151
lastKey = NO_KEY;
1152
return key;
1153
}
1154
1155
/* End of file. */
1156
1157