You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

282 lines
8.3 KiB

  1. /*
  2. * Copyright (C) 2006-2007 Jasper Huijsmans <jasper@xfce.org>
  3. * Copyright (C) 2008-2010 Nick Schermer <nick@xfce.org>
  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. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #ifdef HAVE_MATH_H
  23. #include <math.h>
  24. #endif
  25. #include <libxfce4util/libxfce4util.h>
  26. #include <gtk/gtk.h>
  27. #include <libxfce4panel/xfce-panel-macros.h>
  28. #include <libxfce4panel/xfce-panel-convenience.h>
  29. #include <libxfce4panel/libxfce4panel-alias.h>
  30. /**
  31. * SECTION: convenience
  32. * @title: Convenience Functions
  33. * @short_description: Special purpose widgets and utilities
  34. * @include: libxfce4panel/libxfce4panel.h
  35. *
  36. * This section describes a number of functions that were created
  37. * to help developers of Xfce Panel plugins.
  38. **/
  39. /**
  40. * xfce_panel_create_button:
  41. *
  42. * Create regular #GtkButton with a few properties set to be useful in the
  43. * Xfce panel: Flat (%GTK_RELIEF_NONE), no focus on click and minimal padding.
  44. *
  45. * Returns: newly created #GtkButton.
  46. **/
  47. GtkWidget *
  48. xfce_panel_create_button (void)
  49. {
  50. GtkWidget *button = gtk_button_new ();
  51. GTK_WIDGET_UNSET_FLAGS (button, GTK_CAN_DEFAULT | GTK_CAN_FOCUS);
  52. gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  53. gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
  54. gtk_widget_set_name (button, "xfce-panel-button");
  55. return button;
  56. }
  57. /**
  58. * xfce_panel_create_toggle_button:
  59. *
  60. * Create regular #GtkToggleButton with a few properties set to be useful in
  61. * Xfce panel: Flat (%GTK_RELIEF_NONE), no focus on click and minimal padding.
  62. *
  63. * Returns: newly created #GtkToggleButton.
  64. **/
  65. GtkWidget *
  66. xfce_panel_create_toggle_button (void)
  67. {
  68. GtkWidget *button = gtk_toggle_button_new ();
  69. GTK_WIDGET_UNSET_FLAGS (button, GTK_CAN_DEFAULT | GTK_CAN_FOCUS);
  70. gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  71. gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
  72. gtk_widget_set_name (button, "xfce-panel-toggle-button");
  73. return button;
  74. }
  75. /**
  76. * xfce_panel_get_channel_name:
  77. *
  78. * Function for the name of the Xfconf channel used by the panel. By default
  79. * this returns "xfce4-panel", but you can override this value with the
  80. * environment variable XFCE_PANEL_CHANNEL_NAME.
  81. *
  82. * Returns: name of the Xfconf channel
  83. *
  84. * See also: XFCE_PANEL_CHANNEL_NAME,
  85. * xfce_panel_plugin_xfconf_channel_new and
  86. * xfce_panel_plugin_get_property_base
  87. *
  88. * Since: 4.8
  89. **/
  90. const gchar *
  91. xfce_panel_get_channel_name (void)
  92. {
  93. static const gchar *name = NULL;
  94. if (G_UNLIKELY (name == NULL))
  95. {
  96. name = g_getenv ("XFCE_PANEL_CHANNEL_NAME");
  97. if (G_LIKELY (name == NULL))
  98. name = "xfce4-panel";
  99. }
  100. return name;
  101. }
  102. /**
  103. * xfce_panel_pixbuf_from_source_at_size:
  104. * @source: string that contains the location of an icon
  105. * @icon_theme: icon theme or %NULL to use the default icon theme
  106. * @dest_width: the maximum returned width of the GdkPixbuf
  107. * @dest_height: the maximum returned height of the GdkPixbuf
  108. *
  109. * Try to load a pixbuf from a source string. The source could be
  110. * an abolute path, an icon name or a filename that points to a
  111. * file in the pixmaps directory.
  112. *
  113. * This function is particularly usefull for loading names from
  114. * the Icon key of desktop files.
  115. *
  116. * The pixbuf is never bigger than @dest_width and @dest_height.
  117. * If it is when loaded from the disk, the pixbuf is scaled
  118. * preserving the aspect ratio.
  119. *
  120. * Returns: a GdkPixbuf or %NULL if nothing was found. The value should
  121. * be released with g_object_unref when no longer used.
  122. *
  123. * See also: XfcePanelImage
  124. *
  125. * Since: 4.10
  126. **/
  127. GdkPixbuf *
  128. xfce_panel_pixbuf_from_source_at_size (const gchar *source,
  129. GtkIconTheme *icon_theme,
  130. gint dest_width,
  131. gint dest_height)
  132. {
  133. GdkPixbuf *pixbuf = NULL;
  134. gchar *p;
  135. gchar *name;
  136. gchar *filename;
  137. gint src_w, src_h;
  138. gdouble ratio;
  139. GdkPixbuf *dest;
  140. GError *error = NULL;
  141. gint size = MIN (dest_width, dest_height);
  142. g_return_val_if_fail (source != NULL, NULL);
  143. g_return_val_if_fail (icon_theme == NULL || GTK_IS_ICON_THEME (icon_theme), NULL);
  144. g_return_val_if_fail (dest_width > 0, NULL);
  145. g_return_val_if_fail (dest_height > 0, NULL);
  146. if (G_UNLIKELY (g_path_is_absolute (source)))
  147. {
  148. pixbuf = gdk_pixbuf_new_from_file (source, &error);
  149. if (G_UNLIKELY (pixbuf == NULL))
  150. {
  151. g_message ("Failed to load image \"%s\": %s",
  152. source, error->message);
  153. g_error_free (error);
  154. }
  155. }
  156. else
  157. {
  158. if (G_UNLIKELY (icon_theme == NULL))
  159. icon_theme = gtk_icon_theme_get_default ();
  160. /* try to load from the icon theme */
  161. pixbuf = gtk_icon_theme_load_icon (icon_theme, source, size, 0, NULL);
  162. if (G_UNLIKELY (pixbuf == NULL))
  163. {
  164. /* try to lookup names like application.png in the theme */
  165. p = strrchr (source, '.');
  166. if (p != NULL)
  167. {
  168. name = g_strndup (source, p - source);
  169. pixbuf = gtk_icon_theme_load_icon (icon_theme, name, size, 0, NULL);
  170. g_free (name);
  171. }
  172. /* maybe they point to a file in the pixbufs folder */
  173. if (G_UNLIKELY (pixbuf == NULL))
  174. {
  175. filename = g_build_filename ("pixmaps", source, NULL);
  176. name = xfce_resource_lookup (XFCE_RESOURCE_DATA, filename);
  177. g_free (filename);
  178. if (name != NULL)
  179. {
  180. pixbuf = gdk_pixbuf_new_from_file (name, NULL);
  181. g_free (name);
  182. }
  183. }
  184. }
  185. }
  186. if (G_UNLIKELY (pixbuf == NULL))
  187. {
  188. if (G_UNLIKELY (icon_theme == NULL))
  189. icon_theme = gtk_icon_theme_get_default ();
  190. /* bit ugly as a fallback, but in most cases better then no icon */
  191. pixbuf = gtk_icon_theme_load_icon (icon_theme, GTK_STOCK_MISSING_IMAGE,
  192. size, GTK_ICON_LOOKUP_USE_BUILTIN, NULL);
  193. }
  194. /* scale the pixbug if required */
  195. if (G_LIKELY (pixbuf != NULL))
  196. {
  197. src_w = gdk_pixbuf_get_width (pixbuf);
  198. src_h = gdk_pixbuf_get_height (pixbuf);
  199. if (src_w > dest_width || src_h > dest_height)
  200. {
  201. /* calculate the new dimensions */
  202. ratio = MIN ((gdouble) dest_width / (gdouble) src_w,
  203. (gdouble) dest_height / (gdouble) src_h);
  204. dest_width = rint (src_w * ratio);
  205. dest_height = rint (src_h * ratio);
  206. dest = gdk_pixbuf_scale_simple (pixbuf,
  207. MAX (dest_width, 1),
  208. MAX (dest_height, 1),
  209. GDK_INTERP_BILINEAR);
  210. g_object_unref (G_OBJECT (pixbuf));
  211. pixbuf = dest;
  212. }
  213. }
  214. return pixbuf;
  215. }
  216. /**
  217. * xfce_panel_pixbuf_from_source:
  218. * @source: string that contains the location of an icon
  219. * @icon_theme: icon theme or %NULL to use the default icon theme
  220. * @size: size the icon that should be loaded
  221. *
  222. * See xfce_panel_pixbuf_from_source_at_size
  223. *
  224. * Returns: a GdkPixbuf or %NULL if nothing was found. The value should
  225. * be released with g_object_unref when no longer used.
  226. *
  227. * See also: XfcePanelImage
  228. *
  229. * Since: 4.8
  230. **/
  231. GdkPixbuf *
  232. xfce_panel_pixbuf_from_source (const gchar *source,
  233. GtkIconTheme *icon_theme,
  234. gint size)
  235. {
  236. return xfce_panel_pixbuf_from_source_at_size (source, icon_theme, size, size);
  237. }
  238. #define __XFCE_PANEL_CONVENIENCE_C__
  239. #include <libxfce4panel/libxfce4panel-aliasdef.c>