2015-05-20 16:09:58 -05:00
// 20 may 2015
2016-04-22 19:14:12 -05:00
# include "uipriv_windows.hpp"
2015-05-20 16:09:58 -05:00
2016-01-12 17:14:48 -06:00
// TODO
// - is there extra space on the bottom?
2015-06-02 11:55:12 -05:00
// we as Common Controls 6 users don't need to worry about the height of comboboxes; see http://blogs.msdn.com/b/oldnewthing/archive/2006/03/10/548537.aspx
2015-05-22 08:43:45 -05:00
2015-08-30 11:25:53 -05:00
struct uiCombobox {
uiWindowsControl c ;
2015-05-20 16:09:58 -05:00
HWND hwnd ;
2015-10-08 14:42:58 -05:00
void ( * onSelected ) ( uiCombobox * , void * ) ;
void * onSelectedData ;
2015-05-20 16:09:58 -05:00
} ;
2015-10-08 14:42:58 -05:00
uiWindowsDefineControlWithOnDestroy (
2015-08-30 11:25:53 -05:00
uiCombobox , // type name
2016-04-23 15:52:53 -05:00
uiWindowsUnregisterWM_COMMANDHandler ( me - > hwnd ) ; // on destroy
2015-08-30 11:25:53 -05:00
)
2015-05-29 18:48:27 -05:00
2015-10-08 14:42:58 -05:00
// note: NOT triggered on entering text
static BOOL onWM_COMMAND ( uiControl * cc , HWND hwnd , WORD code , LRESULT * lResult )
{
uiCombobox * c = uiCombobox ( cc ) ;
if ( code ! = CBN_SELCHANGE )
return FALSE ;
( * ( c - > onSelected ) ) ( c , c - > onSelectedData ) ;
* lResult = 0 ;
return TRUE ;
}
2015-05-20 16:09:58 -05:00
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
# define comboboxWidth 107 /* this is actually the shorter progress bar width, but Microsoft only indicates as wide as necessary */
# define comboboxHeight 14
2015-08-31 16:50:23 -05:00
static void minimumSize ( uiWindowsControl * c , uiWindowsSizing * d , intmax_t * width , intmax_t * height )
2015-05-20 16:09:58 -05:00
{
2015-08-30 11:25:53 -05:00
* width = uiWindowsDlgUnitsToX ( comboboxWidth , d - > BaseX ) ;
* height = uiWindowsDlgUnitsToY ( comboboxHeight , d - > BaseY ) ;
2015-05-20 16:09:58 -05:00
}
2015-10-08 14:42:58 -05:00
static void defaultOnSelected ( uiCombobox * c , void * data )
{
// do nothing
}
2015-08-30 11:25:53 -05:00
void uiComboboxAppend ( uiCombobox * c , const char * text )
2015-05-20 16:09:58 -05:00
{
WCHAR * wtext ;
LRESULT res ;
wtext = toUTF16 ( text ) ;
res = SendMessageW ( c - > hwnd , CB_ADDSTRING , 0 , ( LPARAM ) wtext ) ;
if ( res = = ( LRESULT ) CB_ERR )
2016-04-22 19:14:12 -05:00
logLastError ( L " error appending item to uiCombobox " ) ;
2015-05-20 16:09:58 -05:00
else if ( res = = ( LRESULT ) CB_ERRSPACE )
2016-04-22 19:14:12 -05:00
logLastError ( L " memory exhausted appending item to uiCombobox " ) ;
2015-05-20 16:09:58 -05:00
uiFree ( wtext ) ;
}
2015-10-08 14:42:58 -05:00
intmax_t uiComboboxSelected ( uiCombobox * c )
{
LRESULT n ;
n = SendMessage ( c - > hwnd , CB_GETCURSEL , 0 , 0 ) ;
if ( n = = ( LRESULT ) CB_ERR )
return - 1 ;
return ( intmax_t ) n ;
}
2015-10-09 13:39:26 -05:00
void uiComboboxSetSelected ( uiCombobox * c , intmax_t n )
{
// TODO error check
SendMessageW ( c - > hwnd , CB_SETCURSEL , ( WPARAM ) n , 0 ) ;
}
2015-10-08 14:42:58 -05:00
void uiComboboxOnSelected ( uiCombobox * c , void ( * f ) ( uiCombobox * c , void * data ) , void * data )
{
c - > onSelected = f ;
c - > onSelectedData = data ;
}
2015-05-21 20:45:31 -05:00
static uiCombobox * finishNewCombobox ( DWORD style )
2015-05-20 16:09:58 -05:00
{
2015-08-30 11:25:53 -05:00
uiCombobox * c ;
2015-05-20 16:09:58 -05:00
2016-04-24 16:38:48 -05:00
c = ( uiCombobox * ) uiNewControl ( uiCombobox ) ;
2015-05-20 16:09:58 -05:00
2015-08-31 11:33:44 -05:00
c - > hwnd = uiWindowsEnsureCreateControlHWND ( WS_EX_CLIENTEDGE ,
2015-05-29 17:03:24 -05:00
L " combobox " , L " " ,
style | WS_TABSTOP ,
hInstance , NULL ,
TRUE ) ;
2015-05-20 16:09:58 -05:00
2015-10-08 14:42:58 -05:00
uiWindowsRegisterWM_COMMANDHandler ( c - > hwnd , onWM_COMMAND , uiControl ( c ) ) ;
uiComboboxOnSelected ( c , defaultOnSelected , NULL ) ;
2015-08-30 11:25:53 -05:00
uiWindowsFinishNewControl ( c , uiCombobox ) ;
2015-05-20 16:09:58 -05:00
2015-08-30 11:25:53 -05:00
return c ;
2015-05-20 16:09:58 -05:00
}
2015-05-21 20:45:31 -05:00
uiCombobox * uiNewCombobox ( void )
{
return finishNewCombobox ( CBS_DROPDOWNLIST ) ;
}
uiCombobox * uiNewEditableCombobox ( void )
{
return finishNewCombobox ( CBS_DROPDOWN ) ;
}