shithub: lola

ref: 3ca0009a7450e86fed00995d8e296c41619160ba
dir: /simple.c/

View raw version
#include "inc.h"

int bordersz = 4;
int titlesz = 17;//19;

enum {
	TITLE,
	LTITLE,
	TITLEHOLD,
	LTITLEHOLD,
	TITLETEXT,
	LTITLETEXT,
	TITLEHOLDTEXT,
	LTITLEHOLDTEXT,
	FRAME,
	LFRAME,

	NumWinColors
};

Image *wincolors[NumWinColors];
Image *icons[4];

Image *testcol;

void
btn(Image *img, Rectangle r, Image *col, Image *icon, int down)
{
	USED(down);

//	border(img, r, 1, col, ZP);
	r = centerrect(r, icon->r);
	draw(img, r, col, icon, ZP);
}

int
btnctl(Image *img, Rectangle r, Image *col, Image *icon)
{
	int over, prevover;

	prevover = 1;
	btn(img, r, col, icon, 1);
	while(mctl->buttons){
		readmouse(mctl);
		over = ptinrect(mctl->xy, r);
		if(over != prevover)
			btn(img, r, col, icon, over);
		prevover = over;
	}
	if(prevover)
		btn(img, r, col, icon, 0);
	return ptinrect(mctl->xy, r);
}

int
colsel(Window *w)
{
	return w->cur && w->cur->holdmode ?
		w == focused ? 2 : 3 :
		w == focused ? 0 : 1;
}

void
wdecor(Window *w)
{
	if(w->frame == nil)
		return;
	int c = TITLE + colsel(w);
	int c2 = w == focused ? FRAME : LFRAME;

	Rectangle r, b1, b2, b3;

	if(!w->noborder){
		r = w->frame->r;
		border(w->frame, r, bordersz, wincolors[c], ZP);
		border(w->frame, r, 1, wincolors[c2], ZP);
		border(w->frame, insetrect(w->contrect,-1), 1, wincolors[c2], ZP);
	}

	if(!w->notitle){
		r = w->titlerect;
		r.max.y--;
		draw(w->frame, r, wincolors[c], nil, ZP);

		b1 = r;
b1.max.x -= bordersz/2;
		b1.min.x = b1.max.x - titlesz + bordersz;
		b1.max.y = b1.min.y + Dx(b1);
		b2 = rectsubpt(b1, Pt(titlesz, 0));
		b3 = rectsubpt(b2, Pt(titlesz, 0));
		btn(w->frame, b1, wincolors[c+4], icons[3], 0);
		btn(w->frame, b2, wincolors[c+4], icons[1+w->maximized], 0);
		btn(w->frame, b3, wincolors[c+4], icons[0], 0);

r.min.x += bordersz/2;
r.min.y -= 2;
		Point pt = Pt(r.min.x, r.min.y);
//if(w->cur && w->ref == 1)
if(w->cur)
		string(w->frame, pt, wincolors[c+4], pt, font, w->cur->label);
	}

	r = rectsubpt(w->tabrect, Pt(0,1));
//	r.min.x--;
//	r.max.x++;
	draw(w->frame, r, wincolors[c], nil, ZP);

	int n = w->ref;
	if(n > 1){
		int wd = Dx(r)/n;
		int xxx = r.max.x;
		r.max.x = r.min.x + wd;
		for(WinTab *t = w->tab; t; t = t->next){
			if(t->next == nil)
				r.max.x = xxx;
	if(t != w->cur)
	draw(w->frame, r, testcol, nil, ZP);
			Point pt = Pt(r.min.x+bordersz/2, r.min.y);
			string(w->frame, pt, wincolors[c+4], pt, font, t->label);
			r = rectaddpt(r, Pt(wd,0));
		}
	}
}

Window *pick(void);
Window *wcreate_(Rectangle r, bool hidden);
void tmigrate(WinTab *t, Window *w);
void tmoveleft(WinTab *t);
void tmoveright(WinTab *t);

static void
tabctl(Window *w)
{
	Rectangle r;

	if(mctl->buttons & 7){
		wraise(w);
		wfocus(w);
		r = rectsubpt(w->tabrect, Pt(0,1));
		int n = w->ref;
		if(n > 1){
			int wd = Dx(r)/n;
			r.max.x = r.min.x + wd;
			for(WinTab *t = w->tab; t; t = t->next){
				if(ptinrect(mctl->xy, r)){
					if(mctl->buttons & 1){
						tfocus(t);
						/* chording */
						while(mctl->buttons){
							int b = mctl->buttons;
							if(b & 6){
								if(b & 2)
									tmoveleft(t);
								else
									tmoveright(t);
							}
							while(mctl->buttons == b)
								readmouse(mctl);
						}
					}else if(mctl->buttons & 2)
						tdelete(t);
					else if(mctl->buttons & 4){
Point pt = mctl->xy;
						Window *ww = pick();
						if(ww){
							tmigrate(t, ww);
							wraise(ww);
							wfocus(ww);
							return;
						}else{
							ww = wpointto(mctl->xy);
							if(ww == nil){
	tmigrate(t, wcreate_(rectaddpt(w->rect, subpt(mctl->xy, pt)), 0));
							}
						}
					}
					break;
				}
				r = rectaddpt(r, Pt(wd,0));
			}
		}
	}
}

static void
titlectl(Window *w)
{
	if(mctl->buttons & 7){
		wraise(w);
		wfocus(w);
		if(mctl->buttons & 1) {
			int c = TITLE + colsel(w);

			Rectangle r = w->titlerect;
			Rectangle br = Rect(0,0,titlesz-bordersz,titlesz-bordersz);
r.max.x -= bordersz/2;
			Rectangle br1 = rectaddpt(br, Pt(r.max.x-titlesz+bordersz, r.min.y));
			Rectangle br2 = rectsubpt(br1, Pt(titlesz, 0));
			Rectangle br3 = rectsubpt(br2, Pt(titlesz, 0));
			// hack...
			if(w->notitle)
				br1 = br2 = br3 = Rect(0,0,0,0);

			if(ptinrect(mctl->xy, br1)){
				if(btnctl(w->frame, br1, wincolors[c+4], icons[3]))
					wdelete(w);
			}else if(ptinrect(mctl->xy, br2)){
				if(btnctl(w->frame, br2, wincolors[c+4], icons[1+w->maximized])){
					if(w->maximized)
						wrestore(w);
					else
						wmaximize(w);
				}
			}else if(ptinrect(mctl->xy, br3)){
				if(btnctl(w->frame, br3, wincolors[c+4], icons[0]))
					whide(w);
			}else if(!w->maximized)
				grab(w, 1);
		}
		if(mctl->buttons & 4){
//			btn3menu();
			Window *ww = pick();
			if(ww){
				tmigrate(w->cur, ww);
				wraise(ww);
				wfocus(ww);
				return;
			}
		}
	}
}

void
wtitlectl(Window *w)
{
	if(ptinrect(mctl->xy, w->titlerect))
		titlectl(w);
	else
		tabctl(w);
}


static char minbtn[] = {
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 0, 0,
	0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
	0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

void
inittheme(void)
{
	freeimage(colors[HOLDTEXT]);
	freeimage(colors[PALEHOLDTEXT]);
	colors[HOLDTEXT] = getcolor("holdtext", 0x990000FF);
	colors[PALEHOLDTEXT] = getcolor("paleholdtext", 0xBB5D00FF);

//	wincolors[TITLE] = getcolor("title", 0x607DA1FF);
//	wincolors[LTITLE] = getcolor("ltitle", 0xA1A1A1FF);

//	wincolors[TITLE] = getcolor("title", 0x2F78EDFF);
//	wincolors[LTITLE] = getcolor("ltitle", 0x7C9DE3FF);

	wincolors[TITLE] = getcolor("title", 0x5297F9FF);
	wincolors[LTITLE] = getcolor("ltitle", 0x2C60B2FF);
	wincolors[TITLEHOLD] = getcolor("titlehold", 0xED2F2FFF);
	wincolors[LTITLEHOLD] = getcolor("ltitlehold", 0xE36A6AFF);

	wincolors[FRAME] = getcolor("frame", 0x000000FF);
	wincolors[LFRAME] = getcolor("lframe", 0x000000FF);

	wincolors[TITLETEXT] = getcolor("titletext", 0xFFFFFFFF);
	wincolors[LTITLETEXT] = getcolor("ltitletext", 0xFFFFFFFF);
	wincolors[TITLEHOLDTEXT] = getcolor("titleholdtext", 0xFFFFFFFF);
	wincolors[LTITLEHOLDTEXT] = getcolor("ltitleholdtext", 0xFFFFFFFF);

	icons[0] = mkicon(minbtn, 16, 13);
	icons[1] = mkicon(maxbtn, 16, 13);
	icons[2] = mkicon(rstbtn, 16, 13);
	icons[3] = mkicon(closebtn, 16, 13);

	testcol = getcolor("asdfasdfasdf", 0x00000020);
}