]> git.saurik.com Git - wxWidgets.git/blame - samples/tab/test.cpp
corrected (?) wxStringList::Delete()
[wxWidgets.git] / samples / tab / test.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: test.cpp
3// Purpose: Tab demo
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c)
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
23#include "wx/tab.h"
24#include "test.h"
25
4b5f3fe6
JS
26// If 1, use a dialog. Otherwise use a frame.
27#define USE_TABBED_DIALOG 0
28
29MyDialog* dialog = (MyDialog *) NULL;
30MyFrame* frame = (MyFrame *) NULL;
c801d85f
KB
31
32IMPLEMENT_APP(MyApp)
33
34bool MyApp::OnInit(void)
35{
4b5f3fe6
JS
36 // Create the main window
37#if USE_TABBED_DIALOG
c67daf87 38 dialog = new MyDialog((wxFrame *) NULL, -1, (char *) "Tabbed Dialog", wxPoint(-1, -1), wxSize(365, 390), wxDIALOG_MODAL|wxDEFAULT_DIALOG_STYLE);
c801d85f
KB
39
40 dialog->ShowModal();
41
42 // Quit immediately the dialog has been dismissed
43 return FALSE;
4b5f3fe6
JS
44#else
45 frame = new MyFrame((wxFrame*) NULL, -1, (char *) "Tabbed Panel", wxPoint(-1, -1), wxSize(365, 390), wxDEFAULT_FRAME_STYLE);
c801d85f 46
4b5f3fe6
JS
47 return TRUE;
48#endif
c801d85f
KB
49}
50
4b5f3fe6 51void MyApp::InitTabView(wxPanelTabView* view, wxWindow* window)
c801d85f 52{
4b5f3fe6
JS
53 m_okButton = new wxButton(window, wxID_OK, "Close", wxPoint(-1, -1), wxSize(80, 25));
54 m_cancelButton = new wxButton(window, wxID_CANCEL, "Cancel", wxPoint(-1, -1), wxSize(80, 25));
55 m_helpButton = new wxButton(window, wxID_HELP, "Help", wxPoint(-1, -1), wxSize(80, 25));
56 m_okButton->SetDefault();
c801d85f 57
4b5f3fe6
JS
58 wxLayoutConstraints* c = new wxLayoutConstraints;
59 c->right.SameAs(window, wxRight, 4);
60 c->bottom.SameAs(window, wxBottom, 4);
61 c->height.AsIs();
62 c->width.AsIs();
63 m_helpButton->SetConstraints(c);
c801d85f 64
4b5f3fe6
JS
65 c = new wxLayoutConstraints;
66 c->right.SameAs(m_helpButton, wxLeft, 4);
67 c->bottom.SameAs(window, wxBottom, 4);
68 c->height.AsIs();
69 c->width.AsIs();
70 m_cancelButton->SetConstraints(c);
71
72 c = new wxLayoutConstraints;
73 c->right.SameAs(m_cancelButton, wxLeft, 4);
74 c->bottom.SameAs(window, wxBottom, 4);
75 c->height.AsIs();
76 c->width.AsIs();
77 m_okButton->SetConstraints(c);
78
79 wxRect rect;
c801d85f
KB
80 rect.x = 5;
81 rect.y = 70;
82 // Could calculate the view width from the tab width and spacing,
83 // as below, but let's assume we have a fixed view width.
84// rect.width = view->GetTabWidth()*4 + 3*view->GetHorizontalTabSpacing();
85 rect.width = 326;
86 rect.height = 250;
87
88 view->SetViewRect(rect);
89
90 // Calculate the tab width for 4 tabs, based on a view width of 326 and
91 // the current horizontal spacing. Adjust the view width to exactly fit
92 // the tabs.
93 view->CalculateTabWidth(4, TRUE);
94
95 if (!view->AddTab(TEST_TAB_CAT, wxString("Cat")))
96 return;
97
98 if (!view->AddTab(TEST_TAB_DOG, wxString("Dog")))
99 return;
100 if (!view->AddTab(TEST_TAB_GUINEAPIG, wxString("Guinea Pig")))
101 return;
102 if (!view->AddTab(TEST_TAB_GOAT, wxString("Goat")))
103 return;
104 if (!view->AddTab(TEST_TAB_ANTEATER, wxString("Ant-eater")))
105 return;
106 if (!view->AddTab(TEST_TAB_SHEEP, wxString("Sheep")))
107 return;
108 if (!view->AddTab(TEST_TAB_COW, wxString("Cow")))
109 return;
110 if (!view->AddTab(TEST_TAB_HORSE, wxString("Horse")))
111 return;
112 if (!view->AddTab(TEST_TAB_PIG, wxString("Pig")))
113 return;
114 if (!view->AddTab(TEST_TAB_OSTRICH, wxString("Ostrich")))
115 return;
116 if (!view->AddTab(TEST_TAB_AARDVARK, wxString("Aardvark")))
117 return;
118 if (!view->AddTab(TEST_TAB_HUMMINGBIRD,wxString("Hummingbird")))
119 return;
120
121 // Add some panels
4b5f3fe6 122 wxPanel *panel1 = new wxPanel(window, -1, wxPoint(rect.x + 20, rect.y + 10), wxSize(290, 220), wxTAB_TRAVERSAL);
c801d85f
KB
123 (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
124 (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
125
126 view->AddTabWindow(TEST_TAB_CAT, panel1);
127
4b5f3fe6 128 wxPanel *panel2 = new wxPanel(window, -1, wxPoint(rect.x + 20, rect.y + 10), wxSize(290, 220));
c801d85f
KB
129
130 wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
131 (void)new wxListBox(panel2, -1, wxPoint(5, 5), wxSize(170, 80), 5, animals);
132
4b5f3fe6
JS
133 (void)new wxTextCtrl(panel2, -1, "Some notes about the animals in this house", wxPoint(5, 100), wxSize(170, 100),
134 wxTE_MULTILINE);
c801d85f
KB
135
136 view->AddTabWindow(TEST_TAB_DOG, panel2);
4b5f3fe6
JS
137 view->SetTabSelection(TEST_TAB_CAT);
138}
139
140BEGIN_EVENT_TABLE(MyDialog, wxTabbedDialog)
141 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
142 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
143END_EVENT_TABLE()
144
145MyDialog::MyDialog(wxWindow* parent, const wxWindowID id, const wxString& title,
146 const wxPoint& pos, const wxSize& size, const long windowStyle):
147 wxTabbedDialog(parent, id, title, pos, size, windowStyle)
148{
149 Init();
150}
151
152void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
153{
154 EndModal(wxID_OK);
155}
156
157void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
158{
159 EndModal(wxID_CANCEL);
160}
161
162void MyDialog::Init(void)
163{
164 int dialogWidth = 365;
165 int dialogHeight = 390;
c801d85f 166
4b5f3fe6
JS
167 // Note, omit the wxTAB_STYLE_COLOUR_INTERIOR, so we will guarantee a match
168 // with the panel background, and save a bit of time.
169 wxPanelTabView *view = new wxPanelTabView(this, wxTAB_STYLE_DRAW_BOX);
170
171 wxGetApp().InitTabView(view, this);
172
c801d85f 173 // Don't know why this is necessary under Motif...
e3e65dac 174#ifndef __WXMSW__
c801d85f
KB
175 this->SetSize(dialogWidth, dialogHeight-20);
176#endif
177
4b5f3fe6
JS
178 Layout();
179
180 this->Centre(wxBOTH);
181}
182
183BEGIN_EVENT_TABLE(MyFrame, wxFrame)
184 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
185 EVT_BUTTON(wxID_CANCEL, MyFrame::OnOK)
186 EVT_SIZE(MyFrame::OnSize)
187END_EVENT_TABLE()
188
189MyFrame::MyFrame(wxFrame* parent, const wxWindowID id, const wxString& title,
190 const wxPoint& pos, const wxSize& size, const long windowStyle):
191 wxFrame(parent, id, title, pos, size, windowStyle)
192{
193 m_panel = (wxTabbedPanel*) NULL;
194 m_view = (wxPanelTabView*) NULL;
195 Init();
196}
197
198void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
199{
200 this->Destroy();
201}
202
203void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
204{
205 this->Destroy();
206}
207
208void MyFrame::Init(void)
209{
4b5f3fe6 210 m_panel = new wxTabbedPanel(this, -1);
c801d85f 211
4b5f3fe6
JS
212 // Note, omit the wxTAB_STYLE_COLOUR_INTERIOR, so we will guarantee a match
213 // with the panel background, and save a bit of time.
214 m_view = new wxPanelTabView(m_panel, wxTAB_STYLE_DRAW_BOX);
215
216 wxGetApp().InitTabView(m_view, m_panel);
217
c801d85f 218 this->Centre(wxBOTH);
4b5f3fe6
JS
219
220 Show(TRUE);
221}
222
223void MyFrame::OnSize(wxSizeEvent& event)
224{
225 wxFrame::OnSize(event);
226
227 int cw, ch;
228 GetClientSize(& cw, & ch);
229
230 if (m_view && m_panel)
231 {
232 m_panel->Layout();
233
234 int tabHeight = m_view->GetTotalTabHeight();
235 wxRect rect;
236 rect.x = 4;
237 rect.y = tabHeight + 4;
238 rect.width = cw - 8;
239 rect.height = ch - 4 - rect.y - 30; // 30 for buttons
240
241 m_view->SetViewRect(rect);
242
243 m_view->Layout();
244
245 // Need to do it a 2nd time to get the tab height with
246 // the new view width
247 tabHeight = m_view->GetTotalTabHeight();
248 rect.x = 4;
249 rect.y = tabHeight + 4;
250 rect.width = cw - 8;
251 rect.height = ch - 4 - rect.y - 30; // 30 for buttons
252
253 m_view->SetViewRect(rect);
254
255 m_view->Layout();
256
257 // Move all the panels to the new view position and size
258 wxNode* node = m_view->GetWindows().First();
259 while (node)
260 {
261 wxWindow* win = (wxWindow*) node->Data();
262 win->SetSize(rect.x+2, rect.y+2, rect.width-4, rect.height-4);
263
264 node = node->Next();
265 }
266
267 m_panel->Refresh();
268 }
c801d85f
KB
269}
270