summaryrefslogtreecommitdiff
path: root/panel-plugin/title.c
diff options
context:
space:
mode:
Diffstat (limited to 'panel-plugin/title.c')
-rw-r--r--panel-plugin/title.c230
1 files changed, 230 insertions, 0 deletions
diff --git a/panel-plugin/title.c b/panel-plugin/title.c
new file mode 100644
index 0000000..2252271
--- /dev/null
+++ b/panel-plugin/title.c
@@ -0,0 +1,230 @@
+#include <gtk/gtk.h>
+
+#include <libxfce4panel/xfce-panel-plugin.h>
+#include <libxfce4panel/libxfce4panel.h>
+
+#include <libwnck/libwnck.h>
+
+#include <glib.h>
+#include <glib-object.h>
+
+typedef struct {
+ XfcePanelPlugin *plugin;
+
+ GtkWidget *button;
+ GtkWidget *label;
+ GtkWidget *menu;
+
+ WnckScreen *screen;
+ WnckWindow *window;
+
+ gint screen_id;
+ gint wnck_screen_id;
+ gint wnck_window_id;
+} TitlePlugin;
+
+static void title_construct(XfcePanelPlugin *plugin);
+
+XFCE_PANEL_PLUGIN_REGISTER(title_construct);
+
+static void title_free(XfcePanelPlugin *plugin, TitlePlugin *title)
+{
+ if (title->screen_id != 0)
+ g_signal_handler_disconnect(plugin, title->screen_id);
+
+ if (title->wnck_screen_id != 0)
+ g_signal_handler_disconnect(title->screen, title->wnck_screen_id);
+
+ if (title->wnck_window_id != 0)
+ g_signal_handler_disconnect(title->window, title->wnck_window_id);
+
+ if (title->menu != NULL)
+ gtk_widget_destroy(title->menu);
+
+ title->screen_id = 0;
+ title->wnck_screen_id = 0;
+ title->wnck_window_id = 0;
+
+ gtk_widget_destroy(title->button);
+ panel_slice_free(TitlePlugin, title);
+}
+
+static gboolean title_size_changed(XfcePanelPlugin *plugin,
+ gint size,
+ G_GNUC_UNUSED TitlePlugin *title)
+{
+ GtkOrientation orientation;
+
+ orientation = xfce_panel_plugin_get_orientation(plugin);
+
+ if (orientation == GTK_ORIENTATION_HORIZONTAL)
+ gtk_widget_set_size_request(GTK_WIDGET(plugin), -1, size);
+ else
+ gtk_widget_set_size_request(GTK_WIDGET(plugin), size, -1);
+
+ return TRUE;
+}
+
+static void title_update_label(TitlePlugin *title, const char *label)
+{
+ gtk_label_set_label(GTK_LABEL(title->label), label);
+ gtk_widget_set_tooltip_text(title->button, label);
+}
+
+static void title_window_name_changed(WnckWindow *window,
+ TitlePlugin *title)
+{
+ const gchar *name = wnck_window_get_name(window);
+ title_update_label(title, name);
+}
+
+static int title_check_type(WnckWindow *window)
+{
+ WnckWindowType type = wnck_window_get_window_type(window);
+
+ switch (type) {
+ case WNCK_WINDOW_NORMAL:
+ case WNCK_WINDOW_DIALOG:
+ return 0;
+
+ default:
+ break;
+ }
+
+ return 1;
+}
+
+static void title_window_changed(WnckScreen *screen,
+ G_GNUC_UNUSED WnckWindow *window_,
+ TitlePlugin *title)
+{
+ WnckWindow *window;
+ const gchar *name = NULL;
+
+ if (title->wnck_window_id != 0) {
+ g_signal_handler_disconnect(title->window, title->wnck_window_id);
+ title->wnck_window_id = 0;
+ title->window = NULL;
+ }
+
+ window = wnck_screen_get_active_window(screen);
+
+ if (window == NULL || title_check_type(window))
+ gtk_widget_hide(title->button);
+ else {
+ title->window = window;
+
+ name = wnck_window_get_name(window);
+
+ title->wnck_window_id = g_signal_connect(
+ window, "name-changed",
+ G_CALLBACK(title_window_name_changed), title
+ );
+
+ gtk_widget_show(title->button);
+ }
+
+ title_update_label(title, name);
+}
+
+static void title_screen_changed(XfcePanelPlugin *plugin,
+ GdkScreen *screen,
+ TitlePlugin *title)
+{
+ if (title->wnck_screen_id != 0) {
+ g_signal_handler_disconnect(title->screen, title->wnck_screen_id);
+ title->wnck_screen_id = 0;
+ }
+
+ if (title->wnck_window_id != 0) {
+ g_signal_handler_disconnect(title->window, title->wnck_window_id);
+ title->wnck_window_id = 0;
+ title->window = NULL;
+ }
+
+ screen = gtk_widget_get_screen(GTK_WIDGET(plugin));
+ if (screen != NULL) {
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+ title->screen = wnck_screen_get(gdk_screen_get_number(screen));
+ G_GNUC_END_IGNORE_DEPRECATIONS
+
+ title->wnck_screen_id = g_signal_connect(
+ title->screen, "active-window-changed",
+ G_CALLBACK(title_window_changed), title
+ );
+
+ title_window_changed(title->screen, NULL, title);
+ }
+}
+
+static void title_menu_destroy(GtkWidget *menu, TitlePlugin *title)
+{
+ gtk_widget_destroy(menu);
+ title->menu = NULL;
+}
+
+static void title_pressed(GtkButton *button, TitlePlugin *title)
+{
+ if (title->menu != NULL)
+ gtk_widget_destroy(title->menu);
+
+ if (title->window != NULL) {
+ GtkWidget *menu = wnck_action_menu_new(title->window);
+
+ title->menu = menu;
+
+ g_signal_connect(G_OBJECT(menu), "selection-done", G_CALLBACK(title_menu_destroy), title);
+ gtk_menu_attach_to_widget(GTK_MENU(menu), GTK_WIDGET(button), NULL);
+ gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
+ }
+}
+
+static TitlePlugin *title_new(XfcePanelPlugin *plugin)
+{
+ TitlePlugin *title;
+
+ title = g_slice_new0(TitlePlugin);
+
+ title->plugin = plugin;
+
+ title->button = xfce_create_panel_button();
+ gtk_widget_show(title->button);
+
+ title->label = gtk_label_new(NULL);
+ gtk_label_set_ellipsize(GTK_LABEL(title->label), PANGO_ELLIPSIZE_END);
+ gtk_widget_show(title->label);
+
+ gtk_container_add(GTK_CONTAINER(title->button), title->label);
+
+ return title;
+}
+
+static void title_construct(XfcePanelPlugin *plugin)
+{
+ TitlePlugin *title;
+
+ title = title_new(plugin);
+
+ gtk_container_add(GTK_CONTAINER(plugin), title->button);
+
+ xfce_panel_plugin_add_action_widget(plugin, title->button);
+ xfce_panel_plugin_set_expand(plugin, TRUE);
+
+ g_signal_connect(plugin, "free-data",
+ G_CALLBACK(title_free), title);
+
+ g_signal_connect(plugin, "size-changed",
+ G_CALLBACK(title_size_changed), NULL);
+
+ g_signal_connect(title->button, "pressed",
+ G_CALLBACK(title_pressed), title);
+
+ title_screen_changed(plugin, gtk_widget_get_screen(title->button),
+ title);
+
+ title->screen_id = g_signal_connect(
+ plugin, "screen-changed",
+ G_CALLBACK(title_screen_changed), title
+ );
+}
+