Code

Connector tool: make connectors avoid the convex hull of shapes.
[inkscape.git] / src / text-context.cpp
index 1dd5e4e82f0ec1c13d0434fa4ca1ee6eab822b4c..e6f4f083b2945b006b0496707f328f5204255aac 100644 (file)
@@ -35,7 +35,6 @@
 #include "desktop.h"
 #include "desktop-style.h"
 #include "desktop-handles.h"
-#include "desktop-affine.h"
 #include "message-stack.h"
 #include "message-context.h"
 #include "pixmaps/cursor-text.xpm"
@@ -49,7 +48,8 @@
 #include "sp-metrics.h"
 #include "context-fns.h"
 #include "verbs.h"
-
+#include "shape-editor.h"
+#include "selection-chemistry.h"
 #include "text-editing.h"
 
 #include "text-context.h"
@@ -131,9 +131,6 @@ sp_text_context_init(SPTextContext *tc)
     event_context->tolerance = 0;
     event_context->within_tolerance = false;
 
-    event_context->shape_repr = NULL;
-    event_context->shape_knot_holder = NULL;
-
     tc->imc = NULL;
 
     tc->text = NULL;
@@ -171,6 +168,10 @@ sp_text_context_dispose(GObject *obj)
     tc->style_set_connection.~connection();
     tc->sel_changed_connection.~connection();
     tc->sel_modified_connection.~connection();
+
+    delete ec->shape_editor;
+    ec->shape_editor = NULL;
+
     tc->text_sel_end.~iterator();
     tc->text_sel_start.~iterator();
     tc->text_selection_quads.~vector();
@@ -183,26 +184,8 @@ sp_text_context_dispose(GObject *obj)
     }
 
     Inkscape::Rubberband::get(ec->desktop)->stop();
-
-    if (ec->shape_knot_holder) {
-        delete ec->shape_knot_holder;
-        ec->shape_knot_holder = NULL;
-    }
-    if (ec->shape_repr) { // remove old listener
-        sp_repr_remove_listener_by_data(ec->shape_repr, ec);
-        Inkscape::GC::release(ec->shape_repr);
-        ec->shape_repr = 0;
-    }
 }
 
-static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
-    NULL, /* child_added */
-    NULL, /* child_removed */
-    ec_shape_event_attr_changed,
-    NULL, /* content_changed */
-    NULL  /* order_changed */
-};
-
 static void
 sp_text_context_setup(SPEventContext *ec)
 {
@@ -259,15 +242,11 @@ sp_text_context_setup(SPEventContext *ec)
     if (((SPEventContextClass *) parent_class)->setup)
         ((SPEventContextClass *) parent_class)->setup(ec);
 
+    ec->shape_editor = new ShapeEditor(ec->desktop);
+
     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
     if (item && SP_IS_FLOWTEXT (item) && SP_FLOWTEXT(item)->has_internal_frame()) {
-        ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
-        Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(SP_FLOWTEXT(item)->get_frame(NULL));
-        if (shape_repr) {
-            ec->shape_repr = shape_repr;
-            Inkscape::GC::anchor(shape_repr);
-            sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
-        }
+        ec->shape_editor->set_item(item, SH_KNOTHOLDER);
     }
 
     tc->sel_changed_connection = sp_desktop_selection(desktop)->connectChanged(
@@ -406,6 +385,7 @@ sp_text_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEve
         case GDK_BUTTON_RELEASE:
             if (event->button.button == 1 && tc->dragging && !event_context->space_panning) {
                 tc->dragging = 0;
+                sp_event_context_discard_delayed_snap_event(event_context);
                 ret = TRUE;
             }
             break;
@@ -511,7 +491,8 @@ sp_text_context_setup_text(SPTextContext *tc)
     /* yes, it's immediate .. why does it matter? */
     sp_desktop_selection(ec->desktop)->set(text_item);
     Inkscape::GC::release(rtext);
-    text_item->transform = SP_ITEM(ec->desktop->currentRoot())->getRelativeTransform(ec->desktop->currentLayer());
+    text_item->transform = sp_item_i2doc_affine(SP_ITEM(ec->desktop->currentLayer())).inverse();
+
     text_item->updateRepr();
     sp_document_done(sp_desktop_document(ec->desktop), SP_VERB_CONTEXT_TEXT,
                      _("Create text"));
@@ -685,7 +666,7 @@ sp_text_context_root_handler(SPEventContext *const event_context, GdkEvent *cons
                     /* Button 1, set X & Y & new item */
                     sp_desktop_selection(desktop)->clear();
                     Geom::Point dtp = desktop->w2d(Geom::Point(event->button.x, event->button.y));
-                    tc->pdoc = sp_desktop_dt2root_xy_point(desktop, dtp);
+                    tc->pdoc = desktop->dt2doc(dtp);
 
                     tc->show = TRUE;
                     tc->phase = 1;
@@ -900,10 +881,10 @@ sp_text_context_root_handler(SPEventContext *const event_context, GdkEvent *cons
                                 if (MOD__CTRL_ONLY && tc->text) {
                                     SPStyle const *style = sp_te_style_at_position(tc->text, std::min(tc->text_sel_start, tc->text_sel_end));
                                     SPCSSAttr *css = sp_repr_css_attr_new();
-                                    if (style->font_style.computed == SP_CSS_FONT_STYLE_NORMAL)
-                                        sp_repr_css_set_property(css, "font-style", "italic");
-                                    else
+                                    if (style->font_style.computed != SP_CSS_FONT_STYLE_NORMAL)
                                         sp_repr_css_set_property(css, "font-style", "normal");
+                                    else
+                                        sp_repr_css_set_property(css, "font-style", "italic");
                                     sp_te_apply_style(tc->text, tc->text_sel_start, tc->text_sel_end, css);
                                     sp_repr_css_attr_unref(css);
                                     sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
@@ -1030,7 +1011,7 @@ sp_text_context_root_handler(SPEventContext *const event_context, GdkEvent *cons
                                         sp_document_maybe_done(sp_desktop_document(desktop), "kern:left", SP_VERB_CONTEXT_TEXT,
                                                                _("Kern to the left"));
                                     } else {
-                                        if (MOD__CTRL) 
+                                        if (MOD__CTRL)
                                             tc->text_sel_end.cursorLeftWithControl();
                                         else
                                             tc->text_sel_end.cursorLeft();
@@ -1295,6 +1276,9 @@ sp_text_context_root_handler(SPEventContext *const event_context, GdkEvent *cons
                         }
                         Inkscape::Rubberband::get(desktop)->stop();
                     }
+                } else if ((group0_keyval == GDK_x || group0_keyval == GDK_X) && MOD__ALT_ONLY) {
+                    desktop->setToolboxFocusTo ("altx-text");
+                    return TRUE;
                 }
             }
             break;
@@ -1333,7 +1317,7 @@ sp_text_paste_inline(SPEventContext *ec)
 
         Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
         Glib::ustring const clip_text = refClipboard->wait_for_text();
-        
+
         if (!clip_text.empty()) {
                // Fix for 244940
                // The XML standard defines the following as valid characters
@@ -1361,7 +1345,7 @@ sp_text_paste_inline(SPEventContext *ec)
                        itr = text.erase(itr);
                    }
                }
-               
+
             if (!tc->text) { // create text if none (i.e. if nascent_object)
                 sp_text_context_setup_text(tc);
                 tc->nascent_object = 0; // we don't need it anymore, having created a real <text>
@@ -1406,6 +1390,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.
@@ -1448,26 +1447,10 @@ sp_text_context_selection_changed(Inkscape::Selection *selection, SPTextContext
 
     SPEventContext *ec = SP_EVENT_CONTEXT(tc);
 
-    if (ec->shape_knot_holder) { // destroy knotholder
-        delete ec->shape_knot_holder;
-        ec->shape_knot_holder = NULL;
-    }
-
-    if (ec->shape_repr) { // remove old listener
-        sp_repr_remove_listener_by_data(ec->shape_repr, ec);
-        Inkscape::GC::release(ec->shape_repr);
-        ec->shape_repr = 0;
-    }
-
+    ec->shape_editor->unset_item(SH_KNOTHOLDER);
     SPItem *item = selection->singleItem();
     if (item && SP_IS_FLOWTEXT (item) && SP_FLOWTEXT(item)->has_internal_frame()) {
-        ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
-        Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(SP_FLOWTEXT(item)->get_frame(NULL));
-        if (shape_repr) {
-            ec->shape_repr = shape_repr;
-            Inkscape::GC::anchor(shape_repr);
-            sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
-        }
+        ec->shape_editor->set_item(item, SH_KNOTHOLDER);
     }
 
     if (tc->text && (item != tc->text)) {
@@ -1605,6 +1588,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) {
@@ -1614,9 +1599,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; <b>Enter</b> to start new paragraph."));
+            SP_EVENT_CONTEXT(tc)->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("Type or edit flowed text (%d characters); <b>Enter</b> to start new paragraph."), nChars);
         } else {
-            SP_EVENT_CONTEXT(tc)->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Type text; <b>Enter</b> to start new line."));
+            SP_EVENT_CONTEXT(tc)->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("Type or edit text (%d characters); <b>Enter</b> to start new line."), nChars);
         }
 
     } else {
@@ -1737,6 +1722,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: