Use macros instead of raw numbers for login type
[rofl0r-ixchat.git] / src / fe-gtk / editlist.c
blob5af67e32b2ef0a3aad7977b30bb21a2a7fd7f737
1 /* X-Chat
2 * Copyright (C) 1998 Peter Zelezny.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include "fe-gtk.h"
28 #include <gtk/gtkstock.h>
29 #include <gtk/gtkentry.h>
30 #include <gtk/gtkclist.h>
31 #include <gtk/gtkhbox.h>
32 #include <gtk/gtkvseparator.h>
34 #include "../common/xchat.h"
35 #include "../common/cfgfiles.h"
36 #include "../common/xchatc.h"
37 #include "../common/fe.h"
38 #include "menu.h"
39 #include "gtkutil.h"
40 #include "maingui.h"
41 #include "editlist.h"
44 static GtkWidget *editlist_gui_entry_name;
45 static GtkWidget *editlist_gui_entry_cmd;
46 static GtkWidget *editlist_gui_window;
47 static GtkWidget *editlist_gui_list;
48 static GSList *editlist_list;
49 static char *editlist_file;
50 static char *editlist_help;
54 static void
55 editlist_gui_load (GtkWidget * listgad)
57 gchar *nnew[2];
58 GSList *list = editlist_list;
59 struct popup *pop;
61 while (list)
63 pop = (struct popup *) list->data;
64 nnew[0] = pop->name;
65 nnew[1] = pop->cmd;
66 gtk_clist_append (GTK_CLIST (listgad), nnew);
67 list = list->next;
71 static void
72 editlist_gui_row_unselected (GtkWidget * clist, gint row, gint column,
73 GdkEventButton * even, gpointer none)
75 gtk_entry_set_text (GTK_ENTRY (editlist_gui_entry_name), "");
76 gtk_entry_set_text (GTK_ENTRY (editlist_gui_entry_cmd), "");
79 static void
80 editlist_gui_row_selected (GtkWidget * clist, gint row, gint column,
81 GdkEventButton * even, gpointer none)
83 char *name, *cmd;
85 row = gtkutil_clist_selection (editlist_gui_list);
86 if (row != -1)
88 gtk_clist_get_text (GTK_CLIST (clist), row, 0, &name);
89 gtk_clist_get_text (GTK_CLIST (clist), row, 1, &cmd);
91 name = strdup (name);
92 cmd = strdup (cmd);
94 gtk_entry_set_text (GTK_ENTRY (editlist_gui_entry_name), name);
95 gtk_entry_set_text (GTK_ENTRY (editlist_gui_entry_cmd), cmd);
97 free (name);
98 free (cmd);
99 } else
101 editlist_gui_row_unselected (0, 0, 0, 0, 0);
105 static void
106 editlist_gui_handle_cmd (GtkWidget * igad)
108 int row;
109 const char *reply;
111 row = gtkutil_clist_selection (editlist_gui_list);
112 if (row != -1)
114 reply = gtk_entry_get_text (GTK_ENTRY (igad));
115 gtk_clist_set_text (GTK_CLIST (editlist_gui_list), row, 1, reply);
119 static void
120 editlist_gui_handle_name (GtkWidget * igad)
122 int row;
123 const char *ctcp;
125 row = gtkutil_clist_selection (editlist_gui_list);
126 if (row != -1)
128 ctcp = gtk_entry_get_text (GTK_ENTRY (igad));
129 gtk_clist_set_text (GTK_CLIST (editlist_gui_list), row, 0, ctcp);
133 static void
134 editlist_gui_addnew (GtkWidget * igad)
136 int i;
137 gchar *nnew[2];
139 nnew[0] = _("*NEW*");
140 nnew[1] = _("EDIT ME");
142 i = gtk_clist_append (GTK_CLIST (editlist_gui_list), nnew);
143 gtk_clist_select_row (GTK_CLIST (editlist_gui_list), i, 0);
144 gtk_clist_moveto (GTK_CLIST (editlist_gui_list), i, 0, 0.5, 0);
147 static void
148 editlist_gui_delete (GtkWidget * igad)
150 int row;
152 row = gtkutil_clist_selection (editlist_gui_list);
153 if (row != -1)
155 gtk_clist_unselect_all (GTK_CLIST (editlist_gui_list));
156 gtk_clist_remove (GTK_CLIST (editlist_gui_list), row);
160 static void
161 editlist_gui_save (GtkWidget * igad)
163 int fh, i = 0;
164 char buf[512];
165 char *a, *b;
167 fh = xchat_open_file (editlist_file, O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE);
168 if (fh != -1)
170 while (1)
172 if (!gtk_clist_get_text (GTK_CLIST (editlist_gui_list), i, 0, &a))
173 break;
174 gtk_clist_get_text (GTK_CLIST (editlist_gui_list), i, 1, &b);
175 snprintf (buf, sizeof (buf), "NAME %s\nCMD %s\n\n", a, b);
176 write (fh, buf, strlen (buf));
177 i++;
179 close (fh);
180 gtk_widget_destroy (editlist_gui_window);
181 if (editlist_list == replace_list)
183 list_free (&replace_list);
184 list_loadconf (editlist_file, &replace_list, 0);
185 } else if (editlist_list == popup_list)
187 list_free (&popup_list);
188 list_loadconf (editlist_file, &popup_list, 0);
189 } else if (editlist_list == button_list)
191 GSList *list = sess_list;
192 struct session *sess;
193 list_free (&button_list);
194 list_loadconf (editlist_file, &button_list, 0);
195 while (list)
197 sess = (struct session *) list->data;
198 fe_buttons_update (sess);
199 list = list->next;
201 } else if (editlist_list == dlgbutton_list)
203 GSList *list = sess_list;
204 struct session *sess;
205 list_free (&dlgbutton_list);
206 list_loadconf (editlist_file, &dlgbutton_list, 0);
207 while (list)
209 sess = (struct session *) list->data;
210 fe_dlgbuttons_update (sess);
211 list = list->next;
213 } else if (editlist_list == ctcp_list)
215 list_free (&ctcp_list);
216 list_loadconf (editlist_file, &ctcp_list, 0);
217 } else if (editlist_list == command_list)
219 list_free (&command_list);
220 list_loadconf (editlist_file, &command_list, 0);
221 } else if (editlist_list == usermenu_list)
223 list_free (&usermenu_list);
224 list_loadconf (editlist_file, &usermenu_list, 0);
225 usermenu_update ();
226 } else
228 list_free (&urlhandler_list);
229 list_loadconf (editlist_file, &urlhandler_list, 0);
234 static void
235 editlist_gui_help (GtkWidget * igad)
237 /* if (editlist_help)*/
238 fe_message (editlist_help, FE_MSG_INFO);
241 static void
242 editlist_gui_sort (GtkWidget * igad)
244 int row;
246 row = gtkutil_clist_selection (editlist_gui_list);
247 if (row != -1)
248 gtk_clist_unselect_row (GTK_CLIST (editlist_gui_list), row, 0);
249 gtk_clist_sort (GTK_CLIST (editlist_gui_list));
252 static void
253 editlist_gui_movedown (GtkWidget * igad)
255 int row;
256 char *temp;
258 row = gtkutil_clist_selection (editlist_gui_list);
259 if (row != -1)
261 if (!gtk_clist_get_text (GTK_CLIST (editlist_gui_list), row + 1, 0, &temp))
262 return;
263 gtk_clist_freeze (GTK_CLIST (editlist_gui_list));
264 gtk_clist_swap_rows (GTK_CLIST (editlist_gui_list), row, row + 1);
265 gtk_clist_thaw (GTK_CLIST (editlist_gui_list));
266 row++;
267 if (!gtk_clist_row_is_visible (GTK_CLIST (editlist_gui_list), row) !=
268 GTK_VISIBILITY_FULL)
269 gtk_clist_moveto (GTK_CLIST (editlist_gui_list), row, 0, 0.9, 0);
273 static void
274 editlist_gui_moveup (GtkWidget * igad)
276 int row;
278 row = gtkutil_clist_selection (editlist_gui_list);
279 if (row != -1 && row > 0)
281 gtk_clist_freeze (GTK_CLIST (editlist_gui_list));
282 gtk_clist_swap_rows (GTK_CLIST (editlist_gui_list), row - 1, row);
283 gtk_clist_thaw (GTK_CLIST (editlist_gui_list));
284 row--;
285 if (gtk_clist_row_is_visible (GTK_CLIST (editlist_gui_list), row) !=
286 GTK_VISIBILITY_FULL)
287 gtk_clist_moveto (GTK_CLIST (editlist_gui_list), row, 0, 0.1, 0);
291 static void
292 editlist_gui_close (void)
294 editlist_gui_window = 0;
297 void
298 editlist_gui_open (char *title1, char *title2, GSList * list, char *title, char *wmclass,
299 char *file, char *help)
301 gchar *titles[2];
302 GtkWidget *vbox, *hbox, *button;
304 if (title1)
306 titles[0] = title1;
307 titles[1] = title2;
308 } else
310 titles[0] = _("Name");
311 titles[1] = _("Command");
314 if (editlist_gui_window)
316 mg_bring_tofront (editlist_gui_window);
317 return;
320 editlist_list = list;
321 editlist_file = file;
322 editlist_help = help;
324 editlist_gui_window =
325 mg_create_generic_tab (wmclass, title, TRUE, FALSE,
326 editlist_gui_close, NULL, 450, 250, &vbox, 0);
328 editlist_gui_list = gtkutil_clist_new (2, titles, vbox, GTK_POLICY_ALWAYS,
329 editlist_gui_row_selected, 0,
330 editlist_gui_row_unselected, 0,
331 GTK_SELECTION_BROWSE);
332 gtk_clist_set_column_width (GTK_CLIST (editlist_gui_list), 0, 90);
334 hbox = gtk_hbox_new (0, 2);
335 gtk_box_pack_end (GTK_BOX (vbox), hbox, 0, 0, 0);
336 gtk_widget_show (hbox);
338 button = gtkutil_button (hbox, GTK_STOCK_GO_UP, 0, editlist_gui_moveup,
339 0, _("Move Up"));
340 gtk_widget_set_usize (button, 100, 0);
342 button = gtkutil_button (hbox, GTK_STOCK_GO_DOWN, 0, editlist_gui_movedown,
343 0, _("Move Dn"));
344 gtk_widget_set_usize (button, 100, 0);
346 button = gtk_vseparator_new ();
347 gtk_container_add (GTK_CONTAINER (hbox), button);
348 gtk_widget_show (button);
350 button = gtkutil_button (hbox, GTK_STOCK_CANCEL, 0, gtkutil_destroy,
351 editlist_gui_window, _("Cancel"));
352 gtk_widget_set_usize (button, 100, 0);
354 button = gtkutil_button (hbox, GTK_STOCK_SAVE, 0, editlist_gui_save,
355 0, _("Save"));
356 gtk_widget_set_usize (button, 100, 0);
358 hbox = gtk_hbox_new (0, 2);
359 gtk_box_pack_end (GTK_BOX (vbox), hbox, 0, 0, 0);
360 gtk_widget_show (hbox);
362 button = gtkutil_button (hbox, GTK_STOCK_ADD, 0, editlist_gui_addnew,
363 0, _("Add New"));
364 gtk_widget_set_usize (button, 100, 0);
366 button = gtkutil_button (hbox, GTK_STOCK_REMOVE, 0, editlist_gui_delete,
367 0, _("Delete"));
368 gtk_widget_set_usize (button, 100, 0);
370 button = gtk_vseparator_new ();
371 gtk_container_add (GTK_CONTAINER (hbox), button);
372 gtk_widget_show (button);
374 button = gtkutil_button (hbox, GTK_STOCK_SORT_ASCENDING, 0, editlist_gui_sort,
375 0, _("Sort"));
376 gtk_widget_set_usize (button, 100, 0);
378 button = gtkutil_button (hbox, GTK_STOCK_HELP, 0, editlist_gui_help,
379 0, _("Help"));
380 gtk_widget_set_usize (button, 100, 0);
382 if (!help)
383 gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE);
385 hbox = gtk_hbox_new (0, 2);
386 gtk_box_pack_end (GTK_BOX (vbox), hbox, 0, 0, 0);
387 gtk_widget_show (hbox);
389 editlist_gui_entry_name = gtk_entry_new_with_max_length (82);
390 gtk_widget_set_usize (editlist_gui_entry_name, 96, 0);
391 gtk_signal_connect (GTK_OBJECT (editlist_gui_entry_name), "changed",
392 GTK_SIGNAL_FUNC (editlist_gui_handle_name), 0);
393 gtk_box_pack_start (GTK_BOX (hbox), editlist_gui_entry_name, 0, 0, 0);
394 gtk_widget_show (editlist_gui_entry_name);
396 editlist_gui_entry_cmd = gtk_entry_new_with_max_length (255);
397 gtk_signal_connect (GTK_OBJECT (editlist_gui_entry_cmd), "changed",
398 GTK_SIGNAL_FUNC (editlist_gui_handle_cmd), 0);
399 gtk_container_add (GTK_CONTAINER (hbox), editlist_gui_entry_cmd);
400 gtk_widget_show (editlist_gui_entry_cmd);
402 hbox = gtk_hbox_new (0, 2);
403 gtk_box_pack_end (GTK_BOX (vbox), hbox, 0, 0, 0);
404 gtk_widget_show (hbox);
406 editlist_gui_load (editlist_gui_list);
408 gtk_widget_show (editlist_gui_window);