1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #ifndef _CAIRO_UTIL_H
27 #define _CAIRO_UTIL_H
28 
29 #include <stdint.h>
30 #include <cairo.h>
31 
32 #include <wayland-client.h>
33 #include <wayland-util.h>
34 
35 void
36 surface_flush_device(cairo_surface_t *surface);
37 
38 void
39 render_shadow(cairo_t *cr, cairo_surface_t *surface,
40 	      int x, int y, int width, int height, int margin, int top_margin);
41 
42 void
43 tile_source(cairo_t *cr, cairo_surface_t *surface,
44 	    int x, int y, int width, int height, int margin, int top_margin);
45 
46 void
47 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius);
48 
49 cairo_surface_t *
50 load_cairo_surface(const char *filename);
51 
52 struct theme {
53 	cairo_surface_t *active_frame;
54 	cairo_surface_t *inactive_frame;
55 	cairo_surface_t *shadow;
56 	int frame_radius;
57 	int margin;
58 	int width;
59 	int titlebar_height;
60 };
61 
62 struct theme *
63 theme_create(void);
64 void
65 theme_destroy(struct theme *t);
66 
67 enum {
68 	THEME_FRAME_ACTIVE = 1,
69 	THEME_FRAME_MAXIMIZED = 2,
70 	THEME_FRAME_NO_TITLE = 4
71 };
72 
73 void
74 theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags);
75 void
76 theme_render_frame(struct theme *t,
77 		   cairo_t *cr, int width, int height,
78 		   const char *title, cairo_rectangle_int_t *title_rect,
79 		   struct wl_list *buttons, uint32_t flags);
80 
81 enum theme_location {
82 	THEME_LOCATION_INTERIOR = 0,
83 	THEME_LOCATION_RESIZING_TOP = 1,
84 	THEME_LOCATION_RESIZING_BOTTOM = 2,
85 	THEME_LOCATION_RESIZING_LEFT = 4,
86 	THEME_LOCATION_RESIZING_TOP_LEFT = 5,
87 	THEME_LOCATION_RESIZING_BOTTOM_LEFT = 6,
88 	THEME_LOCATION_RESIZING_RIGHT = 8,
89 	THEME_LOCATION_RESIZING_TOP_RIGHT = 9,
90 	THEME_LOCATION_RESIZING_BOTTOM_RIGHT = 10,
91 	THEME_LOCATION_RESIZING_MASK = 15,
92 	THEME_LOCATION_EXTERIOR = 16,
93 	THEME_LOCATION_TITLEBAR = 17,
94 	THEME_LOCATION_CLIENT_AREA = 18,
95 };
96 
97 enum theme_location
98 theme_get_location(struct theme *t, int x, int y, int width, int height, int flags);
99 
100 struct frame;
101 
102 enum frame_status {
103 	FRAME_STATUS_NONE = 0,
104 	FRAME_STATUS_REPAINT = 0x1,
105 	FRAME_STATUS_MINIMIZE = 0x2,
106 	FRAME_STATUS_MAXIMIZE = 0x4,
107 	FRAME_STATUS_CLOSE = 0x8,
108 	FRAME_STATUS_MENU = 0x10,
109 	FRAME_STATUS_RESIZE = 0x20,
110 	FRAME_STATUS_MOVE = 0x40,
111 	FRAME_STATUS_ALL = 0x7f
112 };
113 
114 enum frame_flag {
115 	FRAME_FLAG_ACTIVE = 0x1,
116 	FRAME_FLAG_MAXIMIZED = 0x2
117 };
118 
119 enum {
120 	FRAME_BUTTON_NONE = 0,
121 	FRAME_BUTTON_CLOSE = 0x1,
122 	FRAME_BUTTON_MAXIMIZE = 0x2,
123 	FRAME_BUTTON_MINIMIZE = 0x4,
124 	FRAME_BUTTON_ALL = 0x7
125 };
126 
127 struct frame *
128 frame_create(struct theme *t, int32_t width, int32_t height, uint32_t buttons,
129              const char *title, cairo_surface_t *icon);
130 
131 void
132 frame_destroy(struct frame *frame);
133 
134 /* May set FRAME_STATUS_REPAINT */
135 int
136 frame_set_title(struct frame *frame, const char *title);
137 
138 /* May set FRAME_STATUS_REPAINT */
139 void
140 frame_set_icon(struct frame *frame, cairo_surface_t *icon);
141 
142 /* May set FRAME_STATUS_REPAINT */
143 void
144 frame_set_flag(struct frame *frame, enum frame_flag flag);
145 
146 /* May set FRAME_STATUS_REPAINT */
147 void
148 frame_unset_flag(struct frame *frame, enum frame_flag flag);
149 
150 /* May set FRAME_STATUS_REPAINT */
151 void
152 frame_resize(struct frame *frame, int32_t width, int32_t height);
153 
154 /* May set FRAME_STATUS_REPAINT */
155 void
156 frame_resize_inside(struct frame *frame, int32_t width, int32_t height);
157 
158 int32_t
159 frame_width(struct frame *frame);
160 
161 int32_t
162 frame_height(struct frame *frame);
163 
164 void
165 frame_interior(struct frame *frame, int32_t *x, int32_t *y,
166 	       int32_t *width, int32_t *height);
167 void
168 frame_input_rect(struct frame *frame, int32_t *x, int32_t *y,
169 		 int32_t *width, int32_t *height);
170 void
171 frame_opaque_rect(struct frame *frame, int32_t *x, int32_t *y,
172 		  int32_t *width, int32_t *height);
173 
174 int
175 frame_get_shadow_margin(struct frame *frame);
176 
177 uint32_t
178 frame_status(struct frame *frame);
179 
180 void
181 frame_status_clear(struct frame *frame, enum frame_status status);
182 
183 /* May set FRAME_STATUS_REPAINT */
184 enum theme_location
185 frame_pointer_enter(struct frame *frame, void *pointer, int x, int y);
186 
187 /* May set FRAME_STATUS_REPAINT */
188 enum theme_location
189 frame_pointer_motion(struct frame *frame, void *pointer, int x, int y);
190 
191 /* May set FRAME_STATUS_REPAINT */
192 void
193 frame_pointer_leave(struct frame *frame, void *pointer);
194 
195 /* Call to indicate that a button has been pressed/released.  The return
196  * value for a button release will be the same as for the corresponding
197  * press.  This allows you to more easily track grabs.  If you want the
198  * actual location, simply keep the location from the last
199  * frame_pointer_motion call.
200  *
201  * May set:
202  *	FRAME_STATUS_MINIMIZE
203  *	FRAME_STATUS_MAXIMIZE
204  *	FRAME_STATUS_CLOSE
205  *	FRAME_STATUS_MENU
206  *	FRAME_STATUS_RESIZE
207  *	FRAME_STATUS_MOVE
208  */
209 enum theme_location
210 frame_pointer_button(struct frame *frame, void *pointer,
211 		     uint32_t button, enum wl_pointer_button_state state);
212 
213 enum theme_location
214 frame_touch_down(struct frame *frame, void *data, int32_t id, int x, int y);
215 
216 void
217 frame_touch_up(struct frame *frame, void *data, int32_t id);
218 
219 enum theme_location
220 frame_double_click(struct frame *frame, void *pointer,
221 		   uint32_t button, enum wl_pointer_button_state state);
222 
223 void
224 frame_double_touch_down(struct frame *frame, void *data, int32_t id,
225 			int x, int y);
226 
227 void
228 frame_double_touch_up(struct frame *frame, void *data, int32_t id);
229 
230 void
231 frame_repaint(struct frame *frame, cairo_t *cr);
232 
233 #endif
234