/
window_w32.cpp
2170 lines (1690 loc) · 59.5 KB
/
window_w32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#if defined WIN32 || defined _WIN32
#define COMPILE_MULTIMON_STUBS // Required for multi-monitor support
#ifndef _MULTIMON_USE_SECURE_CRT
# define _MULTIMON_USE_SECURE_CRT 0 // some MinGW platforms have no strncpy_s
#endif
#if defined SM_CMONITORS && !defined MONITOR_DEFAULTTONEAREST
# define MONITOR_DEFAULTTONULL 0x00000000
# define MONITOR_DEFAULTTOPRIMARY 0x00000001
# define MONITOR_DEFAULTTONEAREST 0x00000002
# define MONITORINFOF_PRIMARY 0x00000001
#endif
#ifndef __inout
# define __inout
#endif
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wmissing-declarations"
#endif
#include <MultiMon.h>
#include <commctrl.h>
#include <winuser.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#ifdef HAVE_OPENGL
#include <memory>
#include <algorithm>
#include <vector>
#include <functional>
#include "opencv2/highgui/highgui.hpp"
#include <GL\gl.h>
#endif
static const char* trackbar_text =
" ";
#if defined _M_X64 || defined __x86_64
#define icvGetWindowLongPtr GetWindowLongPtr
#define icvSetWindowLongPtr( hwnd, id, ptr ) SetWindowLongPtr( hwnd, id, (LONG_PTR)(ptr) )
#define icvGetClassLongPtr GetClassLongPtr
#define CV_USERDATA GWLP_USERDATA
#define CV_WNDPROC GWLP_WNDPROC
#define CV_HCURSOR GCLP_HCURSOR
#define CV_HBRBACKGROUND GCLP_HBRBACKGROUND
#else
#define icvGetWindowLongPtr GetWindowLong
#define icvSetWindowLongPtr( hwnd, id, ptr ) SetWindowLong( hwnd, id, (size_t)ptr )
#define icvGetClassLongPtr GetClassLong
#define CV_USERDATA GWL_USERDATA
#define CV_WNDPROC GWL_WNDPROC
#define CV_HCURSOR GCL_HCURSOR
#define CV_HBRBACKGROUND GCL_HBRBACKGROUND
#endif
static void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )
{
assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
memset( bmih, 0, sizeof(*bmih));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = width;
bmih->biHeight = origin ? abs(height) : -abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = (unsigned short)bpp;
bmih->biCompression = BI_RGB;
if( bpp == 8 )
{
RGBQUAD* palette = bmi->bmiColors;
int i;
for( i = 0; i < 256; i++ )
{
palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
palette[i].rgbReserved = 0;
}
}
}
struct CvWindow;
typedef struct CvTrackbar
{
int signature;
HWND hwnd;
char* name;
CvTrackbar* next;
CvWindow* parent;
HWND buddy;
int* data;
int pos;
int maxval;
void (*notify)(int);
void (*notify2)(int, void*);
void* userdata;
int id;
}
CvTrackbar;
typedef struct CvWindow
{
int signature;
HWND hwnd;
char* name;
CvWindow* prev;
CvWindow* next;
HWND frame;
HDC dc;
HGDIOBJ image;
int last_key;
int flags;
int status;//0 normal, 1 fullscreen (YV)
CvMouseCallback on_mouse;
void* on_mouse_param;
struct
{
HWND toolbar;
int pos;
int rows;
WNDPROC toolBarProc;
CvTrackbar* first;
}
toolbar;
int width;
int height;
// OpenGL support
#ifdef HAVE_OPENGL
bool useGl;
HGLRC hGLRC;
CvOpenGlDrawCallback glDrawCallback;
void* glDrawData;
#endif
}
CvWindow;
#define HG_BUDDY_WIDTH 130
#ifndef TBIF_SIZE
#define TBIF_SIZE 0x40
#endif
#ifndef TB_SETBUTTONINFO
#define TB_SETBUTTONINFO (WM_USER + 66)
#endif
#ifndef TBM_GETTOOLTIPS
#define TBM_GETTOOLTIPS (WM_USER + 30)
#endif
static LRESULT CALLBACK HighGUIProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static void icvUpdateWindowPos( CvWindow* window );
static CvWindow* hg_windows = 0;
typedef int (CV_CDECL * CvWin32WindowCallback)(HWND, UINT, WPARAM, LPARAM, int*);
static CvWin32WindowCallback hg_on_preprocess = 0, hg_on_postprocess = 0;
static HINSTANCE hg_hinstance = 0;
static const char* highGUIclassName = "HighGUI class";
static const char* mainHighGUIclassName = "Main HighGUI class";
static void icvCleanupHighgui()
{
cvDestroyAllWindows();
UnregisterClass(highGUIclassName, hg_hinstance);
UnregisterClass(mainHighGUIclassName, hg_hinstance);
}
CV_IMPL int cvInitSystem( int, char** )
{
static int wasInitialized = 0;
// check initialization status
if( !wasInitialized )
{
// Initialize the stogare
hg_windows = 0;
// Register the class
WNDCLASS wndc;
wndc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
wndc.lpfnWndProc = WindowProc;
wndc.cbClsExtra = 0;
wndc.cbWndExtra = 0;
wndc.hInstance = hg_hinstance;
wndc.lpszClassName = highGUIclassName;
wndc.lpszMenuName = highGUIclassName;
wndc.hIcon = LoadIcon(0, IDI_APPLICATION);
wndc.hCursor = (HCURSOR)LoadCursor(0, (LPSTR)(size_t)IDC_CROSS );
wndc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
RegisterClass(&wndc);
wndc.lpszClassName = mainHighGUIclassName;
wndc.lpszMenuName = mainHighGUIclassName;
wndc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndc.lpfnWndProc = MainWindowProc;
RegisterClass(&wndc);
atexit( icvCleanupHighgui );
wasInitialized = 1;
}
return 0;
}
CV_IMPL int cvStartWindowThread(){
return 0;
}
static CvWindow* icvFindWindowByName( const char* name )
{
CvWindow* window = hg_windows;
for( ; window != 0 && strcmp( name, window->name) != 0; window = window->next )
;
return window;
}
static CvWindow* icvWindowByHWND( HWND hwnd )
{
CvWindow* window = (CvWindow*)icvGetWindowLongPtr( hwnd, CV_USERDATA );
return window != 0 && hg_windows != 0 &&
window->signature == CV_WINDOW_MAGIC_VAL ? window : 0;
}
static CvTrackbar* icvTrackbarByHWND( HWND hwnd )
{
CvTrackbar* trackbar = (CvTrackbar*)icvGetWindowLongPtr( hwnd, CV_USERDATA );
return trackbar != 0 && trackbar->signature == CV_TRACKBAR_MAGIC_VAL &&
trackbar->hwnd == hwnd ? trackbar : 0;
}
static const char* icvWindowPosRootKey = "Software\\OpenCV\\HighGUI\\Windows\\";
// Window positions saving/loading added by Philip Gruebele.
//<a href="mailto:pgruebele@cox.net">pgruebele@cox.net</a>
// Restores the window position from the registry saved position.
static void
icvLoadWindowPos( const char* name, CvRect& rect )
{
HKEY hkey;
char szKey[1024];
strcpy( szKey, icvWindowPosRootKey );
strcat( szKey, name );
rect.x = rect.y = CW_USEDEFAULT;
rect.width = rect.height = 320;
if( RegOpenKeyEx(HKEY_CURRENT_USER,szKey,0,KEY_QUERY_VALUE,&hkey) == ERROR_SUCCESS )
{
// Yes we are installed.
DWORD dwType = 0;
DWORD dwSize = sizeof(int);
RegQueryValueEx(hkey, "Left", NULL, &dwType, (BYTE*)&rect.x, &dwSize);
RegQueryValueEx(hkey, "Top", NULL, &dwType, (BYTE*)&rect.y, &dwSize);
RegQueryValueEx(hkey, "Width", NULL, &dwType, (BYTE*)&rect.width, &dwSize);
RegQueryValueEx(hkey, "Height", NULL, &dwType, (BYTE*)&rect.height, &dwSize);
if( rect.x != (int)CW_USEDEFAULT && (rect.x < -200 || rect.x > 3000) )
rect.x = 100;
if( rect.y != (int)CW_USEDEFAULT && (rect.y < -200 || rect.y > 3000) )
rect.y = 100;
if( rect.width != (int)CW_USEDEFAULT && (rect.width < 0 || rect.width > 3000) )
rect.width = 100;
if( rect.height != (int)CW_USEDEFAULT && (rect.height < 0 || rect.height > 3000) )
rect.height = 100;
RegCloseKey(hkey);
}
}
// Window positions saving/loading added by Philip Gruebele.
//<a href="mailto:pgruebele@cox.net">pgruebele@cox.net</a>
// philipg. Saves the window position in the registry
static void
icvSaveWindowPos( const char* name, CvRect rect )
{
static const DWORD MAX_RECORD_COUNT = 100;
HKEY hkey;
char szKey[1024];
char rootKey[1024];
strcpy( szKey, icvWindowPosRootKey );
strcat( szKey, name );
if( RegOpenKeyEx( HKEY_CURRENT_USER,szKey,0,KEY_READ,&hkey) != ERROR_SUCCESS )
{
HKEY hroot;
DWORD count = 0;
FILETIME oldestTime = { UINT_MAX, UINT_MAX };
char oldestKey[1024];
char currentKey[1024];
strcpy( rootKey, icvWindowPosRootKey );
rootKey[strlen(rootKey)-1] = '\0';
if( RegCreateKeyEx(HKEY_CURRENT_USER, rootKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ+KEY_WRITE, 0, &hroot, NULL) != ERROR_SUCCESS )
//RegOpenKeyEx( HKEY_CURRENT_USER,rootKey,0,KEY_READ,&hroot) != ERROR_SUCCESS )
return;
for(;;)
{
DWORD csize = sizeof(currentKey);
FILETIME accesstime = { 0, 0 };
LONG code = RegEnumKeyEx( hroot, count, currentKey, &csize, NULL, NULL, NULL, &accesstime );
if( code != ERROR_SUCCESS && code != ERROR_MORE_DATA )
break;
count++;
if( oldestTime.dwHighDateTime > accesstime.dwHighDateTime ||
(oldestTime.dwHighDateTime == accesstime.dwHighDateTime &&
oldestTime.dwLowDateTime > accesstime.dwLowDateTime) )
{
oldestTime = accesstime;
strcpy( oldestKey, currentKey );
}
}
if( count >= MAX_RECORD_COUNT )
RegDeleteKey( hroot, oldestKey );
RegCloseKey( hroot );
if( RegCreateKeyEx(HKEY_CURRENT_USER,szKey,0,NULL,REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hkey, NULL) != ERROR_SUCCESS )
return;
}
else
{
RegCloseKey( hkey );
if( RegOpenKeyEx( HKEY_CURRENT_USER,szKey,0,KEY_WRITE,&hkey) != ERROR_SUCCESS )
return;
}
RegSetValueEx(hkey, "Left", 0, REG_DWORD, (BYTE*)&rect.x, sizeof(rect.x));
RegSetValueEx(hkey, "Top", 0, REG_DWORD, (BYTE*)&rect.y, sizeof(rect.y));
RegSetValueEx(hkey, "Width", 0, REG_DWORD, (BYTE*)&rect.width, sizeof(rect.width));
RegSetValueEx(hkey, "Height", 0, REG_DWORD, (BYTE*)&rect.height, sizeof(rect.height));
RegCloseKey(hkey);
}
double cvGetModeWindow_W32(const char* name)//YV
{
double result = -1;
CV_FUNCNAME( "cvGetModeWindow_W32" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT; // keep silence here
result = window->status;
__END__;
return result;
}
void cvSetModeWindow_W32( const char* name, double prop_value)//Yannick Verdie
{
CV_FUNCNAME( "cvSetModeWindow_W32" );
__BEGIN__;
CvWindow* window;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if( !window )
CV_ERROR( CV_StsNullPtr, "NULL window" );
if(window->flags & CV_WINDOW_AUTOSIZE)//if the flag CV_WINDOW_AUTOSIZE is set
EXIT;
{
DWORD dwStyle = (DWORD)GetWindowLongPtr(window->frame, GWL_STYLE);
CvRect position;
if (window->status==CV_WINDOW_FULLSCREEN && prop_value==CV_WINDOW_NORMAL)
{
icvLoadWindowPos(window->name,position );
SetWindowLongPtr(window->frame, GWL_STYLE, dwStyle | WS_CAPTION | WS_THICKFRAME);
SetWindowPos(window->frame, HWND_TOP, position.x, position.y , position.width,position.height, SWP_NOZORDER | SWP_FRAMECHANGED);
window->status=CV_WINDOW_NORMAL;
EXIT;
}
if (window->status==CV_WINDOW_NORMAL && prop_value==CV_WINDOW_FULLSCREEN)
{
//save dimension
RECT rect;
GetWindowRect(window->frame, &rect);
CvRect RectCV = cvRect(rect.left, rect.top,rect.right - rect.left, rect.bottom - rect.top);
icvSaveWindowPos(window->name,RectCV );
//Look at coordinate for fullscreen
HMONITOR hMonitor;
MONITORINFO mi;
hMonitor = MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST);
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
//fullscreen
position.x=mi.rcMonitor.left;position.y=mi.rcMonitor.top;
position.width=mi.rcMonitor.right - mi.rcMonitor.left;position.height=mi.rcMonitor.bottom - mi.rcMonitor.top;
SetWindowLongPtr(window->frame, GWL_STYLE, dwStyle & ~WS_CAPTION & ~WS_THICKFRAME);
SetWindowPos(window->frame, HWND_TOP, position.x, position.y , position.width,position.height, SWP_NOZORDER | SWP_FRAMECHANGED);
window->status=CV_WINDOW_FULLSCREEN;
EXIT;
}
}
__END__;
}
double cvGetPropWindowAutoSize_W32(const char* name)
{
double result = -1;
CV_FUNCNAME( "cvSetCloseCallback" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT; // keep silence here
result = window->flags & CV_WINDOW_AUTOSIZE;
__END__;
return result;
}
double cvGetRatioWindow_W32(const char* name)
{
double result = -1;
CV_FUNCNAME( "cvGetRatioWindow_W32" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT; // keep silence here
result = static_cast<double>(window->width) / window->height;
__END__;
return result;
}
double cvGetOpenGlProp_W32(const char* name)
{
double result = -1;
#ifdef HAVE_OPENGL
CV_FUNCNAME( "cvGetOpenGlProp_W32" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT; // keep silence here
result = window->useGl;
__END__;
#endif
(void)name;
return result;
}
// OpenGL support
#ifdef HAVE_OPENGL
namespace
{
void createGlContext(HWND hWnd, HDC& hGLDC, HGLRC& hGLRC, bool& useGl)
{
CV_FUNCNAME( "createGlContext" );
__BEGIN__;
useGl = false;
int PixelFormat;
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
32, // 32 Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
hGLDC = GetDC(hWnd);
if (!hGLDC)
CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Device Context" );
PixelFormat = ChoosePixelFormat(hGLDC, &pfd);
if (!PixelFormat)
CV_ERROR( CV_OpenGlApiCallError, "Can't Find A Suitable PixelFormat" );
if (!SetPixelFormat(hGLDC, PixelFormat, &pfd))
CV_ERROR( CV_OpenGlApiCallError, "Can't Set The PixelFormat" );
hGLRC = wglCreateContext(hGLDC);
if (!hGLRC)
CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Rendering Context" );
if (!wglMakeCurrent(hGLDC, hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
useGl = true;
__END__;
}
void releaseGlContext(CvWindow* window)
{
//CV_FUNCNAME( "releaseGlContext" );
__BEGIN__;
if (window->hGLRC)
{
wglDeleteContext(window->hGLRC);
window->hGLRC = NULL;
}
if (window->dc)
{
ReleaseDC(window->hwnd, window->dc);
window->dc = NULL;
}
window->useGl = false;
__END__;
}
void drawGl(CvWindow* window)
{
CV_FUNCNAME( "drawGl" );
__BEGIN__;
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (window->glDrawCallback)
window->glDrawCallback(window->glDrawData);
if (!SwapBuffers(window->dc))
CV_ERROR( CV_OpenGlApiCallError, "Can't swap OpenGL buffers" );
__END__;
}
void resizeGl(CvWindow* window)
{
CV_FUNCNAME( "resizeGl" );
__BEGIN__;
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
glViewport(0, 0, window->width, window->height);
__END__;
}
}
#endif // HAVE_OPENGL
CV_IMPL int cvNamedWindow( const char* name, int flags )
{
int result = 0;
CV_FUNCNAME( "cvNamedWindow" );
__BEGIN__;
HWND hWnd, mainhWnd;
CvWindow* window;
DWORD defStyle = WS_VISIBLE | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
int len;
CvRect rect;
#ifdef HAVE_OPENGL
bool useGl;
HDC hGLDC;
HGLRC hGLRC;
#endif
cvInitSystem(0,0);
if( !name )
CV_ERROR( CV_StsNullPtr, "NULL name string" );
// Check the name in the storage
window = icvFindWindowByName( name );
if (window != 0)
{
result = 1;
EXIT;
}
if( !(flags & CV_WINDOW_AUTOSIZE))//YV add border in order to resize the window
defStyle |= WS_SIZEBOX;
icvLoadWindowPos( name, rect );
mainhWnd = CreateWindow( "Main HighGUI class", name, defStyle | WS_OVERLAPPED,
rect.x, rect.y, rect.width, rect.height, 0, 0, hg_hinstance, 0 );
if( !mainhWnd )
CV_ERROR( CV_StsError, "Frame window can not be created" );
ShowWindow(mainhWnd, SW_SHOW);
//YV- remove one border by changing the style
hWnd = CreateWindow("HighGUI class", "", (defStyle & ~WS_SIZEBOX) | WS_CHILD, CW_USEDEFAULT, 0, rect.width, rect.height, mainhWnd, 0, hg_hinstance, 0);
if( !hWnd )
CV_ERROR( CV_StsError, "Frame window can not be created" );
#ifndef HAVE_OPENGL
if (flags & CV_WINDOW_OPENGL)
CV_ERROR( CV_OpenGlNotSupported, "Library was built without OpenGL support" );
#else
useGl = false;
hGLDC = 0;
hGLRC = 0;
if (flags & CV_WINDOW_OPENGL)
createGlContext(hWnd, hGLDC, hGLRC, useGl);
#endif
ShowWindow(hWnd, SW_SHOW);
len = (int)strlen(name);
CV_CALL( window = (CvWindow*)cvAlloc(sizeof(CvWindow) + len + 1));
window->signature = CV_WINDOW_MAGIC_VAL;
window->hwnd = hWnd;
window->frame = mainhWnd;
window->name = (char*)(window + 1);
memcpy( window->name, name, len + 1 );
window->flags = flags;
window->image = 0;
#ifndef HAVE_OPENGL
window->dc = CreateCompatibleDC(0);
#else
if (!useGl)
{
window->dc = CreateCompatibleDC(0);
window->hGLRC = 0;
window->useGl = false;
}
else
{
window->dc = hGLDC;
window->hGLRC = hGLRC;
window->useGl = true;
}
window->glDrawCallback = 0;
window->glDrawData = 0;
#endif
window->last_key = 0;
window->status = CV_WINDOW_NORMAL;//YV
window->on_mouse = 0;
window->on_mouse_param = 0;
memset( &window->toolbar, 0, sizeof(window->toolbar));
window->next = hg_windows;
window->prev = 0;
if( hg_windows )
hg_windows->prev = window;
hg_windows = window;
icvSetWindowLongPtr( hWnd, CV_USERDATA, window );
icvSetWindowLongPtr( mainhWnd, CV_USERDATA, window );
// Recalculate window pos
icvUpdateWindowPos( window );
result = 1;
__END__;
return result;
}
#ifdef HAVE_OPENGL
CV_IMPL void cvSetOpenGlContext(const char* name)
{
CV_FUNCNAME( "cvSetOpenGlContext" );
__BEGIN__;
CvWindow* window;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
CV_ERROR( CV_StsNullPtr, "NULL window" );
if (!window->useGl)
CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
__END__;
}
CV_IMPL void cvUpdateWindow(const char* name)
{
CV_FUNCNAME( "cvUpdateWindow" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT;
InvalidateRect(window->hwnd, 0, 0);
__END__;
}
CV_IMPL void cvSetOpenGlDrawCallback(const char* name, CvOpenGlDrawCallback callback, void* userdata)
{
CV_FUNCNAME( "cvCreateOpenGLCallback" );
__BEGIN__;
CvWindow* window;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if( !window )
EXIT;
if (!window->useGl)
CV_ERROR( CV_OpenGlNotSupported, "Window was created without OpenGL context" );
window->glDrawCallback = callback;
window->glDrawData = userdata;
__END__;
}
#endif // HAVE_OPENGL
static void icvRemoveWindow( CvWindow* window )
{
CvTrackbar* trackbar = NULL;
RECT wrect={0,0,0,0};
#ifdef HAVE_OPENGL
if (window->useGl)
releaseGlContext(window);
#endif
if( window->frame )
GetWindowRect( window->frame, &wrect );
if( window->name )
icvSaveWindowPos( window->name, cvRect(wrect.left, wrect.top,
wrect.right-wrect.left, wrect.bottom-wrect.top) );
if( window->hwnd )
icvSetWindowLongPtr( window->hwnd, CV_USERDATA, 0 );
if( window->frame )
icvSetWindowLongPtr( window->frame, CV_USERDATA, 0 );
if( window->toolbar.toolbar )
icvSetWindowLongPtr(window->toolbar.toolbar, CV_USERDATA, 0);
if( window->prev )
window->prev->next = window->next;
else
hg_windows = window->next;
if( window->next )
window->next->prev = window->prev;
window->prev = window->next = 0;
if( window->dc && window->image )
DeleteObject(SelectObject(window->dc,window->image));
if( window->dc )
DeleteDC(window->dc);
for( trackbar = window->toolbar.first; trackbar != 0; )
{
CvTrackbar* next = trackbar->next;
if( trackbar->hwnd )
{
icvSetWindowLongPtr( trackbar->hwnd, CV_USERDATA, 0 );
cvFree( &trackbar );
}
trackbar = next;
}
cvFree( &window );
}
CV_IMPL void cvDestroyWindow( const char* name )
{
CV_FUNCNAME( "cvDestroyWindow" );
__BEGIN__;
CvWindow* window;
HWND mainhWnd;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if( !window )
EXIT;
mainhWnd = window->frame;
SendMessage(window->hwnd, WM_CLOSE, 0, 0);
SendMessage( mainhWnd, WM_CLOSE, 0, 0);
// Do NOT call _remove_window -- CvWindow list will be updated automatically ...
__END__;
}
static void icvScreenToClient( HWND hwnd, RECT* rect )
{
POINT p;
p.x = rect->left;
p.y = rect->top;
ScreenToClient(hwnd, &p);
OffsetRect( rect, p.x - rect->left, p.y - rect->top );
}
/* Calculatess the window coordinates relative to the upper left corner of the mainhWnd window */
static RECT icvCalcWindowRect( CvWindow* window )
{
const int gutter = 1;
RECT crect, trect, rect;
assert(window);
GetClientRect(window->frame, &crect);
if(window->toolbar.toolbar)
{
GetWindowRect(window->toolbar.toolbar, &trect);
icvScreenToClient(window->frame, &trect);
SubtractRect( &rect, &crect, &trect);
}
else
rect = crect;
rect.top += gutter;
rect.left += gutter;
rect.bottom -= gutter;
rect.right -= gutter;
return rect;