ui_style.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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", "mix_bg_text", 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 mix(GdkColor color2, GdkColor color1, float a) {
  51. GdkColor color;
  52. color.red = color1.red * a + color2.red * (1 - a);
  53. color.green = color1.green * a + color2.green * (1 - a);
  54. color.blue = color1.blue * a + color2.blue * (1 - a);
  55. return color;
  56. }
  57. static gint
  58. state_value (const gchar * s)
  59. {
  60. gint u = 0;
  61. while ((states[u]) && (strcmp (states[u], s)))
  62. {
  63. u++;
  64. }
  65. if (states[u])
  66. {
  67. return (u);
  68. }
  69. return (0);
  70. }
  71. static gint
  72. name_value (const gchar * s)
  73. {
  74. gint u = 0;
  75. while ((names[u]) && (strcmp (names[u], s)))
  76. {
  77. u++;
  78. }
  79. if (names[u])
  80. {
  81. return (u);
  82. }
  83. return (0);
  84. }
  85. GdkColor
  86. queryColor (GtkWidget * win, GdkColor c)
  87. {
  88. GdkColor real_color;
  89. GdkColormap *cmap;
  90. cmap = gtk_widget_get_colormap (GTK_WIDGET (win));
  91. if (cmap && GDK_IS_COLORMAP (cmap))
  92. {
  93. gdk_colormap_query_color (cmap, c.pixel, &real_color);
  94. return real_color;
  95. }
  96. else
  97. {
  98. return c;
  99. }
  100. }
  101. static gchar *
  102. print_color (GtkWidget * win, GdkColor color)
  103. {
  104. gchar *s;
  105. s = g_new (gchar, 14);
  106. g_snprintf (s, 14, "#%04x%04x%04x", color.red, color.green,
  107. color.blue);
  108. return s;
  109. }
  110. static gchar *
  111. print_colors (GtkWidget * win, GdkColor * x, int n)
  112. {
  113. GdkColor color;
  114. color = queryColor (win, x[n]);
  115. return print_color(win, color);
  116. }
  117. static gchar *
  118. print_rc_style (GtkWidget * win, const gchar * name, const gchar * state,
  119. GtkStyle * style)
  120. {
  121. GdkColor color, bgColor, textColor;
  122. gchar *s;
  123. gint n, m;
  124. g_return_val_if_fail (state != NULL, NULL);
  125. g_return_val_if_fail (name != NULL, NULL);
  126. n = state_value (state);
  127. m = name_value (name);
  128. switch (m)
  129. {
  130. case GTKSTYLE_FG:
  131. s = print_colors (win, style->fg, n);
  132. break;
  133. case GTKSTYLE_BG:
  134. s = print_colors (win, style->bg, n);
  135. break;
  136. case GTKSTYLE_TEXT:
  137. s = print_colors (win, style->text, n);
  138. break;
  139. case GTKSTYLE_BASE:
  140. s = print_colors (win, style->base, n);
  141. break;
  142. case GTKSTYLE_LIGHT:
  143. s = print_colors (win, style->light, n);
  144. break;
  145. case GTKSTYLE_DARK:
  146. s = print_colors (win, style->dark, n);
  147. break;
  148. default:
  149. case GTKSTYLE_MID:
  150. s = print_colors (win, style->mid, n);
  151. break;
  152. case MIX_BG_TEXT:
  153. bgColor = queryColor (win, style->bg[n]);
  154. textColor = queryColor (win, style->text[n]);
  155. color = mix(bgColor, textColor, UNFOCUSED_TEXT_ALPHA);
  156. s = print_color (win, color);
  157. break;
  158. }
  159. return (s);
  160. }
  161. gchar *
  162. getUIStyle (GtkWidget * win, const gchar * name, const gchar * state)
  163. {
  164. GtkStyle *style;
  165. gchar *s;
  166. TRACE ("entering getUIStyle");
  167. g_return_val_if_fail (win != NULL, NULL);
  168. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  169. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  170. style = gtk_rc_get_style (win);
  171. if (!style)
  172. {
  173. style = gtk_widget_get_style (win);
  174. }
  175. s = print_rc_style (win, name, state, style);
  176. TRACE ("%s[%s]=%s", name, state, s);
  177. return (s);
  178. }
  179. static GdkGC *
  180. _getUIStyle_gc (const gchar * name, const gchar * state, GtkStyle * style)
  181. {
  182. GdkGC *gc;
  183. gint n, m;
  184. g_return_val_if_fail (state != NULL, NULL);
  185. g_return_val_if_fail (name != NULL, NULL);
  186. g_return_val_if_fail (style != NULL, NULL);
  187. g_return_val_if_fail (GTK_IS_STYLE(style), NULL);
  188. n = state_value (state);
  189. m = name_value (name);
  190. switch (m)
  191. {
  192. case GTKSTYLE_FG:
  193. gc = style->fg_gc[n];
  194. break;
  195. case GTKSTYLE_BG:
  196. gc = style->bg_gc[n];
  197. break;
  198. case GTKSTYLE_TEXT:
  199. gc = style->text_gc[n];
  200. break;
  201. case GTKSTYLE_BASE:
  202. gc = style->base_gc[n];
  203. break;
  204. case GTKSTYLE_LIGHT:
  205. gc = style->light_gc[n];
  206. break;
  207. case GTKSTYLE_DARK:
  208. gc = style->dark_gc[n];
  209. break;
  210. default:
  211. case GTKSTYLE_MID:
  212. gc = style->mid_gc[n];
  213. break;
  214. }
  215. return (gc);
  216. }
  217. GdkGC *
  218. getUIStyle_gc (GtkWidget * win, const gchar * name, const gchar * state)
  219. {
  220. GtkStyle *style;
  221. TRACE ("entering getUIStyle_gc");
  222. g_return_val_if_fail (win != NULL, NULL);
  223. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  224. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  225. style = gtk_rc_get_style (win);
  226. if (!style)
  227. {
  228. style = gtk_widget_get_style (win);
  229. }
  230. if (!style)
  231. {
  232. style = gtk_widget_get_default_style ();
  233. }
  234. return (_getUIStyle_gc (name, state, style));
  235. }
  236. PangoFontDescription *
  237. getUIPangoFontDesc (GtkWidget * win)
  238. {
  239. TRACE ("entering getUIPangoFontDesc");
  240. g_return_val_if_fail (win != NULL, NULL);
  241. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  242. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  243. return (win->style->font_desc);
  244. }
  245. PangoContext *
  246. getUIPangoContext (GtkWidget * win)
  247. {
  248. TRACE ("entering getUIPangoContext");
  249. g_return_val_if_fail (win != NULL, NULL);
  250. g_return_val_if_fail (GTK_IS_WIDGET (win), NULL);
  251. g_return_val_if_fail (GTK_WIDGET_REALIZED (win), NULL);
  252. return (gtk_widget_get_pango_context (win));
  253. }