File: mainframe.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 (132 lines) | stat: -rw-r--r-- 3,057 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
/*
	Simple GUI & Network demo application,
	Copyright (c) 2000 by Magnus Norddahl and Kenneth Gangstoe.
*/

#include "mainframe.h"
#include "view.h"
#include "canvas_view.h"

#include <ClanLib/display.h>

// Construction:
MainFrame::MainFrame(CL_ResourceManager *resources, CL_Component *parent, CL_StyleManager *styles)
:
	CL_Frame(CL_Rect(0,0,CL_Display::get_width(),CL_Display::get_height()), parent, styles),
	resources(resources), styles(styles), active_view(NULL), document(NULL)
{
	button_new_canvas = new CL_Button(CL_Rect(0, 20, 100, 40), "New canvas", this);

	slots.connect(button_new_canvas->sig_clicked(), this, &MainFrame::on_button_new_canvas);
	slots.connect(CL_Display::sig_resized(), this, &MainFrame::on_resize);
	slots.connect(sig_paint(), this, &MainFrame::on_paint);

	set_position(CL_Rect(0, 0, CL_Display::get_width(), CL_Display::get_height()));
}

MainFrame::~MainFrame()
{
}

View *MainFrame::get_active_view()
{
	return active_view;
}

CL_ResourceManager *MainFrame::get_resources()
{
	return resources;
}

CL_StyleManager *MainFrame::get_styles()
{
	return styles;
}

#define BUTTON_WIDTH 102
#define BUTTON_HEIGHT 20

void MainFrame::add_view(View *view)
{
	views.push_back(view);
	position_window(view);

	// Calculate position and create new button
	int count = button_views.size();
	int xpos = count * BUTTON_WIDTH;
	int ypos = 0;
	CL_Button *button = new CL_Button(
		CL_Rect(xpos, ypos, xpos + BUTTON_WIDTH - 2, ypos + BUTTON_HEIGHT),
		view->get_title(),
		this);
	button_views.push_back(button);

	CL_Slot slot1 = view->sig_new_title.connect(this, &MainFrame::on_view_new_title, button);
	slots_view_newtitle.push_back(slot1);

	CL_Slot slot2 = button->sig_clicked().connect(this, &MainFrame::on_view_button_clicked, view);
	slots_view_click.push_back(slot2);
}

void MainFrame::remove_view(View *view)
{
	views.remove(view);

	// TODO: Remove slots from slots_view_newtitle and slots_view_click
}

void MainFrame::set_active_view(View *view)
{
	if (active_view != NULL) active_view->show(false);
	active_view = view;
	if (active_view != NULL) active_view->show(true);
}

void MainFrame::position_window(View *view)
{
	// Only position maximized windows.
	if (view->is_floating()) return;

	CL_Rect pos;
	pos.x1 = 0;
	pos.y1 = 20 * 2;
	pos.x2 = get_width();
	pos.y2 = get_height();
	view->set_position(pos);
}

void MainFrame::on_resize(int width, int height)
{
	set_position(width, height);

	// Resize all maximized views.
	for (
		std::list<View *>::iterator it = views.begin();
		it != views.end();
		it++)
	{
		position_window(*it);
	}
}

void MainFrame::on_paint()
{
	CL_Display::fill_rect(0, 0, get_width(), 20, 0.7f, 0.7f, 1.0f);
	CL_Display::fill_rect(0, 20, get_width(), 40, 0.5f, 0.5f, 0.8f);
}

void MainFrame::on_view_new_title(const std::string &title, CL_Button *button)
{
	button->set_text(title);
}

void MainFrame::on_view_button_clicked(View *view)
{
	set_active_view(view);
}

void MainFrame::on_button_new_canvas()
{
	CanvasView *canvas_view = new CanvasView(this);
	set_active_view(canvas_view);
}