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