3 /// Class for turning a set of PNG images into buttons
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"
26 //////////////////////////////////////////////////////////////////////////////
29 PNGButton::PNGButton(wxWindow
*parent
, int ID
, int x
, int y
, bool enabled
)
38 m_bitmaps
[BUTTON_STATE_NORMAL
] = LoadButtonBitmap(BUTTON_STATE_NORMAL
);
41 m_bitmaps
[BUTTON_STATE_FOCUS
] = LoadButtonBitmap(BUTTON_STATE_FOCUS
);
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
);
55 if( !image
.IsOk() || !bmp
.IsOk() ) {
57 throw std::runtime_error(_C("Cannot load button bitmap."));
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
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
86 int pointsize
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)
88 wxFont
font(pointsize
+ 2, wxFONTFAMILY_SWISS
, wxFONTSTYLE_NORMAL
,
91 // for each button, draw it with background + transparency + label
92 for( int i
= 0; i
< 3; i
++ ) {
93 wxBitmap final
= wxBitmap(width
, height
);
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);
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
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
)
134 m_state
= BUTTON_STATE_NORMAL
;
138 void PNGButton::Focus(wxDC
&dc
)
143 m_state
= BUTTON_STATE_FOCUS
;
147 void PNGButton::Push(wxDC
&dc
)
152 m_state
= BUTTON_STATE_PUSHED
;
156 void PNGButton::Click(wxDC
&dc
)
163 m_state
= BUTTON_STATE_NORMAL
;
167 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_id
);
168 m_parent
->GetEventHandler()->ProcessEvent(event
);