forty.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: forty.cpp
  3. // Purpose: Forty Thieves patience game
  4. // Author: Chris Breeze
  5. // Modified by:
  6. // Created: 21/07/97
  7. // Copyright: (c) 1993-1998 Chris Breeze
  8. // Licence: wxWindows licence
  9. //---------------------------------------------------------------------------
  10. // Last modified: 22nd July 1998 - ported to wxWidgets 2.0
  11. /////////////////////////////////////////////////////////////////////////////
  12. // For compilers that support precompilation, includes "wx/wx.h".
  13. #include "wx/wxprec.h"
  14. #ifdef __BORLANDC__
  15. #pragma hdrstop
  16. #endif
  17. #ifndef WX_PRECOMP
  18. #include "wx/wx.h"
  19. #endif
  20. #include "canvas.h"
  21. #include "forty.h"
  22. #include "card.h"
  23. #include "scoredg.h"
  24. #include "forty.xpm"
  25. #if wxUSE_HTML
  26. #include "wx/textfile.h"
  27. #include "wx/html/htmlwin.h"
  28. #endif
  29. #include "wx/stockitem.h"
  30. BEGIN_EVENT_TABLE(FortyFrame, wxFrame)
  31. EVT_MENU(wxID_NEW, FortyFrame::NewGame)
  32. EVT_MENU(wxID_EXIT, FortyFrame::Exit)
  33. EVT_MENU(wxID_ABOUT, FortyFrame::About)
  34. EVT_MENU(wxID_HELP_CONTENTS, FortyFrame::Help)
  35. EVT_MENU(wxID_UNDO, FortyFrame::Undo)
  36. EVT_MENU(wxID_REDO, FortyFrame::Redo)
  37. EVT_MENU(SCORES, FortyFrame::Scores)
  38. EVT_MENU(RIGHT_BUTTON_UNDO, FortyFrame::ToggleRightButtonUndo)
  39. EVT_MENU(HELPING_HAND, FortyFrame::ToggleHelpingHand)
  40. EVT_MENU(LARGE_CARDS, FortyFrame::ToggleCardSize)
  41. EVT_CLOSE(FortyFrame::OnCloseWindow)
  42. END_EVENT_TABLE()
  43. // Create a new application object
  44. IMPLEMENT_APP (FortyApp)
  45. wxColour* FortyApp::m_backgroundColour = 0;
  46. wxColour* FortyApp::m_textColour = 0;
  47. wxBrush* FortyApp::m_backgroundBrush = 0;
  48. FortyApp::~FortyApp()
  49. {
  50. delete m_backgroundColour;
  51. delete m_textColour;
  52. delete m_backgroundBrush;
  53. delete Card::m_symbolBmap;
  54. delete Card::m_pictureBmap;
  55. }
  56. bool FortyApp::OnInit()
  57. {
  58. bool largecards = false;
  59. #ifndef __WXWINCE__
  60. m_helpFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("about.htm");
  61. if (!wxFileExists(m_helpFile))
  62. #endif
  63. {
  64. m_helpFile = wxPathOnly(argv[0]) + wxFILE_SEP_PATH + wxT("about.htm");
  65. }
  66. wxSize size(668,510);
  67. if ((argc > 1) && (!wxStrcmp(argv[1],wxT("-L"))))
  68. {
  69. largecards = true;
  70. size = wxSize(1000,750);
  71. }
  72. FortyFrame* frame = new FortyFrame(
  73. 0,
  74. wxT("Forty Thieves"),
  75. wxDefaultPosition,
  76. size,
  77. largecards
  78. );
  79. // Show the frame
  80. frame->Show(true);
  81. frame->GetCanvas()->ShowPlayerDialog();
  82. return true;
  83. }
  84. const wxColour& FortyApp::BackgroundColour()
  85. {
  86. if (!m_backgroundColour)
  87. {
  88. m_backgroundColour = new wxColour(0, 128, 0);
  89. }
  90. return *m_backgroundColour;
  91. }
  92. const wxBrush& FortyApp::BackgroundBrush()
  93. {
  94. if (!m_backgroundBrush)
  95. {
  96. m_backgroundBrush = new wxBrush(BackgroundColour(), wxSOLID);
  97. }
  98. return *m_backgroundBrush;
  99. }
  100. const wxColour& FortyApp::TextColour()
  101. {
  102. if (!m_textColour)
  103. {
  104. m_textColour = new wxColour(*wxBLACK);
  105. }
  106. return *m_textColour;
  107. }
  108. // My frame constructor
  109. FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size, bool largecards):
  110. wxFrame(frame, wxID_ANY, title, pos, size)
  111. {
  112. #ifdef __WXMAC__
  113. wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
  114. #endif
  115. // set the icon
  116. #ifdef __WXMSW__
  117. SetIcon(wxIcon(wxT("CardsIcon")));
  118. #else
  119. SetIcon(wxIcon(forty_xpm));
  120. #endif
  121. // Make a menu bar
  122. wxMenu* gameMenu = new wxMenu;
  123. gameMenu->Append(wxID_NEW, wxGetStockLabel(wxID_NEW), wxT("Start a new game"));
  124. gameMenu->Append(SCORES, wxT("&Scores..."), wxT("Displays scores"));
  125. gameMenu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), wxT("Exits Forty Thieves"));
  126. wxMenu* editMenu = new wxMenu;
  127. editMenu->Append(wxID_UNDO, wxGetStockLabel(wxID_UNDO), wxT("Undo the last move"));
  128. editMenu->Append(wxID_REDO, wxGetStockLabel(wxID_REDO), wxT("Redo a move that has been undone"));
  129. wxMenu* optionsMenu = new wxMenu;
  130. optionsMenu->Append(RIGHT_BUTTON_UNDO,
  131. wxT("&Right button undo"),
  132. wxT("Enables/disables right mouse button undo and redo"),
  133. true
  134. );
  135. optionsMenu->Append(HELPING_HAND,
  136. wxT("&Helping hand"),
  137. wxT("Enables/disables hand cursor when a card can be moved"),
  138. true
  139. );
  140. optionsMenu->Append(LARGE_CARDS,
  141. wxT("&Large cards"),
  142. wxT("Enables/disables large cards for high resolution displays"),
  143. true
  144. );
  145. optionsMenu->Check(HELPING_HAND, true);
  146. optionsMenu->Check(RIGHT_BUTTON_UNDO, true);
  147. optionsMenu->Check(LARGE_CARDS, largecards ? true : false);
  148. wxMenu* helpMenu = new wxMenu;
  149. helpMenu->Append(wxID_HELP_CONTENTS, wxT("&Help Contents"), wxT("Displays information about playing the game"));
  150. helpMenu->Append(wxID_ABOUT, wxT("&About"), wxT("About Forty Thieves"));
  151. m_menuBar = new wxMenuBar;
  152. m_menuBar->Append(gameMenu, wxT("&Game"));
  153. m_menuBar->Append(editMenu, wxT("&Edit"));
  154. m_menuBar->Append(optionsMenu, wxT("&Options"));
  155. m_menuBar->Append(helpMenu, wxT("&Help"));
  156. SetMenuBar(m_menuBar);
  157. if (largecards)
  158. Card::SetScale(1.3);
  159. m_canvas = new FortyCanvas(this, wxDefaultPosition, size);
  160. wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
  161. topsizer->Add( m_canvas, 1, wxEXPAND | wxALL, 0);
  162. SetSizer( topsizer );
  163. #if wxUSE_STATUSBAR
  164. CreateStatusBar();
  165. #endif // wxUSE_STATUSBAR
  166. topsizer->SetSizeHints( this );
  167. }
  168. void FortyFrame::OnCloseWindow(wxCloseEvent& event)
  169. {
  170. if (m_canvas->OnCloseCanvas() )
  171. {
  172. this->Destroy();
  173. }
  174. else
  175. event.Veto();
  176. }
  177. void
  178. FortyFrame::NewGame(wxCommandEvent&)
  179. {
  180. m_canvas->NewGame();
  181. }
  182. void
  183. FortyFrame::Exit(wxCommandEvent&)
  184. {
  185. Close(true);
  186. }
  187. void
  188. FortyFrame::Help(wxCommandEvent& event)
  189. {
  190. #if wxUSE_HTML
  191. if (wxFileExists(wxGetApp().GetHelpFile()))
  192. {
  193. FortyAboutDialog dialog(this, wxID_ANY, wxT("Forty Thieves Instructions"));
  194. if (dialog.ShowModal() == wxID_OK)
  195. {
  196. }
  197. }
  198. else
  199. #endif
  200. {
  201. About(event);
  202. }
  203. }
  204. void
  205. FortyFrame::About(wxCommandEvent&)
  206. {
  207. wxMessageBox(
  208. wxT("Forty Thieves\n\n")
  209. wxT("A free card game written with the wxWidgets toolkit\n")
  210. wxT("Author: Chris Breeze (c) 1992-2004\n")
  211. wxT("email: chris@breezesys.com"),
  212. wxT("About Forty Thieves"),
  213. wxOK|wxICON_INFORMATION, this
  214. );
  215. }
  216. void
  217. FortyFrame::Undo(wxCommandEvent&)
  218. {
  219. m_canvas->Undo();
  220. }
  221. void
  222. FortyFrame::Redo(wxCommandEvent&)
  223. {
  224. m_canvas->Redo();
  225. }
  226. void
  227. FortyFrame::Scores(wxCommandEvent&)
  228. {
  229. m_canvas->UpdateScores();
  230. ScoreDialog scores(this, m_canvas->GetScoreFile());
  231. scores.Display();
  232. }
  233. void
  234. FortyFrame::ToggleRightButtonUndo(wxCommandEvent& event)
  235. {
  236. bool checked = m_menuBar->IsChecked(event.GetId());
  237. m_canvas->EnableRightButtonUndo(checked);
  238. }
  239. void
  240. FortyFrame::ToggleHelpingHand(wxCommandEvent& event)
  241. {
  242. bool checked = m_menuBar->IsChecked(event.GetId());
  243. m_canvas->EnableHelpingHand(checked);
  244. }
  245. void
  246. FortyFrame::ToggleCardSize(wxCommandEvent& event)
  247. {
  248. bool checked = m_menuBar->IsChecked(event.GetId());
  249. Card::SetScale(checked ? 1.3 : 1);
  250. m_canvas->LayoutGame();
  251. m_canvas->Refresh();
  252. }
  253. //----------------------------------------------------------------------------
  254. // stAboutDialog
  255. //----------------------------------------------------------------------------
  256. FortyAboutDialog::FortyAboutDialog( wxWindow *parent, wxWindowID id, const wxString &title,
  257. const wxPoint &position, const wxSize& size, long style ) :
  258. wxDialog( parent, id, title, position, size, style )
  259. {
  260. AddControls(this);
  261. Centre(wxBOTH);
  262. }
  263. bool FortyAboutDialog::AddControls(wxWindow* parent)
  264. {
  265. #if wxUSE_HTML
  266. wxString htmlText;
  267. wxString htmlFile = wxGetApp().GetHelpFile();
  268. {
  269. wxTextFile file(htmlFile);
  270. if (file.Exists())
  271. {
  272. file.Open();
  273. for ( htmlText = file.GetFirstLine();
  274. !file.Eof();
  275. htmlText << file.GetNextLine() << wxT("\n") ) ;
  276. }
  277. }
  278. if (htmlText.empty())
  279. {
  280. htmlText.Printf(wxT("<html><head><title>Warning</title></head><body><P>Sorry, could not find resource for About dialog<P></body></html>"));
  281. }
  282. // Customize the HTML
  283. htmlText.Replace(wxT("$DATE$"), wxT(__DATE__));
  284. wxSize htmlSize(400, 290);
  285. // Note: in later versions of wxWin this will be fixed so wxRAISED_BORDER
  286. // does the right thing. Meanwhile, this is a workaround.
  287. #ifdef __WXMSW__
  288. long borderStyle = wxDOUBLE_BORDER;
  289. #else
  290. long borderStyle = wxRAISED_BORDER;
  291. #endif
  292. wxHtmlWindow* html = new wxHtmlWindow(this, ID_ABOUT_HTML_WINDOW, wxDefaultPosition, htmlSize, borderStyle);
  293. html -> SetBorders(10);
  294. html -> SetPage(htmlText);
  295. //// Start of sizer-based control creation
  296. wxSizer *item0 = new wxBoxSizer( wxVERTICAL );
  297. wxWindow *item1 = parent->FindWindow( ID_ABOUT_HTML_WINDOW );
  298. wxASSERT( item1 );
  299. item0->Add( item1, 0, wxALIGN_CENTRE|wxALL, 5 );
  300. wxButton *item2 = new wxButton( parent, wxID_CLOSE );
  301. item2->SetDefault();
  302. item2->SetFocus();
  303. SetAffirmativeId(wxID_CLOSE);
  304. item0->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 );
  305. parent->SetSizer( item0 );
  306. parent->Layout();
  307. item0->Fit( parent );
  308. item0->SetSizeHints( parent );
  309. #endif
  310. return true;
  311. }