PIKApp/libpikawidgets/pikadialog.c

817 lines
24 KiB
C
Raw Permalink Normal View History

2023-09-26 00:35:21 +02:00
/* LIBPIKA - The PIKA Library
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* pikadialog.c
* Copyright (C) 2000-2003 Michael Natterer <mitch@gimp.org>
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
2023-10-30 23:55:30 +01:00
#include <gegl.h>
2023-09-26 00:35:21 +02:00
#include <gtk/gtk.h>
#include "libpikabase/pikabase.h"
#include "pikawidgetstypes.h"
#include "pikadialog.h"
#include "pikahelpui.h"
2023-10-30 23:55:30 +01:00
#include "pikawidgetsutils.h"
2023-09-26 00:35:21 +02:00
#include "libpika/libpika-intl.h"
2023-10-30 23:55:30 +01:00
#ifdef G_OS_WIN32
#include <dwmapi.h>
#include <gdk/gdkwin32.h>
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
#endif
2023-09-26 00:35:21 +02:00
/**
* SECTION: pikadialog
* @title: PikaDialog
* @short_description: Constructors for #GtkDialog's and action_areas as
* well as other dialog-related stuff.
*
* Constructors for #GtkDialog's and action_areas as well as other
* dialog-related stuff.
**/
enum
{
PROP_0,
PROP_HELP_FUNC,
PROP_HELP_ID,
PROP_PARENT
};
struct _PikaDialogPrivate
{
PikaHelpFunc help_func;
gchar *help_id;
GtkWidget *help_button;
2023-10-30 23:55:30 +01:00
GBytes *window_handle;
2023-09-26 00:35:21 +02:00
};
#define GET_PRIVATE(obj) (((PikaDialog *) (obj))->priv)
2023-10-30 23:55:30 +01:00
static void pika_dialog_constructed (GObject *object);
static void pika_dialog_dispose (GObject *object);
static void pika_dialog_finalize (GObject *object);
static void pika_dialog_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void pika_dialog_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
2023-09-26 00:35:21 +02:00
2023-10-30 23:55:30 +01:00
static void pika_dialog_hide (GtkWidget *widget);
static gboolean pika_dialog_delete_event (GtkWidget *widget,
GdkEventAny *event);
2023-09-26 00:35:21 +02:00
2023-10-30 23:55:30 +01:00
static void pika_dialog_close (GtkDialog *dialog);
2023-09-26 00:35:21 +02:00
2023-10-30 23:55:30 +01:00
static void pika_dialog_response (GtkDialog *dialog,
gint response_id);
2023-09-26 00:35:21 +02:00
2023-12-02 20:03:24 +01:00
#ifdef G_OS_WIN32
2023-10-30 23:55:30 +01:00
static void pika_dialog_set_title_bar_theme (GtkWidget *dialog);
2023-12-02 20:03:24 +01:00
#endif
2023-09-26 00:35:21 +02:00
G_DEFINE_TYPE_WITH_PRIVATE (PikaDialog, pika_dialog, GTK_TYPE_DIALOG)
#define parent_class pika_dialog_parent_class
static gboolean show_help_button = TRUE;
static void
pika_dialog_class_init (PikaDialogClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
object_class->constructed = pika_dialog_constructed;
object_class->dispose = pika_dialog_dispose;
object_class->finalize = pika_dialog_finalize;
object_class->set_property = pika_dialog_set_property;
object_class->get_property = pika_dialog_get_property;
widget_class->hide = pika_dialog_hide;
widget_class->delete_event = pika_dialog_delete_event;
dialog_class->close = pika_dialog_close;
/**
* PikaDialog:help-func:
*
* Since: 2.2
**/
g_object_class_install_property (object_class, PROP_HELP_FUNC,
g_param_spec_pointer ("help-func",
"Help Func",
"The help function to call when F1 is hit",
PIKA_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
/**
* PikaDialog:help-id:
*
* Since: 2.2
**/
g_object_class_install_property (object_class, PROP_HELP_ID,
g_param_spec_string ("help-id",
"Help ID",
"The help ID to pass to help-func",
NULL,
PIKA_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
/**
* PikaDialog:parent:
*
* Since: 2.8
**/
g_object_class_install_property (object_class, PROP_PARENT,
g_param_spec_object ("parent",
"Parent",
"The dialog's parent widget",
GTK_TYPE_WIDGET,
PIKA_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
}
static void
pika_dialog_init (PikaDialog *dialog)
{
dialog->priv = pika_dialog_get_instance_private (dialog);
g_signal_connect (dialog, "response",
G_CALLBACK (pika_dialog_response),
NULL);
2023-10-30 23:55:30 +01:00
#ifdef G_OS_WIN32
g_signal_connect (GTK_WIDGET (dialog), "map",
G_CALLBACK (pika_dialog_set_title_bar_theme),
NULL);
#endif
2023-09-26 00:35:21 +02:00
}
static void
pika_dialog_constructed (GObject *object)
{
PikaDialogPrivate *private = GET_PRIVATE (object);
G_OBJECT_CLASS (parent_class)->constructed (object);
if (private->help_func)
pika_help_connect (GTK_WIDGET (object),
private->help_func, private->help_id,
object, NULL);
if (show_help_button && private->help_func && private->help_id)
{
private->help_button = gtk_dialog_add_button (GTK_DIALOG (object),
_("_Help"),
GTK_RESPONSE_HELP);
}
2023-10-30 23:55:30 +01:00
pika_widget_set_native_handle (GTK_WIDGET (object), &private->window_handle);
2023-09-26 00:35:21 +02:00
}
static void
pika_dialog_dispose (GObject *object)
{
GdkDisplay *display = NULL;
if (g_main_depth () == 0)
{
display = gtk_widget_get_display (GTK_WIDGET (object));
g_object_ref (display);
}
G_OBJECT_CLASS (parent_class)->dispose (object);
if (display)
{
gdk_display_flush (display);
g_object_unref (display);
}
}
static void
pika_dialog_finalize (GObject *object)
{
PikaDialogPrivate *private = GET_PRIVATE (object);
g_clear_pointer (&private->help_id, g_free);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
pika_dialog_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
PikaDialogPrivate *private = GET_PRIVATE (object);
switch (property_id)
{
case PROP_HELP_FUNC:
private->help_func = g_value_get_pointer (value);
break;
case PROP_HELP_ID:
g_free (private->help_id);
private->help_id = g_value_dup_string (value);
pika_help_set_help_data (GTK_WIDGET (object), NULL, private->help_id);
break;
case PROP_PARENT:
{
GtkWidget *parent = g_value_get_object (value);
if (parent)
{
if (GTK_IS_WINDOW (parent))
{
gtk_window_set_transient_for (GTK_WINDOW (object),
GTK_WINDOW (parent));
}
else
{
GtkWidget *toplevel;
toplevel = gtk_widget_get_toplevel (parent);
if (GTK_IS_WINDOW (toplevel))
{
gtk_window_set_transient_for (GTK_WINDOW (object),
GTK_WINDOW (toplevel));
}
else
{
gtk_window_set_screen (GTK_WINDOW (object),
gtk_widget_get_screen (parent));
gtk_window_set_position (GTK_WINDOW (object),
GTK_WIN_POS_MOUSE);
}
}
}
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pika_dialog_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
PikaDialogPrivate *private = GET_PRIVATE (object);
switch (property_id)
{
case PROP_HELP_FUNC:
g_value_set_pointer (value, private->help_func);
break;
case PROP_HELP_ID:
g_value_set_string (value, private->help_id);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pika_dialog_hide (GtkWidget *widget)
{
/* set focus to NULL so focus_out callbacks are invoked synchronously */
gtk_window_set_focus (GTK_WINDOW (widget), NULL);
GTK_WIDGET_CLASS (parent_class)->hide (widget);
}
static gboolean
pika_dialog_delete_event (GtkWidget *widget,
GdkEventAny *event)
{
return TRUE;
}
static void
pika_dialog_close (GtkDialog *dialog)
{
/* Synthesize delete_event to close dialog. */
GtkWidget *widget = GTK_WIDGET (dialog);
if (gtk_widget_get_window (widget))
{
GdkEvent *event = gdk_event_new (GDK_DELETE);
event->any.window = g_object_ref (gtk_widget_get_window (widget));
event->any.send_event = TRUE;
gtk_main_do_event (event);
gdk_event_free (event);
}
}
static void
pika_dialog_response (GtkDialog *dialog,
gint response_id)
{
PikaDialogPrivate *private = GET_PRIVATE (dialog);
GtkWidget *widget = gtk_dialog_get_widget_for_response (dialog,
response_id);
if (widget &&
(! GTK_IS_BUTTON (widget) ||
gtk_widget_get_focus_on_click (widget)))
{
gtk_widget_grab_focus (widget);
}
/* if our own help button was activated, abort "response" and
* call our help callback.
*/
if (response_id == GTK_RESPONSE_HELP &&
widget == private->help_button)
{
g_signal_stop_emission_by_name (dialog, "response");
if (private->help_func)
private->help_func (private->help_id, dialog);
}
}
/**
* pika_dialog_new: (skip)
* @title: The dialog's title which will be set with
* gtk_window_set_title().
* @role: The dialog's @role which will be set with
* gtk_window_set_role().
* @parent: (nullable): The @parent widget of this dialog.
* @flags: The @flags (see the #GtkDialog documentation).
* @help_func: The function which will be called if the user presses "F1".
* @help_id: The help_id which will be passed to @help_func.
* @...: A %NULL-terminated @va_list destribing the
* action_area buttons.
*
* Creates a new @PikaDialog widget.
*
* This function simply packs the action_area arguments passed in "..."
* into a @va_list variable and passes everything to pika_dialog_new_valist().
*
* For a description of the format of the @va_list describing the
* action_area buttons see gtk_dialog_new_with_buttons().
*
* Returns: A #PikaDialog.
**/
GtkWidget *
pika_dialog_new (const gchar *title,
const gchar *role,
GtkWidget *parent,
GtkDialogFlags flags,
PikaHelpFunc help_func,
const gchar *help_id,
...)
{
GtkWidget *dialog;
va_list args;
g_return_val_if_fail (parent == NULL || GTK_IS_WIDGET (parent), NULL);
g_return_val_if_fail (title != NULL, NULL);
g_return_val_if_fail (role != NULL, NULL);
va_start (args, help_id);
dialog = pika_dialog_new_valist (title, role,
parent, flags,
help_func, help_id,
args);
va_end (args);
return dialog;
}
/**
* pika_dialog_new_valist: (skip)
* @title: The dialog's title which will be set with
* gtk_window_set_title().
* @role: The dialog's @role which will be set with
* gtk_window_set_role().
* @parent: The @parent widget of this dialog or %NULL.
* @flags: The @flags (see the #GtkDialog documentation).
* @help_func: The function which will be called if the user presses "F1".
* @help_id: The help_id which will be passed to @help_func.
* @args: A @va_list destribing the action_area buttons.
*
* Creates a new @PikaDialog widget. If a GtkWindow is specified as
* @parent then the dialog will be made transient for this window.
*
* For a description of the format of the @va_list describing the
* action_area buttons see gtk_dialog_new_with_buttons().
*
* Returns: A #PikaDialog.
**/
GtkWidget *
pika_dialog_new_valist (const gchar *title,
const gchar *role,
GtkWidget *parent,
GtkDialogFlags flags,
PikaHelpFunc help_func,
const gchar *help_id,
va_list args)
{
GtkWidget *dialog;
gboolean use_header_bar;
g_return_val_if_fail (title != NULL, NULL);
g_return_val_if_fail (role != NULL, NULL);
g_return_val_if_fail (parent == NULL || GTK_IS_WIDGET (parent), NULL);
g_object_get (gtk_settings_get_default (),
"gtk-dialogs-use-header", &use_header_bar,
NULL);
dialog = g_object_new (PIKA_TYPE_DIALOG,
"title", title,
"role", role,
"modal", (flags & GTK_DIALOG_MODAL),
"help-func", help_func,
"help-id", help_id,
"parent", parent,
"use-header-bar", use_header_bar,
NULL);
if (parent)
{
if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
g_signal_connect_object (parent, "destroy",
G_CALLBACK (pika_dialog_close),
dialog, G_CONNECT_SWAPPED);
}
pika_dialog_add_buttons_valist (PIKA_DIALOG (dialog), args);
return dialog;
}
/**
* pika_dialog_add_button:
* @dialog: The @dialog to add a button to.
* @button_text: text of button, or stock ID.
* @response_id: response ID for the button.
*
* This function is essentially the same as gtk_dialog_add_button()
* except it ensures there is only one help button and automatically
* sets the RESPONSE_OK widget as the default response.
*
* Returns: (type Gtk.Widget) (transfer none): the button widget that was added.
**/
GtkWidget *
pika_dialog_add_button (PikaDialog *dialog,
const gchar *button_text,
gint response_id)
{
GtkWidget *button;
gboolean use_header_bar;
/* hide the automatically added help button if another one is added */
if (response_id == GTK_RESPONSE_HELP)
{
PikaDialogPrivate *private = GET_PRIVATE (dialog);
if (private->help_button)
{
gtk_widget_destroy (private->help_button);
private->help_button = NULL;
}
}
button = gtk_dialog_add_button (GTK_DIALOG (dialog), button_text,
response_id);
g_object_get (dialog,
"use-header-bar", &use_header_bar,
NULL);
if (use_header_bar &&
(response_id == GTK_RESPONSE_OK ||
response_id == GTK_RESPONSE_CANCEL ||
response_id == GTK_RESPONSE_CLOSE))
{
GtkWidget *header = gtk_dialog_get_header_bar (GTK_DIALOG (dialog));
if (response_id == GTK_RESPONSE_OK)
gtk_dialog_set_default_response (GTK_DIALOG (dialog),
GTK_RESPONSE_OK);
gtk_container_child_set (GTK_CONTAINER (header), button,
"position", 0,
NULL);
}
return button;
}
/**
* pika_dialog_add_buttons: (skip)
* @dialog: The @dialog to add buttons to.
* @...: button_text-response_id pairs.
*
* This function is essentially the same as gtk_dialog_add_buttons()
* except it calls pika_dialog_add_button() instead of gtk_dialog_add_button()
**/
void
pika_dialog_add_buttons (PikaDialog *dialog,
...)
{
va_list args;
va_start (args, dialog);
pika_dialog_add_buttons_valist (dialog, args);
va_end (args);
}
/**
* pika_dialog_add_buttons_valist: (skip)
* @dialog: The @dialog to add buttons to.
* @args: The buttons as va_list.
*
* This function is essentially the same as pika_dialog_add_buttons()
* except it takes a va_list instead of '...'
**/
void
pika_dialog_add_buttons_valist (PikaDialog *dialog,
va_list args)
{
const gchar *button_text;
gint response_id;
g_return_if_fail (PIKA_IS_DIALOG (dialog));
while ((button_text = va_arg (args, const gchar *)))
{
response_id = va_arg (args, gint);
pika_dialog_add_button (dialog, button_text, response_id);
}
}
typedef struct
{
GtkDialog *dialog;
gint response_id;
GMainLoop *loop;
gboolean destroyed;
} RunInfo;
static void
run_shutdown_loop (RunInfo *ri)
{
if (g_main_loop_is_running (ri->loop))
g_main_loop_quit (ri->loop);
}
static void
run_unmap_handler (GtkDialog *dialog,
RunInfo *ri)
{
run_shutdown_loop (ri);
}
static void
run_response_handler (GtkDialog *dialog,
gint response_id,
RunInfo *ri)
{
ri->response_id = response_id;
run_shutdown_loop (ri);
}
static gint
run_delete_handler (GtkDialog *dialog,
GdkEventAny *event,
RunInfo *ri)
{
run_shutdown_loop (ri);
return TRUE; /* Do not destroy */
}
static void
run_destroy_handler (GtkDialog *dialog,
RunInfo *ri)
{
/* shutdown_loop will be called by run_unmap_handler */
ri->destroyed = TRUE;
}
/**
* pika_dialog_run:
* @dialog: a #PikaDialog
*
* This function does exactly the same as gtk_dialog_run() except it
* does not make the dialog modal while the #GMainLoop is running.
*
* Returns: response ID
**/
gint
pika_dialog_run (PikaDialog *dialog)
{
RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL };
gulong response_handler;
gulong unmap_handler;
gulong destroy_handler;
gulong delete_handler;
g_return_val_if_fail (PIKA_IS_DIALOG (dialog), -1);
g_object_ref (dialog);
gtk_window_present (GTK_WINDOW (dialog));
2023-10-30 23:55:30 +01:00
#ifdef G_OS_WIN32
pika_dialog_set_title_bar_theme (GTK_WIDGET (dialog));
#endif
2023-09-26 00:35:21 +02:00
response_handler = g_signal_connect (dialog, "response",
G_CALLBACK (run_response_handler),
&ri);
unmap_handler = g_signal_connect (dialog, "unmap",
G_CALLBACK (run_unmap_handler),
&ri);
delete_handler = g_signal_connect (dialog, "delete-event",
G_CALLBACK (run_delete_handler),
&ri);
destroy_handler = g_signal_connect (dialog, "destroy",
G_CALLBACK (run_destroy_handler),
&ri);
ri.loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (ri.loop);
g_main_loop_unref (ri.loop);
ri.loop = NULL;
ri.destroyed = FALSE;
if (!ri.destroyed)
{
g_signal_handler_disconnect (dialog, response_handler);
g_signal_handler_disconnect (dialog, unmap_handler);
g_signal_handler_disconnect (dialog, delete_handler);
g_signal_handler_disconnect (dialog, destroy_handler);
}
g_object_unref (dialog);
return ri.response_id;
}
/**
* pika_dialog_set_alternative_button_order_from_array:
* @dialog: The #PikaDialog
* @n_buttons: The size of @order
* @order: (array length=n_buttons): array of buttons' response ids.
*
* Reorder @dialog's buttons if [property@Gtk.Settings:gtk-alternative-button-order]
* is set to TRUE. This is mostly a wrapper around the GTK function
* [method@Gtk.Dialog.set_alternative_button_order], except it won't
* output a deprecation warning.
*
* Since: 3.0
**/
void
pika_dialog_set_alternative_button_order_from_array (PikaDialog *dialog,
gint n_buttons,
gint *order)
{
/* since we don't know yet what to do about alternative button order,
* just hide the warnings for now...
*/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
gtk_dialog_set_alternative_button_order_from_array (GTK_DIALOG (dialog), n_buttons, order);
G_GNUC_END_IGNORE_DEPRECATIONS;
}
2023-10-30 23:55:30 +01:00
/**
* pika_dialog_get_native_handle:
* @dialog: The #PikaDialog
*
* Returns an opaque data handle representing the window in the currently
* running platform. You should not try to use this directly. Usually this is to
* be used in functions such as [func@Gimp.brushes_popup] which will allow the
* core process to set this [class@Dialog] as parent to the newly created popup.
*
* Returns: (transfer none): an opaque [struct@GLib.Bytes] identifying this
* window.
*
* Since: 3.0
**/
GBytes *
pika_dialog_get_native_handle (PikaDialog *dialog)
{
return dialog->priv->window_handle;
}
2023-09-26 00:35:21 +02:00
/**
* pika_dialogs_show_help_button: (skip)
* @show: whether a help button should be added when creating a PikaDialog
*
* This function is for internal use only.
*
* Since: 2.2
**/
void
pika_dialogs_show_help_button (gboolean show)
{
show_help_button = show ? TRUE : FALSE;
}
2023-10-30 23:55:30 +01:00
2023-12-02 20:03:24 +01:00
#ifdef G_OS_WIN32
2023-10-30 23:55:30 +01:00
void
pika_dialog_set_title_bar_theme (GtkWidget *dialog)
{
HWND hwnd;
gboolean use_dark_mode = FALSE;
GdkWindow *window = NULL;
window = gtk_widget_get_window (GTK_WIDGET (dialog));
if (window)
{
GtkStyleContext *style;
GdkRGBA *color = NULL;
hwnd = (HWND) gdk_win32_window_get_handle (window);
/* Workaround since we don't have access to PikaGuiConfig.
* If the background color is below the threshold, then we're
* likely in dark mode.
*/
style = gtk_widget_get_style_context (GTK_WIDGET (dialog));
gtk_style_context_get (style, gtk_style_context_get_state (style),
GTK_STYLE_PROPERTY_BACKGROUND_COLOR, &color,
NULL);
if (color)
{
if (color->red < 0.5 && color->green < 0.5 && color->blue < 0.5)
use_dark_mode = TRUE;
gdk_rgba_free (color);
}
DwmSetWindowAttribute (hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
&use_dark_mode, sizeof (use_dark_mode));
UpdateWindow (hwnd);
ShowWindow (hwnd, 5);
/* Toggle the window's visibility so the title bar change appears */
gdk_window_hide (window);
gdk_window_show (window);
}
}
2023-12-02 20:03:24 +01:00
#endif