Merge branch 'wip/gabrybarbe/issue-1056-shortcuts-text-box' into 'master'

Issue #1056 - Implement shortcuts in text tool for toggle bold/italic/underline

Closes #1056

See merge request GNOME/gimp!2242
This commit is contained in:
Gabriele 2025-07-02 20:39:31 +02:00
commit 49cd1a33cc
7 changed files with 132 additions and 4 deletions

View file

@ -61,6 +61,21 @@ static const GimpActionEntry text_tool_actions[] =
text_tool_paste_cmd_callback, text_tool_paste_cmd_callback,
GIMP_HELP_TEXT_TOOL_PASTE }, GIMP_HELP_TEXT_TOOL_PASTE },
{ "text-tool-toggle-bold", GIMP_ICON_FORMAT_TEXT_BOLD,
NC_("text-tool-action", "_Bold"), NULL, { "<primary>B", NULL }, NULL,
text_tool_toggle_bold_cmd_callback,
NULL },
{ "text-tool-toggle-italic", GIMP_ICON_FORMAT_TEXT_ITALIC,
NC_("text-tool-action", "_Italic"), NULL, { "<primary>I", NULL }, NULL,
text_tool_toggle_italic_cmd_callback,
NULL },
{ "text-tool-toggle-underline", GIMP_ICON_FORMAT_TEXT_UNDERLINE,
NC_("text-tool-action", "_Underline"), NULL, { "<primary>U", NULL }, NULL,
text_tool_toggle_underline_cmd_callback,
NULL },
{ "text-tool-delete", GIMP_ICON_EDIT_DELETE, { "text-tool-delete", GIMP_ICON_EDIT_DELETE,
NC_("text-tool-action", "_Delete"), NULL, { NULL }, NULL, NC_("text-tool-action", "_Delete"), NULL, { NULL }, NULL,
text_tool_delete_cmd_callback, text_tool_delete_cmd_callback,
@ -192,6 +207,9 @@ text_tool_actions_update (GimpActionGroup *group,
SET_SENSITIVE ("text-tool-cut", text_sel); SET_SENSITIVE ("text-tool-cut", text_sel);
SET_SENSITIVE ("text-tool-copy", text_sel); SET_SENSITIVE ("text-tool-copy", text_sel);
SET_SENSITIVE ("text-tool-paste", clip); SET_SENSITIVE ("text-tool-paste", clip);
SET_SENSITIVE ("text-tool-toggle-bold", text_sel);
SET_SENSITIVE ("text-tool-toggle-italic", text_sel);
SET_SENSITIVE ("text-tool-toggle-underline",text_sel);
SET_SENSITIVE ("text-tool-delete", text_sel); SET_SENSITIVE ("text-tool-delete", text_sel);
SET_SENSITIVE ("text-tool-clear", text_layer); SET_SENSITIVE ("text-tool-clear", text_layer);
SET_SENSITIVE ("text-tool-load", image); SET_SENSITIVE ("text-tool-load", image);

View file

@ -84,6 +84,36 @@ text_tool_paste_cmd_callback (GimpAction *action,
gimp_text_tool_paste_clipboard (text_tool); gimp_text_tool_paste_clipboard (text_tool);
} }
void
text_tool_toggle_bold_cmd_callback (GimpAction *action,
GVariant *value,
gpointer data)
{
GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
gimp_text_tool_toggle_tag (text_tool, text_tool->buffer->bold_tag);
}
void
text_tool_toggle_italic_cmd_callback (GimpAction *action,
GVariant *value,
gpointer data)
{
GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
gimp_text_tool_toggle_tag (text_tool, text_tool->buffer->italic_tag);
}
void
text_tool_toggle_underline_cmd_callback (GimpAction *action,
GVariant *value,
gpointer data)
{
GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
gimp_text_tool_toggle_tag (text_tool, text_tool->buffer->underline_tag);
}
void void
text_tool_delete_cmd_callback (GimpAction *action, text_tool_delete_cmd_callback (GimpAction *action,
GVariant *value, GVariant *value,

View file

@ -28,6 +28,16 @@ void text_tool_copy_cmd_callback (GimpAction *action,
void text_tool_paste_cmd_callback (GimpAction *action, void text_tool_paste_cmd_callback (GimpAction *action,
GVariant *value, GVariant *value,
gpointer data); gpointer data);
void text_tool_toggle_bold_cmd_callback (GimpAction *action,
GVariant *value,
gpointer data);
void text_tool_toggle_italic_cmd_callback (GimpAction *action,
GVariant *value,
gpointer data);
void text_tool_toggle_underline_cmd_callback
(GimpAction *action,
GVariant *value,
gpointer data);
void text_tool_delete_cmd_callback (GimpAction *action, void text_tool_delete_cmd_callback (GimpAction *action,
GVariant *value, GVariant *value,
gpointer data); gpointer data);

View file

@ -454,6 +454,8 @@ gimp_text_tool_editor_key_press (GimpTextTool *text_tool,
GtkTextIter cursor; GtkTextIter cursor;
GtkTextIter selection; GtkTextIter selection;
gboolean retval = TRUE; gboolean retval = TRUE;
GdkDisplay *display = gdk_display_get_default ();
GdkModifierType primary_mask;
if (! gtk_widget_has_focus (shell->canvas)) if (! gtk_widget_has_focus (shell->canvas))
{ {
@ -499,6 +501,8 @@ gimp_text_tool_editor_key_press (GimpTextTool *text_tool,
gtk_text_buffer_get_iter_at_mark (buffer, &selection, gtk_text_buffer_get_iter_at_mark (buffer, &selection,
gtk_text_buffer_get_selection_bound (buffer)); gtk_text_buffer_get_selection_bound (buffer));
primary_mask = gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
GDK_MODIFIER_INTENT_PRIMARY_ACCELERATOR);
switch (kevent->keyval) switch (kevent->keyval)
{ {
case GDK_KEY_Return: case GDK_KEY_Return:
@ -520,6 +524,24 @@ gimp_text_tool_editor_key_press (GimpTextTool *text_tool,
GIMP_TOOL (text_tool)->display); GIMP_TOOL (text_tool)->display);
break; break;
case GDK_KEY_b:
case GDK_KEY_B:
if ((kevent->state & gimp_get_all_modifiers_mask()) == primary_mask)
gimp_text_tool_toggle_tag (text_tool, text_tool->buffer->bold_tag);
break;
case GDK_KEY_i:
case GDK_KEY_I:
if ((kevent->state & gimp_get_all_modifiers_mask()) == primary_mask)
gimp_text_tool_toggle_tag (text_tool, text_tool->buffer->italic_tag);
break;
case GDK_KEY_u:
case GDK_KEY_U:
if ((kevent->state & gimp_get_all_modifiers_mask()) == primary_mask)
gimp_text_tool_toggle_tag (text_tool, text_tool->buffer->underline_tag);
break;
default: default:
retval = FALSE; retval = FALSE;
} }

View file

@ -2309,6 +2309,47 @@ gimp_text_tool_paste_clipboard (GimpTextTool *text_tool)
clipboard, NULL, TRUE); clipboard, NULL, TRUE);
} }
void
gimp_text_tool_toggle_tag (GimpTextTool *text_tool,
GtkTextTag *tag)
{
GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->buffer);
g_return_if_fail (GIMP_IS_TEXT_TOOL (text_tool));
if (gtk_text_buffer_get_has_selection (buffer))
{
GtkTextIter start;
GtkTextIter end;
GtkTextIter iter;
gboolean is_tag_active = FALSE;
gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
iter = start;
while (! gtk_text_iter_equal (&iter, &end))
{
if (gtk_text_iter_has_tag (&iter, tag))
{
is_tag_active = TRUE;
break;
}
gtk_text_iter_forward_char (&iter);
}
gtk_text_buffer_begin_user_action (buffer);
if (is_tag_active)
gtk_text_buffer_remove_tag (buffer, tag,
&start, &end);
else
gtk_text_buffer_apply_tag (buffer, tag,
&start, &end);
gtk_text_buffer_end_user_action (buffer);
}
}
void void
gimp_text_tool_create_vectors (GimpTextTool *text_tool) gimp_text_tool_create_vectors (GimpTextTool *text_tool)
{ {

View file

@ -114,6 +114,8 @@ void gimp_text_tool_delete_selection (GimpTextTool *text_tool);
void gimp_text_tool_cut_clipboard (GimpTextTool *text_tool); void gimp_text_tool_cut_clipboard (GimpTextTool *text_tool);
void gimp_text_tool_copy_clipboard (GimpTextTool *text_tool); void gimp_text_tool_copy_clipboard (GimpTextTool *text_tool);
void gimp_text_tool_paste_clipboard (GimpTextTool *text_tool); void gimp_text_tool_paste_clipboard (GimpTextTool *text_tool);
void gimp_text_tool_toggle_tag (GimpTextTool *text_tool,
GtkTextTag *tag);
void gimp_text_tool_create_vectors (GimpTextTool *text_tool); void gimp_text_tool_create_vectors (GimpTextTool *text_tool);
gboolean gimp_text_tool_create_vectors_warped (GimpTextTool *text_tool, gboolean gimp_text_tool_create_vectors_warped (GimpTextTool *text_tool,

View file

@ -9,6 +9,11 @@
<item><attribute name="action">text-tool.text-tool-copy</attribute></item> <item><attribute name="action">text-tool.text-tool-copy</attribute></item>
<item><attribute name="action">text-tool.text-tool-paste</attribute></item> <item><attribute name="action">text-tool.text-tool-paste</attribute></item>
<item><attribute name="action">text-tool.text-tool-delete</attribute></item> <item><attribute name="action">text-tool.text-tool-delete</attribute></item>
<section>
<item><attribute name="action">text-tool.text-tool-toggle-bold</attribute></item>
<item><attribute name="action">text-tool.text-tool-toggle-italic</attribute></item>
<item><attribute name="action">text-tool.text-tool-toggle-underline</attribute></item>
</section>
<section> <section>
<item><attribute name="action">text-tool.text-tool-load</attribute></item> <item><attribute name="action">text-tool.text-tool-load</attribute></item>
<item><attribute name="action">text-tool.text-tool-clear</attribute></item> <item><attribute name="action">text-tool.text-tool-clear</attribute></item>