2023-01-15 16:58:43 +00:00
|
|
|
/* Pickers and Launchers
|
2023-09-30 14:05:57 +00:00
|
|
|
* #Keywords: GtkColorDialog, GtkFontDialog, GtkFileDialog, GtkPrintDialog, GtkFileLauncher, GtkUriLauncher
|
2005-05-25 04:09:20 +00:00
|
|
|
*
|
2023-01-15 16:58:43 +00:00
|
|
|
* The dialogs are mainly intended for use in preference dialogs.
|
2023-09-30 14:05:57 +00:00
|
|
|
* They allow to select colors, fonts and files. There is also a
|
|
|
|
* print dialog.
|
2020-09-20 14:57:19 +00:00
|
|
|
*
|
2023-01-15 16:58:43 +00:00
|
|
|
* The launchers let you open files or URIs in applications that
|
|
|
|
* can handle them.
|
2005-05-25 04:09:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2022-11-29 18:04:56 +00:00
|
|
|
static GtkWidget *app_picker;
|
2023-04-05 16:00:16 +00:00
|
|
|
static GtkWidget *print_button;
|
2022-11-29 18:04:56 +00:00
|
|
|
|
2022-10-26 17:38:22 +00:00
|
|
|
static void
|
2023-02-23 19:44:52 +00:00
|
|
|
set_file (GFile *file,
|
|
|
|
gpointer data)
|
2020-09-20 14:57:19 +00:00
|
|
|
{
|
2023-04-05 16:00:16 +00:00
|
|
|
GFileInfo *info;
|
2022-10-26 17:38:22 +00:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
2022-11-29 18:04:56 +00:00
|
|
|
gtk_widget_set_sensitive (app_picker, FALSE);
|
|
|
|
g_object_set_data (G_OBJECT (app_picker), "file", NULL);
|
2022-10-26 17:38:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = g_file_get_basename (file);
|
2022-11-06 14:34:24 +00:00
|
|
|
gtk_label_set_label (GTK_LABEL (data), name);
|
2022-10-26 17:38:22 +00:00
|
|
|
g_free (name);
|
2022-11-29 18:04:56 +00:00
|
|
|
|
|
|
|
gtk_widget_set_sensitive (app_picker, TRUE);
|
|
|
|
g_object_set_data_full (G_OBJECT (app_picker), "file", g_object_ref (file), g_object_unref);
|
2023-04-05 16:00:16 +00:00
|
|
|
|
|
|
|
info = g_file_query_info (file, "standard::content-type", 0, NULL, NULL);
|
|
|
|
if (strcmp (g_file_info_get_content_type (info), "application/pdf") == 0)
|
|
|
|
{
|
|
|
|
gtk_widget_set_sensitive (print_button, TRUE);
|
|
|
|
g_object_set_data_full (G_OBJECT (print_button), "file", g_object_ref (file), g_object_unref);
|
|
|
|
}
|
2022-10-26 17:38:22 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 19:44:52 +00:00
|
|
|
static void
|
|
|
|
file_opened (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
GFile *file;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
file = gtk_file_dialog_open_finish (GTK_FILE_DIALOG (source), result, &error);
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
g_print ("%s\n", error->message);
|
|
|
|
g_error_free (error);
|
2023-04-05 16:00:16 +00:00
|
|
|
gtk_widget_set_sensitive (app_picker, FALSE);
|
|
|
|
g_object_set_data (G_OBJECT (app_picker), "file", NULL);
|
|
|
|
gtk_widget_set_sensitive (print_button, FALSE);
|
|
|
|
g_object_set_data (G_OBJECT (print_button), "file", NULL);
|
2023-02-23 19:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_file (file, data);
|
|
|
|
}
|
|
|
|
|
2022-11-24 15:26:43 +00:00
|
|
|
static gboolean
|
|
|
|
abort_mission (gpointer data)
|
|
|
|
{
|
|
|
|
GCancellable *cancellable = data;
|
|
|
|
|
|
|
|
g_cancellable_cancel (cancellable);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2022-10-26 17:38:22 +00:00
|
|
|
static void
|
2022-11-06 14:34:24 +00:00
|
|
|
open_file (GtkButton *picker,
|
|
|
|
GtkLabel *label)
|
2022-10-26 17:38:22 +00:00
|
|
|
{
|
|
|
|
GtkWindow *parent = GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (picker)));
|
|
|
|
GtkFileDialog *dialog;
|
2022-11-24 15:26:43 +00:00
|
|
|
GCancellable *cancellable;
|
2022-10-26 17:38:22 +00:00
|
|
|
|
|
|
|
dialog = gtk_file_dialog_new ();
|
|
|
|
|
2022-11-24 15:26:43 +00:00
|
|
|
cancellable = g_cancellable_new ();
|
|
|
|
|
|
|
|
g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
|
|
|
|
20,
|
|
|
|
abort_mission, g_object_ref (cancellable), g_object_unref);
|
|
|
|
|
2022-12-23 04:54:32 +00:00
|
|
|
gtk_file_dialog_open (dialog, parent, cancellable, file_opened, label);
|
2022-10-26 17:38:22 +00:00
|
|
|
|
2022-11-24 15:26:43 +00:00
|
|
|
g_object_unref (cancellable);
|
2022-10-26 17:38:22 +00:00
|
|
|
g_object_unref (dialog);
|
2020-09-20 14:57:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 18:04:56 +00:00
|
|
|
static void
|
2023-01-15 16:58:43 +00:00
|
|
|
open_app_done (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer data)
|
2022-11-29 18:04:56 +00:00
|
|
|
{
|
|
|
|
GtkFileLauncher *launcher = GTK_FILE_LAUNCHER (source);
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (!gtk_file_launcher_launch_finish (launcher, result, &error))
|
|
|
|
{
|
|
|
|
g_print ("%s\n", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_app (GtkButton *picker)
|
|
|
|
{
|
|
|
|
GtkWindow *parent = GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (picker)));
|
|
|
|
GtkFileLauncher *launcher;
|
|
|
|
GFile *file;
|
|
|
|
|
|
|
|
file = G_FILE (g_object_get_data (G_OBJECT (picker), "file"));
|
2022-12-13 12:49:10 +00:00
|
|
|
launcher = gtk_file_launcher_new (file);
|
2022-11-29 18:04:56 +00:00
|
|
|
|
2023-01-15 16:58:43 +00:00
|
|
|
gtk_file_launcher_launch (launcher, parent, NULL, open_app_done, NULL);
|
|
|
|
|
|
|
|
g_object_unref (launcher);
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:00:16 +00:00
|
|
|
static void
|
|
|
|
print_file_done (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkPrintDialog *dialog = GTK_PRINT_DIALOG (source);
|
|
|
|
GError *error = NULL;
|
|
|
|
GCancellable *cancellable;
|
|
|
|
unsigned int id;
|
|
|
|
|
|
|
|
cancellable = g_task_get_cancellable (G_TASK (result));
|
|
|
|
id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (cancellable), "timeout"));
|
|
|
|
if (id)
|
|
|
|
g_source_remove (id);
|
|
|
|
|
|
|
|
if (!gtk_print_dialog_print_file_finish (dialog, result, &error))
|
|
|
|
{
|
|
|
|
g_print ("%s\n", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_file (GtkButton *picker)
|
|
|
|
{
|
|
|
|
GtkWindow *parent = GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (picker)));
|
|
|
|
GtkPrintDialog *dialog;
|
|
|
|
GCancellable *cancellable;
|
|
|
|
GFile *file;
|
|
|
|
unsigned int id;
|
|
|
|
|
|
|
|
file = G_FILE (g_object_get_data (G_OBJECT (picker), "file"));
|
|
|
|
dialog = gtk_print_dialog_new ();
|
|
|
|
|
|
|
|
cancellable = g_cancellable_new ();
|
|
|
|
|
|
|
|
id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
|
|
|
|
20,
|
|
|
|
abort_mission, g_object_ref (cancellable), g_object_unref);
|
|
|
|
g_object_set_data (G_OBJECT (cancellable), "timeout", GUINT_TO_POINTER (id));
|
|
|
|
|
2023-09-30 02:01:32 +00:00
|
|
|
gtk_print_dialog_print_file (dialog, parent, NULL, file, cancellable, print_file_done, NULL);
|
2023-04-05 16:00:16 +00:00
|
|
|
|
|
|
|
g_object_unref (cancellable);
|
|
|
|
g_object_unref (dialog);
|
|
|
|
}
|
|
|
|
|
2023-01-15 16:58:43 +00:00
|
|
|
static void
|
|
|
|
open_uri_done (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkUriLauncher *launcher = GTK_URI_LAUNCHER (source);
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (!gtk_uri_launcher_launch_finish (launcher, result, &error))
|
|
|
|
{
|
|
|
|
g_print ("%s\n", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
launch_uri (GtkButton *picker)
|
|
|
|
{
|
|
|
|
GtkWindow *parent = GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (picker)));
|
|
|
|
GtkUriLauncher *launcher;
|
|
|
|
|
|
|
|
launcher = gtk_uri_launcher_new ("http://www.gtk.org");
|
|
|
|
|
|
|
|
gtk_uri_launcher_launch (launcher, parent, NULL, open_uri_done, NULL);
|
2022-11-29 18:04:56 +00:00
|
|
|
|
|
|
|
g_object_unref (launcher);
|
|
|
|
}
|
|
|
|
|
2023-02-23 19:44:52 +00:00
|
|
|
static gboolean
|
|
|
|
on_drop (GtkDropTarget *target,
|
|
|
|
const GValue *value,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
if (G_VALUE_HOLDS (value, G_TYPE_FILE))
|
|
|
|
{
|
|
|
|
set_file (g_value_get_object (value), data);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-05-25 04:09:20 +00:00
|
|
|
GtkWidget *
|
|
|
|
do_pickers (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
static GtkWidget *window = NULL;
|
2022-11-06 14:34:24 +00:00
|
|
|
GtkWidget *table, *label, *picker, *button;
|
2023-02-23 19:44:52 +00:00
|
|
|
GtkDropTarget *drop_target;
|
2005-05-25 04:09:20 +00:00
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2023-01-15 16:58:43 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Pickers and Launchers");
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2011-06-02 01:44:22 +00:00
|
|
|
|
2011-08-31 15:10:19 +00:00
|
|
|
table = gtk_grid_new ();
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (table, 20);
|
|
|
|
gtk_widget_set_margin_end (table, 20);
|
|
|
|
gtk_widget_set_margin_top (table, 20);
|
|
|
|
gtk_widget_set_margin_bottom (table, 20);
|
2023-01-15 16:58:43 +00:00
|
|
|
gtk_grid_set_row_spacing (GTK_GRID (table), 6);
|
|
|
|
gtk_grid_set_column_spacing (GTK_GRID (table), 6);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), table);
|
2005-05-25 04:09:20 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("Color:");
|
2011-06-12 23:39:48 +00:00
|
|
|
gtk_widget_set_halign (label, GTK_ALIGN_START);
|
|
|
|
gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
|
2011-08-31 15:10:19 +00:00
|
|
|
gtk_widget_set_hexpand (label, TRUE);
|
2020-09-20 14:57:19 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (table), label, 0, 0, 1, 1);
|
|
|
|
|
2022-10-26 17:38:22 +00:00
|
|
|
picker = gtk_color_dialog_button_new (gtk_color_dialog_new ());
|
2023-06-11 11:04:56 +00:00
|
|
|
gtk_label_set_mnemonic_widget (GTK_LABEL (label), picker);
|
2020-09-20 14:57:19 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (table), picker, 1, 0, 1, 1);
|
|
|
|
|
2005-05-25 04:09:20 +00:00
|
|
|
label = gtk_label_new ("Font:");
|
2011-06-12 23:39:48 +00:00
|
|
|
gtk_widget_set_halign (label, GTK_ALIGN_START);
|
|
|
|
gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
|
2011-08-31 15:10:19 +00:00
|
|
|
gtk_widget_set_hexpand (label, TRUE);
|
|
|
|
gtk_grid_attach (GTK_GRID (table), label, 0, 1, 1, 1);
|
2020-09-20 14:57:19 +00:00
|
|
|
|
2022-10-26 17:38:22 +00:00
|
|
|
picker = gtk_font_dialog_button_new (gtk_font_dialog_new ());
|
2023-06-11 11:04:56 +00:00
|
|
|
gtk_label_set_mnemonic_widget (GTK_LABEL (label), picker);
|
2011-08-31 15:10:19 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (table), picker, 1, 1, 1, 1);
|
2005-05-25 04:09:20 +00:00
|
|
|
|
2022-10-26 17:38:22 +00:00
|
|
|
label = gtk_label_new ("File:");
|
|
|
|
gtk_widget_set_halign (label, GTK_ALIGN_START);
|
|
|
|
gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
|
|
|
|
gtk_widget_set_hexpand (label, TRUE);
|
|
|
|
gtk_grid_attach (GTK_GRID (table), label, 0, 2, 1, 1);
|
2020-09-20 14:57:19 +00:00
|
|
|
|
2023-01-15 16:58:43 +00:00
|
|
|
picker = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
|
2022-11-06 14:34:24 +00:00
|
|
|
button = gtk_button_new_from_icon_name ("document-open-symbolic");
|
2023-06-11 11:04:56 +00:00
|
|
|
gtk_accessible_update_property (GTK_ACCESSIBLE (button),
|
|
|
|
GTK_ACCESSIBLE_PROPERTY_LABEL, "Select File",
|
|
|
|
-1);
|
2023-02-23 19:44:52 +00:00
|
|
|
|
2022-11-06 14:34:24 +00:00
|
|
|
label = gtk_label_new ("None");
|
2023-02-23 19:44:52 +00:00
|
|
|
|
|
|
|
drop_target = gtk_drop_target_new (G_TYPE_FILE, GDK_ACTION_COPY);
|
|
|
|
g_signal_connect (drop_target, "drop", G_CALLBACK (on_drop), label);
|
|
|
|
gtk_widget_add_controller (button, GTK_EVENT_CONTROLLER (drop_target));
|
|
|
|
|
2022-11-06 14:34:24 +00:00
|
|
|
gtk_label_set_xalign (GTK_LABEL (label), 0.);
|
|
|
|
gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_MIDDLE);
|
|
|
|
gtk_widget_set_hexpand (label, TRUE);
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (open_file), label);
|
|
|
|
gtk_box_append (GTK_BOX (picker), label);
|
|
|
|
gtk_box_append (GTK_BOX (picker), button);
|
2023-01-15 16:58:43 +00:00
|
|
|
app_picker = gtk_button_new_from_icon_name ("emblem-system-symbolic");
|
|
|
|
gtk_widget_set_halign (app_picker, GTK_ALIGN_END);
|
2023-06-11 11:04:56 +00:00
|
|
|
gtk_accessible_update_property (GTK_ACCESSIBLE (app_picker),
|
|
|
|
GTK_ACCESSIBLE_PROPERTY_LABEL, "Open File",
|
|
|
|
-1);
|
2023-01-15 16:58:43 +00:00
|
|
|
gtk_widget_set_sensitive (app_picker, FALSE);
|
|
|
|
g_signal_connect (app_picker, "clicked", G_CALLBACK (open_app), NULL);
|
|
|
|
gtk_box_append (GTK_BOX (picker), app_picker);
|
2022-10-26 17:38:22 +00:00
|
|
|
|
2023-04-05 16:00:16 +00:00
|
|
|
print_button = gtk_button_new_from_icon_name ("printer-symbolic");
|
|
|
|
gtk_widget_set_tooltip_text (print_button, "Print file");
|
|
|
|
gtk_widget_set_sensitive (print_button, FALSE);
|
|
|
|
g_signal_connect (print_button, "clicked", G_CALLBACK (print_file), NULL);
|
|
|
|
gtk_box_append (GTK_BOX (picker), print_button);
|
|
|
|
|
|
|
|
gtk_grid_attach (GTK_GRID (table), picker, 1, 2, 1, 1);
|
2023-01-15 16:58:43 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("URI:");
|
2011-06-12 23:39:48 +00:00
|
|
|
gtk_widget_set_halign (label, GTK_ALIGN_START);
|
|
|
|
gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
|
2011-08-31 15:10:19 +00:00
|
|
|
gtk_widget_set_hexpand (label, TRUE);
|
2023-01-15 16:58:43 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (table), label, 0, 3, 1, 1);
|
2022-10-04 01:54:57 +00:00
|
|
|
|
2023-01-15 16:58:43 +00:00
|
|
|
picker = gtk_button_new_with_label ("www.gtk.org");
|
|
|
|
g_signal_connect (picker, "clicked", G_CALLBACK (launch_uri), NULL);
|
|
|
|
gtk_grid_attach (GTK_GRID (table), picker, 1, 3, 1, 1);
|
2005-05-25 04:09:20 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2023-06-17 15:37:11 +00:00
|
|
|
gtk_window_present (GTK_WINDOW (window));
|
2005-05-25 04:09:20 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2005-05-25 04:09:20 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|