]> git.saurik.com Git - wxWidgets.git/blobdiff - demos/forty/scoredg.cpp
Remove some obsolete window styles, added a new sizer flag
[wxWidgets.git] / demos / forty / scoredg.cpp
index fddbc0e3c4c783c8da222051df65544c7433714e..84d278412bb9b8c825dfd4392e081d53591a9c19 100644 (file)
 #include "wx/wx.h"
 #endif
 
-#if wxUSE_IOSTREAMH
-#if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__MWERKS__)
-#include <strstrea.h>
-#else
-#include <strstream.h>
-#endif
-#else
-#include <strstream>
-//using namespace std;
-#endif
 #include "scorefil.h"
 #include "scoredg.h"
 
+#define USE_GRID_FOR_SCORE 0
+
+#if USE_GRID_FOR_SCORE
+#include "wx/grid.h"
+#else
 class ScoreCanvas : public wxScrolledWindow
 {
 public:
-       ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile);
+       ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size);
        virtual ~ScoreCanvas();
 
        void OnDraw(wxDC& dc);
@@ -53,22 +48,22 @@ private:
        wxString        m_text;
 };
 
-
-ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile) :
-       wxScrolledWindow(parent)
+ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size) :
+       wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
 {
+    SetBackgroundColour(*wxWHITE);
 #ifdef __WXGTK__
        m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
 #else
        m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
 #endif
 
-        wxArrayString players;
+    wxArrayString players;
        scoreFile->GetPlayerList( players);
 
-       ostrstream os;
+       wxString os;
 
-       os << "Player\tWins\tGames\tScore\n";
+       os << wxT("Player\tWins\tGames\tScore\n");
        for (unsigned int i = 0; i < players.Count(); i++)
        {
                int wins, games, score;
@@ -79,15 +74,13 @@ ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile) :
                        average = (2 * score + games) / (2 * games);
                }
 
-               os << players[i] << '\t' 
-                  << wins  << '\t'
-                  << games << '\t'
-                  << average << '\n';
+               os << players[i] << wxT('\t') 
+                  << wins  << wxT('\t')
+                  << games << wxT('\t')
+                  << average << wxT('\n');
        }
-       os << '\0';
-       char* str = os.str();
-       m_text = str;
-       delete str;
+       os << wxT('\0');
+       m_text = os;
 }
 
 ScoreCanvas::~ScoreCanvas()
@@ -98,7 +91,7 @@ void ScoreCanvas::OnDraw(wxDC& dc)
 {
        dc.SetFont(* m_font);
 
-       const char* str = m_text;
+       const wxChar* str = m_text;
        unsigned int tab = 0;
        unsigned int tabstops[] = { 5, 100, 150, 200 };
 
@@ -106,15 +99,15 @@ void ScoreCanvas::OnDraw(wxDC& dc)
        int lineSpacing;
        {
                long w, h;
-               dc.GetTextExtent("Testing", &w, &h);
+               dc.GetTextExtent(wxT("Testing"), &w, &h);
                lineSpacing = (int)h;
        }
 
        int y = 0;
        while (*str)
        {
-               char text[256];
-               char* dest = text;
+               wxChar text[256];
+               wxChar* dest = text;
                
                while (*str && *str >= ' ') *dest++ = *str++;
                *dest = '\0';
@@ -136,45 +129,72 @@ void ScoreCanvas::OnDraw(wxDC& dc)
                if (*str) str++;
        }
 }
+#endif
 
 BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
     EVT_CLOSE(ScoreDialog::OnCloseWindow)
 END_EVENT_TABLE()
 
-ScoreDialog::ScoreDialog(
-                                                       wxWindow* parent,
-                                                       ScoreFile* file
-                                                       ) :
-       wxDialog(parent, -1, "Scores",
-                       wxDefaultPosition, wxSize(310, 200),
+ScoreDialog::ScoreDialog(wxWindow* parent, ScoreFile* file) :
+       wxDialog(parent, wxID_ANY, _("Scores"),
+                       wxDefaultPosition, wxSize(400, 300),
                        wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
        m_scoreFile(file)
 {
-       // enable constraints
-       SetAutoLayout (TRUE);
-
-       ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile);
-       m_OK = new wxButton(this, wxID_OK, "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( _T("%u"), wins );
+        list->SetCellValue(i,1,string_value);
+        string_value.Printf( _T("%u"), games );
+        list->SetCellValue(i,2,string_value);
+        string_value.Printf( _T("%u"), average );
+        list->SetCellValue(i,3,string_value);
+    }
+    list->SetColLabelValue(0, _T("Players"));
+    list->SetColLabelValue(1, _T("Wins"));
+    list->SetColLabelValue(2, _T("Games"));
+    list->SetColLabelValue(3, _T("Score"));
+    list->SetEditable(false);
+    list->AutoSizeColumns();
+    list->AutoSizeRows();
+    list->SetRowLabelSize(0);
+    list->EnableDragRowSize(false);
+    list->EnableDragColSize(false);
+    list->EnableDragGridSize(false);
+#else
+       ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile, wxDefaultPosition, sz);
+#endif
+
+    // locate and resize with sizers
+    wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
+    topsizer->Add( list, 1, wxALL|wxGROW, 10 );
+    topsizer->Add( new wxButton(this, wxID_OK, _("OK")), 0, wxALIGN_CENTER_HORIZONTAL|wxALL , 10 );
+
+    SetSizer( topsizer );
+
+    GetSizer()->Fit(this);
+    GetSizer()->SetSizeHints(this);
+    
+    CentreOnParent();
 }
 
 ScoreDialog::~ScoreDialog()
@@ -183,10 +203,10 @@ ScoreDialog::~ScoreDialog()
 
 void ScoreDialog::Display()
 {
-       Show(TRUE);
+       Show(true);
 }
 
-void ScoreDialog::OnCloseWindow(wxCloseEvent& event)
+void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
 {
     EndModal(wxID_OK);
 }