Subversion Repositories Games.Chess Giants

Rev

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