File: button_default.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (156 lines) | stat: -rw-r--r-- 4,377 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
	$Id: button_default.cpp,v 1.50 2002/01/17 14:38:44 sphair Exp $
	
	ClanGUI, copyrights by various people. Have a look in the CREDITS file.
	
	This sourcecode is distributed using the Library GNU Public Licence,
	version 2 or (at your option) any later version. Please read LICENSE
	for details.
*/

#include "precomp.h"
#include <cstdio>
#include "API/Display/Display/surface.h"
#include "API/Display/Font/font.h"
#include "API/Core/Resources/resource_manager.h"
#include "API/GUI/stylemanager_default.h"
#include "button_default.h"

CL_Button_Default::CL_Button_Default(
	CL_Button *_button,
	CL_StyleManager_Default *_style)
:
	CL_ComponentStyle(_button),
	button(_button),
	style(_style)
{
	resources = style->get_resources();

	font = CL_Font::load("Button/font", resources);
	font_disabled = CL_Font::load("Button/font_disabled", resources);

	slot_set_options = button->sig_set_options().connect(
		this, &CL_Button_Default::on_set_options);
	slot_paint = button->sig_paint().connect(
		this, &CL_Button_Default::on_paint);
	slot_get_preferred_size = button->sig_get_preferred_size().connect(
		this, &CL_Button_Default::on_get_preferred_size);
}

CL_Button_Default::~CL_Button_Default()
{
	delete font;
	delete font_disabled;
}

void CL_Button_Default::on_set_options(const CL_ComponentOptions &options)
{
	if(options.exists("surface_up"))
		button->set_surface_up(CL_Surface::load(options.get_value("surface_up").c_str(), resources));

	if(options.exists("surface_down"))
		button->set_surface_down(CL_Surface::load(options.get_value("surface_down").c_str(), resources));

	if(options.exists("surface_disabled"))
		button->set_surface_disabled(CL_Surface::load(options.get_value("surface_disabled").c_str(), resources));

	if(options.exists("surface_highlighted"))
		button->set_surface_highlighted(CL_Surface::load(options.get_value("surface_highlighted").c_str(), resources));
}

void CL_Button_Default::on_get_preferred_size(CL_Point &size)
{
	CL_Surface *s = button->get_surface_up();
	if(s)
	{
		size.x = s->get_width();
		size.y = s->get_height();
	}
	else
	{
		size.x = font->get_text_width(button->get_text()) + 8;
		size.y = font->get_height() + 4;
	}
}

void CL_Button_Default::on_paint()
{
	int text_width = font->get_text_width(button->get_text().c_str());
	int button_width = button->get_width();
	int text_height = font->get_height();
	int button_height = button->get_height();

	int x_pos = (button_width - text_width) / 2;
	int y_pos = (button_height - text_height) / 2;

	int width = button->get_width();
	int height = button->get_height();

	if(button->is_enabled() == false)
	{
		CL_Surface *surface = button->get_surface_disabled();
		if(surface)
			surface->put_screen(0, 0);
		else
		{
			// Fill
			style->fill_rect(2, 2, width - 2, height - 2, GUICOLOR_BUTTON_DISABLED);

			// Outline
			style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE_DISABLED);

			// Shade box
			style->draw_box(1, 1, width - 1 , height - 1, GUICOLOR_BRIGHT_SHADE_DISABLED, GUICOLOR_DARK_SHADE_DISABLED);
		}

		font_disabled->print_left(x_pos, y_pos, button->get_text().c_str());
	}
	else 
	{
		if(button->is_down())
		{
			CL_Surface *surface = button->get_surface_down();
			if(surface)
				surface->put_screen(0, 0);
			else
			{
				// Fill
				style->fill_rect(2, 2, width - 2, height - 2, GUICOLOR_BUTTON_TOGGLED);

				// Outline
				style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE);

				// Shade box
				style->draw_box(1, 1, width - 1 , height - 1, GUICOLOR_DARK_SHADE, GUICOLOR_BRIGHT_SHADE);
			}

			font->print_left(x_pos + 1, y_pos + 1, button->get_text().c_str());
		}
		else
		{
			CL_Surface *surface = button->get_surface_up();
			CL_Surface *surface_highlighted = button->get_surface_highlighted();
			if(button->has_mouse_over() && surface_highlighted)
				surface_highlighted->put_screen(0, 0);
			else if(surface)
				surface->put_screen(0, 0);
			else
			{
				// Fill
				style->fill_rect(2, 2, width - 2, height - 2, GUICOLOR_BUTTON);

				// Outline
				style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE);

				// Shade box
				style->draw_box(1, 1, width - 1 , height - 1, GUICOLOR_BRIGHT_SHADE, GUICOLOR_DARK_SHADE);

				// Focus
				if(button->has_focus())
					style->draw_rect(2, 2, width - 2, height - 2, GUICOLOR_FOCUS);
			}

			font->print_left(x_pos, y_pos, button->get_text().c_str());
		}
	}
}