1
1
mirror of https://gitlab.gnome.org/GNOME/gimp.git synced 2025-10-06 01:12:40 +02:00

tools: implement shortcuts in text tool...

...for toggle bold/italic/underline.

This commit adds support for common formatting shortcuts in the Text Tool:
- Ctrl+B: Toggle bold
- Ctrl+I: Toggle italic
- Ctrl+U: Toggle underline
This commit is contained in:
Gabriele Barbero
2025-04-29 18:04:42 +02:00
committed by Alx Sa
parent 73979309ab
commit eeeaca928b
7 changed files with 141 additions and 12 deletions

View File

@@ -61,6 +61,21 @@ static const GimpActionEntry text_tool_actions[] =
text_tool_paste_cmd_callback,
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,
NC_("text-tool-action", "_Delete"), NULL, { NULL }, NULL,
text_tool_delete_cmd_callback,
@@ -189,14 +204,17 @@ text_tool_actions_update (GimpActionGroup *group,
#define SET_ACTIVE(action,condition) \
gimp_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);
SET_SENSITIVE ("text-tool-cut", text_sel);
SET_SENSITIVE ("text-tool-copy", text_sel);
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-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 = gimp_text_tool_get_direction (text_tool);
for (i = 0; i < G_N_ELEMENTS (text_tool_direction_actions); i++)

View File

@@ -84,6 +84,36 @@ text_tool_paste_cmd_callback (GimpAction *action,
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
text_tool_delete_cmd_callback (GimpAction *action,
GVariant *value,

View File

@@ -27,6 +27,16 @@ void text_tool_copy_cmd_callback (GimpAction *action,
void text_tool_paste_cmd_callback (GimpAction *action,
GVariant *value,
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,
GVariant *value,
gpointer data);

View File

@@ -448,12 +448,14 @@ gboolean
gimp_text_tool_editor_key_press (GimpTextTool *text_tool,
GdkEventKey *kevent)
{
GimpTool *tool = GIMP_TOOL (text_tool);
GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->buffer);
GimpTool *tool = GIMP_TOOL (text_tool);
GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->buffer);
GtkTextIter cursor;
GtkTextIter selection;
gboolean retval = TRUE;
gboolean retval = TRUE;
GdkDisplay *display = gdk_display_get_default ();
GdkModifierType primary_mask;
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_selection_bound (buffer));
primary_mask = gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
GDK_MODIFIER_INTENT_PRIMARY_ACCELERATOR);
switch (kevent->keyval)
{
case GDK_KEY_Return:
@@ -520,6 +524,24 @@ gimp_text_tool_editor_key_press (GimpTextTool *text_tool,
GIMP_TOOL (text_tool)->display);
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:
retval = FALSE;
}

View File

@@ -2309,6 +2309,48 @@ gimp_text_tool_paste_clipboard (GimpTextTool *text_tool)
clipboard, NULL, TRUE);
}
void
gimp_text_tool_toggle_tag (GimpTextTool *text_tool,
GtkTextTag *tag)
{
GtkTextBuffer *buffer;
g_return_if_fail (GIMP_IS_TEXT_TOOL (text_tool));
g_return_if_fail (GTK_IS_TEXT_BUFFER (text_tool->buffer));
buffer = GTK_TEXT_BUFFER (text_tool->buffer);
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
gimp_text_tool_create_path (GimpTextTool *text_tool)
{

View File

@@ -112,6 +112,8 @@ void gimp_text_tool_delete_selection (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_paste_clipboard (GimpTextTool *text_tool);
void gimp_text_tool_toggle_tag (GimpTextTool *text_tool,
GtkTextTag *tag);
void gimp_text_tool_create_path (GimpTextTool *text_tool);
gboolean gimp_text_tool_create_path_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-paste</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>
<item><attribute name="action">text-tool.text-tool-load</attribute></item>
<item><attribute name="action">text-tool.text-tool-clear</attribute></item>