1
/*******************************************************************************
2
**3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
3
** 10 20 30 40 50 60 70 80
6
** notification-test-cairo
12
** Mirco "MacSlow" Mueller <mirco.mueller@ubuntu.com>
14
** copyright (C) Mirco Mueller, oct. 2008, placed under the terms of the LGPL
16
*******************************************************************************/
22
#include <gdk/gdkkeysyms.h>
24
double g_alpha = 0.95f;
25
gboolean g_entered = FALSE;
26
gboolean g_left = FALSE;
30
draw_round_rect (cairo_t* cr,
31
double aspect, /* aspect-ratio */
32
double x, /* top-left corner */
33
double y, /* top-left corner */
34
double corner_radius, /* "size" of the corners */
35
double width, /* width of the rectangle */
36
double height /* height of the rectangle */)
38
double radius = corner_radius / aspect;
40
/* top-left, right of the corner */
41
cairo_move_to (cr, x + radius, y);
43
/* top-right, left of the corner */
48
/* top-right, below the corner */
53
-90.0f * G_PI / 180.0f,
54
0.0f * G_PI / 180.0f);
56
/* bottom-right, above the corner */
61
/* bottom-right, left of the corner */
67
90.0f * G_PI / 180.0f);
69
/* bottom-left, right of the corner */
74
/* bottom-left, above the corner */
79
90.0f * G_PI / 180.0f,
80
180.0f * G_PI / 180.0f);
82
/* top-left, below the corner */
87
/* top-left, right of the corner */
92
180.0f * G_PI / 180.0f,
93
270.0f * G_PI / 180.0f);
97
screen_changed_handler (GtkWidget* window,
98
GdkScreen* old_screen,
101
GdkScreen* screen = gtk_widget_get_screen (window);
102
GdkColormap* colormap = gdk_screen_get_rgba_colormap (screen);
105
colormap = gdk_screen_get_rgb_colormap (screen);
107
gtk_widget_set_colormap (window, colormap);
111
delete_handler (GtkWidget* window,
120
key_press_handler (GtkWidget* window,
124
if (event->type == GDK_KEY_PRESS &&
125
(event->keyval == GDK_q || event->keyval == GDK_Escape))
127
delete_handler (window, NULL, NULL);
134
update_input_shape (GtkWidget* window,
138
GdkBitmap* mask = NULL;
141
mask = (GdkBitmap*) gdk_pixmap_new (NULL, width, height, 1);
144
cr = gdk_cairo_create (mask);
145
if (cairo_status (cr) == CAIRO_STATUS_SUCCESS)
147
cairo_scale (cr, 1.0f, 1.0f);
148
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
150
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
151
cairo_set_source_rgb (cr, 1.0f, 1.0f, 1.0f);
163
gtk_widget_input_shape_combine_mask (window,
167
gtk_widget_input_shape_combine_mask (window,
173
g_object_unref ((gpointer) mask);
178
expose_handler (GtkWidget* window,
179
GdkEventExpose* event,
184
cr = gdk_cairo_create (window->window);
186
cairo_scale (cr, 1.0f, 1.0f);
187
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
189
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
190
cairo_set_source_rgba (cr, 0.05f, 0.05f, 0.05f, g_alpha);
196
(double) window->allocation.width,
197
(double) window->allocation.height);
201
cairo_select_font_face (cr,
203
CAIRO_FONT_SLANT_NORMAL,
204
CAIRO_FONT_WEIGHT_NORMAL);
205
cairo_set_font_size (cr, 24.0f);
206
cairo_move_to (cr, 50.0f, 50.0f);
207
cairo_text_path (cr, "GTK+ bubble");
208
cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 0.5f);
217
redraw_handler (GtkWidget* window)
219
if (g_left && g_alpha < 0.95f)
222
if (g_entered && g_alpha > 0.1f)
225
gtk_window_set_opacity (GTK_WINDOW (window), g_alpha);
231
pointer_update (GtkWidget* window)
242
if (GTK_WIDGET_REALIZED (window))
244
gtk_widget_get_pointer (window, &pointer_rel_x, &pointer_rel_y);
245
gtk_window_get_position (GTK_WINDOW (window), &win_x, &win_y);
246
pointer_abs_x = win_x + pointer_rel_x;
247
pointer_abs_y = win_y + pointer_rel_y;
248
gtk_window_get_size (GTK_WINDOW (window), &width, &height);
249
if (pointer_abs_x >= win_x &&
250
pointer_abs_x <= win_x + width &&
251
pointer_abs_y >= win_y &&
252
pointer_abs_y <= win_y + height)
271
GtkWidget* window = NULL;
272
GError* error = NULL;
273
guint draw_handler_id = 0;
274
guint pointer_update_id = 0;
279
GOptionContext* option_context = NULL;
280
GOptionEntry options[] = {{"xposition",
285
"x-pos of the top-left corner",
292
"y-pos of the top-left corner",
299
"open window with this width",
306
"open window with this height",
310
gtk_init (&argc, &argv);
312
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
316
gtk_widget_add_events (window,
317
GDK_POINTER_MOTION_MASK |
318
GDK_BUTTON_PRESS_MASK |
319
GDK_BUTTON_RELEASE_MASK);
321
/* hook up input-event handlers to window */
322
g_signal_connect (G_OBJECT (window),
324
G_CALLBACK (delete_handler),
326
g_signal_connect (G_OBJECT (window),
328
G_CALLBACK (key_press_handler),
330
g_signal_connect (G_OBJECT (window),
332
G_CALLBACK (screen_changed_handler),
335
/* handle commandline options */
340
option_context = g_option_context_new ("- prototype for some new tech");
341
g_option_context_add_main_entries (option_context,
343
"notification test");
344
g_option_context_parse (option_context, &argc, &argv, NULL);
345
g_option_context_free (option_context);
347
gtk_widget_set_size_request (window, width, height);
348
gtk_window_move (GTK_WINDOW (window), x, y);
350
/* make sure the window opens with a RGBA-visual */
351
screen_changed_handler (window, NULL, NULL);
352
gtk_widget_realize (window);
353
gdk_window_set_back_pixmap (window->window, NULL, FALSE);
355
/* hook up window-event handlers to window */
356
g_signal_connect (G_OBJECT (window),
358
G_CALLBACK (expose_handler),
361
/* do nasty busy-polling rendering in the drawing-area */
362
draw_handler_id = g_timeout_add (1000/60,
363
(GSourceFunc) redraw_handler,
366
/* read out current mouse-pointer position every 1/10 second */
367
pointer_update_id = g_timeout_add (100,
368
(GSourceFunc) pointer_update,
371
/* "clear" input-mask, set title/icon/attributes */
372
update_input_shape (window, 1, 1);
373
gtk_widget_set_app_paintable (window, TRUE);
374
gtk_window_set_title (GTK_WINDOW (window), "notification-test-cairo");
375
gtk_window_set_icon_from_file (GTK_WINDOW (window),
378
gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
379
gtk_window_set_keep_above (GTK_WINDOW (window), TRUE);
380
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
381
gtk_widget_show_all (window);