source: branches/openyahtzee-1.8/src/MainFrame.cpp @ 145

Last change on this file since 145 was 145, checked in by guyru, 6 years ago

fixed bug #1836473 (wrong title caption)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.6 KB
Line 
1// $Header$
2/***************************************************************************
3 *   Copyright (C) 2006 by Guy Rutenberg   *
4 *   guyrutenberg@gmail.com   *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 *   This program is distributed in the hope that it will be useful,       *
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14 *   GNU General Public License for more details.                          *
15 *                                                                         *
16 *   You should have received a copy of the GNU General Public License     *
17 *   along with this program; if not, write to the                         *
18 *   Free Software Foundation, Inc.,                                       *
19 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20 ***************************************************************************/
21
22 /***********************************************
23 *      This File contains the definitions      *
24 *      of MainFrame's functions                *
25 ***********************************************/
26
27// #define DEBUG
28
29#include <wx/wx.h>
30
31#include "MainFrame.h"
32#include "wxDynamicBitmap.h"
33#include "ObjectsID.h"
34#include "HighScoreDialog.h"
35#include "SettingsDialog.h"
36#include "About.h"
37#include "UtilityFunctions.h"
38#include <iostream>
39#include <sstream>
40#include <cstdlib>
41#include <wx/version.h>
42
43//include the images for the dice
44#include "one.xpm"
45#include "two.xpm"
46#include "three.xpm"
47#include "four.xpm"
48#include "five.xpm"
49#include "six.xpm"
50
51//include the icon file
52#include "Icon.h"
53
54//default values - design
55#define SPACE_SIZE 1
56#define DICE_SPACE 3
57#ifdef WIN32
58        #define KEEP_SPACE 13
59        #define VERTICAL_ROLL_SIZEX 64
60        #define VERTICAL_ROLL_SIZEY 53
61#else
62        #define KEEP_SPACE 5
63        #define VERTICAL_ROLL_SIZEX 64
64        #define VERTICAL_ROLL_SIZEY 64
65#endif
66#define VER_DICE_SPACER 10
67
68//default values - settings
69#define DEF_HIGHSCORESIZE 20
70#define OY_VERSION "1.8.0"
71
72DEFINE_EVENT_TYPE(wxEVT_ENABLE_ROLL)
73
74
75MainFrame::MainFrame(const wxString& title, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE)
76        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size, style)
77{
78        //give the frame an icon
79        SetIcon(wxIcon(ICON));
80
81
82        std::ostringstream sstr;
83
84        m_settingsdb = new SettingsDB(); //Get the settings database connection
85        m_highscoredb = new HighScoreTableDB();
86       
87        InitializeDatabase();//this must come _after_ m_settingsdb and m_highscoredb are created
88
89        bitmap_dices[0] = new wxBitmap(one_xpm);
90        bitmap_dices[1] = new wxBitmap(two_xpm);
91        bitmap_dices[2] = new wxBitmap(three_xpm);
92        bitmap_dices[3] = new wxBitmap(four_xpm);
93        bitmap_dices[4] = new wxBitmap(five_xpm);
94        bitmap_dices[5] = new wxBitmap(six_xpm);
95       
96        //randomize the random-number generator based on the time
97        srand( (unsigned)time( NULL ) );
98
99
100        /*****Create and initialize the menu bar******/
101
102        /*Create the menus*/
103        wxMenu *gameMenu = new wxMenu;  //create File menu
104        wxMenu *helpMenu = new wxMenu;  //create Help menu
105       
106       
107        //insert menu items into menu Help
108        helpMenu->Append(ID_CHECK_FOR_UPDATES, wxT("&Check for Updates"),
109                        wxT("Check for new version of the game via the web"));
110        helpMenu->AppendSeparator();
111        helpMenu->Append(ID_SENDCOMMENT, wxT("&Send a Comment to Developers"),
112                        wxT("Send a comment to the developers of the game"));
113        helpMenu->AppendSeparator();
114        helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
115                        wxT("Show about dialog"));
116
117        //insert menu items into menu File
118        gameMenu->Append(ID_NEWGAME,wxT("&New Game\tF2"),wxT("Start a new game"));
119        //create the undo button and make it disabled
120        gameMenu->Append(ID_UNDO,wxT("&Undo\tCTRL+Z"),wxT("Undo the last move"));
121        gameMenu->Append(ID_SHOWHIGHSCORE,wxT("High &Scores"),wxT("Show high-scores table"));
122        gameMenu->Append(ID_SETTINGS,wxT("Settings"),wxT("Show settings dialog"));
123        gameMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),
124                        wxT("Quit this program"));
125       
126        // Declare the menu-bar and append the freshly created menus to the menu bar...
127        wxMenuBar *menuBar = new wxMenuBar();
128        menuBar->Append(gameMenu, wxT("&Game"));
129        menuBar->Append(helpMenu, wxT("&Help"));
130       
131        // ... and attach this menu bar to the frame
132        SetMenuBar(menuBar);
133       
134        /***End menu-bar ***/
135        wxPanel* panel = new wxPanel(this, ID_PANEL,
136                wxDefaultPosition, wxDefaultSize);
137
138        wxBoxSizer *topSizer;
139        wxFlexGridSizer *diceSizer;
140
141        if (m_horizontallayout) {
142                topSizer = new wxBoxSizer( wxVERTICAL );
143                sectionsSizer = new wxBoxSizer( wxHORIZONTAL );
144                diceSizer = new wxFlexGridSizer(2, 0, 0, 0);
145        } else {
146                topSizer = new wxBoxSizer( wxHORIZONTAL );
147                sectionsSizer = new wxBoxSizer( wxVERTICAL );
148                diceSizer = new wxFlexGridSizer(1, 0, 0);;
149        }
150
151        uppersection = new wxStaticBoxSizer( new wxStaticBox( panel, wxID_ANY, wxT("Upper Section") ), wxVERTICAL);
152       
153lowersection = new wxStaticBoxSizer( new wxStaticBox( panel, wxID_ANY, wxT("Lower Section") ), wxVERTICAL);
154       
155        wxFlexGridSizer* uppergrid = new wxFlexGridSizer(2, 0, 10);
156        wxFlexGridSizer* lowergrid = new wxFlexGridSizer(2, 0, 10);
157
158        //BEGIN layout for the upper section of the score board
159        uppergrid->Add(new wxButton(panel,ID_ACES,wxT("Aces")),0,wxALL,SPACE_SIZE);
160        uppergrid->Add(new wxTextCtrl(panel, ID_ACESTEXT),1,wxALL,SPACE_SIZE);
161        uppergrid->Add(new wxButton(panel,ID_TWOS,wxT("Twos")),0,wxALL,SPACE_SIZE);
162        uppergrid->Add(new wxTextCtrl(panel, ID_TWOSTEXT),1,wxALL,SPACE_SIZE);
163        uppergrid->Add(new wxButton(panel,ID_THREES,wxT("Threes")),0,wxALL,SPACE_SIZE);
164        uppergrid->Add(new wxTextCtrl(panel, ID_THREESTEXT),1,wxALL,SPACE_SIZE);
165        uppergrid->Add(new wxButton(panel,ID_FOURS,wxT("Fours")),0,wxALL,SPACE_SIZE);
166        uppergrid->Add(new wxTextCtrl(panel, ID_FOURSTEXT),1,wxALL,SPACE_SIZE);
167        uppergrid->Add(new wxButton(panel,ID_FIVES,wxT("Fives")),0,wxALL,SPACE_SIZE);
168        uppergrid->Add(new wxTextCtrl(panel, ID_FIVESTEXT),1,wxALL,SPACE_SIZE);
169        uppergrid->Add(new wxButton(panel,ID_SIXES,wxT("Sixes")),0,wxALL,SPACE_SIZE);
170        uppergrid->Add(new wxTextCtrl(panel, ID_SIXESTEXT),1,wxALL,SPACE_SIZE);
171        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total score:")),0,wxALL,SPACE_SIZE);
172        uppergrid->Add(new wxTextCtrl(panel, ID_UPPERSECTIONTOTAL),1,wxALL,SPACE_SIZE);
173        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Bonus:")),0,wxALL,SPACE_SIZE);
174        uppergrid->Add(new wxTextCtrl(panel, ID_BONUS),1,wxALL,SPACE_SIZE);
175        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total of upper section:")),0,wxALL,SPACE_SIZE);
176        uppergrid->Add(new wxTextCtrl(panel, ID_UPPERTOTAL),1,wxALL,SPACE_SIZE);
177        //END layout for the upper section of the score board
178
179        //BEGIN layout for the lower section of the score board
180        lowergrid->Add(new wxButton(panel,ID_THREEOFAKIND,wxT("3 of a kind")),0,wxALL,SPACE_SIZE);
181        lowergrid->Add(new wxTextCtrl(panel, ID_THREEOFAKINDTEXT),1,wxALL,SPACE_SIZE);
182        lowergrid->Add(new wxButton(panel,ID_FOUROFAKIND,wxT("4 of a kind")),0,wxALL,SPACE_SIZE);
183        lowergrid->Add(new wxTextCtrl(panel, ID_FOUROFAKINDTEXT),1,wxALL,SPACE_SIZE);
184        lowergrid->Add(new wxButton(panel,ID_FULLHOUSE,wxT("Full House")),0,wxALL,SPACE_SIZE);
185        lowergrid->Add(new wxTextCtrl(panel, ID_FULLHOUSETEXT),1,wxALL,SPACE_SIZE);
186        lowergrid->Add(new wxButton(panel,ID_SMALLSEQUENCE,wxT("Sequence of 4")),0,wxALL,SPACE_SIZE);
187        lowergrid->Add(new wxTextCtrl(panel, ID_SMALLSEQUENCETEXT),1,wxALL,SPACE_SIZE);
188        lowergrid->Add(new wxButton(panel,ID_LARGESEQUENCE,wxT("Sequence of 5")),0,wxALL,SPACE_SIZE);
189        lowergrid->Add(new wxTextCtrl(panel, ID_LARGESEQUENCETEXT),1,wxALL,SPACE_SIZE);
190        lowergrid->Add(new wxButton(panel,ID_YAHTZEE,wxT("Yahtzee")),0,wxALL,SPACE_SIZE);
191        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEETEXT),1,wxALL,SPACE_SIZE);
192        lowergrid->Add(new wxButton(panel,ID_CHANCE,wxT("Chance")),0,wxALL,SPACE_SIZE);
193        lowergrid->Add(new wxTextCtrl(panel, ID_CHANCETEXT),1,wxALL,SPACE_SIZE);
194        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Yahtzee Bonus")),0,wxALL,SPACE_SIZE);
195        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEEBONUSTEXT),1,wxALL,SPACE_SIZE);
196        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total of lower section:")),0,wxALL,SPACE_SIZE);
197        lowergrid->Add(new wxTextCtrl(panel, ID_LOWERTOTAL),1,wxALL,SPACE_SIZE);
198        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Grand Total:")),0,wxALL,SPACE_SIZE);
199        lowergrid->Add(new wxTextCtrl(panel, ID_GRANDTOTAL),1,wxALL,SPACE_SIZE);
200        //END layout for the lower section of the score board
201
202        uppersection->Add(uppergrid);
203        lowersection->Add(lowergrid);
204        sectionsSizer->Add(uppersection,0,wxALL,5);
205        sectionsSizer->Add(lowersection,0,wxALL,5);
206
207        //BEGIN layout for the dice section of the score board
208        if (m_horizontallayout) {
209                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE1,*bitmap_dices[0]),0,wxALL,DICE_SPACE);
210                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE2,*bitmap_dices[1]),0,wxALL,DICE_SPACE);
211                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE3,*bitmap_dices[2]),0,wxALL,DICE_SPACE);
212                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE4,*bitmap_dices[3]),0,wxALL,DICE_SPACE);
213                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE5,*bitmap_dices[4]),0,wxALL,DICE_SPACE);
214                diceSizer->Add(new wxButton(panel, ID_ROLL, wxT("Roll!"),wxDefaultPosition,wxSize(64,64)),0,wxALL,DICE_SPACE);
215                diceSizer->Add(new wxCheckBox(panel, ID_DICE1KEEP, wxT("Keep")),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
216                diceSizer->Add(new wxCheckBox(panel, ID_DICE2KEEP, wxT("Keep")),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
217                diceSizer->Add(new wxCheckBox(panel, ID_DICE3KEEP, wxT("Keep")),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
218                diceSizer->Add(new wxCheckBox(panel, ID_DICE4KEEP, wxT("Keep")),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
219                diceSizer->Add(new wxCheckBox(panel, ID_DICE5KEEP, wxT("Keep")),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
220        } else {
221                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE1,*bitmap_dices[0]),0,wxALL,DICE_SPACE);
222                diceSizer->Add(new wxCheckBox(panel, ID_DICE1KEEP, wxT("Keep")),0,wxLEFT,KEEP_SPACE);
223                diceSizer->AddSpacer(VER_DICE_SPACER);
224                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE2,*bitmap_dices[1]),0,wxALL,DICE_SPACE);
225                diceSizer->Add(new wxCheckBox(panel, ID_DICE2KEEP, wxT("Keep")),0,wxLEFT,KEEP_SPACE);
226                diceSizer->AddSpacer(VER_DICE_SPACER);
227                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE3,*bitmap_dices[2]),0,wxALL,DICE_SPACE);
228                diceSizer->Add(new wxCheckBox(panel, ID_DICE3KEEP, wxT("Keep")),0,wxLEFT,KEEP_SPACE);
229                diceSizer->AddSpacer(VER_DICE_SPACER);
230                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE4,*bitmap_dices[3]),0,wxALL,DICE_SPACE);
231                diceSizer->Add(new wxCheckBox(panel, ID_DICE4KEEP, wxT("Keep")),0,wxLEFT,KEEP_SPACE);
232                diceSizer->AddSpacer(VER_DICE_SPACER);
233                diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE5,*bitmap_dices[4]),0,wxALL,DICE_SPACE);
234                diceSizer->Add(new wxCheckBox(panel, ID_DICE5KEEP, wxT("Keep")),0,wxLEFT,KEEP_SPACE);
235                diceSizer->AddSpacer(VER_DICE_SPACER);
236                diceSizer->Add(new wxButton(panel, ID_ROLL, wxT("Roll!"),wxDefaultPosition,wxSize(VERTICAL_ROLL_SIZEX,VERTICAL_ROLL_SIZEY)),0,wxALL,DICE_SPACE);
237        }
238        //END layout for the dice section of the score board *******/
239
240       
241        topSizer->Add(sectionsSizer);
242        topSizer->Add(diceSizer);       
243
244        panel->SetSizer(topSizer);
245       
246        topSizer->Fit(this);
247        topSizer->SetSizeHints(this);
248       
249        //make the text boxes uneditable to the user   
250        wxTextCtrl *textctrl;
251        for(int i = ID_ACESTEXT;i<=ID_GRANDTOTAL;i++) {
252                textctrl = (wxTextCtrl*) FindWindow(i);
253                textctrl->SetEditable(false);
254        }
255       
256        //disable the undo button
257        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
258
259
260       
261
262        /***************************************/
263        /********* Declare Event Table *********/
264        /***************************************/
265       
266        //BEGIN connecting the menu items' events
267        Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnQuit));
268        Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAbout));
269        Connect(ID_CHECK_FOR_UPDATES, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnCheckForUpdates));
270        Connect(ID_SENDCOMMENT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnSendComment));
271        Connect(ID_NEWGAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnNewGame));
272        Connect(ID_UNDO, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnUndo));
273        Connect(ID_SHOWHIGHSCORE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnShowHighscore));
274        Connect(ID_SETTINGS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnSettings));
275        //END connecting the menu items' events
276
277        Connect(ID_ROLL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnRollButton));
278        Connect(ID_ROLL, wxEVT_ENABLE_ROLL, wxCommandEventHandler (MainFrame::OnRollButton));
279
280        //BEGIN connecting the scoreboard buttons to the events
281        Connect(ID_ACES,ID_SIXES, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnUpperButtons));
282        Connect(ID_THREEOFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On3ofakindButton));
283        Connect(ID_FOUROFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On4ofakindButton));
284        Connect(ID_FULLHOUSE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnFullHouseButton));
285        Connect(ID_SMALLSEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnSmallSequenceButton));
286        Connect(ID_LARGESEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnLargeSequenceButton));
287        Connect(ID_YAHTZEE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnYahtzeeButton));
288        Connect(ID_CHANCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnChanceButton));
289        Connect(ID_DICE1,ID_DICE5, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnDiceClick));
290        Connect(ID_DICE1KEEP,ID_DICE5KEEP, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (MainFrame::OnKeepClick));
291        //END connecting the scoreboard buttons to the event
292
293        /*** End of Event Table ***/
294
295        ResetRolls();
296        ClearDiceHash();
297        m_yahtzee = false;
298        m_yahtzeebonus = false;
299        m_numofplaysleft = 13;
300
301 }
302
303/*********EVENT PROCCESSING FUNCTIONS********/
304
305
306void MainFrame::OnAbout(wxCommandEvent& event)
307{
308#ifndef PORTABLE
309        AboutDialog *about = new AboutDialog(this,wxID_ANY,wxT("About Open Yahtzee PE"));
310#else
311        AboutDialog *about = new AboutDialog(this,wxID_ANY,wxT("About Open Yahtzee"));
312#endif
313        about->ShowModal();
314}
315
316/**
317 * Connects to the Open Yahtzee website on sourceforge.net and checks for
318 * updates to the game.
319 * \param event
320 */
321void MainFrame::OnCheckForUpdates (wxCommandEvent& event)
322{
323       
324        wxString link = wxT("http://openyahtzee.sourceforge.net/update.php?version=");
325       
326        link += wxT(OY_VERSION);
327
328        LaunchBrowser(link);
329}
330
331
332/**
333 * Starts up the user browser and connects to the Open Yahtzee website's
334 * feedback page.
335 * \param event
336 */
337void MainFrame::OnSendComment (wxCommandEvent& event)
338{
339        wxString link = wxT("http://openyahtzee.sourceforge.net/feedback.php");
340       
341        LaunchBrowser(link);
342}
343
344void MainFrame::OnQuit(wxCommandEvent& event)
345{
346        // Destroy the frame
347        Close();
348}
349
350/**
351 * This is the event handler of the New Game menu item. It starts a new
352 * game and clears the board from the last game.
353 * \param event
354 */
355void MainFrame::OnNewGame(wxCommandEvent& event)
356{
357        //disable the undo button so it won't be enabled when a new game is started.
358        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
359       
360        ResetRolls();
361        ClearDiceHash();
362        m_yahtzee = false;
363        m_numofplaysleft = 13;
364       
365        for (int i = ID_ACESTEXT; i<= ID_GRANDTOTAL; i++)
366                ((wxTextCtrl*) FindWindow(i))->Clear();
367        for (int i = ID_ACES; i<= ID_CHANCE; i++)
368                ((wxButton*) FindWindow(i))->Enable(true);
369}
370
371/**
372 * This function handles the undo events. It's connected to the Undo menu item.
373 *
374 * Note that the function only allows undoing of scoring actions and not dice
375 * rolls. Also it won't allow the undoing of the last move of the game because
376 * of a technical problem relating to the case an high-score was made and then
377 * the user undo the last move and rescore another high-score and so on.
378 * \param event
379 */
380void MainFrame::OnUndo(wxCommandEvent& event)
381{
382        m_rolls = m_rollsundo;
383
384        //after the user scored the button was enabled, check if it should be disabled
385        if (m_rolls <= 0) //we don't have remaining rolls
386                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
387
388        //restore the 'keep' checkboxes
389        for (int i=0; i<5; i++)
390                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
391       
392        //reset the users last choice
393        FindWindow(m_lastmove)->Enable(true);
394
395        //clear the score;
396        ((wxTextCtrl*)FindWindow(ID_ACESTEXT + (m_lastmove - ID_ACES)))->SetValue(wxT(""));
397       
398        //Undo the Yahtzee flag if needed
399        if (m_lastmove == ID_YAHTZEE) {
400                m_yahtzee = false;
401        }
402
403        //undo also the yahtzee bonus if needed
404        if (m_yahtzeebonus) {
405                long temp;
406                wxString tempstr;
407       
408                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
409                tempstr.ToLong(&temp,10);
410                temp -= 100; //this line reduces the points given for the yahtzee bonus
411                tempstr.Printf(wxT("%i"),temp);
412                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
413        }
414
415        //recalculate the subtotals
416        CalculateSubTotal();
417
418        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
419        //cancel the counting for the choice that was canceled
420        m_numofplaysleft++;
421}
422
423/**
424 * This function enables the undo button and stores the last move
425 * \param id
426 */
427inline void MainFrame::EnableUndo(int id)
428{
429        if (m_numofplaysleft) {
430                (GetMenuBar()->FindItem(ID_UNDO))->Enable(true);
431                m_lastmove = id;
432        }
433}
434
435/**
436 * Shows the high-score dialog. Connected to the Game->"Show High Score" menu item.
437 * \param event
438 */
439void MainFrame::OnShowHighscore(wxCommandEvent& event)
440{
441        HighScoreDialog *dialog = new HighScoreDialog(this,wxID_ANY,m_highscoredb);
442        dialog->ShowModal();
443}
444
445/**
446 * Shows the settings dialog. This event-handler is connected to Game->Settings
447 * menu item.
448 * \param event
449 */
450void MainFrame::OnSettings( wxCommandEvent& event)
451{
452        SettingsDialog *dialog = new SettingsDialog(this,wxID_ANY);
453        SettingsDialogData data;
454        std::ostringstream sstr;
455       
456        data.highscoresize = m_highscoredb->GetSize();
457
458        data.animate = (m_settingsdb->GetKey("animate")=="Yes")?true:false;
459        data.subtotal = (m_settingsdb->GetKey("calculatesubtotal")=="Yes")?true:false;
460        data.horizontal = (m_settingsdb->GetKey("horizontallayout")=="Yes")?true:false;
461       
462        dialog->SetData(data);
463        if(dialog->ShowModal()==wxID_OK) { //user saved Changes
464                data = dialog->GetData();
465
466                if(data.reset)
467                        m_highscoredb->SetSize(0);
468               
469                sstr<<data.highscoresize<<std::flush;
470                m_settingsdb->SetKey("highscoresize",sstr.str());
471                               
472                m_highscoredb->SetSize(data.highscoresize);
473               
474                if (data.animate){
475                        m_settingsdb->SetKey("animate","Yes");
476                        m_animate = true;
477                       
478                } else {
479                        m_settingsdb->SetKey("animate","No");
480                        m_animate = false;
481                }
482                if (data.subtotal){
483                        m_settingsdb->SetKey("calculatesubtotal","Yes");
484                        m_calculatesubtotal = true;
485                       
486                } else {
487                        m_settingsdb->SetKey("calculatesubtotal","No");
488                        m_calculatesubtotal = false;
489                }
490
491                if (data.horizontal){
492                        m_settingsdb->SetKey("horizontallayout","Yes");
493                        m_horizontallayout = true;
494                        Relayout();                     
495                } else {
496                        m_settingsdb->SetKey("horizontallayout","No");
497                        m_horizontallayout = false;
498                        Relayout();
499                }
500
501        }
502}
503
504/**
505 * Event handler for the Roll button. It checks the settings if it should
506 * animate the dice and then rolls them accordingly. It also checks for the
507 * status of the "keep" checkboxes.
508 * \param event
509 */
510void MainFrame::OnRollButton (wxCommandEvent& event)
511{
512        if (event.GetEventType() == wxEVT_IDLE) {
513                m_skiproll = false;
514                Disconnect(wxEVT_IDLE,  wxCommandEventHandler(MainFrame::OnRollButton));
515                return;
516        }
517       
518        //skip rolling the dice if the user accidently rolled the dice before they finished spinning.
519        if (m_skiproll) return;
520        m_skiproll = true;
521
522        //roll the dice...
523        if (m_animate) {
524                int dice_throws[5] = {0,0,0,0,0};
525                for (int i=0; i<5; i++) { //set the number of rolls for each dice
526                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
527                                dice_throws[i] = (rand()%15)+3; //ensures the number is at least one.
528                        }
529                }
530                while (dice_throws[0] || dice_throws[1] || dice_throws[2] || dice_throws[3] || dice_throws[4]) {
531                        for (int i=0 ; i<5; i++){
532                                if(dice_throws[i]){
533                                        dice_throws[i]--;
534                                        dice[i] = rand()%6;
535                                        ((wxDynamicBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
536                                }
537                        }
538                        ::wxMilliSleep(100);
539                }
540        } else {
541                for (int i=0; i<5; i++) {
542                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
543                                dice[i] = rand()%6;
544                                ((wxDynamicBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
545                        }
546                }
547        }
548       
549        //Clear old dice-hash and create a new one
550        ClearDiceHash();
551        for (int i=0; i<5; i++)
552                dicehash[dice[i]] += 1;
553
554        //if out of rolls disable the roll butoon
555        m_rolls -= 1;
556        #ifndef DEBUG
557        if (m_rolls <= 0)
558                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
559        #endif
560       
561        //enable the keep checkboxes
562        for (int i=0; i<5; i++)
563                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
564       
565        //we rolled the dices so undoing isn't allowed
566        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
567        m_yahtzeebonus = false; //if we scored yahtzee bonus before we don't care anymore.
568       
569        Connect(wxEVT_IDLE, wxCommandEventHandler(MainFrame::OnRollButton));
570}
571
572/**
573 * This is the event-handler for all of the scoring buttons of the upper section.
574 *
575 * It gets the id of the button that called the event handler by comparing it
576 * to the id of the aces button it figures what button was pressed, And updates
577 * the suiting score textbox (again by comparing the id to the one of the aces
578 * checbox).
579 * \param event
580 */
581void MainFrame::OnUpperButtons (wxCommandEvent& event)
582{
583        wxString out;
584        int temp;
585        if(m_rolls < 3){
586                YahtzeeBonus();
587                temp = dicehash[(event.GetId() - ID_ACES)] * (event.GetId() - ID_ACES + 1);
588       
589                out.Printf(wxT("%i"),temp);
590                ((wxTextCtrl*) FindWindow(event.GetId() - ID_ACES + ID_ACESTEXT))->SetValue(out);
591               
592                PostScore(event.GetId());
593        }
594        else
595                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
596}
597
598/**
599 * Event handler for the "3 of a kind" score button.
600 * \param event
601 */
602void MainFrame::On3ofakindButton(wxCommandEvent& event)
603{
604        if(m_rolls>=3) {
605                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
606                return;
607        }
608        YahtzeeBonus();
609        bool three = false;
610        wxString out;
611        int temp = 0;   
612
613        //check for the conditions of scoring
614        for (int i=0; i<6; i++)
615                if (dicehash[i] >= 3)
616                        three = true;
617        if (three){
618                for(int i = 0; i<5; i++)
619                        temp += dice[i]+1;
620       
621                out.Printf(wxT("%i"),temp);
622                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(out);
623        } else
624                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(wxT("0"));
625       
626        PostScore(event.GetId());
627}
628
629/**
630 * Event handler for the "4 of a kind" score button.
631 * \param event
632 */
633void MainFrame::On4ofakindButton(wxCommandEvent& event)
634{
635        if(m_rolls>=3) {
636                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
637                return;
638        }
639        YahtzeeBonus();
640        bool four = false;
641        wxString out;
642        int temp = 0;   
643
644        //check for the conditions of scoring
645        for (int i=0; i<6; i++)
646                if (dicehash[i] >= 4)
647                        four = true;
648        if (four){
649                for(int i = 0; i<5; i++)
650                        temp += dice[i]+1;
651       
652                out.Printf(wxT("%i"),temp);
653                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(out);
654        } else
655                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(wxT("0"));
656       
657        PostScore(event.GetId());
658}
659
660/**
661 * Event handler for the full-house score button.
662 * \param event
663 */
664void MainFrame::OnFullHouseButton(wxCommandEvent& event)
665{
666        if(m_rolls>=3) {
667                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
668                return;
669        }
670        YahtzeeBonus();
671        bool two = false;
672        bool three = false;
673
674        //check for the conditions of scoring
675        for (int i=0; i<6; i++)
676                if (dicehash[i] == 2)
677                        two = true;
678        for (int i=0; i<6; i++)
679                if (dicehash[i] == 3)
680                        three = true;
681        if ((two && three) || YahtzeeJoker())
682                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("25"));
683        else
684                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("0"));
685       
686        PostScore(event.GetId());
687}
688
689/**
690 * Event handler for the small-sequence score button.
691 * \param event
692 */
693void MainFrame::OnSmallSequenceButton(wxCommandEvent& event)
694{
695        if(m_rolls>=3) {
696                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
697                return;
698        }
699
700        YahtzeeBonus();
701        bool sequence = false;
702        //check for the conditions of scoring
703        if ( (dicehash[0]>=1 && dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1) ||
704                (dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1) ||
705                (dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1 && dicehash[5]>=1))
706                        sequence = true;
707        if (sequence || YahtzeeJoker())
708                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("30"));
709        else
710                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("0"));
711       
712        PostScore(event.GetId());
713}
714
715/**
716 * Event handler for the large-sequence score button.
717 * \param event
718 */
719void MainFrame::OnLargeSequenceButton(wxCommandEvent& event)
720{
721        if(m_rolls>=3) {
722                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
723                return;
724        }
725
726        YahtzeeBonus();
727        bool sequence = false;
728        //check for the conditions of scoring
729        if ( (dicehash[0]==1 && dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1) ||
730                (dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1 && dicehash[5]==1))
731                        sequence = true;
732        if (sequence || YahtzeeJoker())
733                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("40"));
734        else
735                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("0"));
736       
737        PostScore(event.GetId());
738}
739
740/**
741 * Event handler for the yahtzee score button.
742 * \param event
743 */
744void MainFrame::OnYahtzeeButton(wxCommandEvent& event)
745{
746        if(m_rolls>=3) {
747                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
748                return;
749        }
750        //give the score
751        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])){
752                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("50"));
753                m_yahtzee=true;
754        } else
755                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("0"));
756
757        PostScore(event.GetId());
758}
759
760/**
761 * Evnet handler for the chance score button.
762 * \param event
763 */
764void MainFrame::OnChanceButton (wxCommandEvent& event)
765{
766        wxString out;
767        int temp = 0;
768        if(m_rolls < 3){
769                YahtzeeBonus();
770                for(int i = 0; i<5; i++)
771                        temp += dice[i]+1;
772       
773                out.Printf(wxT("%i"),temp);
774                ((wxTextCtrl*) FindWindow(ID_CHANCETEXT))->SetValue(out);
775               
776                PostScore(event.GetId());
777        }
778        else
779                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
780
781}
782
783/**
784 * Event handler for mouse clicks on the dice.
785 *
786 * When clicking on the dice this event-handler ticks the appropriate "keep" checkbox.
787 * \param event
788 */
789void MainFrame::OnDiceClick (wxCommandEvent& event)
790{
791        //tick the checkbox
792        if (((wxCheckBox*) FindWindow(event.GetId()-ID_DICE1 + ID_DICE1KEEP))->IsEnabled()){
793                bool newvalue = (((wxCheckBox*) FindWindow(event.GetId()-ID_DICE1 + ID_DICE1KEEP))->GetValue())?false:true;
794                ((wxCheckBox*) FindWindow(event.GetId()-ID_DICE1 + ID_DICE1KEEP)) -> SetValue(newvalue);
795        }
796
797        //dispatch a click event on the checkbox
798        wxCommandEvent clickevent((event.GetId()-ID_DICE1 + ID_DICE1KEEP),wxEVT_COMMAND_CHECKBOX_CLICKED);
799        clickevent.SetEventObject( this );
800        clickevent.SetId(event.GetId()-ID_DICE1 + ID_DICE1KEEP);
801        this->OnKeepClick(clickevent); ///\todo find a better way to call the event handler.
802}
803
804void MainFrame::OnKeepClick (wxCommandEvent& event)
805{
806        wxCheckBox *temp = (wxCheckBox*) FindWindow(event.GetId());
807        ((wxDynamicBitmap*) FindWindow(event.GetId()-ID_DICE1KEEP + ID_DICE1))->SetGrayScale(temp->GetValue());
808}
809
810//********************************************
811//******        General Functions       ******
812//********************************************
813
814/**
815 * This function clears the dice hash.
816 *
817 * The dice hash is just an array that holds how many dices have each value.
818 */
819void MainFrame::ClearDiceHash()
820{
821        for (int i=0; i<6; i++)
822                dicehash[i] = 0;
823}
824
825/**
826 * This function handles everything related to reseting the dice rolls after scoring.
827 */
828void MainFrame::ResetRolls()
829{
830        m_rollsundo = m_rolls;
831        m_rolls = 3;
832        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(true);
833        for (int i=0; i<5; i++){
834                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> SetValue(false);
835                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(false);
836                ((wxDynamicBitmap*) FindWindow(i+ID_DICE1)) -> SetGrayScale(false);
837        }
838}
839
840/**
841 * This function checks for a Yahtzee Bonus situation and if one exists it scores accordingly.
842 */
843void MainFrame::YahtzeeBonus()
844{
845        long temp;
846        wxString tempstr;
847       
848        //if the player didn't have any yathzees yet he can't have the bonus;
849        if (!m_yahtzee)
850                return;
851        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])) {
852                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
853                tempstr.ToLong(&temp,10);
854                temp += 100;
855                tempstr.Printf(wxT("%i"),temp);
856                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
857                m_yahtzeebonus = true;
858        }       
859}
860
861/**
862 * This function checks whether we have a Yahtzee Joker situation.
863 * \return true if there is Yahtzee Joker, false otherwise.
864 */
865bool MainFrame::YahtzeeJoker()
866{
867        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4]) && !(FindWindow(ID_ACES+dice[0])->IsEnabled()) && !(FindWindow(ID_YAHTZEE)->IsEnabled())) {
868                return true;
869        }
870        return false;
871}
872
873/**
874 * This function handles the end of game.
875 *
876 * When a game ends it calculates the total score and submit it to the high
877 * score list.
878 */
879void MainFrame::EndofGame()
880{
881        if(m_numofplaysleft>0)
882                return;
883       
884        wxString tempstr;
885        long temp;
886        long upperscore = 0;
887        long lowerscore = 0;
888
889        for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){
890                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
891                tempstr.ToLong(&temp,10);
892                upperscore +=temp;
893        }
894       
895        tempstr.Printf(wxT("%i"),upperscore);
896        ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr);
897       
898        //check for bonus
899        if(upperscore>=63) {
900                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("35"));
901                upperscore +=35;
902        } else
903                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("0"));
904       
905        tempstr.Printf(wxT("%i"),upperscore);
906        ((wxTextCtrl*) FindWindow(ID_UPPERTOTAL)) -> SetValue(tempstr);
907       
908        //calculate total on lower section
909        for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) {
910                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
911                tempstr.ToLong(&temp,10);
912                lowerscore +=temp;
913        }
914       
915        tempstr.Printf(wxT("%i"),lowerscore);
916        ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr);
917        tempstr.Printf(wxT("%i"),upperscore + lowerscore);
918        ((wxTextCtrl*) FindWindow(ID_GRANDTOTAL)) -> SetValue(tempstr);
919
920        //disable the roll button;
921        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
922
923        tempstr.Printf(wxT("Your final score is %i points!"),lowerscore+upperscore);
924        wxMessageBox(tempstr, wxT("Game Ended"), wxOK | wxICON_INFORMATION, this);
925
926        //submit to high score
927        HighScoreHandler(lowerscore+upperscore);
928       
929}
930
931/**
932 * This function checks if a given score qualifies for the high score list and
933 * adds it to the list if it does.
934 * @param score The score submitted to the high score list.
935 */
936void MainFrame::HighScoreHandler(int score)
937{
938        int place;
939        std::string name,date;
940        wxCommandEvent newevent;
941
942
943        place = m_highscoredb->IsHighScore(score);
944       
945        if(!place)  //if the score didn't make it to the highscore table do nothing
946                return;
947       
948        wxString msg;
949        msg.Printf(wxT("Your score made it to the high score table. Your place is number %i.\nPlease enter your name below:"),place);
950
951        wxTextEntryDialog infodialog(this,msg,wxT("Please enter your name"),wxT(""),wxOK | wxCENTRE);
952        infodialog.ShowModal();
953
954        name = infodialog.GetValue().mb_str();
955
956        //get the date
957        wxDateTime now = wxDateTime::Now();
958        date = now.FormatISOTime().mb_str();
959        date +=" ";
960        date += now.FormatDate().mb_str();
961
962        m_highscoredb->SendHighScore(name,date,score);
963
964        //now show the high score table
965        newevent.SetId(ID_SHOWHIGHSCORE);
966        newevent.SetEventType(wxEVT_COMMAND_MENU_SELECTED);
967        ProcessEvent(newevent);
968
969}
970
971///this function handles all the post scoring stuff such as disabling the right button.
972/**
973 * This function is always called after scoring and it handles all the post-score
974 * stuff such as reseting the dice rolls, enabling the undo button, calculating
975 * the sub-total scores and ending the game if necessary.
976 * \param id
977 */
978void MainFrame::PostScore(int id)
979{
980        //now after the scoring reset the rolls
981        ResetRolls();
982
983        CalculateSubTotal();
984
985        //and disable the button
986        FindWindow(id)->Enable(false);
987        m_numofplaysleft--;
988        EnableUndo(id);
989        EndofGame();
990}
991
992/**
993 * This function calculates the sub-total scores and should be called after
994 * every score. It does so only if this feature is requested in the settings
995 * dialog.
996 */
997void MainFrame::CalculateSubTotal()
998{
999        if (!m_calculatesubtotal)
1000                return;
1001        long upperscore = 0;
1002        long lowerscore = 0;
1003        wxString tempstr;
1004        long temp;
1005
1006
1007        for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){
1008                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
1009                tempstr.ToLong(&temp,10);
1010                upperscore +=temp;
1011        }
1012       
1013        tempstr.Printf(wxT("%i"),upperscore);
1014        ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr);
1015
1016        for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) {
1017                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
1018                tempstr.ToLong(&temp,10);
1019                lowerscore +=temp;
1020        }
1021       
1022        tempstr.Printf(wxT("%i"),lowerscore);
1023        ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr);
1024}
1025
1026
1027/**
1028 * Initializes the database and stores default settings if needed.
1029 * @return 0 if some error
1030 */
1031int MainFrame::InitializeDatabase()
1032{
1033        std::ostringstream sstr;
1034
1035
1036        if (m_settingsdb->GetKey("highscoresize") == "") { //check if we need to create a new high score table
1037                m_highscoredb->SetSize(DEF_HIGHSCORESIZE);
1038                sstr<<DEF_HIGHSCORESIZE<<std::flush;
1039                m_settingsdb->SetKey("highscoresize", sstr.str());
1040        } else {
1041                int highscoresize = atoi((m_settingsdb->GetKey("highscoresize")).c_str());
1042                //m_highscoredb->SetSize((highscoresize>0)?highscoresize:DEF_HIGHSCORESIZE);
1043                m_highscoredb->SetSize(highscoresize);
1044        }
1045       
1046        if (m_settingsdb->GetKey("animate") == "Yes") {
1047                m_animate = true;
1048        } else if (m_settingsdb->GetKey("animate") == "No") {
1049                m_animate = false;
1050        } else {
1051                m_settingsdb->SetKey("animate", "Yes");
1052                m_animate = true;
1053        }
1054
1055        if (m_settingsdb->GetKey("calculatesubtotal") == "Yes") {
1056                m_calculatesubtotal = true;
1057        } else if (m_settingsdb->GetKey("calculatesubtotal") == "No") {
1058                m_calculatesubtotal = false;
1059        } else {
1060                m_settingsdb->SetKey("calculatesubtotal", "Yes");
1061                m_calculatesubtotal = true;
1062        }
1063
1064        if (m_settingsdb->GetKey("horizontallayout") == "Yes") {
1065                m_horizontallayout = true;
1066        } else if (m_settingsdb->GetKey("horizontallayout") == "No") {
1067                m_horizontallayout = false;
1068        } else {
1069                m_settingsdb->SetKey("horizontallayout", "No");
1070                m_horizontallayout = false;
1071        }
1072        if (m_settingsdb->GetKey("openyahtzeehomepage") == "") {
1073                m_settingsdb->SetKey("openyahtzeehomepage", "http://openyahtzee.sourceforge.net/");
1074        }
1075
1076        if (m_settingsdb->GetKey("updateurl") == "") {
1077                /*the version string will be appended in the end of the
1078                  given url */
1079                m_settingsdb->SetKey("updateurl", "http://openyahtzee.sourceforge.net/update.php?version=");
1080        }
1081       
1082        return 1;
1083}
1084
1085void MainFrame::Relayout()
1086{
1087        wxBoxSizer *topSizer;
1088        wxFlexGridSizer *diceSizer;
1089        bool roll_button_enabled;
1090
1091        lowersection->GetStaticBox()->Destroy();
1092        uppersection->GetStaticBox()->Destroy();
1093        sectionsSizer->Remove(lowersection);
1094        sectionsSizer->Remove(uppersection);
1095
1096        if (m_horizontallayout) {
1097                topSizer = new wxBoxSizer( wxVERTICAL );
1098                sectionsSizer = new wxBoxSizer( wxHORIZONTAL );
1099                diceSizer = new wxFlexGridSizer(2, 0, 0, 0);
1100        } else {
1101                topSizer = new wxBoxSizer( wxHORIZONTAL );
1102                sectionsSizer = new wxBoxSizer( wxVERTICAL );
1103                diceSizer = new wxFlexGridSizer(1, 0, 0);;
1104        }
1105
1106        uppersection = new wxStaticBoxSizer( new wxStaticBox( FindWindow(ID_PANEL), wxID_ANY, wxT("Upper Section") ), wxVERTICAL);
1107        lowersection = new wxStaticBoxSizer( new wxStaticBox( FindWindow(ID_PANEL), wxID_ANY, wxT("Lower Section") ), wxVERTICAL);
1108       
1109        wxFlexGridSizer* uppergrid = new wxFlexGridSizer(2, 0, 10);
1110        wxFlexGridSizer* lowergrid = new wxFlexGridSizer(2, 0, 10);
1111
1112        //BEGIN layout for the upper section of the score board
1113        uppergrid->Add(FindWindow(ID_ACES),0,wxALL,SPACE_SIZE);
1114        uppergrid->Add(FindWindow(ID_ACESTEXT),1,wxALL,SPACE_SIZE);
1115        uppergrid->Add(FindWindow(ID_TWOS),0,wxALL,SPACE_SIZE);
1116        uppergrid->Add(FindWindow(ID_TWOSTEXT),1,wxALL,SPACE_SIZE);
1117        uppergrid->Add(FindWindow(ID_THREES),0,wxALL,SPACE_SIZE);
1118        uppergrid->Add(FindWindow(ID_THREESTEXT),1,wxALL,SPACE_SIZE);
1119        uppergrid->Add(FindWindow(ID_FOURS),0,wxALL,SPACE_SIZE);
1120        uppergrid->Add(FindWindow(ID_FOURSTEXT),1,wxALL,SPACE_SIZE);
1121        uppergrid->Add(FindWindow(ID_FIVES),0,wxALL,SPACE_SIZE);
1122        uppergrid->Add(FindWindow(ID_FIVESTEXT),1,wxALL,SPACE_SIZE);
1123        uppergrid->Add(FindWindow(ID_SIXES),0,wxALL,SPACE_SIZE);
1124        uppergrid->Add(FindWindow(ID_SIXESTEXT),1,wxALL,SPACE_SIZE);
1125        uppergrid->Add(FindWindowByLabel(wxT("Total score:")),0,wxALL,SPACE_SIZE);
1126        uppergrid->Add(FindWindow(ID_UPPERSECTIONTOTAL),1,wxALL,SPACE_SIZE);
1127        uppergrid->Add(FindWindowByLabel(wxT("Bonus:")),0,wxALL,SPACE_SIZE);
1128        uppergrid->Add(FindWindow(ID_BONUS),1,wxALL,SPACE_SIZE);
1129        uppergrid->Add(FindWindowByLabel(wxT("Total of upper section:")),0,wxALL,SPACE_SIZE);
1130        uppergrid->Add(FindWindow(ID_UPPERTOTAL),1,wxALL,SPACE_SIZE);
1131        //END layout for the upper section of the score board
1132
1133        //BEGIN layout for the lower section of the score board
1134        lowergrid->Add(FindWindow(ID_THREEOFAKIND),0,wxALL,SPACE_SIZE);
1135        lowergrid->Add(FindWindow(ID_THREEOFAKINDTEXT),1,wxALL,SPACE_SIZE);
1136        lowergrid->Add(FindWindow(ID_FOUROFAKIND),0,wxALL,SPACE_SIZE);
1137        lowergrid->Add(FindWindow(ID_FOUROFAKINDTEXT),1,wxALL,SPACE_SIZE);
1138        lowergrid->Add(FindWindow(ID_FULLHOUSE),0,wxALL,SPACE_SIZE);
1139        lowergrid->Add(FindWindow(ID_FULLHOUSETEXT),1,wxALL,SPACE_SIZE);
1140        lowergrid->Add(FindWindow(ID_SMALLSEQUENCE),0,wxALL,SPACE_SIZE);
1141        lowergrid->Add(FindWindow(ID_SMALLSEQUENCETEXT),1,wxALL,SPACE_SIZE);
1142        lowergrid->Add(FindWindow(ID_LARGESEQUENCE),0,wxALL,SPACE_SIZE);
1143        lowergrid->Add(FindWindow(ID_LARGESEQUENCETEXT),1,wxALL,SPACE_SIZE);
1144        lowergrid->Add(FindWindow(ID_YAHTZEE),0,wxALL,SPACE_SIZE);
1145        lowergrid->Add(FindWindow(ID_YAHTZEETEXT),1,wxALL,SPACE_SIZE);
1146        lowergrid->Add(FindWindow(ID_CHANCE),0,wxALL,SPACE_SIZE);
1147        lowergrid->Add(FindWindow(ID_CHANCETEXT),1,wxALL,SPACE_SIZE);
1148        lowergrid->Add(FindWindowByLabel(wxT("Yahtzee Bonus")),0,wxALL,SPACE_SIZE);
1149        lowergrid->Add(FindWindow(ID_YAHTZEEBONUSTEXT),1,wxALL,SPACE_SIZE);
1150        lowergrid->Add(FindWindowByLabel(wxT("Total of lower section:")),0,wxALL,SPACE_SIZE);
1151        lowergrid->Add(FindWindow(ID_LOWERTOTAL),1,wxALL,SPACE_SIZE);
1152        lowergrid->Add(FindWindowByLabel(wxT("Grand Total:")),0,wxALL,SPACE_SIZE);
1153        lowergrid->Add(FindWindow(ID_GRANDTOTAL),1,wxALL,SPACE_SIZE);
1154        //END layout for the lower section of the score board
1155
1156        uppersection->Add(uppergrid);
1157        lowersection->Add(lowergrid);
1158        sectionsSizer->Add(uppersection,0,wxALL,5);
1159        sectionsSizer->Add(lowersection,0,wxALL,5);
1160
1161        roll_button_enabled = FindWindow(ID_ROLL)->IsEnabled();
1162        //Change the roll button size if we need to
1163        if (m_horizontallayout) {
1164                FindWindow(ID_ROLL)->Destroy();
1165                new wxButton(FindWindow(ID_PANEL), ID_ROLL, wxT("Roll!"),wxDefaultPosition,wxSize(64,64));
1166        } else {
1167                FindWindow(ID_ROLL)->Destroy();
1168                new wxButton(FindWindow(ID_PANEL), ID_ROLL, wxT("Roll!"),wxDefaultPosition,wxSize(VERTICAL_ROLL_SIZEX,VERTICAL_ROLL_SIZEY));
1169        }
1170        FindWindow(ID_ROLL)->Enable(roll_button_enabled);
1171       
1172        //BEGIN layout for the dice section of the score board
1173        if (m_horizontallayout) {
1174                diceSizer->Add(FindWindow(ID_DICE1),0,wxALL,DICE_SPACE);
1175                diceSizer->Add(FindWindow(ID_DICE2),0,wxALL,DICE_SPACE);
1176                diceSizer->Add(FindWindow(ID_DICE3),0,wxALL,DICE_SPACE);
1177                diceSizer->Add(FindWindow(ID_DICE4),0,wxALL,DICE_SPACE);
1178                diceSizer->Add(FindWindow(ID_DICE5),0,wxALL,DICE_SPACE);
1179                diceSizer->Add(FindWindow(ID_ROLL),0,wxALL,DICE_SPACE);
1180                diceSizer->Add(FindWindow(ID_DICE1KEEP),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
1181                diceSizer->Add(FindWindow(ID_DICE2KEEP),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
1182                diceSizer->Add(FindWindow(ID_DICE3KEEP),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
1183                diceSizer->Add(FindWindow(ID_DICE4KEEP),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
1184                diceSizer->Add(FindWindow(ID_DICE5KEEP),0,wxBOTTOM | wxLEFT,KEEP_SPACE);
1185        } else {
1186                diceSizer->Add(FindWindow(ID_DICE1),0,wxALL,DICE_SPACE);
1187                diceSizer->Add(FindWindow(ID_DICE1KEEP),0,wxLEFT,KEEP_SPACE);
1188                diceSizer->AddSpacer(VER_DICE_SPACER);
1189                diceSizer->Add(FindWindow(ID_DICE2),0,wxALL,DICE_SPACE);
1190                diceSizer->Add(FindWindow(ID_DICE2KEEP),0,wxLEFT,KEEP_SPACE);
1191                diceSizer->AddSpacer(VER_DICE_SPACER);
1192                diceSizer->Add(FindWindow(ID_DICE3),0,wxALL,DICE_SPACE);
1193                diceSizer->Add(FindWindow(ID_DICE3KEEP),0,wxLEFT,KEEP_SPACE);
1194                diceSizer->AddSpacer(VER_DICE_SPACER);
1195                diceSizer->Add(FindWindow(ID_DICE4),0,wxALL,DICE_SPACE);
1196                diceSizer->Add(FindWindow(ID_DICE4KEEP),0,wxLEFT,KEEP_SPACE);
1197                diceSizer->AddSpacer(VER_DICE_SPACER);
1198                diceSizer->Add(FindWindow(ID_DICE5),0,wxALL,DICE_SPACE);
1199                diceSizer->Add(FindWindow(ID_DICE5KEEP),0,wxLEFT,KEEP_SPACE);
1200                diceSizer->AddSpacer(VER_DICE_SPACER);
1201                diceSizer->Add(FindWindow(ID_ROLL),0,wxALL,DICE_SPACE);
1202        }
1203        //END layout for the dice section of the score board *******/
1204
1205       
1206        topSizer->Add(sectionsSizer);
1207        topSizer->Add(diceSizer);       
1208       
1209        topSizer->SetSizeHints(this);
1210
1211        topSizer->Layout();
1212        FindWindow(ID_PANEL)->SetSizerAndFit(topSizer);
1213       
1214        topSizer->Fit(this);
1215       
1216}
1217
Note: See TracBrowser for help on using the repository browser.