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