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