| 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 21 | // |
|---|
| 22 | |
|---|
| 23 | #ifdef HAVE_CONFIG_H |
|---|
| 24 | #include <config.h> |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 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; |
|---|
| 33 | static const wxWindowID ID_PAGE_BASE_PANEL = 200; |
|---|
| 34 | static const wxWindowID ID_PAGE_BASE_IMG_END = ID_PAGE_BASE_PANEL - 1; |
|---|
| 35 | |
|---|
| 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 | |
|---|
| 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() |
|---|
| 48 | |
|---|
| 49 | PanelDlg::PanelDlg(wxWindow* parent, wxWindowID id, const wxString& title) : wxDialog(parent, id, title) |
|---|
| 50 | { |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | PanelDlg::~PanelDlg() |
|---|
| 54 | { |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | void PanelDlg::PositionPage() |
|---|
| 58 | { |
|---|
| 59 | // Position the current page on the dialog. |
|---|
| 60 | |
|---|
| 61 | wxSize button_size = wxButton::GetDefaultSize(); |
|---|
| 62 | |
|---|
| 63 | m_CurrentPage->SetSizeHints(-1, -1, |
|---|
| 64 | DIALOG_WIDTH - EDGE_MARGIN*5 - SCROLLED_WIN_WIDTH, |
|---|
| 65 | DIALOG_HEIGHT - EDGE_MARGIN_V*8 - button_size.GetHeight()); |
|---|
| 66 | m_CurrentPage->SetSize(SCROLLED_WIN_WIDTH + EDGE_MARGIN*3, EDGE_MARGIN_V*2, |
|---|
| 67 | DIALOG_WIDTH - EDGE_MARGIN*5 - SCROLLED_WIN_WIDTH, |
|---|
| 68 | DIALOG_HEIGHT - EDGE_MARGIN_V*8 - button_size.GetHeight()); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void PanelDlg::OnPageChange(wxCommandEvent& event) |
|---|
| 72 | { |
|---|
| 73 | // The user has requested a change of page. |
|---|
| 74 | |
|---|
| 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; |
|---|
| 84 | |
|---|
| 85 | // Remove the current page from display. |
|---|
| 86 | m_CurrentPage->Hide(); |
|---|
| 87 | |
|---|
| 88 | // Display the new page. |
|---|
| 89 | m_CurrentPage = page_ptr; |
|---|
| 90 | m_CurrentPage->Show(); |
|---|
| 91 | PositionPage(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void PanelDlg::SetPages(list<PanelDlgPage*> pages) |
|---|
| 95 | { |
|---|
| 96 | assert(pages.size() >= 1); |
|---|
| 97 | |
|---|
| 98 | m_Pages = pages; |
|---|
| 99 | |
|---|
| 100 | // Create the left-hand page button panel. |
|---|
| 101 | wxScrolledWindow* scrolled_win = new wxScrolledWindow(this, 3000, wxDefaultPosition, wxDefaultSize, |
|---|
| 102 | wxVSCROLL | wxSUNKEN_BORDER); |
|---|
| 103 | wxPanel* page_panel = new wxPanel(scrolled_win, 2000); |
|---|
| 104 | |
|---|
| 105 | // Get the background colour and calculate a "darker" version of it. |
|---|
| 106 | wxColour col = page_panel->GetBackgroundColour(); |
|---|
| 107 | |
|---|
| 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); |
|---|
| 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); |
|---|
| 131 | page_panel_sizer->Add(8, PAGE_ICON_HEAD_FOOT); // add a spacer |
|---|
| 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); |
|---|
| 136 | panel->SetSize(PAGE_PANEL_WIDTH - 8, PAGE_ICON_HEIGHT - PAGE_ICON_HEAD_FOOT); |
|---|
| 137 | |
|---|
| 138 | page_panel_sizer->Add(panel, 0 /* not vertically stretchable */, wxALIGN_CENTER); |
|---|
| 139 | panel->SetBackgroundColour(col); |
|---|
| 140 | |
|---|
| 141 | page_num++; |
|---|
| 142 | } |
|---|
| 143 | page_panel_sizer->Add(8, PAGE_ICON_HEAD_FOOT); // add a spacer |
|---|
| 144 | page_panel->SetAutoLayout(true); |
|---|
| 145 | page_panel->SetSizer(page_panel_sizer); |
|---|
| 146 | int height = PAGE_ICON_HEAD_FOOT + (page_num * PAGE_ICON_HEIGHT); |
|---|
| 147 | page_panel->SetSizeHints(PAGE_PANEL_WIDTH, height); |
|---|
| 148 | page_panel->SetSize(PAGE_PANEL_WIDTH, height); |
|---|
| 149 | scrolled_win->SetScrollbars(0, height / page_num, 0, page_num); |
|---|
| 150 | scrolled_win->SetSize(EDGE_MARGIN, EDGE_MARGIN, SCROLLED_WIN_WIDTH, DIALOG_HEIGHT - EDGE_MARGIN_V*2); |
|---|
| 151 | |
|---|
| 152 | // Darken the background colour for the page selector panel. |
|---|
| 153 | page_panel->SetBackgroundColour(col); |
|---|
| 154 | |
|---|
| 155 | // Create the OK/Cancel button panel. |
|---|
| 156 | wxSize button_size = wxButton::GetDefaultSize(); |
|---|
| 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(); |
|---|
| 161 | |
|---|
| 162 | // Retrieve the panel for the first page and put it in the correct place. |
|---|
| 163 | PanelDlgPage* first_page = *(pages.begin()); |
|---|
| 164 | assert(first_page); |
|---|
| 165 | m_CurrentPage = first_page; |
|---|
| 166 | PositionPage(); |
|---|
| 167 | m_CurrentPage->Show(); |
|---|
| 168 | |
|---|
| 169 | // Set a reasonable size and centre the dialog with respect to the parent window. |
|---|
| 170 | SetSize(DIALOG_WIDTH, DIALOG_HEIGHT); |
|---|
| 171 | CentreOnParent(); |
|---|
| 172 | } |
|---|
| 173 | |
|---|