/* abstract channel view: tabs or tree or anything you like */
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include "chanview.h"
#include "gtkutil.h"
/* treeStore columns */
#define COL_NAME 0 /* (char *) */
#define COL_CHAN 1 /* (chan *) */
#define COL_ATTR 2 /* (PangoAttrList *) */
#define COL_PIXBUF 3 /* (GdkPixbuf *) */
struct _chanview
{
/* impl scratch area */
char implscratch[sizeof (void *) * 8];
GtkTreeStore *store;
int size; /* number of channels in view */
GtkWidget *box; /* the box we destroy when changing implementations */
GtkStyle *style; /* style used for tree */
chan *focused; /* currently focused channel */
int trunc_len;
/* callbacks */
void (*cb_focus) (chanview *, chan *, int tag, void *userdata);
void (*cb_xbutton) (chanview *, chan *, int tag, void *userdata);
gboolean (*cb_contextmenu) (chanview *, chan *, int tag, void *userdata, GdkEventButton *);
int (*cb_compare) (void *a, void *b);
/* impl */
void (*func_init) (chanview *);
void (*func_postinit) (chanview *);
void *(*func_add) (chanview *, chan *, char *, GtkTreeIter *);
void (*func_move_focus) (chanview *, gboolean, int);
void (*func_change_orientation) (chanview *);
void (*func_remove) (chan *);
void (*func_move) (chan *, int delta);
void (*func_move_family) (chan *, int delta);
void (*func_focus) (chan *);
void (*func_set_color) (chan *, PangoAttrList *);
void (*func_rename) (chan *, char *);
gboolean (*func_is_collapsed) (chan *);
chan *(*func_get_parent) (chan *);
void (*func_cleanup) (chanview *);
unsigned int sorted:1;
unsigned int vertical:1;
unsigned int use_icons:1;
};
struct _chan
{
chanview *cv; /* our owner */
GtkTreeIter iter;
void *userdata; /* session * */
void *family; /* server * or null */
void *impl; /* togglebutton or null */
GdkPixbuf *icon;
short allow_closure; /* allow it to be closed when it still has children? */
short tag;
};
static chan *cv_find_chan_by_number (chanview *cv, int num);
static int cv_find_number_of_chan (chanview *cv, chan *find_ch);
/* ======= TABS ======= */
#include "chanview-tabs.c"
/* ======= TREE ======= */
#include "chanview-tree.c"
/* ==== ABSTRACT CHANVIEW ==== */
static char *
truncate_tab_name (char *name, int max)
{
char *buf;
if (max > 2 && g_utf8_strlen (name, -1) > max)
{
/* truncate long channel names */
buf = malloc (strlen (name) + 4);
strcpy (buf, name);
g_utf8_offset_to_pointer (buf, max)[0] = 0;
strcat (buf, "..");
return buf;
}
return name;
}
/* iterate through a model, into 1 depth of children */
static void
model_foreach_1 (GtkTreeModel *model, void (*func)(void *, GtkTreeIter *),
void *userdata)
{
GtkTreeIter iter, inner;
if (gtk_tree_model_get_iter_first (model, &iter))
{
do
{
func (userdata, &iter);
if (gtk_tree_model_iter_children (model, &inner, &iter))
{
do
func (userdata, &inner);
while (gtk_tree_model_iter_next (model, &inner));
}
}
while (gtk_tree_model_iter_next (model, &iter));
}
}
static void
chanview_pop_cb (chanview *cv, GtkTreeIter *iter)
{
chan *ch;
char *name;
PangoAttrList *attr;
gtk_tree_model_get (GTK_TREE_MODEL (cv->store), iter,
COL_NAME, &name, COL_CHAN, &ch, COL_ATTR, &attr, -1);
ch->impl = cv->func_add (cv, ch, name, NULL);
if (attr)
{
cv->func_set_color (ch, attr);
pango_attr_list_unref (attr);
}
g_free (name);
}
static void
chanview_populate (chanview *cv)
{
model_foreach_1 (GTK_TREE_MODEL (cv->store), (void *)chanview_pop_cb, cv);
}
void
chanview_set_impl (chanview *cv, int type)
{
/* cleanup the old one */
if (cv->func_cleanup)
cv->func_cleanup (cv);
switch (type)
{
case 0:
cv->func_init = cv_tabs_init;
cv->func_postinit = cv_tabs_postinit;
cv->func_add = cv_tabs_add;
cv->func_move_focus = cv_tabs_move_focus;
cv->func_change_orientation = cv_tabs_change_orientation;
cv->func_remove = cv_tabs_remove;
cv->func_move = cv_tabs_move;
cv->func_move_family = cv_tabs_move_family;
cv->func_focus = cv_tabs_focus;
cv->func_set_color = cv_tabs_set_color;
cv->func_rename = cv_tabs_rename;
cv->func_is_collapsed = cv_tabs_is_collapsed;
cv->func_get_parent = cv_tabs_get_parent;
cv->func_cleanup = cv_tabs_cleanup;
break;
default:
cv->func_init = cv_tree_init;
cv->func_postinit = cv_tree_postinit;
cv->func_add = cv_tree_add;
cv->func_move_focus = cv_tree_move_focus;
cv->func_change_orientation = cv_tree_change_orientation;
GtkTreeIter iter;
struct notify_per_server *servnot;
view = g_object_get_data (G_OBJECT (notify_window), "view");
if (gtkutil_treeview_get_selected (view, &iter, NPS_COLUMN, &servnot, -1))
{
if (servnot)
open_query (servnot->server, servnot->notify->name, TRUE);
}
}
static void
notify_remove_clicked (GtkWidget * igad)
{
GtkTreeView *view;
GtkTreeModel *model;
GtkTreeIter iter;
GtkTreePath *path = NULL;
gboolean found = FALSE;
char *name;
view = g_object_get_data (G_OBJECT (notify_window), "view");
if (gtkutil_treeview_get_selected (view, &iter, USER_COLUMN, &name, -1))
{
model = gtk_tree_view_get_model (view);
found = (*name != 0);
while (!found) /* the real nick is some previous node */
{
g_free (name); /* it's useless to us */
if (!path)
path = gtk_tree_model_get_path (model, &iter);
if (!gtk_tree_path_prev (path)) /* arrgh! no previous node! */
{
g_warning ("notify list state is invalid\n");
break;
}
if (!gtk_tree_model_get_iter (model, &iter, path))
break;
gtk_tree_model_get (model, &iter, USER_COLUMN, &name, -1);
found = (*name != 0);
}
if (path)
gtk_tree_path_free (path);
if (!found)
return;
/* ok, now we can remove it */
gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
notify_deluser (name);
g_free (name);
}
}
static void
notifygui_add_cb (GtkDialog *dialog, gint response, gpointer entry)
{
char *networks;
char *text;
text = GTK_ENTRY (entry)->text;
if (text[0] && response == GTK_RESPONSE_ACCEPT)
{
networks = GTK_ENTRY (g_object_get_data (G_OBJECT (entry), "net"))->text;
if (g_ascii_strcasecmp (networks, "ALL") == 0 || networks[0] == 0)
notify_adduser (text, NULL);
else
notify_adduser (text, networks);
}
gtk_widget_destroy (GTK_WIDGET (dialog));
}
static void
notifygui_add_enter (GtkWidget *entry, GtkWidget *dialog)
{
gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
}
void
fe_notify_ask (char *nick, char *networks)
{
GtkWidget *dialog;
GtkWidget *entry;
GtkWidget *label;
GtkWidget *wid;
GtkWidget *table;
char *msg = _("Enter nickname to add:");
char buf[256];
dialog = gtk_dialog_new_with_buttons (msg, NULL, 0,
GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
NULL);
if (parent_window)
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
table = gtk_table_new (2, 3, FALSE);
gtk_container_set_border_width (GTK_CONTAINER (table), 12);
gtk_table_set_row_spacings (GTK_TABLE (table), 3);
gtk_table_set_col_spacings (GTK_TABLE (table), 8);
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), table);
label = gtk_label_new (msg);
gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
entry = gtk_entry_new ();
gtk_entry_set_text (GTK_ENTRY (entry), nick);
g_signal_connect (G_OBJECT (entry), "activate",
G_CALLBACK (notifygui_add_enter), dialog);
gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 0, 1);
g_signal_connect (G_OBJECT (dialog), "response",
G_CALLBACK (notifygui_add_cb), entry);
label = gtk_label_new (_("Notify on these networks:"));
gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
wid = gtk_entry_new ();
g_object_set_data (G_OBJECT (entry), "net", wid);
g_signal_connect (G_OBJECT (wid), "activate",
G_CALLBACK (notifygui_add_enter), dialog);
gtk_entry_set_text (GTK_ENTRY (wid), networks ? networks : "ALL");
gtk_table_attach_defaults (GTK_TABLE (table), wid, 1, 2, 2, 3);
label = gtk_label_new (NULL);
snprintf (buf, sizeof (buf), "<i><span size=\"smaller\">%s</span></i>", _("Comma separated list of networks is accepted."));
gtk_label_set_markup (GTK_LABEL (label), buf);
gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 3, 4);
gtk_widget_show_all (dialog);
}
static void
notify_add_clicked (GtkWidget * igad)
{
fe_notify_ask ("", NULL);
}
void
notify_opengui (void)
{
GtkWidget *vbox, *bbox;
GtkWidget *view;
if (notify_window)
{
mg_bring_tofront (notify_window);
return;
}
notify_window =
mg_create_generic_tab ("Notify", _(DISPLAY_NAME": Friends List"), FALSE, TRUE,
notify_closegui, NULL, 400, 250, &vbox, 0);
view = notify_treeview_new (vbox);
g_object_set_data (G_OBJECT (notify_window), "view", view);
bbox = gtk_hbutton_box_new ();
gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_SPREAD);
gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
gtk_box_pack_end (GTK_BOX (vbox), bbox, 0, 0, 0);
gtk_widget_show (bbox);
gtkutil_button (bbox, GTK_STOCK_NEW, 0, notify_add_clicked, 0,
_("Add..."));
notify_button_remove =
gtkutil_button (bbox, GTK_STOCK_DELETE, 0, notify_remove_clicked, 0,
_("Remove"));
notify_button_opendialog =
gtkutil_button (bbox, NULL, 0, notify_opendialog_clicked, 0,
_("Open Dialog"));
gtk_widget_set_sensitive (notify_button_opendialog, FALSE);
gtk_widget_set_sensitive (notify_button_remove, FALSE);
notify_gui_update ();
gtk_widget_show (notify_window);
}
an class="n">new_name;
chan *ret;
new_name = truncate_tab_name (name, cv->trunc_len);
ret = chanview_add_real (cv, new_name, family, userdata, allow_closure, tag, icon, NULL, NULL);
if (new_name != name)
free (new_name);
return ret;
}
int
chanview_get_size (chanview *cv)
{
return cv->size;
}
GtkWidget *
chanview_get_box (chanview *cv)
{
return cv->box;
}
void
chanview_move_focus (chanview *cv, gboolean relative, int num)
{
cv->func_move_focus (cv, relative, num);
}
GtkOrientation
chanview_get_orientation (chanview *cv)
{
return (cv->vertical ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
}
void
chanview_set_orientation (chanview *cv, gboolean vertical)
{
if (vertical != cv->vertical)
{
cv->vertical = vertical;
cv->func_change_orientation (cv);
}
}
int
chan_get_tag (chan *ch)
{
return ch->tag;
}
void *
chan_get_userdata (chan *ch)
{
return ch->userdata;
}
void
chan_focus (chan *ch)
{
if (ch->cv->focused == ch)
return;
ch->cv->func_focus (ch);
}
void
chan_move (chan *ch, int delta)
{
ch->cv->func_move (ch, delta);
}
void
chan_move_family (chan *ch, int delta)
{
ch->cv->func_move_family (ch, delta);
}
void
chan_set_color (chan *ch, PangoAttrList *list)
{
gtk_tree_store_set (ch->cv->store, &ch->iter, COL_ATTR, list, -1);
ch->cv->func_set_color (ch, list);
}
void
chan_rename (chan *ch, char *name, int trunc_len)
{
char *new_name;
new_name = truncate_tab_name (name, trunc_len);
gtk_tree_store_set (ch->cv->store, &ch->iter, COL_NAME, new_name, -1);
ch->cv->func_rename (ch, new_name);
ch->cv->trunc_len = trunc_len;
if (new_name != name)
free (new_name);
}
/* this thing is overly complicated */
static int
cv_find_number_of_chan (chanview *cv, chan *find_ch)
{
GtkTreeIter iter, inner;
chan *ch;
int i = 0;
if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cv->store), &iter))
{
do
{
gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &iter, COL_CHAN, &ch, -1);
if (ch == find_ch)
return i;
i++;
if (gtk_tree_model_iter_children (GTK_TREE_MODEL (cv->store), &inner, &iter))
{
do
{
gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &inner, COL_CHAN, &ch, -1);
if (ch == find_ch)
return i;
i++;
}
while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &inner));
}
}
while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &iter));
}
return 0; /* WARNING */
}
/* this thing is overly complicated too */
static chan *
cv_find_chan_by_number (chanview *cv, int num)
{
GtkTreeIter iter, inner;
chan *ch;
int i = 0;
if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cv->store), &iter))
{
do
{
if (i == num)
{
gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &iter, COL_CHAN, &ch, -1);
return ch;
}
i++;
if (gtk_tree_model_iter_children (GTK_TREE_MODEL (cv->store), &inner, &iter))
{
do
{
if (i == num)
{
gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &inner, COL_CHAN, &ch, -1);
return ch;
}
i++;
}
while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &inner));
}
}
while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &iter));
}
return NULL;
}
static void
chan_emancipate_children (chan *ch)
{
char *name;
chan *childch;
GtkTreeIter childiter;
PangoAttrList *attr;
while (gtk_tree_model_iter_children (GTK_TREE_MODEL (ch->cv->store), &childiter, &ch->iter))
{
/* remove and re-add all the children, but avoid using "ch" as parent */
gtk_tree_model_get (GTK_TREE_MODEL (ch->cv->store), &childiter,
COL_NAME, &name, COL_CHAN, &childch, COL_ATTR, &attr, -1);
ch->cv->func_remove (childch);
gtk_tree_store_remove (ch->cv->store, &childiter);
ch->cv->size--;
chanview_add_real (childch->cv, name, childch->family, childch->userdata, childch->allow_closure, childch->tag, childch->icon, childch, ch);
if (attr)
{
childch->cv->func_set_color (childch, attr);
pango_attr_list_unref (attr);
}
g_free (name);
}
}
gboolean
chan_remove (chan *ch, gboolean force)
{
chan *new_ch;
int i, num;
extern int xchat_is_quitting;
if (xchat_is_quitting) /* avoid lots of looping on exit */
return TRUE;
/* is this ch allowed to be closed while still having children? */
if (!force &&
gtk_tree_model_iter_has_child (GTK_TREE_MODEL (ch->cv->store), &ch->iter) &&
!ch->allow_closure)
return FALSE;
chan_emancipate_children (ch);
ch->cv->func_remove (ch);
/* is it the focused one? */
if (ch->cv->focused == ch)
{
ch->cv->focused = NULL;
/* try to move the focus to some other valid channel */
num = cv_find_number_of_chan (ch->cv, ch);
/* move to the one left of the closing tab */
new_ch = cv_find_chan_by_number (ch->cv, num - 1);
if (new_ch && new_ch != ch)
{
chan_focus (new_ch); /* this'll will set ch->cv->focused for us too */
} else
{
/* if it fails, try focus from tab 0 and up */
for (i = 0; i < ch->cv->size; i++)
{
new_ch = cv_find_chan_by_number (ch->cv, i);
if (new_ch && new_ch != ch)
{
chan_focus (new_ch); /* this'll will set ch->cv->focused for us too */
break;
}
}
}
}
ch->cv->size--;
gtk_tree_store_remove (ch->cv->store, &ch->iter);
free (ch);
return TRUE;
}
gboolean
chan_is_collapsed (chan *ch)
{
return ch->cv->func_is_collapsed (ch);
}
chan *
chan_get_parent (chan *ch)
{
return ch->cv->func_get_parent (ch);
}