text-context.cpp revision 8a0a34615f080ecd1c09986d8963d6afe5937a25
/*
* SPTextContext
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
*
* Copyright (C) 1999-2005 authors
* Copyright (C) 2001 Ximian, Inc.
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gdk/gdkkeysyms.h>
#include <display/sp-ctrlline.h>
#include <display/sodipodi-ctrlrect.h>
#include <display/sp-ctrlquadr.h>
#include <gtkmm/clipboard.h>
#include "macros.h"
#include "sp-text.h"
#include "sp-flowtext.h"
#include "document.h"
#include "sp-namedview.h"
#include "style.h"
#include "selection.h"
#include "desktop.h"
#include "desktop-style.h"
#include "desktop-handles.h"
#include "message-stack.h"
#include "message-context.h"
#include "pixmaps/cursor-text.xpm"
#include "pixmaps/cursor-text-insert.xpm"
#include "object-edit.h"
#include "xml/node-event-vector.h"
#include "preferences.h"
#include "rubberband.h"
#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"
using Inkscape::DocumentUndo;
static gint sp_text_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
static void sp_text_context_selection_modified(Inkscape::Selection *selection, guint flags, SPTextContext *tc);
static SPEventContextClass *parent_class;
{
if (!type) {
sizeof(SPTextContextClass),
sizeof(SPTextContext),
4,
NULL, /* value_table */
};
}
return type;
}
static void
{
}
static void
{
event_context->xp = 0;
event_context->yp = 0;
event_context->tolerance = 0;
event_context->within_tolerance = false;
tc->nascent_object = 0;
}
static void
{
delete ec->shape_editor;
}
}
}
static void
{
if (timeout < 0) {
timeout = 200;
} else {
timeout /= 2;
}
/* im preedit handling is very broken in inkscape for
* multi-byte characters. See bug 1086769.
* We need to let the IM handle the preediting, and
* just take in the characters when they're finished being
* entered.
*/
if (gtk_widget_has_focus(canvas)) {
}
}
}
);
);
);
);
ec->enableSelectionCue();
}
ec->enableGrDrag();
}
}
static void
{
}
ec->enableGrDrag(false);
}
}
}
}
}
}
}
static gint
{
case GDK_BUTTON_PRESS:
// find out clicked item, disregarding groups
// find out click point in document coordinates
// set the cursor closest to that point
// update display
}
}
}
break;
case GDK_2BUTTON_PRESS:
if (layout) {
}
}
break;
case GDK_3BUTTON_PRESS:
}
break;
case GDK_BUTTON_RELEASE:
}
break;
case GDK_MOTION_NOTIFY:
if (!layout) break;
// find out click point in document coordinates
// set the cursor closest to that point
// double-click dragging: go by word
} else
// triple-click dragging: go by line
else
}
// update display
}
break;
}
// find out item under mouse, disregarding groups
if (layout->inputTruncated()) {
} else {
}
if (ibbox) {
}
if (SP_IS_TEXT (item_ungrouped)) {
desktop->event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to edit the text, <b>drag</b> to select part of the text."));
} else {
desktop->event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to edit the flowed text, <b>drag</b> to select part of the text."));
}
}
break;
default:
break;
}
if (!ret) {
}
return ret;
}
static void
{
/* Create <text> */
/* Set style */
/* Create <tspan> */
/* Create TEXT */
/* fixme: Is selection::changed really immediate? */
/* yes, it's immediate .. why does it matter? */
text_item->updateRepr();
_("Create text"));
}
/**
* Insert the character indicated by tc.uni to replace the current selection,
*
*/
static void
{
unsigned int uv;
&& !(g_unichar_validate(static_cast<gunichar>(uv)) && (g_unichar_type(static_cast<gunichar>(uv)) == G_UNICODE_PRIVATE_USE) ) ) {
// This may be due to bad input, so it goes to statusbar.
_("Non-printable character"));
} else {
}
gchar u[10];
u[len] = '\0';
tc->text_sel_start = tc->text_sel_end = sp_te_replace(tc->text, tc->text_sel_start, tc->text_sel_end, u);
_("Insert Unicode character"));
}
}
static void
{
unsigned int uv;
uv = 0xfffd;
}
}
static void
{
char utf8[10];
/* Status bar messages are in pango markup, so we need xml escaping. */
switch(utf8[0]) {
default: break;
}
}
} else {
tc->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("Unicode (<b>Enter</b> to finish): "));
}
}
static gint
{
case GDK_BUTTON_PRESS:
return TRUE;
}
// save drag origin
event_context->within_tolerance = true;
m.unSetup();
GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
/* Processed */
return TRUE;
}
break;
case GDK_MOTION_NOTIFY:
// update cursor and statusbar: we are not over a text object now
}
break; // do not drag if we're within tolerance from origin
}
// Once the user has moved farther than tolerance from the original location
// (indicating they intend to draw, not click), then always process the
// motion notify coordinates as given (no snapping back to origin)
event_context->within_tolerance = false;
m.unSetup();
// status text
GString *xs = SP_PX_TO_METRIC_STRING(fabs((p - tc->p0)[Geom::X]), desktop->namedview->getDefaultMetric());
GString *ys = SP_PX_TO_METRIC_STRING(fabs((p - tc->p0)[Geom::Y]), desktop->namedview->getDefaultMetric());
event_context->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Flowed text frame</b>: %s × %s"), xs->str, ys->str);
} else if (!sp_event_context_knot_mouseover(event_context)) {
m.unSetup();
}
break;
case GDK_BUTTON_RELEASE:
m.unSetup();
}
/* Button 1, set X & Y & new item */
/* Cursor */
// Cursor height is defined by the new text object's font size; it needs to be set
// artificially here, for the text object does not exist yet:
event_context->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Type text; <b>Enter</b> to start new line.")); // FIXME:: this is a copy of a string from _update_cursor below, do not desync
event_context->within_tolerance = false;
// otherwise even one line won't fit; most probably a slip of hand (even if bigger than tolerance)
/* Set style */
_("Create flowed text"));
} else {
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The frame is <b>too small</b> for the current font size. Flowed text not created."));
}
}
return TRUE;
}
break;
case GDK_KEY_PRESS: {
if (group0_keyval == GDK_KP_Add ||
group0_keyval == GDK_KP_Subtract) {
break; // otherwise pass on keypad +/- so they can zoom
}
// there is an active text object in this context, or a new object was just created
// but we have our own so make sure they don't swallow it
//IM did not consume the key, or we're in unimode
/* TODO: ISO 14755 (section 3 Definitions) says that we should also
accept the first 6 characters of alphabets other than the latin
alphabet "if the Latin alphabet is not used". The below is also
reasonable (viz. hope that the user's keyboard includes latin
characters and force latin interpretation -- just as we do for our
keyboard shortcuts), but differs from the ISO 14755
recommendation. */
switch (group0_keyval) {
case GDK_space:
case GDK_KP_Space: {
}
/* Stay in unimode. */
return TRUE;
}
case GDK_BackSpace: {
}
return TRUE;
}
case GDK_Return:
case GDK_KP_Enter: {
}
/* Exit unimode. */
return TRUE;
}
case GDK_Escape: {
// Cancel unimode.
return TRUE;
}
case GDK_Shift_L:
case GDK_Shift_R:
break;
default: {
if (g_ascii_isxdigit(group0_keyval)) {
/* This behaviour is partly to allow us to continue to
use a fixed-length buffer for tc->uni. Reason for
choosing the number 8 is that it's the length of
``canonical form'' mentioned in the ISO 14755 spec.
An advantage over choosing 6 is that it allows using
backspace for typos & misremembering when entering a
6-digit number. */
}
return TRUE;
} else {
/* The intent is to ignore but consume characters that could be
typos for hex digits. Gtk seems to ignore & consume all
non-hex-digits, and we do similar here. Though note that some
shortcuts (like keypad +/- for zoom) get processed before
reaching this code. */
return TRUE;
}
}
}
}
bool cursor_moved = false;
int screenlines = 1;
if (screenlines <= 0)
screenlines = 1;
}
/* Neither unimode nor IM consumed key; process text tool shortcuts */
switch (group0_keyval) {
case GDK_x:
case GDK_X:
if (MOD__ALT_ONLY) {
return TRUE;
}
break;
case GDK_space:
if (MOD__CTRL_ONLY) {
/* No-break space */
}
tc->text_sel_start = tc->text_sel_end = sp_te_replace(tc->text, tc->text_sel_start, tc->text_sel_end, "\302\240");
_("Insert no-break space"));
return TRUE;
}
break;
case GDK_U:
case GDK_u:
} else {
event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("Unicode (<b>Enter</b> to finish): "));
}
}
return TRUE;
}
break;
case GDK_B:
case GDK_b:
SPStyle const *style = sp_te_style_at_position(tc->text, std::min(tc->text_sel_start, tc->text_sel_end));
else
_("Make bold"));
return TRUE;
}
break;
case GDK_I:
case GDK_i:
SPStyle const *style = sp_te_style_at_position(tc->text, std::min(tc->text_sel_start, tc->text_sel_end));
else
_("Make italic"));
return TRUE;
}
break;
case GDK_A:
case GDK_a:
if (layout) {
return TRUE;
}
}
break;
case GDK_Return:
case GDK_KP_Enter:
{
}
(void)success; // TODO cleanup
_("New line"));
return TRUE;
}
case GDK_BackSpace:
if (tc->text) { // if nascent_object, do nothing, but return TRUE; same for all other delete and move keys
bool noSelection = false;
noSelection = true;
}
if (noSelection) {
if (success) {
} else { // nothing deleted
}
} else {
if (success) {
} else { // nothing deleted
}
}
_("Backspace"));
}
return TRUE;
case GDK_Delete:
case GDK_KP_Delete:
bool noSelection = false;
noSelection = true;
}
if (noSelection) {
} else {
if (success) {
} else { // nothing deleted
}
}
_("Delete"));
}
return TRUE;
case GDK_Left:
case GDK_KP_Left:
case GDK_KP_4:
if (MOD__ALT) {
if (MOD__SHIFT)
sp_te_adjust_kerning_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, Geom::Point(mul*-10, 0));
else
sp_te_adjust_kerning_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, Geom::Point(mul*-1, 0));
_("Kern to the left"));
} else {
if (MOD__CTRL)
else
cursor_moved = true;
break;
}
}
return TRUE;
case GDK_Right:
case GDK_KP_Right:
case GDK_KP_6:
if (MOD__ALT) {
if (MOD__SHIFT)
sp_te_adjust_kerning_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, Geom::Point(mul*10, 0));
else
sp_te_adjust_kerning_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, Geom::Point(mul*1, 0));
_("Kern to the right"));
} else {
if (MOD__CTRL)
else
cursor_moved = true;
break;
}
}
return TRUE;
case GDK_Up:
case GDK_KP_Up:
case GDK_KP_8:
if (MOD__ALT) {
if (MOD__SHIFT)
sp_te_adjust_kerning_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, Geom::Point(0, mul*-10));
else
sp_te_adjust_kerning_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, Geom::Point(0, mul*-1));
_("Kern up"));
} else {
if (MOD__CTRL)
else
cursor_moved = true;
break;
}
}
return TRUE;
case GDK_Down:
case GDK_KP_Down:
case GDK_KP_2:
if (MOD__ALT) {
if (MOD__SHIFT)
sp_te_adjust_kerning_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, Geom::Point(0, mul*10));
else
sp_te_adjust_kerning_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, Geom::Point(0, mul*1));
_("Kern down"));
} else {
if (MOD__CTRL)
else
cursor_moved = true;
break;
}
}
return TRUE;
case GDK_Home:
case GDK_KP_Home:
if (MOD__CTRL)
else
cursor_moved = true;
break;
}
return TRUE;
case GDK_End:
case GDK_KP_End:
if (MOD__CTRL)
else
cursor_moved = true;
break;
}
return TRUE;
case GDK_Page_Down:
case GDK_KP_Page_Down:
cursor_moved = true;
break;
}
return TRUE;
case GDK_Page_Up:
case GDK_KP_Page_Up:
cursor_moved = true;
break;
}
return TRUE;
case GDK_Escape:
}
} else {
}
return TRUE;
case GDK_bracketleft:
if (MOD__ALT) {
if (MOD__SHIFT) {
// FIXME: alt+shift+[] does not work, don't know why
} else {
}
} else {
}
_("Rotate counterclockwise"));
return TRUE;
}
}
break;
case GDK_bracketright:
if (MOD__ALT) {
if (MOD__SHIFT) {
// FIXME: alt+shift+[] does not work, don't know why
} else {
}
} else {
}
_("Rotate clockwise"));
return TRUE;
}
}
break;
case GDK_less:
case GDK_comma:
if (MOD__ALT) {
if (MOD__CTRL) {
if (MOD__SHIFT)
else
_("Contract line spacing"));
} else {
if (MOD__SHIFT)
sp_te_adjust_tspan_letterspacing_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, -10);
else
sp_te_adjust_tspan_letterspacing_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, -1);
_("Contract letter spacing"));
}
return TRUE;
}
}
break;
case GDK_greater:
case GDK_period:
if (MOD__ALT) {
if (MOD__CTRL) {
if (MOD__SHIFT)
else
_("Expand line spacing"));
} else {
if (MOD__SHIFT)
sp_te_adjust_tspan_letterspacing_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, 10);
else
sp_te_adjust_tspan_letterspacing_screen(tc->text, tc->text_sel_start, tc->text_sel_end, desktop, 1);
_("Expand letter spacing"));\
}
return TRUE;
}
}
break;
default:
break;
}
if (cursor_moved) {
if (!MOD__SHIFT)
}
return TRUE;
}
} else return TRUE; // return the "I took care of it" value if it was consumed by the IM
} else { // do nothing if there's no object to type in - the key will be sent to parent context,
if ((group0_keyval == GDK_Up ||
group0_keyval == GDK_Down ||
group0_keyval == GDK_KP_Up ||
group0_keyval == GDK_KP_Down )
&& !MOD__CTRL_ONLY) {
return TRUE;
}
}
return TRUE;
}
}
break;
}
case GDK_KEY_RELEASE:
return TRUE;
}
break;
default:
break;
}
// if nobody consumed it so far
if (((SPEventContextClass *) parent_class)->root_handler) { // and there's a handler in parent context,
return ((SPEventContextClass *) parent_class)->root_handler(event_context, event); // send event to parent
} else {
return FALSE; // return "I did nothing" value so that global shortcuts can be activated
}
}
/**
Attempts to paste system clipboard into the currently edited text, returns true on success
*/
bool
{
if (!SP_IS_TEXT_CONTEXT(ec))
return false;
// there is an active text object in this context, or a new object was just created
// Fix for 244940
// The XML standard defines the following as valid characters
// (Extensible Markup Language (XML) 1.0 (Fourth Edition) paragraph 2.2)
// char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
// Since what comes in off the paste buffer will go right into XML, clean
// the text here.
{
// Make sure we don't have a control character. We should really check
// for the whole range above... Add the rest of the invalid cases from
// above if we find additional issues
if(paste_string_uchar >= 0x00000020 ||
paste_string_uchar == 0x00000009 ||
paste_string_uchar == 0x0000000A ||
paste_string_uchar == 0x0000000D) {
itr++;
} else {
}
}
}
// using indices is slow in ustrings. Whatever.
for ( ; ; ) {
tc->text_sel_start = tc->text_sel_end = sp_te_replace(tc->text, tc->text_sel_start, tc->text_sel_end, text.substr(begin).c_str());
break;
}
tc->text_sel_start = tc->text_sel_end = sp_te_replace(tc->text, tc->text_sel_start, tc->text_sel_end, text.substr(begin, end - begin).c_str());
}
_("Paste text"));
return true;
}
} // FIXME: else create and select a new object under cursor!
return false;
}
/**
Gets the raw characters that comprise the currently selected text, converting line
breaks into lf characters.
*/
{
if (!SP_IS_TEXT_CONTEXT(ec))
return "";
return "";
}
{
if (!SP_IS_TEXT_CONTEXT(ec))
return NULL;
return NULL;
if (obj)
return NULL;
}
/**
Deletes the currently selected characters. Returns false if there is no
text selection currently.
*/
{
if (!SP_IS_TEXT_CONTEXT(ec))
return false;
return false;
return false;
if (success) {
} else { // nothing deleted
}
return true;
}
/**
* \param selection Should not be NULL.
*/
static void
{
}
}
if (layout)
} else {
}
// we update cursor without scrolling, because this position may not be final;
// item_handler moves cusros to the point of click immediately
sp_text_context_update_cursor(tc, false);
}
static void
sp_text_context_selection_modified(Inkscape::Selection */*selection*/, guint /*flags*/, SPTextContext *tc)
{
}
static bool
{
return false;
return false; // will get picked up by the parent and applied to the whole text object
_("Set text style"));
return true;
}
static int
{
return QUERY_STYLE_NOTHING;
}
return QUERY_STYLE_NOTHING;
}
} else {
}
if (!begin_it.prevCharacter()) {
}
}
void *rawptr = 0;
continue;
}
}
}
return result;
}
static void
{
return;
if (layout) { // undo can change the text length without us knowing it
}
}
static void
{
// due to interruptible display, tc may already be destroyed during a display update before
// the cursor update (can't do both atomically, alas)
// scroll to show cursor
if (scroll_to_see) {
// unlike mouse moves, here we must scroll all the way at first shot, so we override the autoscrollspeed
else
}
/* fixme: ... need another transformation to get canvas widget coordinate space? */
char const *trunc = "";
bool truncated = false;
if (layout->inputTruncated()) {
truncated = true;
trunc = _(" [truncated]");
}
if (frame) {
if (truncated) {
} else {
}
if (frame_bbox) {
}
}
SP_EVENT_CONTEXT(tc)->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("Type or edit flowed text (%d characters%s); <b>Enter</b> to start new paragraph."), nChars, trunc);
} else {
SP_EVENT_CONTEXT(tc)->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("Type or edit text (%d characters%s); <b>Enter</b> to start new line."), nChars, trunc);
}
} else {
if (!tc->nascent_object) {
SP_EVENT_CONTEXT(tc)->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to select or create text, <b>drag</b> to create flowed text; then type.")); // FIXME: this is a copy of string from tools-switch, do not desync
}
}
}
}
{
// due to interruptible display, tc may already be destroyed during a display update before
// the selection update (can't do both atomically, alas)
for (std::vector<SPCanvasItem*>::iterator it = tc->text_selection_quads.begin() ; it != tc->text_selection_quads.end() ; it++) {
}
quads = sp_te_create_selection_quads(tc->text, tc->text_sel_start, tc->text_sel_end, (tc->text)->i2d_affine());
// FIXME: make the color settable in prefs
// for now, use semitrasparent blue, as cairo cannot do inversion :(
sp_ctrlquadr_set_coords(SP_CTRLQUADR(quad_canvasitem), quads[i], quads[i+1], quads[i+2], quads[i+3]);
}
}
static gint
{
} else {
}
}
return TRUE;
}
static void
{
(void)ti;
/* We have to set it to zero,
* or selection changed signal messes everything up */
/* FIXME: this automatic deletion when nothing is inputted crashes the XML edittor and also crashes when duplicating an empty flowtext.
So don't create an empty flowtext in the first place? Create it when first character is typed.
*/
/*
if ((SP_IS_TEXT(ti) || SP_IS_FLOWTEXT(ti)) && sp_te_input_is_empty(ti)) {
Inkscape::XML::Node *text_repr = ti->getRepr();
// the repr may already have been unparented
// if we were called e.g. as the result of
// an undo or the element being removed from
// the XML editor
if ( text_repr && sp_repr_parent(text_repr) ) {
sp_repr_unparent(text_repr);
SPDocumentUndo::done(sp_desktop_document(tc->desktop), SP_VERB_CONTEXT_TEXT,
_("Remove empty text"));
}
}
*/
}
{
return FALSE;
}
{
return FALSE;
}
static void
{
}
tc->text_sel_start = tc->text_sel_end = sp_te_replace(tc->text, tc->text_sel_start, tc->text_sel_end, string);
_("Type text"));
}
void
sp_text_context_place_cursor (SPTextContext *tc, SPObject *text, Inkscape::Text::Layout::iterator where)
{
}
void
{
}
Inkscape::Text::Layout::iterator *sp_text_context_get_cursor_position(SPTextContext *tc, SPObject *text)
{
return NULL;
return &(tc->text_sel_end);
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :