Implement/fix monitors hotplug support.
[lxde/lxpanel.git] / src / misc.c
index a22beeb..30aeb13 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2006 LxDE Developers, see the file AUTHORS for details.
+ * Copyright (c) 2006-2014 LxDE Developers, see the file AUTHORS for details.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <string.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <libfm/fm-gtk.h>
 
 #include "misc.h"
-#include "panel.h"
+#include "private.h"
 
-//#define DEBUG
 #include "dbg.h"
 
+/* data used by themed images buttons */
+typedef struct {
+    FmIcon *icon;
+    guint theme_changed_handler;
+    GdkPixbuf* pixbuf;
+    GdkPixbuf* hilight;
+    gulong hicolor;
+    gint size; /* desired size */
+} ImgData;
+
+static GQuark img_data_id = 0;
+
+static void on_theme_changed(GtkIconTheme* theme, GtkWidget* img);
+static void _gtk_image_set_from_file_scaled(GtkWidget *img, ImgData *data);
 
 /* X11 data types */
 Atom a_UTF8_STRING;
@@ -55,6 +69,7 @@ Atom a_NET_DESKTOP_VIEWPORT;
 Atom a_NET_DESKTOP_NAMES;
 Atom a_NET_ACTIVE_WINDOW;
 Atom a_NET_CLOSE_WINDOW;
+Atom a_NET_SHOWING_DESKTOP;
 Atom a_NET_SUPPORTED;
 Atom a_NET_WM_STATE;
 Atom a_NET_WM_STATE_SKIP_TASKBAR;
@@ -80,8 +95,17 @@ Atom a_NET_WM_STRUT_PARTIAL;
 Atom a_NET_WM_ICON;
 Atom a_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR;
 
+/* SYSTEM TRAY spec */
+Atom a_NET_SYSTEM_TRAY_OPCODE;
+Atom a_NET_SYSTEM_TRAY_MESSAGE_DATA;
+Atom a_NET_SYSTEM_TRAY_ORIENTATION;
+Atom a_MANAGER;
+
 Atom a_LXPANEL_CMD; /* for private client message */
 
+/* if current window manager is EWMH conforming. */
+gboolean is_ewmh_supported;
+
 enum{
     I_UTF8_STRING,
     I_XROOTPMAP_ID,
@@ -89,13 +113,16 @@ enum{
     I_WM_CLASS,
     I_WM_DELETE_WINDOW,
     I_WM_PROTOCOLS,
+
     I_NET_WORKAREA,
     I_NET_CLIENT_LIST,
     I_NET_CLIENT_LIST_STACKING,
     I_NET_NUMBER_OF_DESKTOPS,
     I_NET_CURRENT_DESKTOP,
+    I_NET_DESKTOP_VIEWPORT,
     I_NET_DESKTOP_NAMES,
     I_NET_ACTIVE_WINDOW,
+    I_NET_SHOWING_DESKTOP,
     I_NET_SUPPORTED,
 
     I_NET_WM_STATE,
@@ -123,6 +150,11 @@ enum{
     I_NET_WM_ICON,
     I_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR,
 
+    I_NET_SYSTEM_TRAY_OPCODE,
+    I_NET_SYSTEM_TRAY_MESSAGE_DATA,
+    I_NET_SYSTEM_TRAY_ORIENTATION,
+    I_MANAGER,
+
     I_LXPANEL_CMD,
     N_ATOMS
 };
@@ -163,16 +195,9 @@ pair bool_pair[] = {
     { 1, "1" },
     { 0, NULL },
 };
-pair pos_pair[] = {
-    { POS_NONE, "none" },
-    { POS_START, "start" },
-    { POS_END,  "end" },
-    { 0, NULL},
-};
-
 
 int
-str2num(pair *p, gchar *str, int defval)
+str2num(pair *p, const gchar *str, int defval)
 {
     ENTER;
     for (;p && p->str; p++) {
@@ -182,8 +207,8 @@ str2num(pair *p, gchar *str, int defval)
     RET(defval);
 }
 
-gchar *
-num2str(pair *p, int num, gchar *defval)
+const gchar *
+num2str(pair *p, int num, const gchar *defval)
 {
     ENTER;
     for (;p && p->str; p++) {
@@ -244,31 +269,11 @@ lxpanel_put_line(FILE* fp, const char* format, ...)
     return (ret + 1);
 }
 
-extern int
-lxpanel_put_str( FILE* fp, const char* name, const char* val )
-{
-    if( G_UNLIKELY( !val || !*val ) )
-        return 0;
-    return lxpanel_put_line( fp, "%s=%s", name, val );
-}
-
-extern int
-lxpanel_put_bool( FILE* fp, const char* name, gboolean val )
-{
-    return lxpanel_put_line( fp, "%s=%c", name, val ? '1' : '0' );
-}
-
-extern int
-lxpanel_put_int( FILE* fp, const char* name, int val )
-{
-    return lxpanel_put_line( fp, "%s=%d", name, val );
-}
-
 extern  int
 lxpanel_get_line(char**fp, line *s)
 {
     gchar *tmp, *tmp2;
-    ENTER;
+
     s->type = LINE_NONE;
     if (!fp)
         RET(s->type);
@@ -296,47 +301,11 @@ lxpanel_get_line(char**fp, line *s)
             *tmp = 0;
             s->type = LINE_BLOCK_START;
         } else {
-            ERR( "parser: unknown token: '%c'\n", *tmp2);
+            g_warning( "parser: unknown token: '%c'", *tmp2);
         }
         break;
     }
-    RET(s->type);
-}
-
-int
-get_line_as_is(char** fp, line *s)
-{
-    gchar *tmp, *tmp2;
-
-    ENTER;
-    if (!fp) {
-        s->type = LINE_NONE;
-        RET(s->type);
-    }
-    s->type = LINE_NONE;
-    while (buf_gets(s->str, s->len, fp)) {
-        g_strstrip(s->str);
-        if (s->str[0] == '#' || s->str[0] == 0)
-        continue;
-        DBG( ">> %s\n", s->str);
-        if (!g_ascii_strcasecmp(s->str, "}")) {
-            s->type = LINE_BLOCK_END;
-            DBG( "        : line_block_end\n");
-            break;
-        }
-        for (tmp = s->str; isalnum(*tmp); tmp++);
-        for (tmp2 = tmp; isspace(*tmp2); tmp2++);
-        if (*tmp2 == '=') {
-            s->type = LINE_VAR;
-        } else if  (*tmp2 == '{') {
-            s->type = LINE_BLOCK_START;
-        } else {
-            DBG( "        : ? <%c>\n", *tmp2);
-        }
-        break;
-    }
-    RET(s->type);
-
+    return s->type;
 }
 
 void resolve_atoms()
@@ -354,8 +323,10 @@ void resolve_atoms()
     atom_names[ I_NET_CLIENT_LIST_STACKING ] = "_NET_CLIENT_LIST_STACKING";
     atom_names[ I_NET_NUMBER_OF_DESKTOPS ] = "_NET_NUMBER_OF_DESKTOPS";
     atom_names[ I_NET_CURRENT_DESKTOP ] = "_NET_CURRENT_DESKTOP";
+    atom_names[ I_NET_DESKTOP_VIEWPORT ] = "_NET_DESKTOP_VIEWPORT";
     atom_names[ I_NET_DESKTOP_NAMES ] = "_NET_DESKTOP_NAMES";
     atom_names[ I_NET_ACTIVE_WINDOW ] = "_NET_ACTIVE_WINDOW";
+    atom_names[ I_NET_SHOWING_DESKTOP ] = "_NET_SHOWING_DESKTOP";
     atom_names[ I_NET_SUPPORTED ] = "_NET_SUPPORTED";
     atom_names[ I_NET_WM_DESKTOP ] = "_NET_WM_DESKTOP";
     atom_names[ I_NET_WM_STATE ] = "_NET_WM_STATE";
@@ -382,13 +353,20 @@ void resolve_atoms()
     atom_names[ I_NET_WM_STRUT_PARTIAL ] = "_NET_WM_STRUT_PARTIAL";
     atom_names[ I_NET_WM_ICON ] = "_NET_WM_ICON";
     atom_names[ I_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR ] = "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR";
+
+    atom_names[ I_NET_SYSTEM_TRAY_OPCODE ] = "_NET_SYSTEM_TRAY_OPCODE";
+    atom_names[ I_NET_SYSTEM_TRAY_MESSAGE_DATA ] = "_NET_SYSTEM_TRAY_MESSAGE_DATA";
+    atom_names[ I_NET_SYSTEM_TRAY_ORIENTATION ] = "_NET_SYSTEM_TRAY_ORIENTATION";
+    atom_names[ I_MANAGER ] = "MANAGER";
+
     atom_names[ I_LXPANEL_CMD ] = "_LXPANEL_CMD";
 
     Atom atoms[ N_ATOMS ];
 
     ENTER;
 
-    if( !  XInternAtoms( GDK_DISPLAY(), atom_names, N_ATOMS, False, atoms ) )
+    if( !  XInternAtoms( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), (char**)atom_names,
+            N_ATOMS, False, atoms ) )
     {
         g_warning( "Error: unable to return Atoms" );
         return;
@@ -400,13 +378,16 @@ void resolve_atoms()
     a_WM_CLASS = atoms[ I_WM_CLASS ];
     a_WM_DELETE_WINDOW = atoms[ I_WM_DELETE_WINDOW ];
     a_WM_PROTOCOLS = atoms[ I_WM_PROTOCOLS ];
+
     a_NET_WORKAREA = atoms[ I_NET_WORKAREA ];
     a_NET_CLIENT_LIST = atoms[ I_NET_CLIENT_LIST ];
     a_NET_CLIENT_LIST_STACKING = atoms[ I_NET_CLIENT_LIST_STACKING ];
     a_NET_NUMBER_OF_DESKTOPS = atoms[ I_NET_NUMBER_OF_DESKTOPS ];
     a_NET_CURRENT_DESKTOP = atoms[ I_NET_CURRENT_DESKTOP ];
+    a_NET_DESKTOP_VIEWPORT = atoms[ I_NET_DESKTOP_VIEWPORT ];
     a_NET_DESKTOP_NAMES = atoms[ I_NET_DESKTOP_NAMES ];
     a_NET_ACTIVE_WINDOW = atoms[ I_NET_ACTIVE_WINDOW ];
+    a_NET_SHOWING_DESKTOP = atoms[ I_NET_SHOWING_DESKTOP ];
     a_NET_SUPPORTED = atoms[ I_NET_SUPPORTED ];
     a_NET_WM_STATE = atoms[ I_NET_WM_STATE ];
     a_NET_WM_STATE_SKIP_TASKBAR = atoms[ I_NET_WM_STATE_SKIP_TASKBAR ];
@@ -425,6 +406,7 @@ void resolve_atoms()
     a_NET_WM_WINDOW_TYPE_DIALOG = atoms[ I_NET_WM_WINDOW_TYPE_DIALOG ];
     a_NET_WM_WINDOW_TYPE_NORMAL = atoms[ I_NET_WM_WINDOW_TYPE_NORMAL ];
     a_NET_WM_DESKTOP = atoms[ I_NET_WM_DESKTOP ];
+    a_NET_WM_PID = atoms[ I_NET_WM_PID ];
     a_NET_WM_NAME = atoms[ I_NET_WM_NAME ];
     a_NET_WM_VISIBLE_NAME = atoms[ I_NET_WM_VISIBLE_NAME ];
     a_NET_WM_STRUT = atoms[ I_NET_WM_STRUT ];
@@ -432,6 +414,11 @@ void resolve_atoms()
     a_NET_WM_ICON = atoms[ I_NET_WM_ICON ];
     a_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR = atoms[ I_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR ];
 
+    a_NET_SYSTEM_TRAY_OPCODE = atoms[ I_NET_SYSTEM_TRAY_OPCODE ];
+    a_NET_SYSTEM_TRAY_MESSAGE_DATA = atoms [ I_NET_SYSTEM_TRAY_MESSAGE_DATA ];
+    a_NET_SYSTEM_TRAY_ORIENTATION = atoms[ I_NET_SYSTEM_TRAY_ORIENTATION ];
+    a_MANAGER = atoms[ I_MANAGER ];
+
     a_LXPANEL_CMD = atoms[ I_LXPANEL_CMD ];
 
     RET();
@@ -439,10 +426,9 @@ void resolve_atoms()
 
 
 void
-Xclimsg(Window win, long type, long l0, long l1, long l2, long l3, long l4)
+Xclimsg(Window win, Atom type, long l0, long l1, long l2, long l3, long l4)
 {
     XClientMessageEvent xev;
-
     xev.type = ClientMessage;
     xev.window = win;
     xev.message_type = type;
@@ -452,9 +438,9 @@ Xclimsg(Window win, long type, long l0, long l1, long l2, long l3, long l4)
     xev.data.l[2] = l2;
     xev.data.l[3] = l3;
     xev.data.l[4] = l4;
-    XSendEvent(GDK_DISPLAY(), GDK_ROOT_WINDOW(), False,
+    XSendEvent(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), GDK_ROOT_WINDOW(), False,
           (SubstructureNotifyMask | SubstructureRedirectMask),
-          (XEvent *) & xev);
+          (XEvent *) &xev);
 }
 
 void
@@ -468,14 +454,13 @@ Xclimsgwm(Window win, Atom type, Atom arg)
     xev.format = 32;
     xev.data.l[0] = arg;
     xev.data.l[1] = GDK_CURRENT_TIME;
-    XSendEvent(GDK_DISPLAY(), win, False, 0L, (XEvent *) &xev);
+    XSendEvent(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), win, False, 0L, (XEvent *) &xev);
 }
 
 
 void *
 get_utf8_property(Window win, Atom atom)
 {
-
     Atom type;
     int format;
     gulong nitems;
@@ -486,7 +471,7 @@ get_utf8_property(Window win, Atom atom)
 
     type = None;
     retval = NULL;
-    result = XGetWindowProperty (GDK_DISPLAY(), win, atom, 0, G_MAXLONG, False,
+    result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), win, atom, 0, G_MAXLONG, False,
           a_UTF8_STRING, &type, &format, &nitems,
           &bytes_after, &tmp);
     if (result != Success || type == None)
@@ -505,15 +490,15 @@ char **
 get_utf8_property_list(Window win, Atom atom, int *count)
 {
     Atom type;
-    int format, i;
-    gulong nitems;
+    int format;
+    gulong nitems, i;
     gulong bytes_after;
     gchar *s, **retval = NULL;
     int result;
     guchar *tmp = NULL;
 
     *count = 0;
-    result = XGetWindowProperty(GDK_DISPLAY(), win, atom, 0, G_MAXLONG, False,
+    result = XGetWindowProperty(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), win, atom, 0, G_MAXLONG, False,
           a_UTF8_STRING, &type, &format, &nitems,
           &bytes_after, &tmp);
     if (result != Success || type != a_UTF8_STRING || tmp == NULL)
@@ -527,7 +512,7 @@ get_utf8_property_list(Window win, Atom atom, int *count)
                 (*count)++;
         }
         retval = g_new0 (char*, *count + 2);
-        for (i = 0, s = val; i < *count; i++, s = s +  strlen (s) + 1) {
+        for (i = 0, s = val; (int)i < *count; i++, s = s +  strlen (s) + 1) {
             retval[i] = g_strdup(s);
         }
         if (val[nitems-1]) {
@@ -557,7 +542,7 @@ get_xaproperty (Window win, Atom prop, Atom type, int *nitems)
 
     ENTER;
     prop_data = NULL;
-    if (XGetWindowProperty (GDK_DISPLAY(), win, prop, 0, 0x7fffffff, False,
+    if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), win, prop, 0, G_MAXLONG, False,
               type, &type_ret, &format_ret, &items_ret,
               &after_ret, &prop_data) != Success)
     {
@@ -581,7 +566,8 @@ text_property_to_utf8 (const XTextProperty *prop)
 
   ENTER;
   list = NULL;
-  count = gdk_text_property_to_utf8_list (gdk_x11_xatom_to_atom (prop->encoding),
+  count = gdk_text_property_to_utf8_list_for_display (gdk_display_get_default(),
+                                          gdk_x11_xatom_to_atom (prop->encoding),
                                           prop->format,
                                           prop->value,
                                           prop->nitems,
@@ -606,7 +592,7 @@ get_textproperty(Window win, Atom atom)
     char *retval;
 
     ENTER;
-    if (XGetTextProperty(GDK_DISPLAY(), win, &text_prop, atom)) {
+    if (XGetTextProperty(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), win, &text_prop, atom)) {
         DBG("format=%d enc=%d nitems=%d value=%s   \n",
               text_prop.format,
               text_prop.encoding,
@@ -626,7 +612,7 @@ int
 get_net_number_of_desktops()
 {
     int desknum;
-    guint32 *data;
+    gulong *data;
 
     ENTER;
     data = get_xaproperty (GDK_ROOT_WINDOW(), a_NET_NUMBER_OF_DESKTOPS,
@@ -644,7 +630,7 @@ int
 get_net_current_desktop ()
 {
     int desk;
-    guint32 *data;
+    gulong *data;
 
     ENTER;
     data = get_xaproperty (GDK_ROOT_WINDOW(), a_NET_CURRENT_DESKTOP, XA_CARDINAL, 0);
@@ -660,7 +646,7 @@ int
 get_net_wm_desktop(Window win)
 {
     int desk = 0;
-    guint32 *data;
+    gulong *data;
 
     ENTER;
     data = get_xaproperty (win, a_NET_WM_DESKTOP, XA_CARDINAL, 0);
@@ -675,7 +661,7 @@ GPid
 get_net_wm_pid(Window win)
 {
     GPid pid = 0;
-    guint32 *data;
+    gulong *data;
 
     ENTER;
     data = get_xaproperty (win, a_NET_WM_PID, XA_CARDINAL, 0);
@@ -694,7 +680,7 @@ get_net_wm_state(Window win, NetWMState *nws)
 
 
     ENTER;
-    bzero(nws, sizeof(nws));
+    memset(nws, 0, sizeof(*nws));
     if (!(state = get_xaproperty(win, a_NET_WM_STATE, XA_ATOM, &num3)))
         RET();
 
@@ -724,9 +710,6 @@ get_net_wm_state(Window win, NetWMState *nws)
     RET();
 }
 
-
-
-
 void
 get_net_wm_window_type(Window win, NetWMWindowType *nwwt)
 {
@@ -735,7 +718,7 @@ get_net_wm_window_type(Window win, NetWMWindowType *nwwt)
 
 
     ENTER;
-    bzero(nwwt, sizeof(nwwt));
+    memset(nwwt, 0, sizeof(*nwwt));
     if (!(state = get_xaproperty(win, a_NET_WM_WINDOW_TYPE, XA_ATOM, &num3)))
         RET();
 
@@ -774,10 +757,6 @@ get_net_wm_window_type(Window win, NetWMWindowType *nwwt)
     RET();
 }
 
-
-
-
-
 int
 get_wm_state (Window win)
 {
@@ -793,13 +772,29 @@ get_wm_state (Window win)
     RET(ret);
 }
 
+int panel_handle_x_error(Display * d, XErrorEvent * ev)
+{
+    char buf[256];
+
+    XGetErrorText(d, ev->error_code, buf, 256);
+    g_warning("lxpanel : X error: %s", buf);
+    return 0;    /* Ignored */
+}
+
+int panel_handle_x_error_swallow_BadWindow_BadDrawable(Display * d, XErrorEvent * ev)
+{
+    if ((ev->error_code != BadWindow) && (ev->error_code != BadDrawable))
+        panel_handle_x_error(d, ev);
+    return 0;    /* Ignored */
+}
+
 static void
 calculate_width(int scrw, int wtype, int allign, int margin,
       int *panw, int *x)
 {
     ENTER;
     DBG("scrw=%d\n", scrw);
-    DBG("IN panw=%d\n", *panw);
+    DBG("IN panw=%d, margin=%d\n", *panw, margin);
     //scrw -= 2;
     if (wtype == WIDTH_PERCENT) {
         /* sanity check */
@@ -811,15 +806,11 @@ calculate_width(int scrw, int wtype, int allign, int margin,
     }
     if (allign != ALLIGN_CENTER) {
         if (margin > scrw) {
-            ERR( "margin is bigger then edge size %d > %d. Ignoring margin\n",
+            g_warning( "margin is bigger then edge size %d > %d. Ignoring margin",
                   margin, scrw);
             margin = 0;
         }
-        if (wtype == WIDTH_PERCENT)
-            //*panw = MAX(scrw - margin, *panw);
-            ;
-        else
-            *panw = MIN(scrw - margin, *panw);
+       *panw = MIN(scrw - margin, *panw);
     }
     DBG("OUT panw=%d\n", *panw);
     if (allign == ALLIGN_LEFT)
@@ -834,53 +825,69 @@ calculate_width(int scrw, int wtype, int allign, int margin,
 }
 
 
-void
-calculate_position(Panel *np)
+void _calculate_position(LXPanel *panel)
 {
-    int sswidth, ssheight, minx, miny;
+    Panel *np = panel->priv;
+    GdkScreen *screen;
+    GdkRectangle marea;
 
     ENTER;
     /* FIXME: Why this doesn't work? */
+    /* if you are still going to use this, be sure to update it to take into
+       account multiple monitors */
     if (0)  {
 //        if (np->curdesk < np->wa_len/4) {
-        minx = np->workarea[np->curdesk*4 + 0];
-        miny = np->workarea[np->curdesk*4 + 1];
-        sswidth  = np->workarea[np->curdesk*4 + 2];
-        ssheight = np->workarea[np->curdesk*4 + 3];
+        marea.x = np->workarea[np->curdesk*4 + 0];
+        marea.y = np->workarea[np->curdesk*4 + 1];
+        marea.width  = np->workarea[np->curdesk*4 + 2];
+        marea.height = np->workarea[np->curdesk*4 + 3];
     } else {
-        minx = miny = 0;
-        sswidth  = gdk_screen_get_width( gtk_widget_get_screen(np->topgwin) );
-        ssheight = gdk_screen_get_height( gtk_widget_get_screen(np->topgwin) );
+        screen = gdk_screen_get_default();
+        if (np->monitor < 0) /* all monitors */
+        {
+            marea.x = 0;
+            marea.y = 0;
+            marea.width = gdk_screen_get_width(screen);
+            marea.height = gdk_screen_get_height(screen);
+        }
+        else if (np->monitor < gdk_screen_get_n_monitors(screen))
+            gdk_screen_get_monitor_geometry(screen,np->monitor,&marea);
+        else
+        {
+            marea.x = 0;
+            marea.y = 0;
+            marea.width = 0;
+            marea.height = 0;
+        }
     }
 
     if (np->edge == EDGE_TOP || np->edge == EDGE_BOTTOM) {
         np->aw = np->width;
-        np->ax = minx;
-        calculate_width(sswidth, np->widthtype, np->allign, np->margin,
+        np->ax = marea.x;
+        calculate_width(marea.width, np->widthtype, np->allign, np->margin,
               &np->aw, &np->ax);
-        np->ah = np->height;
-        np->ah = MIN(PANEL_HEIGHT_MAX, np->ah);
-        np->ah = MAX(PANEL_HEIGHT_MIN, np->ah);
-        np->ay = miny + ((np->edge == EDGE_TOP) ? 0 : (ssheight - np->ah));
+        np->ah = ((( ! np->autohide) || (np->visible)) ? np->height : np->height_when_hidden);
+        np->ay = marea.y + ((np->edge == EDGE_TOP) ? 0 : (marea.height - np->ah));
 
     } else {
         np->ah = np->width;
-        np->ay = miny;
-        calculate_width(ssheight, np->widthtype, np->allign, np->margin,
+        np->ay = marea.y;
+        calculate_width(marea.height, np->widthtype, np->allign, np->margin,
               &np->ah, &np->ay);
-        np->aw = np->height;
-        np->aw = MIN(PANEL_HEIGHT_MAX, np->aw);
-        np->aw = MAX(PANEL_HEIGHT_MIN, np->aw);
-        np->ax = minx + ((np->edge == EDGE_LEFT) ? 0 : (sswidth - np->aw));
+        np->aw = ((( ! np->autohide) || (np->visible)) ? np->height : np->height_when_hidden);
+        np->ax = marea.x + ((np->edge == EDGE_LEFT) ? 0 : (marea.width - np->aw));
     }
     //g_debug("%s - x=%d y=%d w=%d h=%d\n", __FUNCTION__, np->ax, np->ay, np->aw, np->ah);
     RET();
 }
 
-
+void calculate_position(Panel *np)
+{
+    _calculate_position(np->topgwin);
+}
 
 gchar *
-expand_tilda(gchar *file)
+expand_tilda(const gchar *file)
 {
     ENTER;
     RET((file[0] == '~') ?
@@ -889,57 +896,6 @@ expand_tilda(gchar *file)
 
 }
 
-
-
-
-#if 0
-Window
-Select_Window(Display *dpy)
-{
-    int status;
-    Cursor cursor;
-    XEvent event;
-    Window target_win = None, root = RootWindow(dpy,DefaultScreen(dpy));
-    int buttons = 0;
-
-    ENTER;
-    /* Make the target cursor */
-    cursor = XCreateFontCursor(dpy, XC_crosshair);
-
-    /* Grab the pointer using target cursor, letting it room all over */
-    status = XGrabPointer(dpy, root, False,
-          ButtonPressMask|ButtonReleaseMask, GrabModeSync,
-          GrabModeAsync, root, cursor, CurrentTime);
-    if (status != GrabSuccess) {
-        ERR("Can't grab the mouse.");
-        RET(None);
-    }
-    /* Let the user select a window... */
-    while ((target_win == None) || (buttons != 0)) {
-        /* allow one more event */
-        XAllowEvents(dpy, SyncPointer, CurrentTime);
-        XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
-        switch (event.type) {
-        case ButtonPress:
-            if (target_win == None) {
-                target_win = event.xbutton.subwindow; /* window selected */
-                DBG("target win = 0x%x\n", target_win);
-                if (target_win == None) target_win = root;
-            }
-            buttons++;
-            break;
-        case ButtonRelease:
-            if (buttons > 0) /* there may have been some down before we started */
-                buttons--;
-            break;
-        }
-    }
-
-    XUngrabPointer(dpy, CurrentTime);      /* Done with pointer */
-    RET(target_win);
-}
-#endif
-
 /*
  * SuxPanel version 0.1
  * Copyright (c) 2003 Leandro Pereira <leandro@linuxmag.com.br>
@@ -952,100 +908,148 @@ Select_Window(Display *dpy)
  *
  */
 
-GdkPixbuf *
-gdk_pixbuf_scale_ratio(GdkPixbuf *p, int width, int height, GdkInterpType itype, gboolean keep_ratio)
+/* DestroyNotify handler for image data in _gtk_image_new_from_file_scaled. */
+static void img_data_free(ImgData * data)
 {
-    gfloat w, h, rw, rh;
-
-    if (keep_ratio) {
-        w = gdk_pixbuf_get_width(p);
-        h = gdk_pixbuf_get_height(p);
-        rw = w / width;
-        rh = h / height;
-        if (rw > rh)
-            height = h / rw;
+    g_object_unref(data->icon);
+    if (data->theme_changed_handler != 0)
+        g_signal_handler_disconnect(gtk_icon_theme_get_default(), data->theme_changed_handler);
+    if (data->pixbuf != NULL)
+        g_object_unref(data->pixbuf);
+    if (data->hilight != NULL)
+        g_object_unref(data->hilight);
+    g_free(data);
+}
+
+/* Handler for "changed" signal in _gtk_image_new_from_file_scaled. */
+static void on_theme_changed(GtkIconTheme * theme, GtkWidget * img)
+{
+    ImgData * data = (ImgData *) g_object_get_qdata(G_OBJECT(img), img_data_id);
+    _gtk_image_set_from_file_scaled(img, data);
+}
+
+/* consumes reference on icon */
+static void _lxpanel_button_set_icon(GtkWidget* btn, FmIcon* icon, gint size)
+{
+    /* Locate the image within the button. */
+    GtkWidget * child = gtk_bin_get_child(GTK_BIN(btn));
+    GtkWidget * img = NULL;
+    if (GTK_IS_IMAGE(child))
+        img = child;
+    else if (GTK_IS_BOX(child))
+    {
+        GList * children = gtk_container_get_children(GTK_CONTAINER(child));
+        img = GTK_WIDGET(GTK_IMAGE(children->data));
+        g_list_free(children);
+    }
+
+    if (img != NULL)
+    {
+        ImgData * data = (ImgData *) g_object_get_qdata(G_OBJECT(img), img_data_id);
+
+        if (icon != data->icon || size != data->size) /* something was changed */
+        {
+            g_object_unref(data->icon);
+            data->icon = icon;
+            data->size = size;
+            _gtk_image_set_from_file_scaled(img, data);
+        }
         else
-            width =  w / rh;
+            g_object_unref(icon);
     }
-    return  gdk_pixbuf_scale_simple(p, width, height, itype);
+    else
+        g_object_unref(icon);
+}
 
+void lxpanel_button_set_icon(GtkWidget* btn, const gchar *name, gint size)
+{
+    _lxpanel_button_set_icon(btn, fm_icon_from_name(name), size);
 }
 
+void lxpanel_button_update_icon(GtkWidget* btn, FmIcon *icon, gint size)
+{
+    _lxpanel_button_set_icon(btn, g_object_ref(icon), size);
+}
 
-GtkWidget *
-gtk_image_new_from_file_scaled(const gchar *file, gint width,
-      gint height, gboolean keep_ratio)
+/* parameters width and keep_ratio are unused, kept for backward compatibility */
+void fb_button_set_from_file(GtkWidget * btn, const char * img_file, gint width, gint height, gboolean keep_ratio)
 {
-    GtkWidget *img;
-    GdkPixbuf /*- *pb, -*/ *pb_scaled;
-    // gfloat w, h, rw, rh;
-    GtkIconInfo *inf = NULL;
+    lxpanel_button_set_icon(btn, img_file, height);
+}
 
-    ENTER;
+static void _gtk_image_set_from_file_scaled(GtkWidget * img, ImgData * data)
+{
+    if (data->pixbuf != NULL)
+    {
+        g_object_unref(data->pixbuf);
+        data->pixbuf = NULL;
+    }
+
+    /* if there is a cached hilighted version of this pixbuf, free it */
+    if (data->hilight != NULL)
+    {
+        g_object_unref(data->hilight);
+        data->hilight = NULL;
+    }
 
-    if( G_UNLIKELY( ! file ) )
-        goto err;
+    if (G_LIKELY(G_IS_THEMED_ICON(data->icon)))
+        data->pixbuf = fm_pixbuf_from_icon_with_fallback(data->icon, data->size,
+                                                         "application-x-executable");
+    else
+    {
+        char *file = g_icon_to_string(fm_icon_get_gicon(data->icon));
+        data->pixbuf = gdk_pixbuf_new_from_file_at_scale(file, -1, data->size, TRUE, NULL);
+        g_free(file);
+    }
 
-    if ( ! g_file_test(file, G_FILE_TEST_EXISTS))
+    if (data->pixbuf != NULL)
     {
-        /* FIXME: should reload icon when theme gets changed */
-        inf = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(),
-                                         file, MAX(width, height), 0);
-        if( ! inf )
-            goto err;
-        file = gtk_icon_info_get_filename(inf);
+        /* Set the pixbuf into the image widget. */
+        gtk_image_set_from_pixbuf((GtkImage *)img, data->pixbuf);
     }
+    else
+    {
+        /* No pixbuf available.  Set the "missing image" icon. */
+        gtk_image_set_from_stock(GTK_IMAGE(img), GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_BUTTON);
+    }
+}
 
-#if GTK_CHECK_VERSION( 2, 6, 0 )
-    pb_scaled = gdk_pixbuf_new_from_file_at_scale( file, width, height,
-                                                   keep_ratio, NULL );
-    if( !pb_scaled )
-        goto err;
-#else
-    if (!(pb = gdk_pixbuf_new_from_file(file, NULL)))
-        goto err;
-
-    if (keep_ratio) {
-        w = gdk_pixbuf_get_width(pb);
-        h = gdk_pixbuf_get_height(pb);
-        rw = w / width;
-        rh = h / height;
-        if (rw > rh)
-            height = h / rw;
-        else
-            width =  w / rh;
+/* consumes reference on icon */
+static GtkWidget *_gtk_image_new_for_icon(FmIcon *icon, gint size)
+{
+    GtkWidget * img = gtk_image_new();
+    ImgData * data = g_new0(ImgData, 1);
+
+    data->icon = icon;
+    data->size = size;
+    if (img_data_id == 0)
+        img_data_id = g_quark_from_static_string("ImgData");
+    g_object_set_qdata_full(G_OBJECT(img), img_data_id, data, (GDestroyNotify) img_data_free);
+    _gtk_image_set_from_file_scaled(img, data);
+    if (G_IS_THEMED_ICON(data->icon))
+    {
+        /* This image is loaded from icon theme.  Update the image if the icon theme is changed. */
+        data->theme_changed_handler = g_signal_connect(gtk_icon_theme_get_default(), "changed", G_CALLBACK(on_theme_changed), img);
     }
-    pb_scaled = gdk_pixbuf_scale_simple(pb, width, height,
-                                        GDK_INTERP_BILINEAR);
-    g_object_unref(pb);
-#endif
-    img = gtk_image_new_from_pixbuf(pb_scaled);
-    g_object_unref(pb_scaled);
-
-    if( inf )
-        gtk_icon_info_free ( inf );
-
-    RET(img);
-
- err:
-    img = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
-                                   GTK_ICON_SIZE_BUTTON);
-    RET(img);
+    return img;
 }
 
+/* parameters width and keep_ratio are unused, kept for backward compatibility */
+GtkWidget * _gtk_image_new_from_file_scaled(const gchar * file, gint width, gint height, gboolean keep_ratio)
+{
+    return _gtk_image_new_for_icon(fm_icon_from_name(file), height);
+}
 
 void
 get_button_spacing(GtkRequisition *req, GtkContainer *parent, gchar *name)
 {
     GtkWidget *b;
-    //gint focus_width;
-    //gint focus_pad;
 
     ENTER;
     b = gtk_button_new();
     gtk_widget_set_name(GTK_WIDGET(b), name);
-    GTK_WIDGET_UNSET_FLAGS (b, GTK_CAN_FOCUS);
-    GTK_WIDGET_UNSET_FLAGS (b, GTK_CAN_DEFAULT);
+    gtk_widget_set_can_focus(b, FALSE);
+    gtk_widget_set_can_default(b, FALSE);
     gtk_container_set_border_width (GTK_CONTAINER (b), 0);
 
     if (parent)
@@ -1062,15 +1066,9 @@ get_button_spacing(GtkRequisition *req, GtkContainer *parent, gchar *name)
 guint32 gcolor2rgb24(GdkColor *color)
 {
     guint32 i;
-    guint16 r, g, b;
 
     ENTER;
 
-    r = color->red * 0xFF / 0xFFFF;
-    g = color->green * 0xFF / 0xFFFF;
-    b = color->blue * 0xFF / 0xFFFF;
-    DBG("%x %x %x ==> %x %x %x\n", color->red, color->green, color->blue, r, g, b);
-
     i = (color->red * 0xFF / 0xFFFF) & 0xFF;
     i <<= 8;
     i |= (color->green * 0xFF / 0xFFFF) & 0xFF;
@@ -1080,169 +1078,144 @@ guint32 gcolor2rgb24(GdkColor *color)
     RET(i);
 }
 
-
-static gboolean
-fb_button_enter (GtkImage *widget, GdkEventCrossing *event)
+/* Handler for "enter-notify-event" signal on image that has highlighting requested. */
+static gboolean fb_button_enter(GtkImage * widget, GdkEventCrossing * event)
 {
-    GdkPixbuf *dark, *light;
-    int i;
-    gulong hicolor;
-    guchar *src, *up, extra[3];
+    if (gtk_image_get_storage_type(widget) == GTK_IMAGE_PIXBUF)
+    {
+        ImgData * data = (ImgData *) g_object_get_qdata(G_OBJECT(widget), img_data_id);
+        if (data != NULL)
+        {
+            if (data->hilight == NULL)
+            {
+                GdkPixbuf * dark = data->pixbuf;
+                int height = gdk_pixbuf_get_height(dark);
+                int rowstride = gdk_pixbuf_get_rowstride(dark);
+                gulong hicolor = data->hicolor;
 
-    ENTER;
-    if (gtk_image_get_storage_type(widget) != GTK_IMAGE_PIXBUF)
-        RET(TRUE);
-    light = g_object_get_data(G_OBJECT(widget), "light");
-    dark = gtk_image_get_pixbuf(widget);
-    if (!light) {
-        hicolor = (gulong) g_object_get_data(G_OBJECT(widget), "hicolor");
-        light = gdk_pixbuf_add_alpha(dark, FALSE, 0, 0, 0);
-        if (!light)
-            RET(TRUE);
-        src = gdk_pixbuf_get_pixels (light);
-        for (i = 2; i >= 0; i--, hicolor >>= 8)
-            extra[i] = hicolor & 0xFF;
-        for (up = src + gdk_pixbuf_get_height(light) * gdk_pixbuf_get_rowstride (light);
-             src < up; src+=4) {
-            if (src[3] == 0)
-                continue;
-            for (i = 0; i < 3; i++) {
-                if (src[i] + extra[i] >= 255)
-                    src[i] = 255;
-                else
-                    src[i] += extra[i];
+                GdkPixbuf * light = gdk_pixbuf_add_alpha(dark, FALSE, 0, 0, 0);
+                if (light != NULL)
+                {
+                    guchar extra[3];
+                    int i;
+                    for (i = 2; i >= 0; i--, hicolor >>= 8)
+                        extra[i] = hicolor & 0xFF;
+
+                    guchar * src = gdk_pixbuf_get_pixels(light);
+                    guchar * up;
+                    for (up = src + height * rowstride; src < up; src += 4)
+                    {
+                        if (src[3] != 0)
+                        {
+                            for (i = 0; i < 3; i++)
+                            {
+                            int value = src[i] + extra[i];
+                            if (value > 255) value = 255;
+                            src[i] = value;
+                            }
+                        }
+                    }
+                data->hilight = light;
+                }
             }
+
+        if (data->hilight != NULL)
+            gtk_image_set_from_pixbuf(widget, data->hilight);
         }
-        g_object_set_data_full (G_OBJECT(widget), "light", light, g_object_unref);
     }
-    g_object_ref(dark);
-    g_object_set_data_full (G_OBJECT(widget), "dark", dark, g_object_unref);
-    gtk_image_set_from_pixbuf(widget, light);
-    RET(TRUE);
-
+    return TRUE;
 }
 
-static gboolean
-fb_button_leave (GtkImage *widget, GdkEventCrossing *event, gpointer user_data)
+/* Handler for "leave-notify-event" signal on image that has highlighting requested. */
+static gboolean fb_button_leave(GtkImage * widget, GdkEventCrossing * event, gpointer user_data)
 {
-    GdkPixbuf *dark;
-
-    ENTER;
-    if (gtk_image_get_storage_type(widget) != GTK_IMAGE_PIXBUF)
-        RET(TRUE);
-    dark = g_object_get_data(G_OBJECT(widget), "dark");
-    if (dark)
-        gtk_image_set_from_pixbuf(widget, dark);
-    RET(TRUE);
+    if (gtk_image_get_storage_type(widget) == GTK_IMAGE_PIXBUF)
+    {
+        ImgData * data = (ImgData *) g_object_get_qdata(G_OBJECT(widget), img_data_id);
+        if ((data != NULL) && (data->pixbuf != NULL))
+            gtk_image_set_from_pixbuf(widget, data->pixbuf);
+    }
+    return TRUE;
 }
 
 
-GtkWidget *
-fb_button_new_from_file(gchar *fname, int width, int height, gulong hicolor, gboolean keep_ratio)
+/* consumes reference on icon */
+static GtkWidget *_lxpanel_button_new_for_icon(LXPanel *panel, FmIcon *icon,
+                                               gint size, gulong highlight_color,
+                                               const gchar *label)
 {
-    GtkWidget *b, *image;
-    ENTER;
-//    b = gtk_vbox_new(FALSE, 0); //gtk_bgbox_new();
-    b = gtk_event_box_new();
-    gtk_container_set_border_width(GTK_CONTAINER(b), 0);
-    GTK_WIDGET_UNSET_FLAGS (b, GTK_CAN_FOCUS);
-
-    image = gtk_image_new_from_file_scaled(fname, width, height, keep_ratio);
-    gtk_misc_set_alignment(GTK_MISC(image), 0, 0);
-    g_object_set_data(G_OBJECT(image), "hicolor", (gpointer)hicolor);
-    gtk_misc_set_padding (GTK_MISC(image), 0, 0);
-    if (hicolor > 0) {
-        gtk_widget_add_events(b, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
-        g_signal_connect_swapped (G_OBJECT (b), "enter-notify-event",
-              G_CALLBACK (fb_button_enter), image);
-        g_signal_connect_swapped (G_OBJECT (b), "leave-notify-event",
-              G_CALLBACK (fb_button_leave), image);
+    GtkWidget * event_box = gtk_event_box_new();
+    gtk_container_set_border_width(GTK_CONTAINER(event_box), 0);
+    gtk_widget_set_can_focus(event_box, FALSE);
+
+    GtkWidget * image = _gtk_image_new_for_icon(icon, size);
+    gtk_misc_set_padding(GTK_MISC(image), 0, 0);
+    gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.5);
+    if (highlight_color != 0)
+    {
+        ImgData * data = (ImgData *) g_object_get_qdata(G_OBJECT(image), img_data_id);
+        data->hicolor = highlight_color;
+
+        gtk_widget_add_events(event_box, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
+        g_signal_connect_swapped(G_OBJECT(event_box), "enter-notify-event", G_CALLBACK(fb_button_enter), image);
+        g_signal_connect_swapped(G_OBJECT(event_box), "leave-notify-event", G_CALLBACK(fb_button_leave), image);
     }
-    gtk_container_add(GTK_CONTAINER(b), image);
-    gtk_widget_show(image);
-    gtk_widget_show(b);
-    RET(b);
+
+    if (label == NULL)
+        gtk_container_add(GTK_CONTAINER(event_box), image);
+    else
+    {
+        GtkWidget * inner = gtk_hbox_new(FALSE, 0);
+        gtk_container_set_border_width(GTK_CONTAINER(inner), 0);
+        gtk_widget_set_can_focus(inner, FALSE);
+        gtk_container_add(GTK_CONTAINER(event_box), inner);
+
+        gtk_box_pack_start(GTK_BOX(inner), image, FALSE, FALSE, 0);
+
+        GtkWidget * lbl = gtk_label_new("");
+        lxpanel_draw_label_text(panel, lbl, label, FALSE, 1, TRUE);
+        gtk_misc_set_padding(GTK_MISC(lbl), 2, 0);
+        gtk_box_pack_end(GTK_BOX(inner), lbl, FALSE, FALSE, 0);
+    }
+
+    gtk_widget_show_all(event_box);
+    return event_box;
 }
 
-GtkWidget *
-fb_button_new_from_file_with_colorlabel(gchar *fname, int width, int height,
-      gulong hicolor, gulong fcolor, gboolean keep_ratio, gchar *name)
+GtkWidget *lxpanel_button_new_for_icon(LXPanel *panel, const gchar *name, GdkColor *color, const gchar *label)
 {
-    GtkWidget *b, *image, *box, *label;
+    gulong highlight_color = color ? gcolor2rgb24(color) : PANEL_ICON_HIGHLIGHT;
+    return _lxpanel_button_new_for_icon(panel, fm_icon_from_name(name),
+                                        panel->priv->icon_size, highlight_color, label);
+}
 
-    ENTER;
-    b = gtk_event_box_new();
-    gtk_container_set_border_width(GTK_CONTAINER(b), 0);
-    GTK_WIDGET_UNSET_FLAGS (b, GTK_CAN_FOCUS);
-
-    box = gtk_hbox_new(FALSE, 0);
-    gtk_container_set_border_width(GTK_CONTAINER(box), 0);
-    GTK_WIDGET_UNSET_FLAGS (box, GTK_CAN_FOCUS);
-    gtk_container_add(GTK_CONTAINER(b), box);
-
-    image = gtk_image_new_from_file_scaled(fname, width, height, keep_ratio);
-    g_object_set_data(G_OBJECT(image), "hicolor", (gpointer)hicolor);
-    gtk_misc_set_padding (GTK_MISC(image), 0, 0);
-    if (hicolor > 0) {
-        gtk_widget_add_events(b, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
-        g_signal_connect_swapped (G_OBJECT (b), "enter-notify-event",
-              G_CALLBACK (fb_button_enter), image);
-        g_signal_connect_swapped (G_OBJECT (b), "leave-notify-event",
-              G_CALLBACK (fb_button_leave), image);
-    }
-    gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
-    if (name) {
-        label =  gtk_label_new("");
-        char *lname = g_strdup_printf("<span color=\"#%06x\">%s</span>", fcolor, name);
-       gtk_label_set_markup(GTK_LABEL(label), lname);
-        gtk_misc_set_padding(GTK_MISC(label), 2, 0);
-       g_free(lname);
-        gtk_box_pack_end(GTK_BOX(box), label, FALSE, FALSE, 0);
-    }
-    gtk_widget_show_all(b);
-    RET(b);
+GtkWidget *lxpanel_button_new_for_fm_icon(LXPanel *panel, FmIcon *icon, GdkColor *color, const gchar *label)
+{
+    gulong highlight_color = color ? gcolor2rgb24(color) : PANEL_ICON_HIGHLIGHT;
+    return _lxpanel_button_new_for_icon(panel, g_object_ref(icon),
+                                        panel->priv->icon_size, highlight_color, label);
 }
 
-GtkWidget *
-fb_button_new_from_file_with_label(gchar *fname, int width, int height,
-      gulong hicolor, gboolean keep_ratio, gchar *name)
+/* parameters width and keep_ratio are unused, kept for backward compatibility */
+GtkWidget * fb_button_new_from_file(
+    const gchar * image_file, int width, int height, gulong highlight_color, gboolean keep_ratio)
 {
-    GtkWidget *b, *image, *box, *label;
+    return fb_button_new_from_file_with_label(image_file, width, height, highlight_color, keep_ratio, NULL, NULL);
+}
 
-    ENTER;
-    b = gtk_event_box_new();
-    gtk_container_set_border_width(GTK_CONTAINER(b), 0);
-    GTK_WIDGET_UNSET_FLAGS (b, GTK_CAN_FOCUS);
-
-    box = gtk_hbox_new(FALSE, 0);
-    gtk_container_set_border_width(GTK_CONTAINER(box), 0);
-    GTK_WIDGET_UNSET_FLAGS (box, GTK_CAN_FOCUS);
-    gtk_container_add(GTK_CONTAINER(b), box);
-
-    image = gtk_image_new_from_file_scaled(fname, width, height, keep_ratio);
-    g_object_set_data(G_OBJECT(image), "hicolor", (gpointer)hicolor);
-    gtk_misc_set_padding (GTK_MISC(image), 0, 0);
-    if (hicolor > 0) {
-        gtk_widget_add_events(b, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
-        g_signal_connect_swapped (G_OBJECT (b), "enter-notify-event",
-              G_CALLBACK (fb_button_enter), image);
-        g_signal_connect_swapped (G_OBJECT (b), "leave-notify-event",
-              G_CALLBACK (fb_button_leave), image);
-    }
-    gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
-    if (name) {
-        label =  gtk_label_new(name);
-        gtk_misc_set_padding(GTK_MISC(label), 2, 0);
-        gtk_box_pack_end(GTK_BOX(box), label, FALSE, FALSE, 0);
-    }
-    gtk_widget_show_all(b);
-    RET(b);
+/* parameters width and keep_ratio are unused, kept for backward compatibility */
+GtkWidget * fb_button_new_from_file_with_label(
+    const gchar * image_file, int width, int height, gulong highlight_color, gboolean keep_ratio, Panel * panel, const gchar * label)
+{
+    return _lxpanel_button_new_for_icon(panel->topgwin, fm_icon_from_name(image_file), height, highlight_color, label);
 }
 
 char* translate_exec_to_cmd( const char* exec, const char* icon,
                              const char* title, const char* fpath )
 {
     GString* cmd = g_string_sized_new( 256 );
+    if (!exec)
+           return NULL;
     for( ; *exec; ++exec )
     {
         if( G_UNLIKELY(*exec == '%') )
@@ -1253,7 +1226,10 @@ char* translate_exec_to_cmd( const char* exec, const char* icon,
             switch( *exec )
             {
                 case 'c':
-                    g_string_append( cmd, title );
+                    if( title )
+                    {
+                        g_string_append( cmd, title );
+                    }
                     break;
                 case 'i':
                     if( icon )
@@ -1263,12 +1239,13 @@ char* translate_exec_to_cmd( const char* exec, const char* icon,
                     }
                     break;
                 case 'k':
-                {
-                    char* uri = g_filename_to_uri( fpath, NULL, NULL );
-                    g_string_append( cmd, uri );
-                    g_free( uri );
+                    if( fpath )
+                    {
+                        char* uri = g_filename_to_uri( fpath, NULL, NULL );
+                        g_string_append( cmd, uri );
+                        g_free( uri );
+                    }
                     break;
-                }
                 case '%':
                     g_string_append_c( cmd, '%' );
                     break;
@@ -1288,51 +1265,232 @@ char* translate_exec_to_cmd( const char* exec, const char* icon,
  any plugin with a layout box. Since GtkHBox cannot be changed to GtkVBox,
  recreating a new box to replace the old one is required.
 */
+/* for compatibility with old plugins */
 GtkWidget* recreate_box( GtkBox* oldbox, GtkOrientation orientation )
 {
-    GtkBox* newbox;
-    GList *child, *children;
-    GtkWidget* (*my_box_new) (gboolean homogeneous, gint spacing);
+    gtk_orientable_set_orientation(GTK_ORIENTABLE(oldbox), orientation);
+    return GTK_WIDGET(oldbox);
+}
+
+/* for compatibility with old plugins */
+void show_error( GtkWindow* parent_win, const char* msg )
+{
+    fm_show_error(parent_win, NULL, msg);
+}
+
+/* old plugins compatibility mode, use fm_pixbuf_from_icon_with_fallback() instead */
+GdkPixbuf * lxpanel_load_icon(const char * name, int width, int height, gboolean use_fallback)
+{
+    FmIcon * fm_icon;
+    GdkPixbuf * icon = NULL;
+
+    fm_icon = fm_icon_from_name(name ? name : "application-x-executable");
+    /* well, we don't use parameter width and not really use cache here */
+    icon = fm_pixbuf_from_icon_with_fallback(fm_icon, height,
+                            use_fallback ? "application-x-executable" : NULL);
+    g_object_unref(fm_icon);
+    return icon;
+}
+
+/*
+ * Taken from pcmanfm:
+ * Parse Exec command line of app desktop file, and translate
+ * it into a real command which can be passed to g_spawn_command_line_async().
+ * file_list is a null-terminated file list containing full
+ * paths of the files passed to app.
+ * returned char* should be freed when no longer needed.
+ */
+static char* translate_app_exec_to_command_line( const char* pexec,
+                                                 GList* file_list )
+{
+    char* file;
+    GList* l;
+    gchar *tmp;
+    GString* cmd = g_string_new("");
+    gboolean add_files = FALSE;
 
-    if( GTK_IS_HBOX(oldbox) ) {
-        if( orientation == GTK_ORIENTATION_HORIZONTAL )
-            return GTK_WIDGET(oldbox);
+    for( ; *pexec; ++pexec )
+    {
+        if( *pexec == '%' )
+        {
+            ++pexec;
+            switch( *pexec )
+            {
+            case 'U':
+                for( l = file_list; l; l = l->next )
+                {
+                    tmp = g_filename_to_uri( (char*)l->data, NULL, NULL );
+                    file = g_shell_quote( tmp );
+                    g_free( tmp );
+                    g_string_append( cmd, file );
+                    if (l->next)
+                        g_string_append_c( cmd, ' ' );
+                    g_free( file );
+                }
+                add_files = TRUE;
+                break;
+            case 'u':
+                if( file_list && file_list->data )
+                {
+                    file = (char*)file_list->data;
+                    tmp = g_filename_to_uri( file, NULL, NULL );
+                    file = g_shell_quote( tmp );
+                    g_free( tmp );
+                    g_string_append( cmd, file );
+                    g_free( file );
+                    add_files = TRUE;
+                }
+                break;
+            case 'F':
+            case 'N':
+                for( l = file_list; l; l = l->next )
+                {
+                    file = (char*)l->data;
+                    tmp = g_shell_quote( file );
+                    g_string_append( cmd, tmp );
+                    if (l->next)
+                        g_string_append_c( cmd, ' ' );
+                    g_free( tmp );
+                }
+                add_files = TRUE;
+                break;
+            case 'f':
+            case 'n':
+                if( file_list && file_list->data )
+                {
+                    file = (char*)file_list->data;
+                    tmp = g_shell_quote( file );
+                    g_string_append( cmd, tmp );
+                    g_free( tmp );
+                    add_files = TRUE;
+                }
+                break;
+            case 'D':
+                for( l = file_list; l; l = l->next )
+                {
+                    tmp = g_path_get_dirname( (char*)l->data );
+                    file = g_shell_quote( tmp );
+                    g_free( tmp );
+                    g_string_append( cmd, file );
+                    if (l->next)
+                        g_string_append_c( cmd, ' ' );
+                    g_free( file );
+                }
+                add_files = TRUE;
+                break;
+            case 'd':
+                if( file_list && file_list->data )
+                {
+                    tmp = g_path_get_dirname( (char*)file_list->data );
+                    file = g_shell_quote( tmp );
+                    g_free( tmp );
+                    g_string_append( cmd, file );
+                    g_free( tmp );
+                    add_files = TRUE;
+                }
+                break;
+            case 'c':
+                #if 0
+                g_string_append( cmd, vfs_app_desktop_get_disp_name( app ) );
+                #endif
+                break;
+            case 'i':
+                /* Add icon name */
+                #if 0
+                if( vfs_app_desktop_get_icon_name( app ) )
+                {
+                    g_string_append( cmd, "--icon " );
+                    g_string_append( cmd, vfs_app_desktop_get_icon_name( app ) );
+                }
+                #endif
+                break;
+            case 'k':
+                /* Location of the desktop file */
+                break;
+            case 'v':
+                /* Device name */
+                break;
+            case '%':
+                g_string_append_c ( cmd, '%' );
+                break;
+            case '\0':
+                goto _finish;
+                break;
+            }
+        }
+        else  /* not % escaped part */
+        {
+            g_string_append_c ( cmd, *pexec );
+        }
     }
-    else {
-        if( orientation == GTK_ORIENTATION_VERTICAL )
-            return GTK_WIDGET(oldbox);
+_finish:
+    if( ! add_files )
+    {
+        for( l = file_list; l; l = l->next )
+        {
+            g_string_append_c( cmd, ' ' );
+            file = (char*)l->data;
+            tmp = g_shell_quote( file );
+            g_string_append( cmd, tmp );
+            g_free( tmp );
+        }
     }
-    my_box_new = (orientation == GTK_ORIENTATION_HORIZONTAL ? gtk_hbox_new : gtk_vbox_new);
-
-    newbox = GTK_BOX(my_box_new( gtk_box_get_homogeneous(oldbox),
-                                 gtk_box_get_spacing(oldbox) ));
-    gtk_container_set_border_width (GTK_CONTAINER (newbox),
-                                    gtk_container_get_border_width(GTK_CONTAINER(oldbox)) );
-    children = gtk_container_get_children( GTK_CONTAINER (oldbox) );
-    for( child = children; child; child = child->next ) {
-        gboolean expand, fill;
-        guint padding;
-        GtkWidget* w = GTK_WIDGET(child->data);
-        gtk_box_query_child_packing( oldbox, w,
-                                     &expand, &fill, &padding, NULL );
-        /* g_debug( "repack %s, expand=%d, fill=%d", gtk_widget_get_name(w), expand, fill ); */
-        g_object_ref( w );
-        gtk_container_remove( GTK_CONTAINER (oldbox), w );
-        gtk_box_pack_start( newbox, w, expand, fill, padding );
-        g_object_unref( w );
+
+    return g_string_free( cmd, FALSE );
+}
+
+gboolean spawn_command_async(GtkWindow *parent_window, gchar const* workdir,
+        gchar const* cmd)
+{
+    GError* err = NULL;
+    gchar** argv = NULL;
+
+    g_info("lxpanel: spawning \"%s\"...", cmd);
+
+    g_shell_parse_argv(cmd, NULL, &argv, &err);
+    if (!err)
+        g_spawn_async(workdir, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &err);
+
+    if (err)
+    {
+        g_warning("%s\n", err->message);
+        fm_show_error(parent_window, NULL, err->message);
+        g_error_free(err);
     }
-    g_list_free( children );
-    gtk_widget_show_all( GTK_WIDGET(newbox) );
-    gtk_widget_destroy( GTK_WIDGET(oldbox) );
-    return GTK_WIDGET(newbox);
+
+    g_strfreev(argv);
+
+    return !err;
 }
 
-void show_error( GtkWindow* parent_win, const char* msg )
+/* FIXME: this should be replaced with fm_launch_file_simple() */
+gboolean lxpanel_launch_app(const char* exec, GList* files, gboolean in_terminal, char const* in_workdir)
 {
-    GtkWidget* dlg = gtk_message_dialog_new( parent_win,
-                                             GTK_DIALOG_MODAL,
-                                             GTK_MESSAGE_ERROR,
-                                             GTK_BUTTONS_OK, msg );
-    gtk_dialog_run( (GtkDialog*)dlg );
-    gtk_widget_destroy( dlg );
+    GError *error = NULL;
+    char* cmd;
+    if( ! exec )
+        return FALSE;
+    cmd = translate_app_exec_to_command_line(exec, files);
+    if( in_terminal )
+    {
+       char * escaped_cmd = g_shell_quote(cmd);
+        char* term_cmd;
+        const char* term = fm_config->terminal ? fm_config->terminal : "lxterminal";
+        if( strstr(term, "%s") )
+            term_cmd = g_strdup_printf(term, escaped_cmd);
+        else
+            term_cmd = g_strconcat( term, " -e ", escaped_cmd, NULL );
+       g_free(escaped_cmd);
+        if( cmd != exec )
+            g_free(cmd);
+        cmd = term_cmd;
+    }
+
+    spawn_command_async(NULL, in_workdir, cmd);
+
+    g_free(cmd);
+
+    return (error == NULL);
 }
+
+/* vim: set sw=4 et sts=4 : */