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