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