source: trunk/src/MainFrame.cpp @ 164

Last change on this file since 164 was 164, checked in by guyru, 5 years ago

remove dependency on SettingsDB

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