| 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 1 |
| 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 | 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(); |
| 57 | |
| 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); |
| 64 | |
| 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; |
| 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 |
| 122 | wxPanel *panel1 = new wxPanel(window, -1, wxPoint(rect.x + 20, rect.y + 10), wxSize(290, 220), wxTAB_TRAVERSAL); |
| 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 | |
| 128 | wxPanel *panel2 = new wxPanel(window, -1, wxPoint(rect.x + 20, rect.y + 10), wxSize(290, 220)); |
| 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 | |
| 133 | (void)new wxTextCtrl(panel2, -1, "Some notes about the animals in this house", wxPoint(5, 100), wxSize(170, 100), |
| 134 | wxTE_MULTILINE); |
| 135 | |
| 136 | view->AddTabWindow(TEST_TAB_DOG, panel2); |
| 137 | view->SetTabSelection(TEST_TAB_CAT); |
| 138 | } |
| 139 | |
| 140 | BEGIN_EVENT_TABLE(MyDialog, wxTabbedDialog) |
| 141 | EVT_BUTTON(wxID_OK, MyDialog::OnOK) |
| 142 | EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK) |
| 143 | END_EVENT_TABLE() |
| 144 | |
| 145 | MyDialog::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 | |
| 152 | void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) ) |
| 153 | { |
| 154 | EndModal(wxID_OK); |
| 155 | } |
| 156 | |
| 157 | void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) ) |
| 158 | { |
| 159 | EndModal(wxID_CANCEL); |
| 160 | } |
| 161 | |
| 162 | void MyDialog::Init(void) |
| 163 | { |
| 164 | int dialogWidth = 365; |
| 165 | int dialogHeight = 390; |
| 166 | |
| 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 | |
| 173 | // Don't know why this is necessary under Motif... |
| 174 | #ifndef __WXMSW__ |
| 175 | this->SetSize(dialogWidth, dialogHeight-20); |
| 176 | #endif |
| 177 | |
| 178 | Layout(); |
| 179 | |
| 180 | this->Centre(wxBOTH); |
| 181 | } |
| 182 | |
| 183 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
| 184 | EVT_BUTTON(wxID_OK, MyFrame::OnOK) |
| 185 | EVT_BUTTON(wxID_CANCEL, MyFrame::OnOK) |
| 186 | EVT_SIZE(MyFrame::OnSize) |
| 187 | END_EVENT_TABLE() |
| 188 | |
| 189 | MyFrame::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 | |
| 198 | void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) ) |
| 199 | { |
| 200 | this->Destroy(); |
| 201 | } |
| 202 | |
| 203 | void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) ) |
| 204 | { |
| 205 | this->Destroy(); |
| 206 | } |
| 207 | |
| 208 | void MyFrame::Init(void) |
| 209 | { |
| 210 | m_panel = new wxTabbedPanel(this, -1); |
| 211 | |
| 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 | |
| 218 | this->Centre(wxBOTH); |
| 219 | |
| 220 | Show(TRUE); |
| 221 | } |
| 222 | |
| 223 | void 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->LayoutTabs(); |
| 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->LayoutTabs(); |
| 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 | } |
| 269 | } |
| 270 | |