#include "global.hh"

// window styles

#include "style.hh"
#include "config.hh"
#include "windows.hh"

StyleList style_list;

int
ConfigParser::parse_window_styles ()
{
	while (get_line() && !match ("end"))
	{
		Style *style = new Style;

		stripcpy2 (style->pattern);
		skip_space ();
		
		while (!eol ())
		{
			// window behaviour
			if (match ("-list")) style->enable (WINLISTSKIP);
			else if (match ("+list")) style->disable (WINLISTSKIP);
			else if (match ("ontop")) style->enable (KEEPONTOP);
			else if (match ("-ontop")) style->disable (KEEPONTOP);
			else if (match ("sticky")) style->enable (STICKY);
			else if (match ("-sticky")) style->disable (STICKY);
			// decorations style
			else if (match ("decor")) style->enable (DECORATIONS);
			else if (match ("-decor")) style->disable (DECORATIONS);
			else if (match ("title")) style->enable (TITLE);
			else if (match ("-title")) style->disable (TITLE);
			else if (match ("padding")) {
				num (style->pad_wd);
				style->enable (PADDING_WD);
			}
			else if (match ("-padding")) {
				style->pad_wd = 0;
				style->enable (PADDING_WD);
			}
			else if (match ("border")) {
				num (style->border);
				style->enable (BORDER_WD);
			}
			else if (match ("-border")) {
				style->border = 0;
				style->enable (BORDER_WD);
			}
			else
				error ("bad style command: ");

			jmp (',');
			inc ();
			skip_space ();
		};
		
		style_list.add (style);
	};
}

Style::Style ()
{
	// default
	pad_wd = 0;
	border = 0;
	flags = 0;
	mask = 0;
}

int
Style::enable (long flag)
{
	mask |= flag;
	flags |= flag;
}

int
Style::disable (long flag)
{
	mask |= flag;
	flags &= ~flag;
}

Style::~Style ()
{
	delete pattern;
}

int
Style::match (char *str)
{
	return match_wildcards (pattern, str);
}

StyleList::StyleList ()
{
	first = NULL;
}

int
StyleList::add (Style *n)
{
	if (!first)
	{
		first = last = n;
		return 0;
	};

	n->next = NULL;
	last->next = n;
	last = n;
}

StyleList::~StyleList ()
{
	Style *t;

	while (first)
	{
		t = first;
		first = first->next;
		delete t;
	};
}
