///////////////////////////////////////////////////////////////////////////// // Name: table.cpp // tag: An aBridge table. // Author: David Roundy // Modified by: // Copyright: (c) 2001,2002 David Roundy // Licence: GPL //--------------------------------------------------------------------------- // Last modified: Jan 26, 2002 /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ ///////////////////////////////////////////////////////////////////////////// // For compilers that support precompilation, includes . #include #ifndef WX_PRECOMP #include #include #include #include #endif #include "cards.h" #include "game.h" #include "colors.h" #include "bidreview.h" #include "scorewin.h" #include "irc.h" #include "table.h" #include "tables.h" #include "globals.h" #include "lasttrick.h" #include "prefmacros.h" #include "abridgemenus.h" #include "debug.h" // include config.h for VERSION #include "config.h" BEGIN_EVENT_TABLE(aBridgeTable, wxFrame) EVT_MENU(CLAIM, aBridgeTable::Claim) EVT_MENU(SHOWBID, aBridgeTable::ShowBids) EVT_MENU(SHOWSCORES, aBridgeTable::ShowScores) EVT_MENU(SHOWLAST, aBridgeTable::ShowLastTrick) EVT_MENU(wxID_CLOSE, aBridgeTable::OnCloseWindow) EVT_MOVE(aBridgeTable::OnMove) EVT_CLOSE(aBridgeTable::OnCloseWindow) EVT_TEXT_ENTER(TALK, aBridgeTable::Talk) EVT_SET_FOCUS(aBridgeTable::SetTalkFocus) EVT_IRC_MESSAGE(aBridgeTable::ProcessIRCmessage) EVT_GAME(aBridgeTable::ProcessGameEvent) EVT_ACTIVATE(aBridgeTable::OnActivate) END_EVENT_TABLE() // My frame constructor aBridgeTable::aBridgeTable(wxFrame* frame, const wxString &title, int x, int y, int w, int h, Irc *the_irc, wxString the_room) : wxFrame() { m_lasttrick = NULL; m_game = NULL; m_scorewin = NULL; m_bidreview = NULL; Create(frame, -1, title, wxPoint(x, y), wxSize(w, h)); #ifdef __WXMAC__ // we need this in order to allow the about menu relocation, // since ABOUT is not the default id of the about menu wxApp::s_macAboutMenuItemId = ABOUT ; #endif // set the icon #ifdef __WXMSW__ SetIcon(wxIcon("CardsIcon")); #else #ifdef GTK_TBD SetIcon(wxIcon(Cards_bits, Cards_width, Cards_height)); #endif #endif // Create the chat text windows... m_log = new wxTextCtrl( this, -1, "", wxPoint(0,0), wxSize(100,100), wxTE_READONLY | wxTE_MULTILINE ); m_talk = new wxTextCtrl( this, TALK, "", wxPoint(0,0), wxSize(100,-1), wxTE_PROCESS_ENTER ); // Start up the IRC object itself... m_irc = the_irc->Join(the_room, this, m_log); // Make a menu bar #ifndef __WXMAC__ wxMenu* tableMenu = new wxMenu; tableMenu->Append(SHOWBID, "&Show Bids", "Displays scores"); tableMenu->Append(SHOWSCORES, "&Show Scores", "Displays scores"); tableMenu->Append(SHOWLAST, "&Show Last Trick", "Displays last trick"); tableMenu->Append(wxID_CLOSE, "Close Table\tCtrl-W", "Closes table"); wxMenu* actionMenu = new wxMenu; actionMenu->Append(CLAIM, "&Claim", "Claim N tricks"); wxMenu* helpMenu = new wxMenu; helpMenu->Append(ABOUT, "&About", "Displays program version information"); m_menuBar = new wxMenuBar; m_menuBar->Append(tableMenu, "&Table"); m_menuBar->Append(actionMenu, "&Action"); m_menuBar->Append(helpMenu, "&Help"); SetMenuBar(m_menuBar); #endif #ifdef __WXMAC__ m_menuBar = wxMenuBar::MacGetInstalledMenuBar(); m_menuBar->GetMenu(m_menuBar->FindMenu("Table"))->MacEnableMenu(true); m_menuBar->GetMenu(m_menuBar->FindMenu("Action"))->MacEnableMenu(true); #endif DisableClaim(); m_game = new aBridgeGame(this, 0, 0, 610, 465); DebugMsg("In aBridgeTable::aBridgeTable finished creating aBridgeGame"); m_bidreview = new BidReview(this, m_game); DebugMsg("In aBridgeTable::aBridgeTable finished creating BidReview"); m_scorewin = new ScoreWin(this, m_game->GetScores()); m_lasttrick = new LastTrick(this); wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL ); sizer->Add( m_game, 1, wxEXPAND ); sizer->Add( m_log, 0, wxEXPAND ); sizer->Add( m_talk, 0, wxEXPAND ); sizer->SetSizeHints(this); SetAutoLayout( TRUE ); SetSizer( sizer ); if (GetRememberWindowPositions()) { if (GetBidReviewShown()) { wxCommandEvent hello(wxEVT_COMMAND_MENU_SELECTED, SHOWBID); wxPostEvent(this, hello); } if (GetScoreWindowShown()) { wxCommandEvent hello(wxEVT_COMMAND_MENU_SELECTED, SHOWSCORES); wxPostEvent(this, hello); } if (GetLastTrickShown()) { wxCommandEvent hello(wxEVT_COMMAND_MENU_SELECTED, SHOWLAST); wxPostEvent(this, hello); } } } aBridgeTable::~aBridgeTable() { delete m_irc; m_scorewin->SavePositions(); m_scorewin->Destroy(); m_bidreview->SavePositions(); m_bidreview->Destroy(); m_lasttrick->SavePositions(); m_lasttrick->Destroy(); } void aBridgeTable::OnActivate(wxActivateEvent& event) { m_talk->SetFocus(); #ifdef __WXMAC__ m_menuBar->GetMenu(m_menuBar->FindMenu("Table")) ->MacEnableMenu(event.GetActive()); m_menuBar->GetMenu(m_menuBar->FindMenu("Action")) ->MacEnableMenu(event.GetActive()); #endif } void aBridgeTable::EnableClaim() { wxMenu *the_menu = m_menuBar->GetMenu(m_menuBar->FindMenu("Action")); long my_id = the_menu->FindItem("Claim"); the_menu->Enable(my_id, true); } void aBridgeTable::DisableClaim() { wxMenu *the_menu = m_menuBar->GetMenu(m_menuBar->FindMenu("Action")); long my_id = the_menu->FindItem("Claim"); the_menu->Enable(my_id, false); } void aBridgeTable::SayString(wxString str) { m_irc->Send(str); } void aBridgeTable::OnCloseWindow(wxCloseEvent& event) { if (m_game->OnCloseGame() ) { #ifdef __WXMAC__ m_menuBar->GetMenu(m_menuBar->FindMenu("Table")) ->MacEnableMenu(false); m_menuBar->GetMenu(m_menuBar->FindMenu("Action")) ->MacEnableMenu(false); #endif this->Destroy(); } else { event.Veto(); } } void aBridgeTable::Claim(wxCommandEvent&) { m_game->Claim(); } void aBridgeTable::ShowBids(wxCommandEvent&) { m_bidreview->Show(TRUE); m_bidreview->DisallowOneMove(); } void aBridgeTable::ShowLastTrick(wxCommandEvent&) { m_lasttrick->Show(TRUE); m_lasttrick->DisallowOneMove(); } void aBridgeTable::ShowScores(wxCommandEvent&) { m_scorewin->UpdateScores(); m_scorewin->Show(TRUE); m_scorewin->DisallowOneMove(); } void aBridgeTable::SetTalkFocus(wxFocusEvent& evt) { m_talk->SetFocus(); } void aBridgeTable::Talk(wxCommandEvent&) { wxString str = m_talk->GetLineText(0) + "\n";; SayString(m_talk->GetLineText(0) + "\n"); m_log->AppendText(player_name() + "> " + m_talk->GetLineText(0) + "\n"); m_talk->Clear(); } void aBridgeTable::Sit(Seat seat) { wxString seat_string = Seat2String(seat); m_irc->SendMsg("SIT", seat_string); } void aBridgeTable::ProcessIRCmessage(IrcMsgEvent &evt) { wxString type = evt.GetType(); wxString from = evt.GetFrom(); wxString msg = evt.GetMyMessage(); if (type.IsSameAs("SIT")) { m_log->AppendText(from + " just sat down "+ msg +"\n"); m_game->HandleSit(from, String2Seat(msg)); } else if (type.IsSameAs("SUBSTITUTE")) { m_log->AppendText(from + " just substituted "+ msg +"\n"); m_game->HandleSit(from, String2Seat(msg),true); } else if (type.IsSameAs("YOUSIT")) { m_log->AppendText("You just sat down "+ msg +"\n"); m_game->HandleMySit(player_name(), String2Seat(msg)); } else if (type.IsSameAs("YOUSUBSTITUTE")) { m_log->AppendText("You just substituted "+ msg +"\n"); m_game->HandleMySit(player_name(), String2Seat(msg), true); } else if (type.IsSameAs("GETUP")) { m_game->HandleGetUp(String2Seat(msg)); } else if (type.IsSameAs("YOURCARD")) { m_game->CardDealt(msg); } else if (type.IsSameAs("DUMMYCARD")) { m_game->CardDealt(msg); } else if (type.IsSameAs("DECLARERCARD")) { m_game->CardDealt(msg); } else if (type.IsSameAs("HANDSDEALT")) { m_log->AppendText("The hands have now been dealt by "+msg+"\n"); DebugMsg("The hands have been dealt by " + msg); if (IsStringSeat(msg)) { Seat ds = String2Seat(msg); m_game->SetDealerSeat(ds); m_bidreview->NewHand(ds); } m_game->DealDone(); m_scorewin->UpdateScores(); } else if (type.IsSameAs("PLAY")) { m_game->HandlePlay(from, msg); } else if (type.IsSameAs("ACCEPTCLAIM")) { m_game->HandleClaimAccept(msg); } else if (type.IsSameAs("REJECTCLAIM")) { // So far, I don't do anything here. When I write my own claim accept // dialog, I could interrupt it. Also I could inform the claimer. // Claiming definitely requires some work! } else if (type.IsSameAs("CLAIM")) { m_game->HandleClaim(from, msg); } else if (type.IsSameAs("BID")) { m_bidreview->HandleBid(from, msg); m_game->HandleBid(from, msg); } } void aBridgeTable::ProcessGameEvent(GameEvent &evt) { wxString type = evt.GetType(); wxString msg = evt.GetMyMessage(); if (type.IsSameAs("SIT")) { m_irc->SendMsg("SIT", msg); } else if (type.IsSameAs("SUBSTITUTE")) { m_irc->SendMsg("SUBSTITUTE", msg); } else if (type.IsSameAs("STAND")) { m_irc->SendMsg("GETUP", msg); m_game->HandleGetUp(String2Seat(msg)); } else if (type.IsSameAs("PLAY")) { m_irc->SendMsg("PLAY", msg); } else if (type.IsSameAs("REVEAL")) { m_irc->SendMsg("DUMMYCARD", msg); } else if (type.IsSameAs("CLAIM")) { m_irc->SendMsg("CLAIM", msg); } else if (type.IsSameAs("ACCEPTCLAIM")) { m_irc->SendMsg("ACCEPTCLAIM", msg); } else if (type.IsSameAs("REJECTCLAIM")) { m_irc->SendMsg("REJECTCLAIM", msg); } else if (type.IsSameAs("BID")) { m_irc->SendMsg("BID", msg); m_bidreview->HandleBid(player_name(), msg); m_game->HandleBid(player_name(), msg); } else if (type.IsSameAs("NEWHAND")) { DisableClaim(); m_bidreview->NewHand(m_game->DealerSeat()); } else if (type.IsSameAs("LASTTRICK")) { wxString north = msg.BeforeFirst(':'); wxString south = msg.AfterFirst(':').BeforeFirst(':'); wxString east = msg.AfterFirst(':').AfterFirst(':').BeforeFirst(':'); wxString west = msg.AfterLast(':'); m_lasttrick->ShowTrick(north, south, east, west); } else if (type.IsSameAs("IMDECLARER")) { EnableClaim(); } } void aBridgeTable::OnMove(const wxMoveEvent& event) { if (m_lasttrick) m_lasttrick->ParentMoved(); if (m_game) m_game->TableMoved(); if (m_scorewin) m_scorewin->ParentMoved(); if (m_bidreview) m_bidreview->ParentMoved(); }