/* 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 "actions-types.h"

#include "core/pikaimage.h"

#include "text/pikatextlayer.h"

#include "widgets/pikaactiongroup.h"
#include "widgets/pikatexteditor.h"
#include "widgets/pikahelp-ids.h"

#include "display/pikadisplay.h"
#include "display/pikadisplayshell.h"

#include "tools/pikatool.h"
#include "tools/pikatexttool.h"

#include "text-tool-actions.h"
#include "text-tool-commands.h"

#include "pika-intl.h"


static const PikaActionEntry text_tool_actions[] =
{
  { "text-tool-cut", PIKA_ICON_EDIT_CUT,
    NC_("text-tool-action", "Cu_t"), NULL, { "<primary>X", NULL }, NULL,
    text_tool_cut_cmd_callback,
    NULL },

  { "text-tool-copy", PIKA_ICON_EDIT_COPY,
    NC_("text-tool-action", "_Copy"), NULL, { "<primary>C", NULL }, NULL,
    text_tool_copy_cmd_callback,
    NULL },

  { "text-tool-paste", PIKA_ICON_EDIT_PASTE,
    NC_("text-tool-action", "_Paste"), NULL, { "<primary>V", NULL }, NULL,
    text_tool_paste_cmd_callback,
    NULL },

  { "text-tool-delete", PIKA_ICON_EDIT_DELETE,
    NC_("text-tool-action", "_Delete"), NULL, { NULL }, NULL,
    text_tool_delete_cmd_callback,
    NULL },

  { "text-tool-load", PIKA_ICON_DOCUMENT_OPEN,
    NC_("text-tool-action", "_Open text file..."), NULL, { NULL }, NULL,
    text_tool_load_cmd_callback,
    NULL },

  { "text-tool-clear", PIKA_ICON_EDIT_CLEAR,
    NC_("text-tool-action", "Cl_ear"), NULL, { NULL },
    NC_("text-tool-action", "Clear all text"),
    text_tool_clear_cmd_callback,
    NULL },

  { "text-tool-text-to-path", PIKA_ICON_PATH,
    NC_("text-tool-action", "_Path from Text"), NULL, { NULL },
    NC_("text-tool-action",
        "Create a path from the outlines of the current text"),
    text_tool_text_to_path_cmd_callback,
    NULL },

  { "text-tool-text-along-path", PIKA_ICON_PATH,
    NC_("text-tool-action", "Text _along Path"), NULL, { NULL },
    NC_("text-tool-action",
        "Bend the text along the currently active path"),
    text_tool_text_along_path_cmd_callback,
    NULL }
};

static const PikaRadioActionEntry text_tool_direction_actions[] =
{
  { "text-tool-direction-ltr", PIKA_ICON_FORMAT_TEXT_DIRECTION_LTR,
    NC_("text-tool-action", "From left to right"), NULL, { NULL }, NULL,
    PIKA_TEXT_DIRECTION_LTR,
    NULL },

  { "text-tool-direction-rtl", PIKA_ICON_FORMAT_TEXT_DIRECTION_RTL,
    NC_("text-tool-action", "From right to left"), NULL, { NULL }, NULL,
    PIKA_TEXT_DIRECTION_RTL,
    NULL },

  { "text-tool-direction-ttb-rtl", PIKA_ICON_FORMAT_TEXT_DIRECTION_TTB_RTL,
    NC_("text-tool-action", "Vertical, right to left (mixed orientation)"), NULL, { NULL }, NULL,
    PIKA_TEXT_DIRECTION_TTB_RTL,
    NULL },

  { "text-tool-direction-ttb-rtl-upright", PIKA_ICON_FORMAT_TEXT_DIRECTION_TTB_RTL_UPRIGHT,
    NC_("text-tool-action", "Vertical, right to left (upright orientation)"), NULL, { NULL }, NULL,
    PIKA_TEXT_DIRECTION_TTB_RTL_UPRIGHT,
    NULL },

  { "text-tool-direction-ttb-ltr", PIKA_ICON_FORMAT_TEXT_DIRECTION_TTB_LTR,
    NC_("text-tool-action", "Vertical, left to right (mixed orientation)"), NULL, { NULL }, NULL,
    PIKA_TEXT_DIRECTION_TTB_LTR,
    NULL },

  { "text-tool-direction-ttb-ltr-upright", PIKA_ICON_FORMAT_TEXT_DIRECTION_TTB_LTR_UPRIGHT,
    NC_("text-tool-action", "Vertical, left to right (upright orientation)"), NULL, { NULL }, NULL,
    PIKA_TEXT_DIRECTION_TTB_LTR_UPRIGHT,
    NULL }
};


#define SET_HIDE_EMPTY(action,condition) \
        pika_action_group_set_action_hide_empty (group, action, (condition) != 0)

void
text_tool_actions_setup (PikaActionGroup *group)
{
  pika_action_group_add_actions (group, "text-tool-action",
                                 text_tool_actions,
                                 G_N_ELEMENTS (text_tool_actions));

  pika_action_group_add_radio_actions (group, "text-tool-action",
                                       text_tool_direction_actions,
                                       G_N_ELEMENTS (text_tool_direction_actions),
                                       NULL,
                                       PIKA_TEXT_DIRECTION_LTR,
                                       text_tool_direction_cmd_callback);
}

/* The following code is written on the assumption that this is for a
 * context menu, activated by right-clicking in a text layer.
 * Therefore, the tool must have a display.  If for any reason the
 * code is adapted to a different situation, some existence testing
 * will need to be added.
 */
void
text_tool_actions_update (PikaActionGroup *group,
                          gpointer         data)
{
  PikaTextTool     *text_tool  = PIKA_TEXT_TOOL (data);
  PikaDisplay      *display    = PIKA_TOOL (text_tool)->display;
  PikaImage        *image      = pika_display_get_image (display);
  GList            *layers;
  GList            *paths;
  PikaDisplayShell *shell;
  GtkClipboard     *clipboard;
  gboolean          text_layer = FALSE;
  gboolean          text_sel   = FALSE;   /* some text is selected        */
  gboolean          clip       = FALSE;   /* clipboard has text available */
  PikaTextDirection direction;
  gint              i;

  layers = pika_image_get_selected_layers (image);

  if (g_list_length (layers) == 1)
    text_layer = pika_item_is_text_layer (PIKA_ITEM (layers->data));

  paths = pika_image_get_selected_vectors (image);

  text_sel = pika_text_tool_get_has_text_selection (text_tool);

  /* see whether there is text available for pasting */
  shell = pika_display_get_shell (display);
  clipboard = gtk_widget_get_clipboard (shell->canvas,
                                        GDK_SELECTION_CLIPBOARD);
  clip = gtk_clipboard_wait_is_text_available (clipboard);

#define SET_VISIBLE(action,condition) \
        pika_action_group_set_action_visible (group, action, (condition) != 0)
#define SET_SENSITIVE(action,condition) \
        pika_action_group_set_action_sensitive (group, action, (condition) != 0, NULL)
#define SET_ACTIVE(action,condition) \
        pika_action_group_set_action_active (group, action, (condition) != 0)

  SET_SENSITIVE ("text-tool-cut",             text_sel);
  SET_SENSITIVE ("text-tool-copy",            text_sel);
  SET_SENSITIVE ("text-tool-paste",           clip);
  SET_SENSITIVE ("text-tool-delete",          text_sel);
  SET_SENSITIVE ("text-tool-clear",           text_layer);
  SET_SENSITIVE ("text-tool-load",            image);
  SET_SENSITIVE ("text-tool-text-to-path",    text_layer);
  SET_SENSITIVE ("text-tool-text-along-path", text_layer && g_list_length (paths) == 1);

  direction = pika_text_tool_get_direction (text_tool);
  for (i = 0; i < G_N_ELEMENTS (text_tool_direction_actions); i++)
    {
      if (direction == text_tool_direction_actions[i].value)
        {
          SET_ACTIVE (text_tool_direction_actions[i].name, TRUE);
          break;
        }
    }
}
