[PATCH] window.c: frame_button: Maximize, minimize, close, icon buttons in window frame.

Martin Minarik minarik11 at student.fiit.stuba.sk
Wed May 9 17:04:35 PDT 2012


---
 clients/window.c       |  256 +++++++++++++++++++++++++++++++++++++++++++++++-
 data/icon_window.png   |  Bin 0 -> 161 bytes
 data/sign_close.png    |  Bin 0 -> 235 bytes
 data/sign_maximize.png |  Bin 0 -> 204 bytes
 data/sign_minimize.png |  Bin 0 -> 191 bytes
 5 files changed, 253 insertions(+), 3 deletions(-)
 create mode 100644 data/icon_window.png
 create mode 100644 data/sign_close.png
 create mode 100644 data/sign_maximize.png
 create mode 100644 data/sign_minimize.png

diff --git a/clients/window.c b/clients/window.c
index c7da54a..5811e64 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -200,12 +200,48 @@ struct output {
 	void *user_data;
 };
 
+enum frame_button_action {
+	FRAME_BUTTON_NULL = 0,
+	FRAME_BUTTON_ICON = 1,
+	FRAME_BUTTON_CLOSE = 2,
+	FRAME_BUTTON_MINIMIZE = 3,
+	FRAME_BUTTON_MAXIMIZE = 4,
+};
+
+enum frame_button_pointer {
+	FRAME_BUTTON_DEFAULT = 0,
+	FRAME_BUTTON_OVER = 1,
+	FRAME_BUTTON_ACTIVE = 2,
+};
+
+enum frame_button_align {
+	FRAME_BUTTON_RIGHT = 0,
+	FRAME_BUTTON_LEFT = 1,
+};
+
+enum frame_button_decoration {
+	FRAME_BUTTON_NONE = 0,
+	FRAME_BUTTON_FANCY = 1,
+};
+
+struct frame_button {
+	struct widget *widget;
+	struct frame *frame;
+	cairo_surface_t *icon;
+	enum frame_button_action type;
+	enum frame_button_pointer state;
+	struct wl_list link;	/* buttons_list */
+	enum frame_button_align align;
+	enum frame_button_decoration decoration;
+};
+
 struct frame {
 	struct widget *widget;
 	struct widget *child;
 	int margin;
 	int width;
 	int titlebar_height;
+	struct wl_list buttons_list;
 };
 
 struct menu {
@@ -1112,6 +1148,8 @@ frame_resize_handler(struct widget *widget,
 	struct widget *child = frame->child;
 	struct rectangle allocation;
 	struct display *display = widget->window->display;
+	struct frame_button * button;
+	int x_l, x_r, y, w, h;
 	int decoration_width, decoration_height;
 	int opaque_margin;
 
@@ -1133,6 +1171,10 @@ frame_resize_handler(struct widget *widget,
 			      height - 2 * frame->margin);
 
 		opaque_margin = frame->margin + display->frame_radius;
+
+		wl_list_for_each(button, &frame->buttons_list, link) {
+			button->widget->opaque = 0;
+		}
 	} else {
 		decoration_width = 0;
 		decoration_height = 0;
@@ -1142,6 +1184,10 @@ frame_resize_handler(struct widget *widget,
 		allocation.width = width;
 		allocation.height = height;
 		opaque_margin = 0;
+
+		wl_list_for_each(button, &frame->buttons_list, link) {
+			button->widget->opaque = 1;
+		}
 	}
 
 	widget_set_allocation(child, allocation.x, allocation.y,
@@ -1165,6 +1211,190 @@ frame_resize_handler(struct widget *widget,
 			      widget->allocation.width - 2 * opaque_margin,
 			      widget->allocation.height - 2 * opaque_margin);
 	}
+	/* frame internal buttons */
+	x_r = frame->widget->allocation.width - frame->width - frame->margin;
+	x_l = frame->width + frame->margin;
+	y = frame->width + frame->margin;
+	wl_list_for_each(button, &frame->buttons_list, link) {
+		const int button_padding = 4;
+		w = cairo_image_surface_get_width(button->icon);
+		h = cairo_image_surface_get_height(button->icon);
+
+		if (button->decoration == FRAME_BUTTON_FANCY)
+			w += 10;
+
+		if (button->align == FRAME_BUTTON_LEFT) {
+			widget_set_allocation(button->widget,
+				x_l, y , w + 1, h + 1);
+			x_l += w;
+			x_l += button_padding;
+		} else {
+			x_r -= w;
+			widget_set_allocation(button->widget,
+				x_r, y , w + 1, h + 1);
+			x_r -= button_padding;
+		}
+	}
+}
+
+static int
+frame_button_enter_handler(struct widget *widget,
+            struct input *input, int32_t x, int32_t y, void *data)
+{
+	struct frame_button *frame_button = data;
+	widget_schedule_redraw(frame_button->widget);
+	frame_button->state = FRAME_BUTTON_OVER;
+	return POINTER_LEFT_PTR;
+}
+
+static int
+frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
+{
+	struct frame_button *frame_button = data;
+	widget_schedule_redraw(frame_button->widget);
+	frame_button->state = FRAME_BUTTON_DEFAULT;
+	return POINTER_LEFT_PTR;
+}
+
+static void
+frame_button_button_handler(struct widget *widget,
+		struct input *input, uint32_t time,
+		int button, int state, void *data)
+
+{
+	struct frame_button *frame_button = data;
+	struct window *window = widget->window;
+
+	if (button != BTN_LEFT)
+		return;
+
+	switch (state) {
+	case 1:
+		frame_button->state = FRAME_BUTTON_ACTIVE;
+		widget_schedule_redraw(frame_button->widget);
+		return;
+	break;
+	case 0:
+		frame_button->state = FRAME_BUTTON_DEFAULT;
+		widget_schedule_redraw(frame_button->widget);
+	break;
+	}
+
+	switch (frame_button->type) {
+	case FRAME_BUTTON_ICON:
+		fprintf(stderr,"Icon stub\n");
+/*		window_show_frame_menu(window, input, time);
+*/
+	break;
+	case FRAME_BUTTON_CLOSE:
+		if (window->close_handler)
+			window->close_handler(window->parent,
+					window->user_data);
+		else
+			display_exit(window->display);
+	break;
+	case FRAME_BUTTON_MINIMIZE:
+		fprintf(stderr,"Minimize stub\n");
+		return;
+	break;
+	case FRAME_BUTTON_MAXIMIZE:
+		window_set_maximized(window, window->type != TYPE_MAXIMIZED);
+		return;
+	break;
+	default:
+		/* Unknown operation */
+		return;
+	break;
+	}
+}
+
+static void
+frame_button_redraw_handler(struct widget *widget, void *data)
+{
+	struct frame_button *frame_button = data;
+	cairo_t *cr;
+	int width, height, x, y;
+	struct window *window = widget->window;
+
+	x = widget->allocation.x;
+	y = widget->allocation.y;
+	width = widget->allocation.width;
+	height = widget->allocation.height;
+
+	if (!width)
+		return;
+	if (!height)
+		return;
+	if (widget->opaque)
+		return;
+
+	cr = cairo_create(window->cairo_surface);
+
+	if (frame_button->decoration == FRAME_BUTTON_FANCY) {
+		cairo_set_line_width(cr, 1);
+
+		cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
+		cairo_rectangle (cr, x, y, 25, 16);
+
+		cairo_stroke_preserve(cr);
+
+		switch (frame_button->state) {
+		case FRAME_BUTTON_DEFAULT:
+			cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
+		break;
+		case FRAME_BUTTON_OVER:
+			cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
+		break;
+		case FRAME_BUTTON_ACTIVE:
+			cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
+		break;
+		}
+
+		cairo_fill (cr);
+
+		x += 4;
+	}
+
+	cairo_set_source_surface(cr, frame_button->icon,
+			x, y);
+	cairo_paint(cr);
+
+	cairo_destroy(cr);
+}
+
+struct widget *
+frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
+	enum frame_button_align align, enum frame_button_decoration style)
+{
+	struct frame_button *frame_button;
+	const char *icon = data;
+
+	frame_button = malloc (sizeof *frame_button);
+	memset(frame_button, 0, sizeof *frame_button);
+
+	frame_button->icon = cairo_image_surface_create_from_png(icon);
+	frame_button->widget = widget_add_widget(frame->widget, frame_button);
+	frame_button->frame = frame;
+	frame_button->type = type;
+	frame_button->align = align;
+	frame_button->decoration = style;
+
+	wl_list_insert(frame->buttons_list.prev, &frame_button->link);
+
+	widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
+	widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
+	widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
+	widget_set_button_handler(frame_button->widget, frame_button_button_handler);
+	return frame_button->widget;
+}
+
+static void
+frame_button_destroy(struct frame_button *frame_button)
+{
+	wl_list_remove(&frame_button->link);
+	cairo_surface_destroy(frame_button->icon);
+	free(frame_button);
+	return;
 }
 
 static void
@@ -1415,15 +1645,30 @@ frame_create(struct window *window, void *data)
 	frame->widget = window_add_widget(window, frame);
 	frame->child = widget_add_widget(frame->widget, data);
 	frame->margin = 32;
-	frame->width = 4;
-	frame->titlebar_height = 30
-;
+	frame->width = 6;
+	frame->titlebar_height = 27;
+
 	widget_set_redraw_handler(frame->widget, frame_redraw_handler);
 	widget_set_resize_handler(frame->widget, frame_resize_handler);
 	widget_set_enter_handler(frame->widget, frame_enter_handler);
 	widget_set_motion_handler(frame->widget, frame_motion_handler);
 	widget_set_button_handler(frame->widget, frame_button_handler);
 
+	/* Create empty list for frame buttons */
+	wl_list_init(&frame->buttons_list);
+
+	frame_button_create(frame, DATADIR "/weston/icon_window.png",
+		FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
+
+	frame_button_create(frame, DATADIR "/weston/sign_close.png",
+		FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
+
+	frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
+		FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
+
+	frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
+		FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
+
 	window->frame = frame;
 
 	return frame->child;
@@ -1432,6 +1677,11 @@ frame_create(struct window *window, void *data)
 static void
 frame_destroy(struct frame *frame)
 {
+	struct frame_button* button;
+	wl_list_for_each(button, &frame->buttons_list, link) {
+		frame_button_destroy(button);
+	}
+
 	/* frame->child must be destroyed by the application */
 	widget_destroy(frame->widget);
 	free(frame);
diff --git a/data/icon_window.png b/data/icon_window.png
new file mode 100644
index 0000000000000000000000000000000000000000..2587ca347fabf4b16162807bed2b4d63bf66d8c9
GIT binary patch
literal 161
zcmeAS at N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|I14-?iy0WW
zg+Z8+Vb&Z8pdfpRr>`sf9acsW7O|kmPeDS)o-U3d7N?UF76=#^85sOoeuP2IE7srx
z+Xe$5`19XC&cMLPh_Bs1!nA>lEnv<}4K{|WZLHG86P61CwKI6S`njxgN at xNA5$Y&K

literal 0
HcmV?d00001

diff --git a/data/sign_close.png b/data/sign_close.png
new file mode 100644
index 0000000000000000000000000000000000000000..741ea0b05e6a75505fe09dd3b09db60fcf0b7768
GIT binary patch
literal 235
zcmeAS at N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|*pj^6T^Rm@
z;DWu&Cj&(|3p^r=85p>QL70(Y)*K0-AbW|YuPgf<R%S74gTlRGHb5cC64!_l=ltB<
z)VvY~=c3falGGH1^30M91$R&1fbd2>aiAhMPZ!4!i_>o>T5~ZNa5%?T{kOiobn!I{
zF{uKMN&H;$l8<_}M)zLqWma%r)3Ti{bkQBQP+>K;ux3*qzU7U|(>}ZLzW=IaHnW$X
UeL*N=JkUf2Pgg&ebxsLQ0KU0I00000

literal 0
HcmV?d00001

diff --git a/data/sign_maximize.png b/data/sign_maximize.png
new file mode 100644
index 0000000000000000000000000000000000000000..2443a4c03675923262f206298ac694a8bc4aff53
GIT binary patch
literal 204
zcmeAS at N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|*pj^6T^Rm@
z;DWu&Cj&(|3p^r=85p>QL70(Y)*K0-AbW|YuPgf<R%S74xwYBRpMgS>C9V-A&iT2y
zsd*&~&PAz-C8;S2<(VZJ3hti10pX2&;y^{}o-U3d7N?UFBtA6=cK)3&^VjD}$=Uj%
oE5UD?0-0ANFmSg9TFNjmEG=LWj5Rc03e>{j>FVdQ&MBb at 0D0dzga7~l

literal 0
HcmV?d00001

diff --git a/data/sign_minimize.png b/data/sign_minimize.png
new file mode 100644
index 0000000000000000000000000000000000000000..f4f3d9d23b74dad1958ec9f20137cb5b003ecf03
GIT binary patch
literal 191
zcmeAS at N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|*pj^6T^Rm@
z;DWu&Cj&(|3p^r=85p>QL70(Y)*K0-AbW|YuPgf<R%S74p14a|yMaQIC9V-A&iT2y
zsd*&~&PAz-C8;S2<(VZJ3hti10pX2&;y^`Go-U3d7N?UFBv=<GL@>DNB>nfic-%y)
bjfcU)gOTOpox8Jusu(<7{an^LB{Ts57Xvcm

literal 0
HcmV?d00001

-- 
1.7.5.4



More information about the wayland-devel mailing list