3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 Copyright (c) 2005 Kazuki Ohta <mover@hct.zaq.ne.jp>
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define DEBUG_KP_TOOL_TEXT 0
32 #include <kpToolText.h>
33 #include <kpToolTextPrivate.h>
42 #include <kpDocument.h>
43 #include <kpTextSelection.h>
44 #include <kpToolTextBackspaceCommand.h>
45 #include <kpToolTextChangeStyleCommand.h>
46 #include <kpToolSelectionCreateCommand.h>
47 #include <kpToolSelectionEnvironment.h>
48 #include <kpToolTextDeleteCommand.h>
49 #include <kpToolTextEnterCommand.h>
50 #include <kpToolTextInsertCommand.h>
51 #include <kpToolWidgetOpaqueOrTransparent.h>
53 #include <kpViewManager.h>
56 // COMPAT: Need to update InputMethod support.
58 // This code needs lots of updating not just to Qt/KDE4 but also to changes
59 // in KolourPaint internals.
62 void kpToolText::imStartEvent (QIMEvent
*e
)
64 #if DEBUG_KP_TOOL_TEXT && 1
65 kDebug () << "kpToolText::imStartEvent() text='" << e
->text ()
66 << " cursorPos=" << e
->cursorPos ()
67 << " selectionLength=" << e
->selectionLength ()
71 kpTextSelection
*textSel
= document ()->textSelection ();
72 if (hasBegunDraw() || !textSel
)
78 d
->IMStartCursorRow
= viewManager ()->textCursorRow ();
79 d
->IMStartCursorCol
= viewManager ()->textCursorCol ();
80 d
->IMPreeditStr
.clear ();
83 void kpToolText::imComposeEvent (QIMEvent
*e
)
85 #if DEBUG_KP_TOOL_TEXT && 1
86 kDebug () << "kpToolText::imComposeEvent() text='" << e
->text ()
87 << " cursorPos=" << e
->cursorPos ()
88 << " selectionLength=" << e
->selectionLength ()
92 kpTextSelection
*textSel
= document ()->textSelection ();
93 if (hasBegunDraw() || !textSel
)
100 if (d
->IMPreeditStr
.length() > 0 )
102 // set cursor at the start input point
103 viewManager ()->setTextCursorPosition (d
->IMStartCursorRow
, d
->IMStartCursorCol
);
104 for (unsigned int i
= 0; i
< d
->IMPreeditStr
.length(); i
++)
106 if (!d
->deleteCommand
)
108 if (hasBegunShape ())
109 endShape (currentPoint (), normalizedRect ());
111 d
->deleteCommand
= new kpToolTextDeleteCommand (i18n ("Text: Delete"),
112 viewManager ()->textCursorRow (), viewManager ()->textCursorCol (),
113 kpToolTextDeleteCommand::AddDeleteNow
,
114 environ ()->commandEnvironment ());
115 commandHistory ()->addCommand (d
->deleteCommand
, false/*no exec*/);
118 d
->deleteCommand
->addDelete ();
122 // insert new preedit
123 d
->IMPreeditStr
= e
->text();
124 if (d
->IMPreeditStr
.length() > 0)
126 if (!d
->insertCommand
)
128 if (hasBegunShape ())
129 endShape (currentPoint (), normalizedRect ());
131 d
->insertCommand
= new kpToolTextInsertCommand (i18n ("Text: Write"),
132 viewManager ()->textCursorRow (), viewManager ()->textCursorCol (),
134 environ ()->commandEnvironment ());
135 commandHistory ()->addCommand (d
->insertCommand
, false/*no exec*/);
138 d
->insertCommand
->addText (d
->IMPreeditStr
);
142 if (d
->IMStartCursorRow
>= 0)
144 int row
= d
->IMStartCursorRow
;
145 int col
= d
->IMStartCursorCol
+ e
->cursorPos () /* + e->selectionLength()*/;
146 viewManager ()->setTextCursorPosition (row
, col
, true /* update MicroFocusHint */);
150 void kpToolText::imEndEvent (QIMEvent
*e
)
152 #if DEBUG_KP_TOOL_TEXT && 1
153 kDebug () << "kpToolText::imEndEvent() text='" << e
->text ()
154 << " cursorPos=" << e
->cursorPos ()
155 << " selectionLength=" << e
->selectionLength ()
159 kpTextSelection
*textSel
= document ()->textSelection ();
160 if (hasBegunDraw() || !textSel
)
166 // remove old preedit
167 if (d
->IMPreeditStr
.length() > 0 )
169 // set cursor at the start input point
170 viewManager ()->setTextCursorPosition (d
->IMStartCursorRow
, d
->IMStartCursorCol
);
171 for (unsigned int i
= 0; i
< d
->IMPreeditStr
.length(); i
++)
173 if (!d
->deleteCommand
)
175 if (hasBegunShape ())
176 endShape (currentPoint (), normalizedRect ());
178 d
->deleteCommand
= new kpToolTextDeleteCommand (i18n ("Text: Delete"),
179 viewManager ()->textCursorRow (),
180 viewManager ()->textCursorCol (),
181 kpToolTextDeleteCommand::AddDeleteNow
,
182 environ ()->commandEnvironment ());
183 commandHistory ()->addCommand (d
->deleteCommand
, false/*no exec*/);
186 d
->deleteCommand
->addDelete ();
189 d
->IMPreeditStr
.clear ();
192 QString inputStr
= e
->text();
193 if (inputStr
.length() > 0)
195 if (!d
->insertCommand
)
197 if (hasBegunShape ())
198 endShape (currentPoint (), normalizedRect ());
200 d
->insertCommand
= new kpToolTextInsertCommand (i18n ("Text: Write"),
201 viewManager ()->textCursorRow (), viewManager ()->textCursorCol (),
203 environ ()->commandEnvironment ());
204 commandHistory ()->addCommand (d
->insertCommand
, false/*no exec*/);
207 d
->insertCommand
->addText (inputStr
);