home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / yagirc-0.51.tar.gz / yagirc-0.51.tar / yagirc-0.51 / gui_setup_alias.c < prev    next >
C/C++ Source or Header  |  1998-05-09  |  7KB  |  243 lines

  1. /*
  2.  
  3.  gui_setup_alias.c : Aliases notebook entry for setup
  4.  
  5.     Copyright (C) 1998 Timo Sirainen
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. #include <gtk/gtk.h>
  26. #include <glib.h>
  27.  
  28. #include "os.h"
  29. #include "irc.h"
  30. #include "gui.h"
  31.  
  32. static GtkWidget *alias_list, *alias_text, *command_text;
  33. static int current_alias;
  34.  
  35. static int compare_aliases(ALIAS_REC *p1, ALIAS_REC *p2)
  36. {
  37.     int n;
  38.  
  39.     if (p1 == NULL) return -1;
  40.     if (p2 == NULL) return 1;
  41.  
  42.     n = strcasecmp(p1->alias, p2->alias);
  43.     return n;
  44. }
  45.  
  46. /* add new server to server list */
  47. ALIAS_REC *add_alias_list(char *alias, char *cmd)
  48. {
  49.     ALIAS_REC *al;
  50.  
  51.     g_return_val_if_fail(alias != NULL, NULL);
  52.     g_return_val_if_fail(cmd != NULL, NULL);
  53.  
  54.     al = (ALIAS_REC *) g_malloc(sizeof(ALIAS_REC));
  55.     al->alias = g_strdup(strupr(alias));
  56.     al->cmd = g_strdup(strupr(cmd));
  57.  
  58.     aliases = g_list_insert_sorted(aliases, al, (GCompareFunc) compare_aliases);
  59.     return al;
  60. }
  61.  
  62. /* remove server from server list */
  63. void remove_alias_list(ALIAS_REC *rec)
  64. {
  65.     g_return_if_fail(rec != NULL);
  66.  
  67.     aliases = g_list_remove(aliases, rec);
  68.  
  69.     g_free(rec->alias);
  70.     g_free(rec->cmd);
  71.     g_free(rec);
  72. }
  73.  
  74. /* return alias record number in list */
  75. static ALIAS_REC *alias_number(int num)
  76. {
  77.     GList *tmp;
  78.  
  79.     tmp = g_list_first(aliases);
  80.     while (num > 0 && tmp != NULL)
  81.     {
  82.         num--;
  83.         tmp = tmp->next;
  84.     }
  85.  
  86.     return tmp == NULL ? NULL : (ALIAS_REC *) tmp->data;
  87. }
  88.  
  89. void alias_box(char *alias, char *cmd, GtkSignalFunc func)
  90. {
  91.     GtkWidget *win;
  92.     GtkWidget *button;
  93.  
  94.     win = gtk_dialog_new();
  95.     gtk_grab_add (GTK_WIDGET (win));
  96.  
  97.     alias_text = gui_add_label(GTK_WIDGET(GTK_DIALOG(win)->vbox), "Alias:", alias);
  98.     command_text = gui_add_label(GTK_WIDGET(GTK_DIALOG(win)->vbox), "Command:", cmd);
  99.  
  100.     button = gtk_button_new_with_label ("Ok");
  101.     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(win)->action_area), button, TRUE, FALSE, 0);
  102.     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  103.                                func, GTK_OBJECT(win));
  104.     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  105.     gtk_widget_grab_default (button);
  106.     gtk_widget_show (button);
  107.  
  108.     button = gtk_button_new_with_label ("Cancel");
  109.     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(win)->action_area), button, TRUE, FALSE, 0);
  110.     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  111.                                GTK_SIGNAL_FUNC (gtk_widget_destroy),
  112.                                GTK_OBJECT(win));
  113.     gtk_widget_show (button);
  114.  
  115.     gtk_widget_show(win);
  116. }
  117.  
  118. void sig_add_alias(GtkWidget *widget, GtkWidget *dialog)
  119. {
  120.     char *alias, *cmd;
  121.     char *args[2];
  122.     int pos;
  123.     GList *list;
  124.  
  125.     gui_get_label(alias, alias_text);
  126.     gui_get_label(cmd, command_text);
  127.  
  128.     args[0] = alias; args[1] = cmd;
  129.  
  130.     list = g_list_first(aliases);
  131.     for (pos = 0; list != NULL; pos++, list = list->next)
  132.         if (irc_nicks_compare(((ALIAS_REC *) list->data)->alias, alias) > 0) break;
  133.  
  134.     add_alias_list(alias, cmd);
  135.  
  136.     gtk_clist_insert(GTK_CLIST(alias_list), pos, args);
  137.  
  138.     g_free(alias);
  139.     g_free(cmd);
  140.  
  141.     gtk_widget_destroy(dialog);
  142. }
  143.  
  144. void add_alias(GtkWidget *widget)
  145. {
  146.     alias_box("", "", GTK_SIGNAL_FUNC(sig_add_alias));
  147. }
  148.  
  149. void sig_edit_alias(GtkWidget *widget, GtkWidget *dialog)
  150. {
  151.     remove_alias_list(alias_number(current_alias));
  152.     gtk_clist_remove(GTK_CLIST(alias_list), current_alias);
  153.     current_alias = GTK_CLIST(alias_list)->selection == NULL ? -1 :
  154.         (int) GTK_CLIST(alias_list)->selection->data;
  155.  
  156.     sig_add_alias(widget, dialog);
  157. }
  158.  
  159. void edit_alias(GtkWidget *widget, GtkCList *clist)
  160. {
  161.     ALIAS_REC *al;
  162.  
  163.     if (current_alias == -1) return;
  164.  
  165.     al = alias_number(current_alias);
  166.     alias_box(al->alias, al->cmd, GTK_SIGNAL_FUNC(sig_edit_alias));
  167. }
  168.  
  169. void delete_alias(GtkWidget *widget, GtkCList *clist)
  170. {
  171.     if (current_alias == -1) return;
  172.  
  173.     remove_alias_list(alias_number(current_alias));
  174.     gtk_clist_remove(clist, current_alias);
  175.  
  176.     current_alias = clist->selection == NULL ? -1 :
  177.         (int) clist->selection->data;
  178. }
  179.  
  180. void sig_alias_changed(GtkWidget *widget, gint row, gint column,
  181.                        GdkEventButton *bevent)
  182. {
  183.     current_alias = row;
  184. }
  185.  
  186. /* draw alias list section */
  187. void gui_setup_aliases(GtkWidget *vbox)
  188. {
  189.     GtkWidget *hbox;
  190.     GtkWidget *button;
  191.     GList *tmp;
  192.     char *titles[] = { "alias", "command" };
  193.  
  194.     current_alias = -1;
  195.  
  196.     hbox = gtk_hbox_new(FALSE, 0);
  197.     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
  198.     gtk_widget_show(hbox);
  199.  
  200.     /* create alias list widget here for buttons.. */
  201.     alias_list = gtk_clist_new_with_titles(2, titles);
  202.  
  203.     /* add/edit/delete buttons */
  204.     button = gtk_button_new_with_label("Add");
  205.     gtk_signal_connect (GTK_OBJECT (button), "clicked",
  206.                         GTK_SIGNAL_FUNC(add_alias), alias_list);
  207.     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 10);
  208.     gtk_widget_show(button);
  209.     button = gtk_button_new_with_label("Edit");
  210.     gtk_signal_connect (GTK_OBJECT (button), "clicked",
  211.                         GTK_SIGNAL_FUNC(edit_alias), alias_list);
  212.     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 10);
  213.     gtk_widget_show(button);
  214.     button = gtk_button_new_with_label("Delete");
  215.     gtk_signal_connect (GTK_OBJECT (button), "clicked",
  216.                         GTK_SIGNAL_FUNC(delete_alias), alias_list);
  217.     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 10);
  218.     gtk_widget_show(button);
  219.  
  220.     /* alias list */
  221.     gtk_clist_set_column_width (GTK_CLIST (alias_list), 0, 100);
  222.     gtk_clist_set_policy(GTK_CLIST(alias_list), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  223.     //gtk_list_set_selection_mode (GTK_LIST(alias_list), GTK_SELECTION_MULTIPLE);
  224.     gtk_clist_set_selection_mode (GTK_CLIST(alias_list), GTK_SELECTION_BROWSE);
  225.     gtk_signal_connect (GTK_OBJECT (alias_list), "select_row",
  226.                         GTK_SIGNAL_FUNC(sig_alias_changed), NULL);
  227.     gtk_box_pack_start(GTK_BOX(vbox), alias_list, TRUE, TRUE, 10);
  228.     gtk_widget_show(alias_list);
  229.  
  230.     gtk_clist_freeze(GTK_CLIST(alias_list));
  231.     for (tmp = g_list_first(aliases); tmp != NULL; tmp = tmp->next)
  232.     {
  233.         ALIAS_REC *al;
  234.         char *args[2];
  235.  
  236.         al = (ALIAS_REC *) tmp->data;
  237.         args[0] = al->alias; args[1] = al->cmd;
  238.         gtk_clist_append(GTK_CLIST(alias_list), args);
  239.     }
  240.     gtk_clist_thaw(GTK_CLIST(alias_list));
  241. }
  242.  
  243.