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