diff --git a/LUA_GenDialog.cpp b/LUA_GenDialog.cpp index c7cd270..911b120 100644 --- a/LUA_GenDialog.cpp +++ b/LUA_GenDialog.cpp @@ -35,24 +35,18 @@ const int OFFS_CTRLS = 40 ; enum TYPE_CTRLS { CTRL_NONE = 0, CTRL_CHECK = 1, CTRL_COMBO = 2, CTRL_EDIT = 3, CTRL_COLOR = 4, CTRL_BUTTON = 5} ; const char* SEC_SCENE = "Scene" ; const char* KEY_CUSTOMCOLORS = "CustomColors" ; -// Tipi -struct DialogContext { - bool bDone = false ; // flag per chiusura dialogo (X/Ok/Cancel) - bool bCancelled = false ; // true se X/Cancel - STRVECTOR vsResult ; // valori finali da ritornare a Lua -} ; // Variabili Globali -static COLORREF s_CustomColors[16] = {12632256} ; // 16 colori GRAY -static bool s_bCustomColorsInit = false ; // flag di inizializzazione colori static HWND s_hDlg = NULL ; static string s_sCaption ; static int s_nCtrls = 0 ; static string s_sText[MAX_CTRLS] ; static string s_sEdit[MAX_CTRLS] ; static int s_nType[MAX_CTRLS] ; -static bool s_bSelectionMode = false ; // se true allora diventa dialogo "Modeless" -static int s_nActiveRow = - 1 ; // indice della riga di "SB:" attiva -static int s_nDlgItem = -1 ; // indice dell'Item di selezione +static bool s_bOk = false ; // uscita con Ok +static bool s_bCustomColorsInit = false ; // flag di inizializzazione colori custom +static COLORREF s_CustomColors[16] = {12632256} ; // 16 colori GRAY +static bool s_bSelectionMode = false ; // se true allora diventa dialogo "Modeless" +static int s_nDlgItem = -1 ; // indice dell'Item di selezione //------------------------------------------------------------------------------- // OnClosingDialog @@ -78,7 +72,6 @@ OnClosingDialog( HWND hwndDlg) { // reset stato globale s_bSelectionMode = false ; - s_nActiveRow = -1 ; s_nDlgItem = -1 ; // il controllo passa al Main HWND hMain = GetParent( hwndDlg) ; @@ -99,12 +92,11 @@ OnClosingDialog( HWND hwndDlg) // con la scena mentre il dialogo rimane aperto e attivo. // // Parametri: -// hDlg → handle del dialogo -// nEditId → ID dell’edit associato al pulsante BS premuto -// nActiveRow → indice della riga BS attiva (0..MAX_CTRLS-1) +// hDlg handle del dialogo +// nEditId ID dell’edit associato al pulsante BS premuto //------------------------------------------------------------------------------- static void -StartSelectionMode( HWND hDlg, int nEditId, int nActiveRow) +StartSelectionMode( HWND hDlg, int nEditId) { // il dialogo torna modeless rispetto alla scena HWND hMain = GetParent( hDlg) ; @@ -114,9 +106,9 @@ StartSelectionMode( HWND hDlg, int nEditId, int nActiveRow) // disabilito tutti gli edit associati agli SB tranne quello attivo for ( int i = 0; i < s_nCtrls ; ++ i) { - if ( s_sEdit[i].find( "SB:") == 0) { + if ( s_nType[i] == CTRL_BUTTON) { int idEdit = IDC_EDIT1 + i ; - EnableWindow( GetDlgItem( hDlg, idEdit), ( i == nActiveRow ? TRUE : FALSE)) ; + EnableWindow( GetDlgItem( hDlg, idEdit), ( idEdit == s_nDlgItem ? TRUE : FALSE)) ; } } @@ -124,7 +116,6 @@ StartSelectionMode( HWND hDlg, int nEditId, int nActiveRow) s_nDlgItem = - 1 ; ExeDeselectAll() ; // !<-- NB. avendo messo s_nDlgItem = -1 il testo non viene rimosso s_bSelectionMode = true ; - s_nActiveRow = nActiveRow ; s_nDlgItem = nEditId ; AtoWEX<128> wsEdit( "") ; GetDlgItemText( hDlg, nEditId, LPWSTR( wsEdit), 128) ; @@ -149,12 +140,11 @@ static void EndSelectionMode( HWND hDlg) { s_bSelectionMode = false ; - s_nActiveRow = -1 ; s_nDlgItem = -1 ; // disabilito tutti gli edit associati ai BS for ( int i = 0; i < s_nCtrls ; ++ i) { - if ( s_sEdit[i].find( "SB:") == 0) { + if ( s_nType[i] == CTRL_BUTTON) { int idEdit = IDC_EDIT1 + i ; EnableWindow( GetDlgItem( hDlg, idEdit), FALSE) ; SetDlgItemText( hDlg, IDC_BUTTON_SEL1 + i, strztoW( "+")) ; @@ -171,7 +161,23 @@ EndSelectionMode( HWND hDlg) } //----------------------------------------------------------------------------- -LRESULT +static string +ValidateSelection( const string& sSel) +{ + string sVal = "" ; + INTVECTOR vIds ; + FromString( sSel, vIds) ; + for ( int nId : vIds) { + if ( ExeExistsObj( nId)) + sVal += ToString( nId) + "," ; + } + if ( ! sVal.empty()) + sVal.pop_back() ; + return sVal ; +} + +//----------------------------------------------------------------------------- +static LRESULT CALLBACK UpdateDialogSelection( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) { if ( msg == WM_GETDLGCODE) @@ -219,215 +225,214 @@ CALLBACK DialogBoxProc( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam switch ( message) { case WM_INITDIALOG : { - // imposto il dialogo come modale + // imposto il dialogo come modale HWND hMain = GetParent( hwndDlg) ; EnableWindow( hMain, FALSE) ; SetFocus( hwndDlg) ; - // imposto il contesto della funzione per recuperalo in futuro (per la funzione di CallBack Lua) + // imposto il contesto della funzione per recuperalo in futuro (per la funzione di CallBack Lua) SetWindowLongPtr( hwndDlg, GWLP_USERDATA, (LONG_PTR)lParam) ; - // colore di default + // colore di default COLORREF defaultColor = RGB( 255, 0, 0) ; - // inizializzazione colori e brush + // inizializzazione colori e brush for ( int i = 0 ; i < MAX_CTRLS ; ++ i) { selectedColor[i] = defaultColor ; if ( hColorBrush[i] != NULL) DeleteObject( hColorBrush[i]) ; hColorBrush[i] = CreateSolidBrush( selectedColor[i]) ; } - // dati del dialogo + // dati del dialogo HWND hwndOwner = GetParent( hwndDlg) ; if ( hwndOwner == nullptr) hwndOwner = GetDesktopWindow() ; RECT rcOwner, rcDlg ; GetWindowRect( hwndOwner, &rcOwner) ; GetWindowRect( hwndDlg, &rcDlg) ; - // determino la posizione dell'ultima riga di dati da visualizzare + // determino la posizione dell'ultima riga di dati da visualizzare HWND hwndLR = GetDlgItem( hwndDlg, IDC_TEXT1 + Clamp( s_nCtrls, 1, MAX_CTRLS) - 1) ; RECT rcLR ; GetWindowRect( hwndLR, &rcLR) ; POINT ptLR{ rcLR.left, rcLR.top} ; ScreenToClient( hwndDlg, &ptLR) ; - // centro e scalo il dialogo + // centro e scalo il dialogo int nNewH = rcLR.top - rcOwner.top + 2 * OFFS_CTRLS ; // non chiaro perchè va sottratto rcOwner.top ma funziona sempre SetWindowPos( hwndDlg, HWND_TOP, ( rcOwner.left + rcOwner.right - ( rcDlg.right - rcDlg.left)) / 2, ( rcOwner.top + rcOwner.bottom - nNewH) / 2, ( rcDlg.right - rcDlg.left), nNewH, 0) ; - // riposiziono il bottone Ok + // riposiziono il bottone Ok HWND hwndOk = GetDlgItem( hwndDlg, IDOK) ; RECT rcOk ; GetWindowRect( hwndOk, &rcOk) ; POINT ptOk{ rcOk.left, rcOk.top} ; ScreenToClient( hwndDlg, &ptOk) ; SetWindowPos( hwndOk, hwndDlg, ptOk.x, ptLR.y + OFFS_CTRLS, 0, 0, SWP_NOSIZE | SWP_NOZORDER) ; - // riposiziono il bottone Cancel + // riposiziono il bottone Cancel HWND hwndCl = GetDlgItem( hwndDlg, IDCANCEL) ; RECT rcCl ; GetWindowRect( hwndCl, &rcCl) ; POINT ptCl{ rcCl.left, rcCl.top} ; ScreenToClient( hwndDlg, &ptCl) ; SetWindowPos( hwndCl, hwndDlg, ptCl.x, ptLR.y + OFFS_CTRLS, 0, 0, SWP_NOSIZE | SWP_NOZORDER) ; - // stile finestra + // stile finestra LONG style = GetWindowLong( hwndDlg, GWL_STYLE) ; style |= WS_SYSMENU ; SetWindowLong( hwndDlg, GWL_STYLE, style) ; } - // assegno titolo del dialogo + // assegno titolo del dialogo SetWindowText( hwndDlg, stringtoW( s_sCaption)) ; - // assegno testi e valori di default e nascondo linee dei controlli non usati - for ( int i = 0 ; i < MAX_CTRLS ; ++ i) { - if ( i < s_nCtrls) { + // assegno testi e valori di default ai controlli usati + for ( int i = 0 ; i < s_nCtrls ; ++ i) { + // imposto il testo nella riga corrente + SetDlgItemText( hwndDlg, IDC_TEXT1 + i, stringtoW( s_sText[i])) ; - // imposto il testo nella riga corrente - SetDlgItemText( hwndDlg, IDC_TEXT1 + i, stringtoW( s_sText[i])) ; + // --- se CheckBox + if ( s_sEdit[i].starts_with( "CK:")) { + bool bChecked = false ; + FromString( s_sEdit[i].substr( 3), bChecked) ; + CheckDlgButton( hwndDlg, IDC_CHECK1 + i, ( bChecked ? BST_CHECKED : BST_UNCHECKED)) ; + ShowWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), SW_HIDE) ; // nascondo Edit + ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; // nascondo Combo + ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; // nascondo Button di selezione + s_nType[i] = CTRL_CHECK ; + } - // --- se CheckBox - if ( s_sEdit[i].find( "CK:") == 0) { - bool bChecked = false ; - FromString( s_sEdit[i].substr( 3), bChecked) ; - CheckDlgButton( hwndDlg, IDC_CHECK1 + i, ( bChecked ? BST_CHECKED : BST_UNCHECKED)) ; - ShowWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), SW_HIDE) ; // nascondo Edit - ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; // nascondo Combo - ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; // nascondo Button di selezione - s_nType[i] = CTRL_CHECK ; - } - - // --- se ComboBox - else if ( s_sEdit[i].find( "CB:") == 0) { - string sList = s_sEdit[i].substr( 3) ; - STRVECTOR vsVal ; - Tokenize( sList, ",", vsVal) ; - int nSel = 0 ; - HWND hwndCBox = GetDlgItem( hwndDlg, IDC_COMBO1 + i) ; - for ( int j = 0 ; j < ssize( vsVal) ; ++ j) { - string sItem ; - if ( vsVal[j][0] == '*') { - nSel = j ; - sItem = vsVal[j].substr( 1) ; - } - else - sItem = vsVal[j] ; - ComboBox_AddString( hwndCBox, stringtoW( sItem)) ; + // --- se ComboBox + else if ( s_sEdit[i].starts_with( "CB:")) { + string sList = s_sEdit[i].substr( 3) ; + STRVECTOR vsVal ; + Tokenize( sList, ",", vsVal) ; + int nSel = 0 ; + HWND hwndCBox = GetDlgItem( hwndDlg, IDC_COMBO1 + i) ; + for ( int j = 0 ; j < ssize( vsVal) ; ++ j) { + string sItem ; + if ( vsVal[j][0] == '*') { + nSel = j ; + sItem = vsVal[j].substr( 1) ; } - ComboBox_SetCurSel( hwndCBox, nSel) ; - int nEditHeight = int( SendMessage( hwndCBox, CB_GETITEMHEIGHT, (WPARAM)-1, 0)) ; - int nItemHeight = int( SendMessage( hwndCBox, CB_GETITEMHEIGHT, 0, 0)) ; - int nTotalHeight = nEditHeight + nItemHeight * ( ssize( vsVal) + 1) ; - RECT rc ; GetWindowRect( hwndCBox, &rc) ; - HWND parent = GetParent(hwndCBox) ; MapWindowPoints( NULL, parent, (LPPOINT)&rc, 2) ; - SetWindowPos( hwndCBox, NULL, rc.left, rc.top, rc.right - rc.left, nTotalHeight, SWP_NOZORDER) ; - ShowWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), SW_HIDE) ; // nascondo Edit - ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; // nascondo CheckBox - ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; // nascondo Button di selezione - s_nType[i] = CTRL_COMBO ; + else + sItem = vsVal[j] ; + ComboBox_AddString( hwndCBox, stringtoW( sItem)) ; } + ComboBox_SetCurSel( hwndCBox, nSel) ; + int nEditHeight = int( SendMessage( hwndCBox, CB_GETITEMHEIGHT, (WPARAM)-1, 0)) ; + int nItemHeight = int( SendMessage( hwndCBox, CB_GETITEMHEIGHT, 0, 0)) ; + int nTotalHeight = nEditHeight + nItemHeight * ( ssize( vsVal) + 1) ; + RECT rc ; GetWindowRect( hwndCBox, &rc) ; + HWND parent = GetParent(hwndCBox) ; MapWindowPoints( NULL, parent, (LPPOINT)&rc, 2) ; + SetWindowPos( hwndCBox, NULL, rc.left, rc.top, rc.right - rc.left, nTotalHeight, SWP_NOZORDER) ; + ShowWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), SW_HIDE) ; // nascondo Edit + ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; // nascondo CheckBox + ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; // nascondo Button di selezione + s_nType[i] = CTRL_COMBO ; + } - // --- se ColorPicker - else if ( s_sEdit[i].find( "CP:") == 0) { - string sVal = s_sEdit[i].substr( 3) ; - Color CColor ; - if ( ! FromString( sVal, CColor)) - CColor = INVISIBLE ; - selectedColor[i] = RGB( CColor.GetIntRed(), CColor.GetIntGreen(), CColor.GetIntBlue()) ; - if ( hColorBrush[i] != NULL) - DeleteObject( hColorBrush[i]) ; - hColorBrush[i] = CreateSolidBrush( selectedColor[i]) ; - // recupero i valori dei colori personalizzati dal file .ini - if ( ! s_bCustomColorsInit) { - // recupero il file Ini - string sIniFile = ExeGetIniFile() ; - if ( ! sIniFile.empty()) { - string sCustomVals = GetPrivateProfileStringUtf8( SEC_SCENE, KEY_CUSTOMCOLORS, "", sIniFile.c_str()) ; - STRVECTOR vsWinColors ; Tokenize( sCustomVals, ",", vsWinColors) ; - for ( int j = 0 ; j < ssize( vsWinColors) && j < 16 ; ++ j) { - unsigned int unVal ; - if ( FromString( vsWinColors[j], unVal)) { - unsigned int unR = unVal & 0xFF ; - unsigned int unG = ( unVal >> 8) & 0xFF ; - unsigned int unB = ( unVal >> 16) & 0xFF ; - s_CustomColors[j] = RGB( unR, unG, unB) ; - } + // --- se ColorPicker + else if ( s_sEdit[i].starts_with( "CP:")) { + string sVal = s_sEdit[i].substr( 3) ; + Color CColor ; + if ( ! FromString( sVal, CColor)) + CColor = INVISIBLE ; + selectedColor[i] = RGB( CColor.GetIntRed(), CColor.GetIntGreen(), CColor.GetIntBlue()) ; + if ( hColorBrush[i] != NULL) + DeleteObject( hColorBrush[i]) ; + hColorBrush[i] = CreateSolidBrush( selectedColor[i]) ; + // recupero i valori dei colori personalizzati dal file .ini + if ( ! s_bCustomColorsInit) { + // recupero il file Ini + string sIniFile = ExeGetIniFile() ; + if ( ! sIniFile.empty()) { + string sCustomVals = GetPrivateProfileStringUtf8( SEC_SCENE, KEY_CUSTOMCOLORS, "", sIniFile.c_str()) ; + STRVECTOR vsWinColors ; Tokenize( sCustomVals, ",", vsWinColors) ; + for ( int j = 0 ; j < ssize( vsWinColors) && j < 16 ; ++ j) { + unsigned int unVal ; + if ( FromString( vsWinColors[j], unVal)) { + unsigned int unR = unVal & 0xFF ; + unsigned int unG = ( unVal >> 8) & 0xFF ; + unsigned int unB = ( unVal >> 16) & 0xFF ; + s_CustomColors[j] = RGB( unR, unG, unB) ; } } - s_bCustomColorsInit = true ; } - // nascondo qualsiasi tipo di componente - ShowWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), SW_HIDE) ; // nascondo Edit - ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; // nascondo Combo - ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; // nascondo Check - ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; // nascondo Button di selezione - - // margini per dimensione del rettangolo preview pickColor - const int PREVIEW_HEIGHT = 20 ; // altezza del rettangolo - const int PREVIEW_MIN_WIDTH = 40 ; // larghezza minima - - // creazione della static preview - HWND hwndPreview = NULL ; - HWND hwndEdit = GetDlgItem( hwndDlg, IDC_EDIT1 + i) ; - RECT rcEdit ; GetClientRect( hwndDlg, &rcEdit) ; // coordinate client del dialog - if ( hwndEdit != nullptr && GetWindowRect( hwndEdit, &rcEdit)) { - // converti rcLabel in coordinate client - POINT ptEdit = { rcEdit.left, rcEdit.top } ; - ScreenToClient( hwndDlg, &ptEdit) ; - int x = ptEdit.x ; - int y = ptEdit.y ; - int width = rcEdit.right - rcEdit.left ; - if ( width < PREVIEW_MIN_WIDTH) - width = PREVIEW_MIN_WIDTH ; - hwndPreview = CreateWindowEx( 0, L"STATIC", NULL, - WS_CHILD | WS_VISIBLE | SS_NOTIFY | SS_CENTERIMAGE | WS_BORDER, - x, y, width, PREVIEW_HEIGHT, - hwndDlg, ( HMENU)INT_PTR( IDC_COLOR1 + i), - GetModuleHandle( NULL), NULL) ; - } - // pulizia elementi testuali - if ( hwndPreview) { - SetWindowText( hwndPreview, L"") ; - SetWindowPos( hwndPreview, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE) ; - InvalidateRect( hwndPreview, NULL, TRUE) ; - UpdateWindow( hwndPreview) ; - } - s_nType[i] = CTRL_COLOR ; + s_bCustomColorsInit = true ; } + // nascondo qualsiasi tipo di componente + ShowWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), SW_HIDE) ; // nascondo Edit + ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; // nascondo Combo + ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; // nascondo Check + ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; // nascondo Button di selezione - // --- se Button di selezione - else if ( s_sEdit[i].find( "SB:") == 0) { - ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; // nascondo la Combo - ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; // nascondo la Check - SetDlgItemText( hwndDlg, IDC_BUTTON_SEL1 + i, strztoW( "+")) ; // modalità non Selezione - EnableWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), FALSE) ; // disattivo la Edit - // associo alla casella di testo la funzione di KeyPress su Enter - HWND hEdit = GetDlgItem( hwndDlg, IDC_EDIT1 + i) ; - if ( hEdit != nullptr) - SetWindowSubclass( hEdit, UpdateDialogSelection, 1, (DWORD_PTR)( IDC_EDIT1 + i)) ; - s_nType[i] = CTRL_BUTTON ; - } + // margini per dimensione del rettangolo preview pickColor + const int PREVIEW_HEIGHT = 20 ; // altezza del rettangolo + const int PREVIEW_MIN_WIDTH = 40 ; // larghezza minima - // --- se Testo - else { - SetDlgItemText( hwndDlg, IDC_EDIT1 + i, stringtoW( s_sEdit[i])) ; - ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; // nascondo Combo - ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; // nascondo Check - ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; // nascondo Button di selezione - s_nType[i] = CTRL_EDIT ; + // creazione della static preview + HWND hwndPreview = NULL ; + HWND hwndEdit = GetDlgItem( hwndDlg, IDC_EDIT1 + i) ; + RECT rcEdit ; GetClientRect( hwndDlg, &rcEdit) ; // coordinate client del dialog + if ( hwndEdit != nullptr && GetWindowRect( hwndEdit, &rcEdit)) { + // converti rcLabel in coordinate client + POINT ptEdit = { rcEdit.left, rcEdit.top } ; + ScreenToClient( hwndDlg, &ptEdit) ; + int x = ptEdit.x ; + int y = ptEdit.y ; + int width = rcEdit.right - rcEdit.left ; + if ( width < PREVIEW_MIN_WIDTH) + width = PREVIEW_MIN_WIDTH ; + hwndPreview = CreateWindowEx( 0, L"STATIC", NULL, + WS_CHILD | WS_VISIBLE | SS_NOTIFY | SS_CENTERIMAGE | WS_BORDER, + x, y, width, PREVIEW_HEIGHT, + hwndDlg, ( HMENU)INT_PTR( IDC_COLOR1 + i), + GetModuleHandle( NULL), NULL) ; } + // pulizia elementi testuali + if ( hwndPreview) { + SetWindowText( hwndPreview, L"") ; + SetWindowPos( hwndPreview, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE) ; + InvalidateRect( hwndPreview, NULL, TRUE) ; + UpdateWindow( hwndPreview) ; + } + s_nType[i] = CTRL_COLOR ; } - // se numero di controllo superiore al massimo, non visualizzo nullo ( in questo caso aggiungere elementi a Dialog in .rc) + + // se Button di selezione + else if ( s_sEdit[i].starts_with( "SB:")) { + string sVal = ValidateSelection( s_sEdit[i].substr( 3)) ; + SetDlgItemText( hwndDlg, IDC_EDIT1 + i, stringtoW( sVal)) ; + ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; // nascondo la Combo + ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; // nascondo la Check + SetDlgItemText( hwndDlg, IDC_BUTTON_SEL1 + i, strztoW( "+")) ; // modalità non Selezione + EnableWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), FALSE) ; // disattivo la Edit + // associo alla casella di testo la funzione di KeyPress su Enter + HWND hEdit = GetDlgItem( hwndDlg, IDC_EDIT1 + i) ; + if ( hEdit != nullptr) + SetWindowSubclass( hEdit, UpdateDialogSelection, 1, (DWORD_PTR)( IDC_EDIT1 + i)) ; + s_nType[i] = CTRL_BUTTON ; + } + + // altrimenti Testo else { - // nascondo tutto - ShowWindow( GetDlgItem( hwndDlg, IDC_TEXT1 + i), SW_HIDE) ; - ShowWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), SW_HIDE) ; - ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; - ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; - ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; - s_nType[i] = CTRL_NONE ; + SetDlgItemText( hwndDlg, IDC_EDIT1 + i, stringtoW( s_sEdit[i])) ; + ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; // nascondo Combo + ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; // nascondo Check + ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; // nascondo Button di selezione + s_nType[i] = CTRL_EDIT ; } } + + // nascondo i controlli non definiti + for ( int i = s_nCtrls ; i < MAX_CTRLS ; ++ i) { + ShowWindow( GetDlgItem( hwndDlg, IDC_TEXT1 + i), SW_HIDE) ; + ShowWindow( GetDlgItem( hwndDlg, IDC_EDIT1 + i), SW_HIDE) ; + ShowWindow( GetDlgItem( hwndDlg, IDC_COMBO1 + i), SW_HIDE) ; + ShowWindow( GetDlgItem( hwndDlg, IDC_CHECK1 + i), SW_HIDE) ; + ShowWindow( GetDlgItem( hwndDlg, IDC_BUTTON_SEL1 + i), SW_HIDE) ; + s_nType[i] = CTRL_NONE ; + } break ; case WM_CTLCOLORSTATIC : @@ -448,14 +453,13 @@ CALLBACK DialogBoxProc( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam case WM_COMMAND : { - int id = LOWORD( wParam) ; - int code = HIWORD( wParam) ; + int nId = LOWORD( wParam) ; + int nCode = HIWORD( wParam) ; // --- se click di un Button di selezione - if ( id >= IDC_BUTTON_SEL1 && id < IDC_BUTTON_SEL1 + MAX_CTRLS) { - int nBtnId = id ; - int nEditId = IDC_EDIT1 + ( id - IDC_BUTTON_SEL1) ; - int nActiveRow = nEditId - IDC_EDIT1 ; + if ( nId >= IDC_BUTTON_SEL1 && nId < IDC_BUTTON_SEL1 + MAX_CTRLS) { + int nBtnId = nId ; + int nEditId = IDC_EDIT1 + ( nId - IDC_BUTTON_SEL1) ; // se il bottone premuto è lo stesso if ( s_nDlgItem == nEditId) { // se selezione attiva, allora deve terminare @@ -466,7 +470,7 @@ CALLBACK DialogBoxProc( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam // se selezione non attiva, allora viene attivata else { SetDlgItemText( hwndDlg, nBtnId, strztoW( "S")) ; - StartSelectionMode( hwndDlg, nEditId, nActiveRow) ; + StartSelectionMode( hwndDlg, nEditId) ; } } // se nuovo buttone premuto @@ -481,14 +485,13 @@ CALLBACK DialogBoxProc( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam s_nDlgItem = nEditId ; SetDlgItemText( hwndDlg, nBtnId, strztoW( "S")) ; // il dialogo diventa "Modeless" temporaneamente - s_nActiveRow = nEditId - IDC_EDIT1 ; - StartSelectionMode( hwndDlg, nEditId, nActiveRow) ; + StartSelectionMode( hwndDlg, nEditId) ; } - return TRUE ; // !<-- esco + return TRUE ; } // --- se comando di Preview per brush di colori mediante click del mouse - else if ( code == STN_CLICKED && id >= IDC_COLOR1 && id <= IDC_COLOR8) { + else if ( nCode == STN_CLICKED && nId >= IDC_COLOR1 && nId <= IDC_COLOR8) { // se selezione attiva, allora deve terminare if ( s_bSelectionMode) { // se esisteva una selezione precedente, questa viene annullata @@ -499,7 +502,7 @@ CALLBACK DialogBoxProc( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam } EndSelectionMode( hwndDlg) ; } - int idx = id - IDC_COLOR1 ; + int idx = nId - IDC_COLOR1 ; HWND hwndPreview = GetDlgItem( hwndDlg, IDC_COLOR1 + idx) ; if ( ! hwndPreview) return TRUE ; @@ -527,87 +530,64 @@ CALLBACK DialogBoxProc( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam InvalidateRect( hwndPreview, NULL, TRUE) ; UpdateWindow( hwndPreview) ; } - return TRUE ; // !<-- Esco + return TRUE ; } switch ( LOWORD( wParam)) { case IDOK : - { - // recupero il contesto impostato alla creazione della finestra (durante esecuzione WM_INITDIALOG) - DialogContext* pdcCtx = (DialogContext*)GetWindowLongPtr( hwndDlg, GWLP_USERDATA) ; - if ( pdcCtx != nullptr) { - // inizializzo i risultati - pdcCtx->vsResult.clear() ; - pdcCtx->vsResult.reserve( s_nCtrls) ; - // recupero i valori degli Item - for ( int i = 0 ; i < s_nCtrls ; ++ i) { - AtoWEX<128> wsEdit( "") ; - s_sEdit[i] = "" ; - switch ( s_nType[i]) { - case CTRL_CHECK : - s_sEdit[i] = ( IsDlgButtonChecked( hwndDlg, IDC_CHECK1 + i) == BST_CHECKED ? "1" : "0") ; - break ; - case CTRL_COMBO : - if ( GetDlgItemText( hwndDlg, IDC_COMBO1 + i, LPWSTR( wsEdit), 128) > 0) - s_sEdit[i] = wstrztoA( wsEdit) ; - break ; - case CTRL_EDIT : - if ( GetDlgItemText( hwndDlg, IDC_EDIT1 + i, LPWSTR( wsEdit), 128) > 0) - s_sEdit[i] = wstrztoA( wsEdit) ; - break ; - case CTRL_COLOR : - { - COLORREF col = selectedColor[i] ; - BYTE r = GetRValue( col), g = GetGValue( col), b = GetBValue( col) ; - char buf[16] ; - sprintf_s( buf, sizeof( buf), "%u,%u,%u", r, g, b) ; - s_sEdit[i] = buf ; - } - break ; - case CTRL_BUTTON : - if ( GetDlgItemText( hwndDlg, IDC_EDIT1 + i, LPWSTR( wsEdit), 128) > 0) - s_sEdit[i] = wstrztoA( wsEdit) ; - break ; - } - - // inserisco i valori all'interno della tabella Lua - pdcCtx->vsResult.push_back( s_sEdit[i]) ; + // recupero i valori degli Item + for ( int i = 0 ; i < s_nCtrls ; ++ i) { + AtoWEX<128> wsEdit( "") ; + s_sEdit[i] = "" ; + switch ( s_nType[i]) { + case CTRL_CHECK : + s_sEdit[i] = ( IsDlgButtonChecked( hwndDlg, IDC_CHECK1 + i) == BST_CHECKED ? "1" : "0") ; + break ; + case CTRL_COMBO : + if ( GetDlgItemText( hwndDlg, IDC_COMBO1 + i, LPWSTR( wsEdit), 128) > 0) + s_sEdit[i] = wstrztoA( wsEdit) ; + break ; + case CTRL_EDIT : + if ( GetDlgItemText( hwndDlg, IDC_EDIT1 + i, LPWSTR( wsEdit), 128) > 0) + s_sEdit[i] = wstrztoA( wsEdit) ; + break ; + case CTRL_COLOR : + { + COLORREF col = selectedColor[i] ; + BYTE r = GetRValue( col), g = GetGValue( col), b = GetBValue( col) ; + char buf[16] ; + sprintf_s( buf, sizeof( buf), "%u,%u,%u", r, g, b) ; + s_sEdit[i] = buf ; } - - pdcCtx->bCancelled = false ; - pdcCtx->bDone = true ; + break ; + case CTRL_BUTTON : + if ( GetDlgItemText( hwndDlg, IDC_EDIT1 + i, LPWSTR( wsEdit), 128) > 0) + s_sEdit[i] = wstrztoA( wsEdit) ; + break ; } - - // chiudo il dialogo - OnClosingDialog( hwndDlg) ; - return TRUE ; } - [[fallthrough]] ; + // chiudo il dialogo + OnClosingDialog( hwndDlg) ; + s_bOk = true ; + return TRUE ; case IDCANCEL : - { - // pulizia brush colori - for ( int i = 0 ; i < MAX_CTRLS ; ++ i) { - if ( hColorBrush[i] != NULL) { - DeleteObject( hColorBrush[i]) ; - hColorBrush[i] = NULL ; - } + // pulizia brush colori + for ( int i = 0 ; i < MAX_CTRLS ; ++ i) { + if ( hColorBrush[i] != NULL) { + DeleteObject( hColorBrush[i]) ; + hColorBrush[i] = NULL ; } - // recupero il contesto impostato alla creazione della finestra ( durante esecuzione WM_INITDIALOG) e se esiste lo rimuovo - DialogContext* pdcCtx = ( DialogContext*)GetWindowLongPtr( hwndDlg, GWLP_USERDATA) ; - if ( pdcCtx != nullptr) { - pdcCtx->bCancelled = true ; - pdcCtx->bDone = true ; - } - // chiudo il dialogo - OnClosingDialog( hwndDlg) ; - return TRUE ; } + // chiudo il dialogo + OnClosingDialog( hwndDlg) ; + s_bOk = false ; + return TRUE ; } // se siamo in modalità di Selezione e il comando attivo non si riferisce ad un button di Selezione (SB), il dialogo torna modale if ( s_bSelectionMode) { - bool bIsBS = ( id >= IDC_BUTTON_SEL1 && id < IDC_BUTTON_SEL1 + MAX_CTRLS) ; - bool bIsActiveEdit = ( id == IDC_EDIT1 + s_nActiveRow) ; + bool bIsBS = ( nId >= IDC_BUTTON_SEL1 && nId < IDC_BUTTON_SEL1 + MAX_CTRLS) ; + bool bIsActiveEdit = ( nId == s_nDlgItem) ; // il dialogo torna modale if ( ! bIsBS && ! bIsActiveEdit) EndSelectionMode( hwndDlg) ; @@ -616,17 +596,10 @@ CALLBACK DialogBoxProc( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam } case WM_CLOSE : - { - // recupero il contesto impostato alla creazione della finestra ( durante esecuzione WM_INITDIALOG) e se esiste lo rimuovo - DialogContext* pdcCtx = (DialogContext*)GetWindowLongPtr( hwndDlg, GWLP_USERDATA) ; - if ( pdcCtx != nullptr) { - pdcCtx->bCancelled = true ; - pdcCtx->bDone = true ; - } - // chiudo il dialogo - OnClosingDialog( hwndDlg) ; - return TRUE ; - } + // chiudo il dialogo + OnClosingDialog( hwndDlg) ; + s_bOk = false ; + return TRUE ; } return FALSE ; @@ -641,10 +614,10 @@ UpdateDialogIds( void) return false ; // recupero tutti gli elementi selezionati - string sIds ; + string sIds = "" ; int nId = ExeGetFirstSelectedObj() ; while ( nId != GDB_ID_NULL) { - sIds.append( ToString( nId) + ",") ; + sIds += ToString( nId) + "," ; nId = ExeGetNextSelectedObj() ; } if ( ! sIds.empty()) @@ -736,14 +709,12 @@ LuaDialogBox( lua_State* L) return 1 ; } - // definizione del contesto - DialogContext dcCtx ; - // creazione di un dialogo Modeless HWND hMainWnd = ExeGetMainWindowHandle() ; + s_bOk = false ; + s_bSelectionMode = false ; s_nDlgItem = -1 ; - s_hDlg = ( CreateDialogParam( GetModuleIstance(), MAKEINTRESOURCE( IDD_LUADLG), hMainWnd, - (DLGPROC)DialogBoxProc, (LPARAM) &dcCtx)) ; + s_hDlg = ( CreateDialogParam( GetModuleIstance(), MAKEINTRESOURCE( IDD_LUADLG), hMainWnd, (DLGPROC)DialogBoxProc, NULL)) ; if ( s_hDlg == NULL) { LuaSetParam( L) ; return 1 ; @@ -760,8 +731,8 @@ LuaDialogBox( lua_State* L) GetWindowRect( pGseCtx->m_hWnd, &rctWnd) ; // message Loop - MSG msg ; - while ( ! dcCtx.bDone && IsWindow( s_hDlg)) { + while ( IsWindow( s_hDlg)) { + MSG msg ; if ( GetMessage( &msg, NULL, 0, 0) <= 0) break ; if ( ! IsDialogMessage( s_hDlg, &msg) && @@ -772,9 +743,13 @@ LuaDialogBox( lua_State* L) } // ritorno i valori lua - if ( dcCtx.bCancelled) - LuaSetParam( L) ; + if ( s_bOk) { + STRVECTOR vRes ; + for ( int i = 0 ; i < s_nCtrls ; ++ i) + vRes.emplace_back( s_sEdit[i]) ; + LuaSetParam( L, vRes) ; + } else - LuaSetParam( L, dcCtx.vsResult) ; + LuaSetParam( L) ; return 1 ; }