restructure configure so pkg-config derived SSL flags get used
[rofl0r-ixchat.git] / src / fe-gtk / gtkutil.c
blobb6ba38a51d0fe188ebdfb2643e6e33d7ebcb33f5
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
18 #define _FILE_OFFSET_BITS 64 /* allow selection of large files */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include "fe-gtk.h"
29 #include <gtk/gtkbutton.h>
30 #include <gtk/gtkclist.h>
31 #include <gtk/gtkscrolledwindow.h>
32 #include <gtk/gtkmessagedialog.h>
33 #include <gtk/gtkwindow.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtkimage.h>
36 #include <gtk/gtktooltips.h>
37 #include <gtk/gtklabel.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkstock.h>
40 #include <gtk/gtkspinbutton.h>
41 #include <gtk/gtkclipboard.h>
42 #include <gtk/gtktreeview.h>
43 #include <gtk/gtktreeselection.h>
44 #include <gtk/gtkcellrenderertext.h>
45 #include <gtk/gtkcellrenderertoggle.h>
46 #include <gtk/gtkversion.h>
47 #include <gtk/gtkfilechooserdialog.h>
49 #include "../common/xchat.h"
50 #include "../common/fe.h"
51 #include "../common/util.h"
52 #include "gtkutil.h"
53 #include "pixmaps.h"
55 /* gtkutil.c, just some gtk wrappers */
57 extern void path_part (char *file, char *path, int pathlen);
60 struct file_req
62 GtkWidget *dialog;
63 void *userdata;
64 filereqcallback callback;
65 int flags; /* FRF_* flags */
68 static char last_dir[256] = "";
71 static void
72 gtkutil_file_req_destroy (GtkWidget * wid, struct file_req *freq)
74 freq->callback (freq->userdata, NULL);
75 free (freq);
78 static void
79 gtkutil_check_file (char *file, struct file_req *freq)
81 struct stat st;
82 int axs = FALSE;
84 path_part (file, last_dir, sizeof (last_dir));
86 /* check if the file is readable or writable */
87 if (freq->flags & FRF_WRITE)
89 if (access (last_dir, W_OK) == 0)
90 axs = TRUE;
91 } else
93 if (stat (file, &st) != -1)
95 if (!S_ISDIR (st.st_mode) || (freq->flags & FRF_CHOOSEFOLDER))
96 axs = TRUE;
100 if (axs)
102 char *utf8_file;
103 /* convert to UTF8. It might be converted back to locale by
104 server.c's g_convert */
105 utf8_file = xchat_filename_to_utf8 (file, -1, NULL, NULL, NULL);
106 if (utf8_file)
108 freq->callback (freq->userdata, utf8_file);
109 g_free (utf8_file);
110 } else
112 fe_message ("Filename encoding is corrupt.", FE_MSG_ERROR);
114 } else
116 if (freq->flags & FRF_WRITE)
117 fe_message (_("Cannot write to that file."), FE_MSG_ERROR);
118 else
119 fe_message (_("Cannot read that file."), FE_MSG_ERROR);
123 static void
124 gtkutil_file_req_done (GtkWidget * wid, struct file_req *freq)
126 GSList *files, *cur;
127 GtkFileChooser *fs = GTK_FILE_CHOOSER (freq->dialog);
129 if (freq->flags & FRF_MULTIPLE)
131 files = cur = gtk_file_chooser_get_filenames (fs);
132 while (cur)
134 gtkutil_check_file (cur->data, freq);
135 g_free (cur->data);
136 cur = cur->next;
138 if (files)
139 g_slist_free (files);
140 } else
142 if (freq->flags & FRF_CHOOSEFOLDER)
143 gtkutil_check_file (gtk_file_chooser_get_current_folder (fs), freq);
144 else
145 gtkutil_check_file (gtk_file_chooser_get_filename (fs), freq);
148 /* this should call the "destroy" cb, where we free(freq) */
149 gtk_widget_destroy (freq->dialog);
152 static void
153 gtkutil_file_req_response (GtkWidget *dialog, gint res, struct file_req *freq)
155 switch (res)
157 case GTK_RESPONSE_ACCEPT:
158 gtkutil_file_req_done (dialog, freq);
159 break;
161 case GTK_RESPONSE_CANCEL:
162 /* this should call the "destroy" cb, where we free(freq) */
163 gtk_widget_destroy (freq->dialog);
167 void
168 gtkutil_file_req (const char *title, void *callback, void *userdata, char *filter,
169 int flags)
171 struct file_req *freq;
172 GtkWidget *dialog;
173 extern char *get_xdir_fs (void);
175 if (flags & FRF_WRITE)
177 dialog = gtk_file_chooser_dialog_new (title, NULL,
178 GTK_FILE_CHOOSER_ACTION_SAVE,
179 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
180 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
181 NULL);
182 if (filter && filter[0]) /* filter becomes initial name when saving */
184 char temp[1024];
185 path_part (filter, temp, sizeof (temp));
186 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), temp);
187 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), file_part (filter));
190 if (!(flags & FRF_NOASKOVERWRITE))
191 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
193 else
194 dialog = gtk_file_chooser_dialog_new (title, NULL,
195 GTK_FILE_CHOOSER_ACTION_OPEN,
196 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
197 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
198 NULL);
199 if (flags & FRF_MULTIPLE)
200 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);
201 if (last_dir[0])
202 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), last_dir);
203 if (flags & FRF_ADDFOLDER)
204 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog),
205 get_xdir_fs (), NULL);
206 if (flags & FRF_CHOOSEFOLDER)
208 gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
209 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), filter);
211 else
213 if (filter && (flags & FRF_FILTERISINITIAL))
214 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), filter);
217 freq = malloc (sizeof (struct file_req));
218 freq->dialog = dialog;
219 freq->flags = flags;
220 freq->callback = callback;
221 freq->userdata = userdata;
223 g_signal_connect (G_OBJECT (dialog), "response",
224 G_CALLBACK (gtkutil_file_req_response), freq);
225 g_signal_connect (G_OBJECT (dialog), "destroy",
226 G_CALLBACK (gtkutil_file_req_destroy), (gpointer) freq);
227 gtk_widget_show (dialog);
230 void
231 gtkutil_destroy (GtkWidget * igad, GtkWidget * dgad)
233 gtk_widget_destroy (dgad);
236 static void
237 gtkutil_get_str_response (GtkDialog *dialog, gint arg1, gpointer entry)
239 void (*callback) (int cancel, char *text, void *user_data);
240 char *text;
241 void *user_data;
243 text = (char *) gtk_entry_get_text (GTK_ENTRY (entry));
244 callback = g_object_get_data (G_OBJECT (dialog), "cb");
245 user_data = g_object_get_data (G_OBJECT (dialog), "ud");
247 switch (arg1)
249 case GTK_RESPONSE_REJECT:
250 callback (TRUE, text, user_data);
251 gtk_widget_destroy (GTK_WIDGET (dialog));
252 break;
253 case GTK_RESPONSE_ACCEPT:
254 callback (FALSE, text, user_data);
255 gtk_widget_destroy (GTK_WIDGET (dialog));
256 break;
260 static void
261 gtkutil_str_enter (GtkWidget *entry, GtkWidget *dialog)
263 gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
266 void
267 fe_get_str (char *msg, char *def, void *callback, void *userdata)
269 GtkWidget *dialog;
270 GtkWidget *entry;
271 GtkWidget *hbox;
272 GtkWidget *label;
274 dialog = gtk_dialog_new_with_buttons (msg, NULL, 0,
275 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
276 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
277 NULL);
278 gtk_box_set_homogeneous (GTK_BOX (GTK_DIALOG (dialog)->vbox), TRUE);
279 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
280 hbox = gtk_hbox_new (TRUE, 0);
282 g_object_set_data (G_OBJECT (dialog), "cb", callback);
283 g_object_set_data (G_OBJECT (dialog), "ud", userdata);
285 entry = gtk_entry_new ();
286 g_signal_connect (G_OBJECT (entry), "activate",
287 G_CALLBACK (gtkutil_str_enter), dialog);
288 gtk_entry_set_text (GTK_ENTRY (entry), def);
289 gtk_box_pack_end (GTK_BOX (hbox), entry, 0, 0, 0);
291 label = gtk_label_new (msg);
292 gtk_box_pack_end (GTK_BOX (hbox), label, 0, 0, 0);
294 g_signal_connect (G_OBJECT (dialog), "response",
295 G_CALLBACK (gtkutil_get_str_response), entry);
297 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), hbox);
299 gtk_widget_show_all (dialog);
302 static void
303 gtkutil_get_number_response (GtkDialog *dialog, gint arg1, gpointer spin)
305 void (*callback) (int cancel, int value, void *user_data);
306 int num;
307 void *user_data;
309 num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spin));
310 callback = g_object_get_data (G_OBJECT (dialog), "cb");
311 user_data = g_object_get_data (G_OBJECT (dialog), "ud");
313 switch (arg1)
315 case GTK_RESPONSE_REJECT:
316 callback (TRUE, num, user_data);
317 gtk_widget_destroy (GTK_WIDGET (dialog));
318 break;
319 case GTK_RESPONSE_ACCEPT:
320 callback (FALSE, num, user_data);
321 gtk_widget_destroy (GTK_WIDGET (dialog));
322 break;
326 void
327 fe_get_int (char *msg, int def, void *callback, void *userdata)
329 GtkWidget *dialog;
330 GtkWidget *spin;
331 GtkWidget *hbox;
332 GtkWidget *label;
333 GtkAdjustment *adj;
335 dialog = gtk_dialog_new_with_buttons (msg, NULL, 0,
336 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
337 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
338 NULL);
339 gtk_box_set_homogeneous (GTK_BOX (GTK_DIALOG (dialog)->vbox), TRUE);
340 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
341 hbox = gtk_hbox_new (TRUE, 0);
343 g_object_set_data (G_OBJECT (dialog), "cb", callback);
344 g_object_set_data (G_OBJECT (dialog), "ud", userdata);
346 spin = gtk_spin_button_new (NULL, 1, 0);
347 adj = gtk_spin_button_get_adjustment ((GtkSpinButton*)spin);
348 adj->lower = 0;
349 adj->upper = 1024;
350 adj->step_increment = 1;
351 gtk_adjustment_changed (adj);
352 gtk_spin_button_set_value ((GtkSpinButton*)spin, def);
353 gtk_box_pack_end (GTK_BOX (hbox), spin, 0, 0, 0);
355 label = gtk_label_new (msg);
356 gtk_box_pack_end (GTK_BOX (hbox), label, 0, 0, 0);
358 g_signal_connect (G_OBJECT (dialog), "response",
359 G_CALLBACK (gtkutil_get_number_response), spin);
361 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), hbox);
363 gtk_widget_show_all (dialog);
366 GtkWidget *
367 gtkutil_button (GtkWidget *box, char *stock, char *tip, void *callback,
368 void *userdata, char *labeltext)
370 GtkWidget *wid, *img, *bbox;
372 wid = gtk_button_new ();
374 if (labeltext)
376 gtk_button_set_label (GTK_BUTTON (wid), labeltext);
377 gtk_button_set_image (GTK_BUTTON (wid), gtk_image_new_from_stock (stock, GTK_ICON_SIZE_MENU));
378 gtk_button_set_use_underline (GTK_BUTTON (wid), TRUE);
379 if (box)
380 gtk_container_add (GTK_CONTAINER (box), wid);
382 else
384 bbox = gtk_hbox_new (0, 0);
385 gtk_container_add (GTK_CONTAINER (wid), bbox);
386 gtk_widget_show (bbox);
388 img = gtk_image_new_from_stock (stock, GTK_ICON_SIZE_MENU);
389 if (stock == GTK_STOCK_GOTO_LAST)
390 gtk_widget_set_usize (img, 10, 6);
391 gtk_container_add (GTK_CONTAINER (bbox), img);
392 gtk_widget_show (img);
393 gtk_box_pack_start (GTK_BOX (box), wid, 0, 0, 0);
396 g_signal_connect (G_OBJECT (wid), "clicked",
397 G_CALLBACK (callback), userdata);
398 gtk_widget_show (wid);
399 if (tip)
400 add_tip (wid, tip);
402 return wid;
405 void
406 gtkutil_label_new (char *text, GtkWidget * box)
408 GtkWidget *label = gtk_label_new (text);
409 gtk_container_add (GTK_CONTAINER (box), label);
410 gtk_widget_show (label);
413 GtkWidget *
414 gtkutil_entry_new (int max, GtkWidget * box, void *callback,
415 gpointer userdata)
417 GtkWidget *entry = gtk_entry_new_with_max_length (max);
418 gtk_container_add (GTK_CONTAINER (box), entry);
419 if (callback)
420 g_signal_connect (G_OBJECT (entry), "changed",
421 G_CALLBACK (callback), userdata);
422 gtk_widget_show (entry);
423 return entry;
426 GtkWidget *
427 gtkutil_clist_new (int columns, char *titles[],
428 GtkWidget * box, int policy,
429 void *select_callback, gpointer select_userdata,
430 void *unselect_callback,
431 gpointer unselect_userdata, int selection_mode)
433 GtkWidget *clist, *win;
435 win = gtk_scrolled_window_new (0, 0);
436 gtk_container_add (GTK_CONTAINER (box), win);
437 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (win),
438 GTK_POLICY_AUTOMATIC, policy);
439 gtk_widget_show (win);
441 if (titles)
442 clist = gtk_clist_new_with_titles (columns, titles);
443 else
444 clist = gtk_clist_new (columns);
446 gtk_clist_set_selection_mode (GTK_CLIST (clist), selection_mode);
447 gtk_clist_column_titles_passive (GTK_CLIST (clist));
448 gtk_container_add (GTK_CONTAINER (win), clist);
449 if (select_callback)
451 g_signal_connect (G_OBJECT (clist), "select_row",
452 G_CALLBACK (select_callback), select_userdata);
454 if (unselect_callback)
456 g_signal_connect (G_OBJECT (clist), "unselect_row",
457 G_CALLBACK (unselect_callback), unselect_userdata);
459 gtk_widget_show (clist);
461 return clist;
465 gtkutil_clist_selection (GtkWidget * clist)
467 if (GTK_CLIST (clist)->selection)
468 return GPOINTER_TO_INT(GTK_CLIST (clist)->selection->data);
469 return -1;
472 static int
473 int_compare (const int * elem1, const int * elem2)
475 return (*elem1) - (*elem2);
479 gtkutil_clist_multiple_selection (GtkWidget * clist, int ** rows, const int max_rows)
481 int i = 0;
482 GList *tmp_clist;
483 *rows = malloc (sizeof (int) * max_rows );
484 memset( *rows, -1, max_rows * sizeof(int) );
486 for( tmp_clist = GTK_CLIST(clist)->selection;
487 tmp_clist && i < max_rows; tmp_clist = tmp_clist->next, i++)
489 (*rows)[i] = GPOINTER_TO_INT( tmp_clist->data );
491 qsort(*rows, i, sizeof(int), (void *)int_compare);
492 return i;
496 void
497 add_tip (GtkWidget * wid, char *text)
499 static GtkTooltips *tip = NULL;
500 if (!tip)
501 tip = gtk_tooltips_new ();
502 gtk_tooltips_set_tip (tip, wid, text, 0);
505 void
506 show_and_unfocus (GtkWidget * wid)
508 GTK_WIDGET_UNSET_FLAGS (wid, GTK_CAN_FOCUS);
509 gtk_widget_show (wid);
512 void
513 gtkutil_set_icon (GtkWidget *win)
515 gtk_window_set_icon (GTK_WINDOW (win), pix_xchat);
518 extern GtkWidget *parent_window; /* maingui.c */
520 GtkWidget *
521 gtkutil_window_new (char *title, char *role, int width, int height, int flags)
523 GtkWidget *win;
525 win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
526 gtkutil_set_icon (win);
527 gtk_window_set_title (GTK_WINDOW (win), title);
528 gtk_window_set_default_size (GTK_WINDOW (win), width, height);
529 gtk_window_set_role (GTK_WINDOW (win), role);
530 if (flags & 1)
531 gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_MOUSE);
532 if ((flags & 2) && parent_window)
534 gtk_window_set_type_hint (GTK_WINDOW (win), GDK_WINDOW_TYPE_HINT_DIALOG);
535 gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent_window));
538 return win;
541 /* pass NULL as selection to paste to both clipboard & X11 text */
542 void
543 gtkutil_copy_to_clipboard (GtkWidget *widget, GdkAtom selection,
544 const gchar *str)
546 GtkWidget *win;
547 GtkClipboard *clip, *clip2;
549 win = gtk_widget_get_toplevel (GTK_WIDGET (widget));
550 if (GTK_WIDGET_TOPLEVEL (win))
552 int len = strlen (str);
554 if (selection)
556 clip = gtk_widget_get_clipboard (win, selection);
557 gtk_clipboard_set_text (clip, str, len);
558 } else
560 /* copy to both primary X selection and clipboard */
561 clip = gtk_widget_get_clipboard (win, GDK_SELECTION_PRIMARY);
562 clip2 = gtk_widget_get_clipboard (win, GDK_SELECTION_CLIPBOARD);
563 gtk_clipboard_set_text (clip, str, len);
564 gtk_clipboard_set_text (clip2, str, len);
569 /* Treeview util functions */
571 GtkWidget *
572 gtkutil_treeview_new (GtkWidget *box, GtkTreeModel *model,
573 GtkTreeCellDataFunc mapper, ...)
575 GtkWidget *win, *view;
576 GtkCellRenderer *renderer = NULL;
577 GtkTreeViewColumn *col;
578 va_list args;
579 int col_id = 0;
580 GType type;
581 char *title, *attr;
583 win = gtk_scrolled_window_new (0, 0);
584 gtk_container_add (GTK_CONTAINER (box), win);
585 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (win),
586 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
587 gtk_widget_show (win);
589 view = gtk_tree_view_new_with_model (model);
590 /* the view now has a ref on the model, we can unref it */
591 g_object_unref (G_OBJECT (model));
592 gtk_container_add (GTK_CONTAINER (win), view);
594 va_start (args, mapper);
595 for (col_id = va_arg (args, int); col_id != -1; col_id = va_arg (args, int))
597 type = gtk_tree_model_get_column_type (model, col_id);
598 switch (type)
600 case G_TYPE_BOOLEAN:
601 renderer = gtk_cell_renderer_toggle_new ();
602 attr = "active";
603 break;
604 case G_TYPE_STRING: /* fall through */
605 default:
606 renderer = gtk_cell_renderer_text_new ();
607 attr = "text";
608 break;
611 title = va_arg (args, char *);
612 if (mapper) /* user-specified function to set renderer attributes */
614 col = gtk_tree_view_column_new_with_attributes (title, renderer, NULL);
615 gtk_tree_view_column_set_cell_data_func (col, renderer, mapper,
616 GINT_TO_POINTER (col_id), NULL);
617 } else
619 /* just set the typical attribute for this type of renderer */
620 col = gtk_tree_view_column_new_with_attributes (title, renderer,
621 attr, col_id, NULL);
623 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
626 va_end (args);
628 return view;
631 gboolean
632 gtkutil_treemodel_string_to_iter (GtkTreeModel *model, gchar *pathstr, GtkTreeIter *iter_ret)
634 GtkTreePath *path = gtk_tree_path_new_from_string (pathstr);
635 gboolean success;
637 success = gtk_tree_model_get_iter (model, iter_ret, path);
638 gtk_tree_path_free (path);
639 return success;
642 /*gboolean
643 gtkutil_treeview_get_selected_iter (GtkTreeView *view, GtkTreeIter *iter_ret)
645 GtkTreeModel *store;
646 GtkTreeSelection *select;
648 select = gtk_tree_view_get_selection (view);
649 return gtk_tree_selection_get_selected (select, &store, iter_ret);
652 gboolean
653 gtkutil_treeview_get_selected (GtkTreeView *view, GtkTreeIter *iter_ret, ...)
655 GtkTreeModel *store;
656 GtkTreeSelection *select;
657 gboolean has_selected;
658 va_list args;
660 select = gtk_tree_view_get_selection (view);
661 has_selected = gtk_tree_selection_get_selected (select, &store, iter_ret);
663 if (has_selected) {
664 va_start (args, iter_ret);
665 gtk_tree_model_get_valist (store, iter_ret, args);
666 va_end (args);
669 return has_selected;