Split the sample in three source files + three header files, for improved
[wxWidgets.git] / demos / life / dialogs.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialogs.cpp
3 // Purpose: Life! dialogs
4 // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
5 // Modified by:
6 // Created: Jan/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ==========================================================================
13 // declarations
14 // ==========================================================================
15
16 // --------------------------------------------------------------------------
17 // headers
18 // --------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dialogs.h"
22 #endif
23
24 // for compilers that support precompilation, includes "wx/wx.h"
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 // for all others, include the necessary headers
32 #ifndef WX_PRECOMP
33 #include "wx/wx.h"
34 #endif
35
36 #include "wx/statline.h"
37 #include "wx/spinctrl.h"
38
39 #include "dialogs.h"
40 #include "life.h"
41 #include "game.h"
42
43 // --------------------------------------------------------------------------
44 // constants
45 // --------------------------------------------------------------------------
46
47 // IDs for the controls and the menu commands
48 enum
49 {
50 // listbox in samples dialog
51 ID_LISTBOX = 2001
52 };
53
54 // sample configurations
55 #include "samples.inc"
56
57 // --------------------------------------------------------------------------
58 // event tables and other macros for wxWindows
59 // --------------------------------------------------------------------------
60
61 // Event tables
62 BEGIN_EVENT_TABLE(LifeNewGameDialog, wxDialog)
63 EVT_BUTTON (wxID_OK, LifeNewGameDialog::OnOK)
64 END_EVENT_TABLE()
65
66 BEGIN_EVENT_TABLE(LifeSamplesDialog, wxDialog)
67 EVT_LISTBOX (ID_LISTBOX, LifeSamplesDialog::OnListBox)
68 END_EVENT_TABLE()
69
70
71 // ==========================================================================
72 // implementation
73 // ==========================================================================
74
75 // --------------------------------------------------------------------------
76 // LifeNewGameDialog
77 // --------------------------------------------------------------------------
78
79 LifeNewGameDialog::LifeNewGameDialog(wxWindow *parent, int *w, int *h)
80 : wxDialog(parent, -1,
81 _("New game"),
82 wxDefaultPosition,
83 wxDefaultSize,
84 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
85 {
86 m_w = w;
87 m_h = h;
88
89 // spin ctrls
90 m_spinctrlw = new wxSpinCtrl( this, -1 );
91 m_spinctrlw->SetValue(*m_w);
92 m_spinctrlw->SetRange(LIFE_MIN, LIFE_MAX);
93
94 m_spinctrlh = new wxSpinCtrl( this, -1 );
95 m_spinctrlh->SetValue(*m_h);
96 m_spinctrlh->SetRange(LIFE_MIN, LIFE_MAX);
97
98 // component layout
99 wxBoxSizer *inputsizer1 = new wxBoxSizer( wxHORIZONTAL );
100 inputsizer1->Add( new wxStaticText(this, -1, _("Width")), 1, wxCENTRE | wxLEFT, 20);
101 inputsizer1->Add( m_spinctrlw, 2, wxCENTRE | wxLEFT | wxRIGHT, 20 );
102
103 wxBoxSizer *inputsizer2 = new wxBoxSizer( wxHORIZONTAL );
104 inputsizer2->Add( new wxStaticText(this, -1, _("Height")), 1, wxCENTRE | wxLEFT, 20);
105 inputsizer2->Add( m_spinctrlh, 2, wxCENTRE | wxLEFT | wxRIGHT, 20 );
106
107 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
108 topsizer->Add( CreateTextSizer(_("Enter board dimensions")), 0, wxALL, 10 );
109 topsizer->Add( new wxStaticLine(this, -1), 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 10);
110 topsizer->Add( inputsizer1, 1, wxGROW | wxLEFT | wxRIGHT, 5 );
111 topsizer->Add( inputsizer2, 1, wxGROW | wxLEFT | wxRIGHT, 5 );
112 topsizer->Add( new wxStaticLine(this, -1), 0, wxGROW | wxLEFT | wxRIGHT | wxTOP, 10);
113 topsizer->Add( CreateButtonSizer(wxOK | wxCANCEL), 0, wxCENTRE | wxALL, 10);
114
115 // activate
116 SetSizer(topsizer);
117 SetAutoLayout(TRUE);
118 topsizer->SetSizeHints(this);
119 topsizer->Fit(this);
120 Centre(wxBOTH);
121 }
122
123 void LifeNewGameDialog::OnOK(wxCommandEvent& WXUNUSED(event))
124 {
125 *m_w = m_spinctrlw->GetValue();
126 *m_h = m_spinctrlh->GetValue();
127
128 EndModal(wxID_OK);
129 }
130
131 // --------------------------------------------------------------------------
132 // LifeSamplesDialog
133 // --------------------------------------------------------------------------
134
135 LifeSamplesDialog::LifeSamplesDialog(wxWindow *parent)
136 : wxDialog(parent, -1,
137 _("Sample games"),
138 wxDefaultPosition,
139 wxDefaultSize,
140 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
141 {
142 m_value = 0;
143
144 // create and populate the list of available samples
145 m_list = new wxListBox( this, ID_LISTBOX,
146 wxDefaultPosition,
147 wxDefaultSize,
148 0, NULL,
149 wxLB_SINGLE | wxLB_NEEDED_SB | wxLB_HSCROLL );
150
151 for (unsigned i = 0; i < (sizeof(g_shapes) / sizeof(LifeShape)); i++)
152 m_list->Append(g_shapes[i].m_name);
153
154 // descriptions
155 wxStaticBox *statbox = new wxStaticBox( this, -1, _("Description"));
156 m_life = new Life( 16, 16 );
157 m_life->SetShape(g_shapes[0]);
158 m_canvas = new LifeCanvas( this, m_life, FALSE );
159 m_text = new wxTextCtrl( this, -1,
160 g_shapes[0].m_desc,
161 wxDefaultPosition,
162 wxSize(300, 60),
163 wxTE_MULTILINE | wxTE_READONLY);
164
165 // layout components
166 wxStaticBoxSizer *sizer1 = new wxStaticBoxSizer( statbox, wxVERTICAL );
167 sizer1->Add( m_canvas, 2, wxGROW | wxCENTRE | wxALL, 5);
168 sizer1->Add( m_text, 1, wxGROW | wxCENTRE | wxALL, 5 );
169
170 wxBoxSizer *sizer2 = new wxBoxSizer( wxHORIZONTAL );
171 sizer2->Add( m_list, 0, wxGROW | wxCENTRE | wxALL, 5 );
172 sizer2->Add( sizer1, 1, wxGROW | wxCENTRE | wxALL, 5 );
173
174 wxBoxSizer *sizer3 = new wxBoxSizer( wxVERTICAL );
175 sizer3->Add( CreateTextSizer(_("Select one configuration")), 0, wxALL, 10 );
176 sizer3->Add( new wxStaticLine(this, -1), 0, wxGROW | wxLEFT | wxRIGHT, 10 );
177 sizer3->Add( sizer2, 1, wxGROW | wxCENTRE | wxALL, 5 );
178 sizer3->Add( new wxStaticLine(this, -1), 0, wxGROW | wxLEFT | wxRIGHT, 10 );
179 sizer3->Add( CreateButtonSizer(wxOK | wxCANCEL), 0, wxCENTRE | wxALL, 10 );
180
181 // activate
182 SetSizer(sizer3);
183 SetAutoLayout(TRUE);
184 sizer3->SetSizeHints(this);
185 sizer3->Fit(this);
186 Centre(wxBOTH);
187 }
188
189 LifeSamplesDialog::~LifeSamplesDialog()
190 {
191 m_canvas->Destroy();
192 delete m_life;
193 }
194
195 int LifeSamplesDialog::GetValue()
196 {
197 return m_value;
198 }
199
200 void LifeSamplesDialog::OnListBox(wxCommandEvent& event)
201 {
202 if (event.GetSelection() != -1)
203 {
204 m_value = m_list->GetSelection();
205 m_text->SetValue(g_shapes[ event.GetSelection() ].m_desc);
206 m_life->SetShape(g_shapes[ event.GetSelection() ]);
207
208 m_canvas->DrawEverything(TRUE); // force redraw everything
209 m_canvas->Refresh(FALSE); // do not erase background
210 }
211 }
212