From 006384edafb68817d798733793249b12f33b5d80 Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Sat, 27 Sep 2014 17:27:04 -0400 Subject: [PATCH] highgui: Support to change trackbar count in setTrackbarPos and replaced deprecated CreateToolbarEx in Windows. --- modules/highgui/include/opencv2/highgui.hpp | 15 +++++ .../include/opencv2/highgui/highgui_c.h | 1 + modules/highgui/src/window.cpp | 10 +++ modules/highgui/src/window_QT.cpp | 13 ++++ modules/highgui/src/window_cocoa.mm | 31 +++++++++ modules/highgui/src/window_gtk.cpp | 37 +++++++++++ modules/highgui/src/window_w32.cpp | 65 ++++++++++++------- 7 files changed, 150 insertions(+), 22 deletions(-) diff --git a/modules/highgui/include/opencv2/highgui.hpp b/modules/highgui/include/opencv2/highgui.hpp index 1c06bf0787..410a6bbcf9 100644 --- a/modules/highgui/include/opencv2/highgui.hpp +++ b/modules/highgui/include/opencv2/highgui.hpp @@ -463,6 +463,21 @@ panel. */ CV_EXPORTS_W void setTrackbarPos(const String& trackbarname, const String& winname, int pos); +/** @brief Sets the trackbar maximum position. + +@param trackbarname Name of the trackbar. +@param winname Name of the window that is the parent of trackbar. +@param maxval New maximum position. + +The function sets the maximum position of the specified trackbar in the specified window. + +@note + +**[Qt Backend Only]** winname can be empty (or NULL) if the trackbar is attached to the control +panel. + */ +CV_EXPORTS_W void setTrackbarMax(const String& trackbarname, const String& winname, int maxval); + //! @addtogroup highgui_opengl OpenGL support //! @{ diff --git a/modules/highgui/include/opencv2/highgui/highgui_c.h b/modules/highgui/include/opencv2/highgui/highgui_c.h index a8780ade06..46d4c95492 100644 --- a/modules/highgui/include/opencv2/highgui/highgui_c.h +++ b/modules/highgui/include/opencv2/highgui/highgui_c.h @@ -165,6 +165,7 @@ CVAPI(int) cvCreateTrackbar2( const char* trackbar_name, const char* window_name /* retrieve or set trackbar position */ CVAPI(int) cvGetTrackbarPos( const char* trackbar_name, const char* window_name ); CVAPI(void) cvSetTrackbarPos( const char* trackbar_name, const char* window_name, int pos ); +CVAPI(void) cvSetTrackbarMax(const char* trackbar_name, const char* window_name, int maxval); enum { diff --git a/modules/highgui/src/window.cpp b/modules/highgui/src/window.cpp index e3c997c08e..f43f86411b 100644 --- a/modules/highgui/src/window.cpp +++ b/modules/highgui/src/window.cpp @@ -206,6 +206,11 @@ void cv::setTrackbarPos( const String& trackbarName, const String& winName, int cvSetTrackbarPos(trackbarName.c_str(), winName.c_str(), value ); } +void cv::setTrackbarMax(const String& trackbarName, const String& winName, int maxval) +{ + cvSetTrackbarMax(trackbarName.c_str(), winName.c_str(), maxval); +} + int cv::getTrackbarPos( const String& trackbarName, const String& winName ) { return cvGetTrackbarPos(trackbarName.c_str(), winName.c_str()); @@ -573,6 +578,11 @@ CV_IMPL void cvSetTrackbarPos( const char*, const char*, int ) CV_NO_GUI_ERROR( "cvSetTrackbarPos" ); } +CV_IMPL void cvSetTrackbarMax(const char*, const char*, int) +{ + CV_NO_GUI_ERROR( "cvSetTrackbarMax" ); +} + CV_IMPL void* cvGetWindowHandle( const char* ) { CV_NO_GUI_ERROR( "cvGetWindowHandle" ); diff --git a/modules/highgui/src/window_QT.cpp b/modules/highgui/src/window_QT.cpp index fc94dedfbb..7f37e143a8 100644 --- a/modules/highgui/src/window_QT.cpp +++ b/modules/highgui/src/window_QT.cpp @@ -654,6 +654,19 @@ CV_IMPL void cvSetTrackbarPos(const char* name_bar, const char* window_name, int } +CV_IMPL void cvSetTrackbarMax(const char* name_bar, const char* window_name, int maxval) +{ + if (maxval >= 0) + { + QPointer t = icvFindTrackBarByName(name_bar, window_name); + if (t) + { + t->slider->setMaximum(maxval); + } + } +} + + /* assign callback for mouse events */ CV_IMPL void cvSetMouseCallback(const char* window_name, CvMouseCallback on_mouse, void* param) { diff --git a/modules/highgui/src/window_cocoa.mm b/modules/highgui/src/window_cocoa.mm index 4542fb292b..414ed64755 100644 --- a/modules/highgui/src/window_cocoa.mm +++ b/modules/highgui/src/window_cocoa.mm @@ -61,6 +61,7 @@ CV_IMPL int cvCreateTrackbar2(const char* trackbar_name,const char* window_name, CV_IMPL void cvSetMouseCallback( const char* name, CvMouseCallback function, void* info) {} CV_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name ) {return 0;} CV_IMPL void cvSetTrackbarPos(const char* trackbar_name, const char* window_name, int pos) {} +CV_IMPL void cvSetTrackbarMax(const char* trackbar_name, const char* window_name, int maxval) {} CV_IMPL void* cvGetWindowHandle( const char* name ) {return NULL;} CV_IMPL const char* cvGetWindowName( void* window_handle ) {return NULL;} CV_IMPL int cvNamedWindow( const char* name, int flags ) {return 0; } @@ -421,6 +422,36 @@ CV_IMPL void cvSetTrackbarPos(const char* trackbar_name, const char* window_name __END__; } +CV_IMPL void cvSetTrackbarMax(const char* trackbar_name, const char* window_name, int maxval) +{ + CV_FUNCNAME("cvSetTrackbarPos"); + + CVWindow *window = nil; + CVSlider *slider = nil; + NSAutoreleasePool* localpool5 = nil; + + __BEGIN__; + //cout << "cvSetTrackbarPos" << endl; + if(trackbar_name == NULL || window_name == NULL) + CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" ); + + if (localpool5 != nil) [localpool5 drain]; + localpool5 = [[NSAutoreleasePool alloc] init]; + + window = cvGetWindow(window_name); + if(window) { + slider = [[window sliders] valueForKey:[NSString stringWithFormat:@"%s", trackbar_name]]; + if(slider) { + if(maxval >= 0) { + [[slider slider] setMaxValue:maxval]; + } + } + } + [localpool5 drain]; + + __END__; +} + CV_IMPL void* cvGetWindowHandle( const char* name ) { //cout << "cvGetWindowHandle" << endl; diff --git a/modules/highgui/src/window_gtk.cpp b/modules/highgui/src/window_gtk.cpp index 4b916e6487..23dd588cb9 100644 --- a/modules/highgui/src/window_gtk.cpp +++ b/modules/highgui/src/window_gtk.cpp @@ -1584,6 +1584,43 @@ CV_IMPL void cvSetTrackbarPos( const char* trackbar_name, const char* window_nam } +CV_IMPL void cvSetTrackbarMax(const char* trackbar_name, const char* window_name, int maxval) +{ + CV_FUNCNAME("cvSetTrackbarMax"); + + __BEGIN__; + + if (maxval >= 0) + { + CvWindow* window = 0; + CvTrackbar* trackbar = 0; + + if (trackbar_name == 0 || window_name == 0) + { + CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name"); + } + + window = icvFindWindowByName( window_name ); + if (window) + { + trackbar = icvFindTrackbarByName(window, trackbar_name); + if (trackbar) + { + trackbar->maxval = maxval; + + CV_LOCK_MUTEX(); + + gtk_range_set_range(GTK_RANGE(trackbar->widget), 0, trackbar->maxval); + + CV_UNLOCK_MUTEX(); + } + } + } + + __END__; +} + + CV_IMPL void* cvGetWindowHandle( const char* window_name ) { void* widget = 0; diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index 6b692e0d27..81748c9fb1 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -1982,22 +1982,6 @@ icvFindTrackbarByName( const CvWindow* window, const char* name ) } -typedef struct -{ - UINT cbSize; - DWORD dwMask; - int idCommand; - int iImage; - BYTE fsState; - BYTE fsStyle; - WORD cx; - DWORD lParam; - LPSTR pszText; - int cchText; -} -ButtonInfo; - - static int icvCreateTrackbar( const char* trackbar_name, const char* window_name, int* val, int count, CvTrackbarCallback on_notify, @@ -2017,7 +2001,7 @@ icvCreateTrackbar( const char* trackbar_name, const char* window_name, if( !window_name || !trackbar_name ) CV_ERROR( CV_StsNullPtr, "NULL window or trackbar name" ); - if( count <= 0 ) + if( count < 0 ) CV_ERROR( CV_StsOutOfRange, "Bad trackbar maximal value" ); window = icvFindWindowByName(window_name); @@ -2027,8 +2011,8 @@ icvCreateTrackbar( const char* trackbar_name, const char* window_name, trackbar = icvFindTrackbarByName(window,trackbar_name); if( !trackbar ) { - TBBUTTON tbs; - ButtonInfo tbis; + TBBUTTON tbs = {0}; + TBBUTTONINFO tbis = {0}; RECT rect; int bcount; int len = (int)strlen( trackbar_name ); @@ -2038,9 +2022,14 @@ icvCreateTrackbar( const char* trackbar_name, const char* window_name, { const int default_height = 30; - window->toolbar.toolbar = CreateToolbarEx( - window->frame, WS_CHILD | CCS_TOP | TBSTYLE_WRAPABLE, - 1, 0, 0, 0, 0, 0, 16, 20, 16, 16, sizeof(TBBUTTON)); + // CreateToolbarEx is deprecated and forces linking against Comctl32.lib. + window->toolbar.toolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, + WS_CHILD | CCS_TOP | TBSTYLE_WRAPABLE | BTNS_AUTOSIZE | BTNS_BUTTON, + 0, 0, 0, 0, + window->frame, NULL, GetModuleHandle(NULL), NULL); + // CreateToolbarEx automatically sends this but CreateWindowEx doesn't. + SendMessage(window->toolbar.toolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0); + GetClientRect(window->frame, &rect); MoveWindow( window->toolbar.toolbar, 0, 0, rect.right - rect.left, default_height, TRUE); @@ -2288,6 +2277,38 @@ CV_IMPL void cvSetTrackbarPos( const char* trackbar_name, const char* window_nam } +CV_IMPL void cvSetTrackbarMax(const char* trackbar_name, const char* window_name, int maxval) +{ + CV_FUNCNAME( "cvSetTrackbarMax" ); + + __BEGIN__; + + if (maxval >= 0) + { + CvWindow* window = 0; + CvTrackbar* trackbar = 0; + if (trackbar_name == 0 || window_name == 0) + { + CV_ERROR(CV_StsNullPtr, "NULL trackbar or window name"); + } + + window = icvFindWindowByName(window_name); + if (window) + { + trackbar = icvFindTrackbarByName(window, trackbar_name); + if (trackbar) + { + // The position will be min(pos, maxval). + trackbar->maxval = maxval; + SendMessage(trackbar->hwnd, TBM_SETRANGEMAX, (WPARAM)TRUE, (LPARAM)maxval); + } + } + } + + __END__; +} + + CV_IMPL void* cvGetWindowHandle( const char* window_name ) { void* hwnd = 0;