source: trunk/src/MainFrame.cpp @ 163

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

remove old settings dialog and switch to new one

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