///////////////////////////////////////////////////////////////////////////// // Name: tables.cpp // tag: Implement a bridge room with all the visual tables // Author: David Roundy // Modified by: // Created: 2/2002 // Copyright: (c) 2002 David Roundy // Licence: GPL /* 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 #endif #include "colors.h" #include "tables.h" #include "tablepic.h" #include "debug.h" BEGIN_EVENT_TABLE(aBridgeTables, wxScrolledWindow) EVT_SET_FOCUS(aBridgeTables::SetTalkFocus) EVT_BRIDGETABLES(aBridgeTables::ProcessTablesEvent) END_EVENT_TABLE() aBridgeTables::aBridgeTables(wxWindow* parent, int x, int y, int w, int h) : wxScrolledWindow(parent, -1, wxPoint(x, y), wxSize(w, h), wxHSCROLL | wxVSCROLL) { #ifdef __WXGTK__ m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL); #else m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL); #endif SetBackgroundColour(aBridgeBackgroundColour()); m_arrowCursor = new wxCursor(wxCURSOR_ARROW); SetScrollbars(20, 20, 40, 200); m_sizer = new wxGridSizer( 2,10 ); m_tablepics = new TablePicList(wxKEY_STRING); // m_sizer->SetSizeHints(this); m_sizer->SetDimension(0,0,800,1*200); // :( ugh SetAutoLayout( TRUE ); //SetSizer( m_sizer ); } aBridgeTables::~aBridgeTables() { delete m_arrowCursor; delete m_sizer; delete m_tablepics; } void aBridgeTables::SetTalkFocus(wxFocusEvent& evt) { wxPostEvent(GetParent()->GetEventHandler(), evt); //GetParent()->GetEventHandler()->ProcessEvent(evt); } void aBridgeTables::OnDraw(wxDC& dc) { dc.SetFont(* m_font); } void aBridgeTables::SendEvent(const wxString &type, const wxString &msg) { TablesEvent my_event( this, type, msg); wxPostEvent( GetParent()->GetEventHandler(), my_event); //if ( GetParent()->GetEventHandler()->ProcessEvent( my_event ) == FALSE ) // wxLogWarning( "Tables: Could not process sent event!" ); } TablePic *aBridgeTables::AddTable(const wxString &table_name) { TablePicList::Node *node = m_tablepics->Find(table_name); if (node) return node->GetData(); TablePic *temp = new TablePic(this, table_name); m_tablepics->Append(table_name, temp); m_sizer->Add( temp); m_sizer->SetDimension(0,0,800,(m_tablepics->GetCount()+1)/2*200); // :( ugh m_sizer->Layout(); return temp; } void aBridgeTables::RemoveTable(const wxString &table_name) { TablePicList::Node *node = m_tablepics->Find(table_name); if (node) { TablePic *temp = node->GetData(); m_tablepics->DeleteNode(node); m_sizer->Remove( temp); delete temp; } else { DebugMsg("Couldn't find " + table_name + " in tablepics list"); } m_sizer->SetDimension(0,0,800,(m_tablepics->GetCount()+1)/2*200); // :( ugh m_sizer->Layout(); } void aBridgeTables::SetTableTopic(const wxString &table_name, const wxString &topic) { TablePicList::Node *node = m_tablepics->Find(table_name); if (node) { node->GetData()->SetTopic(topic); } } void aBridgeTables::HandleSit(const wxString &table_name, const wxString &seat, const wxString &player) { TablePicList::Node *node = m_tablepics->Find(table_name); if (node) { node->GetData()->Sit(seat, player); } } void aBridgeTables::HandleJoinedTable(const wxString &table_name) { TablePicList::Node *node = m_tablepics->Find(table_name); if (node) { node->GetData()->JoinTable(); } else { // Must be a new table!!! AddTable(table_name)->JoinTable(); } } void aBridgeTables::HandleLeaveTable(const wxString &table_name) { TablePicList::Node *node = m_tablepics->Find(table_name); if (node) { node->GetData()->LeaveTable(); } } void aBridgeTables::HandleStand(const wxString &table_name, const wxString &seat) { TablePicList::Node *node = m_tablepics->Find(table_name); if (node) { node->GetData()->Stand(seat); } } void aBridgeTables::ProcessTablesEvent(TablesEvent &evt) { // Alas, this is poor design. Tables events are used to send messages to // 'tables' both from 'room' and from 'tablepic', which makes for nasty // confusion. I almost certainly should have designed different events // (or maybe differnt evend IDs or something) for the two directions. wxString type = evt.GetType(); wxString msg = evt.GetMyMessage(); if (type.IsSameAs("TABLE")) { DebugMsg("There is a table named " + msg); AddTable(msg); } else if (type.IsSameAs("JOINTABLE")) { SendEvent("JOIN", msg); } else if (type.IsSameAs("SITBUTTON")) { SendEvent("SIT", msg); } else if (type.IsSameAs("JOINEDTABLE")) { // I've just joined a table HandleJoinedTable(msg); } else if (type.IsSameAs("TABLECLOSED")) { RemoveTable(msg); } else if (type.IsSameAs("TOPIC")) { wxString table = msg.BeforeFirst(':'); wxString topic = msg.AfterFirst(':'); SetTableTopic(table, topic); } else if (type.IsSameAs("SIT")) { wxString table = msg.BeforeFirst(':'); wxString player = msg.AfterLast(':'); wxString seat = msg.AfterFirst(':').BeforeFirst(':'); HandleSit(table, seat, player); } else if (type.IsSameAs("STAND")) { wxString table = msg.BeforeFirst(':'); wxString seat = msg.AfterFirst(':'); HandleStand(table, seat); } else if (type.IsSameAs("PART")) { HandleLeaveTable(msg); } } // TablesEvent !!!!!!!!!!!!!!!!!!! IMPLEMENT_DYNAMIC_CLASS( TablesEvent, wxEvent ) DEFINE_EVENT_TYPE(wxEVT_COMMAND_BRIDGETABLES) TablesEvent::TablesEvent( wxWindow* win, const wxString& type, const wxString& message) { SetEventType( wxEVT_COMMAND_BRIDGETABLES ); SetEventObject( win ); m_type = type; m_message = message; } #if wxCHECK_VERSION(2, 3, 0) #else void TablesEvent::CopyObject( wxObject& obj_d ) const { wxCommandEvent::CopyObject( obj_d ); TablesEvent &evt = (TablesEvent &) obj_d; evt.m_type = m_type; evt.m_message = m_message; } #endif