Guitarix
gx_cairo_callbacks.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
3  * Copyright (C) 2011 Pete Shorthose
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  * --------------------------------------------------------------------------
19  *
20  *
21  * This are the cairo callbacks to draw the guitarix UI
22  *
23  *
24  * --------------------------------------------------------------------------
25  */
26 
27 #include "guitarix.h"
28 
29 /* --------------------------- gx_cairo namespace ------------------------ */
30 namespace gx_cairo
31 {
32 
33 static void render (GtkWidget *wi, cairo_t* cr) {
34  double rect_width = wi->allocation.width;
35  double rect_height = wi->allocation.height;
36  double x0 = wi->allocation.x;
37  double y0 = wi->allocation.y;
38 
39  cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 0.0f);
40  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
41  cairo_paint (cr);
42 
43  string path = string(GX_PIXMAPS_DIR) + "/gx_splash.png";
44  cairo_surface_t *image = cairo_image_surface_create_from_png(path.c_str());
45  cairo_rectangle(cr, x0, y0, rect_width, rect_height);
46  cairo_set_source_surface(cr, image, 0, 0);
47  cairo_paint(cr);
48  cairo_surface_destroy (image);
49 }
50 
51 void make_transparency(GtkWidget* wi) {
52 
53  // get widget dimension
54  gint rect_width = wi->allocation.width;
55  gint rect_height = wi->allocation.height;
56 
57  // make Image to fake transparency
58  static GdkBitmap* ShapeBitmap = NULL;
59  static cairo_t* cr = NULL;
60 
61  ShapeBitmap = gdk_pixmap_new(NULL, rect_width, rect_height, 1);
62  if (ShapeBitmap) {
63  cr = gdk_cairo_create (ShapeBitmap);
64  if (cairo_status (cr) == CAIRO_STATUS_SUCCESS) {
65  render (wi, cr);
66  cairo_destroy (cr);
67  // make widget invisible
68  gtk_widget_shape_combine_mask (wi, NULL, 0, 0);
69  // set sharp mask to visible part
70  gtk_widget_shape_combine_mask (wi, ShapeBitmap, 0, 0);
71  }
72  g_object_unref(ShapeBitmap);
73  }
74 }
75 
76 gboolean splash_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
77 {
78  cairo_t* cr = NULL;
79  cr = gdk_cairo_create (wi->window);
80  if (!cr) return FALSE;
81 
82  static bool ms = true;
83  if(ms) {
85  ms = false;
86  }
87  render (wi, cr);
88  cairo_destroy (cr);
89  return FALSE;
90 }
91 
92 gboolean rectangle_skin_color_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
93 {
94  cairo_t *cr;
95  /* create a cairo context */
96  cr = gdk_cairo_create(wi->window);
97  GdkRegion *region;
98  region = gdk_region_rectangle (&wi->allocation);
99  gdk_region_intersect (region, ev->region);
100  gdk_cairo_region (cr, region);
101  cairo_clip (cr);
102 
103  double x0 = wi->allocation.x+2;
104  double y0 = wi->allocation.y+2;
105  double rect_width = wi->allocation.width-2;
106  double rect_height = wi->allocation.height-2;
107 
108  cairo_rectangle (cr, x0,y0,rect_width,rect_height+1);
109  cairo_set_source_rgb (cr, 0, 0, 0);
110  cairo_fill (cr);
111 
112  cairo_pattern_t*pat =
113  cairo_pattern_create_linear (0, y0, 0, y0+rect_height);
114  //cairo_pattern_create_radial (-50, y0, 5,rect_width-10, rect_height, 20.0);
115  cairo_pattern_add_color_stop_rgba (pat, 0, 0.2, 0.2, 0.3, 0.6);
116  cairo_pattern_add_color_stop_rgba (pat, 1, 0.05, 0.05, 0.05, 0.6);
117  cairo_set_source (cr, pat);
118  cairo_rectangle (cr, x0+1,y0+1,rect_width-2,rect_height-1);
119  cairo_fill (cr);
120 
121  cairo_pattern_destroy (pat);
122  cairo_destroy(cr);
123  gdk_region_destroy (region);
124  return FALSE;
125 
126 }
127 
128 gboolean conv_widget_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
129 {
130  cairo_t *cr;
131  cairo_pattern_t *pat;
132 
133  /* create a cairo context */
134  cr = gdk_cairo_create(wi->window);
135  GdkRegion *region;
136  region = gdk_region_rectangle (&wi->allocation);
137  gdk_region_intersect (region, ev->region);
138  gdk_cairo_region (cr, region);
139  cairo_clip (cr);
140 
141  double x0 = wi->allocation.x+5;
142  double y0 = wi->allocation.y+5;
143  double rect_width = wi->allocation.width-10;
144  double rect_height = wi->allocation.height-10;
145  double radius = 36.;
146  double x1,y1;
147  x1=x0+rect_width;
148  y1=y0+rect_height;
149 
150  cairo_move_to (cr, x0, y0 + radius);
151  cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
152  cairo_line_to (cr, x1 - radius, y0);
153  cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
154  cairo_line_to (cr, x1 , y1 - radius);
155  cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
156  cairo_line_to (cr, x0 + radius, y1);
157  cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
158  cairo_close_path (cr);
159 
160  pat = cairo_pattern_create_linear (0, y0, 0, y1);
161  cairo_pattern_add_color_stop_rgba (pat, 1, 0., 0., 0., 0.8);
162  cairo_pattern_add_color_stop_rgba (pat, 0, 0, 0, 0, 0.4);
163  cairo_set_source (cr, pat);
164  cairo_fill_preserve (cr);
165 
166  cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
167  cairo_set_line_width (cr, 9.0);
168  cairo_stroke (cr);
169 
170  cairo_move_to (cr, x0, y0 + radius);
171  cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
172  cairo_line_to (cr, x1 - radius, y0);
173  cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
174  cairo_line_to (cr, x1 , y1 - radius);
175  cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
176  cairo_line_to (cr, x0 + radius, y1);
177  cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
178  cairo_close_path (cr);
179 
180  cairo_set_source_rgb (cr, 0.2, 0.2, 0.2);
181  cairo_set_line_width (cr, 1.0);
182  cairo_stroke (cr);
183 
184  cairo_pattern_destroy (pat);
185  cairo_destroy(cr);
186  gdk_region_destroy (region);
187 
188  return FALSE;
189 }
190 
191 gboolean error_box_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
192 {
193  cairo_t *cr;
194  /* create a cairo context */
195  cr = gdk_cairo_create(wi->window);
196  GdkRegion *region;
197  region = gdk_region_rectangle (&wi->allocation);
198  gdk_region_intersect (region, ev->region);
199  gdk_cairo_region (cr, region);
200  cairo_clip (cr);
201 
202  double x0 = wi->allocation.x;
203  double y0 = wi->allocation.y;
204  double rect_width = wi->allocation.width;
205  double rect_height = wi->allocation.height;
206 
207  cairo_rectangle (cr, x0,y0,rect_width,rect_height);
208  cairo_set_source_rgb (cr, 0.22, 0.22, 0.22);
209  // cairo_set_line_width(cr, 2.0);
210  cairo_fill(cr);
211 
212  cairo_destroy(cr);
213  gdk_region_destroy (region);
214 
215  return FALSE;
216 }
217 
218 gboolean start_box_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
219 {
220  cairo_t *cr;
221  /* create a cairo context */
222  cr = gdk_cairo_create(wi->window);
223  GdkRegion *region;
224  region = gdk_region_rectangle (&wi->allocation);
225  gdk_region_intersect (region, ev->region);
226  gdk_cairo_region (cr, region);
227  cairo_clip (cr);
228 
229  double x0 = wi->allocation.x+1;
230  double y0 = wi->allocation.y+1;
231  double rect_width = wi->allocation.width-2;
232  double rect_height = wi->allocation.height-2;
233 
234  cairo_rectangle (cr, x0-1,y0-1,rect_width+2,rect_height+2);
235  cairo_set_source_rgb (cr, 0, 0, 0);
236  cairo_set_line_width(cr, 2.0);
237  cairo_stroke(cr);
238 
239  cairo_pattern_t*pat = cairo_pattern_create_linear (x0, y0+rect_height/2,x0, y0);
240  cairo_pattern_set_extend(pat, CAIRO_EXTEND_REFLECT);
241 
242  cairo_pattern_add_color_stop_rgba (pat, 0, 0.1, 0.1, 0.2, 0.6);
243  cairo_pattern_add_color_stop_rgba (pat, 1, 0.05, 0.05, 0.05, 0.6);
244 
245  cairo_set_source (cr, pat);
246  cairo_rectangle (cr, x0+2,y0+2,rect_width-4,rect_height-4);
247  cairo_fill (cr);
248 
249  cairo_rectangle (cr, x0+8,y0+31,rect_width-16,rect_height-75);
250  cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
251  cairo_set_line_width(cr, 2.0);
252  cairo_stroke_preserve(cr);
253  pat = cairo_pattern_create_linear (x0+8, y0+rect_height/2-37,x0, y0);
254  cairo_pattern_set_extend(pat, CAIRO_EXTEND_REFLECT);
255  cairo_pattern_add_color_stop_rgba (pat, 0, 0.2, 0.2, 0.3, 0.6);
256  cairo_pattern_add_color_stop_rgba (pat, 1, 0.05, 0.05, 0.05, 0.6);
257  cairo_set_source (cr, pat);
258  cairo_fill (cr);
259 
260  cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
261  cairo_set_line_width(cr, 2.0);
262  cairo_move_to(cr,x0+rect_width-3, y0+3);
263  cairo_line_to(cr, x0+rect_width-3, y0+rect_height-2);
264  cairo_line_to(cr, x0+2, y0+rect_height-2);
265  cairo_stroke(cr);
266 
267  cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
268  cairo_set_line_width(cr, 2.0);
269  cairo_move_to(cr,x0+3, y0+rect_height-1);
270  cairo_line_to(cr, x0+3, y0+3);
271  cairo_line_to(cr, x0+rect_width-3, y0+3);
272  cairo_stroke(cr);
273 
274  cairo_set_source_rgb(cr, 0.5, 0.5, 0.5);
275  cairo_set_line_width(cr, 1.5);
276  cairo_arc (cr, x0+7, y0+7, 1.5, 0, 2*M_PI);
277  cairo_move_to(cr,x0+rect_width-8, y0+7);
278  cairo_arc (cr, x0+rect_width-7, y0+7, 1.5, 0, 2*M_PI);
279  cairo_move_to(cr,x0+rect_width-7, y0+rect_height-6);
280  cairo_arc (cr, x0+rect_width-7, y0+rect_height-6, 1.5, 0, 2*M_PI);
281  cairo_move_to(cr,x0+7, y0+rect_height-6);
282  cairo_arc (cr, x0+7, y0+rect_height-6, 1.5, 0, 2*M_PI);
283  cairo_stroke_preserve(cr);
284  cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
285  cairo_fill (cr);
286 
287  cairo_pattern_destroy (pat);
288  cairo_destroy(cr);
289  gdk_region_destroy (region);
290 
291  return FALSE;
292 }
293 }
void make_transparency(GtkWidget *wi)
gboolean conv_widget_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
gboolean error_box_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
#define M_PI
gboolean splash_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
gboolean rectangle_skin_color_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
gboolean start_box_expose(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)