Skip to content

Commit

Permalink
Implement config dialog, fixed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamYuan committed Feb 1, 2021
1 parent 4432885 commit 4ceb8ef
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 19 deletions.
79 changes: 79 additions & 0 deletions wintitle-dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,85 @@
#include "wintitle-dialogs.h"
#include "wintitle.h"

#define BORDER 16
#define GRID_PADDING 8

void bind_spinner_value_to(GtkSpinButton *spinner, GObject *object, const gchar *property) {
guint value = 0;

g_object_get(object, property, &value, NULL);
gtk_spin_button_set_value(spinner, value);

GObject *adjustment;
g_object_get(G_OBJECT(spinner), "adjustment", &adjustment, NULL);
g_object_bind_property(G_OBJECT(adjustment), "value", G_OBJECT(object), property,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
g_object_unref(adjustment);
}

void bind_toggle_value_to(GtkToggleButton *toggle, GObject *object, const gchar *property) {
gboolean value;
g_object_get(object, property, &value, NULL);
gtk_toggle_button_set_active(toggle, value);

g_object_bind_property(G_OBJECT(toggle), "active", G_OBJECT(object), property,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
}

void bind_toggle_button_value_to(GtkToggleButton *toggle, GObject *object, const gchar *property) {
gboolean value;
g_object_get(object, property, &value, NULL);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), value); // Seemed to be nessessary.
g_object_bind_property(G_OBJECT(toggle), "active", G_OBJECT(object), property,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
}

static void wintitle_plugin_configure_response(GtkWidget *dialog, gint response, XfcePanelPlugin *panel_plugin) {
g_object_set_data(G_OBJECT(panel_plugin), "dialog", NULL);
xfce_panel_plugin_unblock_menu(panel_plugin);
gtk_widget_destroy(dialog);
}

void wintitle_plugin_configure(XfcePanelPlugin *panel_plugin) {
WintitlePlugin *plugin = XFCE_WINTITLE_PLUGIN(panel_plugin);

xfce_panel_plugin_block_menu(panel_plugin);

GtkWidget *dialog = xfce_titled_dialog_new_with_buttons(
_("Window Title"), GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(panel_plugin))),
GTK_DIALOG_DESTROY_WITH_PARENT, "gtk-close", GTK_RESPONSE_OK, NULL);

gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
gtk_window_set_icon_name(GTK_WINDOW(dialog), "xfce4-settings");

g_object_set_data(G_OBJECT(panel_plugin), "dialog", dialog);
g_signal_connect(G_OBJECT(dialog), "response", G_CALLBACK(wintitle_plugin_configure_response), plugin);
gtk_container_set_border_width(GTK_CONTAINER(dialog), BORDER);

GtkWidget *container = gtk_dialog_get_content_area(GTK_DIALOG(dialog));

GtkWidget *widget = NULL;
// title-max-chars
widget = gtk_spin_button_new_with_range(TITLE_MAX_CHARS_MIN, TITLE_MAX_CHARS_MAX, 1.0);
bind_spinner_value_to(GTK_SPIN_BUTTON(widget), G_OBJECT(plugin), "title-max-chars");
gtk_container_add(GTK_CONTAINER(container), gtk_label_new("Window title max width (chars)"));
gtk_container_add(GTK_CONTAINER(container), widget);

// spacing
widget = gtk_spin_button_new_with_range(SPACING_MIN, SPACING_MAX, 1.0);
bind_spinner_value_to(GTK_SPIN_BUTTON(widget), G_OBJECT(plugin), "spacing");
gtk_container_add(GTK_CONTAINER(container), gtk_label_new("Spacing between icon and title (pixels)"));
gtk_container_add(GTK_CONTAINER(container), widget);

// use-mini-icon
widget = gtk_check_button_new_with_label("Use mini icon");
bind_toggle_value_to(GTK_TOGGLE_BUTTON(widget), G_OBJECT(plugin), "use-mini-icon");
gtk_container_add(GTK_CONTAINER(container), widget);

gtk_widget_show_all(dialog);
}

void wintitle_plugin_about(XfcePanelPlugin *plugin) {
const gchar *auth[] = {"Yitian Yuan (AdamYuan) <[email protected]>", NULL};
return gtk_show_about_dialog(NULL, //
Expand Down
96 changes: 84 additions & 12 deletions wintitle.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "wintitle.h"
#include "wintitle-dialogs.h"
#include "wintitle.h"

enum {
PROP_0,
PROP_TITLE_MAX_CHARS,
PROP_SPACING,
PROP_USE_MINI_ICON,
};

struct _WintitlePluginClass {
XfcePanelPluginClass __parent__;
Expand All @@ -28,7 +35,7 @@ struct _WintitlePlugin {
// Configuration
guint title_max_chars;
guint spacing;
gboolean mini_icon;
gboolean use_mini_icon;

GtkWidget *box;
GtkWidget *icon;
Expand All @@ -44,13 +51,23 @@ XFCE_PANEL_DEFINE_PLUGIN(WintitlePlugin, wintitle_plugin);
// STATE UPDATES //
///////////////////
static void wintitle_plugin_update_window_title(WintitlePlugin *plugin) {
if (!plugin->window || !WNCK_IS_WINDOW(plugin->window)) {
gtk_label_set_text(GTK_LABEL(plugin->label), "");
return;
}
gtk_label_set_text(GTK_LABEL(plugin->label), wnck_window_get_name(plugin->window));
}

static void wintitle_plugin_update_window_icon(WintitlePlugin *plugin) {
if (!plugin->window || !WNCK_IS_WINDOW(plugin->window)) {
gtk_image_set_from_pixbuf(GTK_IMAGE(plugin->icon), NULL);
gtk_box_set_spacing(GTK_BOX(plugin->box), 0);
return;
}
GdkPixbuf *pixbuf = NULL;
if (!wnck_window_get_icon_is_fallback(plugin->window)) {
pixbuf = plugin->mini_icon ? wnck_window_get_mini_icon(plugin->window) : wnck_window_get_icon(plugin->window);
pixbuf =
plugin->use_mini_icon ? wnck_window_get_mini_icon(plugin->window) : wnck_window_get_icon(plugin->window);
}
gtk_image_set_from_pixbuf(GTK_IMAGE(plugin->icon), pixbuf);
gtk_box_set_spacing(GTK_BOX(plugin->box), pixbuf ? plugin->spacing : 0);
Expand Down Expand Up @@ -84,18 +101,23 @@ static void wintitle_plugin_window_icon_changed(WnckWindow *window, WintitlePlug

static void wintitle_plugin_active_window_changed(WnckScreen *screen, WnckWindow *previous_window,
WintitlePlugin *plugin) {
if (plugin->window) {
if (plugin->window && WNCK_IS_WINDOW(plugin->window)) {
g_signal_handlers_disconnect_by_func(G_OBJECT(plugin->window), G_CALLBACK(wintitle_plugin_window_name_changed),
plugin);
g_signal_handlers_disconnect_by_func(G_OBJECT(plugin->window), G_CALLBACK(wintitle_plugin_window_icon_changed),
plugin);
}

plugin->window = wnck_screen_get_active_window(screen);
wintitle_plugin_update_window_title(plugin);
wintitle_plugin_update_window_icon(plugin);

g_signal_connect(G_OBJECT(plugin->window), "name-changed", G_CALLBACK(wintitle_plugin_window_name_changed), plugin);
g_signal_connect(G_OBJECT(plugin->window), "icon-changed", G_CALLBACK(wintitle_plugin_window_icon_changed), plugin);
if (plugin->window && WNCK_IS_WINDOW(plugin->window)) {
g_signal_connect(G_OBJECT(plugin->window), "name-changed", G_CALLBACK(wintitle_plugin_window_name_changed),
plugin);
g_signal_connect(G_OBJECT(plugin->window), "icon-changed", G_CALLBACK(wintitle_plugin_window_icon_changed),
plugin);
}
}

////////////////////////
Expand All @@ -114,19 +136,58 @@ static void wintitle_plugin_construct(XfcePanelPlugin *panel_plugin) {
xfce_panel_plugin_menu_show_about(panel_plugin);

const PanelProperty properties[] = {
{"title-max-chars", G_TYPE_UINT}, {"spacing", G_TYPE_UINT}, {"mini-icon", G_TYPE_BOOLEAN}, {NULL}};
panel_properties_bind(NULL, G_OBJECT(plugin), xfce_panel_plugin_get_property_base(panel_plugin), properties, FALSE);
{"title-max-chars", G_TYPE_UINT}, {"spacing", G_TYPE_UINT}, {"use-mini-icon", G_TYPE_BOOLEAN}, NULL};
panel_properties_bind(NULL, G_OBJECT(plugin), xfce_panel_plugin_get_property_base(panel_plugin), properties, TRUE);
}

static void wintitle_plugin_free_data(XfcePanelPlugin *panel_plugin) {
WintitlePlugin *plugin = XFCE_WINTITLE_PLUGIN(panel_plugin);
g_signal_handlers_disconnect_by_func(G_OBJECT(plugin->screen), G_CALLBACK(wintitle_plugin_active_window_changed),
plugin);
}

static void wintitle_plugin_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
WintitlePlugin *plugin = XFCE_WINTITLE_PLUGIN(object);
switch (prop_id) {
case PROP_TITLE_MAX_CHARS:
g_value_set_uint(value, plugin->title_max_chars);
break;
case PROP_SPACING:
g_value_set_uint(value, plugin->spacing);
break;
case PROP_USE_MINI_ICON:
g_value_set_boolean(value, plugin->use_mini_icon);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}

static void wintitle_plugin_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) {
WintitlePlugin *plugin = XFCE_WINTITLE_PLUGIN(object);
switch (prop_id) {
case PROP_TITLE_MAX_CHARS:
plugin->title_max_chars = g_value_get_uint(value);
gtk_label_set_max_width_chars(GTK_LABEL(plugin->label), plugin->title_max_chars);
break;
case PROP_SPACING:
plugin->spacing = g_value_get_uint(value);
gtk_box_set_spacing(GTK_BOX(plugin->box), gtk_image_get_pixbuf(GTK_IMAGE(plugin->icon)) ? plugin->spacing : 0);
break;
case PROP_USE_MINI_ICON:
plugin->use_mini_icon = g_value_get_boolean(value);
wintitle_plugin_update_window_icon(plugin);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}

static void wintitle_plugin_init(WintitlePlugin *plugin) {
// default properties
plugin->title_max_chars = DEFAULT_TITLE_MAX_CHARS;
plugin->spacing = DEFAULT_SPACING;
plugin->mini_icon = DEFAULT_MINI_ICON;
plugin->use_mini_icon = DEFAULT_USE_MINI_ICON;

plugin->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, plugin->spacing);
plugin->icon = gtk_image_new();
Expand Down Expand Up @@ -159,10 +220,21 @@ static void wintitle_plugin_class_init(WintitlePluginClass *class) {
plugin_class->construct = wintitle_plugin_construct;
plugin_class->free_data = wintitle_plugin_free_data;
plugin_class->orientation_changed = wintitle_plugin_orientation_changed;
plugin_class->configure_plugin = NULL; // wintitle_plugin_configure;
plugin_class->configure_plugin = wintitle_plugin_configure;
plugin_class->about = wintitle_plugin_about;

GObjectClass *gobject_class = G_OBJECT_CLASS(class);
gobject_class->get_property = NULL; // wintitle_plugin_get_property;
gobject_class->set_property = NULL; // wintitle_plugin_set_property;
gobject_class->get_property = wintitle_plugin_get_property;
gobject_class->set_property = wintitle_plugin_set_property;
g_object_class_install_property(gobject_class, PROP_TITLE_MAX_CHARS,
g_param_spec_uint("title-max-chars", NULL, NULL, //
TITLE_MAX_CHARS_MIN, TITLE_MAX_CHARS_MAX, DEFAULT_TITLE_MAX_CHARS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property(gobject_class, PROP_SPACING,
g_param_spec_uint("spacing", NULL, NULL, //
SPACING_MIN, SPACING_MAX, DEFAULT_SPACING,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property(gobject_class, PROP_USE_MINI_ICON,
g_param_spec_boolean("use-mini-icon", NULL, NULL, DEFAULT_USE_MINI_ICON, //
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}
5 changes: 2 additions & 3 deletions wintitle.desktop
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[Xfce Panel]
Type=X-XFCE-PanelPlugin
Encoding=UTF-8
Name=WindowTitle
Comment=Window Title Plugin
Icon=xfce4-display
Name=Window Title
Comment=Shows activate window title and icon.
X-XFCE-Internal=true
X-XFCE-Module=wintitle
X-XFCE-API=2.0
21 changes: 17 additions & 4 deletions wintitle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
#define WINTITLE_H

#define DEFAULT_TITLE_MAX_CHARS 80
#define TITLE_MAX_CHARS_MAX 200
#define TITLE_MAX_CHARS_MIN 20

#define DEFAULT_SPACING 5
#define DEFAULT_MINI_ICON FALSE
#define SPACING_MAX 20
#define SPACING_MIN 0

#define DEFAULT_USE_MINI_ICON FALSE

#include <common/panel-private.h>
#include <common/panel-utils.h>
#include <common/panel-xfconf.h>
Expand All @@ -30,7 +37,10 @@
#include <libxfce4panel/xfce-panel-plugin.h>
#include <libxfce4util/libxfce4util.h>

#include "wintitle-dialogs.h"
G_BEGIN_DECLS

typedef struct _WintitlePluginClass WintitlePluginClass;
typedef struct _WintitlePlugin WintitlePlugin;

#define XFCE_TYPE_WINTITLE_PLUGIN (wintitle_plugin_get_type())
#define XFCE_WINTITLE_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), XFCE_TYPE_WINTITLE_PLUGIN, WintitlePlugin))
Expand All @@ -41,6 +51,9 @@
#define XFCE_WINTITLE_PLUGIN_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), XFCE_TYPE_WINTITLE_PLUGIN), WintitlePluginClass)

typedef struct _WintitlePluginClass WintitlePluginClass;
typedef struct _WintitlePlugin WintitlePlugin;
GType wintitle_plugin_get_type(void) G_GNUC_CONST;
void wintitle_plugin_register_type(XfcePanelTypeModule *typeModule);

G_END_DECLS

#endif

0 comments on commit 4ceb8ef

Please sign in to comment.