jborder.cpp 6.6 KB
/***************************************************************************
 *   Copyright (C) 2005 by Jeff Ferr                                       *
 *   root@sat                                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "Stdafx.h"
#include "jborder.h"
#include "jcomponent.h"

namespace jgui {

Border::Border():
	jcommon::Object()
{
	jcommon::Object::SetClassName("jgui::Border");
		
	_is_visible = true;
	_border_size = 2;
}

Border::~Border()
{
}

void Border::SetSize(int size)
{
	if (size < 0) {
		size = 0;
	}

	_border_size = size;
}

int Border::GetSize()
{
	return _border_size;
}

void Border::SetVisible(bool b)
{
	if (_is_visible == b) {
		return;
	}

	_is_visible = b;
}

bool Border::IsVisible()
{
	return _is_visible;
}

void Border::SetBorderType(jcomponent_border_t t)
{
	_border_type = t;
}

jcomponent_border_t Border::GetBorderType()
{
	return _border_type;
}

void Border::SetBorderColor(int red, int green, int blue, int alpha)
{
	_border_color = Color(red, green, blue, alpha);
}

void Border::SetBorderFocusColor(int red, int green, int blue, int alpha)
{
	_focus_border_color = Color(red, green , blue, alpha);
}

void Border::SetBorderColor(const Color &color)
{
	_border_color = color;
}

void Border::SetBorderFocusColor(const Color &color)
{
	_focus_border_color = color;
}

Color & Border::GetBorderColor()
{
	return _border_color;
}

Color & Border::GetBorderFocusColor()
{
	return _focus_border_color;
}

void Border::PaintBorderBackground(Component *c, Graphics *g)
{
	int x = c->GetX(),
			y = c->GetY(),
			width = c->GetWidth(),
			height = c->GetHeight();

	if (_border_type == FLAT_BORDER) {
		g->FillRectangle(x, y, width, height);
	} else if (_border_type == LINE_BORDER) {
		g->FillRectangle(x, y, width, height);
	} else if (_border_type == GRADIENT_BORDER) {
		g->FillRectangle(x, y, width, height);
	} else if (_border_type == ROUND_BORDER) {
		g->FillRoundRectangle(x, y, width-1, height-1);
	} else if (_border_type == BEVEL_BORDER) {
		g->FillBevelRectangle(x, y, width, height);
	} else if (_border_type == DOWN_BEVEL_BORDER) {
		g->FillBevelRectangle(x, y, width, height);
	} else if (_border_type == ETCHED_BORDER) {
		g->FillRectangle(x, y, width, height);
	} else {
		g->FillRectangle(x, y, width, height);
	}
}

void Border::PaintBorder(Component *c, Graphics *g)
{
	if (c == NULL || g == NULL) {
		return;
	}

	if (_is_visible == false) {
		return;
	}

	int xp = 0, 
			yp = 0,
			wp = c->GetWidth()-1,
			hp = c->GetHeight()-1,
			size = _border_size;
	int dr = _border_color.GetRed(),
			dg = _border_color.GetGreen(),
			db = _border_color.GetBlue(),
			da = _border_color.GetAlpha();
	int step = 0x20;

	g->SetLineWidth(1);
	
	if (c->HasFocus() == true) {
		dr = _focus_border_color.GetRed();
		dg = _focus_border_color.GetGreen();
		db = _focus_border_color.GetBlue();
		da = _focus_border_color.GetAlpha();
	}
	
	if (_border_type == FLAT_BORDER) {
		g->SetColor(dr, dg, db, da);
		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->DrawRectangle(xp+i, yp+i, wp-2*i, hp-2*i);
		}
	} else if (_border_type == LINE_BORDER) {
		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->SetColor(dr+step, dg+step, db+step);
			g->DrawLine(xp+i, yp+i, xp+wp-i, yp+i); //cima
			g->SetColor(dr-step, dg-step, db-step);
			g->DrawLine(xp+i, yp+hp-i, xp+wp-i, yp+hp-i); //baixo
		}

		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->SetColor(dr+step, dg+step, db+step);
			g->DrawLine(xp+i, yp+i, xp+i, yp+hp-i); //esquerda
			g->SetColor(dr-step, dg-step, db-step);
			g->DrawLine(xp+wp-i, yp+i, xp+wp-i, yp+hp-i); //direita
		}
	} else if (_border_type == GRADIENT_BORDER) {
		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->SetColor(dr+step*(size-i), dg+step*(size-i), db+step*(size-i));
			g->DrawLine(xp+i, yp+i, xp+wp-i, yp+i); //cima
			g->SetColor(dr-step*(size-i), dg-step*(size-i), db-step*(size-i));
			g->DrawLine(xp+i, yp+hp-i, xp+wp-i, yp+hp-i); //baixo
		}

		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->SetColor(dr+step*(size-i), dg+step*(size-i), db+step*(size-i));
			g->DrawLine(xp+i, yp+i, xp+i, yp+hp-i); //esquerda
			g->SetColor(dr-step*(size-i), dg-step*(size-i), db-step*(size-i));
			g->DrawLine(xp+wp-i, yp+i, xp+wp-i, yp+hp-i); //direita
		}
	} else if (_border_type == ROUND_BORDER) {
		g->SetColor(dr, dg, db, da);
		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->DrawRoundRectangle(xp+i, yp+i, wp-2*i, hp-2*i-1);
		}
	} else if (_border_type == BEVEL_BORDER) {
		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->SetColor(dr, dg, db, da);
			g->DrawBevelRectangle(i, i, c->GetWidth()-2*i, c->GetHeight()-2*i-1);
			g->SetColor(0x00, 0x00, 0x00, 0xff);
			g->DrawBevelRectangle(i+2, i+2, c->GetWidth()-2*(i+2), c->GetHeight()-2*(i+2)-1);
		}
	} else if (_border_type == DOWN_BEVEL_BORDER) {
		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->SetColor(0x00, 0x00, 0x00, 0xff);
			g->DrawBevelRectangle(i, i, c->GetWidth()-2*i, c->GetHeight()-2*i-1);
			g->SetColor(dr, dg, db, da);
			g->DrawBevelRectangle(i+2, i+2, c->GetWidth()-2*(i+2), c->GetHeight()-2*(i+2)-1);
		}
	} else if (_border_type == ETCHED_BORDER) {
		for (int i=0; i<size && i<wp && i<hp; i++) {
			g->SetColor(dr+step, dg+step, db+step, da);
			g->DrawRectangle(xp+i, yp+i, wp-2*i, hp-2*i);
			g->SetColor(0x00, 0x00, 0x00, 0xff);
			g->DrawRectangle(xp+(i+2), yp+(i+2), wp-2*(i+2), hp-2*(i+2));
		}
	}

	if (c->IsEnabled() == false) {
		g->SetColor(0x00, 0x00, 0x00, 0x80);
		g->FillRectangle(0, 0, c->GetWidth(), c->GetHeight());
	}
}

}