[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 |
---|
[ecbc6c18] | 20 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[02c7c1a] | 21 | // |
---|
| 22 | |
---|
[cbfa50d] | 23 | #ifdef HAVE_CONFIG_H |
---|
| 24 | #include <config.h> |
---|
| 25 | #endif |
---|
| 26 | |
---|
[02c7c1a] | 27 | #include "paneldlg.h" |
---|
| 28 | |
---|
| 29 | #include <assert.h> |
---|
| 30 | |
---|
| 31 | static const wxWindowID ID_PAGE_BASE = 100; |
---|
| 32 | static const wxWindowID ID_PAGE_BASE_IMG = 150; |
---|
[2f2509e] | 33 | static const wxWindowID ID_PAGE_BASE_PANEL = 200; |
---|
| 34 | static const wxWindowID ID_PAGE_BASE_IMG_END = ID_PAGE_BASE_PANEL - 1; |
---|
| 35 | |
---|
[55a5a1d] | 36 | static const int PAGE_ICON_HEAD_FOOT = 12; |
---|
| 37 | static const int PAGE_ICON_HEIGHT = 75; |
---|
| 38 | static const int PAGE_PANEL_WIDTH = int((48 + (2*PAGE_ICON_HEAD_FOOT)) * 4.0/3.0); |
---|
| 39 | static const int EDGE_MARGIN = 4; |
---|
| 40 | static const int EDGE_MARGIN_V = 3; |
---|
| 41 | static const int SCROLLED_WIN_WIDTH = 16 + PAGE_PANEL_WIDTH; |
---|
| 42 | static const int DIALOG_WIDTH = 500; |
---|
| 43 | static const int DIALOG_HEIGHT = PAGE_ICON_HEIGHT*4 + PAGE_ICON_HEAD_FOOT; |
---|
| 44 | |
---|
[2f2509e] | 45 | BEGIN_EVENT_TABLE(PanelDlg, wxDialog) |
---|
| 46 | EVT_COMMAND_RANGE(ID_PAGE_BASE_IMG, ID_PAGE_BASE_IMG_END, wxEVT_COMMAND_BUTTON_CLICKED, PanelDlg::OnPageChange) |
---|
| 47 | END_EVENT_TABLE() |
---|
[02c7c1a] | 48 | |
---|
[d0867c3] | 49 | PanelDlg::PanelDlg(wxWindow* parent, wxWindowID id, const wxString& title) : wxDialog(parent, id, title) |
---|
[02c7c1a] | 50 | { |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | PanelDlg::~PanelDlg() |
---|
| 54 | { |
---|
| 55 | } |
---|
| 56 | |
---|
[55a5a1d] | 57 | void PanelDlg::PositionPage() |
---|
| 58 | { |
---|
| 59 | // Position the current page on the dialog. |
---|
[096e56c] | 60 | |
---|
[55a5a1d] | 61 | wxSize button_size = wxButton::GetDefaultSize(); |
---|
| 62 | |
---|
| 63 | m_CurrentPage->SetSizeHints(-1, -1, |
---|
[203d2a7] | 64 | DIALOG_WIDTH - EDGE_MARGIN*5 - SCROLLED_WIN_WIDTH, |
---|
| 65 | DIALOG_HEIGHT - EDGE_MARGIN_V*8 - button_size.GetHeight()); |
---|
[d0867c3] | 66 | m_CurrentPage->SetSize(SCROLLED_WIN_WIDTH + EDGE_MARGIN*3, EDGE_MARGIN_V*2, |
---|
[203d2a7] | 67 | DIALOG_WIDTH - EDGE_MARGIN*5 - SCROLLED_WIN_WIDTH, |
---|
| 68 | DIALOG_HEIGHT - EDGE_MARGIN_V*8 - button_size.GetHeight()); |
---|
[55a5a1d] | 69 | } |
---|
| 70 | |
---|
[2f2509e] | 71 | void PanelDlg::OnPageChange(wxCommandEvent& event) |
---|
| 72 | { |
---|
| 73 | // The user has requested a change of page. |
---|
[096e56c] | 74 | |
---|
[2f2509e] | 75 | // Identify the new page. |
---|
| 76 | int page = event.GetId() - ID_PAGE_BASE_IMG; |
---|
| 77 | assert(page >= 0 && page < m_Pages.size()); |
---|
| 78 | list<PanelDlgPage*>::iterator iter = m_Pages.begin(); |
---|
| 79 | for (int x = 0; x < page; x++) iter++; |
---|
| 80 | PanelDlgPage* page_ptr = *iter; |
---|
| 81 | assert(page_ptr); |
---|
| 82 | |
---|
| 83 | if (page_ptr == m_CurrentPage) return; |
---|
[096e56c] | 84 | |
---|
[2f2509e] | 85 | // Remove the current page from display. |
---|
| 86 | m_CurrentPage->Hide(); |
---|
| 87 | |
---|
| 88 | // Display the new page. |
---|
| 89 | m_CurrentPage = page_ptr; |
---|
[55a5a1d] | 90 | m_CurrentPage->Show(); |
---|
| 91 | PositionPage(); |
---|
[2f2509e] | 92 | } |
---|
| 93 | |
---|
[02c7c1a] | 94 | void PanelDlg::SetPages(list<PanelDlgPage*> pages) |
---|
| 95 | { |
---|
| 96 | assert(pages.size() >= 1); |
---|
| 97 | |
---|
[2f2509e] | 98 | m_Pages = pages; |
---|
| 99 | |
---|
[02c7c1a] | 100 | // Create the left-hand page button panel. |
---|
[2f2509e] | 101 | wxScrolledWindow* scrolled_win = new wxScrolledWindow(this, 3000, wxDefaultPosition, wxDefaultSize, |
---|
| 102 | wxVSCROLL | wxSUNKEN_BORDER); |
---|
| 103 | wxPanel* page_panel = new wxPanel(scrolled_win, 2000); |
---|
[02c7c1a] | 104 | |
---|
| 105 | // Get the background colour and calculate a "darker" version of it. |
---|
| 106 | wxColour col = page_panel->GetBackgroundColour(); |
---|
[096e56c] | 107 | |
---|
[d0867c3] | 108 | unsigned char r = (unsigned char)(double(col.Red()) * 0.5); |
---|
| 109 | unsigned char g = (unsigned char)(double(col.Green()) * 0.5); |
---|
| 110 | unsigned char b = (unsigned char)(double(col.Blue()) * 0.5); |
---|
[02c7c1a] | 111 | |
---|
| 112 | col.Set(r, g, b); |
---|
| 113 | |
---|
| 114 | // Fill the page button panel. |
---|
| 115 | wxBoxSizer* page_panel_sizer = new wxBoxSizer(wxVERTICAL); |
---|
| 116 | list<PanelDlgPage*>::iterator iter = pages.begin(); |
---|
| 117 | int page_num = 0; |
---|
| 118 | while (iter != pages.end()) { |
---|
| 119 | PanelDlgPage* page = *iter++; |
---|
| 120 | |
---|
| 121 | // Get the text and icon for this button. |
---|
| 122 | const wxString& text = page->GetName(); |
---|
| 123 | const wxBitmap& icon = page->GetIcon(); |
---|
| 124 | |
---|
| 125 | // Create the button and label together inside a panel. |
---|
| 126 | wxPanel* panel = new wxPanel(page_panel, ID_PAGE_BASE_PANEL + page_num); |
---|
| 127 | wxBoxSizer* panel_sizer = new wxBoxSizer(wxVERTICAL); |
---|
| 128 | wxBitmapButton* img_button = new wxBitmapButton(panel, ID_PAGE_BASE_IMG + page_num, icon, |
---|
| 129 | wxPoint(0, 0), wxSize(48, 48)); |
---|
| 130 | wxStaticText* label = new wxStaticText(panel, ID_PAGE_BASE + page_num, text); |
---|
[2f2509e] | 131 | page_panel_sizer->Add(8, PAGE_ICON_HEAD_FOOT); // add a spacer |
---|
[02c7c1a] | 132 | panel_sizer->Add(img_button, 0, wxALIGN_CENTER, 4); |
---|
| 133 | panel_sizer->Add(label, 0, wxALIGN_CENTER, 4); |
---|
| 134 | panel->SetAutoLayout(true); |
---|
| 135 | panel->SetSizer(panel_sizer); |
---|
[2f2509e] | 136 | panel->SetSize(PAGE_PANEL_WIDTH - 8, PAGE_ICON_HEIGHT - PAGE_ICON_HEAD_FOOT); |
---|
[02c7c1a] | 137 | |
---|
| 138 | page_panel_sizer->Add(panel, 0 /* not vertically stretchable */, wxALIGN_CENTER); |
---|
| 139 | panel->SetBackgroundColour(col); |
---|
| 140 | |
---|
| 141 | page_num++; |
---|
| 142 | } |
---|
[2f2509e] | 143 | page_panel_sizer->Add(8, PAGE_ICON_HEAD_FOOT); // add a spacer |
---|
[02c7c1a] | 144 | page_panel->SetAutoLayout(true); |
---|
| 145 | page_panel->SetSizer(page_panel_sizer); |
---|
[2f2509e] | 146 | int height = PAGE_ICON_HEAD_FOOT + (page_num * PAGE_ICON_HEIGHT); |
---|
[02c7c1a] | 147 | page_panel->SetSizeHints(PAGE_PANEL_WIDTH, height); |
---|
| 148 | page_panel->SetSize(PAGE_PANEL_WIDTH, height); |
---|
[2f2509e] | 149 | scrolled_win->SetScrollbars(0, height / page_num, 0, page_num); |
---|
[55a5a1d] | 150 | scrolled_win->SetSize(EDGE_MARGIN, EDGE_MARGIN, SCROLLED_WIN_WIDTH, DIALOG_HEIGHT - EDGE_MARGIN_V*2); |
---|
[096e56c] | 151 | |
---|
[55a5a1d] | 152 | // Darken the background colour for the page selector panel. |
---|
[02c7c1a] | 153 | page_panel->SetBackgroundColour(col); |
---|
| 154 | |
---|
[55a5a1d] | 155 | // Create the OK/Cancel button panel. |
---|
| 156 | wxSize button_size = wxButton::GetDefaultSize(); |
---|
[203d2a7] | 157 | wxButton* close_button = new wxButton(this, wxID_CANCEL, "Close", |
---|
| 158 | wxPoint(DIALOG_WIDTH - button_size.GetWidth() - EDGE_MARGIN*4, |
---|
| 159 | DIALOG_HEIGHT - button_size.GetHeight() - EDGE_MARGIN_V*4)); |
---|
| 160 | close_button->SetDefault(); |
---|
[02c7c1a] | 161 | |
---|
[55a5a1d] | 162 | // Retrieve the panel for the first page and put it in the correct place. |
---|
[02c7c1a] | 163 | PanelDlgPage* first_page = *(pages.begin()); |
---|
| 164 | assert(first_page); |
---|
[2f2509e] | 165 | m_CurrentPage = first_page; |
---|
[55a5a1d] | 166 | PositionPage(); |
---|
[d0867c3] | 167 | m_CurrentPage->Show(); |
---|
[02c7c1a] | 168 | |
---|
[2f2509e] | 169 | // Set a reasonable size and centre the dialog with respect to the parent window. |
---|
[55a5a1d] | 170 | SetSize(DIALOG_WIDTH, DIALOG_HEIGHT); |
---|
[2f2509e] | 171 | CentreOnParent(); |
---|
[02c7c1a] | 172 | } |
---|
| 173 | |
---|