2011-02-24 04:14:30 +01:00
|
|
|
/* X-Chat
|
|
|
|
* Copyright (C) 1998 Peter Zelezny.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-12-23 20:36:54 +01:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2011-02-24 04:14:30 +01:00
|
|
|
*/
|
|
|
|
#define _FILE_OFFSET_BITS 64 /* allow selection of large files */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2011-12-11 17:34:02 +01:00
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
#include "fe-gtk.h"
|
|
|
|
|
2013-03-17 23:11:23 +01:00
|
|
|
#include <gdk/gdkkeysyms.h>
|
2014-01-31 23:20:17 +01:00
|
|
|
#if defined (WIN32) || defined (__APPLE__)
|
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
#endif
|
2011-02-24 04:14:30 +01:00
|
|
|
|
2012-10-24 21:33:02 +02:00
|
|
|
#include "../common/hexchat.h"
|
2011-02-24 04:14:30 +01:00
|
|
|
#include "../common/fe.h"
|
|
|
|
#include "../common/util.h"
|
2012-10-22 17:00:48 +02:00
|
|
|
#include "../common/cfgfiles.h"
|
2012-10-24 21:33:02 +02:00
|
|
|
#include "../common/hexchatc.h"
|
2012-10-30 22:13:40 +01:00
|
|
|
#include "../common/typedef.h"
|
2011-02-24 04:14:30 +01:00
|
|
|
#include "gtkutil.h"
|
|
|
|
#include "pixmaps.h"
|
2011-12-11 17:34:02 +01:00
|
|
|
|
2011-02-28 18:59:32 +01:00
|
|
|
#ifdef WIN32
|
2012-07-21 14:26:19 +02:00
|
|
|
#include <io.h>
|
2011-12-11 17:34:02 +01:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2011-02-28 18:59:32 +01:00
|
|
|
#endif
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
/* gtkutil.c, just some gtk wrappers */
|
|
|
|
|
|
|
|
extern void path_part (char *file, char *path, int pathlen);
|
|
|
|
|
|
|
|
struct file_req
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
void *userdata;
|
|
|
|
filereqcallback callback;
|
|
|
|
int flags; /* FRF_* flags */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtkutil_file_req_destroy (GtkWidget * wid, struct file_req *freq)
|
|
|
|
{
|
|
|
|
freq->callback (freq->userdata, NULL);
|
|
|
|
free (freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtkutil_check_file (char *file, struct file_req *freq)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int axs = FALSE;
|
2013-08-31 02:19:10 +02:00
|
|
|
char temp[256];
|
2011-02-24 04:14:30 +01:00
|
|
|
|
2013-08-31 02:19:10 +02:00
|
|
|
path_part (file, temp, sizeof (temp));
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
/* check if the file is readable or writable */
|
|
|
|
if (freq->flags & FRF_WRITE)
|
|
|
|
{
|
2013-08-31 02:19:10 +02:00
|
|
|
if (access (temp, W_OK) == 0)
|
2011-02-24 04:14:30 +01:00
|
|
|
axs = TRUE;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (stat (file, &st) != -1)
|
|
|
|
{
|
|
|
|
if (!S_ISDIR (st.st_mode) || (freq->flags & FRF_CHOOSEFOLDER))
|
|
|
|
axs = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (axs)
|
|
|
|
{
|
|
|
|
char *utf8_file;
|
|
|
|
/* convert to UTF8. It might be converted back to locale by
|
|
|
|
server.c's g_convert */
|
2012-10-30 11:35:39 +01:00
|
|
|
utf8_file = hexchat_filename_to_utf8 (file, -1, NULL, NULL, NULL);
|
2011-02-24 04:14:30 +01:00
|
|
|
if (utf8_file)
|
|
|
|
{
|
|
|
|
freq->callback (freq->userdata, utf8_file);
|
|
|
|
g_free (utf8_file);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
fe_message ("Filename encoding is corrupt.", FE_MSG_ERROR);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (freq->flags & FRF_WRITE)
|
|
|
|
fe_message (_("Cannot write to that file."), FE_MSG_ERROR);
|
|
|
|
else
|
|
|
|
fe_message (_("Cannot read that file."), FE_MSG_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtkutil_file_req_done (GtkWidget * wid, struct file_req *freq)
|
|
|
|
{
|
|
|
|
GSList *files, *cur;
|
|
|
|
GtkFileChooser *fs = GTK_FILE_CHOOSER (freq->dialog);
|
|
|
|
|
|
|
|
if (freq->flags & FRF_MULTIPLE)
|
|
|
|
{
|
|
|
|
files = cur = gtk_file_chooser_get_filenames (fs);
|
|
|
|
while (cur)
|
|
|
|
{
|
|
|
|
gtkutil_check_file (cur->data, freq);
|
|
|
|
g_free (cur->data);
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
if (files)
|
|
|
|
g_slist_free (files);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (freq->flags & FRF_CHOOSEFOLDER)
|
|
|
|
gtkutil_check_file (gtk_file_chooser_get_current_folder (fs), freq);
|
|
|
|
else
|
|
|
|
gtkutil_check_file (gtk_file_chooser_get_filename (fs), freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this should call the "destroy" cb, where we free(freq) */
|
|
|
|
gtk_widget_destroy (freq->dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtkutil_file_req_response (GtkWidget *dialog, gint res, struct file_req *freq)
|
|
|
|
{
|
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case GTK_RESPONSE_ACCEPT:
|
|
|
|
gtkutil_file_req_done (dialog, freq);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_RESPONSE_CANCEL:
|
|
|
|
/* this should call the "destroy" cb, where we free(freq) */
|
|
|
|
gtk_widget_destroy (freq->dialog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-07-21 21:42:48 +02:00
|
|
|
gtkutil_file_req (const char *title, void *callback, void *userdata, char *filter, char *extensions,
|
2011-02-24 04:14:30 +01:00
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
struct file_req *freq;
|
|
|
|
GtkWidget *dialog;
|
2012-07-21 21:42:48 +02:00
|
|
|
GtkFileFilter *filefilter;
|
2011-02-24 04:14:30 +01:00
|
|
|
extern char *get_xdir_fs (void);
|
2012-07-21 21:42:48 +02:00
|
|
|
char *token;
|
|
|
|
char *tokenbuffer;
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
if (flags & FRF_WRITE)
|
|
|
|
{
|
|
|
|
dialog = gtk_file_chooser_dialog_new (title, NULL,
|
|
|
|
GTK_FILE_CHOOSER_ACTION_SAVE,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL);
|
2012-07-14 20:46:42 +02:00
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
if (!(flags & FRF_NOASKOVERWRITE))
|
|
|
|
gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dialog = gtk_file_chooser_dialog_new (title, NULL,
|
|
|
|
GTK_FILE_CHOOSER_ACTION_OPEN,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL);
|
2013-08-31 02:19:10 +02:00
|
|
|
|
|
|
|
if (filter && filter[0] && (flags & FRF_FILTERISINITIAL))
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
2013-08-31 02:19:10 +02:00
|
|
|
if (flags & FRF_WRITE)
|
2012-10-22 17:00:48 +02:00
|
|
|
{
|
2013-08-31 02:19:10 +02:00
|
|
|
char temp[1024];
|
|
|
|
path_part (filter, temp, sizeof (temp));
|
|
|
|
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), temp);
|
|
|
|
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), file_part (filter));
|
2012-10-22 17:00:48 +02:00
|
|
|
}
|
|
|
|
else
|
2013-08-31 02:19:10 +02:00
|
|
|
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), filter);
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
2013-08-31 02:19:10 +02:00
|
|
|
else if (!(flags & FRF_RECENTLYUSED))
|
|
|
|
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), get_xdir ());
|
|
|
|
|
|
|
|
if (flags & FRF_MULTIPLE)
|
|
|
|
gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);
|
|
|
|
if (flags & FRF_CHOOSEFOLDER)
|
|
|
|
gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
|
2011-02-24 04:14:30 +01:00
|
|
|
|
2013-08-31 21:31:22 +02:00
|
|
|
if ((flags & FRF_EXTENSIONS || flags & FRF_MIMETYPES) && extensions != NULL)
|
2012-07-21 21:42:48 +02:00
|
|
|
{
|
|
|
|
filefilter = gtk_file_filter_new ();
|
|
|
|
tokenbuffer = g_strdup (extensions);
|
|
|
|
token = strtok (tokenbuffer, ";");
|
|
|
|
|
|
|
|
while (token != NULL)
|
|
|
|
{
|
2013-08-31 21:31:22 +02:00
|
|
|
if (flags & FRF_EXTENSIONS)
|
|
|
|
gtk_file_filter_add_pattern (filefilter, token);
|
|
|
|
else
|
|
|
|
gtk_file_filter_add_mime_type (filefilter, token);
|
2012-07-21 21:42:48 +02:00
|
|
|
token = strtok (NULL, ";");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (tokenbuffer);
|
|
|
|
gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filefilter);
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:19:10 +02:00
|
|
|
gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), get_xdir (), NULL);
|
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
freq = malloc (sizeof (struct file_req));
|
|
|
|
freq->dialog = dialog;
|
|
|
|
freq->flags = flags;
|
|
|
|
freq->callback = callback;
|
|
|
|
freq->userdata = userdata;
|
|
|
|
|
|
|
|
g_signal_connect (G_OBJECT (dialog), "response",
|
|
|
|
G_CALLBACK (gtkutil_file_req_response), freq);
|
|
|
|
g_signal_connect (G_OBJECT (dialog), "destroy",
|
|
|
|
G_CALLBACK (gtkutil_file_req_destroy), (gpointer) freq);
|
|
|
|
gtk_widget_show (dialog);
|
|
|
|
}
|
|
|
|
|
2013-03-17 23:11:23 +01:00
|
|
|
static gboolean
|
|
|
|
gtkutil_esc_destroy (GtkWidget * win, GdkEventKey * key, gpointer userdata)
|
|
|
|
{
|
2013-04-05 22:33:35 +02:00
|
|
|
GtkWidget *wid;
|
2013-04-05 21:56:57 +02:00
|
|
|
|
2013-04-05 22:33:35 +02:00
|
|
|
/* Destroy the window of detached utils */
|
|
|
|
if (!gtk_widget_is_toplevel (win))
|
|
|
|
{
|
|
|
|
if (gdk_window_get_type_hint (gtk_widget_get_window (win)) == GDK_WINDOW_TYPE_HINT_DIALOG)
|
|
|
|
wid = gtk_widget_get_parent (win);
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wid = win;
|
2013-04-05 21:56:57 +02:00
|
|
|
|
2013-09-15 09:21:56 +02:00
|
|
|
if (key->keyval == GDK_KEY_Escape)
|
2013-04-05 22:33:35 +02:00
|
|
|
gtk_widget_destroy (wid);
|
|
|
|
|
2013-03-17 23:11:23 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtkutil_destroy_on_esc (GtkWidget *win)
|
|
|
|
{
|
|
|
|
g_signal_connect (G_OBJECT (win), "key_press_event", G_CALLBACK (gtkutil_esc_destroy), win);
|
|
|
|
}
|
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
void
|
|
|
|
gtkutil_destroy (GtkWidget * igad, GtkWidget * dgad)
|
|
|
|
{
|
|
|
|
gtk_widget_destroy (dgad);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtkutil_get_str_response (GtkDialog *dialog, gint arg1, gpointer entry)
|
|
|
|
{
|
|
|
|
void (*callback) (int cancel, char *text, void *user_data);
|
|
|
|
char *text;
|
|
|
|
void *user_data;
|
|
|
|
|
|
|
|
text = (char *) gtk_entry_get_text (GTK_ENTRY (entry));
|
|
|
|
callback = g_object_get_data (G_OBJECT (dialog), "cb");
|
|
|
|
user_data = g_object_get_data (G_OBJECT (dialog), "ud");
|
|
|
|
|
|
|
|
switch (arg1)
|
|
|
|
{
|
|
|
|
case GTK_RESPONSE_REJECT:
|
|
|
|
callback (TRUE, text, user_data);
|
|
|
|
gtk_widget_destroy (GTK_WIDGET (dialog));
|
|
|
|
break;
|
|
|
|
case GTK_RESPONSE_ACCEPT:
|
|
|
|
callback (FALSE, text, user_data);
|
|
|
|
gtk_widget_destroy (GTK_WIDGET (dialog));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtkutil_str_enter (GtkWidget *entry, GtkWidget *dialog)
|
|
|
|
{
|
|
|
|
gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fe_get_str (char *msg, char *def, void *callback, void *userdata)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
GtkWidget *entry;
|
|
|
|
GtkWidget *hbox;
|
|
|
|
GtkWidget *label;
|
2013-07-03 20:48:45 +02:00
|
|
|
extern GtkWidget *parent_window;
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
dialog = gtk_dialog_new_with_buttons (msg, NULL, 0,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
|
|
|
|
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL);
|
2013-05-12 07:43:27 +02:00
|
|
|
|
2013-07-03 20:48:45 +02:00
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
|
2013-05-12 07:43:27 +02:00
|
|
|
gtk_box_set_homogeneous (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), TRUE);
|
2012-10-22 22:27:30 +02:00
|
|
|
|
2013-01-19 21:33:16 +01:00
|
|
|
if (userdata == (void *)1) /* nick box is usually on the very bottom, make it centered */
|
2012-10-22 22:27:30 +02:00
|
|
|
{
|
|
|
|
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
|
|
|
|
}
|
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
hbox = gtk_hbox_new (TRUE, 0);
|
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (dialog), "cb", callback);
|
|
|
|
g_object_set_data (G_OBJECT (dialog), "ud", userdata);
|
|
|
|
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
g_signal_connect (G_OBJECT (entry), "activate",
|
|
|
|
G_CALLBACK (gtkutil_str_enter), dialog);
|
|
|
|
gtk_entry_set_text (GTK_ENTRY (entry), def);
|
|
|
|
gtk_box_pack_end (GTK_BOX (hbox), entry, 0, 0, 0);
|
|
|
|
|
|
|
|
label = gtk_label_new (msg);
|
|
|
|
gtk_box_pack_end (GTK_BOX (hbox), label, 0, 0, 0);
|
|
|
|
|
|
|
|
g_signal_connect (G_OBJECT (dialog), "response",
|
|
|
|
G_CALLBACK (gtkutil_get_str_response), entry);
|
|
|
|
|
2013-05-12 07:43:27 +02:00
|
|
|
gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox);
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
gtk_widget_show_all (dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtkutil_get_number_response (GtkDialog *dialog, gint arg1, gpointer spin)
|
|
|
|
{
|
|
|
|
void (*callback) (int cancel, int value, void *user_data);
|
|
|
|
int num;
|
|
|
|
void *user_data;
|
|
|
|
|
|
|
|
num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spin));
|
|
|
|
callback = g_object_get_data (G_OBJECT (dialog), "cb");
|
|
|
|
user_data = g_object_get_data (G_OBJECT (dialog), "ud");
|
|
|
|
|
|
|
|
switch (arg1)
|
|
|
|
{
|
|
|
|
case GTK_RESPONSE_REJECT:
|
|
|
|
callback (TRUE, num, user_data);
|
|
|
|
gtk_widget_destroy (GTK_WIDGET (dialog));
|
|
|
|
break;
|
|
|
|
case GTK_RESPONSE_ACCEPT:
|
|
|
|
callback (FALSE, num, user_data);
|
|
|
|
gtk_widget_destroy (GTK_WIDGET (dialog));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 23:06:58 +01:00
|
|
|
static void
|
|
|
|
gtkutil_get_bool_response (GtkDialog *dialog, gint arg1, gpointer spin)
|
|
|
|
{
|
|
|
|
void (*callback) (int value, void *user_data);
|
|
|
|
void *user_data;
|
|
|
|
|
|
|
|
callback = g_object_get_data (G_OBJECT (dialog), "cb");
|
|
|
|
user_data = g_object_get_data (G_OBJECT (dialog), "ud");
|
|
|
|
|
|
|
|
switch (arg1)
|
|
|
|
{
|
|
|
|
case GTK_RESPONSE_REJECT:
|
|
|
|
callback (0, user_data);
|
|
|
|
gtk_widget_destroy (GTK_WIDGET (dialog));
|
|
|
|
break;
|
|
|
|
case GTK_RESPONSE_ACCEPT:
|
|
|
|
callback (1, user_data);
|
|
|
|
gtk_widget_destroy (GTK_WIDGET (dialog));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
void
|
|
|
|
fe_get_int (char *msg, int def, void *callback, void *userdata)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
GtkWidget *spin;
|
|
|
|
GtkWidget *hbox;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkAdjustment *adj;
|
2013-07-03 20:48:45 +02:00
|
|
|
extern GtkWidget *parent_window;
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
dialog = gtk_dialog_new_with_buttons (msg, NULL, 0,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
|
|
|
|
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL);
|
2013-05-12 07:43:27 +02:00
|
|
|
gtk_box_set_homogeneous (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), TRUE);
|
2011-02-24 04:14:30 +01:00
|
|
|
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
|
2013-07-03 20:48:45 +02:00
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
|
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
hbox = gtk_hbox_new (TRUE, 0);
|
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (dialog), "cb", callback);
|
|
|
|
g_object_set_data (G_OBJECT (dialog), "ud", userdata);
|
|
|
|
|
|
|
|
spin = gtk_spin_button_new (NULL, 1, 0);
|
|
|
|
adj = gtk_spin_button_get_adjustment ((GtkSpinButton*)spin);
|
2013-05-12 07:43:27 +02:00
|
|
|
gtk_adjustment_set_lower (adj, 0);
|
|
|
|
gtk_adjustment_set_upper (adj, 1024);
|
|
|
|
gtk_adjustment_set_step_increment (adj, 1);
|
2011-02-24 04:14:30 +01:00
|
|
|
gtk_adjustment_changed (adj);
|
|
|
|
gtk_spin_button_set_value ((GtkSpinButton*)spin, def);
|
|
|
|
gtk_box_pack_end (GTK_BOX (hbox), spin, 0, 0, 0);
|
|
|
|
|
|
|
|
label = gtk_label_new (msg);
|
|
|
|
gtk_box_pack_end (GTK_BOX (hbox), label, 0, 0, 0);
|
|
|
|
|
|
|
|
g_signal_connect (G_OBJECT (dialog), "response",
|
|
|
|
G_CALLBACK (gtkutil_get_number_response), spin);
|
|
|
|
|
2013-05-12 07:43:27 +02:00
|
|
|
gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox);
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
gtk_widget_show_all (dialog);
|
|
|
|
}
|
|
|
|
|
2013-12-31 23:06:58 +01:00
|
|
|
void
|
|
|
|
fe_get_bool (char *title, char *prompt, void *callback, void *userdata)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
GtkWidget *prompt_label;
|
|
|
|
extern GtkWidget *parent_window;
|
|
|
|
|
|
|
|
dialog = gtk_dialog_new_with_buttons (title, NULL, 0,
|
|
|
|
GTK_STOCK_NO, GTK_RESPONSE_REJECT,
|
|
|
|
GTK_STOCK_YES, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL);
|
|
|
|
gtk_box_set_homogeneous (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), TRUE);
|
|
|
|
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
|
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
|
|
|
|
|
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (dialog), "cb", callback);
|
|
|
|
g_object_set_data (G_OBJECT (dialog), "ud", userdata);
|
|
|
|
|
|
|
|
prompt_label = gtk_label_new (prompt);
|
|
|
|
|
|
|
|
g_signal_connect (G_OBJECT (dialog), "response",
|
|
|
|
G_CALLBACK (gtkutil_get_bool_response), NULL);
|
|
|
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), prompt_label);
|
|
|
|
|
|
|
|
gtk_widget_show_all (dialog);
|
|
|
|
}
|
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
GtkWidget *
|
|
|
|
gtkutil_button (GtkWidget *box, char *stock, char *tip, void *callback,
|
|
|
|
void *userdata, char *labeltext)
|
|
|
|
{
|
|
|
|
GtkWidget *wid, *img, *bbox;
|
|
|
|
|
|
|
|
wid = gtk_button_new ();
|
|
|
|
|
|
|
|
if (labeltext)
|
|
|
|
{
|
|
|
|
gtk_button_set_label (GTK_BUTTON (wid), labeltext);
|
|
|
|
gtk_button_set_image (GTK_BUTTON (wid), gtk_image_new_from_stock (stock, GTK_ICON_SIZE_MENU));
|
|
|
|
gtk_button_set_use_underline (GTK_BUTTON (wid), TRUE);
|
|
|
|
if (box)
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), wid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bbox = gtk_hbox_new (0, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (wid), bbox);
|
|
|
|
gtk_widget_show (bbox);
|
|
|
|
|
|
|
|
img = gtk_image_new_from_stock (stock, GTK_ICON_SIZE_MENU);
|
2012-09-02 18:08:25 +02:00
|
|
|
if (strcmp (stock, GTK_STOCK_GOTO_LAST) == 0)
|
|
|
|
{
|
2011-02-24 04:14:30 +01:00
|
|
|
gtk_widget_set_usize (img, 10, 6);
|
2012-09-02 18:08:25 +02:00
|
|
|
}
|
2011-02-24 04:14:30 +01:00
|
|
|
gtk_container_add (GTK_CONTAINER (bbox), img);
|
|
|
|
gtk_widget_show (img);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box), wid, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_signal_connect (G_OBJECT (wid), "clicked",
|
|
|
|
G_CALLBACK (callback), userdata);
|
|
|
|
gtk_widget_show (wid);
|
|
|
|
if (tip)
|
2014-01-18 10:08:32 +01:00
|
|
|
gtk_widget_set_tooltip_text (wid, tip);
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
return wid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtkutil_label_new (char *text, GtkWidget * box)
|
|
|
|
{
|
|
|
|
GtkWidget *label = gtk_label_new (text);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), label);
|
|
|
|
gtk_widget_show (label);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
gtkutil_entry_new (int max, GtkWidget * box, void *callback,
|
|
|
|
gpointer userdata)
|
|
|
|
{
|
|
|
|
GtkWidget *entry = gtk_entry_new_with_max_length (max);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), entry);
|
|
|
|
if (callback)
|
|
|
|
g_signal_connect (G_OBJECT (entry), "changed",
|
|
|
|
G_CALLBACK (callback), userdata);
|
|
|
|
gtk_widget_show (entry);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
gtkutil_clist_new (int columns, char *titles[],
|
|
|
|
GtkWidget * box, int policy,
|
|
|
|
void *select_callback, gpointer select_userdata,
|
|
|
|
void *unselect_callback,
|
|
|
|
gpointer unselect_userdata, int selection_mode)
|
|
|
|
{
|
|
|
|
GtkWidget *clist, *win;
|
|
|
|
|
|
|
|
win = gtk_scrolled_window_new (0, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), win);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (win),
|
|
|
|
GTK_POLICY_AUTOMATIC, policy);
|
|
|
|
gtk_widget_show (win);
|
|
|
|
|
|
|
|
if (titles)
|
|
|
|
clist = gtk_clist_new_with_titles (columns, titles);
|
|
|
|
else
|
|
|
|
clist = gtk_clist_new (columns);
|
|
|
|
|
|
|
|
gtk_clist_set_selection_mode (GTK_CLIST (clist), selection_mode);
|
|
|
|
gtk_clist_column_titles_passive (GTK_CLIST (clist));
|
|
|
|
gtk_container_add (GTK_CONTAINER (win), clist);
|
|
|
|
if (select_callback)
|
|
|
|
{
|
|
|
|
g_signal_connect (G_OBJECT (clist), "select_row",
|
|
|
|
G_CALLBACK (select_callback), select_userdata);
|
|
|
|
}
|
|
|
|
if (unselect_callback)
|
|
|
|
{
|
|
|
|
g_signal_connect (G_OBJECT (clist), "unselect_row",
|
|
|
|
G_CALLBACK (unselect_callback), unselect_userdata);
|
|
|
|
}
|
|
|
|
gtk_widget_show (clist);
|
|
|
|
|
|
|
|
return clist;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gtkutil_clist_selection (GtkWidget * clist)
|
|
|
|
{
|
|
|
|
if (GTK_CLIST (clist)->selection)
|
|
|
|
return GPOINTER_TO_INT(GTK_CLIST (clist)->selection->data);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
int_compare (const int * elem1, const int * elem2)
|
|
|
|
{
|
|
|
|
return (*elem1) - (*elem2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gtkutil_clist_multiple_selection (GtkWidget * clist, int ** rows, const int max_rows)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
GList *tmp_clist;
|
|
|
|
*rows = malloc (sizeof (int) * max_rows );
|
|
|
|
memset( *rows, -1, max_rows * sizeof(int) );
|
|
|
|
|
|
|
|
for( tmp_clist = GTK_CLIST(clist)->selection;
|
|
|
|
tmp_clist && i < max_rows; tmp_clist = tmp_clist->next, i++)
|
|
|
|
{
|
|
|
|
(*rows)[i] = GPOINTER_TO_INT( tmp_clist->data );
|
|
|
|
}
|
|
|
|
qsort(*rows, i, sizeof(int), (void *)int_compare);
|
|
|
|
return i;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
show_and_unfocus (GtkWidget * wid)
|
|
|
|
{
|
2013-05-12 07:43:27 +02:00
|
|
|
gtk_widget_set_can_focus (wid, FALSE);
|
2011-02-24 04:14:30 +01:00
|
|
|
gtk_widget_show (wid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtkutil_set_icon (GtkWidget *win)
|
|
|
|
{
|
2012-11-03 09:10:38 +01:00
|
|
|
#ifndef WIN32
|
|
|
|
/* FIXME: Magically breaks icon rendering in most
|
|
|
|
* (sub)windows, but OFC only on Windows. GTK <3
|
|
|
|
*/
|
2012-11-03 02:33:44 +01:00
|
|
|
gtk_window_set_icon (GTK_WINDOW (win), pix_hexchat);
|
2012-11-03 09:10:38 +01:00
|
|
|
#endif
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern GtkWidget *parent_window; /* maingui.c */
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
gtkutil_window_new (char *title, char *role, int width, int height, int flags)
|
|
|
|
{
|
|
|
|
GtkWidget *win;
|
|
|
|
|
|
|
|
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtkutil_set_icon (win);
|
|
|
|
#ifdef WIN32
|
2012-11-11 13:51:50 +01:00
|
|
|
gtk_window_set_wmclass (GTK_WINDOW (win), "HexChat", "hexchat");
|
2011-02-24 04:14:30 +01:00
|
|
|
#endif
|
|
|
|
gtk_window_set_title (GTK_WINDOW (win), title);
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (win), width, height);
|
|
|
|
gtk_window_set_role (GTK_WINDOW (win), role);
|
|
|
|
if (flags & 1)
|
|
|
|
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_MOUSE);
|
|
|
|
if ((flags & 2) && parent_window)
|
|
|
|
{
|
|
|
|
gtk_window_set_type_hint (GTK_WINDOW (win), GDK_WINDOW_TYPE_HINT_DIALOG);
|
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent_window));
|
2013-08-05 10:03:26 +02:00
|
|
|
gtk_window_set_destroy_with_parent (GTK_WINDOW (win), TRUE);
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pass NULL as selection to paste to both clipboard & X11 text */
|
|
|
|
void
|
|
|
|
gtkutil_copy_to_clipboard (GtkWidget *widget, GdkAtom selection,
|
|
|
|
const gchar *str)
|
|
|
|
{
|
|
|
|
GtkWidget *win;
|
|
|
|
GtkClipboard *clip, *clip2;
|
|
|
|
|
|
|
|
win = gtk_widget_get_toplevel (GTK_WIDGET (widget));
|
2013-05-12 07:43:27 +02:00
|
|
|
if (gtk_widget_is_toplevel (win))
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
|
|
|
int len = strlen (str);
|
|
|
|
|
|
|
|
if (selection)
|
|
|
|
{
|
|
|
|
clip = gtk_widget_get_clipboard (win, selection);
|
|
|
|
gtk_clipboard_set_text (clip, str, len);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/* copy to both primary X selection and clipboard */
|
|
|
|
clip = gtk_widget_get_clipboard (win, GDK_SELECTION_PRIMARY);
|
|
|
|
clip2 = gtk_widget_get_clipboard (win, GDK_SELECTION_CLIPBOARD);
|
|
|
|
gtk_clipboard_set_text (clip, str, len);
|
|
|
|
gtk_clipboard_set_text (clip2, str, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Treeview util functions */
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
gtkutil_treeview_new (GtkWidget *box, GtkTreeModel *model,
|
|
|
|
GtkTreeCellDataFunc mapper, ...)
|
|
|
|
{
|
|
|
|
GtkWidget *win, *view;
|
|
|
|
GtkCellRenderer *renderer = NULL;
|
|
|
|
GtkTreeViewColumn *col;
|
|
|
|
va_list args;
|
|
|
|
int col_id = 0;
|
|
|
|
GType type;
|
|
|
|
char *title, *attr;
|
|
|
|
|
|
|
|
win = gtk_scrolled_window_new (0, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), win);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (win),
|
|
|
|
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_widget_show (win);
|
|
|
|
|
|
|
|
view = gtk_tree_view_new_with_model (model);
|
|
|
|
/* the view now has a ref on the model, we can unref it */
|
|
|
|
g_object_unref (G_OBJECT (model));
|
|
|
|
gtk_container_add (GTK_CONTAINER (win), view);
|
|
|
|
|
|
|
|
va_start (args, mapper);
|
|
|
|
for (col_id = va_arg (args, int); col_id != -1; col_id = va_arg (args, int))
|
|
|
|
{
|
|
|
|
type = gtk_tree_model_get_column_type (model, col_id);
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case G_TYPE_BOOLEAN:
|
|
|
|
renderer = gtk_cell_renderer_toggle_new ();
|
|
|
|
attr = "active";
|
|
|
|
break;
|
|
|
|
case G_TYPE_STRING: /* fall through */
|
|
|
|
default:
|
|
|
|
renderer = gtk_cell_renderer_text_new ();
|
|
|
|
attr = "text";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
title = va_arg (args, char *);
|
|
|
|
if (mapper) /* user-specified function to set renderer attributes */
|
|
|
|
{
|
|
|
|
col = gtk_tree_view_column_new_with_attributes (title, renderer, NULL);
|
|
|
|
gtk_tree_view_column_set_cell_data_func (col, renderer, mapper,
|
|
|
|
GINT_TO_POINTER (col_id), NULL);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/* just set the typical attribute for this type of renderer */
|
|
|
|
col = gtk_tree_view_column_new_with_attributes (title, renderer,
|
|
|
|
attr, col_id, NULL);
|
|
|
|
}
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtkutil_treemodel_string_to_iter (GtkTreeModel *model, gchar *pathstr, GtkTreeIter *iter_ret)
|
|
|
|
{
|
|
|
|
GtkTreePath *path = gtk_tree_path_new_from_string (pathstr);
|
|
|
|
gboolean success;
|
|
|
|
|
|
|
|
success = gtk_tree_model_get_iter (model, iter_ret, path);
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*gboolean
|
|
|
|
gtkutil_treeview_get_selected_iter (GtkTreeView *view, GtkTreeIter *iter_ret)
|
|
|
|
{
|
|
|
|
GtkTreeModel *store;
|
|
|
|
GtkTreeSelection *select;
|
|
|
|
|
|
|
|
select = gtk_tree_view_get_selection (view);
|
|
|
|
return gtk_tree_selection_get_selected (select, &store, iter_ret);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtkutil_treeview_get_selected (GtkTreeView *view, GtkTreeIter *iter_ret, ...)
|
|
|
|
{
|
|
|
|
GtkTreeModel *store;
|
|
|
|
GtkTreeSelection *select;
|
|
|
|
gboolean has_selected;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
select = gtk_tree_view_get_selection (view);
|
|
|
|
has_selected = gtk_tree_selection_get_selected (select, &store, iter_ret);
|
|
|
|
|
|
|
|
if (has_selected) {
|
|
|
|
va_start (args, iter_ret);
|
|
|
|
gtk_tree_model_get_valist (store, iter_ret, args);
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_selected;
|
|
|
|
}
|
|
|
|
|
2014-01-31 23:20:17 +01:00
|
|
|
#if defined (WIN32) || defined (__APPLE__)
|
|
|
|
gboolean
|
|
|
|
gtkutil_find_font (const char *fontname)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n_families;
|
|
|
|
const char *family_name;
|
|
|
|
PangoFontMap *fontmap;
|
|
|
|
PangoFontFamily *family;
|
|
|
|
PangoFontFamily **families;
|
|
|
|
|
|
|
|
fontmap = pango_cairo_font_map_get_default ();
|
|
|
|
pango_font_map_list_families (fontmap, &families, &n_families);
|
|
|
|
|
|
|
|
for (i = 0; i < n_families; i++)
|
|
|
|
{
|
|
|
|
family = families[i];
|
|
|
|
family_name = pango_font_family_get_name (family);
|
|
|
|
|
|
|
|
if (!g_ascii_strcasecmp (family_name, fontname))
|
|
|
|
{
|
|
|
|
g_free (families);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (families);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|