ui_style.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* $Id$
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2, or (at your option)
  5. any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., Inc., 51 Franklin Street, Fifth Floor, Boston,
  13. MA 02110-1301, USA.
  14. xfwm4 - (c) 2002-2011 Olivier Fourdan
  15. xfwm4-windowck-plugin - (c) 2013 Cedric leporcq
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #ifdef HAVE_MEMORY_H
  21. #include <memory.h>
  22. #endif
  23. #include <stdio.h>
  24. #ifdef HAVE_STRING_H
  25. #include <string.h>
  26. #endif
  27. #include <glib.h>
  28. #include <gdk/gdk.h>
  29. #include <gtk/gtk.h>
  30. #include <pango/pango-font.h>
  31. #include <libxfce4util/libxfce4util.h>
  32. #include "ui_style.h"
  33. char *states[] = {
  34. "normal", "active", "prelight", "selected", "insensitive", NULL
  35. };
  36. char *names[] = {
  37. "fg", "bg", "text", "base", "light", "dark", "mid", NULL
  38. };
  39. typedef enum {
  40. GTKSTYLE_FG = 0,
  41. GTKSTYLE_BG,
  42. GTKSTYLE_TEXT,
  43. GTKSTYLE_BASE,
  44. GTKSTYLE_LIGHT,
  45. GTKSTYLE_DARK,
  46. GTKSTYLE_MID,
  47. MIX_BG_TEXT,
  48. COLOR_NAMES
  49. } ColorNames;
  50. GdkColor shade (GdkColor color, float b)
  51. {
  52. color.red = color.red * b;
  53. color.green = color.green * b;
  54. color.blue = color.blue * b;
  55. return color;
  56. }
  57. GdkColor mix (GdkColor color2, GdkColor color1, float a)
  58. {
  59. GdkColor color;
  60. color.red = color1.red * a + color2.red * (1 - a);
  61. color.green = color1.green * a + color2.green * (1 - a);
  62. color.blue = color1.blue * a + color2.blue * (1 - a);
  63. return color;
  64. }
  65. static gint
  66. state_value (const gchar * s)
  67. {
  68. gint u = 0;
  69. while ((states[u]) && (strcmp (states[u], s)))
  70. {
  71. u++;
  72. }
  73. if (states[u])
  74. {
  75. return (u);
  76. }
  77. return (0);
  78. }
  79. static gint
  80. name_value (const gchar * s)
  81. {
  82. gint u = 0;
  83. while ((names[u]) && (strcmp (names[u], s)))
  84. {
  85. u++;
  86. }
  87. if (names[u])
  88. {
  89. return (u);
  90. }
  91. return (0);
  92. }
  93. GdkColor
  94. query_color (GtkWidget * win, GdkColor c)
  95. {
  96. GdkColor real_color;
  97. GdkColormap *cmap;
  98. cmap = gtk_widget_get_colormap (GTK_WIDGET (win));
  99. if (cmap && GDK_IS_COLORMAP (cmap))
  100. {
  101. gdk_colormap_query_color (cmap, c.pixel, &real_color);
  102. return real_color;
  103. }
  104. else
  105. {
  106. return c;
  107. }
  108. }
  109. static gchar *
  110. print_color (GtkWidget * win, GdkColor color)
  111. {
  112. gchar *s;
  113. s = g_new (gchar, 14);
  114. g_snprintf (s, 14, "#%04x%04x%04x", color.red, color.green,
  115. color.blue);
  116. return s;
  117. }
  118. static gchar *
  119. print_colors (GtkWidget * win, GdkColor * x, int n)
  120. {
  121. GdkColor color;
  122. color = query_color (win, x[n]);
  123. return print_color(win, color);
  124. }
  125. static gchar *
  126. print_rc_style (GtkWidget * win, const gchar * name, const gchar * state,
  127. GtkStyle * style)
  128. {
  129. gchar *s;
  130. gint n, m;
  131. g_return_val_if_fail (state != NULL, NULL);
  132. g_return_val_if_fail (name != NULL, NULL);
  133. n = state_value (state);
  134. m = name_value (name);
  135. switch (m)
  136. {
  137. case GTKSTYLE_FG:
  138. s = print_colors (win, style->fg, n);
  139. break;
  140. case GTKSTYLE_BG:
  141. s = print_colors (win, style->bg, n);
  142. break;
  143. case GTKSTYLE_TEXT:
  144. s = print_colors (win, style->text, n);
  145. break;
  146. case GTKSTYLE_BASE:
  147. s = print_colors (win, style->base, n);
  148. break;
  149. case GTKSTYLE_LIGHT:
  150. s = print_colors (win, style->light, n);
  151. break;
  152. case GTKSTYLE_DARK:
  153. s = print_colors (win, style->dark, n);
  154. break;
  155. default:
  156. case GTKSTYLE_MID:
  157. s = print_colors (win, style->mid, n);
  158. break;
  159. }
  160. return (s);
  161. }
  162. static GtkStyle *
  163. get_ui_style (GtkWidget * win)
  164. {
  165. GtkStyle *style;
  166. TRACE ("entering get_ui_style");
  167. style = gtk_rc_get_style (win);
  168. if (!style)
  169. {
  170. style = gtk_widget_get_style (win);
  171. }
  172. return (style);
  173. }
  174. gchar *
  175. get_ui_color (GtkWidget * win, const gchar * name, const gchar * state)
  176. {
  177. GtkStyle *style;
  178. gchar *s;
  179. TRACE ("entering get_ui_color");
  180. g_return_val_if_fail (win != NULL, NULL);
  181. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  182. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  183. style = get_ui_style (win);
  184. s = print_rc_style (win, name, state, style);
  185. TRACE ("%s[%s]=%s", name, state, s);
  186. return (s);
  187. }
  188. gchar *
  189. mix_bg_fg (GtkWidget * win, const gchar * state, float alpha, float beta)
  190. {
  191. GdkColor color, bgColor, fgColor;
  192. GtkStyle *style;
  193. gchar *s;
  194. gint n;
  195. TRACE ("entering mix_bg_fg_ui");
  196. g_return_val_if_fail (win != NULL, NULL);
  197. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  198. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  199. style = get_ui_style (win);
  200. n = state_value (state);
  201. bgColor = query_color (win, style->bg[n]);
  202. fgColor = query_color (win, style->fg[n]);
  203. color = shade (mix (bgColor, fgColor, alpha), beta);
  204. s = print_color (win, color);
  205. TRACE ("mix_bg_fg[%s]=%s", state, s);
  206. return (s);
  207. }
  208. static GdkGC *
  209. _getUIStyle_gc (const gchar * name, const gchar * state, GtkStyle * style)
  210. {
  211. GdkGC *gc;
  212. gint n, m;
  213. g_return_val_if_fail (state != NULL, NULL);
  214. g_return_val_if_fail (name != NULL, NULL);
  215. g_return_val_if_fail (style != NULL, NULL);
  216. g_return_val_if_fail (GTK_IS_STYLE(style), NULL);
  217. n = state_value (state);
  218. m = name_value (name);
  219. switch (m)
  220. {
  221. case GTKSTYLE_FG:
  222. gc = style->fg_gc[n];
  223. break;
  224. case GTKSTYLE_BG:
  225. gc = style->bg_gc[n];
  226. break;
  227. case GTKSTYLE_TEXT:
  228. gc = style->text_gc[n];
  229. break;
  230. case GTKSTYLE_BASE:
  231. gc = style->base_gc[n];
  232. break;
  233. case GTKSTYLE_LIGHT:
  234. gc = style->light_gc[n];
  235. break;
  236. case GTKSTYLE_DARK:
  237. gc = style->dark_gc[n];
  238. break;
  239. default:
  240. case GTKSTYLE_MID:
  241. gc = style->mid_gc[n];
  242. break;
  243. }
  244. return (gc);
  245. }
  246. GdkGC *
  247. getUIStyle_gc (GtkWidget * win, const gchar * name, const gchar * state)
  248. {
  249. GtkStyle *style;
  250. TRACE ("entering getUIStyle_gc");
  251. g_return_val_if_fail (win != NULL, NULL);
  252. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  253. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  254. style = gtk_rc_get_style (win);
  255. if (!style)
  256. {
  257. style = gtk_widget_get_style (win);
  258. }
  259. if (!style)
  260. {
  261. style = gtk_widget_get_default_style ();
  262. }
  263. return (_getUIStyle_gc (name, state, style));
  264. }
  265. PangoFontDescription *
  266. getUIPangoFontDesc (GtkWidget * win)
  267. {
  268. TRACE ("entering getUIPangoFontDesc");
  269. g_return_val_if_fail (win != NULL, NULL);
  270. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  271. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  272. return (win->style->font_desc);
  273. }
  274. PangoContext *
  275. getUIPangoContext (GtkWidget * win)
  276. {
  277. TRACE ("entering getUIPangoContext");
  278. g_return_val_if_fail (win != NULL, NULL);
  279. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  280. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  281. return (gtk_widget_get_pango_context (win));
  282. }