windowck.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* $Id$
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Copyright (C) 2013 Alessio Piccoli <alepic@geckoblu.net>
  18. * Cedric Leporcq <cedl38@gmail.com>
  19. *
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifdef HAVE_STRING_H
  25. #include <string.h>
  26. #endif
  27. #include <gtk/gtk.h>
  28. #include <libxfce4util/libxfce4util.h>
  29. #include <libxfce4panel/libxfce4panel.h>
  30. #include <common/wck-plugin.h>
  31. #include "windowck.h"
  32. #include "windowck-dialogs.h"
  33. #include "windowck-title.h"
  34. /* default settings */
  35. #define DEFAULT_ONLY_MAXIMIZED TRUE
  36. #define DEFAULT_SHOW_ON_DESKTOP FALSE
  37. #define DEFAULT_HIDE_TITLE FALSE
  38. #define DEFAULT_SHOW_TOOLTIPS TRUE
  39. #define DEFAULT_SHOW_ICON TRUE
  40. #define DEFAULT_ICON_ON_RIGHT FALSE
  41. #define DEFAULT_SIZE_MODE FIXE
  42. #define DEFAULT_TITLE_SIZE 80
  43. #define DEFAULT_TITLE_ALIGNMENT CENTER
  44. #define DEFAULT_TITLE_PADDING 3
  45. #define DEFAULT_CUSTOM_FONT FALSE
  46. #define DEFAULT_TITLE_FONT "sans 10"
  47. /* prototypes */
  48. static void windowck_construct(XfcePanelPlugin *plugin);
  49. void windowck_save(XfcePanelPlugin *plugin, WindowckPlugin *wckp) {
  50. XfceRc *rc;
  51. gchar *file;
  52. /* get the config file location */
  53. file = xfce_panel_plugin_save_location(plugin, TRUE);
  54. if (G_UNLIKELY (file == NULL)) {
  55. DBG("Failed to open config file");
  56. return;
  57. }
  58. /* open the config file, read/write */
  59. rc = xfce_rc_simple_open(file, FALSE);
  60. g_free(file);
  61. if (G_LIKELY (rc != NULL)) {
  62. /* save the settings */
  63. DBG(".");
  64. xfce_rc_write_bool_entry(rc, "only_maximized", wckp->prefs->only_maximized);
  65. xfce_rc_write_bool_entry(rc, "show_on_desktop", wckp->prefs->show_on_desktop);
  66. xfce_rc_write_bool_entry(rc, "show_icon", wckp->prefs->show_icon);
  67. xfce_rc_write_bool_entry(rc, "icon_on_right", wckp->prefs->icon_on_right);
  68. xfce_rc_write_bool_entry(rc, "hide_title", wckp->prefs->hide_title);
  69. xfce_rc_write_bool_entry(rc, "show_tooltips", wckp->prefs->show_tooltips);
  70. xfce_rc_write_int_entry(rc, "size_mode", wckp->prefs->size_mode);
  71. xfce_rc_write_int_entry(rc, "title_size", wckp->prefs->title_size);
  72. xfce_rc_write_bool_entry(rc, "custom_font", wckp->prefs->custom_font);
  73. if (wckp->prefs->title_font)
  74. xfce_rc_write_entry(rc, "title_font", wckp->prefs->title_font);
  75. xfce_rc_write_int_entry(rc, "title_alignment", wckp->prefs->title_alignment);
  76. xfce_rc_write_int_entry(rc, "title_padding", wckp->prefs->title_padding);
  77. /* close the rc file */
  78. xfce_rc_close(rc);
  79. }
  80. }
  81. static void windowck_read(WindowckPlugin *wckp) {
  82. XfceRc *rc;
  83. gchar *file;
  84. const gchar *title_font;
  85. /* allocate memory for the preferences structure */
  86. wckp->prefs = g_slice_new0(WCKPreferences);
  87. /* get the plugin config file location */
  88. file = xfce_panel_plugin_save_location(wckp->plugin, TRUE);
  89. if (G_LIKELY (file != NULL)) {
  90. /* open the config file, readonly */
  91. rc = xfce_rc_simple_open(file, TRUE);
  92. /* cleanup */
  93. g_free(file);
  94. if (G_LIKELY (rc != NULL)) {
  95. /* read the settings */
  96. wckp->prefs->only_maximized = xfce_rc_read_bool_entry(rc, "only_maximized", DEFAULT_ONLY_MAXIMIZED);
  97. wckp->prefs->show_on_desktop = xfce_rc_read_bool_entry(rc, "show_on_desktop", DEFAULT_SHOW_ON_DESKTOP);
  98. wckp->prefs->show_icon = xfce_rc_read_bool_entry(rc, "show_icon", DEFAULT_SHOW_ICON);
  99. wckp->prefs->icon_on_right = xfce_rc_read_bool_entry(rc, "icon_on_right", DEFAULT_ICON_ON_RIGHT);
  100. wckp->prefs->hide_title = xfce_rc_read_bool_entry(rc, "hide_title", DEFAULT_HIDE_TITLE);
  101. wckp->prefs->show_tooltips = xfce_rc_read_bool_entry(rc, "show_tooltips", DEFAULT_SHOW_TOOLTIPS);
  102. wckp->prefs->size_mode = xfce_rc_read_int_entry (rc, "size_mode", DEFAULT_SIZE_MODE);
  103. wckp->prefs->title_size = xfce_rc_read_int_entry(rc, "title_size", DEFAULT_TITLE_SIZE);
  104. wckp->prefs->custom_font = xfce_rc_read_bool_entry(rc, "custom_font", DEFAULT_CUSTOM_FONT);
  105. title_font = xfce_rc_read_entry(rc, "title_font", DEFAULT_TITLE_FONT);
  106. wckp->prefs->title_font = g_strdup(title_font);
  107. wckp->prefs->title_alignment = xfce_rc_read_int_entry(rc, "title_alignment", DEFAULT_TITLE_ALIGNMENT);
  108. wckp->prefs->title_padding = xfce_rc_read_int_entry(rc, "title_padding", DEFAULT_TITLE_PADDING);
  109. /* cleanup */
  110. xfce_rc_close(rc);
  111. /* leave the function, everything went well */
  112. return;
  113. }
  114. }
  115. /* something went wrong, apply default values */
  116. DBG("Applying default settings");
  117. wckp->prefs->only_maximized = DEFAULT_ONLY_MAXIMIZED;
  118. wckp->prefs->show_on_desktop = DEFAULT_SHOW_ON_DESKTOP;
  119. wckp->prefs->show_icon = DEFAULT_SHOW_ICON;
  120. wckp->prefs->icon_on_right = DEFAULT_ICON_ON_RIGHT;
  121. wckp->prefs->hide_title = DEFAULT_HIDE_TITLE;
  122. wckp->prefs->show_tooltips = DEFAULT_SHOW_TOOLTIPS;
  123. wckp->prefs->size_mode = DEFAULT_SIZE_MODE;
  124. wckp->prefs->title_size = DEFAULT_TITLE_SIZE;
  125. wckp->prefs->custom_font = DEFAULT_CUSTOM_FONT;
  126. wckp->prefs->title_font = DEFAULT_TITLE_FONT;
  127. wckp->prefs->title_alignment = DEFAULT_TITLE_ALIGNMENT;
  128. wckp->prefs->title_padding = DEFAULT_TITLE_PADDING;
  129. }
  130. static void createIcon (WindowckPlugin *wckp) {
  131. wckp->icon = g_slice_new0 (WindowIcon);
  132. wckp->icon->eventbox = GTK_EVENT_BOX (gtk_event_box_new());
  133. wckp->icon->image = gtk_image_new();
  134. gtk_widget_set_can_focus (GTK_WIDGET(wckp->icon->eventbox), TRUE);
  135. gtk_container_add (GTK_CONTAINER (wckp->icon->eventbox), wckp->icon->image);
  136. gtk_event_box_set_visible_window (wckp->icon->eventbox, FALSE);
  137. gtk_box_pack_start (GTK_BOX (wckp->hvbox), GTK_WIDGET(wckp->icon->eventbox), FALSE, FALSE, 0);
  138. gtk_widget_show_all(GTK_WIDGET(wckp->icon->eventbox));
  139. }
  140. static WindowckPlugin * windowck_new(XfcePanelPlugin *plugin) {
  141. WindowckPlugin *wckp;
  142. GtkOrientation orientation;
  143. GtkWidget *label;
  144. /* allocate memory for the plugin structure */
  145. wckp = g_slice_new0 (WindowckPlugin);
  146. /* pointer to plugin */
  147. wckp->plugin = plugin;
  148. /* read the user settings */
  149. windowck_read(wckp);
  150. /* get the current orientation */
  151. orientation = xfce_panel_plugin_get_orientation(plugin);
  152. /* not needed for shrink mode */
  153. if (!wckp->prefs->size_mode == SHRINK)
  154. xfce_panel_plugin_set_shrink (plugin, TRUE);
  155. /* create some panel widgets */
  156. wckp->ebox = gtk_event_box_new();
  157. gtk_event_box_set_visible_window(GTK_EVENT_BOX(wckp->ebox), FALSE);
  158. gtk_widget_set_name(wckp->ebox, "XfceWindowckPlugin");
  159. wckp->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
  160. wckp->hvbox = xfce_hvbox_new(orientation, FALSE, 2);
  161. /* some wckp widgets */
  162. label = gtk_label_new("");
  163. wckp->title = GTK_LABEL (label);
  164. createIcon (wckp);
  165. gtk_box_pack_start (GTK_BOX(wckp->hvbox), label, TRUE, TRUE, 0);
  166. if (wckp->prefs->icon_on_right) {
  167. gtk_box_reorder_child (GTK_BOX (wckp->hvbox), GTK_WIDGET(wckp->icon->eventbox), 1);
  168. }
  169. gtk_container_add(GTK_CONTAINER(wckp->alignment), GTK_WIDGET(wckp->hvbox));
  170. gtk_container_add(GTK_CONTAINER(wckp->ebox), wckp->alignment);
  171. /* show widgets */
  172. gtk_widget_show(wckp->ebox);
  173. gtk_widget_show(wckp->alignment);
  174. gtk_widget_show(wckp->hvbox);
  175. gtk_widget_show(label);
  176. return wckp;
  177. }
  178. static void windowck_free(XfcePanelPlugin *plugin, WindowckPlugin *wckp) {
  179. GtkWidget *dialog;
  180. /* check if the dialog is still open. if so, destroy it */
  181. dialog = g_object_get_data(G_OBJECT (plugin), "dialog");
  182. if (G_UNLIKELY (dialog != NULL))
  183. gtk_widget_destroy(dialog);
  184. /* destroy the panel widgets */
  185. gtk_widget_destroy(wckp->hvbox);
  186. /* free the plugin structure */
  187. g_slice_free(WindowIcon, wckp->icon);
  188. g_slice_free(WckUtils, wckp->win);
  189. g_slice_free(WCKPreferences, wckp->prefs);
  190. g_slice_free(WindowckPlugin, wckp);
  191. }
  192. static void windowck_orientation_changed(XfcePanelPlugin *plugin, GtkOrientation orientation, WindowckPlugin *wckp) {
  193. /* change the orienation of the box */
  194. xfce_hvbox_set_orientation(XFCE_HVBOX (wckp->hvbox), orientation);
  195. }
  196. static void windowck_screen_position_changed(XfcePanelPlugin *plugin, XfceScreenPosition *position, WindowckPlugin *wckp) {
  197. if (wckp->prefs->size_mode != SHRINK) {
  198. xfce_panel_plugin_set_shrink (plugin, FALSE);
  199. gtk_label_set_width_chars(wckp->title, 1);
  200. xfce_panel_plugin_set_shrink (plugin, TRUE);
  201. resizeTitle(wckp);
  202. }
  203. }
  204. static gboolean windowck_size_changed(XfcePanelPlugin *plugin, gint size, WindowckPlugin *wckp) {
  205. GtkOrientation orientation;
  206. /* get the orientation of the plugin */
  207. orientation = xfce_panel_plugin_get_orientation(plugin);
  208. /* set the widget size */
  209. if (orientation == GTK_ORIENTATION_HORIZONTAL)
  210. gtk_widget_set_size_request(GTK_WIDGET (plugin), -1, size);
  211. else
  212. gtk_widget_set_size_request(GTK_WIDGET (plugin), size, -1);
  213. /* set icon size */
  214. if (size >= 32)
  215. wckp->icon->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
  216. else
  217. wckp->icon->size = GTK_ICON_SIZE_MENU;
  218. /* we handled the orientation */
  219. return TRUE;
  220. }
  221. static void windowck_construct(XfcePanelPlugin *plugin) {
  222. WindowckPlugin *wckp;
  223. /* setup transation domain */
  224. xfce_textdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
  225. /* create the plugin */
  226. wckp = windowck_new(plugin);
  227. /* add the ebox to the panel */
  228. gtk_container_add(GTK_CONTAINER (plugin), wckp->ebox);
  229. /* show the panel's right-click menu on this ebox */
  230. xfce_panel_plugin_add_action_widget(plugin, wckp->ebox);
  231. // Set event handling (icon & title clicks)
  232. g_signal_connect(G_OBJECT (wckp->ebox), "button-press-event", G_CALLBACK (on_title_pressed), wckp);
  233. g_signal_connect(G_OBJECT (wckp->ebox), "button-release-event", G_CALLBACK (on_title_released), wckp);
  234. g_signal_connect(G_OBJECT (wckp->icon->eventbox), "button-release-event", G_CALLBACK (on_icon_released), wckp);
  235. /* connect plugin signals */
  236. g_signal_connect(G_OBJECT (plugin), "free-data", G_CALLBACK (windowck_free), wckp);
  237. g_signal_connect(G_OBJECT (plugin), "save", G_CALLBACK (windowck_save), wckp);
  238. g_signal_connect(G_OBJECT (plugin), "size-changed", G_CALLBACK (windowck_size_changed), wckp);
  239. g_signal_connect(G_OBJECT (plugin), "screen-position-changed", G_CALLBACK (windowck_screen_position_changed), wckp);
  240. g_signal_connect(G_OBJECT (plugin), "orientation-changed", G_CALLBACK (windowck_orientation_changed), wckp);
  241. /* show the configure menu item and connect signal */
  242. xfce_panel_plugin_menu_show_configure(plugin);
  243. g_signal_connect(G_OBJECT (plugin), "configure-plugin", G_CALLBACK (windowck_configure), wckp);
  244. /* show the about menu item and connect signal */
  245. xfce_panel_plugin_menu_show_about(plugin);
  246. g_signal_connect (G_OBJECT (plugin), "about",
  247. G_CALLBACK (wck_about), "windowck-plugin");
  248. // //plugin->priv->menu_items;
  249. // GtkWidget *item1 = gtk_menu_item_new_with_label("Test");
  250. // xfce_panel_plugin_menu_insert_item(plugin, GTK_MENU_ITEM(item1));
  251. // gtk_widget_show(GTK_WIDGET(item1));
  252. /* start tracking title size */
  253. initTitle(wckp);
  254. /* start tracking title text */
  255. wckp->win = g_slice_new0 (WckUtils);
  256. initWnck(wckp->win, wckp->prefs->only_maximized, wckp);
  257. }
  258. /* register the plugin */
  259. XFCE_PANEL_PLUGIN_REGISTER(windowck_construct);