tzwrapper.cc: fixed use of iterator after erase
[barry.git] / desktop / src / PNGButton.cc
blob163d13df56b655a201f80046ef69ff3a046ba443
1 ///
2 /// \file PNGButton.cc
3 /// Class for turning a set of PNG images into buttons
4 ///
6 /*
7 Copyright (C) 2009-2013, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "PNGButton.h"
23 #include "barrydesktop.h"
24 #include "wxi18n.h"
26 //////////////////////////////////////////////////////////////////////////////
27 // PNGButton
29 PNGButton::PNGButton(wxWindow *parent, int ID, int x, int y, bool enabled)
30 : m_parent(parent)
31 , m_id(ID)
32 , m_x(x)
33 , m_y(y)
34 , m_state(0)
35 , m_enabled(enabled)
37 // normal[0]
38 m_bitmaps[BUTTON_STATE_NORMAL] = LoadButtonBitmap(BUTTON_STATE_NORMAL);
40 // focus[1]
41 m_bitmaps[BUTTON_STATE_FOCUS] = LoadButtonBitmap(BUTTON_STATE_FOCUS);
43 // pushed[2]
44 m_bitmaps[BUTTON_STATE_PUSHED] = LoadButtonBitmap(BUTTON_STATE_PUSHED);
46 // and the label text, translated
47 m_label = GetButtonLabel(m_id);
50 wxBitmap PNGButton::LoadButtonBitmap(int state)
52 wxString file = GetButtonFilename(m_id, state);
53 wxImage image(file);
54 wxBitmap bmp(image);
55 if( !image.IsOk() || !bmp.IsOk() ) {
56 wxGetApp().Yield();
57 throw std::runtime_error(_C("Cannot load button bitmap."));
59 return bmp;
63 // Due to limitations in DC transparency support, and for speed reasons,
64 // we use this Init() opportunity to create a brand new bitmap for each
65 // button, which includes the background and the transparency bitmap drawing,
66 // and the button text.
68 void PNGButton::Init(wxDC &dc)
70 // grab the existing background first
71 int width = m_bitmaps[BUTTON_STATE_NORMAL].GetWidth();
72 int height = m_bitmaps[BUTTON_STATE_NORMAL].GetHeight();
74 m_background = wxBitmap(width, height);
76 // copy it over, no transparency
78 wxMemoryDC grab_dc;
79 grab_dc.SelectObject(m_background);
80 grab_dc.Blit(0, 0, width, height, &dc, m_x, m_y, wxCOPY, false);
83 // this font may be modified by DrawButtonLabelDC... keep it
84 // outside the loop so the same size font will be used for all
85 // buttons
86 int pointsize = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
87 .GetPointSize();
88 wxFont font(pointsize + 2, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL,
89 wxFONTWEIGHT_BOLD);
91 // for each button, draw it with background + transparency + label
92 for( int i = 0; i < 3; i++ ) {
93 wxBitmap final = wxBitmap(width, height);
96 wxMemoryDC dc;
97 dc.SelectObject(final);
99 // draw the background, no transparency
100 dc.DrawBitmap(m_background, 0, 0, false);
102 // draw the button, with transparency
103 dc.DrawBitmap(m_bitmaps[i], 0, 0);
105 // draw the text
106 DrawButtonLabelDC(dc, m_bitmaps[i],
107 m_label, font, *wxBLACK, 80, 12, -15, -15);
110 // copy final button bitmap into m_bitmaps array for use
111 m_bitmaps[i] = final;
115 void PNGButton::Draw(wxDC &dc)
117 // just splat the final button onto the screen at the exepcted
118 // coordinates
119 dc.DrawBitmap(m_bitmaps[m_state], m_x, m_y, false);
122 /// This is only used if the button is moved, and the background needs to
123 /// be restored. Currently only theoretical.
124 void PNGButton::Erase(wxDC &dc)
126 dc.DrawBitmap(m_background, m_x, m_y, false);
129 void PNGButton::Normal(wxDC &dc)
131 if( !m_enabled )
132 return;
134 m_state = BUTTON_STATE_NORMAL;
135 Draw(dc);
138 void PNGButton::Focus(wxDC &dc)
140 if( !m_enabled )
141 return;
143 m_state = BUTTON_STATE_FOCUS;
144 Draw(dc);
147 void PNGButton::Push(wxDC &dc)
149 if( !m_enabled )
150 return;
152 m_state = BUTTON_STATE_PUSHED;
153 Draw(dc);
156 void PNGButton::Click(wxDC &dc)
158 if( !m_enabled )
159 return;
161 if( IsPushed() ) {
162 // return to normal
163 m_state = BUTTON_STATE_NORMAL;
164 Draw(dc);
166 // send the event
167 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_id);
168 m_parent->GetEventHandler()->ProcessEvent(event);