Debian Codegraph @

code/controls/StatusBar.cpp

raw
1 /*
2 Copyright (C) 2009-2010 wxLauncher Team
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <wx/wx.h>
20 #include <wx/filename.h>
21 #include "apis/SkinManager.h"
22 #include "controls/StatusBar.h"
23 #include "global/ids.h"
24 #include "controls/Logger.h"
25 #include "generated/configure_launcher.h"
26
27 #include "global/MemoryDebugging.h" // Last include for memory debugging
28
29 enum FieldIDs {
30 SB_FIELD_ICON = 0,
31 SB_FIELD_MAINTEXT,
32 SB_FIELD_PROGRESS_BAR,
33 SB_FIELD_PROGRESS_TEXT,
34 SB_FIELD_MAX,
35 };
36
37 #if IS_APPLE
38 const int ICON_FIELD_WIDTH = 18;
39 #else
40 const int ICON_FIELD_WIDTH = 25;
41 #endif
42
43 BEGIN_EVENT_TABLE(StatusBar, wxStatusBar)
44 EVT_SIZE(StatusBar::OnSize)
45 EVT_COMMAND(wxID_NONE, EVT_TC_SKIN_CHANGED, StatusBar::OnTCSkinChanged)
46 END_EVENT_TABLE()
47
48 StatusBar::StatusBar(wxWindow *parent)
49 :wxStatusBar(parent) {
50 this->parent = parent;
51 this->showingToolTip = false;
52
53 SkinSystem::RegisterTCSkinChanged(this);
54
55 wxCommandEvent nullEvent;
56 OnTCSkinChanged(nullEvent);
57
58 // Just creating these now, will place them in the OnSize event handler
59 new wxStaticBitmap(this, ID_STATUSBAR_STATUS_ICON, this->icons[ID_SB_OK]);
60 #if 0 // this progress bar doesn't need to be here
61 new wxGauge(this, ID_STATUSBAR_PROGRESS_BAR, 100);
62 #endif
63 // eliminate (by zeroing the width) the fields we're currently not using
64 // FIXME and when we start using those fields, add the width back!
65 int widths[] = { ICON_FIELD_WIDTH, -1, 0, 0 };
66
67 wxASSERT_MSG( sizeof(widths)/sizeof(int) == SB_FIELD_MAX,
68 wxString::Format(
69 _T("Number of fields (%d) and number of widths (%d) do not match"),
70 SB_FIELD_MAX, sizeof(widths)/sizeof(int)));
71
72 this->SetFieldsCount(SB_FIELD_MAX, widths);
73
74 this->SetStatusText(_T("Status bar created"), SB_FIELD_MAINTEXT);
75
76 dynamic_cast<Logger*>(wxLog::GetActiveTarget())->SetStatusBarTarget(this);
77 }
78
79 StatusBar::~StatusBar() {
80 // logger could be null if Logger already destroyed, so must check
81 Logger* logger = dynamic_cast<Logger*>(wxLog::GetActiveTarget());
82 if (logger) {
83 logger->SetStatusBarTarget(NULL);
84 }
85 }
86
87 void StatusBar::OnSize(wxSizeEvent& WXUNUSED(event)) {
88 wxWindow* icon = dynamic_cast<wxWindow*>(wxWindow::FindWindowById(ID_STATUSBAR_STATUS_ICON, this));
89 wxCHECK_RET( icon != NULL, _T("Cannot find status bar icon control"));
90
91 wxRect iconrect;
92 this->GetFieldRect(SB_FIELD_ICON, iconrect);
93 // OS X-only hack to keep the icon from being clipped off
94 #if IS_APPLE
95 iconrect.SetX(-3);
96 iconrect.SetY(-2);
97 #endif
98 icon->SetSize(iconrect);
99
100 #if 0 // this progress bar doesn't need to be here
101 wxWindow* bar = dynamic_cast<wxWindow*>(wxWindow::FindWindowById(ID_STATUSBAR_PROGRESS_BAR, this));
102 wxCHECK_RET( bar != NULL, _T("Cannot find status bar progress bar"));
103
104 wxRect barrect;
105 this->GetFieldRect(SB_FIELD_PROGRESS_BAR, barrect);
106 bar->SetSize(barrect);
107 #endif
108 }
109
110 void StatusBar::OnTCSkinChanged(wxCommandEvent &WXUNUSED(event)) {
111 this->icons[ID_SB_OK] = SkinSystem::GetSkinSystem()->GetOkIcon();
112
113 this->icons[ID_SB_WARNING] = SkinSystem::GetSkinSystem()->GetWarningIcon();
114
115 this->icons[ID_SB_ERROR] = SkinSystem::GetSkinSystem()->GetErrorIcon();
116
117 this->icons[ID_SB_INFO] = SkinSystem::GetSkinSystem()->GetInfoIcon();
118
119 for( int i = 0; i < ID_SB_MAX_ID; i++) { // Check that all icons are okay.
120 if ( !this->icons[i].IsOk() ) {
121 this->icons[i] = wxNullBitmap;
122 wxLogWarning(_T("Icon ID: %d is not okay"), i);
123 }
124 }
125 }
126
127 void StatusBar::SetMainStatusText(wxString msg, int icon) {
128 this->SetStatusText(msg, SB_FIELD_MAINTEXT);
129 if ( icon > ID_SB_NO_CHANGE && icon < ID_SB_MAX_ID ) {
130 wxStaticBitmap* iconImage = dynamic_cast<wxStaticBitmap*>(wxWindow::FindWindowById(ID_STATUSBAR_STATUS_ICON, this));
131 wxCHECK_RET( iconImage != NULL, _T("Cannot find status bar icon image"));
132
133 iconImage->SetBitmap(this->icons[icon]);
134 iconImage->Refresh();
135 } else {
136 if ( icon != ID_SB_NO_CHANGE ) {
137 wxLogWarning(_T("SetMainStatusText was passed (%d) as an icon, which is out of range"), icon);
138 }
139 }
140 }
141
142 /** Causes the status bar to show the msg until EndToolTipStatusText() is
143 called. When EndToolTipStatusText() is called the status text will be returned
144 to the original text. */
145 void StatusBar::StartToolTipStatusText(wxString msg) {
146 if (!this->showingToolTip) {
147 this->PushStatusText(msg, SB_FIELD_MAINTEXT);
148 this->showingToolTip = true;
149 }
150 }
151
152 /** Returns the statusbar text to the orginal message after a mouse over.
153 See StartToolTipStatusText() for more information. */
154 void StatusBar::EndToolTipStatusText() {
155 if (this->showingToolTip) {
156 this->PopStatusText(SB_FIELD_MAINTEXT);
157 this->showingToolTip = false;
158 }
159 }