File: ToolButton.cpp

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (126 lines) | stat: -rw-r--r-- 4,172 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Copyright (C) 2011 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "wx/sysopt.h"
#include "wx/wfstream.h"
#include "wx/filename.h"
#include "wx/image.h"

#include "ToolButton.h"
#include "ScenarioEditor/Tools/Common/Tools.h"
#include "ScenarioEditor/SectionLayout.h"
#include "General/Datafile.h"

BEGIN_EVENT_TABLE(ToolButton, wxButton)
	EVT_BUTTON(wxID_ANY, ToolButton::OnClick)
END_EVENT_TABLE()

ToolButton::ToolButton
	(ToolManager& toolManager, wxWindow *parent, const wxString& label, const wxString& toolName, const wxSize& size, long style)
	: wxButton(parent, wxID_ANY, label, wxDefaultPosition, size, style)
	, m_ToolManager(toolManager)
	, m_Tool(toolName)
{
	// Explicitly set appearance, so that the button is always owner-drawn
	// (by the wxButton code), rather than initially using the native
	// (fixed color) button appearance.
	SetSelectedAppearance(false);

	RegisterToolButton(this, toolName);
}

void ToolButton::OnClick(wxCommandEvent& WXUNUSED(evt))
{
	// Toggle on/off
	if (m_Selected)
		m_ToolManager.SetCurrentTool(_T(""));
	else
		m_ToolManager.SetCurrentTool(m_Tool);
}

void ToolButton::SetSelectedAppearance(bool selected)
{
	m_Selected = selected;
	if (selected)
		SetBackgroundColour(wxColor(0xee, 0xcc, 0x55));
	else
		SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
}

//////////////////////////////////////////////////////////////////////////

BEGIN_EVENT_TABLE(ToolButtonBar, wxToolBar)
	EVT_TOOL(wxID_ANY, ToolButtonBar::OnTool)
END_EVENT_TABLE()

ToolButtonBar::ToolButtonBar(ToolManager& toolManager, wxWindow* parent, SectionLayout* sectionLayout, int baseID, long style)
: wxToolBar(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style)
, m_ToolManager(toolManager), m_SectionLayout(sectionLayout), m_Id(baseID), m_Size(-1)
{
	/* "msw.remap: If 1 (the default), wxToolBar bitmap colors will be remapped
	   to the current theme's values. Set this to 0 to disable this functionality,
	   for example if you're using more than 16 colors in your tool bitmaps." */
	wxSystemOptions::SetOption(wxT("msw.remap"), 0); // (has global effect)
}

void ToolButtonBar::AddToolButton(const wxString& shortLabel, const wxString& longLabel,
								  const wxString& iconPNGFilename, const wxString& toolName,
								  const wxString& sectionPage)
{
	wxFileName iconPath (_T("tools/atlas/toolbar/"));
	iconPath.MakeAbsolute(Datafile::GetDataDirectory());
	iconPath.SetFullName(iconPNGFilename);
	wxFFileInputStream fstr (iconPath.GetFullPath());
	if (! fstr.Ok())
	{
		wxLogError(_("Failed to open toolbar icon file '%s'"), iconPath.GetFullPath().c_str());
		return;
	}
	wxImage img (fstr, wxBITMAP_TYPE_PNG);
	if (! img.Ok())
	{
		wxLogError(_("Failed to load toolbar icon image '%s'"), iconPath.GetFullPath().c_str());
		return;
	}

	if (m_Size == -1)
	{
		m_Size = img.GetWidth();
		SetToolBitmapSize(wxSize(m_Size, m_Size));
	}

	if (img.GetWidth() != m_Size || img.GetHeight() != m_Size)
		img = img.Scale(m_Size, m_Size);

	AddCheckTool(m_Id, shortLabel, wxBitmap(img), wxNullBitmap, longLabel);
	m_Buttons[m_Id] = Button(toolName, sectionPage);

	RegisterToolBarButton(this, m_Id, toolName);

	++m_Id;
}

void ToolButtonBar::OnTool(wxCommandEvent& evt)
{
	std::map<int, Button>::iterator it = m_Buttons.find(evt.GetId());
	wxCHECK_RET(it != m_Buttons.end(), _T("Invalid toolbar button"));
	m_ToolManager.SetCurrentTool(it->second.name);
	if (! it->second.sectionPage.IsEmpty())
		m_SectionLayout->SelectPage(it->second.sectionPage);
}