From 29ffa46d08c868c2d8cf76e6a69cd65faa666570 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 31 Jul 2009 16:32:24 -0400 Subject: [PATCH] Move drawing functions from shell-global into new shell-drawing.c file Just to avoid shell-global.c bloat. --- js/ui/appDisplay.js | 10 +-- js/ui/overlay.js | 36 ++++----- js/ui/widget.js | 6 +- src/Makefile.am | 2 + src/shell-drawing.c | 180 ++++++++++++++++++++++++++++++++++++++++++++ src/shell-drawing.h | 28 +++++++ src/shell-global.c | 177 ------------------------------------------- src/shell-global.h | 16 ---- 8 files changed, 236 insertions(+), 219 deletions(-) create mode 100644 src/shell-drawing.c create mode 100644 src/shell-drawing.h diff --git a/js/ui/appDisplay.js b/js/ui/appDisplay.js index a790e3fee..9ecfc116b 100644 --- a/js/ui/appDisplay.js +++ b/js/ui/appDisplay.js @@ -497,11 +497,11 @@ WellDisplayItem.prototype = { if (this._windows.length > 0) { let glow = new Shell.DrawingArea({}); glow.connect('redraw', Lang.bind(this, function (e, tex) { - Shell.Global.clutter_cairo_texture_draw_glow(tex, - GLOW_COLOR.red / 255, - GLOW_COLOR.green / 255, - GLOW_COLOR.blue / 255, - GLOW_COLOR.alpha / 255); + Shell.draw_glow(tex, + GLOW_COLOR.red / 255, + GLOW_COLOR.green / 255, + GLOW_COLOR.blue / 255, + GLOW_COLOR.alpha / 255); })); this._name.connect('notify::allocation', Lang.bind(this, function (n, alloc) { let x = this._name.x; diff --git a/js/ui/overlay.js b/js/ui/overlay.js index b2156af47..f0ea2e770 100644 --- a/js/ui/overlay.js +++ b/js/ui/overlay.js @@ -258,12 +258,12 @@ Dash.prototype = { dashPane.append(dashBackground, Big.BoxPackFlags.EXPAND); - let dashLeft = global.create_horizontal_gradient(DASH_LEFT_COLOR, - DASH_MIDDLE_COLOR); - let dashRight = global.create_horizontal_gradient(DASH_MIDDLE_COLOR, - DASH_RIGHT_COLOR); - let dashShadow = global.create_horizontal_gradient(SHADOW_COLOR, - TRANSPARENT_COLOR); + let dashLeft = Shell.create_horizontal_gradient(DASH_LEFT_COLOR, + DASH_MIDDLE_COLOR); + let dashRight = Shell.create_horizontal_gradient(DASH_MIDDLE_COLOR, + DASH_RIGHT_COLOR); + let dashShadow = Shell.create_horizontal_gradient(SHADOW_COLOR, + TRANSPARENT_COLOR); dashShadow.set_width(SHADOW_WIDTH); dashBackground.append(dashLeft, Big.BoxPackFlags.EXPAND); @@ -424,12 +424,12 @@ Dash.prototype = { border_color: DASH_BORDER_COLOR }); this._resultsPane.add_actor(resultsBackground); - let resultsLeft = global.create_horizontal_gradient(PANE_LEFT_COLOR, - PANE_MIDDLE_COLOR); - let resultsRight = global.create_horizontal_gradient(PANE_MIDDLE_COLOR, - PANE_RIGHT_COLOR); - let resultsShadow = global.create_horizontal_gradient(SHADOW_COLOR, - TRANSPARENT_COLOR); + let resultsLeft = Shell.create_horizontal_gradient(PANE_LEFT_COLOR, + PANE_MIDDLE_COLOR); + let resultsRight = Shell.create_horizontal_gradient(PANE_MIDDLE_COLOR, + PANE_RIGHT_COLOR); + let resultsShadow = Shell.create_horizontal_gradient(SHADOW_COLOR, + TRANSPARENT_COLOR); resultsShadow.set_width(SHADOW_WIDTH); resultsBackground.append(resultsLeft, Big.BoxPackFlags.EXPAND); @@ -460,12 +460,12 @@ Dash.prototype = { this._detailsPane.append(detailsBackground, Big.BoxPackFlags.EXPAND); - let detailsLeft = global.create_horizontal_gradient(PANE_LEFT_COLOR, - PANE_MIDDLE_COLOR); - let detailsRight = global.create_horizontal_gradient(PANE_MIDDLE_COLOR, - PANE_RIGHT_COLOR); - let detailsShadow = global.create_horizontal_gradient(SHADOW_COLOR, - TRANSPARENT_COLOR); + let detailsLeft = Shell.create_horizontal_gradient(PANE_LEFT_COLOR, + PANE_MIDDLE_COLOR); + let detailsRight = Shell.create_horizontal_gradient(PANE_MIDDLE_COLOR, + PANE_RIGHT_COLOR); + let detailsShadow = Shell.create_horizontal_gradient(SHADOW_COLOR, + TRANSPARENT_COLOR); detailsShadow.set_width(SHADOW_WIDTH); detailsBackground.append(detailsLeft, Big.BoxPackFlags.EXPAND); diff --git a/js/ui/widget.js b/js/ui/widget.js index 4445aba22..e5006d18a 100644 --- a/js/ui/widget.js +++ b/js/ui/widget.js @@ -162,9 +162,9 @@ ClockWidget.prototype = { _updateCairo: function(time) { let global = Shell.Global.get(); - global.clutter_cairo_texture_draw_clock(this.collapsedActor, - time.getHours() % 12, - time.getMinutes()); + Shell.draw_clock(this.collapsedActor, + time.getHours() % 12, + time.getMinutes()); } }; diff --git a/src/Makefile.am b/src/Makefile.am index 8a7eae6e6..8bc6bd39a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -58,6 +58,8 @@ libgnome_shell_la_SOURCES = \ shell-app-system.h \ shell-arrow.c \ shell-arrow.h \ + shell-drawing.c \ + shell-drawing.h \ shell-drawing-area.c \ shell-drawing-area.h \ shell-embedded-window.c \ diff --git a/src/shell-drawing.c b/src/shell-drawing.c new file mode 100644 index 000000000..58617bdb6 --- /dev/null +++ b/src/shell-drawing.c @@ -0,0 +1,180 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ + +#include "shell-drawing.h" +#include + +/** + * shell_create_vertical_gradient: + * @top: the color at the top + * @bottom: the color at the bottom + * + * Creates a vertical gradient actor. + * + * Return value: (transfer none): a #ClutterCairoTexture actor with the + * gradient. The texture actor is floating, hence (transfer none). + */ +ClutterCairoTexture * +shell_create_vertical_gradient (ClutterColor *top, + ClutterColor *bottom) +{ + ClutterCairoTexture *texture; + cairo_t *cr; + cairo_pattern_t *pattern; + + /* Draw the gradient on an 8x8 pixel texture. Because the gradient is drawn + * from the uppermost to the lowermost row, after stretching 1/16 of the + * texture height has the top color and 1/16 has the bottom color. The 8 + * pixel width is chosen for reasons related to graphics hardware internals. + */ + texture = CLUTTER_CAIRO_TEXTURE (clutter_cairo_texture_new (8, 8)); + cr = clutter_cairo_texture_create (texture); + + pattern = cairo_pattern_create_linear (0, 0, 0, 8); + cairo_pattern_add_color_stop_rgba (pattern, 0, + top->red / 255., + top->green / 255., + top->blue / 255., + top->alpha / 255.); + cairo_pattern_add_color_stop_rgba (pattern, 1, + bottom->red / 255., + bottom->green / 255., + bottom->blue / 255., + bottom->alpha / 255.); + + cairo_set_source (cr, pattern); + cairo_paint (cr); + + cairo_pattern_destroy (pattern); + cairo_destroy (cr); + + return texture; +} + +/** + * shell_create_horizontal_gradient: + * @left: the color on the left + * @right: the color on the right + * + * Creates a horizontal gradient actor. + * + * Return value: (transfer none): a #ClutterCairoTexture actor with the + * gradient. The texture actor is floating, hence (transfer none). + */ +ClutterCairoTexture * +shell_create_horizontal_gradient (ClutterColor *left, + ClutterColor *right) +{ + ClutterCairoTexture *texture; + cairo_t *cr; + cairo_pattern_t *pattern; + + /* Draw the gradient on an 8x1 pixel texture. Because the gradient is drawn + * from the left to the right column, after stretching 1/16 of the + * texture width has the left side color and 1/16 has the right side color. + * There is no reason to use the 8 pixel height that would be similar to the + * reason we are using the 8 pixel width for the vertical gradient, so we + * are just using the 1 pixel height instead. + */ + texture = CLUTTER_CAIRO_TEXTURE (clutter_cairo_texture_new (8, 1)); + cr = clutter_cairo_texture_create (texture); + + pattern = cairo_pattern_create_linear (0, 0, 8, 0); + cairo_pattern_add_color_stop_rgba (pattern, 0, + left->red / 255., + left->green / 255., + left->blue / 255., + left->alpha / 255.); + cairo_pattern_add_color_stop_rgba (pattern, 1, + right->red / 255., + right->green / 255., + right->blue / 255., + right->alpha / 255.); + + cairo_set_source (cr, pattern); + cairo_paint (cr); + + cairo_pattern_destroy (pattern); + cairo_destroy (cr); + + return texture; +} + +void +shell_draw_clock (ClutterCairoTexture *texture, + int hour, + int minute) +{ + cairo_t *cr; + guint width, height; + double xc, yc, radius, hour_radius, minute_radius; + double angle; + + clutter_cairo_texture_get_surface_size (texture, &width, &height); + xc = (double)width / 2; + yc = (double)height / 2; + radius = (double)(MIN(width, height)) / 2 - 2; + minute_radius = radius - 3; + hour_radius = radius / 2; + + clutter_cairo_texture_clear (texture); + cr = clutter_cairo_texture_create (texture); + cairo_set_line_width (cr, 1.0); + + /* Outline */ + cairo_arc (cr, xc, yc, radius, 0.0, 2.0 * M_PI); + cairo_stroke (cr); + + /* Hour hand. (We add a fraction to @hour for the minutes, then + * convert to radians, and then subtract pi/2 because cairo's origin + * is at 3:00, not 12:00.) + */ + angle = ((hour + minute / 60.0) / 12.0) * 2.0 * M_PI - M_PI / 2.0; + cairo_move_to (cr, xc, yc); + cairo_line_to (cr, + xc + hour_radius * cos (angle), + yc + hour_radius * sin (angle)); + cairo_stroke (cr); + + /* Minute hand */ + angle = (minute / 60.0) * 2.0 * M_PI - M_PI / 2.0; + cairo_move_to (cr, xc, yc); + cairo_line_to (cr, + xc + minute_radius * cos (angle), + yc + minute_radius * sin (angle)); + cairo_stroke (cr); + + cairo_destroy (cr); +} + +void +shell_draw_glow (ClutterCairoTexture *texture, + double red, + double green, + double blue, + double alpha) +{ + cairo_t *cr; + guint width, height; + cairo_pattern_t *gradient; + + clutter_cairo_texture_get_surface_size (texture, &width, &height); + + clutter_cairo_texture_clear (texture); + cr = clutter_cairo_texture_create (texture); + + cairo_save (cr); + cairo_translate (cr, width / 2.0, height / 2.0); + cairo_scale (cr, width / 2.0, height / 2.0); + + gradient = cairo_pattern_create_radial (0.0, 0.0, 0.0, 0.0, 0.0, 1.0); + cairo_pattern_add_color_stop_rgba (gradient, 0.0, red, green, blue, alpha); + cairo_pattern_add_color_stop_rgba (gradient, 0.7, red, green, blue, alpha * 0.7); + cairo_pattern_add_color_stop_rgba (gradient, 1.0, red, green, blue, alpha * 0.3); + cairo_set_source (cr, gradient); + + cairo_arc (cr, 0.0, 0.0, 1.0, 0.0, 2.0 * M_PI); + cairo_fill (cr); + cairo_restore (cr); + cairo_pattern_destroy (gradient); + cairo_destroy (cr); +} diff --git a/src/shell-drawing.h b/src/shell-drawing.h new file mode 100644 index 000000000..78c4fcfd1 --- /dev/null +++ b/src/shell-drawing.h @@ -0,0 +1,28 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ + +#ifndef __SHELL_DRAWING_H__ +#define __SHELL_DRAWING_H__ + +#include + +G_BEGIN_DECLS + +ClutterCairoTexture *shell_create_vertical_gradient (ClutterColor *top, + ClutterColor *bottom); + +ClutterCairoTexture *shell_create_horizontal_gradient (ClutterColor *left, + ClutterColor *right); + +void shell_draw_clock (ClutterCairoTexture *texture, + int hour, + int minute); + +void shell_draw_glow (ClutterCairoTexture *texture, + double red, + double blue, + double green, + double alpha); + +G_END_DECLS + +#endif /* __SHELL_GLOBAL_H__ */ diff --git a/src/shell-global.c b/src/shell-global.c index 575956c16..03ac1e9a5 100644 --- a/src/shell-global.c +++ b/src/shell-global.c @@ -785,102 +785,6 @@ grab_notify (GtkWidget *widget, gboolean was_grabbed, gpointer user_data) shell_global_set_stage_input_mode (global, global->input_mode); } -/** - * shell_global_create_vertical_gradient: - * @top: the color at the top - * @bottom: the color at the bottom - * - * Creates a vertical gradient actor. - * - * Return value: (transfer none): a #ClutterCairoTexture actor with the - * gradient. The texture actor is floating, hence (transfer none). - */ -ClutterCairoTexture * -shell_global_create_vertical_gradient (ClutterColor *top, - ClutterColor *bottom) -{ - ClutterCairoTexture *texture; - cairo_t *cr; - cairo_pattern_t *pattern; - - /* Draw the gradient on an 8x8 pixel texture. Because the gradient is drawn - * from the uppermost to the lowermost row, after stretching 1/16 of the - * texture height has the top color and 1/16 has the bottom color. The 8 - * pixel width is chosen for reasons related to graphics hardware internals. - */ - texture = CLUTTER_CAIRO_TEXTURE (clutter_cairo_texture_new (8, 8)); - cr = clutter_cairo_texture_create (texture); - - pattern = cairo_pattern_create_linear (0, 0, 0, 8); - cairo_pattern_add_color_stop_rgba (pattern, 0, - top->red / 255., - top->green / 255., - top->blue / 255., - top->alpha / 255.); - cairo_pattern_add_color_stop_rgba (pattern, 1, - bottom->red / 255., - bottom->green / 255., - bottom->blue / 255., - bottom->alpha / 255.); - - cairo_set_source (cr, pattern); - cairo_paint (cr); - - cairo_pattern_destroy (pattern); - cairo_destroy (cr); - - return texture; -} - -/** - * shell_global_create_horizontal_gradient: - * @left: the color on the left - * @right: the color on the right - * - * Creates a horizontal gradient actor. - * - * Return value: (transfer none): a #ClutterCairoTexture actor with the - * gradient. The texture actor is floating, hence (transfer none). - */ -ClutterCairoTexture * -shell_global_create_horizontal_gradient (ClutterColor *left, - ClutterColor *right) -{ - ClutterCairoTexture *texture; - cairo_t *cr; - cairo_pattern_t *pattern; - - /* Draw the gradient on an 8x1 pixel texture. Because the gradient is drawn - * from the left to the right column, after stretching 1/16 of the - * texture width has the left side color and 1/16 has the right side color. - * There is no reason to use the 8 pixel height that would be similar to the - * reason we are using the 8 pixel width for the vertical gradient, so we - * are just using the 1 pixel height instead. - */ - texture = CLUTTER_CAIRO_TEXTURE (clutter_cairo_texture_new (8, 1)); - cr = clutter_cairo_texture_create (texture); - - pattern = cairo_pattern_create_linear (0, 0, 8, 0); - cairo_pattern_add_color_stop_rgba (pattern, 0, - left->red / 255., - left->green / 255., - left->blue / 255., - left->alpha / 255.); - cairo_pattern_add_color_stop_rgba (pattern, 1, - right->red / 255., - right->green / 255., - right->blue / 255., - right->alpha / 255.); - - cairo_set_source (cr, pattern); - cairo_paint (cr); - - cairo_pattern_destroy (pattern); - cairo_destroy (cr); - - return texture; -} - /* * Updates the global->root_pixmap actor with the root window's pixmap or fails * with a warning. @@ -1104,84 +1008,3 @@ shell_global_get_max_word_width (ShellGlobal *global, ClutterActor *ref, const c g_strfreev (components); return (guint)max; } - -void -shell_global_clutter_cairo_texture_draw_clock (ClutterCairoTexture *texture, - int hour, - int minute) -{ - cairo_t *cr; - guint width, height; - double xc, yc, radius, hour_radius, minute_radius; - double angle; - - clutter_cairo_texture_get_surface_size (texture, &width, &height); - xc = (double)width / 2; - yc = (double)height / 2; - radius = (double)(MIN(width, height)) / 2 - 2; - minute_radius = radius - 3; - hour_radius = radius / 2; - - clutter_cairo_texture_clear (texture); - cr = clutter_cairo_texture_create (texture); - cairo_set_line_width (cr, 1.0); - - /* Outline */ - cairo_arc (cr, xc, yc, radius, 0.0, 2.0 * M_PI); - cairo_stroke (cr); - - /* Hour hand. (We add a fraction to @hour for the minutes, then - * convert to radians, and then subtract pi/2 because cairo's origin - * is at 3:00, not 12:00.) - */ - angle = ((hour + minute / 60.0) / 12.0) * 2.0 * M_PI - M_PI / 2.0; - cairo_move_to (cr, xc, yc); - cairo_line_to (cr, - xc + hour_radius * cos (angle), - yc + hour_radius * sin (angle)); - cairo_stroke (cr); - - /* Minute hand */ - angle = (minute / 60.0) * 2.0 * M_PI - M_PI / 2.0; - cairo_move_to (cr, xc, yc); - cairo_line_to (cr, - xc + minute_radius * cos (angle), - yc + minute_radius * sin (angle)); - cairo_stroke (cr); - - cairo_destroy (cr); -} - - -void -shell_global_clutter_cairo_texture_draw_glow (ClutterCairoTexture *texture, - double red, - double green, - double blue, - double alpha) -{ - cairo_t *cr; - guint width, height; - cairo_pattern_t *gradient; - - clutter_cairo_texture_get_surface_size (texture, &width, &height); - - clutter_cairo_texture_clear (texture); - cr = clutter_cairo_texture_create (texture); - - cairo_save (cr); - cairo_translate (cr, width / 2.0, height / 2.0); - cairo_scale (cr, width / 2.0, height / 2.0); - - gradient = cairo_pattern_create_radial (0.0, 0.0, 0.0, 0.0, 0.0, 1.0); - cairo_pattern_add_color_stop_rgba (gradient, 0.0, red, green, blue, alpha); - cairo_pattern_add_color_stop_rgba (gradient, 0.7, red, green, blue, alpha * 0.7); - cairo_pattern_add_color_stop_rgba (gradient, 1.0, red, green, blue, alpha * 0.3); - cairo_set_source (cr, gradient); - - cairo_arc (cr, 0.0, 0.0, 1.0, 0.0, 2.0 * M_PI); - cairo_fill (cr); - cairo_restore (cr); - cairo_pattern_destroy (gradient); - cairo_destroy (cr); -} diff --git a/src/shell-global.h b/src/shell-global.h index 52cd9efb4..5d2c28d35 100644 --- a/src/shell-global.h +++ b/src/shell-global.h @@ -71,28 +71,12 @@ void shell_global_ungrab_keyboard (ShellGlobal *global); void shell_global_reexec_self (ShellGlobal *global); -ClutterCairoTexture *shell_global_create_vertical_gradient (ClutterColor *top, - ClutterColor *bottom); - -ClutterCairoTexture *shell_global_create_horizontal_gradient (ClutterColor *left, - ClutterColor *right); - void shell_global_format_time_relative_pretty (ShellGlobal *global, guint delta, char **text, guint *update_time); ClutterActor *shell_global_create_root_pixmap_actor (ShellGlobal *global); guint shell_global_get_max_word_width (ShellGlobal *global, ClutterActor *ref, const char *text, const char *font); -void shell_global_clutter_cairo_texture_draw_clock (ClutterCairoTexture *texture, - int hour, - int minute); - -void shell_global_clutter_cairo_texture_draw_glow (ClutterCairoTexture *texture, - double red, - double blue, - double green, - double alpha); - G_END_DECLS #endif /* __SHELL_GLOBAL_H__ */