From 283b476d68aaebafad5c687c34438dd2fe00c1ef Mon Sep 17 00:00:00 2001 From: buliabyak Date: Mon, 16 Feb 2009 00:23:40 +0000 Subject: [PATCH] utilities for getting style at cursor, placing cursor at iterator or coordinates, getting cursor iterator; display char count when editing in text tool --- src/text-context.cpp | 46 +++++++++++++++++++++++++++++++++++++++++--- src/text-context.h | 4 ++++ src/text-editing.cpp | 12 ++++++++++-- src/text-editing.h | 2 +- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/text-context.cpp b/src/text-context.cpp index 6e4b637b8..b2efd0cc2 100644 --- a/src/text-context.cpp +++ b/src/text-context.cpp @@ -50,7 +50,7 @@ #include "context-fns.h" #include "verbs.h" #include "shape-editor.h" - +#include "selection-chemistry.h" #include "text-editing.h" #include "text-context.h" @@ -1391,6 +1391,21 @@ sp_text_get_selected_text(SPEventContext const *ec) return sp_te_get_string_multiline(tc->text, tc->text_sel_start, tc->text_sel_end); } +SPCSSAttr * +sp_text_get_style_at_cursor(SPEventContext const *ec) +{ + if (!SP_IS_TEXT_CONTEXT(ec)) + return NULL; + SPTextContext const *tc = SP_TEXT_CONTEXT(ec); + if (tc->text == NULL) + return NULL; + + SPObject const *obj = sp_te_object_at_position(tc->text, tc->text_sel_end); + if (obj) + return take_style_from_item((SPItem *) obj); + return NULL; +} + /** Deletes the currently selected characters. Returns false if there is no text selection currently. @@ -1574,6 +1589,8 @@ sp_text_context_update_cursor(SPTextContext *tc, bool scroll_to_see) tc->show = TRUE; tc->phase = 1; + Inkscape::Text::Layout const *layout = te_get_layout(tc->text); + int const nChars = layout->iteratorToCharIndex(layout->end()); if (SP_IS_FLOWTEXT(tc->text)) { SPItem *frame = SP_FLOWTEXT(tc->text)->get_frame (NULL); // first frame only if (frame) { @@ -1583,9 +1600,9 @@ sp_text_context_update_cursor(SPTextContext *tc, bool scroll_to_see) SP_CTRLRECT(tc->frame)->setRectangle(*frame_bbox); } } - SP_EVENT_CONTEXT(tc)->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Type flowed text; Enter to start new paragraph.")); + SP_EVENT_CONTEXT(tc)->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("Type or edit flowed text (%d characters); Enter to start new paragraph."), nChars); } else { - SP_EVENT_CONTEXT(tc)->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Type text; Enter to start new line.")); + SP_EVENT_CONTEXT(tc)->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("Type or edit text (%d characters); Enter to start new line."), nChars); } } else { @@ -1706,6 +1723,29 @@ sptc_commit(GtkIMContext */*imc*/, gchar *string, SPTextContext *tc) _("Type text")); } +void +sp_text_context_place_cursor (SPTextContext *tc, SPObject *text, Inkscape::Text::Layout::iterator where) +{ + SP_EVENT_CONTEXT_DESKTOP (tc)->selection->set (text); + tc->text_sel_start = tc->text_sel_end = where; + sp_text_context_update_cursor(tc); + sp_text_context_update_text_selection(tc); +} + +void +sp_text_context_place_cursor_at (SPTextContext *tc, SPObject *text, Geom::Point const p) +{ + SP_EVENT_CONTEXT_DESKTOP (tc)->selection->set (text); + sp_text_context_place_cursor (tc, text, sp_te_get_position_by_coords(tc->text, p)); +} + +Inkscape::Text::Layout::iterator *sp_text_context_get_cursor_position(SPTextContext *tc, SPObject *text) +{ + if (text != tc->text) + return NULL; + return &(tc->text_sel_end); +} + /* Local Variables: diff --git a/src/text-context.h b/src/text-context.h index b9068e497..a6e2e8db7 100644 --- a/src/text-context.h +++ b/src/text-context.h @@ -84,6 +84,10 @@ GtkType sp_text_context_get_type (void); bool sp_text_paste_inline(SPEventContext *ec); Glib::ustring sp_text_get_selected_text(SPEventContext const *ec); +SPCSSAttr *sp_text_get_style_at_cursor(SPEventContext const *ec); bool sp_text_delete_selection(SPEventContext *ec); +void sp_text_context_place_cursor (SPTextContext *tc, SPObject *text, Inkscape::Text::Layout::iterator where); +void sp_text_context_place_cursor_at (SPTextContext *tc, SPObject *text, Geom::Point const p); +Inkscape::Text::Layout::iterator *sp_text_context_get_cursor_position(SPTextContext *tc, SPObject *text); #endif diff --git a/src/text-editing.cpp b/src/text-editing.cpp index 2ccc91195..5b9db13d4 100644 --- a/src/text-editing.cpp +++ b/src/text-editing.cpp @@ -109,6 +109,14 @@ sp_te_get_cursor_coords (SPItem const *item, Inkscape::Text::Layout::iterator co } SPStyle const * sp_te_style_at_position(SPItem const *text, Inkscape::Text::Layout::iterator const &position) +{ + SPObject const *pos_obj = sp_te_object_at_position(text, position); + if (pos_obj) + return SP_OBJECT_STYLE(pos_obj); + return NULL; +} + +SPObject const * sp_te_object_at_position(SPItem const *text, Inkscape::Text::Layout::iterator const &position) { Inkscape::Text::Layout const *layout = te_get_layout(text); if (layout == NULL) @@ -119,8 +127,8 @@ SPStyle const * sp_te_style_at_position(SPItem const *text, Inkscape::Text::Layo pos_obj = SP_OBJECT(rawptr); if (pos_obj == 0) pos_obj = text; while (SP_OBJECT_STYLE(pos_obj) == NULL) - pos_obj = SP_OBJECT_PARENT(pos_obj); // SPStrings don't have style - return SP_OBJECT_STYLE(pos_obj); + pos_obj = SP_OBJECT_PARENT(pos_obj); // not interested in SPStrings + return pos_obj; } /* diff --git a/src/text-editing.h b/src/text-editing.h index b1fb6b200..83ddae77f 100644 --- a/src/text-editing.h +++ b/src/text-editing.h @@ -38,8 +38,8 @@ Inkscape::Text::Layout::iterator sp_te_get_position_by_coords (SPItem const *ite void sp_te_get_cursor_coords (SPItem const *item, Inkscape::Text::Layout::iterator const &position, Geom::Point &p0, Geom::Point &p1); double sp_te_get_average_linespacing (SPItem *text); - SPStyle const * sp_te_style_at_position(SPItem const *text, Inkscape::Text::Layout::iterator const &position); +SPObject const * sp_te_object_at_position(SPItem const *text, Inkscape::Text::Layout::iterator const &position); Inkscape::Text::Layout::iterator sp_te_insert(SPItem *item, Inkscape::Text::Layout::iterator const &position, gchar const *utf8); Inkscape::Text::Layout::iterator sp_te_replace(SPItem *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, gchar const *utf8); -- 2.30.2