Subversion Repositories Games.Chess Giants

Rev

Rev 1 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // dialog_changeappearance.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // dialog template
  7. #define THIS_DIALOG DIALOG_CHANGEAPPEARANCE
  8.  
  9.  
  10. // global variables
  11. static wchar_t ofn_customfilter[256] = L"";
  12.  
  13.  
  14. // prototypes of local functions
  15. static void StartThread_ThisDialog (void *thread_parms);
  16. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  17. static void StartThread_DialogBoxLoad (void *thread_parms);
  18.  
  19.  
  20. void DialogBox_ChangeAppearance (void)
  21. {
  22.    // helper function to fire up the modeless dialog box
  23.  
  24.    _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
  25.  
  26.    return; // return as soon as the thread is fired up
  27. }
  28.  
  29.  
  30. void DialogBox_ChangeAppearance_Validated (void)
  31. {
  32.    // callback function called by the main game thread when the dialog box is validated
  33.  
  34.    // remember this callback is no longer to be called
  35.    is_dialogbox_changeappearance_validated = false;
  36.  
  37.    return; // finished
  38. }
  39.  
  40.  
  41. static void StartThread_ThisDialog (void *thread_parms)
  42. {
  43.    // this function runs in a separate thread, for that's the only way (seemingly)
  44.    // to implement a non-modal message box using the Common Controls library.
  45.  
  46.    // display the dialog box
  47.    if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
  48.       is_dialogbox_changeappearance_validated = true;
  49.  
  50.    the_board.reevaluate = true; // refresh the GUI buttons if needed
  51.    return; // _endthread() implied
  52. }
  53.  
  54.  
  55. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  56. {
  57.    // message handler for the dialog box
  58.  
  59.    unsigned short wParam_hiword;
  60.    unsigned short wParam_loword;
  61.    HWND hComboBoxWnd;
  62.    int theme_index;
  63.  
  64.    // filter out the commonly used message values
  65.    wParam_hiword = HIWORD (wParam);
  66.    wParam_loword = LOWORD (wParam);
  67.  
  68.    // have we just fired up this window ?
  69.    if (message == WM_INITDIALOG)
  70.    {
  71.       // center the window
  72.       CenterWindow (hWnd, hMainWnd);
  73.  
  74.       // set dialog icons (small one for title bar & big one for task manager)
  75.       SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  76.       SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  77.  
  78.       // set window title and control texts
  79.       SetWindowText (hWnd, LOCALIZE (L"ChangeAppearance_Title"));
  80.       Static_SetText (GetDlgItem (hWnd, GROUPBOX_TABLESKIN), LOCALIZE (L"ChangeAppearance_TableSkin"));
  81.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_CHANGEAPPEARANCE_THEMEINFO), (theme->description != NULL ? theme->description : L""));
  82.       SetWindowText (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWGRID), LOCALIZE (L"ChangeAppearance_ShowGrid"));
  83.       SetWindowText (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWICONSRATHER), LOCALIZE (L"ChangeAppearance_ShowIconsRather"));
  84.       SetWindowText (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_CUSTOMBACKGROUND), LOCALIZE (L"ChangeAppearance_CustomPicture"));
  85.       SetWindowText (GetDlgItem (hWnd, BUTTON_BROWSE), LOCALIZE (L"Button_Browse"));
  86.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_CHANGEAPPEARANCE_STATUSBAR), LOCALIZE (L"ChangeAppearance_StatusBar"));
  87.  
  88.       // get a quick access to the themes combo box
  89.       hComboBoxWnd = GetDlgItem (hWnd, COMBOBOX_THEMES);
  90.  
  91.       // populate the themes combo box
  92.       ComboBox_ResetContent (hComboBoxWnd);
  93.       for (theme_index = 0; theme_index < theme_count; theme_index++)
  94.          ComboBox_AddString (hComboBoxWnd, themes[theme_index].name);
  95.  
  96.       // cycle through all the themes now...
  97.       for (theme_index = 0; theme_index < theme_count; theme_index++)
  98.          if (_wcsicmp (themes[theme_index].name, theme->name) == 0)
  99.          {
  100.             ComboBox_SetCurSel (hComboBoxWnd, theme_index); // put the selection back
  101.             break; // and stop searching as soon as we've found it again
  102.          }
  103.  
  104.       // initialize the checkboxes
  105.       Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWGRID), (want_grid ? BST_CHECKED : BST_UNCHECKED));
  106.       Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWICONSRATHER), (want_flaticons ? BST_CHECKED : BST_UNCHECKED));
  107.       Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_CUSTOMBACKGROUND), (want_custombackground ? BST_CHECKED : BST_UNCHECKED));
  108.  
  109.       // enable or disable the browse button based on whether we want a custom background or not
  110.       EnableWindow (GetDlgItem (hWnd, BUTTON_BROWSE), want_custombackground);
  111.    }
  112.  
  113.    // else did we click the close button on the title bar ?
  114.    else if (message == WM_CLOSE)
  115.       EndDialog (hWnd, 0); // close the dialog box
  116.  
  117.    // else did we take action on one of the controls ?
  118.    else if (message == WM_COMMAND)
  119.    {
  120.       // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
  121.       if (wParam_loword == IDCANCEL)
  122.          EndDialog (hWnd, 0); // close the dialog box
  123.  
  124.       // else given the control that's clicked, update the relevant skin
  125.       else if ((wParam_loword == COMBOBOX_THEMES) && (wParam_hiword == CBN_SELCHANGE))
  126.       {
  127.          theme_index = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_THEMES));
  128.          if ((theme_index >= 0) && (theme_index < theme_count))
  129.          {
  130.             while (!themes[theme_index].is_loaded)
  131.                Sleep (100); // wait until theme is fully loaded
  132.             theme = &themes[theme_index]; // update the selected theme (WARNING: race condition ?)
  133.  
  134.             // update the theme info text
  135.             Static_SetText (GetDlgItem (hWnd, STATICTEXT_CHANGEAPPEARANCE_THEMEINFO), (theme->description != NULL ? theme->description : L""));
  136.          }
  137.          the_scene.update = true; // redraw scene
  138.       }
  139.       else if (wParam_loword == CHECKBOX_CHANGEAPPEARANCE_SHOWGRID)
  140.       {
  141.          want_grid = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWGRID)) != 0);
  142.          the_scene.update = true; // redraw scene
  143.       }
  144.       else if (wParam_loword == CHECKBOX_CHANGEAPPEARANCE_SHOWICONSRATHER)
  145.       {
  146.          want_flaticons = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWICONSRATHER)) != 0);
  147.          the_scene.update = true; // redraw scene
  148.       }
  149.       else if (wParam_loword == CHECKBOX_CHANGEAPPEARANCE_CUSTOMBACKGROUND)
  150.       {
  151.          want_custombackground = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_CUSTOMBACKGROUND)) != 0);
  152.          EnableWindow (GetDlgItem (hWnd, BUTTON_BROWSE), want_custombackground);
  153.          the_scene.update = true; // redraw scene
  154.       }
  155.  
  156.       // else did we click the browse button ?
  157.       else if (wParam_loword == BUTTON_BROWSE)
  158.          _beginthread (StartThread_DialogBoxLoad, 0, (void *) hWnd); // fire up the load dialog box thread
  159.    }
  160.  
  161.    // call the default dialog message processing function to keep things going
  162.    return (false);
  163. }
  164.  
  165.  
  166. static void StartThread_DialogBoxLoad (void *thread_parms)
  167. {
  168.    // this function runs in a separate thread, for that's the only way (seemingly)
  169.    // to implement a non-modal Open dialog box using the Common Controls library.
  170.  
  171.    OPENFILENAME ofn;
  172.  
  173.    // reset and prepare the open file name structure
  174.    memset (&ofn, 0, sizeof (ofn));
  175.    ofn.lStructSize = sizeof (ofn);
  176.    ofn.hwndOwner = (HWND) thread_parms;
  177.    ofn.lpstrFilter = LOCALIZE (L"ImageFileFilter");
  178.    ofn.lpstrDefExt = L"jpg"; // default extension
  179.    ofn.lpstrCustomFilter = ofn_customfilter;
  180.    ofn.nMaxCustFilter = WCHAR_SIZEOF (ofn_customfilter);
  181.    ofn.nFilterIndex = 1; // first filter (list is 1-based)
  182.    ofn.lpstrFile = custombackground_pathname;
  183.    ofn.nMaxFile = WCHAR_SIZEOF (custombackground_pathname);
  184.    ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  185.  
  186.    // display the dialog box and get the file name
  187.    if (GetOpenFileName (&ofn))
  188.       Background_LoadImage (&custombg, custombackground_pathname);
  189.  
  190.    the_scene.update = true; // redraw scene
  191.    return; // _endthread() implied
  192. }
  193.