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