Bumped copyright dates for 2013
[barry.git] / desktop / src / BaseButtons.cc
blob655efed2a4dd4a4a375596c6be6ec33ee7582d91
1 ///
2 /// \file BaseButtons.cc
3 /// Support class for BaseFrame
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 "BaseButtons.h"
23 #include "BaseFrame.h"
24 #include "PNGButton.h"
25 #include "windowids.h"
26 #include "util.h"
27 #include <wx/wx.h>
28 #include <vector>
30 using namespace std;
32 //////////////////////////////////////////////////////////////////////////////
33 // BaseButtons
35 BaseButtons::BaseButtons(wxWindow *parent)
36 : m_current(0)
38 // first, discover the size of the average button
39 wxString file = GetButtonFilename(MainMenu_BackupAndRestore,
40 BUTTON_STATE_NORMAL);
41 wxImage sizer(file);
42 m_buttonWidth = sizer.GetWidth();
43 m_buttonHeight = sizer.GetHeight();
45 for( int i = MainMenu_FirstButton; i <= MainMenu_LastButton; i++ ) {
46 int row = (i - MainMenu_FirstButton) / 3;
47 int col = (i - MainMenu_FirstButton) % 3;
48 int y_offset = MAIN_HEADER_OFFSET; // skip the header
50 PNGButton *button = new PNGButton(parent, i,
51 col * m_buttonWidth,
52 row * m_buttonHeight + y_offset,
53 IsButtonEnabled(i));
55 m_buttons.push_back(button);
59 BaseButtons::~BaseButtons()
61 vector<PNGButton*>::iterator b = m_buttons.begin();
62 for( ; b != m_buttons.end(); ++b ) {
63 delete *b;
65 m_buttons.clear();
68 PNGButton* BaseButtons::CalculateHit(int x, int y)
70 int col = x / m_buttonWidth;
71 if( x < 0 || col < 0 || col > 2 )
72 return 0;
74 int y_offset = MAIN_HEADER_OFFSET; // graphic header size
76 int row = (y - y_offset) / m_buttonHeight;
77 if( y < y_offset || row < 0 || row > 2 )
78 return 0;
80 unsigned int index = col + row * 3;
81 if( index >= m_buttons.size() )
82 return 0;
84 return m_buttons[index];
87 void BaseButtons::InitAll(wxDC &dc)
89 vector<PNGButton*>::iterator b = m_buttons.begin();
90 for( ; b != m_buttons.end(); ++b ) {
91 (*b)->Init(dc);
95 void BaseButtons::DrawAll(wxDC &dc)
97 vector<PNGButton*>::iterator b = m_buttons.begin();
98 for( ; b != m_buttons.end(); ++b ) {
99 (*b)->Draw(dc);
103 void BaseButtons::HandleMotion(wxDC &dc, int x, int y)
105 PNGButton *hit = CalculateHit(x, y);
106 if( hit != m_current ) {
107 // clean up old hit
108 if( m_current ) {
109 m_current->Normal(dc);
112 m_current = hit;
114 // draw the new state
115 if( m_current )
116 m_current->Focus(dc);
120 void BaseButtons::HandleDown(wxDC &dc, int x, int y)
122 HandleMotion(dc, x, y);
123 if( m_current ) {
124 m_current->Push(dc);
128 void BaseButtons::HandleUp(wxDC &dc, int x, int y)
130 HandleMotion(dc, x, y);
131 if( m_current && m_current->IsPushed() ) {
132 m_current->Click(dc);