[02c7c1a] | 1 | // |
---|
| 2 | // paneldlg.cc |
---|
| 3 | // |
---|
| 4 | // Dialog boxes with multiple pages and a selector panel. |
---|
| 5 | // |
---|
| 6 | // Copyright (C) 2002 Mark R. Shinwell |
---|
| 7 | // |
---|
| 8 | // This program is free software; you can redistribute it and/or modify |
---|
| 9 | // it under the terms of the GNU General Public License as published by |
---|
| 10 | // the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | // (at your option) any later version. |
---|
| 12 | // |
---|
| 13 | // This program is distributed in the hope that it will be useful, |
---|
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | // GNU General Public License for more details. |
---|
| 17 | // |
---|
| 18 | // You should have received a copy of the GNU General Public License |
---|
| 19 | // along with this program; if not, write to the Free Software |
---|
| 20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 21 | // |
---|
| 22 | |
---|
| 23 | #include "paneldlg.h" |
---|
| 24 | |
---|
| 25 | #include <assert.h> |
---|
| 26 | |
---|
| 27 | static const wxWindowID ID_PAGE_BASE = 100; |
---|
| 28 | static const wxWindowID ID_PAGE_BASE_IMG = 150; |
---|
[2f2509e] | 29 | static const wxWindowID ID_PAGE_BASE_PANEL = 200; |
---|
| 30 | static const wxWindowID ID_PAGE_BASE_IMG_END = ID_PAGE_BASE_PANEL - 1; |
---|
| 31 | |
---|
| 32 | BEGIN_EVENT_TABLE(PanelDlg, wxDialog) |
---|
| 33 | EVT_COMMAND_RANGE(ID_PAGE_BASE_IMG, ID_PAGE_BASE_IMG_END, wxEVT_COMMAND_BUTTON_CLICKED, PanelDlg::OnPageChange) |
---|
| 34 | END_EVENT_TABLE() |
---|
[02c7c1a] | 35 | |
---|
| 36 | PanelDlg::PanelDlg(wxWindow* parent, wxWindowID id, const wxString& title) : |
---|
[2f2509e] | 37 | wxDialog(parent, id, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) |
---|
[02c7c1a] | 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | PanelDlg::~PanelDlg() |
---|
| 42 | { |
---|
| 43 | } |
---|
| 44 | |
---|
[2f2509e] | 45 | void PanelDlg::OnPageChange(wxCommandEvent& event) |
---|
| 46 | { |
---|
| 47 | // The user has requested a change of page. |
---|
| 48 | |
---|
| 49 | // Identify the new page. |
---|
| 50 | int page = event.GetId() - ID_PAGE_BASE_IMG; |
---|
| 51 | assert(page >= 0 && page < m_Pages.size()); |
---|
| 52 | list<PanelDlgPage*>::iterator iter = m_Pages.begin(); |
---|
| 53 | for (int x = 0; x < page; x++) iter++; |
---|
| 54 | PanelDlgPage* page_ptr = *iter; |
---|
| 55 | assert(page_ptr); |
---|
| 56 | |
---|
| 57 | if (page_ptr == m_CurrentPage) return; |
---|
| 58 | |
---|
| 59 | // Remove the current page from display. |
---|
| 60 | m_CurrentPage->Hide(); |
---|
| 61 | m_VertSizer->Remove(m_CurrentPage); |
---|
| 62 | m_VertSizer->Layout(); |
---|
| 63 | |
---|
| 64 | // Display the new page. |
---|
| 65 | m_VertSizer->Prepend(page_ptr, 1 /* fill available space */, wxALL, 4); |
---|
| 66 | m_CurrentPage = page_ptr; |
---|
| 67 | |
---|
| 68 | // Force a re-layout of the sizer. |
---|
| 69 | m_VertSizer->Layout(); |
---|
| 70 | page_ptr->Show(); |
---|
| 71 | } |
---|
| 72 | |
---|
[02c7c1a] | 73 | void PanelDlg::SetPages(list<PanelDlgPage*> pages) |
---|
| 74 | { |
---|
| 75 | assert(pages.size() >= 1); |
---|
| 76 | |
---|
[2f2509e] | 77 | m_Pages = pages; |
---|
| 78 | |
---|
[02c7c1a] | 79 | // Create sizers. |
---|
| 80 | wxBoxSizer* horiz_sizer = new wxBoxSizer(wxHORIZONTAL); |
---|
| 81 | m_VertSizer = new wxBoxSizer(wxVERTICAL); |
---|
| 82 | |
---|
| 83 | // Create the left-hand page button panel. |
---|
[2f2509e] | 84 | wxScrolledWindow* scrolled_win = new wxScrolledWindow(this, 3000, wxDefaultPosition, wxDefaultSize, |
---|
| 85 | wxVSCROLL | wxSUNKEN_BORDER); |
---|
| 86 | wxPanel* page_panel = new wxPanel(scrolled_win, 2000); |
---|
[02c7c1a] | 87 | |
---|
| 88 | // Get the background colour and calculate a "darker" version of it. |
---|
| 89 | wxColour col = page_panel->GetBackgroundColour(); |
---|
| 90 | |
---|
| 91 | unsigned char r = (unsigned char)(double(col.Red()) * 0.4); |
---|
| 92 | unsigned char g = (unsigned char)(double(col.Green()) * 0.4); |
---|
| 93 | unsigned char b = (unsigned char)(double(col.Blue()) * 0.4); |
---|
| 94 | |
---|
| 95 | col.Set(r, g, b); |
---|
| 96 | |
---|
| 97 | // Fill the page button panel. |
---|
| 98 | wxBoxSizer* page_panel_sizer = new wxBoxSizer(wxVERTICAL); |
---|
| 99 | const int PAGE_ICON_HEAD_FOOT = 12; |
---|
| 100 | const int PAGE_ICON_HEIGHT = 75; |
---|
[2f2509e] | 101 | const int PAGE_PANEL_WIDTH = int((48 + (2*PAGE_ICON_HEAD_FOOT)) * 4.0/3.0); |
---|
[02c7c1a] | 102 | list<PanelDlgPage*>::iterator iter = pages.begin(); |
---|
| 103 | int page_num = 0; |
---|
| 104 | while (iter != pages.end()) { |
---|
| 105 | PanelDlgPage* page = *iter++; |
---|
| 106 | |
---|
| 107 | // Get the text and icon for this button. |
---|
| 108 | const wxString& text = page->GetName(); |
---|
| 109 | const wxBitmap& icon = page->GetIcon(); |
---|
| 110 | |
---|
| 111 | // Create the button and label together inside a panel. |
---|
| 112 | wxPanel* panel = new wxPanel(page_panel, ID_PAGE_BASE_PANEL + page_num); |
---|
| 113 | wxBoxSizer* panel_sizer = new wxBoxSizer(wxVERTICAL); |
---|
| 114 | wxBitmapButton* img_button = new wxBitmapButton(panel, ID_PAGE_BASE_IMG + page_num, icon, |
---|
| 115 | wxPoint(0, 0), wxSize(48, 48)); |
---|
| 116 | wxStaticText* label = new wxStaticText(panel, ID_PAGE_BASE + page_num, text); |
---|
[2f2509e] | 117 | page_panel_sizer->Add(8, PAGE_ICON_HEAD_FOOT); // add a spacer |
---|
[02c7c1a] | 118 | panel_sizer->Add(img_button, 0, wxALIGN_CENTER, 4); |
---|
| 119 | panel_sizer->Add(label, 0, wxALIGN_CENTER, 4); |
---|
| 120 | panel->SetAutoLayout(true); |
---|
| 121 | panel->SetSizer(panel_sizer); |
---|
[2f2509e] | 122 | panel->SetSize(PAGE_PANEL_WIDTH - 8, PAGE_ICON_HEIGHT - PAGE_ICON_HEAD_FOOT); |
---|
[02c7c1a] | 123 | |
---|
| 124 | page_panel_sizer->Add(panel, 0 /* not vertically stretchable */, wxALIGN_CENTER); |
---|
| 125 | panel->SetBackgroundColour(col); |
---|
| 126 | |
---|
| 127 | page_num++; |
---|
| 128 | } |
---|
[2f2509e] | 129 | page_panel_sizer->Add(8, PAGE_ICON_HEAD_FOOT); // add a spacer |
---|
[02c7c1a] | 130 | page_panel->SetAutoLayout(true); |
---|
| 131 | page_panel->SetSizer(page_panel_sizer); |
---|
[2f2509e] | 132 | int height = PAGE_ICON_HEAD_FOOT + (page_num * PAGE_ICON_HEIGHT); |
---|
[02c7c1a] | 133 | page_panel->SetSizeHints(PAGE_PANEL_WIDTH, height); |
---|
| 134 | page_panel->SetSize(PAGE_PANEL_WIDTH, height); |
---|
[2f2509e] | 135 | scrolled_win->SetScrollbars(0, height / page_num, 0, page_num); |
---|
| 136 | scrolled_win->SetSize(/* FIXME */ 16 + PAGE_PANEL_WIDTH, 500); |
---|
[02c7c1a] | 137 | |
---|
| 138 | // Darken the background colour. |
---|
| 139 | page_panel->SetBackgroundColour(col); |
---|
| 140 | |
---|
| 141 | // Fill the horizontal sizer. |
---|
[2f2509e] | 142 | horiz_sizer->Add(scrolled_win, 0 /* not horizontally stretchable */, wxEXPAND | wxALL, 2); |
---|
[02c7c1a] | 143 | horiz_sizer->Add(m_VertSizer, 1 /* fill available space */, wxEXPAND | wxALL, 2); |
---|
| 144 | |
---|
| 145 | // Retrieve the panel for the first page. |
---|
| 146 | PanelDlgPage* first_page = *(pages.begin()); |
---|
| 147 | assert(first_page); |
---|
[2f2509e] | 148 | m_CurrentPage = first_page; |
---|
[02c7c1a] | 149 | |
---|
| 150 | // Create the OK/Cancel button panel. |
---|
[2f2509e] | 151 | m_ButtonSizer = new wxBoxSizer(wxHORIZONTAL); |
---|
[02c7c1a] | 152 | wxButton* ok_button = new wxButton(this, wxID_OK, "OK"); |
---|
[2f2509e] | 153 | ok_button->SetDefault(); |
---|
[02c7c1a] | 154 | wxButton* cancel_button = new wxButton(this, wxID_CANCEL, "Cancel"); |
---|
[2f2509e] | 155 | m_ButtonSizer->Add(ok_button, 0 /* not horizontally stretchable */, wxALIGN_RIGHT | wxRIGHT, 4); |
---|
| 156 | m_ButtonSizer->Add(cancel_button, 0 /* not horizontally stretchable */, wxALIGN_RIGHT); |
---|
[02c7c1a] | 157 | |
---|
| 158 | // Fill the sizer holding the page and the button panel. |
---|
| 159 | m_VertSizer->Add(first_page, 1 /* fill available space */, wxALIGN_TOP | wxALL, 4); |
---|
[2f2509e] | 160 | m_VertSizer->Add(m_ButtonSizer, 0 /* not vertically stretchable */, wxALIGN_RIGHT | wxALL, 4); |
---|
[02c7c1a] | 161 | |
---|
| 162 | // Cause the dialog to use the horizontal sizer. |
---|
| 163 | SetAutoLayout(true); |
---|
| 164 | SetSizer(horiz_sizer); |
---|
| 165 | horiz_sizer->Fit(this); |
---|
| 166 | |
---|
[2f2509e] | 167 | // Set a reasonable size and centre the dialog with respect to the parent window. |
---|
| 168 | SetSize(500, PAGE_ICON_HEIGHT*4 + PAGE_ICON_HEAD_FOOT); |
---|
| 169 | CentreOnParent(); |
---|
[02c7c1a] | 170 | } |
---|
| 171 | |
---|