]> git.saurik.com Git - wxWidgets.git/blobdiff - demos/forty/scoredg.cpp
Merge in from trunk r67662 to r64801
[wxWidgets.git] / demos / forty / scoredg.cpp
index d8f31924a293f6e9650f323735c4bbd2e8591ae3..b12e16f2c2ef4762eb9c27a90f1444159b5526af 100644 (file)
@@ -6,16 +6,9 @@
 // Created:     21/07/97
 // RCS-ID:      $Id$
 // Copyright:   (c) 1993-1998 Chris Breeze
 // Created:     21/07/97
 // RCS-ID:      $Id$
 // Copyright:   (c) 1993-1998 Chris Breeze
-// Licence:    wxWindows licence
-//---------------------------------------------------------------------------
-// Last modified: 22nd July 1998 - ported to wxWindows 2.0
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 /////////////////////////////////////////////////////////////////////////////
 
-#if defined(__GNUG__) && !defined(__APPLE__)
-#pragma implementation
-#pragma interface
-#endif
-
 // For compilers that support precompilation, includes "wx/wx.h".
 #include "wx/wxprec.h"
 
 // For compilers that support precompilation, includes "wx/wx.h".
 #include "wx/wxprec.h"
 
 #include "scorefil.h"
 #include "scoredg.h"
 
 #include "scorefil.h"
 #include "scoredg.h"
 
+// adjust USE_GRID_FOR_SCORE with O or 1 to your preferences
+// by default it takes wxGrid component for score display if available in target port
+#define USE_GRID_FOR_SCORE     wxUSE_GRID
+
+#if USE_GRID_FOR_SCORE
+#include "wx/grid.h"
+#else
 class ScoreCanvas : public wxScrolledWindow
 {
 public:
 class ScoreCanvas : public wxScrolledWindow
 {
 public:
-       ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile);
-       virtual ~ScoreCanvas();
+    ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size);
+    virtual ~ScoreCanvas();
 
 
-       void OnDraw(wxDC& dc);
+    void OnDraw(wxDC& dc);
 
 private:
 
 private:
-       wxFont*         m_font;
-       wxString        m_text;
+    wxFont     *m_font;
+    wxString    m_text;
 };
 
 };
 
-
-ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile) :
-       wxScrolledWindow(parent)
+ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size) :
+    wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
 {
 {
+    SetBackgroundColour(*wxWHITE);
 #ifdef __WXGTK__
 #ifdef __WXGTK__
-       m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
+    m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
 #else
 #else
-       m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
+    m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
 #endif
 
 #endif
 
-        wxArrayString players;
-       scoreFile->GetPlayerList( players);
-
-       wxString os;
-
-       os << _T("Player\tWins\tGames\tScore\n");
-       for (unsigned int i = 0; i < players.Count(); i++)
-       {
-               int wins, games, score;
-               scoreFile->ReadPlayersScore(players[i], wins, games, score);
-               int average = 0;
-               if (games > 0)
-               {
-                       average = (2 * score + games) / (2 * games);
-               }
-
-               os << players[i] << _T('\t') 
-                  << wins  << _T('\t')
-                  << games << _T('\t')
-                  << average << _T('\n');
-       }
-       os << _T('\0');
-       m_text = os;
+    wxArrayString players;
+    scoreFile->GetPlayerList( players);
+
+    wxString os;
+
+    os << wxT("Player\tWins\tGames\tScore\n");
+    for (unsigned int i = 0; i < players.Count(); i++)
+    {
+        int wins, games, score;
+        scoreFile->ReadPlayersScore(players[i], wins, games, score);
+        int average = 0;
+        if (games > 0)
+        {
+            average = (2 * score + games) / (2 * games);
+        }
+
+        os << players[i] << wxT('\t')
+           << wins  << wxT('\t')
+           << games << wxT('\t')
+           << average << wxT('\n');
+    }
+    os << wxT('\0');
+    m_text = os;
 }
 
 ScoreCanvas::~ScoreCanvas()
 }
 
 ScoreCanvas::~ScoreCanvas()
@@ -84,97 +84,126 @@ ScoreCanvas::~ScoreCanvas()
 
 void ScoreCanvas::OnDraw(wxDC& dc)
 {
 
 void ScoreCanvas::OnDraw(wxDC& dc)
 {
-       dc.SetFont(* m_font);
-
-       const wxChar* str = m_text;
-       unsigned int tab = 0;
-       unsigned int tabstops[] = { 5, 100, 150, 200 };
-
-       // get the line spacing for the current font
-       int lineSpacing;
-       {
-               long w, h;
-               dc.GetTextExtent(_T("Testing"), &w, &h);
-               lineSpacing = (int)h;
-       }
-
-       int y = 0;
-       while (*str)
-       {
-               wxChar text[256];
-               wxChar* dest = text;
-               
-               while (*str && *str >= _T(' ')) *dest++ = *str++;
-               *dest = _T('\0');
-
-               dc.DrawText(text, tabstops[tab], y);
-
-               if (*str == _T('\t'))
-               {
-                       if (tab < sizeof(tabstops) / sizeof(tabstops[0]) - 1)
-                       {
-                               tab++;
-                       }
-               }
-               else if (*str == _T('\n'))
-               {
-                       tab = 0;
-                       y += lineSpacing;
-               }
-               if (*str) str++;
-       }
+    dc.SetFont(* m_font);
+
+    const wxChar* str = m_text;
+    unsigned int tab = 0;
+    unsigned int tabstops[] = { 5, 100, 150, 200 };
+
+    // get the line spacing for the current font
+    int lineSpacing;
+    {
+        long w, h;
+        dc.GetTextExtent(wxT("Testing"), &w, &h);
+        lineSpacing = (int)h;
+    }
+
+    int y = 0;
+    while (*str)
+    {
+        wxChar text[256];
+        wxChar* dest = text;
+
+        while (*str && *str >= ' ') *dest++ = *str++;
+        *dest = '\0';
+
+        dc.DrawText(text, tabstops[tab], y);
+
+        if (*str == '\t')
+        {
+            if (tab < sizeof(tabstops) / sizeof(tabstops[0]) - 1)
+            {
+                tab++;
+            }
+        }
+        else if (*str == '\n')
+        {
+            tab = 0;
+            y += lineSpacing;
+        }
+        if (*str) str++;
+    }
 }
 }
+#endif
 
 BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
     EVT_CLOSE(ScoreDialog::OnCloseWindow)
 END_EVENT_TABLE()
 
 
 BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
     EVT_CLOSE(ScoreDialog::OnCloseWindow)
 END_EVENT_TABLE()
 
-ScoreDialog::ScoreDialog(
-                                                       wxWindow* parent,
-                                                       ScoreFile* file
-                                                       ) :
-       wxDialog(parent, -1, _T("Scores"),
-                       wxDefaultPosition, wxSize(310, 200),
-                       wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
-       m_scoreFile(file)
+ScoreDialog::ScoreDialog(wxWindow* parent, ScoreFile* file) :
+    wxDialog(parent, wxID_ANY, _("Scores"),
+            wxDefaultPosition, wxSize(400, 300)),
+    m_scoreFile(file)
 {
 {
-       // enable constraints
-       SetAutoLayout (TRUE);
-
-       ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile);
-       m_OK = new wxButton(this, wxID_OK, _T("OK"));
-
-       wxLayoutConstraints* layout;
-
-       // Constrain the OK button
-       layout = new wxLayoutConstraints;
-       layout->left.SameAs             (this,  wxLeft,         10);
-       layout->bottom.SameAs   (this,   wxBottom,      10);
-       layout->height.AsIs();
-       layout->width.AsIs();
-       m_OK->SetConstraints(layout);
-
-       // Constrain the list of players
-       layout = new wxLayoutConstraints;
-       layout->left.SameAs             (this,  wxLeft,         10);
-       layout->right.SameAs    (this,  wxRight,        10);
-       layout->top.SameAs              (this,  wxTop,          10);
-       layout->bottom.SameAs   (m_OK,  wxTop,          10);
-       list->SetConstraints(layout);
-
-       Layout();
-}
+    // create grid with players
+    wxArrayString players;
+    file->GetPlayerList(players);
+
+    wxSize sz = wxSize(400, 300);
+
+#if USE_GRID_FOR_SCORE
+    wxGrid* list = new wxGrid(this, wxID_ANY, wxDefaultPosition, sz, 0);
+    list->CreateGrid(players.Count(), 4);
+    for (unsigned int i = 0; i < players.Count(); i++)
+    {
+        int wins, games, score;
+        wxString string_value;
+
+        file->ReadPlayersScore(players[i], wins, games, score);
+        int average = 0;
+        if (games > 0)
+        {
+            average = (2 * score + games) / (2 * games);
+        }
+        list->SetCellValue(i,0,players[i]);
+        string_value.Printf( wxT("%u"), wins );
+        list->SetCellValue(i,1,string_value);
+        string_value.Printf( wxT("%u"), games );
+        list->SetCellValue(i,2,string_value);
+        string_value.Printf( wxT("%u"), average );
+        list->SetCellValue(i,3,string_value);
+    }
+    list->SetColLabelValue(0, wxT("Players"));
+    list->SetColLabelValue(1, wxT("Wins"));
+    list->SetColLabelValue(2, wxT("Games"));
+    list->SetColLabelValue(3, wxT("Score"));
+    list->SetEditable(false);
+    list->AutoSizeColumns();
+    list->AutoSizeRows();
+    list->SetRowLabelSize(0);
+    list->EnableDragRowSize(false);
+    list->EnableDragColSize(false);
+    list->EnableDragGridSize(false);
+    list->ClearSelection();
+    list->EnableEditing(false);
+    sz.x = wxDefaultCoord;
+#else
+    ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile, wxDefaultPosition, sz);
+#endif
 
 
-ScoreDialog::~ScoreDialog()
-{
+    list->SetInitialSize(sz);
+
+    // locate and resize with sizers
+    wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
+    topsizer->Add( list, 1, wxALL|wxGROW, 10 );
+    wxButton *button = new wxButton(this, wxID_OK);
+    topsizer->Add( button, 0, wxALIGN_CENTER_HORIZONTAL|wxALL , 10 );
+    button->SetFocus();
+
+    SetSizer( topsizer );
+
+    GetSizer()->Fit(this);
+    GetSizer()->SetSizeHints(this);
+
+    CentreOnParent();
 }
 
 void ScoreDialog::Display()
 {
 }
 
 void ScoreDialog::Display()
 {
-       Show(TRUE);
+    ShowModal();
 }
 
 }
 
-void ScoreDialog::OnCloseWindow(wxCloseEvent& event)
+void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
 {
     EndModal(wxID_OK);
 }
 {
     EndModal(wxID_OK);
 }