Skip to content

Commit

Permalink
Finished strip workspace numbers feature
Browse files Browse the repository at this point in the history
  • Loading branch information
denesb committed Feb 23, 2015
1 parent 921a746 commit 3cbef59
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
3 changes: 1 addition & 2 deletions panel-plugin/i3w-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/


#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include <libxfce4util/libxfce4util.h>
Expand Down Expand Up @@ -221,7 +220,7 @@ i3_workspaces_config_show(i3WorkspacesConfig *config, XfcePanelPlugin *plugin,
void
strip_workspace_numbers_changed(GtkWidget *button, i3WorkspacesConfig *config)
{
//config->strip_workspace_numbers = gtk_toogle_button_get_active(GTK_TOGGLE_BUTTON(button));
config->strip_workspace_numbers = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
}

void
Expand Down
73 changes: 54 additions & 19 deletions panel-plugin/i3w-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ static void
remove_workspaces(i3WorkspacesPlugin *i3_workspaces);

static void
set_button_label(GtkWidget *button, gchar *name, gboolean focused,
gboolean urgent, i3WorkspacesConfig *config);
set_button_label(GtkWidget *button, i3workspace *workspace,
i3WorkspacesConfig *config);

static gchar *
strip_workspace_numbers(const gchar *name, int num);

static void
on_workspace_clicked(GtkWidget *button, gpointer data);
Expand Down Expand Up @@ -125,7 +128,6 @@ construct_workspaces(XfcePanelPlugin *plugin)
{
i3WorkspacesPlugin *i3_workspaces;
GtkOrientation orientation;
GtkWidget *label;

/* allocate memory for the plugin structure */
i3_workspaces = panel_slice_new0(i3WorkspacesPlugin);
Expand Down Expand Up @@ -317,8 +319,7 @@ add_workspaces(i3WorkspacesPlugin *i3_workspaces)
button = xfce_panel_create_button();
gtk_button_set_label(GTK_BUTTON(button), workspace->name);

set_button_label(button, workspace->name, workspace->focused,
workspace->urgent, i3_workspaces->config);
set_button_label(button, workspace, i3_workspaces->config);

g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(on_workspace_clicked), i3_workspaces);
Expand Down Expand Up @@ -399,8 +400,7 @@ on_workspace_blurred(i3workspace *workspace, gpointer data)
i3WorkspacesPlugin *i3_workspaces = (i3WorkspacesPlugin *) data;

GtkWidget *button = i3_workspaces->buttons[workspace->num];
set_button_label(button, workspace->name, workspace->focused,
workspace->urgent, i3_workspaces->config);
set_button_label(button, workspace, i3_workspaces->config);
}

/**
Expand All @@ -417,8 +417,7 @@ on_workspace_focused(i3workspace *workspace, gpointer data)

workspace->urgent = FALSE; // reset the urgent flag
GtkWidget *button = i3_workspaces->buttons[workspace->num];
set_button_label(button, workspace->name, workspace->focused,
workspace->urgent, i3_workspaces->config);
set_button_label(button, workspace, i3_workspaces->config);
}

/**
Expand All @@ -434,8 +433,7 @@ on_workspace_urgent(i3workspace *workspace, gpointer data)
i3WorkspacesPlugin *i3_workspaces = (i3WorkspacesPlugin *) data;

GtkWidget *button = i3_workspaces->buttons[workspace->num];
set_button_label(button, workspace->name, workspace->focused,
workspace->urgent, i3_workspaces->config);
set_button_label(button, workspace, i3_workspaces->config);
}

/**
Expand All @@ -448,21 +446,25 @@ on_workspace_urgent(i3workspace *workspace, gpointer data)
* Generate the label for the workspace button.
*/
static void
set_button_label(GtkWidget *button, gchar *name, gboolean focused, gboolean urgent,
set_button_label(GtkWidget *button, i3workspace *workspace,
i3WorkspacesConfig *config)
{
static gchar * template = "<span foreground=\"#%06X\" weight=\"%s\">%s</span>";
static gchar * focused_weight = "bold";
static gchar * blurred_weight = "normal";

gchar *name = config->strip_workspace_numbers ?
strip_workspace_numbers(workspace->name, workspace->num) :
workspace->name;

// allocate space for the maximum possible size of the label
gulong maxlen = strlen(name) + 51;
gchar *label_str = (gchar *) calloc(maxlen, sizeof(gchar));

g_snprintf(label_str, maxlen, template,
urgent ? config->urgent_color :
focused ? config->focused_color : config->normal_color,
focused ? focused_weight : blurred_weight,
workspace->urgent ? config->urgent_color :
(workspace->focused ? config->focused_color : config->normal_color),
workspace->focused ? focused_weight : blurred_weight,
name);

GtkWidget * label = gtk_bin_get_child(GTK_BIN(button));
Expand All @@ -471,6 +473,38 @@ set_button_label(GtkWidget *button, gchar *name, gboolean focused, gboolean urge
free(label_str);
}

/**
* strip_workspace_numbers:
* @workspaceName - the name of the workspace
* @workspaceNum - the number of the workspace
*
* Strips the workspace name of the workspace number.
* Returns: an i3workspace*
*/
static gchar *
strip_workspace_numbers(const gchar *name, int num)
{
size_t offset = 0;
offset += (num < 10) ? 1 : 2;
if (name[offset] == ':') offset++;

int len = strlen(name);
gchar *strippedName = NULL;

if (offset < len)
{
int strippedLen = len - offset + 1;
strippedName = (gchar*) malloc(strippedLen);
strippedName = memcpy(strippedName, name + offset, strippedLen);
}
else
{
strippedName = strdup(name);
}

return strippedName;
}

/**
* on_workspace_clicked:
* @button: the clicked button
Expand All @@ -482,20 +516,21 @@ static void
on_workspace_clicked(GtkWidget *button, gpointer data)
{
i3WorkspacesPlugin *i3_workspaces = (i3WorkspacesPlugin *)data;
i3workspace **workspaces = i3wm_get_workspaces(i3_workspaces->i3wm);
i3workspace *workspace = NULL;

gint button_index = 0;
gint i;
for (i = 1; i < I3_WORKSPACE_N; i++)
{
if (i3_workspaces->buttons[i] == button)
{
button_index = i;
workspace = workspaces[i];
break;
}
}

GError *err = NULL;
i3wm_goto_workspace(i3_workspaces->i3wm, button_index, &err);
i3wm_goto_workspace(i3_workspaces->i3wm, workspace, &err);
if (err != NULL)
{
fprintf(stderr, "%s", err->message);
Expand All @@ -505,13 +540,13 @@ on_workspace_clicked(GtkWidget *button, gpointer data)
static void
on_ipc_shutdown(gpointer i3_w)
{
g_printf("on_ipc_shutdown\n");
i3WorkspacesPlugin *i3_workspaces = (i3WorkspacesPlugin *) i3_w;

remove_workspaces(i3_workspaces);
i3wm_destruct(i3_workspaces->i3wm);
i3_workspaces->i3wm = NULL;

//FIXME fix this
//TODO: error_state_on();
//recover_from_disconnect(i3_workspaces);
//TODO: error_state_off();
Expand Down
12 changes: 6 additions & 6 deletions panel-plugin/i3wm-delegate.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ i3windowManager * i3wm_construct(GError **err)
void
i3wm_destruct(i3windowManager *i3wm)
{
g_printf("i3wm_destruct\n");
g_object_unref(i3wm->connection);

int i;
Expand Down Expand Up @@ -236,17 +235,17 @@ i3wm_set_ipch_shutdown_callback(i3windowManager *i3wm,
/**
* i3wm_goto_workspace:
* @i3wm: the window manager delegate struct
* @workspace_num: the index of the workspace to jump to
* @workspace: the workspace to jump to
*
* Instruct the window manager to jump to the specified workspace.
*/
void
i3wm_goto_workspace(i3windowManager *i3wm, gint workspace_num, GError **err)
i3wm_goto_workspace(i3windowManager *i3wm, i3workspace *workspace, GError **err)
{
size_t cmd_size = 13 * sizeof(gchar);
size_t cmd_size = (13 + strlen(workspace->name)) * sizeof(gchar);
gchar *command_str = (gchar *) malloc(cmd_size);
memset(command_str, 0, cmd_size);
sprintf(command_str, "workspace %i", workspace_num);
sprintf(command_str, "workspace %s", workspace->name);

GError *ipc_err = NULL;

Expand All @@ -273,6 +272,7 @@ i3wm_goto_workspace(i3windowManager *i3wm, gint workspace_num, GError **err)
/**
* create_workspace:
* @workspaceReply: the i3ipcWorkspaceReply struct
* @strip: strip workspace numbers ?
*
* Create a i3workspace struct from the i3ipcWorkspaceReply struct
*
Expand Down Expand Up @@ -325,7 +325,7 @@ init_workspaces(i3windowManager *i3wm, GError **err)
GSList *listItem;
for (listItem = workspacesList; listItem != NULL; listItem = listItem->next)
{
i3workspace *workspace = create_workspace((i3ipcWorkspaceReply *) listItem->data);
i3workspace *workspace = create_workspace((i3ipcWorkspaceReply *)listItem->data);
i3wm->workspaces[workspace->num] = workspace;
}

Expand Down
2 changes: 1 addition & 1 deletion panel-plugin/i3wm-delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ void
i3wm_set_ipch_shutdown_callback(i3windowManager *i3wm, i3wmIpcShutdownCallback callback, gpointer data);

void
i3wm_goto_workspace(i3windowManager *i3wm, gint workspace_num, GError **err);
i3wm_goto_workspace(i3windowManager *i3wm, i3workspace *workspace, GError **err);

#endif /* !__I3W_DELEGATE_H__ */

0 comments on commit 3cbef59

Please sign in to comment.