source: trunk/OpenYahtzee/src/MainFrame.cpp @ 123

Last change on this file since 123 was 123, checked in by guyru, 7 years ago

more work on scoring hints

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