File: listbox_opengl.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 (82 lines) | stat: -rw-r--r-- 1,879 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
#include <ClanLib/display.h>
#include <ClanLib/gl.h>

#include "listbox_opengl.h"
#include "stylemanager_opengl.h"

CL_ListBox_OpenGL::CL_ListBox_OpenGL(
	CL_ListBox *_listbox,
	CL_StyleManager_OpenGL *style)
:
	CL_ComponentStyle(_listbox),
	listbox(_listbox),
	style(style),
	resources(style->get_resources())
{
	font = CL_Font::load("ListBox/font", resources);

	CL_Component *client_area = listbox->get_client_area();

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

	slot_paint_background = listbox->sig_paint().connect(
		this, &CL_ListBox_OpenGL::on_paint_background);
	slot_paint_listbox = client_area->sig_paint().connect(
		this, &CL_ListBox_OpenGL::on_paint_listbox);
}

CL_ListBox_OpenGL::~CL_ListBox_OpenGL()
{
//	resources->get_resource("Listbox/font")->unload();
}

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

	// Outline
	CL_Display::draw_rect(0, 0, width, height, 0.5f, 0.5f, 0.5f);

	// Fill
	CL_Display::fill_rect(1, 1, width - 1, height - 1, 0.0f, 0.0f, 0.0f);
}

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

	int pos = 0;
	int offset = listbox->get_top_item();

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

		if (pos == listbox->get_current_item())
		{
			CL_Display::fill_rect(
				0, (pos - offset) * font->get_height(),
				width, (pos + 1 - offset) * font->get_height(),
				0.1f, 0.1f, 0.1f);
		}

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

		pos++;

		if((pos - offset + 1) * font->get_height() >= height)
			break;
	}
}