PIKApp/app/dialogs/layer-add-mask-dialog.c

253 lines
8.8 KiB
C

/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libpikawidgets/pikawidgets.h"
#include "dialogs-types.h"
#include "core/pika.h"
#include "core/pikachannel.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "widgets/pikacontainercombobox.h"
#include "widgets/pikacontainerview.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikaviewabledialog.h"
#include "widgets/pikawidgets-utils.h"
#include "layer-add-mask-dialog.h"
#include "pika-intl.h"
typedef struct _LayerAddMaskDialog LayerAddMaskDialog;
struct _LayerAddMaskDialog
{
GList *layers;
PikaAddMaskType add_mask_type;
PikaChannel *channel;
gboolean invert;
PikaAddMaskCallback callback;
gpointer user_data;
};
/* local function prototypes */
static void layer_add_mask_dialog_free (LayerAddMaskDialog *private);
static void layer_add_mask_dialog_response (GtkWidget *dialog,
gint response_id,
LayerAddMaskDialog *private);
static gboolean layer_add_mask_dialog_channel_selected (PikaContainerView *view,
GList *viewables,
GList *paths,
LayerAddMaskDialog *private);
/* public functions */
GtkWidget *
layer_add_mask_dialog_new (GList *layers,
PikaContext *context,
GtkWidget *parent,
PikaAddMaskType add_mask_type,
gboolean invert,
PikaAddMaskCallback callback,
gpointer user_data)
{
LayerAddMaskDialog *private;
GtkWidget *dialog;
GtkWidget *vbox;
GtkWidget *frame;
GtkWidget *combo;
GtkWidget *button;
PikaImage *image;
PikaChannel *channel;
GList *channels;
gchar *title;
gchar *desc;
gint n_layers = g_list_length (layers);
g_return_val_if_fail (layers, NULL);
g_return_val_if_fail (GTK_IS_WIDGET (parent), NULL);
g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
private = g_slice_new0 (LayerAddMaskDialog);
private->layers = layers;
private->add_mask_type = add_mask_type;
private->invert = invert;
private->callback = callback;
private->user_data = user_data;
title = ngettext ("Add Layer Mask", "Add Layer Masks", n_layers);
title = g_strdup_printf (title, n_layers);
desc = ngettext ("Add a Mask to the Layer", "Add Masks to %d Layers", n_layers);
desc = g_strdup_printf (desc, n_layers);
dialog = pika_viewable_dialog_new (layers, context,
title,
"pika-layer-add-mask",
PIKA_ICON_LAYER_MASK,
desc,
parent,
pika_standard_help_func,
PIKA_HELP_LAYER_MASK_ADD,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_Add"), GTK_RESPONSE_OK,
NULL);
g_free (title);
g_free (desc);
pika_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
g_object_weak_ref (G_OBJECT (dialog),
(GWeakNotify) layer_add_mask_dialog_free, private);
g_signal_connect (dialog, "response",
G_CALLBACK (layer_add_mask_dialog_response),
private);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
vbox, TRUE, TRUE, 0);
gtk_widget_show (vbox);
frame =
pika_enum_radio_frame_new (PIKA_TYPE_ADD_MASK_TYPE,
gtk_label_new (_("Initialize Layer Mask to:")),
G_CALLBACK (pika_radio_button_update),
&private->add_mask_type, NULL,
&button);
pika_int_radio_group_set_active (GTK_RADIO_BUTTON (button),
private->add_mask_type);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
image = pika_item_get_image (PIKA_ITEM (layers->data));
combo = pika_container_combo_box_new (pika_image_get_channels (image),
context,
PIKA_VIEW_SIZE_SMALL, 1);
pika_enum_radio_frame_add (GTK_FRAME (frame), combo,
PIKA_ADD_MASK_CHANNEL, TRUE);
gtk_widget_show (combo);
g_signal_connect (combo, "select-items",
G_CALLBACK (layer_add_mask_dialog_channel_selected),
private);
channels = pika_image_get_selected_channels (image);
if (channels)
/* Mask dialog only requires one channel. Just take any of the
* selected ones randomly.
*/
channel = channels->data;
else
channel = PIKA_CHANNEL (pika_container_get_first_child (pika_image_get_channels (image)));
pika_container_view_select_item (PIKA_CONTAINER_VIEW (combo),
PIKA_VIEWABLE (channel));
button = gtk_check_button_new_with_mnemonic (_("In_vert mask"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), private->invert);
gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);
gtk_widget_show (button);
g_signal_connect (button, "toggled",
G_CALLBACK (pika_toggle_button_update),
&private->invert);
return dialog;
}
/* private functions */
static void
layer_add_mask_dialog_free (LayerAddMaskDialog *private)
{
g_slice_free (LayerAddMaskDialog, private);
}
static void
layer_add_mask_dialog_response (GtkWidget *dialog,
gint response_id,
LayerAddMaskDialog *private)
{
if (response_id == GTK_RESPONSE_OK)
{
PikaImage *image = pika_item_get_image (PIKA_ITEM (private->layers->data));
if (private->add_mask_type == PIKA_ADD_MASK_CHANNEL &&
! private->channel)
{
pika_message_literal (image->pika,
G_OBJECT (dialog), PIKA_MESSAGE_WARNING,
_("Please select a channel first"));
return;
}
private->callback (dialog,
private->layers,
private->add_mask_type,
private->channel,
private->invert,
private->user_data);
}
else
{
gtk_widget_destroy (dialog);
}
}
static gboolean
layer_add_mask_dialog_channel_selected (PikaContainerView *view,
GList *viewables,
GList *paths,
LayerAddMaskDialog *private)
{
g_return_val_if_fail (g_list_length (viewables) < 2, FALSE);
private->channel = viewables? PIKA_CHANNEL (viewables->data) : NULL;
return TRUE;
}