1 /* X-Chat
2  * Copyright (C) 1998 Peter Zelezny.
3  *
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.
8  *
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.
13  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
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 <fcntl.h>
26 
27 #include "fe-gtk.h"
28 
29 #include <gdk/gdkkeysyms.h>
30 #if defined (WIN32) || defined (__APPLE__)
31 #include <pango/pangocairo.h>
32 #endif
33 
34 #ifdef GDK_WINDOWING_X11
35 #include <gdk/gdkx.h>
36 #endif
37 
38 #include "../common/hexchat.h"
39 #include "../common/fe.h"
40 #include "../common/util.h"
41 #include "../common/cfgfiles.h"
42 #include "../common/hexchatc.h"
43 #include "../common/typedef.h"
44 #include "gtkutil.h"
45 #include "pixmaps.h"
46 
47 #ifdef WIN32
48 #include <io.h>
49 #else
50 #include <unistd.h>
51 #endif
52 
53 /* gtkutil.c, just some gtk wrappers */
54 
55 extern void path_part (char *file, char *path, int pathlen);
56 
57 struct file_req
58 {
59 	GtkWidget *dialog;
60 	void *userdata;
61 	filereqcallback callback;
62 	int flags;		/* FRF_* flags */
63 };
64 
65 static void
gtkutil_file_req_destroy(GtkWidget * wid,struct file_req * freq)66 gtkutil_file_req_destroy (GtkWidget * wid, struct file_req *freq)
67 {
68 	freq->callback (freq->userdata, NULL);
69 	g_free (freq);
70 }
71 
72 static void
gtkutil_check_file(char * filename,struct file_req * freq)73 gtkutil_check_file (char *filename, struct file_req *freq)
74 {
75 	int axs = FALSE;
76 
77 	GFile *file = g_file_new_for_path (filename);
78 
79 	if (freq->flags & FRF_WRITE)
80 	{
81 		GFile *parent = g_file_get_parent (file);
82 
83 		GFileInfo *fi = g_file_query_info (parent, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
84 		if (fi != NULL)
85 		{
86 			if (g_file_info_get_attribute_boolean (fi, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
87 			{
88 				axs = TRUE;
89 			}
90 
91 			g_object_unref (fi);
92 		}
93 
94 		g_object_unref (parent);
95 	}
96 	else
97 	{
98 		GFileInfo *fi = g_file_query_info (file, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
99 
100 		if (fi != NULL)
101 		{
102 			if (g_file_info_get_file_type (fi) != G_FILE_TYPE_DIRECTORY || (freq->flags & FRF_CHOOSEFOLDER))
103 			{
104 				axs = TRUE;
105 			}
106 
107 			g_object_unref (fi);
108 		}
109 	}
110 
111 	g_object_unref (file);
112 
113 	if (axs)
114 	{
115 		char *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
116 		if (filename_utf8 != NULL)
117 		{
118 			freq->callback (freq->userdata, filename_utf8);
119 			g_free (filename_utf8);
120 		}
121 		else
122 		{
123 			fe_message ("Filename encoding is corrupt.", FE_MSG_ERROR);
124 		}
125 	}
126 	else
127 	{
128 		if (freq->flags & FRF_WRITE)
129 		{
130 			fe_message (_("Cannot write to that file."), FE_MSG_ERROR);
131 		}
132 		else
133 		{
134 			fe_message (_("Cannot read that file."), FE_MSG_ERROR);
135 		}
136 	}
137 }
138 
139 static void
gtkutil_file_req_done(GtkWidget * wid,struct file_req * freq)140 gtkutil_file_req_done (GtkWidget * wid, struct file_req *freq)
141 {
142 	GSList *files, *cur;
143 	GtkFileChooser *fs = GTK_FILE_CHOOSER (freq->dialog);
144 
145 	if (freq->flags & FRF_MULTIPLE)
146 	{
147 		files = cur = gtk_file_chooser_get_filenames (fs);
148 		while (cur)
149 		{
150 			gtkutil_check_file (cur->data, freq);
151 			g_free (cur->data);
152 			cur = cur->next;
153 		}
154 		if (files)
155 			g_slist_free (files);
156 	}
157 	else
158 	{
159 		if (freq->flags & FRF_CHOOSEFOLDER)
160 		{
161 			gchar *filename = gtk_file_chooser_get_current_folder (fs);
162 			gtkutil_check_file (filename, freq);
163 			g_free (filename);
164 		}
165 		else
166 		{
167 			gchar *filename = gtk_file_chooser_get_filename (fs);
168 			gtkutil_check_file (gtk_file_chooser_get_filename (fs), freq);
169 			g_free (filename);
170 		}
171 	}
172 
173 	/* this should call the "destroy" cb, where we free(freq) */
174 	gtk_widget_destroy (freq->dialog);
175 }
176 
177 static void
gtkutil_file_req_response(GtkWidget * dialog,gint res,struct file_req * freq)178 gtkutil_file_req_response (GtkWidget *dialog, gint res, struct file_req *freq)
179 {
180 	switch (res)
181 	{
182 	case GTK_RESPONSE_ACCEPT:
183 		gtkutil_file_req_done (dialog, freq);
184 		break;
185 
186 	case GTK_RESPONSE_CANCEL:
187 		/* this should call the "destroy" cb, where we free(freq) */
188 		gtk_widget_destroy (freq->dialog);
189 	}
190 }
191 
192 void
gtkutil_file_req(const char * title,void * callback,void * userdata,char * filter,char * extensions,int flags)193 gtkutil_file_req (const char *title, void *callback, void *userdata, char *filter, char *extensions,
194 						int flags)
195 {
196 	struct file_req *freq;
197 	GtkWidget *dialog;
198 	GtkFileFilter *filefilter;
199 	extern char *get_xdir_fs (void);
200 	char *token;
201 	char *tokenbuffer;
202 
203 	if (flags & FRF_WRITE)
204 	{
205 		dialog = gtk_file_chooser_dialog_new (title, NULL,
206 												GTK_FILE_CHOOSER_ACTION_SAVE,
207 												GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
208 												GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
209 												NULL);
210 
211 		if (!(flags & FRF_NOASKOVERWRITE))
212 			gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
213 	}
214 	else
215 		dialog = gtk_file_chooser_dialog_new (title, NULL,
216 												GTK_FILE_CHOOSER_ACTION_OPEN,
217 												GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
218 												GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
219 												NULL);
220 
221 	if (filter && filter[0] && (flags & FRF_FILTERISINITIAL))
222 	{
223 		if (flags & FRF_WRITE)
224 		{
225 			char temp[1024];
226 			path_part (filter, temp, sizeof (temp));
227 			gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), temp);
228 			gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), file_part (filter));
229 		}
230 		else
231 			gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), filter);
232 	}
233 	else if (!(flags & FRF_RECENTLYUSED))
234 		gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), get_xdir ());
235 
236 	if (flags & FRF_MULTIPLE)
237 		gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);
238 	if (flags & FRF_CHOOSEFOLDER)
239 		gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
240 
241 	if ((flags & FRF_EXTENSIONS || flags & FRF_MIMETYPES) && extensions != NULL)
242 	{
243 		filefilter = gtk_file_filter_new ();
244 		tokenbuffer = g_strdup (extensions);
245 		token = strtok (tokenbuffer, ";");
246 
247 		while (token != NULL)
248 		{
249 			if (flags & FRF_EXTENSIONS)
250 				gtk_file_filter_add_pattern (filefilter, token);
251 			else
252 				gtk_file_filter_add_mime_type (filefilter, token);
253 			token = strtok (NULL, ";");
254 		}
255 
256 		g_free (tokenbuffer);
257 		gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filefilter);
258 	}
259 
260 	gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), get_xdir (), NULL);
261 
262 	freq = g_new (struct file_req, 1);
263 	freq->dialog = dialog;
264 	freq->flags = flags;
265 	freq->callback = callback;
266 	freq->userdata = userdata;
267 
268 	g_signal_connect (G_OBJECT (dialog), "response",
269 							G_CALLBACK (gtkutil_file_req_response), freq);
270 	g_signal_connect (G_OBJECT (dialog), "destroy",
271 						   G_CALLBACK (gtkutil_file_req_destroy), (gpointer) freq);
272 	gtk_widget_show (dialog);
273 }
274 
275 static gboolean
gtkutil_esc_destroy(GtkWidget * win,GdkEventKey * key,gpointer userdata)276 gtkutil_esc_destroy (GtkWidget * win, GdkEventKey * key, gpointer userdata)
277 {
278 	GtkWidget *wid;
279 
280 	/* Destroy the window of detached utils */
281 	if (!gtk_widget_is_toplevel (win))
282 	{
283 		if (gdk_window_get_type_hint (gtk_widget_get_window (win)) == GDK_WINDOW_TYPE_HINT_DIALOG)
284 			wid = gtk_widget_get_parent (win);
285 		else
286 			return FALSE;
287 	}
288 	else
289 		wid = win;
290 
291 	if (key->keyval == GDK_KEY_Escape)
292 		gtk_widget_destroy (wid);
293 
294 	return FALSE;
295 }
296 
297 void
gtkutil_destroy_on_esc(GtkWidget * win)298 gtkutil_destroy_on_esc (GtkWidget *win)
299 {
300 	g_signal_connect (G_OBJECT (win), "key_press_event", G_CALLBACK (gtkutil_esc_destroy), win);
301 }
302 
303 void
gtkutil_destroy(GtkWidget * igad,GtkWidget * dgad)304 gtkutil_destroy (GtkWidget * igad, GtkWidget * dgad)
305 {
306 	gtk_widget_destroy (dgad);
307 }
308 
309 static void
gtkutil_get_str_response(GtkDialog * dialog,gint arg1,gpointer entry)310 gtkutil_get_str_response (GtkDialog *dialog, gint arg1, gpointer entry)
311 {
312 	void (*callback) (int cancel, char *text, void *user_data);
313 	char *text;
314 	void *user_data;
315 
316 	text = (char *) gtk_entry_get_text (GTK_ENTRY (entry));
317 	callback = g_object_get_data (G_OBJECT (dialog), "cb");
318 	user_data = g_object_get_data (G_OBJECT (dialog), "ud");
319 
320 	switch (arg1)
321 	{
322 	case GTK_RESPONSE_REJECT:
323 		callback (TRUE, text, user_data);
324 		gtk_widget_destroy (GTK_WIDGET (dialog));
325 		break;
326 	case GTK_RESPONSE_ACCEPT:
327 		callback (FALSE, text, user_data);
328 		gtk_widget_destroy (GTK_WIDGET (dialog));
329 		break;
330 	}
331 }
332 
333 static void
gtkutil_str_enter(GtkWidget * entry,GtkWidget * dialog)334 gtkutil_str_enter (GtkWidget *entry, GtkWidget *dialog)
335 {
336 	gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
337 }
338 
339 void
fe_get_str(char * msg,char * def,void * callback,void * userdata)340 fe_get_str (char *msg, char *def, void *callback, void *userdata)
341 {
342 	GtkWidget *dialog;
343 	GtkWidget *entry;
344 	GtkWidget *hbox;
345 	GtkWidget *label;
346 	extern GtkWidget *parent_window;
347 
348 	dialog = gtk_dialog_new_with_buttons (msg, NULL, 0,
349 										GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
350 										GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
351 										NULL);
352 
353 	gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
354 	gtk_box_set_homogeneous (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), TRUE);
355 
356 	if (userdata == (void *)1)	/* nick box is usually on the very bottom, make it centered */
357 	{
358 		gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
359 	}
360 	else
361 	{
362 		gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
363 	}
364 
365 	hbox = gtk_hbox_new (TRUE, 0);
366 
367 	g_object_set_data (G_OBJECT (dialog), "cb", callback);
368 	g_object_set_data (G_OBJECT (dialog), "ud", userdata);
369 
370 	entry = gtk_entry_new ();
371 	g_signal_connect (G_OBJECT (entry), "activate",
372 						 	G_CALLBACK (gtkutil_str_enter), dialog);
373 	gtk_entry_set_text (GTK_ENTRY (entry), def);
374 	gtk_box_pack_end (GTK_BOX (hbox), entry, 0, 0, 0);
375 
376 	label = gtk_label_new (msg);
377 	gtk_box_pack_end (GTK_BOX (hbox), label, 0, 0, 0);
378 
379 	g_signal_connect (G_OBJECT (dialog), "response",
380 						   G_CALLBACK (gtkutil_get_str_response), entry);
381 
382 	gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox);
383 
384 	gtk_widget_show_all (dialog);
385 }
386 
387 static void
gtkutil_get_number_response(GtkDialog * dialog,gint arg1,gpointer spin)388 gtkutil_get_number_response (GtkDialog *dialog, gint arg1, gpointer spin)
389 {
390 	void (*callback) (int cancel, int value, void *user_data);
391 	int num;
392 	void *user_data;
393 
394 	num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spin));
395 	callback = g_object_get_data (G_OBJECT (dialog), "cb");
396 	user_data = g_object_get_data (G_OBJECT (dialog), "ud");
397 
398 	switch (arg1)
399 	{
400 	case GTK_RESPONSE_REJECT:
401 		callback (TRUE, num, user_data);
402 		gtk_widget_destroy (GTK_WIDGET (dialog));
403 		break;
404 	case GTK_RESPONSE_ACCEPT:
405 		callback (FALSE, num, user_data);
406 		gtk_widget_destroy (GTK_WIDGET (dialog));
407 		break;
408 	}
409 }
410 
411 static void
gtkutil_get_bool_response(GtkDialog * dialog,gint arg1,gpointer spin)412 gtkutil_get_bool_response (GtkDialog *dialog, gint arg1, gpointer spin)
413 {
414 	void (*callback) (int value, void *user_data);
415 	void *user_data;
416 
417 	callback = g_object_get_data (G_OBJECT (dialog), "cb");
418 	user_data = g_object_get_data (G_OBJECT (dialog), "ud");
419 
420 	switch (arg1)
421 	{
422 	case GTK_RESPONSE_REJECT:
423 		callback (0, user_data);
424 		gtk_widget_destroy (GTK_WIDGET (dialog));
425 		break;
426 	case GTK_RESPONSE_ACCEPT:
427 		callback (1, user_data);
428 		gtk_widget_destroy (GTK_WIDGET (dialog));
429 		break;
430 	}
431 }
432 
433 void
fe_get_int(char * msg,int def,void * callback,void * userdata)434 fe_get_int (char *msg, int def, void *callback, void *userdata)
435 {
436 	GtkWidget *dialog;
437 	GtkWidget *spin;
438 	GtkWidget *hbox;
439 	GtkWidget *label;
440 	GtkAdjustment *adj;
441 	extern GtkWidget *parent_window;
442 
443 	dialog = gtk_dialog_new_with_buttons (msg, NULL, 0,
444 										GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
445 										GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
446 										NULL);
447 	gtk_box_set_homogeneous (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), TRUE);
448 	gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
449 	gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
450 
451 	hbox = gtk_hbox_new (TRUE, 0);
452 
453 	g_object_set_data (G_OBJECT (dialog), "cb", callback);
454 	g_object_set_data (G_OBJECT (dialog), "ud", userdata);
455 
456 	spin = gtk_spin_button_new (NULL, 1, 0);
457 	adj = gtk_spin_button_get_adjustment ((GtkSpinButton*)spin);
458 	gtk_adjustment_set_lower (adj, 0);
459 	gtk_adjustment_set_upper (adj, 1024);
460 	gtk_adjustment_set_step_increment (adj, 1);
461 	gtk_adjustment_changed (adj);
462 	gtk_spin_button_set_value ((GtkSpinButton*)spin, def);
463 	gtk_box_pack_end (GTK_BOX (hbox), spin, 0, 0, 0);
464 
465 	label = gtk_label_new (msg);
466 	gtk_box_pack_end (GTK_BOX (hbox), label, 0, 0, 0);
467 
468 	g_signal_connect (G_OBJECT (dialog), "response",
469 						   G_CALLBACK (gtkutil_get_number_response), spin);
470 
471 	gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox);
472 
473 	gtk_widget_show_all (dialog);
474 }
475 
476 void
fe_get_bool(char * title,char * prompt,void * callback,void * userdata)477 fe_get_bool (char *title, char *prompt, void *callback, void *userdata)
478 {
479 	GtkWidget *dialog;
480 	GtkWidget *prompt_label;
481 	extern GtkWidget *parent_window;
482 
483 	dialog = gtk_dialog_new_with_buttons (title, NULL, 0,
484 		GTK_STOCK_NO, GTK_RESPONSE_REJECT,
485 		GTK_STOCK_YES, GTK_RESPONSE_ACCEPT,
486 		NULL);
487 	gtk_box_set_homogeneous (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), TRUE);
488 	gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
489 	gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
490 
491 
492 	g_object_set_data (G_OBJECT (dialog), "cb", callback);
493 	g_object_set_data (G_OBJECT (dialog), "ud", userdata);
494 
495 	prompt_label = gtk_label_new (prompt);
496 
497 	g_signal_connect (G_OBJECT (dialog), "response",
498 		G_CALLBACK (gtkutil_get_bool_response), NULL);
499 
500 	gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), prompt_label);
501 
502 	gtk_widget_show_all (dialog);
503 }
504 
505 GtkWidget *
gtkutil_button(GtkWidget * box,char * stock,char * tip,void * callback,void * userdata,char * labeltext)506 gtkutil_button (GtkWidget *box, char *stock, char *tip, void *callback,
507 					 void *userdata, char *labeltext)
508 {
509 	GtkWidget *wid, *img, *bbox;
510 
511 	wid = gtk_button_new ();
512 
513 	if (labeltext)
514 	{
515 		gtk_button_set_label (GTK_BUTTON (wid), labeltext);
516 		gtk_button_set_image (GTK_BUTTON (wid), gtk_image_new_from_stock (stock, GTK_ICON_SIZE_MENU));
517 		gtk_button_set_use_underline (GTK_BUTTON (wid), TRUE);
518 		if (box)
519 			gtk_container_add (GTK_CONTAINER (box), wid);
520 	}
521 	else
522 	{
523 		bbox = gtk_hbox_new (0, 0);
524 		gtk_container_add (GTK_CONTAINER (wid), bbox);
525 		gtk_widget_show (bbox);
526 
527 		img = gtk_image_new_from_stock (stock, GTK_ICON_SIZE_MENU);
528 		gtk_container_add (GTK_CONTAINER (bbox), img);
529 		gtk_widget_show (img);
530 		gtk_box_pack_start (GTK_BOX (box), wid, 0, 0, 0);
531 	}
532 
533 	g_signal_connect (G_OBJECT (wid), "clicked",
534 							G_CALLBACK (callback), userdata);
535 	gtk_widget_show (wid);
536 	if (tip)
537 		gtk_widget_set_tooltip_text (wid, tip);
538 
539 	return wid;
540 }
541 
542 void
gtkutil_label_new(char * text,GtkWidget * box)543 gtkutil_label_new (char *text, GtkWidget * box)
544 {
545 	GtkWidget *label = gtk_label_new (text);
546 	gtk_container_add (GTK_CONTAINER (box), label);
547 	gtk_widget_show (label);
548 }
549 
550 GtkWidget *
gtkutil_entry_new(int max,GtkWidget * box,void * callback,gpointer userdata)551 gtkutil_entry_new (int max, GtkWidget * box, void *callback,
552 						 gpointer userdata)
553 {
554 	GtkWidget *entry = gtk_entry_new ();
555 	gtk_entry_set_max_length (GTK_ENTRY (entry), max);
556 	gtk_container_add (GTK_CONTAINER (box), entry);
557 	if (callback)
558 		g_signal_connect (G_OBJECT (entry), "changed",
559 								G_CALLBACK (callback), userdata);
560 	gtk_widget_show (entry);
561 	return entry;
562 }
563 
564 void
show_and_unfocus(GtkWidget * wid)565 show_and_unfocus (GtkWidget * wid)
566 {
567 	gtk_widget_set_can_focus (wid, FALSE);
568 	gtk_widget_show (wid);
569 }
570 
571 void
gtkutil_set_icon(GtkWidget * win)572 gtkutil_set_icon (GtkWidget *win)
573 {
574 #ifndef WIN32
575 	/* FIXME: Magically breaks icon rendering in most
576 	 * (sub)windows, but OFC only on Windows. GTK <3
577 	 */
578 	gtk_window_set_icon (GTK_WINDOW (win), pix_hexchat);
579 #endif
580 }
581 
582 extern GtkWidget *parent_window;	/* maingui.c */
583 
584 GtkWidget *
gtkutil_window_new(char * title,char * role,int width,int height,int flags)585 gtkutil_window_new (char *title, char *role, int width, int height, int flags)
586 {
587 	GtkWidget *win;
588 
589 	win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
590 	gtkutil_set_icon (win);
591 #ifdef WIN32
592 	gtk_window_set_wmclass (GTK_WINDOW (win), "HexChat", "hexchat");
593 #endif
594 	gtk_window_set_title (GTK_WINDOW (win), title);
595 	gtk_window_set_default_size (GTK_WINDOW (win), width, height);
596 	gtk_window_set_role (GTK_WINDOW (win), role);
597 	if (flags & 1)
598 		gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_MOUSE);
599 	if ((flags & 2) && parent_window)
600 	{
601 		gtk_window_set_type_hint (GTK_WINDOW (win), GDK_WINDOW_TYPE_HINT_DIALOG);
602 		gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent_window));
603 		gtk_window_set_destroy_with_parent (GTK_WINDOW (win), TRUE);
604 	}
605 
606 	return win;
607 }
608 
609 /* pass NULL as selection to paste to both clipboard & X11 text */
610 void
gtkutil_copy_to_clipboard(GtkWidget * widget,GdkAtom selection,const gchar * str)611 gtkutil_copy_to_clipboard (GtkWidget *widget, GdkAtom selection,
612                            const gchar *str)
613 {
614 	GtkWidget *win;
615 	GtkClipboard *clip, *clip2;
616 
617 	win = gtk_widget_get_toplevel (GTK_WIDGET (widget));
618 	if (gtk_widget_is_toplevel (win))
619 	{
620 		int len = strlen (str);
621 
622 		if (selection)
623 		{
624 			clip = gtk_widget_get_clipboard (win, selection);
625 			gtk_clipboard_set_text (clip, str, len);
626 		} else
627 		{
628 			/* copy to both primary X selection and clipboard */
629 			clip = gtk_widget_get_clipboard (win, GDK_SELECTION_PRIMARY);
630 			clip2 = gtk_widget_get_clipboard (win, GDK_SELECTION_CLIPBOARD);
631 			gtk_clipboard_set_text (clip, str, len);
632 			gtk_clipboard_set_text (clip2, str, len);
633 		}
634 	}
635 }
636 
637 /* Treeview util functions */
638 
639 GtkWidget *
gtkutil_treeview_new(GtkWidget * box,GtkTreeModel * model,GtkTreeCellDataFunc mapper,...)640 gtkutil_treeview_new (GtkWidget *box, GtkTreeModel *model,
641                       GtkTreeCellDataFunc mapper, ...)
642 {
643 	GtkWidget *win, *view;
644 	GtkCellRenderer *renderer = NULL;
645 	GtkTreeViewColumn *col;
646 	va_list args;
647 	int col_id = 0;
648 	GType type;
649 	char *title, *attr;
650 
651 	win = gtk_scrolled_window_new (0, 0);
652 	gtk_container_add (GTK_CONTAINER (box), win);
653 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (win),
654 											  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
655 	gtk_widget_show (win);
656 
657 	view = gtk_tree_view_new_with_model (model);
658 	/* the view now has a ref on the model, we can unref it */
659 	g_object_unref (G_OBJECT (model));
660 	gtk_container_add (GTK_CONTAINER (win), view);
661 
662 	va_start (args, mapper);
663 	for (col_id = va_arg (args, int); col_id != -1; col_id = va_arg (args, int))
664 	{
665 		type = gtk_tree_model_get_column_type (model, col_id);
666 		switch (type)
667 		{
668 			case G_TYPE_BOOLEAN:
669 				renderer = gtk_cell_renderer_toggle_new ();
670 				attr = "active";
671 				break;
672 			case G_TYPE_STRING:	/* fall through */
673 			default:
674 				renderer = gtk_cell_renderer_text_new ();
675 				attr = "text";
676 				break;
677 		}
678 
679 		title = va_arg (args, char *);
680 		if (mapper)	/* user-specified function to set renderer attributes */
681 		{
682 			col = gtk_tree_view_column_new_with_attributes (title, renderer, NULL);
683 			gtk_tree_view_column_set_cell_data_func (col, renderer, mapper,
684 			                                         GINT_TO_POINTER (col_id), NULL);
685 		} else
686 		{
687 			/* just set the typical attribute for this type of renderer */
688 			col = gtk_tree_view_column_new_with_attributes (title, renderer,
689 			                                                attr, col_id, NULL);
690 		}
691 		gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
692 		if (title == NULL)
693 			gtk_tree_view_column_set_visible (col, FALSE);
694 	}
695 
696 	va_end (args);
697 
698 	return view;
699 }
700 
701 gboolean
gtkutil_treemodel_string_to_iter(GtkTreeModel * model,gchar * pathstr,GtkTreeIter * iter_ret)702 gtkutil_treemodel_string_to_iter (GtkTreeModel *model, gchar *pathstr, GtkTreeIter *iter_ret)
703 {
704 	GtkTreePath *path = gtk_tree_path_new_from_string (pathstr);
705 	gboolean success;
706 
707 	success = gtk_tree_model_get_iter (model, iter_ret, path);
708 	gtk_tree_path_free (path);
709 	return success;
710 }
711 
712 /*gboolean
713 gtkutil_treeview_get_selected_iter (GtkTreeView *view, GtkTreeIter *iter_ret)
714 {
715 	GtkTreeModel *store;
716 	GtkTreeSelection *select;
717 
718 	select = gtk_tree_view_get_selection (view);
719 	return gtk_tree_selection_get_selected (select, &store, iter_ret);
720 }*/
721 
722 gboolean
gtkutil_treeview_get_selected(GtkTreeView * view,GtkTreeIter * iter_ret,...)723 gtkutil_treeview_get_selected (GtkTreeView *view, GtkTreeIter *iter_ret, ...)
724 {
725 	GtkTreeModel *store;
726 	GtkTreeSelection *select;
727 	gboolean has_selected;
728 	va_list args;
729 
730 	select = gtk_tree_view_get_selection (view);
731 	has_selected = gtk_tree_selection_get_selected (select, &store, iter_ret);
732 
733 	if (has_selected) {
734 		va_start (args, iter_ret);
735 		gtk_tree_model_get_valist (store, iter_ret, args);
736 		va_end (args);
737 	}
738 
739 	return has_selected;
740 }
741 
742 gboolean
gtkutil_tray_icon_supported(GtkWindow * window)743 gtkutil_tray_icon_supported (GtkWindow *window)
744 {
745 #ifndef GDK_WINDOWING_X11
746 	return TRUE;
747 #else
748 	GdkScreen *screen = gtk_window_get_screen (window);
749 	GdkDisplay *display = gdk_screen_get_display (screen);
750 	int screen_number = gdk_screen_get_number (screen);
751 	Display *xdisplay = gdk_x11_display_get_xdisplay (display);
752 	char *selection_name = g_strdup_printf ("_NET_SYSTEM_TRAY_S%d", screen_number);
753 	Atom selection_atom = XInternAtom (xdisplay, selection_name, False);
754 	Window tray_window = None;
755 
756 	XGrabServer (xdisplay);
757 
758 	tray_window = XGetSelectionOwner (xdisplay, selection_atom);
759 
760 	XUngrabServer (xdisplay);
761 	XFlush (xdisplay);
762 	g_free (selection_name);
763 
764 	return (tray_window != None);
765 #endif
766 }
767 
768 #if defined (WIN32) || defined (__APPLE__)
769 gboolean
gtkutil_find_font(const char * fontname)770 gtkutil_find_font (const char *fontname)
771 {
772 	int i;
773 	int n_families;
774 	const char *family_name;
775 	PangoFontMap *fontmap;
776 	PangoFontFamily *family;
777 	PangoFontFamily **families;
778 
779 	fontmap = pango_cairo_font_map_get_default ();
780 	pango_font_map_list_families (fontmap, &families, &n_families);
781 
782 	for (i = 0; i < n_families; i++)
783 	{
784 		family = families[i];
785 		family_name = pango_font_family_get_name (family);
786 
787 		if (!g_ascii_strcasecmp (family_name, fontname))
788 		{
789 			g_free (families);
790 			return TRUE;
791 		}
792 	}
793 
794 	g_free (families);
795 	return FALSE;
796 }
797 #endif
798