KeosWin32ConfigDialog.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 #include "KeosWin32ConfigDialog.h"
00022 #include "KeosRoot.h"
00023 #include "KeosRenderSystem.h"
00024 #include "resource.h"
00025 #include "KeosException.h"
00026 
00027 namespace
00028 {
00029   Keos::CWin32ConfigDialog* dlg;  // This is a pointer to instance, since this is a static member
00030 }
00031 
00032 namespace Keos
00033 {
00034   //=======================================================================
00035   // CWin32ConfigDialog implementation
00036   //=======================================================================
00037 
00038   //-----------------------------------------------------------------------
00039   CWin32ConfigDialog::CWin32ConfigDialog(HINSTANCE hInst)
00040   {
00041     m_HInstance = hInst;
00042     m_pSelectedRenderSystem = 0;
00043   }
00044 
00045   //-----------------------------------------------------------------------
00046   CWin32ConfigDialog::~CWin32ConfigDialog()
00047   {}
00048 
00049   //-----------------------------------------------------------------------
00050   BOOL CWin32ConfigDialog::DlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
00051   {
00052     HWND hwndDlgItem;
00053     static RenderSystemList* lstRend;
00054     RenderSystemList::iterator pRend;
00055     static ConfigOptionMap opts;
00056     String err;
00057 
00058     int i, sel, savedSel;
00059     static bool fullScreen = true;
00060 
00061     switch (iMsg)
00062     {
00063       case WM_INITDIALOG:
00064       {
00065         // Load saved settings
00066         CRoot::Instance().RestoreConfig();
00067         dlg->m_pSelectedRenderSystem = CRoot::Instance().GetRenderSystem();
00068         // Get all render systems
00069         lstRend = CRoot::Instance().GetAvailableRenderers();
00070         pRend = lstRend->begin();
00071         i = 0;
00072         while (pRend != lstRend->end())
00073         {
00074           hwndDlgItem = GetDlgItem(hDlg, IDC_CBO_RENDERSYSTEM);
00075 
00076           SendMessage(hwndDlgItem, CB_ADDSTRING, 0,
00077                       (LPARAM)(char*)(*pRend)->GetName().c_str());
00078 
00079           if (*pRend == dlg->m_pSelectedRenderSystem)
00080           {
00081             // Select
00082             SendMessage(hwndDlgItem, CB_SETCURSEL, (WPARAM)i, 0);
00083             // Refresh Options
00084             // Get options from render system
00085             opts = (*pRend)->GetConfigOptions();
00086             // Reset list box
00087             hwndDlgItem = GetDlgItem(hDlg, IDC_LST_OPTIONS);
00088             //SendMessage(hwndDlgItem, LB_RESETCONTENT, 0, 0);
00089             // Iterate through options
00090             ConfigOptionMap::iterator pOpt = opts.begin();
00091             String strLine;
00092             while ( pOpt != opts.end() )
00093             {
00094               if (pOpt->second.name != "RenderSystem")
00095               {
00096                 strLine = pOpt->second.name + ": " + pOpt->second.currentValue;
00097                 SendMessage(hwndDlgItem, LB_ADDSTRING, 0, (LPARAM)strLine.c_str());
00098               }
00099               ++pOpt;
00100             }
00101           }
00102 
00103           ++pRend;
00104           ++i;
00105         }
00106 
00107         // Keos version
00108         hwndDlgItem = GetDlgItem(hDlg, IDC_LBL_VERSION);
00109         String strVersion = "v" + CRoot::Instance().GetVersion();
00110         SetWindowText(hwndDlgItem, strVersion.c_str());
00111 
00112         // Center myself
00113         int x, y, screenWidth, screenHeight;
00114         RECT rcDlg;
00115         GetWindowRect(hDlg, &rcDlg);
00116         screenWidth = GetSystemMetrics(SM_CXFULLSCREEN);
00117         screenHeight = GetSystemMetrics(SM_CYFULLSCREEN);
00118 
00119         x = (screenWidth / 2) - ((rcDlg.right - rcDlg.left) / 2);
00120         y = (screenHeight / 2) - ((rcDlg.bottom - rcDlg.top) / 2);
00121 
00122         MoveWindow(hDlg, x, y, (rcDlg.right - rcDlg.left),
00123                    (rcDlg.bottom - rcDlg.top), TRUE);
00124 
00125         return TRUE;
00126       }
00127 
00128       case WM_COMMAND:
00129         switch (LOWORD(wParam))
00130         {
00131           case IDC_CBO_RENDERSYSTEM:
00132             hwndDlgItem = GetDlgItem(hDlg, IDC_CBO_RENDERSYSTEM);
00133             sel = SendMessage( hwndDlgItem, CB_GETCOUNT, 0, 0 );
00134 
00135             if (HIWORD(wParam) == CBN_SELCHANGE )
00136             {
00137               // Change label and combo "option"
00138               hwndDlgItem = GetDlgItem(hDlg, IDC_CBO_OPTION);
00139               SendMessage(hwndDlgItem, CB_RESETCONTENT, 0, 0);
00140               hwndDlgItem = GetDlgItem(hDlg, IDC_LBL_OPTION);
00141               SetWindowText(hwndDlgItem, "[Click On An Option]:");
00142 
00143               // RenderSystem selected
00144               // Get selected index
00145               hwndDlgItem = GetDlgItem(hDlg, IDC_CBO_RENDERSYSTEM);
00146               sel = SendMessage(hwndDlgItem, CB_GETCURSEL, 0, 0);
00147               if (sel != -1)
00148               {
00149                 // Get RenderSystem selected
00150                 pRend = lstRend->begin();
00151                 dlg->m_pSelectedRenderSystem = pRend[sel];
00152                 // refresh options
00153                 // Get options from render system
00154                 opts = pRend[sel]->GetConfigOptions();
00155                 // Reset list box
00156                 hwndDlgItem = GetDlgItem(hDlg, IDC_LST_OPTIONS);
00157                 SendMessage(hwndDlgItem, LB_RESETCONTENT, 0, 0);
00158                 // Iterate through options
00159                 ConfigOptionMap::iterator pOpt = opts.begin();
00160                 String strLine;
00161                 while (pOpt != opts.end())
00162                 {
00163                   if (pOpt->second.name != "RenderSystem")
00164                   {
00165                     strLine = pOpt->second.name + ": " + pOpt->second.currentValue;
00166                     SendMessage(hwndDlgItem, LB_ADDSTRING, 0, (LPARAM)strLine.c_str());
00167                   }
00168                   ++pOpt;
00169                 }
00170               }
00171             }
00172 
00173             return TRUE;
00174 
00175           case IDC_LST_OPTIONS:
00176             if (HIWORD(wParam) == LBN_SELCHANGE)
00177             {
00178               // Selection in list box of options changed
00179               // Update combo and label in edit section
00180               hwndDlgItem = GetDlgItem(hDlg, IDC_LST_OPTIONS);
00181               sel = SendMessage(hwndDlgItem, LB_GETCURSEL, 0, 0);
00182               if (sel != -1)
00183               {
00184                 ConfigOptionMap::iterator pOpt = opts.begin();
00185                 for (i = 0; i < sel; i++)
00186                   ++pOpt;
00187                 // Set label text
00188                 hwndDlgItem = GetDlgItem(hDlg, IDC_LBL_OPTION);
00189                 SetWindowText(hwndDlgItem, pOpt->second.name.c_str());
00190                 // Set combo options
00191                 hwndDlgItem = GetDlgItem(hDlg, IDC_CBO_OPTION);
00192                 SendMessage(hwndDlgItem, CB_RESETCONTENT, 0, 0);
00193                 StringVector::iterator pPoss = pOpt->second.possibleValues.begin();
00194                 i = 0;
00195                 while (pPoss != pOpt->second.possibleValues.end())
00196                 {
00197                   SendMessage(hwndDlgItem, CB_ADDSTRING, 0, (LPARAM)pPoss[0].c_str());
00198                   if (pPoss[0] == pOpt->second.currentValue)
00199                     // Select current value
00200                     SendMessage(hwndDlgItem, CB_SETCURSEL, (WPARAM)i, 0);
00201                   ++pPoss;
00202                   ++i;
00203                 }
00204                 // Enable/disable combo depending on (not)immutable
00205                 EnableWindow(hwndDlgItem, !(pOpt->second.immutable));
00206               }
00207             }
00208 
00209             return TRUE;
00210 
00211           case IDC_CBO_OPTION:
00212             if (HIWORD(wParam) == CBN_SELCHANGE)
00213             {
00214               // Updated an option
00215               // Get option
00216               hwndDlgItem = GetDlgItem(hDlg, IDC_LST_OPTIONS);
00217               sel = SendMessage(hwndDlgItem, LB_GETCURSEL, 0, 0);
00218               savedSel = sel;
00219               ConfigOptionMap::iterator pOpt = opts.begin();
00220               for (i = 0; i < sel; i++)
00221                 ++pOpt;
00222               // Get selected value
00223               hwndDlgItem = GetDlgItem(hDlg, IDC_CBO_OPTION);
00224               sel = SendMessage(hwndDlgItem, CB_GETCURSEL, 0, 0);
00225 
00226               if (sel != -1)
00227               {
00228                 StringVector::iterator pPoss = pOpt->second.possibleValues.begin();
00229 
00230                 // Set option
00231                 dlg->m_pSelectedRenderSystem->SetConfigOption(
00232                   pOpt->second.name, pPoss[sel]);
00233                 // Re-retrieve options
00234                 opts = dlg->m_pSelectedRenderSystem->GetConfigOptions();
00235 
00236                 // Reset options list box
00237                 hwndDlgItem = GetDlgItem(hDlg, IDC_LST_OPTIONS);
00238                 SendMessage(hwndDlgItem, LB_RESETCONTENT, 0, 0);
00239                 // Iterate through options
00240                 pOpt = opts.begin();
00241                 String strLine;
00242                 while (pOpt != opts.end())
00243                 {
00244                   if (pOpt->second.name != "RenderSystem")
00245                   {
00246                     strLine = pOpt->second.name + ": " + pOpt->second.currentValue;
00247                     SendMessage(hwndDlgItem, LB_ADDSTRING, 0, (LPARAM)strLine.c_str());
00248                   }
00249                   ++pOpt;
00250                 }
00251                 // Select previously selected item
00252                 SendMessage(hwndDlgItem, LB_SETCURSEL, savedSel, 0);
00253               }
00254 
00255             }
00256             return TRUE;
00257 
00258           case IDOK:
00259             // Set render system
00260             if (!dlg->m_pSelectedRenderSystem)
00261             {
00262               MessageBox(NULL, "Please choose a rendering system.", "KEOS", MB_OK | MB_ICONEXCLAMATION);
00263               return TRUE;
00264             }
00265 
00266             CRoot::Instance().SetRenderSystem(dlg->m_pSelectedRenderSystem);
00267             CRoot::Instance().SaveConfig();
00268 
00269             EndDialog(hDlg, TRUE);
00270             return TRUE;
00271 
00272           case IDCANCEL:
00273             EndDialog(hDlg, FALSE);
00274             return TRUE;
00275         }
00276     }
00277 
00278     return FALSE;
00279   }
00280 
00281   //-----------------------------------------------------------------------
00282   bool CWin32ConfigDialog::Display(void)
00283   {
00284     // Display dialog
00285     // Don't return to caller until dialog dismissed
00286     int i;
00287     dlg = this;
00288     i = DialogBox(m_HInstance, MAKEINTRESOURCE(IDD_DIALOG_CONFIG), NULL, DlgProc);
00289 
00290     if (i == -1)
00291     {
00292       int winError = GetLastError();
00293       char* errDesc;
00294       int i;
00295 
00296       errDesc = new char[255];
00297       // Try windows errors first
00298       i = FormatMessage(
00299             FORMAT_MESSAGE_FROM_SYSTEM |
00300             FORMAT_MESSAGE_IGNORE_INSERTS,
00301             NULL,
00302             winError,
00303             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00304             (LPTSTR) errDesc,
00305             255,
00306             NULL
00307           );
00308 
00309       throw CException("CWin32ConfigDialog::Display");
00310     }
00311     if (i)
00312       return true;
00313     else
00314       return false;
00315   }
00316 } // namespace Keos
00317 

Generated on Fri Mar 9 14:29:03 2007 for Keos by  doxygen 1.5.1-p1