// dialog_changeappearance.cpp
 
 
 
#include "../common.h"
 
 
 
 
 
// dialog template
 
#define THIS_DIALOG DIALOG_CHANGEAPPEARANCE
 
 
 
 
 
// global variables
 
static wchar_t ofn_customfilter[256] = L"";
 
 
 
 
 
// prototypes of local functions
 
static void StartThread_ThisDialog (void *thread_parms);
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
static void StartThread_DialogBoxLoad (void *thread_parms);
 
 
 
 
 
void DialogBox_ChangeAppearance (void)
 
{
 
   // helper function to fire up the modeless dialog box
 
 
 
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
 
 
 
   return; // return as soon as the thread is fired up
 
}
 
 
 
 
 
void DialogBox_ChangeAppearance_Validated (void)
 
{
 
   // callback function called by the main game thread when the dialog box is validated
 
 
 
   // remember this callback is no longer to be called
 
   is_dialogbox_changeappearance_validated = false;
 
 
 
   return; // finished
 
}
 
 
 
 
 
static void StartThread_ThisDialog (void *thread_parms)
 
{
 
   // this function runs in a separate thread, for that's the only way (seemingly)
 
   // to implement a non-modal message box using the Common Controls library.
 
 
 
   // display the dialog box
 
   if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
 
      is_dialogbox_changeappearance_validated = true;
 
 
 
   return; // _endthread() implied
 
}
 
 
 
 
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
 
{
 
   // message handler for the dialog box
 
 
 
   unsigned short wParam_hiword;
 
   unsigned short wParam_loword;
 
   HWND hComboBoxWnd;
 
   int theme_index;
 
 
 
   // filter out the commonly used message values
 
   wParam_hiword = HIWORD (wParam);
 
   wParam_loword = LOWORD (wParam);
 
 
 
   // have we just fired up this window ?
 
   if (message == WM_INITDIALOG)
 
   {
 
      // center the window
 
      CenterWindow (hWnd, hMainWnd);
 
 
 
      // set dialog icons (small one for title bar & big one for task manager)
 
      SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
 
      SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
 
 
 
      // set window title and control texts
 
      SetWindowText (hWnd, LOCALIZE (L"ChangeAppearance_Title"));
 
      Static_SetText (GetDlgItem (hWnd, GROUPBOX_TABLESKIN), LOCALIZE (L"ChangeAppearance_TableSkin"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_CHANGEAPPEARANCE_THEMEINFO), (theme->description != NULL ? theme->description : L""));
 
      SetWindowText (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWGRID), LOCALIZE (L"ChangeAppearance_ShowGrid"));
 
      SetWindowText (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWICONSRATHER), LOCALIZE (L"ChangeAppearance_ShowIconsRather"));
 
      SetWindowText (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_CUSTOMBACKGROUND), LOCALIZE (L"ChangeAppearance_CustomPicture"));
 
      SetWindowText (GetDlgItem (hWnd, BUTTON_BROWSE), LOCALIZE (L"Button_Browse"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_CHANGEAPPEARANCE_STATUSBAR), LOCALIZE (L"ChangeAppearance_StatusBar"));
 
 
 
      // get a quick access to the themes combo box
 
      hComboBoxWnd = GetDlgItem (hWnd, COMBOBOX_THEMES);
 
 
 
      // populate the themes combo box
 
      ComboBox_ResetContent (hComboBoxWnd);
 
      for (theme_index = 0; theme_index < theme_count; theme_index++)
 
         ComboBox_AddString (hComboBoxWnd, themes[theme_index].name);
 
 
 
      // cycle through all the themes now...
 
      for (theme_index = 0; theme_index < theme_count; theme_index++)
 
         if (_wcsicmp (themes[theme_index].name, theme->name) == 0)
 
         {
 
            ComboBox_SetCurSel (hComboBoxWnd, theme_index); // put the selection back
 
            break; // and stop searching as soon as we've found it again
 
         }
 
 
 
      // initialize the checkboxes
 
      Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWGRID), (want_grid ? BST_CHECKED : BST_UNCHECKED));
 
      Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWICONSRATHER), (want_flaticons ? BST_CHECKED : BST_UNCHECKED));
 
      Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_CUSTOMBACKGROUND), (want_custombackground ? BST_CHECKED : BST_UNCHECKED));
 
 
 
      // enable or disable the browse button based on whether we want a custom background or not
 
      EnableWindow (GetDlgItem (hWnd, BUTTON_BROWSE), want_custombackground);
 
   }
 
 
 
   // else did we click the close button on the title bar ?
 
   else if (message == WM_CLOSE)
 
      EndDialog (hWnd, 0); // close the dialog box
 
 
 
   // else did we take action on one of the controls ?
 
   else if (message == WM_COMMAND)
 
   {
 
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
 
      if (wParam_loword == IDCANCEL)
 
         EndDialog (hWnd, 0); // close the dialog box
 
 
 
      // else given the control that's clicked, update the relevant skin
 
      else if ((wParam_loword == COMBOBOX_THEMES) && (wParam_hiword == CBN_SELCHANGE))
 
      {
 
         theme_index = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_THEMES));
 
         if ((theme_index >= 0) && (theme_index < theme_count))
 
         {
 
            while (!themes[theme_index].is_loaded)
 
               Sleep (100); // wait until theme is fully loaded
 
            theme = &themes[theme_index]; // update the selected theme (WARNING: race condition ?)
 
 
 
            // update the theme info text
 
            Static_SetText (GetDlgItem (hWnd, STATICTEXT_CHANGEAPPEARANCE_THEMEINFO), (theme->description != NULL ? theme->description : L""));
 
         }
 
         the_scene.update = true; // redraw scene
 
      }
 
      else if (wParam_loword == CHECKBOX_CHANGEAPPEARANCE_SHOWGRID)
 
      {
 
         want_grid = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWGRID)) != 0);
 
         the_scene.update = true; // redraw scene
 
      }
 
      else if (wParam_loword == CHECKBOX_CHANGEAPPEARANCE_SHOWICONSRATHER)
 
      {
 
         want_flaticons = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_SHOWICONSRATHER)) != 0);
 
         the_scene.update = true; // redraw scene
 
      }
 
      else if (wParam_loword == CHECKBOX_CHANGEAPPEARANCE_CUSTOMBACKGROUND)
 
      {
 
         want_custombackground = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_CHANGEAPPEARANCE_CUSTOMBACKGROUND)) != 0);
 
         EnableWindow (GetDlgItem (hWnd, BUTTON_BROWSE), want_custombackground);
 
         the_scene.update = true; // redraw scene
 
      }
 
 
 
      // else did we click the browse button ?
 
      else if (wParam_loword == BUTTON_BROWSE)
 
         _beginthread (StartThread_DialogBoxLoad, 0, (void *) hWnd); // fire up the load dialog box thread
 
   }
 
 
 
   // call the default dialog message processing function to keep things going
 
   return (false);
 
}
 
 
 
 
 
static void StartThread_DialogBoxLoad (void *thread_parms)
 
{
 
   // this function runs in a separate thread, for that's the only way (seemingly)
 
   // to implement a non-modal Open dialog box using the Common Controls library.
 
 
 
   OPENFILENAME ofn;
 
 
 
   // reset and prepare the open file name structure
 
   memset (&ofn, 0, sizeof (ofn));
 
   ofn.lStructSize = sizeof (ofn);
 
   ofn.hwndOwner = (HWND) thread_parms;
 
   ofn.lpstrFilter = LOCALIZE (L"ImageFileFilter");
 
   ofn.lpstrDefExt = L"jpg"; // default extension
 
   ofn.lpstrCustomFilter = ofn_customfilter;
 
   ofn.nMaxCustFilter = WCHAR_SIZEOF (ofn_customfilter);
 
   ofn.nFilterIndex = 1; // first filter (list is 1-based)
 
   ofn.lpstrFile = custombackground_pathname;
 
   ofn.nMaxFile = WCHAR_SIZEOF (custombackground_pathname);
 
   ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
 
 
 
   // display the dialog box and get the file name
 
   if (GetOpenFileName (&ofn))
 
      Background_LoadImage (&custombg, custombackground_pathname);
 
 
 
   the_scene.update = true; // redraw scene
 
   return; // _endthread() implied
 
}