]> git.saurik.com Git - wxWidgets.git/blame - samples/tab/tab.cpp
reSWIGged for new runtime, etc.
[wxWidgets.git] / samples / tab / tab.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
8fef0a6e 2// Name: tab.cpp
c801d85f
KB
3// Purpose: Tab demo
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c)
2f6c54eb 9// Licence: wxWindows licence
c801d85f
KB
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"
8fef0a6e 24#include "tab.h"
c801d85f 25
f2d0790b
MB
26#if wxUSE_TAB_DIALOG == 0
27#error "wxUSE_TAB_DIALOG must be defined as 1 in setup.h."
28#endif
29
4b5f3fe6 30// If 1, use a dialog. Otherwise use a frame.
ce3ed50d 31#define USE_TABBED_DIALOG 1
4b5f3fe6
JS
32
33MyDialog* dialog = (MyDialog *) NULL;
34MyFrame* frame = (MyFrame *) NULL;
c801d85f
KB
35
36IMPLEMENT_APP(MyApp)
37
38bool MyApp::OnInit(void)
39{
4b5f3fe6
JS
40 // Create the main window
41#if USE_TABBED_DIALOG
c67daf87 42 dialog = new MyDialog((wxFrame *) NULL, -1, (char *) "Tabbed Dialog", wxPoint(-1, -1), wxSize(365, 390), wxDIALOG_MODAL|wxDEFAULT_DIALOG_STYLE);
c801d85f
KB
43
44 dialog->ShowModal();
45
46 // Quit immediately the dialog has been dismissed
47 return FALSE;
4b5f3fe6
JS
48#else
49 frame = new MyFrame((wxFrame*) NULL, -1, (char *) "Tabbed Panel", wxPoint(-1, -1), wxSize(365, 390), wxDEFAULT_FRAME_STYLE);
c801d85f 50
4b5f3fe6
JS
51 return TRUE;
52#endif
c801d85f
KB
53}
54
4b5f3fe6 55void MyApp::InitTabView(wxPanelTabView* view, wxWindow* window)
c801d85f 56{
4b5f3fe6
JS
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();
c801d85f 61
4b5f3fe6
JS
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);
c801d85f 68
4b5f3fe6
JS
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;
c801d85f
KB
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
4b5f3fe6 126 wxPanel *panel1 = new wxPanel(window, -1, wxPoint(rect.x + 20, rect.y + 10), wxSize(290, 220), wxTAB_TRAVERSAL);
c801d85f
KB
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
4b5f3fe6 132 wxPanel *panel2 = new wxPanel(window, -1, wxPoint(rect.x + 20, rect.y + 10), wxSize(290, 220));
c801d85f
KB
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
4b5f3fe6
JS
137 (void)new wxTextCtrl(panel2, -1, "Some notes about the animals in this house", wxPoint(5, 100), wxSize(170, 100),
138 wxTE_MULTILINE);
c801d85f
KB
139
140 view->AddTabWindow(TEST_TAB_DOG, panel2);
4b5f3fe6
JS
141 view->SetTabSelection(TEST_TAB_CAT);
142}
143
144BEGIN_EVENT_TABLE(MyDialog, wxTabbedDialog)
145 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
146 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
147END_EVENT_TABLE()
148
149MyDialog::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
156void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
157{
158 EndModal(wxID_OK);
159}
160
161void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
162{
163 EndModal(wxID_CANCEL);
164}
165
166void MyDialog::Init(void)
167{
168 int dialogWidth = 365;
169 int dialogHeight = 390;
c801d85f 170
4b5f3fe6
JS
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.
f2d0790b 173 wxPanelTabView *view = new wxPanelTabView((wxPanel *)this, wxTAB_STYLE_DRAW_BOX);
4b5f3fe6
JS
174
175 wxGetApp().InitTabView(view, this);
176
c801d85f 177 // Don't know why this is necessary under Motif...
e3e65dac 178#ifndef __WXMSW__
c801d85f
KB
179 this->SetSize(dialogWidth, dialogHeight-20);
180#endif
181
4b5f3fe6
JS
182 Layout();
183
184 this->Centre(wxBOTH);
185}
186
187BEGIN_EVENT_TABLE(MyFrame, wxFrame)
188 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
189 EVT_BUTTON(wxID_CANCEL, MyFrame::OnOK)
190 EVT_SIZE(MyFrame::OnSize)
191END_EVENT_TABLE()
192
193MyFrame::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
202void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
203{
204 this->Destroy();
205}
206
207void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
208{
209 this->Destroy();
210}
211
212void MyFrame::Init(void)
213{
4b5f3fe6 214 m_panel = new wxTabbedPanel(this, -1);
c801d85f 215
4b5f3fe6
JS
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
c801d85f 222 this->Centre(wxBOTH);
4b5f3fe6
JS
223
224 Show(TRUE);
225}
226
227void 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
60a67569 247 m_view->LayoutTabs();
4b5f3fe6
JS
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
60a67569 259 m_view->LayoutTabs();
4b5f3fe6
JS
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 }
c801d85f
KB
273}
274