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 */
23 #include <sys/types.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"
55 /* gtkutil.c, just some gtk wrappers */
57 extern void path_part (char *file
, char *path
, int pathlen
);
64 filereqcallback callback
;
65 int flags
; /* FRF_* flags */
68 static char last_dir
[256] = "";
72 gtkutil_file_req_destroy (GtkWidget
* wid
, struct file_req
*freq
)
74 freq
->callback (freq
->userdata
, NULL
);
79 gtkutil_check_file (char *file
, struct file_req
*freq
)
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)
93 if (stat (file
, &st
) != -1)
95 if (!S_ISDIR (st
.st_mode
) || (freq
->flags
& FRF_CHOOSEFOLDER
))
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
);
108 freq
->callback (freq
->userdata
, utf8_file
);
112 fe_message ("Filename encoding is corrupt.", FE_MSG_ERROR
);
116 if (freq
->flags
& FRF_WRITE
)
117 fe_message (_("Cannot write to that file."), FE_MSG_ERROR
);
119 fe_message (_("Cannot read that file."), FE_MSG_ERROR
);
124 gtkutil_file_req_done (GtkWidget
* wid
, struct file_req
*freq
)
127 GtkFileChooser
*fs
= GTK_FILE_CHOOSER (freq
->dialog
);
129 if (freq
->flags
& FRF_MULTIPLE
)
131 files
= cur
= gtk_file_chooser_get_filenames (fs
);
134 gtkutil_check_file (cur
->data
, freq
);
139 g_slist_free (files
);
142 if (freq
->flags
& FRF_CHOOSEFOLDER
)
143 gtkutil_check_file (gtk_file_chooser_get_current_folder (fs
), freq
);
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
);
153 gtkutil_file_req_response (GtkWidget
*dialog
, gint res
, struct file_req
*freq
)
157 case GTK_RESPONSE_ACCEPT
:
158 gtkutil_file_req_done (dialog
, freq
);
161 case GTK_RESPONSE_CANCEL
:
162 /* this should call the "destroy" cb, where we free(freq) */
163 gtk_widget_destroy (freq
->dialog
);
168 gtkutil_file_req (const char *title
, void *callback
, void *userdata
, char *filter
,
171 struct file_req
*freq
;
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
,
182 if (filter
&& filter
[0]) /* filter becomes initial name when saving */
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
);
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
,
199 if (flags
& FRF_MULTIPLE
)
200 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog
), TRUE
);
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
);
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
;
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
);
231 gtkutil_destroy (GtkWidget
* igad
, GtkWidget
* dgad
)
233 gtk_widget_destroy (dgad
);
237 gtkutil_get_str_response (GtkDialog
*dialog
, gint arg1
, gpointer entry
)
239 void (*callback
) (int cancel
, char *text
, 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");
249 case GTK_RESPONSE_REJECT
:
250 callback (TRUE
, text
, user_data
);
251 gtk_widget_destroy (GTK_WIDGET (dialog
));
253 case GTK_RESPONSE_ACCEPT
:
254 callback (FALSE
, text
, user_data
);
255 gtk_widget_destroy (GTK_WIDGET (dialog
));
261 gtkutil_str_enter (GtkWidget
*entry
, GtkWidget
*dialog
)
263 gtk_dialog_response (GTK_DIALOG (dialog
), GTK_RESPONSE_ACCEPT
);
267 fe_get_str (char *msg
, char *def
, void *callback
, void *userdata
)
274 dialog
= gtk_dialog_new_with_buttons (msg
, NULL
, 0,
275 GTK_STOCK_CANCEL
, GTK_RESPONSE_REJECT
,
276 GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
,
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
);
303 gtkutil_get_number_response (GtkDialog
*dialog
, gint arg1
, gpointer spin
)
305 void (*callback
) (int cancel
, int value
, 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");
315 case GTK_RESPONSE_REJECT
:
316 callback (TRUE
, num
, user_data
);
317 gtk_widget_destroy (GTK_WIDGET (dialog
));
319 case GTK_RESPONSE_ACCEPT
:
320 callback (FALSE
, num
, user_data
);
321 gtk_widget_destroy (GTK_WIDGET (dialog
));
327 fe_get_int (char *msg
, int def
, void *callback
, void *userdata
)
335 dialog
= gtk_dialog_new_with_buttons (msg
, NULL
, 0,
336 GTK_STOCK_CANCEL
, GTK_RESPONSE_REJECT
,
337 GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
,
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
);
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
);
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 ();
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
);
380 gtk_container_add (GTK_CONTAINER (box
), wid
);
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
);
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
);
414 gtkutil_entry_new (int max
, GtkWidget
* box
, void *callback
,
417 GtkWidget
*entry
= gtk_entry_new_with_max_length (max
);
418 gtk_container_add (GTK_CONTAINER (box
), entry
);
420 g_signal_connect (G_OBJECT (entry
), "changed",
421 G_CALLBACK (callback
), userdata
);
422 gtk_widget_show (entry
);
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
);
442 clist
= gtk_clist_new_with_titles (columns
, titles
);
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
);
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
);
465 gtkutil_clist_selection (GtkWidget
* clist
)
467 if (GTK_CLIST (clist
)->selection
)
468 return GPOINTER_TO_INT(GTK_CLIST (clist
)->selection
->data
);
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
)
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
);
497 add_tip (GtkWidget
* wid
, char *text
)
499 static GtkTooltips
*tip
= NULL
;
501 tip
= gtk_tooltips_new ();
502 gtk_tooltips_set_tip (tip
, wid
, text
, 0);
506 show_and_unfocus (GtkWidget
* wid
)
508 GTK_WIDGET_UNSET_FLAGS (wid
, GTK_CAN_FOCUS
);
509 gtk_widget_show (wid
);
513 gtkutil_set_icon (GtkWidget
*win
)
515 gtk_window_set_icon (GTK_WINDOW (win
), pix_xchat
);
518 extern GtkWidget
*parent_window
; /* maingui.c */
521 gtkutil_window_new (char *title
, char *role
, int width
, int height
, int flags
)
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
);
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
));
541 /* pass NULL as selection to paste to both clipboard & X11 text */
543 gtkutil_copy_to_clipboard (GtkWidget
*widget
, GdkAtom selection
,
547 GtkClipboard
*clip
, *clip2
;
549 win
= gtk_widget_get_toplevel (GTK_WIDGET (widget
));
550 if (GTK_WIDGET_TOPLEVEL (win
))
552 int len
= strlen (str
);
556 clip
= gtk_widget_get_clipboard (win
, selection
);
557 gtk_clipboard_set_text (clip
, str
, len
);
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 */
572 gtkutil_treeview_new (GtkWidget
*box
, GtkTreeModel
*model
,
573 GtkTreeCellDataFunc mapper
, ...)
575 GtkWidget
*win
, *view
;
576 GtkCellRenderer
*renderer
= NULL
;
577 GtkTreeViewColumn
*col
;
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
);
601 renderer
= gtk_cell_renderer_toggle_new ();
604 case G_TYPE_STRING
: /* fall through */
606 renderer
= gtk_cell_renderer_text_new ();
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
);
619 /* just set the typical attribute for this type of renderer */
620 col
= gtk_tree_view_column_new_with_attributes (title
, renderer
,
623 gtk_tree_view_append_column (GTK_TREE_VIEW (view
), col
);
632 gtkutil_treemodel_string_to_iter (GtkTreeModel
*model
, gchar
*pathstr
, GtkTreeIter
*iter_ret
)
634 GtkTreePath
*path
= gtk_tree_path_new_from_string (pathstr
);
637 success
= gtk_tree_model_get_iter (model
, iter_ret
, path
);
638 gtk_tree_path_free (path
);
643 gtkutil_treeview_get_selected_iter (GtkTreeView *view, GtkTreeIter *iter_ret)
646 GtkTreeSelection *select;
648 select = gtk_tree_view_get_selection (view);
649 return gtk_tree_selection_get_selected (select, &store, iter_ret);
653 gtkutil_treeview_get_selected (GtkTreeView
*view
, GtkTreeIter
*iter_ret
, ...)
656 GtkTreeSelection
*select
;
657 gboolean has_selected
;
660 select
= gtk_tree_view_get_selection (view
);
661 has_selected
= gtk_tree_selection_get_selected (select
, &store
, iter_ret
);
664 va_start (args
, iter_ret
);
665 gtk_tree_model_get_valist (store
, iter_ret
, args
);