]>
git.saurik.com Git - wxWidgets.git/blob - samples/tab/tab.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
26 #if wxUSE_TAB_DIALOG == 0
27 #error "wxUSE_TAB_DIALOG must be defined as 1 in setup.h."
30 // If 1, use a dialog. Otherwise use a frame.
31 #define USE_TABBED_DIALOG 1
33 MyDialog
* dialog
= (MyDialog
*) NULL
;
34 MyFrame
* frame
= (MyFrame
*) NULL
;
38 bool MyApp::OnInit(void)
40 // Create the main window
42 dialog
= new MyDialog((wxFrame
*) NULL
, -1, (char *) "Tabbed Dialog", wxPoint(-1, -1), wxSize(365, 390), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
);
46 // Quit immediately the dialog has been dismissed
49 frame
= new MyFrame((wxFrame
*) NULL
, -1, (char *) "Tabbed Panel", wxPoint(-1, -1), wxSize(365, 390), wxDEFAULT_FRAME_STYLE
);
55 void MyApp::InitTabView(wxPanelTabView
* view
, wxWindow
* window
)
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();
62 wxLayoutConstraints
* c
= new wxLayoutConstraints
;
63 c
->right
.SameAs(window
, wxRight
, 4);
64 c
->bottom
.SameAs(window
, wxBottom
, 4);
67 m_helpButton
->SetConstraints(c
);
69 c
= new wxLayoutConstraints
;
70 c
->right
.SameAs(m_helpButton
, wxLeft
, 4);
71 c
->bottom
.SameAs(window
, wxBottom
, 4);
74 m_cancelButton
->SetConstraints(c
);
76 c
= new wxLayoutConstraints
;
77 c
->right
.SameAs(m_cancelButton
, wxLeft
, 4);
78 c
->bottom
.SameAs(window
, wxBottom
, 4);
81 m_okButton
->SetConstraints(c
);
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();
92 view
->SetViewRect(rect
);
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
97 view
->CalculateTabWidth(4, TRUE
);
99 if (!view
->AddTab(TEST_TAB_CAT
, wxString("Cat")))
102 if (!view
->AddTab(TEST_TAB_DOG
, wxString("Dog")))
104 if (!view
->AddTab(TEST_TAB_GUINEAPIG
, wxString("Guinea Pig")))
106 if (!view
->AddTab(TEST_TAB_GOAT
, wxString("Goat")))
108 if (!view
->AddTab(TEST_TAB_ANTEATER
, wxString("Ant-eater")))
110 if (!view
->AddTab(TEST_TAB_SHEEP
, wxString("Sheep")))
112 if (!view
->AddTab(TEST_TAB_COW
, wxString("Cow")))
114 if (!view
->AddTab(TEST_TAB_HORSE
, wxString("Horse")))
116 if (!view
->AddTab(TEST_TAB_PIG
, wxString("Pig")))
118 if (!view
->AddTab(TEST_TAB_OSTRICH
, wxString("Ostrich")))
120 if (!view
->AddTab(TEST_TAB_AARDVARK
, wxString("Aardvark")))
122 if (!view
->AddTab(TEST_TAB_HUMMINGBIRD
,wxString("Hummingbird")))
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));
130 view
->AddTabWindow(TEST_TAB_CAT
, panel1
);
132 wxPanel
*panel2
= new wxPanel(window
, -1, wxPoint(rect
.x
+ 20, rect
.y
+ 10), wxSize(290, 220));
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
);
137 (void)new wxTextCtrl(panel2
, -1, "Some notes about the animals in this house", wxPoint(5, 100), wxSize(170, 100),
140 view
->AddTabWindow(TEST_TAB_DOG
, panel2
);
141 view
->SetTabSelection(TEST_TAB_CAT
);
144 BEGIN_EVENT_TABLE(MyDialog
, wxTabbedDialog
)
145 EVT_BUTTON(wxID_OK
, MyDialog::OnOK
)
146 EVT_BUTTON(wxID_CANCEL
, MyDialog::OnOK
)
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
)
156 void MyDialog::OnOK(wxCommandEvent
& WXUNUSED(event
) )
161 void MyDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
) )
163 EndModal(wxID_CANCEL
);
166 void MyDialog::Init(void)
168 int dialogWidth
= 365;
169 int dialogHeight
= 390;
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
);
175 wxGetApp().InitTabView(view
, this);
177 // Don't know why this is necessary under Motif...
179 this->SetSize(dialogWidth
, dialogHeight
-20);
184 this->Centre(wxBOTH
);
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
)
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
)
197 m_panel
= (wxTabbedPanel
*) NULL
;
198 m_view
= (wxPanelTabView
*) NULL
;
202 void MyFrame::OnOK(wxCommandEvent
& WXUNUSED(event
) )
207 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
) )
212 void MyFrame::Init(void)
214 m_panel
= new wxTabbedPanel(this, -1);
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
);
220 wxGetApp().InitTabView(m_view
, m_panel
);
222 this->Centre(wxBOTH
);
227 void MyFrame::OnSize(wxSizeEvent
& event
)
229 wxFrame::OnSize(event
);
232 GetClientSize(& cw
, & ch
);
234 if (m_view
&& m_panel
)
238 int tabHeight
= m_view
->GetTotalTabHeight();
241 rect
.y
= tabHeight
+ 4;
243 rect
.height
= ch
- 4 - rect
.y
- 30; // 30 for buttons
245 m_view
->SetViewRect(rect
);
247 m_view
->LayoutTabs();
249 // Need to do it a 2nd time to get the tab height with
250 // the new view width
251 tabHeight
= m_view
->GetTotalTabHeight();
253 rect
.y
= tabHeight
+ 4;
255 rect
.height
= ch
- 4 - rect
.y
- 30; // 30 for buttons
257 m_view
->SetViewRect(rect
);
259 m_view
->LayoutTabs();
261 // Move all the panels to the new view position and size
262 wxNode
* node
= m_view
->GetWindows().First();
265 wxWindow
* win
= (wxWindow
*) node
->Data();
266 win
->SetSize(rect
.x
+2, rect
.y
+2, rect
.width
-4, rect
.height
-4);