windowck.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 "windowck.h"
  31. #include "windowck-dialogs.h"
  32. #include "windowck-title.h"
  33. /* default settings */
  34. #define DEFAULT_ONLY_MAXIMIZED TRUE
  35. #define DEFAULT_SHOW_ON_DESKTOP FALSE
  36. #define DEFAULT_HIDE_TITLE FALSE
  37. #define DEFAULT_FULL_NAME TRUE
  38. #define DEFAULT_TWO_LINES FALSE
  39. #define DEFAULT_SHOW_TOOLTIPS TRUE
  40. #define DEFAULT_SHOW_APP_ICON TRUE
  41. #define DEFAULT_ICON_ON_RIGHT FALSE
  42. #define DEFAULT_SHOW_WINDOW_MENU TRUE
  43. #define DEFAULT_SIZE_MODE FIXE
  44. #define DEFAULT_TITLE_SIZE 80
  45. #define DEFAULT_TITLE_ALIGNMENT CENTER
  46. #define DEFAULT_TITLE_PADDING 3
  47. #define DEFAULT_SYNC_WM_FONT TRUE
  48. #define DEFAULT_TITLE_FONT "sans 10"
  49. #define DEFAULT_SUBTITLE_FONT "sans 10"
  50. #define DEFAULT_INACTIVE_TEXT_ALPHA 60
  51. #define DEFAULT_INACTIVE_TEXT_SHADE 110
  52. /* prototypes */
  53. static void windowck_construct(XfcePanelPlugin *plugin);
  54. void windowck_save(XfcePanelPlugin *plugin, WindowckPlugin *wckp)
  55. {
  56. XfceRc *rc;
  57. gchar *file;
  58. /* get the config file location */
  59. file = xfce_panel_plugin_save_location(plugin, TRUE);
  60. if (G_UNLIKELY (file == NULL))
  61. {
  62. DBG("Failed to open config file");
  63. return;
  64. }
  65. /* open the config file, read/write */
  66. rc = xfce_rc_simple_open(file, FALSE);
  67. g_free(file);
  68. if (G_LIKELY (rc != NULL))
  69. {
  70. /* save the settings */
  71. DBG(".");
  72. xfce_rc_write_bool_entry(rc, "only_maximized", wckp->prefs->only_maximized);
  73. xfce_rc_write_bool_entry(rc, "show_on_desktop", wckp->prefs->show_on_desktop);
  74. xfce_rc_write_bool_entry(rc, "show_app_icon", wckp->prefs->show_app_icon);
  75. xfce_rc_write_bool_entry(rc, "icon_on_right", wckp->prefs->icon_on_right);
  76. xfce_rc_write_bool_entry(rc, "show_window_menu", wckp->prefs->show_window_menu);
  77. xfce_rc_write_bool_entry(rc, "hide_title", wckp->prefs->hide_title);
  78. xfce_rc_write_bool_entry(rc, "full_name", wckp->prefs->full_name);
  79. xfce_rc_write_bool_entry(rc, "two_lines", wckp->prefs->two_lines);
  80. xfce_rc_write_bool_entry(rc, "show_tooltips", wckp->prefs->show_tooltips);
  81. xfce_rc_write_int_entry(rc, "size_mode", wckp->prefs->size_mode);
  82. xfce_rc_write_int_entry(rc, "title_size", wckp->prefs->title_size);
  83. xfce_rc_write_bool_entry(rc, "sync_wm_font", wckp->prefs->sync_wm_font);
  84. if (wckp->prefs->title_font)
  85. xfce_rc_write_entry(rc, "title_font", wckp->prefs->title_font);
  86. if (wckp->prefs->subtitle_font)
  87. xfce_rc_write_entry(rc, "subtitle_font", wckp->prefs->subtitle_font);
  88. xfce_rc_write_int_entry(rc, "title_alignment", wckp->prefs->title_alignment);
  89. xfce_rc_write_int_entry(rc, "title_padding", wckp->prefs->title_padding);
  90. xfce_rc_write_int_entry(rc, "inactive_text_alpha", wckp->prefs->inactive_text_alpha);
  91. xfce_rc_write_int_entry(rc, "inactive_text_shade", wckp->prefs->inactive_text_shade);
  92. /* close the rc file */
  93. xfce_rc_close(rc);
  94. }
  95. }
  96. static void windowck_read(WindowckPlugin *wckp)
  97. {
  98. XfceRc *rc;
  99. gchar *file;
  100. const gchar *title_font, *subtitle_font;
  101. /* allocate memory for the preferences structure */
  102. wckp->prefs = g_slice_new0(WCKPreferences);
  103. /* get the plugin config file location */
  104. file = xfce_panel_plugin_save_location(wckp->plugin, TRUE);
  105. if (G_LIKELY (file != NULL))
  106. {
  107. /* open the config file, readonly */
  108. rc = xfce_rc_simple_open(file, TRUE);
  109. /* cleanup */
  110. g_free(file);
  111. if (G_LIKELY (rc != NULL))
  112. {
  113. /* read the settings */
  114. wckp->prefs->only_maximized = xfce_rc_read_bool_entry(rc, "only_maximized", DEFAULT_ONLY_MAXIMIZED);
  115. wckp->prefs->show_on_desktop = xfce_rc_read_bool_entry(rc, "show_on_desktop", DEFAULT_SHOW_ON_DESKTOP);
  116. wckp->prefs->show_app_icon = xfce_rc_read_bool_entry(rc, "show_app_icon", DEFAULT_SHOW_APP_ICON);
  117. wckp->prefs->icon_on_right = xfce_rc_read_bool_entry(rc, "icon_on_right", DEFAULT_ICON_ON_RIGHT);
  118. wckp->prefs->show_window_menu = xfce_rc_read_bool_entry(rc, "show_window_menu", DEFAULT_SHOW_WINDOW_MENU);
  119. wckp->prefs->hide_title = xfce_rc_read_bool_entry(rc, "hide_title", DEFAULT_HIDE_TITLE);
  120. wckp->prefs->full_name = xfce_rc_read_bool_entry(rc, "full_name", DEFAULT_FULL_NAME);
  121. wckp->prefs->two_lines = xfce_rc_read_bool_entry(rc, "two_lines", DEFAULT_TWO_LINES);
  122. wckp->prefs->show_tooltips = xfce_rc_read_bool_entry(rc, "show_tooltips", DEFAULT_SHOW_TOOLTIPS);
  123. wckp->prefs->size_mode = xfce_rc_read_int_entry (rc, "size_mode", DEFAULT_SIZE_MODE);
  124. wckp->prefs->title_size = xfce_rc_read_int_entry(rc, "title_size", DEFAULT_TITLE_SIZE);
  125. wckp->prefs->sync_wm_font = xfce_rc_read_bool_entry(rc, "sync_wm_font", DEFAULT_SYNC_WM_FONT);
  126. title_font = xfce_rc_read_entry(rc, "title_font", DEFAULT_TITLE_FONT);
  127. wckp->prefs->title_font = g_strdup(title_font);
  128. subtitle_font = xfce_rc_read_entry(rc, "subtitle_font", DEFAULT_SUBTITLE_FONT);
  129. wckp->prefs->subtitle_font = g_strdup(subtitle_font);
  130. wckp->prefs->title_alignment = xfce_rc_read_int_entry(rc, "title_alignment", DEFAULT_TITLE_ALIGNMENT);
  131. wckp->prefs->title_padding = xfce_rc_read_int_entry(rc, "title_padding", DEFAULT_TITLE_PADDING);
  132. wckp->prefs->inactive_text_alpha = xfce_rc_read_int_entry(rc, "inactive_text_alpha", DEFAULT_INACTIVE_TEXT_ALPHA);
  133. wckp->prefs->inactive_text_shade = xfce_rc_read_int_entry(rc, "inactive_text_shade", DEFAULT_INACTIVE_TEXT_SHADE);
  134. /* cleanup */
  135. xfce_rc_close(rc);
  136. /* leave the function, everything went well */
  137. return;
  138. }
  139. }
  140. /* something went wrong, apply default values */
  141. DBG("Applying default settings");
  142. wckp->prefs->only_maximized = DEFAULT_ONLY_MAXIMIZED;
  143. wckp->prefs->show_on_desktop = DEFAULT_SHOW_ON_DESKTOP;
  144. wckp->prefs->show_app_icon = DEFAULT_SHOW_APP_ICON;
  145. wckp->prefs->icon_on_right = DEFAULT_ICON_ON_RIGHT;
  146. wckp->prefs->show_window_menu = DEFAULT_SHOW_WINDOW_MENU;
  147. wckp->prefs->hide_title = DEFAULT_HIDE_TITLE;
  148. wckp->prefs->full_name = DEFAULT_FULL_NAME;
  149. wckp->prefs->two_lines = DEFAULT_TWO_LINES;
  150. wckp->prefs->show_tooltips = DEFAULT_SHOW_TOOLTIPS;
  151. wckp->prefs->size_mode = DEFAULT_SIZE_MODE;
  152. wckp->prefs->title_size = DEFAULT_TITLE_SIZE;
  153. wckp->prefs->sync_wm_font = DEFAULT_SYNC_WM_FONT;
  154. wckp->prefs->title_font = DEFAULT_TITLE_FONT;
  155. wckp->prefs->subtitle_font = DEFAULT_SUBTITLE_FONT;
  156. wckp->prefs->title_alignment = DEFAULT_TITLE_ALIGNMENT;
  157. wckp->prefs->title_padding = DEFAULT_TITLE_PADDING;
  158. wckp->prefs->inactive_text_alpha = DEFAULT_INACTIVE_TEXT_ALPHA;
  159. wckp->prefs->inactive_text_shade = DEFAULT_INACTIVE_TEXT_SHADE;
  160. }
  161. void create_symbol (gboolean show_app_icon, WindowckPlugin *wckp)
  162. {
  163. if (GTK_IS_WIDGET (wckp->icon->symbol))
  164. gtk_widget_destroy (wckp->icon->symbol);
  165. if (wckp->prefs->show_window_menu)
  166. {
  167. if (show_app_icon)
  168. wckp->icon->symbol = xfce_panel_image_new();
  169. else
  170. wckp->icon->symbol = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
  171. gtk_container_add (GTK_CONTAINER (wckp->icon->eventbox), wckp->icon->symbol);
  172. gtk_widget_show_all (GTK_WIDGET(wckp->icon->eventbox));
  173. }
  174. else
  175. {
  176. gtk_widget_hide_all (GTK_WIDGET(wckp->icon->eventbox));
  177. }
  178. }
  179. static void create_icon (WindowckPlugin *wckp)
  180. {
  181. wckp->icon = g_slice_new0 (WindowIcon);
  182. wckp->icon->eventbox = GTK_EVENT_BOX (gtk_event_box_new());
  183. wckp->icon->symbol = NULL;
  184. gtk_widget_set_can_focus (GTK_WIDGET(wckp->icon->eventbox), TRUE);
  185. gtk_event_box_set_visible_window (wckp->icon->eventbox, FALSE);
  186. gtk_box_pack_start (GTK_BOX (wckp->hvbox), GTK_WIDGET(wckp->icon->eventbox), FALSE, FALSE, 0);
  187. create_symbol (wckp->prefs->show_app_icon, wckp);
  188. }
  189. static WindowckPlugin * windowck_new(XfcePanelPlugin *plugin)
  190. {
  191. WindowckPlugin *wckp;
  192. GtkOrientation orientation;
  193. GtkWidget *label;
  194. /* allocate memory for the plugin structure */
  195. wckp = g_slice_new0 (WindowckPlugin);
  196. /* pointer to plugin */
  197. wckp->plugin = plugin;
  198. /* read the user settings */
  199. windowck_read(wckp);
  200. /* get the current orientation */
  201. orientation = xfce_panel_plugin_get_orientation(plugin);
  202. /* not needed for shrink mode */
  203. if (!wckp->prefs->size_mode == SHRINK)
  204. xfce_panel_plugin_set_shrink (plugin, TRUE);
  205. /* create some panel widgets */
  206. wckp->ebox = gtk_event_box_new();
  207. gtk_event_box_set_visible_window(GTK_EVENT_BOX(wckp->ebox), FALSE);
  208. gtk_widget_set_name(wckp->ebox, "XfceWindowckPlugin");
  209. wckp->alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
  210. wckp->hvbox = xfce_hvbox_new(orientation, FALSE, 2);
  211. /* some wckp widgets */
  212. label = gtk_label_new("");
  213. wckp->title = GTK_LABEL (label);
  214. create_icon (wckp);
  215. gtk_box_pack_start (GTK_BOX(wckp->hvbox), label, TRUE, TRUE, 0);
  216. if (wckp->prefs->icon_on_right)
  217. {
  218. gtk_box_reorder_child (GTK_BOX (wckp->hvbox), GTK_WIDGET(wckp->icon->eventbox), 1);
  219. }
  220. gtk_container_add(GTK_CONTAINER(wckp->alignment), GTK_WIDGET(wckp->hvbox));
  221. gtk_container_add(GTK_CONTAINER(wckp->ebox), wckp->alignment);
  222. /* show widgets */
  223. gtk_widget_show(wckp->ebox);
  224. gtk_widget_show(wckp->alignment);
  225. gtk_widget_show(wckp->hvbox);
  226. gtk_widget_show(label);
  227. return wckp;
  228. }
  229. static void windowck_free(XfcePanelPlugin *plugin, WindowckPlugin *wckp)
  230. {
  231. GtkWidget *dialog;
  232. /* check if the dialog is still open. if so, destroy it */
  233. dialog = g_object_get_data(G_OBJECT (plugin), "dialog");
  234. if (G_UNLIKELY (dialog != NULL))
  235. gtk_widget_destroy(dialog);
  236. /* destroy the panel widgets */
  237. gtk_widget_destroy(wckp->hvbox);
  238. /* free the plugin structure */
  239. g_slice_free(WindowIcon, wckp->icon);
  240. g_slice_free(WckUtils, wckp->win);
  241. g_slice_free(WCKPreferences, wckp->prefs);
  242. g_slice_free(WindowckPlugin, wckp);
  243. }
  244. static void windowck_orientation_changed(XfcePanelPlugin *plugin, GtkOrientation orientation, WindowckPlugin *wckp)
  245. {
  246. /* change the orienation of the box */
  247. xfce_hvbox_set_orientation(XFCE_HVBOX (wckp->hvbox), orientation);
  248. }
  249. static void windowck_screen_position_changed(XfcePanelPlugin *plugin, XfceScreenPosition *position, WindowckPlugin *wckp)
  250. {
  251. if (wckp->prefs->size_mode != SHRINK)
  252. {
  253. xfce_panel_plugin_set_shrink (plugin, FALSE);
  254. gtk_label_set_width_chars(wckp->title, 1);
  255. xfce_panel_plugin_set_shrink (plugin, TRUE);
  256. resize_title(wckp);
  257. }
  258. }
  259. static gboolean windowck_size_changed(XfcePanelPlugin *plugin, gint size, WindowckPlugin *wckp)
  260. {
  261. GtkOrientation orientation;
  262. /* get the orientation of the plugin */
  263. orientation = xfce_panel_plugin_get_orientation(plugin);
  264. /* set the widget size */
  265. if (orientation == GTK_ORIENTATION_HORIZONTAL)
  266. gtk_widget_set_size_request(GTK_WIDGET (plugin), -1, size);
  267. else
  268. gtk_widget_set_size_request(GTK_WIDGET (plugin), size, -1);
  269. /* we handled the orientation */
  270. return TRUE;
  271. }
  272. static void on_refresh_item_activated (GtkMenuItem *refresh, WindowckPlugin *wckp)
  273. {
  274. init_title(wckp);
  275. reload_wnck_title (wckp);
  276. }
  277. static void windowck_construct(XfcePanelPlugin *plugin)
  278. {
  279. WindowckPlugin *wckp;
  280. GtkWidget *refresh;
  281. /* setup transation domain */
  282. xfce_textdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
  283. /* create the plugin */
  284. wckp = windowck_new(plugin);
  285. /* add the ebox to the panel */
  286. gtk_container_add(GTK_CONTAINER (plugin), wckp->ebox);
  287. /* show the panel's right-click menu on this ebox */
  288. xfce_panel_plugin_add_action_widget(plugin, wckp->ebox);
  289. // Set event handling (icon & title clicks)
  290. g_signal_connect(G_OBJECT (wckp->ebox), "button-press-event", G_CALLBACK (on_title_pressed), wckp);
  291. g_signal_connect(G_OBJECT (wckp->ebox), "button-release-event", G_CALLBACK (on_title_released), wckp);
  292. g_signal_connect(G_OBJECT (wckp->icon->eventbox), "button-release-event", G_CALLBACK (on_icon_released), wckp);
  293. /* connect plugin signals */
  294. g_signal_connect(G_OBJECT (plugin), "free-data", G_CALLBACK (windowck_free), wckp);
  295. g_signal_connect(G_OBJECT (plugin), "save", G_CALLBACK (windowck_save), wckp);
  296. g_signal_connect(G_OBJECT (plugin), "size-changed", G_CALLBACK (windowck_size_changed), wckp);
  297. g_signal_connect(G_OBJECT (plugin), "screen-position-changed", G_CALLBACK (windowck_screen_position_changed), wckp);
  298. g_signal_connect(G_OBJECT (plugin), "orientation-changed", G_CALLBACK (windowck_orientation_changed), wckp);
  299. /* show the configure menu item and connect signal */
  300. xfce_panel_plugin_menu_show_configure(plugin);
  301. g_signal_connect(G_OBJECT (plugin), "configure-plugin", G_CALLBACK (windowck_configure), wckp);
  302. /* show the about menu item and connect signal */
  303. xfce_panel_plugin_menu_show_about(plugin);
  304. g_signal_connect (G_OBJECT (plugin), "about",
  305. G_CALLBACK (wck_about), "windowck-plugin");
  306. /* add custom menu items */
  307. refresh = show_refresh_item (plugin);
  308. g_signal_connect (G_OBJECT (refresh), "activate", G_CALLBACK (on_refresh_item_activated), wckp);
  309. /* start tracking title text */
  310. wckp->win = g_slice_new0 (WckUtils);
  311. init_wnck(wckp->win, wckp->prefs->only_maximized, wckp);
  312. /* start tracking title size */
  313. init_title(wckp);
  314. }
  315. /* register the plugin */
  316. XFCE_PANEL_PLUGIN_REGISTER(windowck_construct);