File: listbox_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 (140 lines) | stat: -rw-r--r-- 3,301 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
/*
	$Id: listbox_default.cpp,v 1.45 2002/01/16 19:16:51 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 "listbox_default.h"
#include "API/Display/Font/font.h"
#include "API/GUI/scrollbar.h"
#include "API/GUI/listbox_item.h"
#include "API/Display/Display/display.h"

CL_ListBox_Default::CL_ListBox_Default(
	CL_ListBox *_listbox,
	CL_StyleManager_Default *style)
: CL_ComponentStyle(_listbox), listbox(_listbox)
{
	this->style = style;

	resources = style->get_resources();
	font = CL_Font::load("ListBox/font", resources);

	CL_Component *client_area = listbox->get_client_area();

//	int height = client_area->get_height();
//	int max = height / font->get_height();

	int font_height = font->get_height();
	listbox->set_item_height(font_height);
//	listbox->set_max_visible_items(max);

/*	int height;
	if (options.exists("height"))
	{
		height = options.get_value_as_int("height");
		int max = height / font->get_height();
		listbox->set_max_visible_items(max);
	}
	else
	{
		height = listbox->get_max_visible_items() * font->get_height();
	}
*/
	slot_paint_background = listbox->sig_paint().connect(
		this, &CL_ListBox_Default::on_paint_background);
	slot_paint_listbox = client_area->sig_paint().connect(
		this, &CL_ListBox_Default::on_paint_listbox);
}

CL_ListBox_Default::~CL_ListBox_Default()
{
	delete font;
}

void CL_ListBox_Default::on_paint_background()
{
	int width = listbox->get_width();
	int height = listbox->get_height();

	bool focus = listbox->has_child(listbox->get_focus());

	if(listbox->is_enabled() == false || focus == false)
	{
		// Fill
		style->fill_rect(1, 1, width - 1, height - 1, GUICOLOR_WHITE);

		// Outline
		style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE);
	}
	else
	{
		// Fill
		style->fill_rect(2, 2, width - 2, height - 2, GUICOLOR_WHITE);

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

		// Shade box
		style->draw_box(1, 1, width - 1, height - 1, GUICOLOR_DARKER_SHADE, GUICOLOR_MEDIUM_SHADE);
	}
}

void CL_ListBox_Default::on_paint_listbox()
{
	int width = listbox->get_client_area()->get_width();
//	int height = listbox->get_client_area()->get_height();

	int pos = 0;

	bool focus = listbox->has_child(listbox->get_focus());

	std::vector<CL_ListBox_Item> items = listbox->get_items();
	std::vector<CL_ListBox_Item>::iterator it;
	int offset = listbox->get_top_item();
	for (it = items.begin(); it != items.end(); it++)
	{
		if(pos < listbox->get_top_item())
		{
			pos++;
			continue;
		}

		if((*it).selected)
//		if(listbox->is_selected(pos))
		{
//			int text_width = font->get_text_width((*it).c_str());
		
			if(focus)
			{
				style->fill_rect(
					0,
					(pos - offset) * font->get_height(),
					width,
					(pos + 1 - offset) * font->get_height(),
					GUICOLOR_SELECTION);
			}
			else
			{
				style->draw_rect(
					0,
					(pos - offset) * font->get_height(),
					width,
					(pos + 1 - offset) * font->get_height(),
					GUICOLOR_SELECTION);
			}
		}

		font->print_left(
			0,
			(pos - offset) * font->get_height(),
			(*it).str.c_str());

		pos++;
	}
}