| 1 |
/* Adwaita - a GTK+ engine |
| 2 |
* |
| 3 |
* Copyright (C) 2012 Red Hat, Inc. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library 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 GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* Authors: Cosimo Cecchi <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <gmodule.h> |
| 23 |
#include <glib.h> |
| 24 |
#include <gtk/gtk.h> |
| 25 |
|
| 26 |
#ifdef GDK_WINDOWING_X11 |
| 27 |
#include <gdk/gdkx.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
/*************************************** |
| 31 |
* Register & Initialize Drawing Style * |
| 32 |
***************************************/ |
| 33 |
#define ADWAITA_TYPE_STYLE (adwaita_style_get_type ()) |
| 34 |
#define ADWAITA_STYLE(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), ADWAITA_TYPE_STYLE, AdwaitaStyle)) |
| 35 |
#define ADWAITA_STYLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ADWAITA_TYPE_STYLE, AdwaitaStyleClass)) |
| 36 |
#define ADWAITA_IS_STYLE(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), ADWAITA_TYPE_STYLE)) |
| 37 |
#define ADWAITA_IS_STYLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ADWAITA_TYPE_STYLE)) |
| 38 |
#define ADWAITA_STYLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ADWAITA_TYPE_STYLE, AdwaitaStyleClass)) |
| 39 |
|
| 40 |
typedef struct |
| 41 |
{ |
| 42 |
GtkStyle parent_instance; |
| 43 |
} AdwaitaStyle; |
| 44 |
|
| 45 |
typedef struct |
| 46 |
{ |
| 47 |
GtkStyleClass parent_class; |
| 48 |
} AdwaitaStyleClass; |
| 49 |
|
| 50 |
G_DEFINE_DYNAMIC_TYPE (AdwaitaStyle, adwaita_style, GTK_TYPE_STYLE) |
| 51 |
|
| 52 |
static void |
| 53 |
do_toplevel_hack (GtkWidget *widget, |
| 54 |
const gchar *widget_name) |
| 55 |
{ |
| 56 |
gboolean tried_hack; |
| 57 |
|
| 58 |
tried_hack = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "adwaita-toplevel-hack")); |
| 59 |
|
| 60 |
if (!tried_hack) |
| 61 |
{ |
| 62 |
g_object_set_data (G_OBJECT (widget), |
| 63 |
"adwaita-toplevel-hack", GINT_TO_POINTER (1)); |
| 64 |
gtk_widget_set_name (widget, widget_name); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
static gboolean |
| 69 |
wm_is_fallback (void) |
| 70 |
{ |
| 71 |
#ifdef GDK_WINDOWING_X11 |
| 72 |
const gchar *name; |
| 73 |
name = gdk_x11_screen_get_window_manager_name (gdk_screen_get_default ()); |
| 74 |
return g_strcmp0 (name, "GNOME Shell") != 0; |
| 75 |
#else |
| 76 |
return TRUE; |
| 77 |
#endif |
| 78 |
} |
| 79 |
|
| 80 |
static cairo_t * |
| 81 |
drawable_to_cairo (GdkDrawable *window, |
| 82 |
GdkRectangle *area) |
| 83 |
{ |
| 84 |
cairo_t *cr; |
| 85 |
|
| 86 |
g_return_val_if_fail (window != NULL, NULL); |
| 87 |
|
| 88 |
cr = (cairo_t*) gdk_cairo_create (window); |
| 89 |
cairo_set_line_width (cr, 1.0); |
| 90 |
cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE); |
| 91 |
cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER); |
| 92 |
|
| 93 |
if (area) |
| 94 |
{ |
| 95 |
cairo_rectangle (cr, area->x, area->y, area->width, area->height); |
| 96 |
cairo_clip_preserve (cr); |
| 97 |
cairo_new_path (cr); |
| 98 |
} |
| 99 |
|
| 100 |
return cr; |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
adwaita_draw_box (GtkStyle * style, |
| 105 |
GdkWindow * window, |
| 106 |
GtkStateType state_type, |
| 107 |
GtkShadowType shadow_type, |
| 108 |
GdkRectangle * area, |
| 109 |
GtkWidget * widget, |
| 110 |
const gchar * detail, |
| 111 |
gint x, |
| 112 |
gint y, |
| 113 |
gint width, |
| 114 |
gint height) |
| 115 |
{ |
| 116 |
if (GTK_IS_MENU (widget) && |
| 117 |
g_strcmp0 (detail, "menu") == 0 && |
| 118 |
wm_is_fallback ()) |
| 119 |
{ |
| 120 |
cairo_t *cr = drawable_to_cairo (window, area); |
| 121 |
cairo_set_source_rgb (cr, 0.34, 0.34, 0.33); |
| 122 |
cairo_rectangle (cr, x, y, width, height); |
| 123 |
cairo_stroke (cr); |
| 124 |
|
| 125 |
cairo_destroy (cr); |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
GTK_STYLE_CLASS (adwaita_style_parent_class)->draw_box (style, window, state_type, shadow_type, |
| 130 |
area, widget, detail, |
| 131 |
x, y, width, height); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
adwaita_draw_flat_box (GtkStyle *style, |
| 137 |
GdkWindow *window, |
| 138 |
GtkStateType state_type, |
| 139 |
GtkShadowType shadow_type, |
| 140 |
GdkRectangle *area, |
| 141 |
GtkWidget *widget, |
| 142 |
const gchar *detail, |
| 143 |
gint x, |
| 144 |
gint y, |
| 145 |
gint width, |
| 146 |
gint height) |
| 147 |
{ |
| 148 |
const gchar *app_name; |
| 149 |
|
| 150 |
GTK_STYLE_CLASS (adwaita_style_parent_class)->draw_flat_box (style, window, state_type, shadow_type, |
| 151 |
area, widget, detail, |
| 152 |
x, y, width, height); |
| 153 |
|
| 154 |
/* HACK: this is totally awful, but I don't see a better way to "tag" the OO.o hierarchy */ |
| 155 |
if (!GTK_IS_WINDOW (widget) || |
| 156 |
(gtk_window_get_window_type (GTK_WINDOW (widget)) != GTK_WINDOW_TOPLEVEL)) |
| 157 |
return; |
| 158 |
|
| 159 |
app_name = g_get_application_name (); |
| 160 |
if (g_str_has_prefix (app_name, "OpenOffice.org")) |
| 161 |
do_toplevel_hack (widget, "openoffice-toplevel"); |
| 162 |
else if (g_str_has_prefix (app_name, "LibreOffice")) |
| 163 |
do_toplevel_hack (widget, "libreoffice-toplevel"); |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
adwaita_style_init (AdwaitaStyle *style) |
| 168 |
{ |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
adwaita_style_class_init (AdwaitaStyleClass * klass) |
| 173 |
{ |
| 174 |
GtkStyleClass *style_class = GTK_STYLE_CLASS (klass); |
| 175 |
|
| 176 |
style_class->draw_box = adwaita_draw_box; |
| 177 |
style_class->draw_flat_box = adwaita_draw_flat_box; |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
adwaita_style_class_finalize (AdwaitaStyleClass * klass) |
| 182 |
{ |
| 183 |
} |
| 184 |
|
| 185 |
/********************************** |
| 186 |
* Register & Initialize RC Style * |
| 187 |
**********************************/ |
| 188 |
#define ADWAITA_TYPE_RC_STYLE (adwaita_rc_style_get_type ()) |
| 189 |
#define ADWAITA_RC_STYLE(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), ADWAITA_TYPE_RC_STYLE, AdwaitaRcStyle)) |
| 190 |
#define ADWAITA_RC_STYLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ADWAITA_TYPE_RC_STYLE, AdwaitaRcStyleClass)) |
| 191 |
#define ADWAITA_IS_RC_STYLE(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), ADWAITA_TYPE_RC_STYLE)) |
| 192 |
#define ADWAITA_IS_RC_STYLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ADWAITA_TYPE_RC_STYLE)) |
| 193 |
#define ADWAITA_RC_STYLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ADWAITA_TYPE_RC_STYLE, AdwaitaRcStyleClass)) |
| 194 |
|
| 195 |
typedef struct |
| 196 |
{ |
| 197 |
GtkRcStyle parent_instance; |
| 198 |
} AdwaitaRcStyle; |
| 199 |
|
| 200 |
typedef struct |
| 201 |
{ |
| 202 |
GtkRcStyleClass parent_class; |
| 203 |
} AdwaitaRcStyleClass; |
| 204 |
|
| 205 |
G_DEFINE_DYNAMIC_TYPE (AdwaitaRcStyle, adwaita_rc_style, GTK_TYPE_RC_STYLE) |
| 206 |
|
| 207 |
static GtkStyle * |
| 208 |
adwaita_rc_style_create_style (GtkRcStyle *rc_style) |
| 209 |
{ |
| 210 |
return g_object_new (ADWAITA_TYPE_STYLE, NULL); |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
adwaita_rc_style_init (AdwaitaRcStyle *rc_style) |
| 215 |
{ |
| 216 |
} |
| 217 |
|
| 218 |
static void |
| 219 |
adwaita_rc_style_class_init (AdwaitaRcStyleClass * klass) |
| 220 |
{ |
| 221 |
GtkRcStyleClass *rc_class = GTK_RC_STYLE_CLASS (klass); |
| 222 |
|
| 223 |
rc_class->create_style = adwaita_rc_style_create_style; |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
adwaita_rc_style_class_finalize (AdwaitaRcStyleClass * klass) |
| 228 |
{ |
| 229 |
} |
| 230 |
|
| 231 |
/**************** |
| 232 |
* Engine Hooks * |
| 233 |
****************/ |
| 234 |
void |
| 235 |
theme_init (GTypeModule * module) |
| 236 |
{ |
| 237 |
adwaita_rc_style_register_type (module); |
| 238 |
adwaita_style_register_type (module); |
| 239 |
} |
| 240 |
|
| 241 |
void |
| 242 |
theme_exit (void) |
| 243 |
{ |
| 244 |
} |
| 245 |
|
| 246 |
GtkRcStyle * |
| 247 |
theme_create_rc_style (void) |
| 248 |
{ |
| 249 |
return g_object_new (ADWAITA_TYPE_RC_STYLE, NULL); |
| 250 |
} |