Fix parsing id from launch button config.
[lxde/lxpanel.git] / src / plugins / launchtaskbar.c
1 /**
2 * Copyright (c) 2006 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 /*
20 * Started by Giuseppe Penone <giuspen@gmail.com> merging launchbar and taskbar
21 * and adding interoperability between them.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <signal.h>
35 #include <errno.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38
39 #include <gdk-pixbuf/gdk-pixbuf.h>
40 #include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
41 #include <gdk/gdk.h>
42 #include <glib/gi18n.h>
43
44 #include <libfm/fm-gtk.h>
45
46 #include "misc.h"
47 #include "ev.h"
48 #include "plugin.h"
49 #include "icon.xpm"
50 #include "icon-grid.h"
51 #ifndef DISABLE_MENU
52 # include "menu-policy.h"
53 #endif
54
55 #include "dbg.h" /* for ERR macro. FIXME: is it required? */
56
57
58 #define PANEL_ICON_SIZE 24 /* see the private.h */
59
60 extern FbEv *fbev;
61
62 /* Column definitions for configuration dialogs. */
63 enum {
64 COL_ICON,
65 COL_TITLE,
66 COL_ICON_NAME,
67 COL_BTN,
68 N_COLS
69 };
70
71 typedef struct LaunchTaskBarPlugin LaunchTaskBarPlugin;
72
73 /* Structure representing a class. This comes from WM_CLASS, and should identify windows that come from an application. */
74 typedef struct _task_class {
75 struct _task_class *p_taskclass_flink; /* Forward link */
76 char * res_class; /* Class name */
77 struct _task * p_task_head; /* Head of list of tasks with this class */
78 struct _task * p_task_visible; /* Task that is visible in current desktop, if any */
79 char * visible_name; /* Name that will be visible for grouped tasks */
80 int visible_count; /* Count of tasks that are visible in current desktop */
81 } TaskClass;
82
83 /* Structure representing a "task", an open window. */
84 typedef struct _task {
85 struct _task * p_task_flink_xwid; /* Forward link to next task in X window ID order */
86 LaunchTaskBarPlugin * tb; /* Back pointer to plugin */
87 Window win; /* X window ID */
88 char * name; /* Taskbar label when normal, from WM_NAME or NET_WM_NAME */
89 char * name_iconified; /* Taskbar label when iconified */
90 char * exec_bin; /* Exec bin associated to Window */
91 Atom name_source; /* Atom that is the source of taskbar label */
92 TaskClass * p_taskclass; /* Class, from WM_CLASS */
93 struct _task * p_task_flink_same_class; /* Forward link to task in same class */
94 GtkWidget * button; /* Button representing task in taskbar */
95 GtkWidget * image; /* Icon for task, child of button */
96 Atom image_source; /* Atom that is the source of taskbar icon */
97 GtkWidget * label; /* Label for task, child of button */
98 int desktop; /* Desktop that contains task, needed to switch to it on Raise */
99 gint monitor; /* Monitor that the window is on or closest to */
100 guint flash_timeout; /* Timer for urgency notification */
101 unsigned int focused :1; /* True if window has focus */
102 unsigned int iconified :1; /* True if window is iconified, from WM_STATE */
103 unsigned int urgency :1; /* True if window has an urgency hint, from WM_HINTS */
104 unsigned int flash_state :1; /* One-bit counter to flash taskbar */
105 unsigned int entered_state :1; /* True if cursor is inside taskbar button */
106 unsigned int present_in_client_list :1; /* State during WM_CLIENT_LIST processing to detect deletions */
107 } Task;
108
109 /* Representative of one launch button.
110 * Note that the launch parameters come from the specified desktop file, or from the configuration file.
111 * This structure is also used during the "add to launchtaskbar" dialog to hold menu items. */
112 typedef struct {
113 LaunchTaskBarPlugin * p; /* Back pointer to plugin */
114 GtkWidget * widget; /* Pointer to button */
115 GtkWidget * image_widget; /* Pointer to image */
116 FmFileInfo * fi; /* Launcher application descriptor */
117 config_setting_t * settings; /* Pointer to settings */
118 FmDndDest * dd; /* Drag and drop support */
119 } LaunchButton;
120
121 /* Private context for taskbar plugin. */
122 struct LaunchTaskBarPlugin {
123 /* LAUNCHBAR */
124 IconGrid *lb_icon_grid; /* Icon grid managing the container */
125 GSList *buttons; /* Launchbar buttons */
126 LaunchButton *bootstrap_button; /* Bootstrapping button for empty launchtaskbar */
127 FmIcon * add_icon; /* Icon for bootstrap_button */
128 GtkWidget *p_button_add, *p_button_remove, *p_label_menu_app_exec, *p_label_def_app_exec;
129 /* TASKBAR */
130 Task * p_task_list; /* List of tasks to be displayed in taskbar */
131 TaskClass * p_taskclass_list; /* Window class list */
132 IconGrid * tb_icon_grid; /* Manager for taskbar buttons */
133 GtkWidget * menu; /* Popup menu for task control (Close, Raise, etc.) */
134 GtkWidget * group_menu; /* Popup menu for grouping selection */
135 GdkPixbuf * fallback_pixbuf; /* Fallback task icon when none is available */
136 int number_of_desktops; /* Number of desktops, from NET_WM_NUMBER_OF_DESKTOPS */
137 int current_desktop; /* Current desktop, from NET_WM_CURRENT_DESKTOP */
138 Task * focused; /* Task that has focus */
139 Task * focused_previous; /* Task that had focus just before panel got it */
140 Task * menutask; /* Task for which popup menu is open */
141 guint dnd_delay_timer; /* Timer for drag and drop delay */
142 int icon_size; /* Size of task icons */
143 gboolean show_all_desks; /* User preference: show windows from all desktops */
144 gboolean tooltips; /* User preference: show tooltips */
145 gboolean icons_only; /* User preference: show icons only, omit name */
146 gboolean use_mouse_wheel; /* User preference: scroll wheel does iconify and raise */
147 gboolean use_urgency_hint; /* User preference: windows with urgency will flash */
148 gboolean flat_button; /* User preference: taskbar buttons have visible background */
149 gboolean grouped_tasks; /* User preference: windows from same task are grouped onto a single button */
150 gboolean same_monitor_only; /* User preference: only show windows that are in the same monitor as the taskbar */
151 int task_width_max; /* Maximum width of a taskbar button in horizontal orientation */
152 int spacing; /* Spacing between taskbar buttons */
153 gboolean use_net_active; /* NET_WM_ACTIVE_WINDOW is supported by the window manager */
154 gboolean net_active_checked; /* True if use_net_active is valid */
155 /* COMMON */
156 #ifndef DISABLE_MENU
157 GtkWidget *p_menuitem_lock_tbp;
158 GtkWidget *p_menuitem_unlock_tbp;
159 GtkWidget *p_menuitem_new_instance;
160 #endif
161 GtkWidget * plugin; /* Back pointer to Plugin */
162 Panel * panel; /* Back pointer to panel */
163 config_setting_t * settings;
164 GtkWidget *p_evbox_launchbar;
165 GtkWidget *p_evbox_taskbar;
166 GtkWidget *config_dlg; /* Configuration dialog */
167 GtkWidget *p_notebook_page_launch;
168 GtkWidget *p_notebook_page_task;
169 GKeyFile *p_key_file_special_cases;
170 gboolean lb_on;
171 gboolean lb_built;
172 gboolean tb_on;
173 gboolean tb_built;
174 };
175
176 static gchar *launchtaskbar_rc = "style 'launchtaskbar-style'\n"
177 "{\n"
178 "GtkWidget::focus-line-width=0\n"
179 "GtkWidget::focus-padding=0\n"
180 "GtkButton::default-border={0,0,0,0}\n"
181 "GtkWidget::focus-padding=0\n"
182 "GtkButton::default-outside-border={0,0,0,0}\n"
183 "GtkButton::inner-border={0,0,0,0}\n"
184 "}\n"
185 "widget '*launchtaskbar*' style 'launchtaskbar-style'";
186
187 #define DRAG_ACTIVE_DELAY 1000
188 #define TASK_WIDTH_MAX 200
189 #define ALL_WORKSPACES -1
190 #define ICON_ONLY_EXTRA 6 /* Amount needed to have button lay out symmetrically */
191 #define ICON_BUTTON_TRIM 4 /* Amount needed to have button remain on panel */
192
193 static void launchtaskbar_destructor(gpointer user_data);
194
195 static void taskbar_redraw(LaunchTaskBarPlugin * tb);
196 static void task_delete(LaunchTaskBarPlugin * tb, Task * tk, gboolean unlink);
197 static GdkPixbuf * task_update_icon(LaunchTaskBarPlugin * tb, Task * tk, Atom source);
198 static gboolean flash_window_timeout(Task * tk);
199 static void task_group_menu_destroy(LaunchTaskBarPlugin * tb);
200 static gboolean taskbar_popup_activate_event(GtkWidget * widget, GdkEventButton * event, Task * tk);
201 static void taskbar_update_style(LaunchTaskBarPlugin * tb);
202 static void taskbar_net_client_list(GtkWidget * widget, LaunchTaskBarPlugin * tb);
203 static void taskbar_net_current_desktop(GtkWidget * widget, LaunchTaskBarPlugin * tb);
204 static void taskbar_net_number_of_desktops(GtkWidget * widget, LaunchTaskBarPlugin * tb);
205 static void taskbar_net_active_window(GtkWidget * widget, LaunchTaskBarPlugin * tb);
206 static gboolean task_has_urgency(Task * tk);
207 static GdkFilterReturn taskbar_event_filter(XEvent * xev, GdkEvent * event, LaunchTaskBarPlugin * tb);
208 static void taskbar_make_menu(LaunchTaskBarPlugin * tb);
209 static void taskbar_window_manager_changed(GdkScreen * screen, LaunchTaskBarPlugin * tb);
210 static void taskbar_apply_configuration(LaunchTaskBarPlugin * ltbp);
211
212 static void f_get_exec_cmd_from_pid(GPid pid, gchar *buffer_128, const gchar *proc_file)
213 {
214 buffer_128[0] = '\0';
215 FILE *pipe;
216 gchar command[64];
217 snprintf(command, 64, "cat /proc/%u/%s", pid, proc_file);
218 pipe = popen(command, "r");
219 if(pipe == NULL)
220 ERR("ltbp: popen '%s'\n", command);
221 else if(fgets(buffer_128, 128, pipe) == NULL)
222 ERR("ltbp: fgets '%s'\n", command);
223 else
224 {
225 gchar *p_char = strchr(buffer_128, '\n');
226 if(p_char != NULL) *p_char = '\0';
227 }
228 if(pipe != NULL) pclose(pipe);
229 }
230
231 #ifndef DISABLE_MENU
232 static FmFileInfo *f_find_menu_launchbutton_recursive(const char *exec_bin)
233 {
234 MenuCache *mc;
235 guint32 flags;
236 GSList *apps, *l;
237 size_t len = strlen(exec_bin);
238 const char *exec;
239 char *str_path;
240 FmPath *path;
241 FmFileInfoJob *job;
242 FmFileInfo *fi = NULL;
243
244 /* FIXME: cache it in Task object */
245 mc = panel_menu_cache_new(&flags);
246 apps = menu_cache_list_all_apps(mc);
247 for (l = apps; l; l = l->next)
248 {
249 exec = menu_cache_app_get_exec(MENU_CACHE_APP(l->data));
250 /* we don't check flags here because user always can manually
251 start any app that isn't visible in the desktop menu */
252 if (strncmp(exec, exec_bin, len) == 0 && (exec[len] == ' ' || exec[len] == 0))
253 break;
254 }
255 if (l)
256 {
257 str_path = menu_cache_dir_make_path(MENU_CACHE_DIR(l->data));
258 path = fm_path_new_relative(fm_path_get_apps_menu(), str_path+13); /* skip /Applications */
259 g_free(str_path);
260 job = fm_file_info_job_new(NULL, FM_FILE_INFO_JOB_NONE);
261 fm_file_info_job_add(job, path);
262 fm_path_unref(path);
263 if (!fm_job_run_sync(FM_JOB(job)))
264 g_warning("launchtaskbar: problem running file info job");
265 else
266 fi = fm_file_info_list_pop_head(job->file_infos);
267 g_object_unref(job);
268 }
269 g_slist_foreach(apps, (GFunc)menu_cache_item_unref, NULL);
270 g_slist_free(apps);
271 g_debug("f_find_menu_launchbutton_recursive: search '%s' found=%d", exec_bin, (fi != NULL));
272 return fi;
273 }
274 #endif
275
276 /* Deallocate a LaunchButton. */
277 static void launchbutton_free(LaunchButton * btn)
278 {
279 if (btn->fi)
280 fm_file_info_unref(btn->fi);
281 if (btn->dd)
282 g_object_unref(btn->dd);
283 g_free(btn);
284 }
285
286 /* Handler for "button-press-event" event from launchtaskbar button. */
287 static gboolean launchbutton_press_event(GtkWidget * widget, GdkEventButton * event, LaunchButton * b)
288 {
289 /* Standard right-click handling. */
290 if (lxpanel_plugin_button_press_event(b->p->plugin, event, b->p->panel))
291 return TRUE;
292
293 if (event->button == 1) /* left button */
294 {
295 if (b->fi == NULL) /* The bootstrap button */
296 lxpanel_plugin_show_config_dialog(b->p->panel, b->p->plugin);
297 else
298 lxpanel_launch_path(b->p->panel, fm_file_info_get_path(b->fi));
299 }
300 return TRUE;
301 }
302
303 /* Handler for "drag-motion" event from launchtaskbar button. */
304 static gboolean launchbutton_drag_motion_event(
305 GtkWidget * widget,
306 GdkDragContext * context,
307 gint x,
308 gint y,
309 guint time,
310 LaunchButton * b)
311 {
312 GdkAtom target;
313 GdkDragAction action = 0;
314
315 fm_dnd_dest_set_dest_file(b->dd, b->fi);
316 target = fm_dnd_dest_find_target(b->dd, context);
317 if (target != GDK_NONE && fm_dnd_dest_is_target_supported(b->dd, target))
318 action = fm_dnd_dest_get_default_action(b->dd, context, target);
319 gdk_drag_status(context, action, time);
320 /* g_debug("launchbutton_drag_motion_event: act=%u",action); */
321 return (action != 0);
322 }
323
324 /* Build the graphic elements for the bootstrap launchtaskbar button. */
325 static void launchbutton_build_bootstrap(LaunchTaskBarPlugin *lb)
326 {
327 if(lb->bootstrap_button == NULL)
328 {
329 GdkPixbuf * icon;
330 /* Build a button that has the stock "Add" icon.
331 * The "desktop-id" being NULL is the marker that this is the bootstrap button. */
332 lb->bootstrap_button = g_new0(LaunchButton, 1);
333 lb->bootstrap_button->p = lb;
334
335 /* Create an event box. */
336 GtkWidget * event_box = gtk_event_box_new();
337 gtk_container_set_border_width(GTK_CONTAINER(event_box), 0);
338 #if GTK_CHECK_VERSION(2,18,0)
339 gtk_widget_set_can_focus (event_box, FALSE);
340 #else
341 GTK_WIDGET_UNSET_FLAGS(event_box, GTK_CAN_FOCUS);
342 #endif
343 lb->bootstrap_button->widget = event_box;
344 g_signal_connect(event_box, "button-press-event", G_CALLBACK(launchbutton_press_event), lb->bootstrap_button);
345
346 /* Create an image containing the stock "Add" icon as a child of the event box. */
347 lb->add_icon = fm_icon_from_name(GTK_STOCK_ADD);
348 icon = fm_pixbuf_from_icon(lb->add_icon, lb->icon_size);
349 lb->bootstrap_button->image_widget = gtk_image_new_from_pixbuf(icon);
350 g_object_unref(icon);
351 gtk_misc_set_padding(GTK_MISC(lb->bootstrap_button->image_widget), 0, 0);
352 gtk_misc_set_alignment(GTK_MISC(lb->bootstrap_button->image_widget), 0, 0);
353 gtk_container_add(GTK_CONTAINER(event_box), lb->bootstrap_button->image_widget);
354
355 /* Add the bootstrap button to the icon grid. By policy it is empty at this point. */
356 icon_grid_add(lb->lb_icon_grid, event_box, TRUE);
357 }
358 else
359 icon_grid_set_visible(lb->lb_icon_grid, lb->bootstrap_button->widget, TRUE);
360 }
361
362 #ifndef DISABLE_MENU
363 static LaunchButton *launchbar_exec_bin_exists(LaunchTaskBarPlugin *lb, FmFileInfo *fi)
364 {
365 LaunchButton *ret_val = NULL;
366 FmPath *path;
367 GSList* l;
368
369 if (!fi)
370 return NULL;
371 path = fm_file_info_get_path(fi);
372 for(l = lb->buttons; l != NULL; l = l->next)
373 {
374 LaunchButton *btn = (LaunchButton *)l->data;
375 if (btn->fi && fm_path_equal(path, fm_file_info_get_path(btn->fi)))
376 {
377 ret_val = btn;
378 break;
379 }
380 }
381 return ret_val;
382 }
383 #endif
384
385 static void launchbar_update_after_taskbar_class_added(LaunchTaskBarPlugin *ltbp, Task *tk)
386 {
387 GPid pid = get_net_wm_pid(tk->win);
388 gchar exec_bin_full[128];
389 f_get_exec_cmd_from_pid(pid, exec_bin_full, "cmdline");
390 gchar *p_char = strrchr(exec_bin_full, '/');
391 if(p_char == NULL) p_char = exec_bin_full;
392 else p_char++;
393 g_free(tk->exec_bin);
394 if(strcmp(p_char, "python") == 0)
395 {
396 f_get_exec_cmd_from_pid(pid, exec_bin_full, "comm");
397 }
398 else
399 {
400 tk->exec_bin = g_key_file_get_string(ltbp->p_key_file_special_cases,
401 "special_cases", p_char, NULL);
402 if (tk->exec_bin != NULL) /* found this key */
403 return;
404 }
405 tk->exec_bin = g_strdup(exec_bin_full);
406
407 /*if(ltbp->lb_on)
408 {
409 LaunchButton *btn = launchbar_exec_bin_exists(&ltbp->lbp, tk->exec_bin);
410 g_print("\nTB '%s' OPEN (pid=%u), in LB: %c\n",
411 tk->exec_bin, pid, btn != NULL ? 'Y':'N');
412 }*/
413 }
414
415 static void launchbar_update_after_taskbar_class_removed(LaunchTaskBarPlugin *ltbp, Task *tk)
416 {
417 /*if(ltbp->lb_on)
418 {
419 LaunchButton *btn = launchbar_exec_bin_exists(&ltbp->lbp, tk->exec_bin);
420 g_print("\nTB '%s' CLOSE, in LB: %c\n", tk->exec_bin, btn != NULL ? 'Y':'N');
421 }*/
422 }
423
424 /* Build the graphic elements for a launchtaskbar button. The desktop_id field is already established. */
425 /* NOTE: this func consumes reference on fi */
426 static LaunchButton *launchbutton_for_file_info(LaunchTaskBarPlugin * lb, FmFileInfo * fi)
427 {
428 LaunchButton *btn;
429 GtkWidget *button;
430
431 if (fi == NULL)
432 {
433 g_warning("launchbar: desktop entry does not exist\n");
434 return NULL;
435 }
436
437 /* Allocate the LaunchButton structure. */
438 btn = g_new0(LaunchButton, 1);
439 btn->p = lb;
440 btn->fi = fi;
441
442 /* Create a button with the specified icon. */
443 button = lxpanel_button_new_for_fm_icon(lb->panel, fm_file_info_get_icon(fi),
444 NULL, NULL);
445 btn->widget = button;
446 #if GTK_CHECK_VERSION(2,18,0)
447 gtk_widget_set_can_focus(button, FALSE);
448 #else
449 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
450 #endif
451
452 gtk_widget_set_tooltip_text(button, fm_file_info_get_disp_name(fi));
453
454 /* Add the button to the icon grid. */
455 icon_grid_add(lb->lb_icon_grid, button, TRUE);
456
457 /* Drag and drop support. */
458 btn->dd = fm_dnd_dest_new_with_handlers(button);
459
460 /* Connect signals. */
461 g_signal_connect(button, "button-press-event", G_CALLBACK(launchbutton_press_event), (gpointer) btn);
462 g_signal_connect(button, "drag-motion", G_CALLBACK(launchbutton_drag_motion_event), btn);
463
464 /* If the list goes from null to non-null, remove the bootstrap button. */
465 if ((lb->buttons == NULL) && (lb->bootstrap_button != NULL))
466 icon_grid_set_visible(lb->lb_icon_grid, lb->bootstrap_button->widget, FALSE);
467
468 /* Append at end of list to preserve configured order. */
469 lb->buttons = g_slist_append(lb->buttons, btn);
470
471 /* Show the widget and return. */
472 gtk_widget_show(button);
473 plugin_widget_set_background(button, lb->panel);
474 return btn;
475 }
476
477 static LaunchButton *launchbutton_build_gui(LaunchTaskBarPlugin * lb, FmPath * id)
478 {
479 /* Try to get the file data */
480 FmFileInfoJob *job = fm_file_info_job_new(NULL, FM_FILE_INFO_JOB_NONE);
481 FmFileInfo *fi;
482
483 fm_file_info_job_add(job, id);
484 if (!fm_job_run_sync(FM_JOB(job)))
485 {
486 g_warning("launchbar: problem running file info job\n");
487 g_object_unref(job);
488 return NULL;
489 }
490 fi = fm_file_info_list_pop_head(job->file_infos);
491 g_object_unref(job);
492 return launchbutton_for_file_info(lb, fi);
493 }
494
495 static LaunchButton *launchbutton_search_and_build_gui(LaunchTaskBarPlugin * lb, FmPath * id)
496 {
497 FmDirListJob *job = fm_dir_list_job_new2(id, FM_DIR_LIST_JOB_FAST);
498 FmFileInfo *fi;
499
500 if (!fm_job_run_sync(FM_JOB(job)))
501 {
502 g_warning("launchbar: problem running file search job\n");
503 g_object_unref(job);
504 return NULL;
505 }
506 fi = fm_file_info_list_pop_head(job->files);
507 g_object_unref(job);
508 return launchbutton_for_file_info(lb, fi);
509 }
510
511 /* Read the configuration file entry for a launchtaskbar button and create it. */
512 static gboolean launchbutton_constructor(LaunchTaskBarPlugin * lb, config_setting_t * s)
513 {
514 LaunchButton *btn = NULL;
515 const char *str;
516 char *str_path = NULL;
517 FmPath *path;
518
519 /* Read parameters from the configuration file and validate. */
520 if (!config_setting_lookup_string(s, "id", &str) || str[0] == '\0')
521 return FALSE;
522
523 /* Build the structures and return. */
524 if (str[0] == '~')
525 {
526 str_path = expand_tilda(str);
527 path = fm_path_new_for_path(str_path);
528 btn = launchbutton_build_gui(lb, path);
529 }
530 else if (strchr(str, '/') != NULL)
531 {
532 path = fm_path_new_for_str(str);
533 /* FIXME: check if str contains invalid path */
534 btn = launchbutton_build_gui(lb, path);
535 }
536 else
537 {
538 str_path = g_strdup_printf("search://menu://applications/?recursive=1&show_hidden=1&name=%s", str);
539 path = fm_path_new_for_uri(str_path);
540 btn = launchbutton_search_and_build_gui(lb, path);
541 }
542 g_free(str_path);
543 fm_path_unref(path);
544 if (btn)
545 btn->settings = s;
546 return (btn != NULL);
547 }
548
549 /* prototype of this is app_info_create_from_commandline() in libfm */
550 static gboolean _launchbutton_create_id(LaunchTaskBarPlugin * lb, config_setting_t * s)
551 {
552 const char *icon = NULL, *name, *exec, *path = NULL;
553 char *dirname, *filename;
554 int fd, terminal = 0;
555 gboolean ret = FALSE;
556
557 if (!config_setting_lookup_string(s, "action", &exec) || exec[0] == '\0')
558 return FALSE;
559 if (!config_setting_lookup_string(s, "tooltip", &name) || name[0] == '\0')
560 name = "Launcher"; /* placeholder, XDG requires a non-empty name */
561 config_setting_lookup_string(s, "image", &icon);
562 config_setting_lookup_string(s, "path", &path);
563 config_setting_lookup_int(s, "terminal", &terminal);
564
565 dirname = g_build_filename(g_get_user_data_dir(), "applications", NULL);
566 if (g_mkdir_with_parents(dirname, 0700) == 0)
567 {
568 filename = g_strdup_printf("%s/lxpanel-launcher-XXXXXX.desktop", dirname);
569 fd = g_mkstemp (filename);
570 if (fd != -1)
571 {
572 GString* content = g_string_sized_new(256);
573
574 g_string_printf(content,
575 "[" G_KEY_FILE_DESKTOP_GROUP "]\n"
576 G_KEY_FILE_DESKTOP_KEY_TYPE "=" G_KEY_FILE_DESKTOP_TYPE_APPLICATION "\n"
577 G_KEY_FILE_DESKTOP_KEY_NAME "=%s\n"
578 G_KEY_FILE_DESKTOP_KEY_EXEC "=%s\n"
579 G_KEY_FILE_DESKTOP_KEY_CATEGORIES "=X-LXPanel;\n",
580 name, exec);
581 if (icon)
582 g_string_append_printf(content, "Icon=%s\n", icon);
583 if (terminal)
584 g_string_append(content, G_KEY_FILE_DESKTOP_KEY_TERMINAL "=true\n");
585 if (path && path[0] == '/')
586 g_string_append_printf(content, "Path=%s\n", path);
587 close(fd);
588 ret = g_file_set_contents(filename, content->str, content->len, NULL);
589 if (ret) {
590 config_group_set_string(s, "id", filename);
591 /* FIXME: is it reasonable to remove obsolete keys too? */
592 panel_config_save(lb->panel);
593 } else
594 g_unlink(filename);
595 g_string_free(content, TRUE);
596 }
597 g_free(filename);
598 }
599 g_free(dirname);
600 if (ret) /* we created it, let use it */
601 return launchbutton_constructor(lb, s);
602 return FALSE;
603 }
604
605 static void launchtaskbar_constructor_add_default_special_case(LaunchTaskBarPlugin *ltbp, const gchar *tk_exec, const gchar *mb_exec)
606 {
607 g_key_file_set_value(ltbp->p_key_file_special_cases, "special_cases", tk_exec, mb_exec);
608 }
609
610 static void launchtaskbar_constructor_launch(LaunchTaskBarPlugin *ltbp, gboolean build_bootstrap)
611 {
612 config_setting_t *settings;
613
614 if(!ltbp->lb_built)
615 {
616 ltbp->lb_built = TRUE;
617 if(ltbp->lb_icon_grid == NULL)
618 {
619 /* Allocate an icon grid manager to manage the container. */
620 ltbp->lb_icon_grid = icon_grid_new(ltbp->panel, ltbp->p_evbox_launchbar,
621 panel_get_orientation(ltbp->panel),
622 ltbp->icon_size, ltbp->icon_size,
623 3, 0,
624 panel_get_height(ltbp->panel));
625 }
626 /* Read parameters from the configuration file. */
627 settings = config_setting_get_member(ltbp->settings, "");
628 if (settings && config_setting_is_list(settings))
629 {
630 config_setting_t *s;
631 guint i;
632
633 for (i = 0; (s = config_setting_get_elem(settings, i)) != NULL; )
634 {
635 if (strcmp(config_setting_get_name(s), "Button") != 0)
636 {
637 g_warning("launchtaskbar: illegal token %s\n", config_setting_get_name(s));
638 config_setting_destroy(s);
639 }
640 else if (!launchbutton_constructor(ltbp, s) &&
641 /* try to create desktop id from old-style manual setup */
642 !_launchbutton_create_id(ltbp, s))
643 {
644 g_warning( "launchtaskbar: can't init button\n");
645 /* FIXME: show failed id to the user instead */
646 config_setting_destroy(s);
647 }
648 else /* success, accept the setting */
649 i++;
650 }
651 }
652 if(build_bootstrap)
653 {
654 if(ltbp->buttons == NULL)
655 launchbutton_build_bootstrap(ltbp);
656 }
657 }
658 else
659 {
660 gtk_widget_set_visible(ltbp->p_evbox_launchbar, TRUE);
661 }
662 }
663
664 static void launchtaskbar_constructor_task(LaunchTaskBarPlugin *ltbp)
665 {
666 if(!ltbp->tb_built)
667 {
668 config_setting_t *s = ltbp->settings;
669 gint tmp_int;
670
671 ltbp->tb_built = TRUE;
672
673 /* Parse configuration now */
674 if (config_setting_lookup_int(s, "tooltips", &tmp_int))
675 ltbp->tooltips = (tmp_int != 0);
676 if (config_setting_lookup_int(s, "IconsOnly", &tmp_int))
677 ltbp->icons_only = (tmp_int != 0);
678 if (config_setting_lookup_int(s, "ShowAllDesks", &tmp_int))
679 ltbp->show_all_desks = (tmp_int != 0);
680 if (config_setting_lookup_int(s, "SameMonitorOnly", &tmp_int))
681 ltbp->same_monitor_only = (tmp_int != 0);
682 config_setting_lookup_int(s, "MaxTaskWidth", &ltbp->task_width_max);
683 config_setting_lookup_int(s, "spacing", &ltbp->spacing);
684 if (config_setting_lookup_int(s, "UseMouseWheel", &tmp_int))
685 ltbp->use_mouse_wheel = (tmp_int != 0);
686 if (config_setting_lookup_int(s, "UseUrgencyHint", &tmp_int))
687 ltbp->use_urgency_hint = (tmp_int != 0);
688 if (config_setting_lookup_int(s, "FlatButton", &tmp_int))
689 ltbp->flat_button = (tmp_int != 0);
690 if (config_setting_lookup_int(s, "GroupedTasks", &tmp_int))
691 ltbp->grouped_tasks = (tmp_int != 0);
692
693 /* Make container for task buttons as a child of top level widget. */
694 ltbp->tb_icon_grid = icon_grid_new(ltbp->panel, ltbp->p_evbox_taskbar,
695 panel_get_orientation(ltbp->panel),
696 ltbp->task_width_max, ltbp->icon_size,
697 ltbp->spacing, 0,
698 panel_get_height(ltbp->panel));
699 icon_grid_set_constrain_width(ltbp->tb_icon_grid, TRUE);
700 taskbar_update_style(ltbp);
701
702 /* Add GDK event filter. */
703 gdk_window_add_filter(NULL, (GdkFilterFunc) taskbar_event_filter, ltbp);
704
705 /* Connect signals to receive root window events and initialize root window properties. */
706 ltbp->number_of_desktops = get_net_number_of_desktops();
707 ltbp->current_desktop = get_net_current_desktop();
708 g_signal_connect(G_OBJECT(fbev), "current-desktop", G_CALLBACK(taskbar_net_current_desktop), (gpointer) ltbp);
709 g_signal_connect(G_OBJECT(fbev), "active-window", G_CALLBACK(taskbar_net_active_window), (gpointer) ltbp);
710 g_signal_connect(G_OBJECT(fbev), "number-of-desktops", G_CALLBACK(taskbar_net_number_of_desktops), (gpointer) ltbp);
711 g_signal_connect(G_OBJECT(fbev), "client-list", G_CALLBACK(taskbar_net_client_list), (gpointer) ltbp);
712
713 /* Make right-click menu for task buttons.
714 * It is retained for the life of the taskbar and will be shown as needed.
715 * Number of desktops and edge is needed for this operation. */
716 taskbar_make_menu(ltbp);
717
718 /* Connect a signal to be notified when the window manager changes. This causes re-evaluation of the "use_net_active" status. */
719 g_signal_connect(gtk_widget_get_screen(ltbp->p_evbox_taskbar), "window-manager-changed", G_CALLBACK(taskbar_window_manager_changed), ltbp);
720
721 /* Fetch the client list and redraw the taskbar. Then determine what window has focus. */
722 taskbar_net_client_list(NULL, ltbp);
723 taskbar_net_active_window(NULL, ltbp);
724 }
725 else
726 {
727 gtk_widget_set_visible(ltbp->p_evbox_taskbar, TRUE);
728 }
729 }
730
731 /* Plugin constructor. */
732 static GtkWidget *launchtaskbar_constructor(Panel *panel, config_setting_t *settings)
733 {
734 GtkWidget *p;
735 LaunchTaskBarPlugin *ltbp;
736
737 gtk_rc_parse_string(launchtaskbar_rc);
738
739 /* Allocate plugin context and set into Plugin private data pointer. */
740 ltbp = g_new0(LaunchTaskBarPlugin, 1);
741 ltbp->panel = panel;
742 ltbp->settings = settings;
743
744 /* Initialize to defaults. */
745 ltbp->icon_size = panel_get_icon_size(panel);
746 ltbp->tooltips = TRUE;
747 ltbp->icons_only = FALSE;
748 ltbp->show_all_desks = TRUE;
749 ltbp->task_width_max = TASK_WIDTH_MAX;
750 ltbp->spacing = 1;
751 ltbp->use_mouse_wheel = TRUE;
752 ltbp->use_urgency_hint = TRUE;
753 ltbp->grouped_tasks = FALSE;
754
755 /* Special cases key file */
756 ltbp->p_key_file_special_cases = g_key_file_new();
757 gchar *special_cases_filepath = g_build_filename(g_get_user_config_dir(),
758 "lxpanel", "launchtaskbar.cfg", NULL);
759 if (!g_key_file_load_from_file(ltbp->p_key_file_special_cases,
760 special_cases_filepath,
761 G_KEY_FILE_KEEP_COMMENTS, NULL))
762 {
763 launchtaskbar_constructor_add_default_special_case(ltbp, "synaptic", "synaptic-pkexec");
764 launchtaskbar_constructor_add_default_special_case(ltbp, "soffice.bin", "libreoffice");
765 launchtaskbar_constructor_add_default_special_case(ltbp, "x-terminal-emulator", "lxterminal");
766 gchar *key_file_data = g_key_file_to_data(ltbp->p_key_file_special_cases, NULL, NULL);
767 g_file_set_contents(special_cases_filepath, key_file_data, -1, NULL);
768 g_free(key_file_data);
769 }
770 g_free(special_cases_filepath);
771
772 /* Allocate top level widget and set into Plugin widget pointer. */
773 ltbp->plugin = p = panel_box_new(panel, FALSE, 5);
774 lxpanel_plugin_set_data(p, ltbp, launchtaskbar_destructor);
775 ltbp->p_evbox_launchbar = gtk_event_box_new();
776 ltbp->p_evbox_taskbar = gtk_event_box_new();
777 gtk_box_pack_start(GTK_BOX(p), ltbp->p_evbox_launchbar, FALSE, TRUE, 0);
778 gtk_box_pack_start(GTK_BOX(p), ltbp->p_evbox_taskbar, TRUE, TRUE, 0);
779
780 gtk_container_set_border_width(GTK_CONTAINER(p), 0);
781 gtk_container_set_border_width(GTK_CONTAINER(ltbp->p_evbox_launchbar), 0);
782 gtk_container_set_border_width(GTK_CONTAINER(ltbp->p_evbox_taskbar), 0);
783 ltbp->lb_on = TRUE;
784 ltbp->lb_built = FALSE;
785 ltbp->tb_on = TRUE;
786 ltbp->tb_built = FALSE;
787 #if GTK_CHECK_VERSION(2,18,0)
788 gtk_widget_set_has_window(p, FALSE);
789 #else
790 GTK_WIDGET_SET_FLAGS(p, GTK_NO_WINDOW);
791 #endif
792
793 /* Read parameters from the configuration file. */
794 config_setting_lookup_int(settings, "LaunchBarON", &ltbp->lb_on);
795 config_setting_lookup_int(settings, "TaskBarON", &ltbp->lb_on);
796
797 if(ltbp->lb_on) launchtaskbar_constructor_launch(ltbp, TRUE/*build_bootstrap*/);
798 if(ltbp->tb_on) launchtaskbar_constructor_task(ltbp);
799
800 return p;
801 }
802
803 static void launchtaskbar_destructor_launch(LaunchTaskBarPlugin *ltbp)
804 {
805 /* Free the launchbar. */
806 g_slist_foreach(ltbp->buttons, (GFunc) launchbutton_free, NULL);
807 icon_grid_free(ltbp->lb_icon_grid);
808 ltbp->lb_icon_grid = NULL;
809
810 /* Free the bootstrap button if it exists. */
811 if(ltbp->bootstrap_button != NULL)
812 {
813 launchbutton_free(ltbp->bootstrap_button);
814 ltbp->bootstrap_button = NULL;
815 }
816
817 if (ltbp->add_icon != NULL)
818 g_object_unref(ltbp->add_icon);
819 }
820
821 static void launchtaskbar_destructor_task(LaunchTaskBarPlugin *ltbp)
822 {
823 /* Remove GDK event filter. */
824 gdk_window_remove_filter(NULL, (GdkFilterFunc) taskbar_event_filter, ltbp);
825
826 /* Remove root window signal handlers. */
827 g_signal_handlers_disconnect_by_func(fbev, taskbar_net_current_desktop, ltbp);
828 g_signal_handlers_disconnect_by_func(fbev, taskbar_net_active_window, ltbp);
829 g_signal_handlers_disconnect_by_func(fbev, taskbar_net_number_of_desktops, ltbp);
830 g_signal_handlers_disconnect_by_func(fbev, taskbar_net_client_list, ltbp);
831
832 /* Remove "window-manager-changed" handler. */
833 g_signal_handlers_disconnect_by_func(gtk_widget_get_screen(ltbp->p_evbox_taskbar), taskbar_window_manager_changed, ltbp);
834
835 /* Deallocate task list. */
836 while(ltbp->p_task_list != NULL)
837 task_delete(ltbp, ltbp->p_task_list, TRUE);
838
839 /* Deallocate class list. */
840 while(ltbp->p_taskclass_list != NULL)
841 {
842 TaskClass * tc = ltbp->p_taskclass_list;
843 ltbp->p_taskclass_list = tc->p_taskclass_flink;
844 g_free(tc->res_class);
845 g_free(tc);
846 }
847
848 /* Deallocate other memory. */
849 gtk_widget_destroy(ltbp->menu);
850 icon_grid_free(ltbp->tb_icon_grid);
851 }
852
853 /* Plugin destructor. */
854 static void launchtaskbar_destructor(gpointer user_data)
855 {
856 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)user_data;
857
858 // TASKBAR
859 if(ltbp->tb_built) launchtaskbar_destructor_task(ltbp);
860
861 // LAUNCHBAR
862 if(ltbp->lb_built) launchtaskbar_destructor_launch(ltbp);
863
864 // LAUNCHTASKBAR
865
866 /* Deallocate all memory. */
867 if (ltbp->p_key_file_special_cases != NULL)
868 g_key_file_free(ltbp->p_key_file_special_cases);
869 g_free(ltbp);
870 }
871
872 /* Handler for "clicked" action on launchtaskbar configuration dialog "Add" button. */
873 static void launchbar_configure_add_button(GtkButton * widget, LaunchTaskBarPlugin *ltbp)
874 {
875 GtkTreeView * menu_view = GTK_TREE_VIEW(g_object_get_data(G_OBJECT(ltbp->config_dlg), "menu_view"));
876 GtkTreeView * defined_view = GTK_TREE_VIEW(g_object_get_data(G_OBJECT(ltbp->config_dlg), "defined_view"));
877 FmPath * sel_path = fm_app_menu_view_dup_selected_app_desktop_path(menu_view);
878 LaunchButton * btn;
879
880 if (sel_path != NULL && (btn = launchbutton_build_gui(ltbp, sel_path)) != NULL)
881 {
882 GtkListStore * list = GTK_LIST_STORE(gtk_tree_view_get_model(defined_view));
883 GtkTreeIter it;
884 GdkPixbuf* pix;
885 char *path;
886 gtk_list_store_append(list, &it);
887 pix = fm_pixbuf_from_icon(fm_file_info_get_icon(btn->fi), PANEL_ICON_SIZE);
888 gtk_list_store_set(list, &it,
889 COL_ICON, pix,
890 COL_TITLE, fm_file_info_get_disp_name(btn->fi),
891 COL_BTN, btn,
892 -1);
893 g_object_unref(pix);
894 path = fm_path_to_str(sel_path);
895 /* g_debug("*** path '%s'",path); */
896 btn->settings = config_group_add_subgroup(ltbp->settings, "Button");
897 config_group_set_string(btn->settings, "id", path);
898 g_free(path);
899 fm_path_unref(sel_path);
900 }
901 }
902
903 static void launchbar_remove_button(LaunchTaskBarPlugin *ltbp, LaunchButton *btn)
904 {
905 icon_grid_remove(ltbp->lb_icon_grid, btn->widget);
906 ltbp->buttons = g_slist_remove(ltbp->buttons, btn);
907 config_setting_destroy(btn->settings);
908 launchbutton_free(btn);
909 /* Put the bootstrap button back if the list becomes empty. */
910 if(ltbp->buttons == NULL)
911 launchbutton_build_bootstrap(ltbp);
912 }
913
914 /* Handler for "clicked" action on launchtaskbar configuration dialog "Remove" button. */
915 static void launchbar_configure_remove_button(GtkButton * widget, LaunchTaskBarPlugin *ltbp)
916 {
917 GtkTreeView * defined_view = GTK_TREE_VIEW(g_object_get_data(G_OBJECT(ltbp->config_dlg), "defined_view"));
918 GtkTreeModel * list;
919 GtkTreeIter it;
920 if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(defined_view), &list, &it))
921 {
922 LaunchButton * btn;
923 gtk_tree_model_get(list, &it, COL_BTN, &btn, -1);
924
925 /* We have found a selected button.
926 * Remove it from the icon grid, the data structure, and the view. */
927 gtk_list_store_remove(GTK_LIST_STORE(list), &it);
928 gtk_widget_set_visible(ltbp->p_label_def_app_exec, FALSE);
929
930 launchbar_remove_button(ltbp, btn);
931 }
932 }
933
934 /* Handler for "clicked" action on launchtaskbar configuration dialog "Move Up" button. */
935 static void launchbar_configure_move_up_button(GtkButton * widget, LaunchTaskBarPlugin *ltbp)
936 {
937 GtkTreeView * defined_view = GTK_TREE_VIEW(g_object_get_data(G_OBJECT(ltbp->config_dlg), "defined_view"));
938 GtkTreeModel * list;
939 GtkTreeIter it;
940 if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(defined_view), &list, &it))
941 {
942 LaunchButton *btn;
943 gtk_tree_model_get(GTK_TREE_MODEL(list), &it, COL_BTN, &btn, -1);
944 GtkTreePath * path = gtk_tree_model_get_path(GTK_TREE_MODEL(list), &it);
945 if ((gtk_tree_path_get_indices(path)[0] > 0)
946 && (gtk_tree_path_prev(path)))
947 {
948 GtkTreeIter it2;
949 if (gtk_tree_model_get_iter(list, &it2, path))
950 {
951 /* We have found a selected button that can be moved.
952 * Reorder it in the icon grid, the data structure, and the view. */
953 int i = gtk_tree_path_get_indices(path)[0];
954 ltbp->buttons = g_slist_remove(ltbp->buttons, btn);
955 ltbp->buttons = g_slist_insert(ltbp->buttons, btn, i);
956 gtk_list_store_move_before(GTK_LIST_STORE(list), &it, &it2);
957 icon_grid_reorder_child(ltbp->lb_icon_grid, btn->widget, i);
958 config_setting_move_elem(btn->settings,
959 config_setting_get_parent(btn->settings),
960 i);
961 }
962 }
963 gtk_tree_path_free(path);
964 }
965 }
966
967 /* Handler for "clicked" action on launchtaskbar configuration dialog "Move Down" button. */
968 static void launchbar_configure_move_down_button(GtkButton * widget, LaunchTaskBarPlugin *ltbp)
969 {
970 GtkTreeView * defined_view = GTK_TREE_VIEW(g_object_get_data(G_OBJECT(ltbp->config_dlg), "defined_view"));
971 GtkTreeModel * list;
972 GtkTreeIter it;
973 if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(defined_view), &list, &it))
974 {
975 LaunchButton *btn;
976 gtk_tree_model_get(GTK_TREE_MODEL(list), &it, COL_BTN, &btn, -1);
977 GtkTreePath * path = gtk_tree_model_get_path(GTK_TREE_MODEL(list), &it);
978 int n = gtk_tree_model_iter_n_children(list, NULL);
979 if (gtk_tree_path_get_indices(path)[0] < (n - 1))
980 {
981 gtk_tree_path_next(path);
982 GtkTreeIter it2;
983 if (gtk_tree_model_get_iter( list, &it2, path))
984 {
985 /* We have found a selected button that can be moved.
986 * Reorder it in the icon grid, the data structure, and the view. */
987 int i = gtk_tree_path_get_indices(path)[0];
988 ltbp->buttons = g_slist_remove(ltbp->buttons, btn);
989 ltbp->buttons = g_slist_insert(ltbp->buttons, btn, i + 1);
990 gtk_list_store_move_after(GTK_LIST_STORE(list), &it, &it2);
991 icon_grid_reorder_child( ltbp->lb_icon_grid, btn->widget, i);
992 config_setting_move_elem(btn->settings,
993 config_setting_get_parent(btn->settings),
994 i);
995 }
996 }
997 gtk_tree_path_free(path);
998 }
999 }
1000
1001 /* Initialize the list of existing launchtaskbar buttons when the configuration dialog is shown. */
1002 static void launchbar_configure_initialize_list(LaunchTaskBarPlugin *ltbp, GtkWidget * dlg, GtkTreeView * view)
1003 {
1004 /* Set the selection mode. */
1005 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(view), GTK_SELECTION_BROWSE);
1006
1007 /* Define a column. */
1008 GtkTreeViewColumn* col = gtk_tree_view_get_column(view, 0);
1009
1010 /* Establish the pixbuf column cell renderer. */
1011 GtkCellRenderer * render = gtk_cell_renderer_pixbuf_new();
1012 gtk_tree_view_column_pack_start(col, render, FALSE);
1013 gtk_tree_view_column_set_attributes(col, render, "pixbuf", COL_ICON, NULL);
1014
1015 /* Establish the text column cell renderer. */
1016 render = gtk_cell_renderer_text_new();
1017 gtk_tree_view_column_pack_start(col, render, TRUE);
1018 gtk_tree_view_column_add_attribute(col, render, "text", COL_TITLE);
1019
1020 /* Establish the column data types. */
1021 GtkListStore* list = GTK_LIST_STORE(gtk_tree_view_get_model(view));
1022
1023 /* Initialize from defined launchtaskbar buttons. */
1024 GSList* l;
1025 for (l = ltbp->buttons; l != NULL; l = l->next)
1026 {
1027 LaunchButton * btn = (LaunchButton *) l->data;
1028 GdkPixbuf * pix;
1029 GtkTreeIter it;
1030 gtk_list_store_append(list, &it);
1031 pix = fm_pixbuf_from_icon(fm_file_info_get_icon(btn->fi), PANEL_ICON_SIZE);
1032 gtk_list_store_set(list, &it,
1033 COL_ICON, pix,
1034 COL_TITLE, fm_file_info_get_disp_name(btn->fi),
1035 COL_BTN, btn,
1036 -1);
1037 g_object_unref(pix);
1038 }
1039 g_object_set_data(G_OBJECT(dlg), "defined_view", view);
1040 }
1041
1042 static void plugin_set_expand_status(LaunchTaskBarPlugin *ltbp, gboolean expand_new)
1043 {
1044 gboolean old_expand, fill;
1045 guint padding;
1046 GtkPackType pack_type;
1047 GtkWidget *box = gtk_widget_get_parent(ltbp->plugin);
1048 g_return_if_fail(box);
1049 gtk_box_query_child_packing(GTK_BOX(box), ltbp->plugin, &old_expand, &fill, &padding, &pack_type);
1050 gtk_box_set_child_packing(GTK_BOX(box), ltbp->plugin, expand_new, fill, padding, pack_type);
1051 }
1052
1053 static void on_radiobutton_launch_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1054 {
1055 if(!gtk_toggle_button_get_active(p_togglebutton)) return;
1056 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1057
1058 if(!ltbp->lb_on)
1059 {
1060 ltbp->lb_on = TRUE;
1061 launchtaskbar_constructor_launch(ltbp, TRUE/*build_bootstrap*/);
1062 }
1063 if(ltbp->tb_on)
1064 {
1065 ltbp->tb_on = FALSE;
1066 gtk_widget_set_visible(ltbp->p_evbox_taskbar, FALSE);
1067 }
1068 gtk_widget_set_visible(ltbp->p_notebook_page_launch, TRUE);
1069 gtk_widget_set_visible(ltbp->p_notebook_page_task, FALSE);
1070 config_group_set_int(ltbp->settings, "LaunchBarON", ltbp->lb_on);
1071
1072 plugin_set_expand_status(ltbp, FALSE);
1073 }
1074
1075 static void on_radiobutton_task_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1076 {
1077 if(!gtk_toggle_button_get_active(p_togglebutton)) return;
1078 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1079
1080 if(ltbp->lb_on)
1081 {
1082 ltbp->lb_on = FALSE;
1083 gtk_widget_set_visible(ltbp->p_evbox_launchbar, FALSE);
1084 }
1085 if(!ltbp->tb_on)
1086 {
1087 ltbp->tb_on = TRUE;
1088 launchtaskbar_constructor_task(ltbp);
1089 }
1090 gtk_widget_set_visible(ltbp->p_notebook_page_launch, FALSE);
1091 gtk_widget_set_visible(ltbp->p_notebook_page_task, TRUE);
1092 config_group_set_int(ltbp->settings, "TaskBarON", ltbp->tb_on);
1093
1094 plugin_set_expand_status(ltbp, TRUE);
1095 }
1096
1097 static void on_radiobutton_launchtask_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1098 {
1099 if(!gtk_toggle_button_get_active(p_togglebutton)) return;
1100 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1101
1102 if(!ltbp->lb_on)
1103 {
1104 ltbp->lb_on = TRUE;
1105 launchtaskbar_constructor_launch(ltbp, TRUE/*build_bootstrap*/);
1106 }
1107 if(!ltbp->tb_on)
1108 {
1109 ltbp->tb_on = TRUE;
1110 launchtaskbar_constructor_task(ltbp);
1111 }
1112 gtk_widget_set_visible(ltbp->p_notebook_page_launch, TRUE);
1113 gtk_widget_set_visible(ltbp->p_notebook_page_task, TRUE);
1114 config_group_set_int(ltbp->settings, "LaunchBarON", 1);
1115 config_group_set_int(ltbp->settings, "TaskBarON", 1);
1116
1117 plugin_set_expand_status(ltbp, TRUE);
1118 }
1119
1120 static void on_checkbutton_show_tooltips_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1121 {
1122 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1123 ltbp->tooltips = gtk_toggle_button_get_active(p_togglebutton);
1124 //g_print("\nltbp->tooltips upd\n");
1125 config_group_set_int(ltbp->settings, "tooltips", ltbp->tooltips);
1126 taskbar_apply_configuration(ltbp);
1127 }
1128
1129 static void on_checkbutton_icons_only_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1130 {
1131 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1132 ltbp->icons_only = gtk_toggle_button_get_active(p_togglebutton);
1133 //g_print("\ntb->icons_only upd\n");
1134 config_group_set_int(ltbp->settings, "IconsOnly", ltbp->icons_only);
1135 taskbar_apply_configuration(ltbp);
1136 }
1137
1138 static void on_checkbutton_flat_buttons_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1139 {
1140 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1141 ltbp->flat_button = gtk_toggle_button_get_active(p_togglebutton);
1142 //g_print("\ntb->flat_button upd\n");
1143 config_group_set_int(ltbp->settings, "FlatButton", ltbp->flat_button);
1144 taskbar_apply_configuration(ltbp);
1145 }
1146
1147 static void on_checkbutton_show_all_desks_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1148 {
1149 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1150 ltbp->show_all_desks = gtk_toggle_button_get_active(p_togglebutton);
1151 //g_print("\ntb->show_all_desks upd\n");
1152 config_group_set_int(ltbp->settings, "ShowAllDesks", ltbp->show_all_desks);
1153 taskbar_apply_configuration(ltbp);
1154 }
1155
1156 static void on_checkbutton_same_monitor_only_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1157 {
1158 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1159 ltbp->same_monitor_only = gtk_toggle_button_get_active(p_togglebutton);
1160 //g_print("\ntb->same_monitor_only upd\n");
1161 config_group_set_int(ltbp->settings, "SameMonitorOnly", ltbp->same_monitor_only);
1162 taskbar_apply_configuration(ltbp);
1163 }
1164
1165 static void on_checkbutton_mouse_wheel_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1166 {
1167 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1168 ltbp->use_mouse_wheel = gtk_toggle_button_get_active(p_togglebutton);
1169 //g_print("\ntb->use_mouse_wheel upd\n");
1170 config_group_set_int(ltbp->settings, "UseMouseWheel", ltbp->use_mouse_wheel);
1171 taskbar_apply_configuration(ltbp);
1172 }
1173
1174 static void on_checkbutton_urgency_hint_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1175 {
1176 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1177 ltbp->use_urgency_hint = gtk_toggle_button_get_active(p_togglebutton);
1178 //g_print("\ntb->use_urgency_hint upd\n");
1179 config_group_set_int(ltbp->settings, "UseUrgencyHint", ltbp->use_urgency_hint);
1180 taskbar_apply_configuration(ltbp);
1181 }
1182
1183 static void on_checkbutton_grouped_tasks_toggled(GtkToggleButton *p_togglebutton, gpointer p_data)
1184 {
1185 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1186 ltbp->grouped_tasks = gtk_toggle_button_get_active(p_togglebutton);
1187 //g_print("\ntb->grouped_tasks upd\n");
1188 config_group_set_int(ltbp->settings, "GroupedTasks", ltbp->grouped_tasks);
1189 taskbar_apply_configuration(ltbp);
1190 }
1191
1192 static void on_spinbutton_max_width_value_changed(GtkSpinButton *p_spinbutton, gpointer p_data)
1193 {
1194 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1195 ltbp->task_width_max = gtk_spin_button_get_value(p_spinbutton);
1196 //g_print("\ntb->task_width_max upd\n");
1197 config_group_set_int(ltbp->settings, "MaxTaskWidth", ltbp->task_width_max);
1198 taskbar_apply_configuration(ltbp);
1199 }
1200
1201 static void on_spinbutton_spacing_value_changed(GtkSpinButton *p_spinbutton, gpointer p_data)
1202 {
1203 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)p_data;
1204 ltbp->spacing = gtk_spin_button_get_value(p_spinbutton);
1205 //g_print("\ntb->spacing upd\n");
1206 config_group_set_int(ltbp->settings, "spacing", ltbp->spacing);
1207 taskbar_apply_configuration(ltbp);
1208 }
1209
1210 static gboolean on_defined_view_button_press_event(GtkWidget *p_widget, GdkEventButton *p_event, gpointer p_data)
1211 {
1212 LaunchTaskBarPlugin *lb = (LaunchTaskBarPlugin *)p_data;
1213 if(p_event->button == 1)
1214 {
1215 if(p_event->type == GDK_2BUTTON_PRESS)
1216 {
1217 gtk_button_clicked(GTK_BUTTON(lb->p_button_remove));
1218 }
1219 }
1220 return FALSE;
1221 }
1222
1223 static void on_defined_view_cursor_changed(GtkTreeView *p_treeview, gpointer p_data)
1224 {
1225 gboolean label_set = FALSE;
1226 LaunchTaskBarPlugin *lb = (LaunchTaskBarPlugin *)p_data;
1227 GtkTreeIter tree_iter_sel;
1228 GtkTreeModel* p_treemodel = gtk_tree_view_get_model(p_treeview);
1229 GtkTreeSelection *p_treeselection = gtk_tree_view_get_selection(p_treeview);
1230 if(gtk_tree_selection_get_selected(p_treeselection,
1231 (GtkTreeModel **)(&p_treemodel),
1232 &tree_iter_sel))
1233 {
1234 LaunchButton * p_btn;
1235 gtk_tree_model_get(p_treemodel, &tree_iter_sel, COL_BTN, &p_btn, -1);
1236 if( (p_btn != NULL) && (p_btn->fi != NULL) )
1237 {
1238 GString *p_gstring = g_string_new("");
1239 g_string_printf(p_gstring, "<i>%s</i>", fm_file_info_get_disp_name(p_btn->fi));
1240 gtk_label_set_markup(GTK_LABEL(lb->p_label_def_app_exec), p_gstring->str);
1241 g_string_free(p_gstring, TRUE/*free also gstring->str*/);
1242 label_set = TRUE;
1243 }
1244 }
1245 gtk_widget_set_visible(lb->p_label_def_app_exec, label_set);
1246 gtk_widget_set_sensitive(lb->p_button_remove, label_set);
1247 }
1248
1249 static void on_menu_view_cursor_changed(GtkTreeView *p_treeview, gpointer p_data)
1250 {
1251 gboolean label_set = FALSE;
1252 LaunchTaskBarPlugin *lb = (LaunchTaskBarPlugin *)p_data;
1253 GAppInfo *app = fm_app_menu_view_dup_selected_app(p_treeview);
1254
1255 if (app)
1256 {
1257 GString *p_gstring = g_string_new("");
1258 if (g_app_info_get_description(app))
1259 g_string_printf(p_gstring, "<i>%s</i>", g_app_info_get_description(app));
1260 else
1261 g_string_printf(p_gstring, "<i>%s</i>", g_app_info_get_name(app));
1262 gtk_label_set_markup(GTK_LABEL(lb->p_label_menu_app_exec), p_gstring->str);
1263 g_string_free(p_gstring, TRUE/*free also gstring->str*/);
1264 label_set = TRUE;
1265 }
1266 gtk_widget_set_visible(lb->p_label_menu_app_exec, label_set);
1267 gtk_widget_set_sensitive(lb->p_button_add, label_set);
1268 }
1269
1270 /* Callback when the configuration dialog is to be shown. */
1271 static GtkWidget *launchtaskbar_configure(Panel *panel, GtkWidget *p, GtkWindow *parent)
1272 {
1273 LaunchTaskBarPlugin *ltbp = lxpanel_plugin_get_data(p);
1274
1275 {
1276 GtkWidget *dlg, *btn, *defined_view, *menu_view, *menu_view_window;
1277 GtkBuilder *builder = gtk_builder_new();
1278
1279 gtk_builder_add_from_file(builder, PACKAGE_UI_DIR "/launchtaskbar.ui", NULL);
1280 dlg = (GtkWidget *)gtk_builder_get_object(builder, "dlg");
1281 panel_apply_icon(GTK_WINDOW(dlg));
1282
1283 defined_view = (GtkWidget *)gtk_builder_get_object(builder, "defined_view");
1284 menu_view_window = (GtkWidget*)gtk_builder_get_object(builder, "menu_view_window");
1285 if (menu_view_window == NULL) /* fallback for old glade file */
1286 {
1287 menu_view_window = (GtkWidget*)gtk_builder_get_object(builder, "scroll2");
1288 gtk_widget_destroy(gtk_bin_get_child(GTK_BIN(menu_view_window)));
1289 }
1290 menu_view = GTK_WIDGET(fm_app_menu_view_new());
1291 gtk_container_add(GTK_CONTAINER(menu_view_window), menu_view);
1292 gtk_widget_show(menu_view);
1293 ltbp->p_label_def_app_exec = (GtkWidget*)gtk_builder_get_object(builder, "label_def_app_exec");
1294 ltbp->p_label_menu_app_exec = (GtkWidget*)gtk_builder_get_object(builder, "label_menu_app_exec");
1295
1296 /* Connect signals. */
1297 ltbp->p_button_add = (GtkWidget *)gtk_builder_get_object(builder, "button_add");
1298 g_signal_connect(ltbp->p_button_add, "clicked", G_CALLBACK(launchbar_configure_add_button), ltbp);
1299
1300 ltbp->p_button_remove = (GtkWidget *)gtk_builder_get_object(builder, "button_remove");
1301 g_signal_connect(ltbp->p_button_remove, "clicked", G_CALLBACK(launchbar_configure_remove_button), ltbp);
1302
1303 btn = (GtkWidget *)gtk_builder_get_object(builder, "button_up");
1304 g_signal_connect(btn, "clicked", G_CALLBACK(launchbar_configure_move_up_button), ltbp);
1305
1306 btn = (GtkWidget *)gtk_builder_get_object(builder, "button_down");
1307 g_signal_connect(btn, "clicked", G_CALLBACK(launchbar_configure_move_down_button), ltbp);
1308
1309 g_signal_connect(defined_view, "button-press-event", G_CALLBACK(on_defined_view_button_press_event), ltbp);
1310 g_signal_connect(defined_view, "cursor-changed", G_CALLBACK(on_defined_view_cursor_changed), ltbp);
1311 g_signal_connect(menu_view, "cursor-changed", G_CALLBACK(on_menu_view_cursor_changed), ltbp);
1312
1313 GtkWidget *p_checkbutton_show_tooltips, *p_checkbutton_icons_only, *p_checkbutton_flat_buttons;
1314 GtkWidget *p_checkbutton_show_all_desks, *p_checkbutton_same_monitor_only;
1315 GtkWidget *p_checkbutton_mouse_wheel, *p_checkbutton_urgency_hint;
1316 GtkWidget *p_checkbutton_grouped_tasks, *p_spinbutton_max_width, *p_spinbutton_spacing;
1317 GtkWidget *p_notebook, *p_radiobutton_launch, *p_radiobutton_task, *p_radiobutton_launchtask;
1318
1319 p_notebook = (GtkWidget *)gtk_builder_get_object(builder, "notebook");
1320 ltbp->p_notebook_page_launch = gtk_notebook_get_nth_page(GTK_NOTEBOOK(p_notebook), 0);
1321 ltbp->p_notebook_page_task = gtk_notebook_get_nth_page(GTK_NOTEBOOK(p_notebook), 1);
1322 gtk_widget_set_visible(ltbp->p_notebook_page_launch, ltbp->lb_on);
1323 gtk_widget_set_visible(ltbp->p_notebook_page_task, ltbp->tb_on);
1324
1325 p_radiobutton_launch = (GtkWidget *)gtk_builder_get_object(builder, "radiobutton_launch");
1326 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_launch), !ltbp->tb_on);
1327 p_radiobutton_task = (GtkWidget *)gtk_builder_get_object(builder, "radiobutton_task");
1328 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_task), !ltbp->lb_on);
1329 p_radiobutton_launchtask = (GtkWidget *)gtk_builder_get_object(builder, "radiobutton_launchtask");
1330 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_launchtask), ltbp->lb_on && ltbp->tb_on);
1331 g_signal_connect(GTK_TOGGLE_BUTTON(p_radiobutton_launch), "toggled",
1332 G_CALLBACK(on_radiobutton_launch_toggled), ltbp);
1333 g_signal_connect(GTK_TOGGLE_BUTTON(p_radiobutton_task), "toggled",
1334 G_CALLBACK(on_radiobutton_task_toggled), ltbp);
1335 g_signal_connect(GTK_TOGGLE_BUTTON(p_radiobutton_launchtask), "toggled",
1336 G_CALLBACK(on_radiobutton_launchtask_toggled), ltbp);
1337
1338 p_checkbutton_show_tooltips = (GtkWidget *)gtk_builder_get_object(builder, "checkbutton_show_tooltips");
1339 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_show_tooltips), ltbp->tooltips);
1340 g_signal_connect(GTK_TOGGLE_BUTTON(p_checkbutton_show_tooltips), "toggled",
1341 G_CALLBACK(on_checkbutton_show_tooltips_toggled), ltbp);
1342
1343 p_checkbutton_icons_only = (GtkWidget *)gtk_builder_get_object(builder, "checkbutton_icons_only");
1344 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_icons_only), ltbp->icons_only);
1345 g_signal_connect(GTK_TOGGLE_BUTTON(p_checkbutton_icons_only), "toggled",
1346 G_CALLBACK(on_checkbutton_icons_only_toggled), ltbp);
1347
1348 p_checkbutton_flat_buttons = (GtkWidget *)gtk_builder_get_object(builder, "checkbutton_flat_buttons");
1349 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_flat_buttons), ltbp->flat_button);
1350 g_signal_connect(GTK_TOGGLE_BUTTON(p_checkbutton_flat_buttons), "toggled",
1351 G_CALLBACK(on_checkbutton_flat_buttons_toggled), ltbp);
1352
1353 p_checkbutton_show_all_desks = (GtkWidget *)gtk_builder_get_object(builder, "checkbutton_show_all_desks");
1354 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_show_all_desks), ltbp->show_all_desks);
1355 g_signal_connect(GTK_TOGGLE_BUTTON(p_checkbutton_show_all_desks), "toggled",
1356 G_CALLBACK(on_checkbutton_show_all_desks_toggled), ltbp);
1357
1358 p_checkbutton_same_monitor_only = (GtkWidget *)gtk_builder_get_object(builder, "checkbutton_same_monitor_only");
1359 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_same_monitor_only), ltbp->same_monitor_only);
1360 g_signal_connect(GTK_TOGGLE_BUTTON(p_checkbutton_same_monitor_only), "toggled",
1361 G_CALLBACK(on_checkbutton_same_monitor_only_toggled), ltbp);
1362
1363 p_checkbutton_mouse_wheel = (GtkWidget *)gtk_builder_get_object(builder, "checkbutton_mouse_wheel");
1364 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_mouse_wheel), ltbp->use_mouse_wheel);
1365 g_signal_connect(GTK_TOGGLE_BUTTON(p_checkbutton_mouse_wheel), "toggled",
1366 G_CALLBACK(on_checkbutton_mouse_wheel_toggled), ltbp);
1367
1368 p_checkbutton_urgency_hint = (GtkWidget *)gtk_builder_get_object(builder, "checkbutton_urgency_hint");
1369 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_urgency_hint), ltbp->use_urgency_hint);
1370 g_signal_connect(GTK_TOGGLE_BUTTON(p_checkbutton_urgency_hint), "toggled",
1371 G_CALLBACK(on_checkbutton_urgency_hint_toggled), ltbp);
1372
1373 p_checkbutton_grouped_tasks = (GtkWidget *)gtk_builder_get_object(builder, "checkbutton_grouped_tasks");
1374 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_grouped_tasks), ltbp->grouped_tasks);
1375 g_signal_connect(GTK_TOGGLE_BUTTON(p_checkbutton_grouped_tasks), "toggled",
1376 G_CALLBACK(on_checkbutton_grouped_tasks_toggled), ltbp);
1377
1378 p_spinbutton_max_width = (GtkWidget *)gtk_builder_get_object(builder, "spinbutton_max_width");
1379 gtk_spin_button_set_value(GTK_SPIN_BUTTON(p_spinbutton_max_width), ltbp->task_width_max);
1380 g_signal_connect(GTK_SPIN_BUTTON(p_spinbutton_max_width), "value-changed",
1381 G_CALLBACK(on_spinbutton_max_width_value_changed), ltbp);
1382
1383 p_spinbutton_spacing = (GtkWidget *)gtk_builder_get_object(builder, "spinbutton_spacing");
1384 gtk_spin_button_set_value(GTK_SPIN_BUTTON(p_spinbutton_spacing), ltbp->spacing);
1385 g_signal_connect(GTK_SPIN_BUTTON(p_spinbutton_spacing), "value-changed",
1386 G_CALLBACK(on_spinbutton_spacing_value_changed), ltbp);
1387
1388 ltbp->config_dlg = dlg;
1389
1390 /* Initialize the tree view contents. */
1391 launchbar_configure_initialize_list(ltbp, dlg, GTK_TREE_VIEW(defined_view));
1392 g_object_set_data(G_OBJECT(dlg), "menu_view", menu_view);
1393
1394 gtk_widget_set_visible(ltbp->p_label_menu_app_exec, FALSE);
1395 gtk_widget_set_visible(ltbp->p_label_def_app_exec, FALSE);
1396 gtk_widget_set_sensitive(ltbp->p_button_add, FALSE);
1397 gtk_widget_set_sensitive(ltbp->p_button_remove, FALSE);
1398
1399 g_object_unref(builder);
1400 }
1401 return ltbp->config_dlg;
1402 }
1403
1404 /* Callback when panel configuration changes. */
1405 static void launchtaskbar_panel_configuration_changed(Panel *panel, GtkWidget *p)
1406 {
1407 /* Set orientation into the icon grid. */
1408 LaunchTaskBarPlugin *ltbp = lxpanel_plugin_get_data(p);
1409 int new_icon_size = panel_get_icon_size(panel);
1410
1411 icon_grid_set_geometry(ltbp->lb_icon_grid, panel_get_orientation(panel),
1412 new_icon_size, new_icon_size, 3, 0,
1413 panel_get_height(panel));
1414
1415 /* Reset the bootstrap button. */
1416 if (ltbp->bootstrap_button != NULL)
1417 {
1418 GdkPixbuf * icon = fm_pixbuf_from_icon(ltbp->add_icon, new_icon_size);
1419 gtk_image_set_from_pixbuf(GTK_IMAGE(ltbp->bootstrap_button->image_widget), icon);
1420 g_object_unref(icon);
1421 }
1422
1423 taskbar_update_style(ltbp);
1424 taskbar_make_menu(ltbp);
1425
1426 /* If the icon size changed, refetch all the icons. */
1427 if (new_icon_size != ltbp->icon_size)
1428 {
1429 Task * tk;
1430 GSList * l;
1431 ltbp->icon_size = new_icon_size;
1432 for (tk = ltbp->p_task_list; tk != NULL; tk = tk->p_task_flink_xwid)
1433 {
1434 GdkPixbuf * pixbuf = task_update_icon(ltbp, tk, None);
1435 if (pixbuf != NULL)
1436 {
1437 gtk_image_set_from_pixbuf(GTK_IMAGE(tk->image), pixbuf);
1438 g_object_unref(pixbuf);
1439 }
1440 }
1441 for (l = ltbp->buttons; l != NULL; l = l->next)
1442 {
1443 LaunchButton * btn = (LaunchButton *) l->data;
1444 lxpanel_button_update_icon(btn->widget, fm_file_info_get_icon(btn->fi),
1445 new_icon_size);
1446 }
1447
1448 }
1449
1450 /* Redraw all the labels. Icon size or font color may have changed. */
1451 taskbar_redraw(ltbp);
1452 }
1453
1454 /* Set an urgency timer on a task. */
1455 static void set_timer_on_task(Task * tk)
1456 {
1457 gint interval;
1458 g_return_if_fail(tk->flash_timeout == 0);
1459 g_object_get(gtk_widget_get_settings(tk->button), "gtk-cursor-blink-time", &interval, NULL);
1460 tk->flash_timeout = g_timeout_add(interval, (GSourceFunc) flash_window_timeout, tk);
1461 }
1462
1463 /* Determine if a task is visible considering only its desktop placement. */
1464 static gboolean task_is_visible_on_current_desktop(LaunchTaskBarPlugin * tb, Task * tk)
1465 {
1466 return ((tk->desktop == ALL_WORKSPACES) || (tk->desktop == tb->current_desktop) || (tb->show_all_desks));
1467 }
1468
1469 /* Recompute the visible task for a class when the class membership changes.
1470 * Also transfer the urgency state to the visible task if necessary. */
1471 static void recompute_group_visibility_for_class(LaunchTaskBarPlugin * tb, TaskClass * tc)
1472 {
1473 tc->visible_count = 0;
1474 tc->p_task_visible = NULL;
1475 tc->visible_name = NULL;
1476 Task * flashing_task = NULL;
1477 gboolean class_has_urgency = FALSE;
1478 Task * tk;
1479 for (tk = tc->p_task_head; tk != NULL; tk = tk->p_task_flink_same_class)
1480 {
1481 if (task_is_visible_on_current_desktop(tb, tk))
1482 {
1483 /* Count visible tasks and make the first visible task the one that is used for display. */
1484 if (tc->visible_count == 0)
1485 tc->p_task_visible = tk;
1486 tc->visible_count += 1;
1487
1488 /* Compute summary bit for urgency anywhere in the class. */
1489 if (tk->urgency)
1490 class_has_urgency = TRUE;
1491
1492 /* If there is urgency, record the currently flashing task. */
1493 if (tk->flash_timeout != 0)
1494 flashing_task = tk;
1495
1496 /* Compute the visible name. If all visible windows have the same title, use that.
1497 * Otherwise, use the class name. This follows WNCK.
1498 * Note that the visible name is not a separate string, but is set to point to one of the others. */
1499 if (tc->visible_name == NULL)
1500 tc->visible_name = tk->name;
1501 else if ((tc->visible_name != tc->res_class)
1502 && (tc->visible_name != NULL) && (tk->name != NULL)
1503 && (strcmp(tc->visible_name, tk->name) != 0))
1504 tc->visible_name = tc->res_class;
1505 }
1506 }
1507
1508 /* Transfer the flash timeout to the visible task. */
1509 if (class_has_urgency)
1510 {
1511 if (flashing_task == NULL)
1512 {
1513 /* Set the flashing context and flash the window immediately. */
1514 tc->p_task_visible->flash_state = TRUE;
1515 flash_window_timeout(tc->p_task_visible);
1516
1517 /* Set the timer, since none is set. */
1518 set_timer_on_task(tc->p_task_visible);
1519 }
1520 else if (flashing_task != tc->p_task_visible)
1521 {
1522 /* Reset the timer on the new representative.
1523 * There will be a slight hiccup on the flash cadence. */
1524 g_source_remove(flashing_task->flash_timeout);
1525 flashing_task->flash_timeout = 0;
1526 tc->p_task_visible->flash_state = flashing_task->flash_state;
1527 flashing_task->flash_state = FALSE;
1528 set_timer_on_task(tc->p_task_visible);
1529 }
1530 }
1531 else
1532 {
1533 /* No task has urgency. Cancel the timer if one is set. */
1534 if (flashing_task != NULL)
1535 {
1536 g_source_remove(flashing_task->flash_timeout);
1537 flashing_task->flash_state = FALSE;
1538 flashing_task->flash_timeout = 0;
1539 }
1540 }
1541 }
1542
1543 /* Recompute the visible task for all classes when the desktop changes. */
1544 static void recompute_group_visibility_on_current_desktop(LaunchTaskBarPlugin * tb)
1545 {
1546 TaskClass * tc;
1547 for (tc = tb->p_taskclass_list; tc != NULL; tc = tc->p_taskclass_flink)
1548 {
1549 recompute_group_visibility_for_class(tb, tc);
1550 }
1551 }
1552
1553 /* Draw the label and tooltip on a taskbar button. */
1554 static void task_draw_label(Task * tk)
1555 {
1556 TaskClass * tc = tk->p_taskclass;
1557 gboolean bold_style = (((tk->entered_state) || (tk->flash_state)) && (tk->tb->flat_button));
1558 char *label;
1559
1560 if ((tk->tb->grouped_tasks) && (tc != NULL) && (tc->p_task_visible == tk) && (tc->visible_count > 1))
1561 {
1562 label = g_strdup_printf("(%d) %s", tc->visible_count, tc->visible_name);
1563 }
1564 else
1565 {
1566 label = g_strdup(tk->iconified ? tk->name_iconified : tk->name);
1567 }
1568
1569 if (tk->tb->tooltips)
1570 gtk_widget_set_tooltip_text(tk->button, label);
1571
1572 panel_draw_label_text(tk->tb->panel, tk->label, label, bold_style, 1,
1573 tk->tb->flat_button);
1574
1575 g_free(label);
1576 }
1577
1578 /* Determine if a task is visible. */
1579 static gboolean task_is_visible(LaunchTaskBarPlugin * tb, Task * tk)
1580 {
1581 /* Not visible due to grouping. */
1582 if ((tb->grouped_tasks) && (tk->p_taskclass != NULL) && (tk->p_taskclass->p_task_visible != tk))
1583 return FALSE;
1584
1585 /* Not on same monitor */
1586 if (tb->same_monitor_only && panel_get_monitor(tb->panel) != tk->monitor)
1587 return FALSE;
1588
1589 /* Desktop placement. */
1590 return task_is_visible_on_current_desktop(tb, tk);
1591 }
1592
1593 /* Redraw a task button. */
1594 static void task_button_redraw(Task * tk, LaunchTaskBarPlugin * tb)
1595 {
1596 if (task_is_visible(tb, tk))
1597 {
1598 task_draw_label(tk);
1599 icon_grid_set_visible(tb->tb_icon_grid, tk->button, TRUE);
1600 }
1601 else
1602 icon_grid_set_visible(tb->tb_icon_grid, tk->button, FALSE);
1603 }
1604
1605 /* Redraw all tasks in the taskbar. */
1606 static void taskbar_redraw(LaunchTaskBarPlugin * tb)
1607 {
1608 Task * tk;
1609 for (tk = tb->p_task_list; tk != NULL; tk = tk->p_task_flink_xwid)
1610 task_button_redraw(tk, tb);
1611 }
1612
1613 /* Determine if a task should be visible given its NET_WM_STATE. */
1614 static gboolean accept_net_wm_state(NetWMState * nws)
1615 {
1616 return ( ! (nws->skip_taskbar));
1617 }
1618
1619 /* Determine if a task should be visible given its NET_WM_WINDOW_TYPE. */
1620 static gboolean accept_net_wm_window_type(NetWMWindowType * nwwt)
1621 {
1622 return ( ! ((nwwt->desktop) || (nwwt->dock) || (nwwt->splash)));
1623 }
1624
1625 /* Free the names associated with a task. */
1626 static void task_free_names(Task * tk)
1627 {
1628 g_free(tk->name);
1629 g_free(tk->name_iconified);
1630 tk->name = tk->name_iconified = NULL;
1631 }
1632
1633 /* Set the names associated with a task.
1634 * This is expected to be the same as the title the window manager is displaying. */
1635 static void task_set_names(Task * tk, Atom source)
1636 {
1637 char * name = NULL;
1638
1639 /* Try _NET_WM_VISIBLE_NAME, which supports UTF-8.
1640 * If it is set, the window manager is displaying it as the window title. */
1641 if ((source == None) || (source == a_NET_WM_VISIBLE_NAME))
1642 {
1643 name = get_utf8_property(tk->win, a_NET_WM_VISIBLE_NAME);
1644 if (name != NULL)
1645 tk->name_source = a_NET_WM_VISIBLE_NAME;
1646 }
1647
1648 /* Try _NET_WM_NAME, which supports UTF-8, but do not overwrite _NET_WM_VISIBLE_NAME. */
1649 if ((name == NULL)
1650 && ((source == None) || (source == a_NET_WM_NAME))
1651 && ((tk->name_source == None) || (tk->name_source == a_NET_WM_NAME) || (tk->name_source == XA_WM_NAME)))
1652 {
1653 name = get_utf8_property(tk->win, a_NET_WM_NAME);
1654 if (name != NULL)
1655 tk->name_source = a_NET_WM_NAME;
1656 }
1657
1658 /* Try WM_NAME, which supports only ISO-8859-1, but do not overwrite _NET_WM_VISIBLE_NAME or _NET_WM_NAME. */
1659 if ((name == NULL)
1660 && ((source == None) || (source == XA_WM_NAME))
1661 && ((tk->name_source == None) || (tk->name_source == XA_WM_NAME)))
1662 {
1663 name = get_textproperty(tk->win, XA_WM_NAME);
1664 if (name != NULL)
1665 tk->name_source = XA_WM_NAME;
1666 }
1667
1668 /* Set the name into the task context, and also on the tooltip. */
1669 if (name != NULL)
1670 {
1671 task_free_names(tk);
1672 tk->name = g_strdup(name);
1673 tk->name_iconified = g_strdup_printf("[%s]", name);
1674 g_free(name);
1675
1676 /* Redraw the button. */
1677 task_button_redraw(tk, tk->tb);
1678 }
1679 }
1680
1681 /* Unlink a task from the class list because its class changed or it was deleted. */
1682 static void task_unlink_class(Task * tk)
1683 {
1684 TaskClass * tc = tk->p_taskclass;
1685 if (tc != NULL)
1686 {
1687 /* Action in Launchbar after class removed */
1688 launchbar_update_after_taskbar_class_removed(tk->tb, tk);
1689
1690 /* Remove from per-class task list. */
1691 if (tc->p_task_head == tk)
1692 {
1693 /* Removing the head of the list. This causes a new task to be the visible task, so we redraw. */
1694 tc->p_task_head = tk->p_task_flink_same_class;
1695 if (tc->p_task_head != NULL)
1696 task_button_redraw(tc->p_task_head, tk->tb);
1697 }
1698 else
1699 {
1700 /* Locate the task and its predecessor in the list and then remove it. For safety, ensure it is found. */
1701 Task * tk_pred = NULL;
1702 Task * tk_cursor;
1703 for (
1704 tk_cursor = tc->p_task_head;
1705 ((tk_cursor != NULL) && (tk_cursor != tk));
1706 tk_pred = tk_cursor, tk_cursor = tk_cursor->p_task_flink_same_class) ;
1707 if (tk_cursor == tk)
1708 tk_pred->p_task_flink_same_class = tk->p_task_flink_same_class;
1709 }
1710 tk->p_task_flink_same_class = NULL;
1711
1712 /* Recompute group visibility. */
1713 recompute_group_visibility_for_class(tk->tb, tc);
1714 }
1715 }
1716
1717 /* Enter class with specified name. */
1718 static TaskClass * taskbar_enter_res_class(LaunchTaskBarPlugin * tb, char * res_class, gboolean * p_name_consumed)
1719 {
1720 /* Find existing entry or insertion point. */
1721 *p_name_consumed = FALSE;
1722 TaskClass * tc_pred = NULL;
1723 TaskClass * tc;
1724 for (tc = tb->p_taskclass_list; tc != NULL; tc_pred = tc, tc = tc->p_taskclass_flink)
1725 {
1726 int status = strcmp(res_class, tc->res_class);
1727 if (status == 0)
1728 return tc;
1729 if (status < 0)
1730 break;
1731 }
1732
1733 /* Insert new entry. */
1734 tc = g_new0(TaskClass, 1);
1735 tc->res_class = res_class;
1736 *p_name_consumed = TRUE;
1737 if (tc_pred == NULL)
1738 {
1739 tc->p_taskclass_flink = tb->p_taskclass_list;
1740 tb->p_taskclass_list = tc;
1741 }
1742 else
1743 {
1744 tc->p_taskclass_flink = tc_pred->p_taskclass_flink;
1745 tc_pred->p_taskclass_flink = tc;
1746 }
1747 return tc;
1748 }
1749
1750 /* Set the class associated with a task. */
1751 static void task_set_class(Task * tk)
1752 {
1753 /* Read the WM_CLASS property. */
1754 XClassHint ch;
1755 ch.res_name = NULL;
1756 ch.res_class = NULL;
1757 XGetClassHint(GDK_DISPLAY(), tk->win, &ch);
1758
1759 /* If the res_name was returned, free it. We make no use of it at this time. */
1760 if (ch.res_name != NULL)
1761 {
1762 XFree(ch.res_name);
1763 }
1764
1765 /* If the res_class was returned, process it.
1766 * This identifies the application that created the window and is the basis for taskbar grouping. */
1767 if (ch.res_class != NULL)
1768 {
1769 /* Convert the class to UTF-8 and enter it in the class table. */
1770 gchar * res_class = g_locale_to_utf8(ch.res_class, -1, NULL, NULL, NULL);
1771 if (res_class != NULL)
1772 {
1773 gboolean name_consumed;
1774 TaskClass * tc = taskbar_enter_res_class(tk->tb, res_class, &name_consumed);
1775 if ( ! name_consumed) g_free(res_class);
1776
1777 /* If the task changed class, update data structures. */
1778 TaskClass * old_tc = tk->p_taskclass;
1779 if (old_tc != tc)
1780 {
1781 /* Unlink from previous class, if any. */
1782 task_unlink_class(tk);
1783
1784 /* Add to end of per-class task list. Do this to keep the popup menu in order of creation. */
1785 if (tc->p_task_head == NULL)
1786 tc->p_task_head = tk;
1787 else
1788 {
1789 Task * tk_pred;
1790 for (tk_pred = tc->p_task_head; tk_pred->p_task_flink_same_class != NULL; tk_pred = tk_pred->p_task_flink_same_class) ;
1791 tk_pred->p_task_flink_same_class = tk;
1792 task_button_redraw(tk, tk->tb);
1793 }
1794 tk->p_taskclass = tc;
1795
1796 /* Recompute group visibility. */
1797 recompute_group_visibility_for_class(tk->tb, tc);
1798
1799 /* Action in Launchbar after class added */
1800 launchbar_update_after_taskbar_class_added(tk->tb, tk);
1801 }
1802 }
1803 XFree(ch.res_class);
1804 }
1805 }
1806
1807 /* Look up a task in the task list. */
1808 static Task * task_lookup(LaunchTaskBarPlugin * tb, Window win)
1809 {
1810 Task * tk;
1811 for (tk = tb->p_task_list; tk != NULL; tk = tk->p_task_flink_xwid)
1812 {
1813 if (tk->win == win)
1814 return tk;
1815 if (tk->win > win)
1816 break;
1817 }
1818 return NULL;
1819 }
1820
1821 /* Delete a task and optionally unlink it from the task list. */
1822 static void task_delete(LaunchTaskBarPlugin * tb, Task * tk, gboolean unlink)
1823 {
1824 /* If we think this task had focus, remove that. */
1825 if (tb->focused == tk)
1826 tb->focused = NULL;
1827
1828 /* If there is an urgency timeout, remove it. */
1829 if (tk->flash_timeout != 0) {
1830 g_source_remove(tk->flash_timeout);
1831 tk->flash_timeout = 0;
1832 }
1833
1834 /* Deallocate structures. */
1835 icon_grid_remove(tb->tb_icon_grid, tk->button);
1836 task_free_names(tk);
1837 g_free(tk->exec_bin);
1838 task_unlink_class(tk);
1839
1840 /* If requested, unlink the task from the task list.
1841 * If not requested, the caller will do this. */
1842 if (unlink)
1843 {
1844 if (tb->p_task_list == tk)
1845 tb->p_task_list = tk->p_task_flink_xwid;
1846 else
1847 {
1848 /* Locate the task and its predecessor in the list and then remove it. For safety, ensure it is found. */
1849 Task * tk_pred = NULL;
1850 Task * tk_cursor;
1851 for (
1852 tk_cursor = tb->p_task_list;
1853 ((tk_cursor != NULL) && (tk_cursor != tk));
1854 tk_pred = tk_cursor, tk_cursor = tk_cursor->p_task_flink_xwid) ;
1855 if (tk_cursor == tk)
1856 tk_pred->p_task_flink_xwid = tk->p_task_flink_xwid;
1857 }
1858 }
1859
1860 /* Deallocate the task structure. */
1861 g_free(tk);
1862 }
1863
1864 /* Get a pixbuf from a pixmap.
1865 * Originally from libwnck, Copyright (C) 2001 Havoc Pennington. */
1866 static GdkPixbuf * _wnck_gdk_pixbuf_get_from_pixmap(Pixmap xpixmap, int width, int height)
1867 {
1868 /* Get the drawable. */
1869 GdkDrawable * drawable = gdk_xid_table_lookup(xpixmap);
1870 if (drawable != NULL)
1871 g_object_ref(G_OBJECT(drawable));
1872 else
1873 drawable = gdk_pixmap_foreign_new(xpixmap);
1874
1875 GdkColormap * colormap = NULL;
1876 GdkPixbuf * retval = NULL;
1877 if (drawable != NULL)
1878 {
1879 /* Get the colormap.
1880 * If the drawable has no colormap, use no colormap or the system colormap as recommended in the documentation of gdk_drawable_get_colormap. */
1881 colormap = gdk_drawable_get_colormap(drawable);
1882 gint depth = gdk_drawable_get_depth(drawable);
1883 if (colormap != NULL)
1884 g_object_ref(G_OBJECT(colormap));
1885 else if (depth == 1)
1886 colormap = NULL;
1887 else
1888 {
1889 colormap = gdk_screen_get_system_colormap(gdk_drawable_get_screen(drawable));
1890 g_object_ref(G_OBJECT(colormap));
1891 }
1892
1893 /* Be sure we aren't going to fail due to visual mismatch. */
1894 #if GTK_CHECK_VERSION(2,22,0)
1895 if ((colormap != NULL) && (gdk_visual_get_depth(gdk_colormap_get_visual(colormap)) != depth))
1896 #else
1897 if ((colormap != NULL) && (gdk_colormap_get_visual(colormap)->depth != depth))
1898 #endif
1899 {
1900 g_object_unref(G_OBJECT(colormap));
1901 colormap = NULL;
1902 }
1903
1904 /* Do the major work. */
1905 retval = gdk_pixbuf_get_from_drawable(NULL, drawable, colormap, 0, 0, 0, 0, width, height);
1906 }
1907
1908 /* Clean up and return. */
1909 if (colormap != NULL)
1910 g_object_unref(G_OBJECT(colormap));
1911 if (drawable != NULL)
1912 g_object_unref(G_OBJECT(drawable));
1913 return retval;
1914 }
1915
1916 /* Apply a mask to a pixbuf.
1917 * Originally from libwnck, Copyright (C) 2001 Havoc Pennington. */
1918 static GdkPixbuf * apply_mask(GdkPixbuf * pixbuf, GdkPixbuf * mask)
1919 {
1920 /* Initialize. */
1921 int w = MIN(gdk_pixbuf_get_width(mask), gdk_pixbuf_get_width(pixbuf));
1922 int h = MIN(gdk_pixbuf_get_height(mask), gdk_pixbuf_get_height(pixbuf));
1923 GdkPixbuf * with_alpha = gdk_pixbuf_add_alpha(pixbuf, FALSE, 0, 0, 0);
1924 guchar * dst = gdk_pixbuf_get_pixels(with_alpha);
1925 guchar * src = gdk_pixbuf_get_pixels(mask);
1926 int dst_stride = gdk_pixbuf_get_rowstride(with_alpha);
1927 int src_stride = gdk_pixbuf_get_rowstride(mask);
1928
1929 /* Loop to do the work. */
1930 int i;
1931 for (i = 0; i < h; i += 1)
1932 {
1933 int j;
1934 for (j = 0; j < w; j += 1)
1935 {
1936 guchar * s = src + i * src_stride + j * 3;
1937 guchar * d = dst + i * dst_stride + j * 4;
1938
1939 /* s[0] == s[1] == s[2], they are 255 if the bit was set, 0 otherwise. */
1940 d[3] = ((s[0] == 0) ? 0 : 255); /* 0 = transparent, 255 = opaque */
1941 }
1942 }
1943
1944 return with_alpha;
1945 }
1946
1947 /* Get an icon from the window manager for a task, and scale it to a specified size. */
1948 static GdkPixbuf * get_wm_icon(Window task_win, guint required_width, guint required_height, Atom source, Atom * current_source)
1949 {
1950 /* The result. */
1951 GdkPixbuf * pixmap = NULL;
1952 Atom possible_source = None;
1953 int result = -1;
1954
1955 if ((source == None) || (source == a_NET_WM_ICON))
1956 {
1957 /* Important Notes:
1958 * According to freedesktop.org document:
1959 * http://standards.freedesktop.org/wm-spec/wm-spec-1.4.html#id2552223
1960 * _NET_WM_ICON contains an array of 32-bit packed CARDINAL ARGB.
1961 * However, this is incorrect. Actually it's an array of long integers.
1962 * Toolkits like gtk+ use unsigned long here to store icons.
1963 * Besides, according to manpage of XGetWindowProperty, when returned format,
1964 * is 32, the property data will be stored as an array of longs
1965 * (which in a 64-bit application will be 64-bit values that are
1966 * padded in the upper 4 bytes).
1967 */
1968
1969 /* Get the window property _NET_WM_ICON, if possible. */
1970 Atom type = None;
1971 int format;
1972 gulong nitems;
1973 gulong bytes_after;
1974 gulong * data = NULL;
1975 result = XGetWindowProperty(
1976 GDK_DISPLAY(),
1977 task_win,
1978 a_NET_WM_ICON,
1979 0, G_MAXLONG,
1980 False, XA_CARDINAL,
1981 &type, &format, &nitems, &bytes_after, (void *) &data);
1982
1983 /* Inspect the result to see if it is usable. If not, and we got data, free it. */
1984 if ((type != XA_CARDINAL) || (nitems <= 0))
1985 {
1986 if (data != NULL)
1987 XFree(data);
1988 result = -1;
1989 }
1990
1991 /* If the result is usable, extract the icon from it. */
1992 if (result == Success)
1993 {
1994 /* Get the largest icon available, unless there is one that is the desired size. */
1995 /* FIXME: should we try to find an icon whose size is closest to
1996 * required_width and required_height to reduce unnecessary resizing? */
1997 gulong * pdata = data;
1998 gulong * pdata_end = data + nitems;
1999 gulong * max_icon = NULL;
2000 gulong max_w = 0;
2001 gulong max_h = 0;
2002 while ((pdata + 2) < pdata_end)
2003 {
2004 /* Extract the width and height. */
2005 guint w = pdata[0];
2006 guint h = pdata[1];
2007 gulong size = w * h;
2008 pdata += 2;
2009
2010 /* Bounds check the icon. */
2011 if (pdata + size > pdata_end)
2012 break;
2013
2014 /* Rare special case: the desired size is the same as icon size. */
2015 if ((required_width == w) && (required_height == h))
2016 {
2017 max_icon = pdata;
2018 max_w = w;
2019 max_h = h;
2020 break;
2021 }
2022
2023 /* If the icon is the largest so far, capture it. */
2024 if ((w > max_w) && (h > max_h))
2025 {
2026 max_icon = pdata;
2027 max_w = w;
2028 max_h = h;
2029 }
2030 pdata += size;
2031 }
2032
2033 /* If an icon was extracted, convert it to a pixbuf.
2034 * Its size is max_w and max_h. */
2035 if (max_icon != NULL)
2036 {
2037 /* Allocate enough space for the pixel data. */
2038 gulong len = max_w * max_h;
2039 guchar * pixdata = g_new(guchar, len * 4);
2040
2041 /* Loop to convert the pixel data. */
2042 guchar * p = pixdata;
2043 gulong i;
2044 for (i = 0; i < len; p += 4, i += 1)
2045 {
2046 guint argb = max_icon[i];
2047 guint rgba = (argb << 8) | (argb >> 24);
2048 p[0] = rgba >> 24;
2049 p[1] = (rgba >> 16) & 0xff;
2050 p[2] = (rgba >> 8) & 0xff;
2051 p[3] = rgba & 0xff;
2052 }
2053
2054 /* Initialize a pixmap with the pixel data. */
2055 pixmap = gdk_pixbuf_new_from_data(
2056 pixdata,
2057 GDK_COLORSPACE_RGB,
2058 TRUE, 8, /* has_alpha, bits_per_sample */
2059 max_w, max_h, max_w * 4,
2060 (GdkPixbufDestroyNotify) g_free,
2061 NULL);
2062 possible_source = a_NET_WM_ICON;
2063 }
2064 else
2065 result = -1;
2066
2067 /* Free the X property data. */
2068 XFree(data);
2069 }
2070 }
2071
2072 /* No icon available from _NET_WM_ICON. Next try WM_HINTS, but do not overwrite _NET_WM_ICON. */
2073 if ((result != Success) && (*current_source != a_NET_WM_ICON)
2074 && ((source == None) || (source != a_NET_WM_ICON)))
2075 {
2076 XWMHints * hints = XGetWMHints(GDK_DISPLAY(), task_win);
2077 result = (hints != NULL) ? Success : -1;
2078 Pixmap xpixmap = None;
2079 Pixmap xmask = None;
2080
2081 if (result == Success)
2082 {
2083 /* WM_HINTS is available. Extract the X pixmap and mask. */
2084 if ((hints->flags & IconPixmapHint))
2085 xpixmap = hints->icon_pixmap;
2086 if ((hints->flags & IconMaskHint))
2087 xmask = hints->icon_mask;
2088 XFree(hints);
2089 if (xpixmap != None)
2090 {
2091 result = Success;
2092 possible_source = XA_WM_HINTS;
2093 }
2094 else
2095 result = -1;
2096 }
2097
2098 if (result != Success)
2099 {
2100 /* No icon available from _NET_WM_ICON or WM_HINTS. Next try KWM_WIN_ICON. */
2101 Atom type = None;
2102 int format;
2103 gulong nitems;
2104 gulong bytes_after;
2105 Pixmap *icons = NULL;
2106 Atom kwin_win_icon_atom = gdk_x11_get_xatom_by_name("KWM_WIN_ICON");
2107 result = XGetWindowProperty(
2108 GDK_DISPLAY(),
2109 task_win,
2110 kwin_win_icon_atom,
2111 0, G_MAXLONG,
2112 False, kwin_win_icon_atom,
2113 &type, &format, &nitems, &bytes_after, (void *) &icons);
2114
2115 /* Inspect the result to see if it is usable. If not, and we got data, free it. */
2116 if (type != kwin_win_icon_atom)
2117 {
2118 if (icons != NULL)
2119 XFree(icons);
2120 result = -1;
2121 }
2122
2123 /* If the result is usable, extract the X pixmap and mask from it. */
2124 if (result == Success)
2125 {
2126 xpixmap = icons[0];
2127 xmask = icons[1];
2128 if (xpixmap != None)
2129 {
2130 result = Success;
2131 possible_source = kwin_win_icon_atom;
2132 }
2133 else
2134 result = -1;
2135 }
2136 }
2137
2138 /* If we have an X pixmap, get its geometry.*/
2139 unsigned int w, h;
2140 if (result == Success)
2141 {
2142 Window unused_win;
2143 int unused;
2144 unsigned int unused_2;
2145 result = XGetGeometry(
2146 GDK_DISPLAY(), xpixmap,
2147 &unused_win, &unused, &unused, &w, &h, &unused_2, &unused_2) ? Success : -1;
2148 }
2149
2150 /* If we have an X pixmap and its geometry, convert it to a GDK pixmap. */
2151 if (result == Success)
2152 {
2153 pixmap = _wnck_gdk_pixbuf_get_from_pixmap(xpixmap, w, h);
2154 result = ((pixmap != NULL) ? Success : -1);
2155 }
2156
2157 /* If we have success, see if the result needs to be masked.
2158 * Failures here are implemented as nonfatal. */
2159 if ((result == Success) && (xmask != None))
2160 {
2161 Window unused_win;
2162 int unused;
2163 unsigned int unused_2;
2164 if (XGetGeometry(
2165 GDK_DISPLAY(), xmask,
2166 &unused_win, &unused, &unused, &w, &h, &unused_2, &unused_2))
2167 {
2168 /* Convert the X mask to a GDK pixmap. */
2169 GdkPixbuf * mask = _wnck_gdk_pixbuf_get_from_pixmap(xmask, w, h);
2170 if (mask != NULL)
2171 {
2172 /* Apply the mask. */
2173 GdkPixbuf * masked_pixmap = apply_mask(pixmap, mask);
2174 g_object_unref(G_OBJECT(pixmap));
2175 g_object_unref(G_OBJECT(mask));
2176 pixmap = masked_pixmap;
2177 }
2178 }
2179 }
2180 }
2181
2182 /* If we got a pixmap, scale it and return it. */
2183 if (pixmap == NULL)
2184 return NULL;
2185 else
2186 {
2187 GdkPixbuf * ret = gdk_pixbuf_scale_simple(pixmap, required_width, required_height, GDK_INTERP_TILES);
2188 g_object_unref(pixmap);
2189 *current_source = possible_source;
2190 return ret;
2191 }
2192 }
2193
2194 /* Update the icon of a task. */
2195 static GdkPixbuf * task_update_icon(LaunchTaskBarPlugin * tb, Task * tk, Atom source)
2196 {
2197 /* Get the icon from the window's hints. */
2198 GdkPixbuf * pixbuf = get_wm_icon(tk->win, MAX(0, tb->icon_size - ICON_BUTTON_TRIM),
2199 MAX(0, tb->icon_size - ICON_BUTTON_TRIM),
2200 source, &tk->image_source);
2201
2202 /* If that fails, and we have no other icon yet, return the fallback icon. */
2203 if ((pixbuf == NULL)
2204 && ((source == None) || (tk->image_source == None)))
2205 {
2206 /* Establish the fallback task icon. This is used when no other icon is available. */
2207 if (tb->fallback_pixbuf == NULL)
2208 tb->fallback_pixbuf = gdk_pixbuf_new_from_xpm_data((const char **) icon_xpm);
2209 g_object_ref(tb->fallback_pixbuf);
2210 pixbuf = tb->fallback_pixbuf;
2211 }
2212
2213 /* Return what we have. This may be NULL to indicate that no change should be made to the icon. */
2214 return pixbuf;
2215 }
2216
2217 /* Timer expiration for urgency notification. Also used to draw the button in setting and clearing urgency. */
2218 static gboolean flash_window_timeout(Task * tk)
2219 {
2220 /* Set state on the button and redraw. */
2221 if ( ! tk->tb->flat_button)
2222 gtk_widget_set_state(tk->button, tk->flash_state ? GTK_STATE_SELECTED : GTK_STATE_NORMAL);
2223 task_draw_label(tk);
2224
2225 /* Complement the flashing context. */
2226 tk->flash_state = ! tk->flash_state;
2227 return TRUE;
2228 }
2229
2230 /* Set urgency notification. */
2231 static void task_set_urgency(Task * tk)
2232 {
2233 LaunchTaskBarPlugin * tb = tk->tb;
2234 TaskClass * tc = tk->p_taskclass;
2235 if ((tb->grouped_tasks) && (tc != NULL) && (tc->visible_count > 1))
2236 recompute_group_visibility_for_class(tk->tb, tk->p_taskclass);
2237 else
2238 {
2239 /* Set the flashing context and flash the window immediately. */
2240 tk->flash_state = TRUE;
2241 flash_window_timeout(tk);
2242
2243 /* Set the timer if none is set. */
2244 if (tk->flash_timeout == 0)
2245 set_timer_on_task(tk);
2246 }
2247 }
2248
2249 /* Clear urgency notification. */
2250 static void task_clear_urgency(Task * tk)
2251 {
2252 LaunchTaskBarPlugin * tb = tk->tb;
2253 TaskClass * tc = tk->p_taskclass;
2254 if ((tb->grouped_tasks) && (tc != NULL) && (tc->visible_count > 1))
2255 recompute_group_visibility_for_class(tk->tb, tk->p_taskclass);
2256 else
2257 {
2258 /* Remove the timer if one is set. */
2259 if (tk->flash_timeout != 0)
2260 {
2261 g_source_remove(tk->flash_timeout);
2262 tk->flash_timeout = 0;
2263 }
2264
2265 /* Clear the flashing context and unflash the window immediately. */
2266 tk->flash_state = FALSE;
2267 flash_window_timeout(tk);
2268 tk->flash_state = FALSE;
2269 }
2270 }
2271
2272 /* Do the proper steps to raise a window.
2273 * This means removing it from iconified state and bringing it to the front.
2274 * We also switch the active desktop and viewport if needed. */
2275 static void task_raise_window(Task * tk, guint32 time)
2276 {
2277 /* Change desktop if needed. */
2278 if ((tk->desktop != ALL_WORKSPACES) && (tk->desktop != tk->tb->current_desktop))
2279 Xclimsg(GDK_ROOT_WINDOW(), a_NET_CURRENT_DESKTOP, tk->desktop, 0, 0, 0, 0);
2280
2281 /* Evaluate use_net_active if not yet done. */
2282 if ( ! tk->tb->net_active_checked)
2283 {
2284 LaunchTaskBarPlugin * tb = tk->tb;
2285 GdkAtom net_active_atom = gdk_x11_xatom_to_atom(a_NET_ACTIVE_WINDOW);
2286 tb->use_net_active = gdk_x11_screen_supports_net_wm_hint(gtk_widget_get_screen(tb->plugin), net_active_atom);
2287 tb->net_active_checked = TRUE;
2288 }
2289
2290 /* Raise the window. We can use NET_ACTIVE_WINDOW if the window manager supports it.
2291 * Otherwise, do it the old way with XMapRaised and XSetInputFocus. */
2292 if (tk->tb->use_net_active)
2293 Xclimsg(tk->win, a_NET_ACTIVE_WINDOW, 2, time, 0, 0, 0);
2294 else
2295 {
2296 GdkWindow * gdkwindow = gdk_xid_table_lookup(tk->win);
2297 if (gdkwindow != NULL)
2298 gdk_window_show(gdkwindow);
2299 else
2300 XMapRaised(GDK_DISPLAY(), tk->win);
2301
2302 /* There is a race condition between the X server actually executing the XMapRaised and this code executing XSetInputFocus.
2303 * If the window is not viewable, the XSetInputFocus will fail with BadMatch. */
2304 XWindowAttributes attr;
2305 XGetWindowAttributes(GDK_DISPLAY(), tk->win, &attr);
2306 if (attr.map_state == IsViewable)
2307 XSetInputFocus(GDK_DISPLAY(), tk->win, RevertToNone, time);
2308 }
2309
2310 /* Change viewport if needed. */
2311 XWindowAttributes xwa;
2312 XGetWindowAttributes(GDK_DISPLAY(), tk->win, &xwa);
2313 Xclimsg(tk->win, a_NET_DESKTOP_VIEWPORT, xwa.x, xwa.y, 0, 0, 0);
2314 }
2315
2316 /* Position-calculation callback for grouped-task and window-management popup menu. */
2317 static void taskbar_popup_set_position(GtkWidget * menu, gint * px, gint * py, gboolean * push_in, gpointer data)
2318 {
2319 Task * tk = (Task *) data;
2320
2321 /* Get the allocation of the popup menu. */
2322 GtkRequisition popup_req;
2323 gtk_widget_size_request(menu, &popup_req);
2324
2325 /* Determine the coordinates. */
2326 lxpanel_plugin_popup_set_position_helper(tk->tb->panel, tk->button, menu,
2327 &popup_req, px, py);
2328 *push_in = TRUE;
2329 }
2330
2331 /* Handler for "activate" event from "close all windows" menu*/
2332 static void taskbar_close_all_windows (GtkWidget * widget, Task * tk )
2333 {
2334 Task * tk_cursor;
2335 for (tk_cursor = tk->p_taskclass->p_task_head; tk_cursor != NULL;
2336 tk_cursor = tk_cursor->p_task_flink_same_class)
2337 {
2338 if (task_is_visible_on_current_desktop(tk->tb, tk_cursor))
2339 {
2340 Xclimsgwm(tk_cursor->win, a_WM_PROTOCOLS, a_WM_DELETE_WINDOW);
2341 }
2342 }
2343 task_group_menu_destroy(tk->tb);
2344 }
2345
2346 /* Remove the grouped-task popup menu from the screen. */
2347 static void task_group_menu_destroy(LaunchTaskBarPlugin * tb)
2348 {
2349 if (tb->group_menu != NULL)
2350 {
2351 gtk_widget_destroy(tb->group_menu);
2352 tb->group_menu = NULL;
2353 }
2354 }
2355
2356 /* Handler for "button-press-event" event from taskbar button,
2357 * or "activate" event from grouped-task popup menu item. */
2358 static gboolean taskbar_task_control_event(GtkWidget * widget, GdkEventButton * event, Task * tk, gboolean popup_menu)
2359 {
2360 LaunchTaskBarPlugin * tb = tk->tb;
2361 TaskClass * tc = tk->p_taskclass;
2362 if ((tb->grouped_tasks) && (tc != NULL) && (tc->visible_count > 1) && (GTK_IS_BUTTON(widget)))
2363 {
2364 /* This is grouped-task representative, meaning that there is a class
2365 * with at least two windows. */
2366 GtkWidget * menu = NULL;
2367 if( event->button == 1 ) /* Left click */
2368 {
2369 menu = gtk_menu_new();
2370 /* Bring up a popup menu listing all the class members. */
2371 Task * tk_cursor;
2372 for (tk_cursor = tc->p_task_head; tk_cursor != NULL;
2373 tk_cursor = tk_cursor->p_task_flink_same_class)
2374 {
2375 if (task_is_visible_on_current_desktop(tb, tk_cursor))
2376 {
2377 /* The menu item has the name, or the iconified name, and
2378 * the icon of the application window. */
2379 GtkWidget * mi = gtk_image_menu_item_new_with_label(((tk_cursor->iconified) ?
2380 tk_cursor->name_iconified : tk_cursor->name));
2381 GtkWidget * im = gtk_image_new_from_pixbuf(gtk_image_get_pixbuf(
2382 GTK_IMAGE(tk_cursor->image)));
2383 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mi), im);
2384 g_signal_connect(mi, "button-press-event",
2385 G_CALLBACK(taskbar_popup_activate_event), (gpointer) tk_cursor);
2386 gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi);
2387 }
2388 }
2389 }
2390 else if(event->button == 3) /* Right click */
2391 {
2392 menu = gtk_menu_new();
2393 GtkWidget * mi = gtk_menu_item_new_with_mnemonic (_("_Close all windows"));
2394 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), mi);
2395 g_signal_connect( mi, "activate", G_CALLBACK(taskbar_close_all_windows), tk);
2396 }
2397
2398 /* Show the menu. Set context so we can find the menu later to dismiss it.
2399 * Use a position-calculation callback to get the menu nicely
2400 * positioned with respect to the button. */
2401 if (menu) {
2402 gtk_widget_show_all(menu);
2403 tb->group_menu = menu;
2404 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
2405 (GtkMenuPositionFunc) taskbar_popup_set_position, (gpointer) tk,
2406 event->button, event->time);
2407 }
2408 }
2409 else
2410 {
2411 /* Not a grouped-task representative, or entered from the grouped-task popup menu. */
2412 Task * visible_task = (((tk->p_taskclass == NULL) || ( ! tk->tb->grouped_tasks)) ? tk : tk->p_taskclass->p_task_visible);
2413 task_group_menu_destroy(tb);
2414
2415 if (event->button == 1)
2416 {
2417 /* Left button.
2418 * If the task is iconified, raise it.
2419 * If the task is not iconified and has focus, iconify it.
2420 * If the task is not iconified and does not have focus, raise it. */
2421 if (tk->iconified)
2422 task_raise_window(tk, event->time);
2423 else if ((tk->focused) || (tk == tb->focused_previous))
2424 XIconifyWindow(GDK_DISPLAY(), tk->win, DefaultScreen(GDK_DISPLAY()));
2425 else
2426 task_raise_window(tk, event->time);
2427 }
2428 else if (event->button == 2)
2429 {
2430 /* Middle button. Toggle the shaded state of the window. */
2431 Xclimsg(tk->win, a_NET_WM_STATE,
2432 2, /* a_NET_WM_STATE_TOGGLE */
2433 a_NET_WM_STATE_SHADED,
2434 0, 0, 0);
2435 }
2436 else if(event->button == 3)
2437 {
2438 /* Right button. Bring up the window state popup menu. */
2439 tk->tb->menutask = tk;
2440 #ifndef DISABLE_MENU
2441 LaunchTaskBarPlugin *ltbp = (LaunchTaskBarPlugin *)tk->tb;
2442 if(ltbp->lb_on)
2443 {
2444 FmFileInfo *fi = f_find_menu_launchbutton_recursive(tk->exec_bin);
2445 LaunchButton *btn = launchbar_exec_bin_exists(ltbp, fi);
2446 /* FIXME: shouldn't we make file info at task button creation? */
2447 //g_print("\nTB '%s' right-click, in LB: %c\n", tk->exec_bin, btn != NULL ? 'Y':'N');
2448 if(btn != NULL)
2449 {
2450 gtk_widget_set_visible(ltbp->p_menuitem_lock_tbp, FALSE);
2451 gtk_widget_set_visible(ltbp->p_menuitem_unlock_tbp, TRUE);
2452 gtk_widget_set_visible(ltbp->p_menuitem_new_instance, TRUE);
2453 }
2454 else
2455 {
2456 gtk_widget_set_visible(ltbp->p_menuitem_lock_tbp, fi != NULL);
2457 gtk_widget_set_visible(ltbp->p_menuitem_unlock_tbp, FALSE);
2458 gtk_widget_set_visible(ltbp->p_menuitem_new_instance, fi != NULL);
2459 }
2460 if (fi)
2461 fm_file_info_unref(fi);
2462 }
2463 else
2464 {
2465 gtk_widget_set_visible(ltbp->p_menuitem_lock_tbp, FALSE);
2466 gtk_widget_set_visible(ltbp->p_menuitem_unlock_tbp, FALSE);
2467 gtk_widget_set_visible(ltbp->p_menuitem_new_instance, FALSE);
2468 }
2469 #endif
2470 gtk_menu_popup(
2471 GTK_MENU(tb->menu),
2472 NULL, NULL,
2473 (GtkMenuPositionFunc) taskbar_popup_set_position, (gpointer) visible_task,
2474 event->button, event->time);
2475 }
2476 }
2477
2478 /* As a matter of policy, avoid showing selected or prelight states on flat buttons. */
2479 if (tb->flat_button)
2480 gtk_widget_set_state(widget, GTK_STATE_NORMAL);
2481 return TRUE;
2482 }
2483
2484 /* Handler for "button-press-event" event from taskbar button. */
2485 static gboolean taskbar_button_press_event(GtkWidget * widget, GdkEventButton * event, Task * tk)
2486 {
2487 return taskbar_task_control_event(widget, event, tk, FALSE);
2488 }
2489
2490 /* Handler for "activate" event from grouped-task popup menu item. */
2491 static gboolean taskbar_popup_activate_event(GtkWidget * widget, GdkEventButton * event, Task * tk)
2492 {
2493 return taskbar_task_control_event(widget, event, tk, TRUE);
2494 }
2495
2496 /* Handler for "drag-motion" timeout. */
2497 static gboolean taskbar_button_drag_motion_timeout(Task * tk)
2498 {
2499 guint time = gtk_get_current_event_time();
2500 task_raise_window(tk, ((time != 0) ? time : CurrentTime));
2501 tk->tb->dnd_delay_timer = 0;
2502 return FALSE;
2503 }
2504
2505 /* Handler for "drag-motion" event from taskbar button. */
2506 static gboolean taskbar_button_drag_motion(GtkWidget * widget, GdkDragContext * drag_context, gint x, gint y, guint time, Task * tk)
2507 {
2508 /* Prevent excessive motion notification. */
2509 if (tk->tb->dnd_delay_timer == 0)
2510 tk->tb->dnd_delay_timer = g_timeout_add(DRAG_ACTIVE_DELAY, (GSourceFunc) taskbar_button_drag_motion_timeout, tk);
2511 gdk_drag_status(drag_context, 0, time);
2512 return TRUE;
2513 }
2514
2515 /* Handler for "drag-leave" event from taskbar button. */
2516 static void taskbar_button_drag_leave(GtkWidget * widget, GdkDragContext * drag_context, guint time, Task * tk)
2517 {
2518 /* Cancel the timer if set. */
2519 if (tk->tb->dnd_delay_timer != 0)
2520 {
2521 g_source_remove(tk->tb->dnd_delay_timer);
2522 tk->tb->dnd_delay_timer = 0;
2523 }
2524 return;
2525 }
2526
2527 /* Handler for "enter" event from taskbar button. This indicates that the cursor position has entered the button. */
2528 static void taskbar_button_enter(GtkWidget * widget, Task * tk)
2529 {
2530 tk->entered_state = TRUE;
2531 if (tk->tb->flat_button)
2532 gtk_widget_set_state(widget, GTK_STATE_NORMAL);
2533 task_draw_label(tk);
2534 }
2535
2536 /* Handler for "leave" event from taskbar button. This indicates that the cursor position has left the button. */
2537 static void taskbar_button_leave(GtkWidget * widget, Task * tk)
2538 {
2539 tk->entered_state = FALSE;
2540 task_draw_label(tk);
2541 }
2542
2543 /* Handler for "scroll-event" event from taskbar button. */
2544 static gboolean taskbar_button_scroll_event(GtkWidget * widget, GdkEventScroll * event, Task * tk)
2545 {
2546 LaunchTaskBarPlugin * tb = tk->tb;
2547 TaskClass * tc = tk->p_taskclass;
2548 if ((tb->use_mouse_wheel)
2549 && (( ! tb->grouped_tasks) || (tc == NULL) || (tc->visible_count == 1)))
2550 {
2551 if ((event->direction == GDK_SCROLL_UP) || (event->direction == GDK_SCROLL_LEFT))
2552 task_raise_window(tk, event->time);
2553 else
2554 XIconifyWindow(GDK_DISPLAY(), tk->win, DefaultScreen(GDK_DISPLAY()));
2555 }
2556 return TRUE;
2557 }
2558
2559 /* Handler for "size-allocate" event from taskbar button. */
2560 static void taskbar_button_size_allocate(GtkWidget * btn, GtkAllocation * alloc, Task * tk)
2561 {
2562 if (GTK_WIDGET_REALIZED(btn))
2563 {
2564 /* Get the coordinates of the button. */
2565 int x, y;
2566 #if GTK_CHECK_VERSION(2,22,0)
2567 gdk_window_get_origin(gtk_button_get_event_window(GTK_BUTTON(btn)), &x, &y);
2568 #else
2569 gdk_window_get_origin(GTK_BUTTON(btn)->event_window, &x, &y);
2570 #endif
2571
2572
2573 /* Send a NET_WM_ICON_GEOMETRY property change on the window. */
2574 guint32 data[4];
2575 data[0] = x;
2576 data[1] = y;
2577 data[2] = alloc->width;
2578 data[3] = alloc->height;
2579 XChangeProperty(GDK_DISPLAY(), tk->win,
2580 gdk_x11_get_xatom_by_name("_NET_WM_ICON_GEOMETRY"),
2581 XA_CARDINAL, 32, PropModeReplace, (guchar *) &data, 4);
2582 }
2583 }
2584
2585 /* Update style on the taskbar when created or after a configuration change. */
2586 static void taskbar_update_style(LaunchTaskBarPlugin * tb)
2587 {
2588 icon_grid_set_geometry(tb->tb_icon_grid, panel_get_orientation(tb->panel),
2589 ((tb->icons_only) ? tb->icon_size + ICON_ONLY_EXTRA : tb->task_width_max),
2590 tb->icon_size, tb->spacing, 0, panel_get_height(tb->panel));
2591 }
2592
2593 /* Update style on a task button when created or after a configuration change. */
2594 static void task_update_style(Task * tk, LaunchTaskBarPlugin * tb)
2595 {
2596 if (tb->icons_only)
2597 gtk_widget_hide(tk->label);
2598 else
2599 gtk_widget_show(tk->label);
2600
2601 if( tb->flat_button )
2602 {
2603 gtk_toggle_button_set_active((GtkToggleButton*)tk->button, FALSE);
2604 gtk_button_set_relief(GTK_BUTTON(tk->button), GTK_RELIEF_NONE);
2605 }
2606 else
2607 {
2608 gtk_toggle_button_set_active((GtkToggleButton*)tk->button, tk->focused);
2609 gtk_button_set_relief(GTK_BUTTON(tk->button), GTK_RELIEF_NORMAL);
2610 }
2611
2612 task_draw_label(tk);
2613 }
2614
2615 /* Build graphic elements needed for a task button. */
2616 static void task_build_gui(LaunchTaskBarPlugin * tb, Task * tk)
2617 {
2618 /* NOTE
2619 * 1. the extended mask is sum of taskbar and pager needs
2620 * see bug [ 940441 ] pager loose track of windows
2621 *
2622 * Do not change event mask to gtk windows spawned by this gtk client
2623 * this breaks gtk internals */
2624 if (! gdk_window_lookup(tk->win))
2625 XSelectInput(GDK_DISPLAY(), tk->win, PropertyChangeMask | StructureNotifyMask);
2626
2627 /* Allocate a toggle button as the top level widget. */
2628 tk->button = gtk_toggle_button_new();
2629 gtk_container_set_border_width(GTK_CONTAINER(tk->button), 0);
2630 gtk_drag_dest_set(tk->button, 0, NULL, 0, 0);
2631
2632 /* Connect signals to the button. */
2633 g_signal_connect(tk->button, "button-press-event", G_CALLBACK(taskbar_button_press_event), (gpointer) tk);
2634 g_signal_connect(G_OBJECT(tk->button), "drag-motion", G_CALLBACK(taskbar_button_drag_motion), (gpointer) tk);
2635 g_signal_connect(G_OBJECT(tk->button), "drag-leave", G_CALLBACK(taskbar_button_drag_leave), (gpointer) tk);
2636 g_signal_connect_after(G_OBJECT (tk->button), "enter", G_CALLBACK(taskbar_button_enter), (gpointer) tk);
2637 g_signal_connect_after(G_OBJECT (tk->button), "leave", G_CALLBACK(taskbar_button_leave), (gpointer) tk);
2638 g_signal_connect_after(G_OBJECT(tk->button), "scroll-event", G_CALLBACK(taskbar_button_scroll_event), (gpointer) tk);
2639 g_signal_connect(tk->button, "size-allocate", G_CALLBACK(taskbar_button_size_allocate), (gpointer) tk);
2640
2641 /* Create a box to contain the application icon and window title. */
2642 GtkWidget * container = gtk_hbox_new(FALSE, 1);
2643 gtk_container_set_border_width(GTK_CONTAINER(container), 0);
2644
2645 /* Create an image to contain the application icon and add it to the box. */
2646 GdkPixbuf* pixbuf = task_update_icon(tb, tk, None);
2647 tk->image = gtk_image_new_from_pixbuf(pixbuf);
2648 gtk_misc_set_padding(GTK_MISC(tk->image), 0, 0);
2649 g_object_unref(pixbuf);
2650 gtk_widget_show(tk->image);
2651 gtk_box_pack_start(GTK_BOX(container), tk->image, FALSE, FALSE, 0);
2652
2653 /* Create a label to contain the window title and add it to the box. */
2654 tk->label = gtk_label_new(NULL);
2655 gtk_misc_set_alignment(GTK_MISC(tk->label), 0.0, 0.5);
2656 gtk_label_set_ellipsize(GTK_LABEL(tk->label), PANGO_ELLIPSIZE_END);
2657 gtk_box_pack_start(GTK_BOX(container), tk->label, TRUE, TRUE, 0);
2658
2659 /* Add the box to the button. */
2660 gtk_widget_show(container);
2661 gtk_container_add(GTK_CONTAINER(tk->button), container);
2662 gtk_container_set_border_width(GTK_CONTAINER(tk->button), 0);
2663
2664 /* Add the button to the taskbar. */
2665 icon_grid_add(tb->tb_icon_grid, tk->button, TRUE);
2666 #if GTK_CHECK_VERSION(2,18,0)
2667 gtk_widget_set_can_focus(GTK_WIDGET(tk->button),FALSE);
2668 gtk_widget_set_can_default(GTK_WIDGET(tk->button),FALSE);
2669 #else
2670 GTK_WIDGET_UNSET_FLAGS(tk->button, GTK_CAN_FOCUS);
2671 GTK_WIDGET_UNSET_FLAGS(tk->button, GTK_CAN_DEFAULT);
2672 #endif
2673
2674 /* Update styles on the button. */
2675 task_update_style(tk, tb);
2676
2677 /* Flash button for window with urgency hint. */
2678 if (tk->urgency)
2679 task_set_urgency(tk);
2680 }
2681
2682 /* Determine which monitor a given window is associated with */
2683 static gint get_window_monitor(Window win)
2684 {
2685 GdkDisplay *display;
2686 GdkWindow *gwin;
2687 gint m;
2688
2689 display = gdk_display_get_default();
2690 g_assert(display);
2691 gwin = gdk_x11_window_foreign_new_for_display(display,win);
2692 g_assert(gwin);
2693 m = gdk_screen_get_monitor_at_window(gdk_window_get_screen(gwin),gwin);
2694 gdk_window_unref(gwin);
2695 return m;
2696 }
2697
2698 /*****************************************************
2699 * handlers for NET actions *
2700 *****************************************************/
2701
2702 /* Handler for "client-list" event from root window listener. */
2703 static void taskbar_net_client_list(GtkWidget * widget, LaunchTaskBarPlugin * tb)
2704 {
2705 LaunchTaskBarPlugin *ltbp = tb;
2706 if(!ltbp->tb_on) return;
2707
2708 /* Get the NET_CLIENT_LIST property. */
2709 int client_count;
2710 Window * client_list = get_xaproperty(GDK_ROOT_WINDOW(), a_NET_CLIENT_LIST, XA_WINDOW, &client_count);
2711 if (client_list != NULL)
2712 {
2713 /* Loop over client list, correlating it with task list. */
2714 int i;
2715 for (i = 0; i < client_count; i++)
2716 {
2717 /* Search for the window in the task list. Set up context to do an insert right away if needed. */
2718 Task * tk_pred = NULL;
2719 Task * tk_cursor;
2720 Task * tk = NULL;
2721 for (tk_cursor = tb->p_task_list; tk_cursor != NULL; tk_pred = tk_cursor, tk_cursor = tk_cursor->p_task_flink_xwid)
2722 {
2723 if (tk_cursor->win == client_list[i])
2724 {
2725 tk = tk_cursor;
2726 break;
2727 }
2728 if (tk_cursor->win > client_list[i])
2729 break;
2730 }
2731
2732 /* Task is already in task list. */
2733 if (tk != NULL)
2734 tk->present_in_client_list = TRUE;
2735
2736 /* Task is not in task list. */
2737 else
2738 {
2739 /* Evaluate window state and window type to see if it should be in task list. */
2740 NetWMWindowType nwwt;
2741 NetWMState nws;
2742 get_net_wm_state(client_list[i], &nws);
2743 get_net_wm_window_type(client_list[i], &nwwt);
2744 if ((accept_net_wm_state(&nws))
2745 && (accept_net_wm_window_type(&nwwt)))
2746 {
2747 /* Allocate and initialize new task structure. */
2748 tk = g_new0(Task, 1);
2749 tk->present_in_client_list = TRUE;
2750 tk->win = client_list[i];
2751 tk->tb = tb;
2752 tk->name_source = None;
2753 tk->image_source = None;
2754 tk->iconified = (get_wm_state(tk->win) == IconicState);
2755 tk->desktop = get_net_wm_desktop(tk->win);
2756 if (tb->same_monitor_only)
2757 tk->monitor = get_window_monitor(tk->win);
2758 if (tb->use_urgency_hint)
2759 tk->urgency = task_has_urgency(tk);
2760 task_build_gui(tb, tk);
2761 task_set_names(tk, None);
2762 task_set_class(tk);
2763
2764 /* Link the task structure into the task list. */
2765 if (tk_pred == NULL)
2766 {
2767 tk->p_task_flink_xwid = tb->p_task_list;
2768 tb->p_task_list = tk;
2769 }
2770 else
2771 {
2772 tk->p_task_flink_xwid = tk_pred->p_task_flink_xwid;
2773 tk_pred->p_task_flink_xwid = tk;
2774 }
2775 }
2776 }
2777 }
2778 XFree(client_list);
2779 }
2780
2781 /* Remove windows from the task list that are not present in the NET_CLIENT_LIST. */
2782 Task * tk_pred = NULL;
2783 Task * tk = tb->p_task_list;
2784 while (tk != NULL)
2785 {
2786 Task * tk_succ = tk->p_task_flink_xwid;
2787 if (tk->present_in_client_list)
2788 {
2789 tk->present_in_client_list = FALSE;
2790 tk_pred = tk;
2791 }
2792 else
2793 {
2794 if (tk_pred == NULL)
2795 tb->p_task_list = tk_succ;
2796 else tk_pred->p_task_flink_xwid = tk_succ;
2797 task_delete(tb, tk, FALSE);
2798 }
2799 tk = tk_succ;
2800 }
2801
2802 /* Redraw the taskbar. */
2803 taskbar_redraw(tb);
2804 }
2805
2806 /* Handler for "current-desktop" event from root window listener. */
2807 static void taskbar_net_current_desktop(GtkWidget * widget, LaunchTaskBarPlugin * tb)
2808 {
2809 LaunchTaskBarPlugin *ltbp = tb;
2810 if(!ltbp->tb_on) return;
2811
2812 /* Store the local copy of current desktops. Redisplay the taskbar. */
2813 tb->current_desktop = get_net_current_desktop();
2814 recompute_group_visibility_on_current_desktop(tb);
2815 taskbar_redraw(tb);
2816 }
2817
2818 /* Handler for "number-of-desktops" event from root window listener. */
2819 static void taskbar_net_number_of_desktops(GtkWidget * widget, LaunchTaskBarPlugin * tb)
2820 {
2821 LaunchTaskBarPlugin *ltbp = tb;
2822 if(!ltbp->tb_on) return;
2823
2824 /* Store the local copy of number of desktops. Recompute the popup menu and redisplay the taskbar. */
2825 tb->number_of_desktops = get_net_number_of_desktops();
2826 taskbar_make_menu(tb);
2827 taskbar_redraw(tb);
2828 }
2829
2830 /* Handler for "active-window" event from root window listener. */
2831 static void taskbar_net_active_window(GtkWidget * widget, LaunchTaskBarPlugin * tb)
2832 {
2833 LaunchTaskBarPlugin *ltbp = tb;
2834 if(!ltbp->tb_on) return;
2835
2836 gboolean drop_old = FALSE;
2837 gboolean make_new = FALSE;
2838 Task * ctk = tb->focused;
2839 Task * ntk = NULL;
2840
2841 /* Get the window that has focus. */
2842 Window * f = get_xaproperty(GDK_ROOT_WINDOW(), a_NET_ACTIVE_WINDOW, XA_WINDOW, 0);
2843 if (f == NULL)
2844 {
2845 /* No window has focus. */
2846 drop_old = TRUE;
2847 tb->focused_previous = NULL;
2848 }
2849 else
2850 {
2851 if (*f == panel_get_xwindow(tb->panel))
2852 {
2853 /* Taskbar window gained focus (this isn't supposed to be able to happen). Remember current focus. */
2854 if (ctk != NULL)
2855 {
2856 tb->focused_previous = ctk;
2857 drop_old = TRUE;
2858 }
2859 }
2860 else
2861 {
2862 /* Identify task that gained focus. */
2863 tb->focused_previous = NULL;
2864 ntk = task_lookup(tb, *f);
2865 if (ntk != ctk)
2866 {
2867 drop_old = TRUE;
2868 make_new = TRUE;
2869 }
2870 }
2871 XFree(f);
2872 }
2873
2874 /* If our idea of the current task lost focus, update data structures. */
2875 if ((ctk != NULL) && (drop_old))
2876 {
2877 ctk->focused = FALSE;
2878 tb->focused = NULL;
2879 if(!tb->flat_button) /* relieve the button if flat buttons is not used. */
2880 gtk_toggle_button_set_active((GtkToggleButton*)ctk->button, FALSE);
2881
2882 task_button_redraw(ctk, tb);
2883 }
2884
2885 /* If a task gained focus, update data structures. */
2886 if ((ntk != NULL) && (make_new))
2887 {
2888 if(!tb->flat_button) /* depress the button if flat buttons is not used. */
2889 gtk_toggle_button_set_active((GtkToggleButton*)ntk->button, TRUE);
2890 ntk->focused = TRUE;
2891 tb->focused = ntk;
2892 task_button_redraw(ntk, tb);
2893 }
2894 }
2895
2896 /* Determine if the "urgency" hint is set on a window. */
2897 static gboolean task_has_urgency(Task * tk)
2898 {
2899 gboolean result = FALSE;
2900 XWMHints * hints = (XWMHints *) get_xaproperty(tk->win, XA_WM_HINTS, XA_WM_HINTS, 0);
2901 if (hints != NULL)
2902 {
2903 if (hints->flags & XUrgencyHint)
2904 result = TRUE;
2905 XFree(hints);
2906 }
2907 return result;
2908 }
2909
2910 /* Handle PropertyNotify event.
2911 * http://tronche.com/gui/x/icccm/
2912 * http://standards.freedesktop.org/wm-spec/wm-spec-1.4.html */
2913 static void taskbar_property_notify_event(LaunchTaskBarPlugin *tb, XEvent *ev)
2914 {
2915 /* State may be PropertyNewValue, PropertyDeleted. */
2916 if (((XPropertyEvent*) ev)->state == PropertyNewValue)
2917 {
2918 Atom at = ev->xproperty.atom;
2919 Window win = ev->xproperty.window;
2920 if (win != GDK_ROOT_WINDOW())
2921 {
2922 /* Look up task structure by X window handle. */
2923 Task * tk = task_lookup(tb, win);
2924 if (tk != NULL)
2925 {
2926 /* Install an error handler that ignores BadWindow.
2927 * We frequently get a PropertyNotify event on deleted windows. */
2928 XErrorHandler previous_error_handler = XSetErrorHandler(panel_handle_x_error_swallow_BadWindow_BadDrawable);
2929
2930 /* Dispatch on atom. */
2931 if (at == a_NET_WM_DESKTOP)
2932 {
2933 /* Window changed desktop. */
2934 tk->desktop = get_net_wm_desktop(win);
2935 taskbar_redraw(tb);
2936 }
2937 else if ((at == XA_WM_NAME) || (at == a_NET_WM_NAME) || (at == a_NET_WM_VISIBLE_NAME))
2938 {
2939 /* Window changed name. */
2940 task_set_names(tk, at);
2941 if (tk->p_taskclass != NULL)
2942 {
2943 /* A change to the window name may change the visible name of the class. */
2944 recompute_group_visibility_for_class(tb, tk->p_taskclass);
2945 if (tk->p_taskclass->p_task_visible != NULL)
2946 task_draw_label(tk->p_taskclass->p_task_visible);
2947 }
2948 }
2949 else if (at == XA_WM_CLASS)
2950 {
2951 /* Window changed class. */
2952 task_set_class(tk);
2953 taskbar_redraw(tb);
2954 }