Source: src/cl_widget.cc


Annotated List
Files
Globals
Hierarchy
Index
//widget - menu and windows

#include "cl_widget.h"

void WIDGET::init()
{
	border = NULL;
	prop_int.clear();
	title_str.clear();
	content_str.clear();
	visible_val = 0;

	prop_int[WIDGET_TEXT_CENTER]	= 0;
	prop_int[WIDGET_LINE_SPACE]	= 2;
}

void WIDGET::canvas(BITMAP *bmp)
{
	scr = bmp;
}

void WIDGET::borders(ANIM *a)
{
	border = a;
}


void WIDGET::content(string s)
{
	int p;
	string tmp,a;
	content_str = s;
	tmp = s;
	lines.clear();

	while((p = tmp.find(TAG_NEW_LINE)) > -1) {
		a = tmp.substr(0,p);
		lines.push_back(a);
#ifdef HARD_DEBUG
		message("Line: [%s]", a.c_str());
#endif
		tmp = tmp.substr(p+4, tmp.length());
	}
	compute_size();
}

void WIDGET::compute_size()
{
	int fh;		//font height
	int lc;		//line count
	int tl, max_tl;		//text lenght
	deque::iterator i;

	max_tl = 0;
	
	fh = text_height(font) + prop_int[WIDGET_LINE_SPACE];
	lc = 1;

	for(i=lines.begin(); i!=lines.end(); i++) {
		tl = text_length(font, i->c_str());
		if(max_tl < tl) {
			max_tl = tl;
		}
		lc++;
	}

	tl = text_length(font, title_str.c_str());
	if(max_tl < tl) {
		max_tl = tl;
	}

	text_h = fh * lc;
	text_w = max_tl;
}

void WIDGET::print_content(int x, int y)
{
	deque::iterator i;
	for(i=lines.begin(); i!=lines.end(); i++) {
		if(prop_int[WIDGET_TEXT_CENTER]) {
			textprintf_centre(scr, font, x + text_w/2, y, COL_WHITE, "%s", i->c_str());
		} else {
			textprintf(scr, font, x, y, COL_WHITE, "%s", i->c_str());
		}
		y += text_height(font) + prop_int[WIDGET_LINE_SPACE];
	}
}

void WIDGET::draw(int x, int y, int sx, int sy)
{
	int tl, th;
	int px, py;	//position of titile
	int mx, my;	//difference of text position
	int drs_x, drs_y, drs_sx, drs_sy;
	mx = my = 0;

	if(sx < 8) { sx = 8; }
	if(sy < 8) { sy = 8; }
	
	drs_x = x;
	drs_y = y;
	drs_sx = sx + 1;
	drs_sy = sy + 1;
	if(border) {
		draw_sprite(scr, border->get(MENU_SECTOR_0), x, y);
		draw_sprite(scr, border->get(MENU_SECTOR_8), x + sx, y + sy);
	} else {	
		if(!title_str.empty()) {	//title of window
			sy += 8;
			rectfill(scr, x, y, x+sx, y+sy, COL_BLACK);
			rect(scr, x, y, x+sx, y+sy, COL_WHITE);

			tl = text_length(font, title_str.c_str()) +4;
			th = text_height(font) + 4;
		
			px = x + sx/2 - tl/2;
			py = y - th/2;
			
			rectfill(scr, px - 2, py - 2 , px + tl + 2, py + th + 2, COL_BLACK);
			rect(scr, px - 2, py - 2 , px + tl + 2, py + th + 2, COL_WHITE);
			textprintf(scr, font, px + 2, py + 2, COL_WHITE, "%s", title_str.c_str());

			drs_y = py - 4;
			drs_sy += th + 10;
			my = th/2 + 2;
		} else {
			rectfill(scr, x, y, x+sx, y+sy, COL_BLACK);
			rect(scr, x, y, x+sx, y+sy, COL_WHITE);
		}
		print_content(x+mx+5, y+my+5);
	}

	DRS_add_rectangle(drs_x, drs_y, drs_sx, drs_sy);
}

void WIDGET::draw()
{
	draw(SCREEN_SIZE_X / 2 - text_w/2, SCREEN_SIZE_Y / 2 - text_h/2, text_w + 8, text_h + 8);
}

void WIDGET::draw_visible()
{
	if(visible_val) {
		draw();
	}
}

void WIDGET::title(string t)
{
	title_str = t;
}

void WIDGET::show()
{
	visible_val = 1;
}

void WIDGET::hide()
{
	visible_val = 0;
}

int WIDGET::visible()
{
	return visible_val;
}

void WIDGET::property(string name, int val)
{
#ifdef HARD_DEBUG
	message("Widget property [%s] = [%d]", name.c_str(), val);
#endif
	prop_int[name] = val;
}


Generated by: georgik on armada on Sat Jul 24 07:07:15 2004, using kdoc 2.0a54.