~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to lib/kotext/KoTextView.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2001-2006 David Faure <faure@kde.org>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#ifndef kotextview_h
 
21
#define kotextview_h
 
22
 
 
23
#include <qobject.h>
 
24
#include <qpoint.h>
 
25
#include <qcolor.h>
 
26
#include <qfont.h>
 
27
#include <KoRuler.h> // for KoTabulatorList
 
28
#include <KoTextObject.h> // for KoTextView
 
29
#include <koffice_export.h>
 
30
class KoBgSpellCheck;
 
31
class KoTextObject;
 
32
class KoTextDocument;
 
33
class KoTextParag;
 
34
class KoTextFormat;
 
35
class KoParagCounter;
 
36
class KCommand;
 
37
class QTimer;
 
38
class KAction;
 
39
class KInstance;
 
40
class KDataToolInfo;
 
41
class KoLinkVariable;
 
42
class KoVariable;
 
43
class KoTextViewIface;
 
44
#include "KoRichText.h"
 
45
class KoBorder;
 
46
class KoParagStyle;
 
47
 
 
48
/**
 
49
 * Object that is created to edit a KoTextObject.
 
50
 * It handles all the events for it (mouse, keyboard).
 
51
 * There can be several KoTextView instances for the same KoTextObject.
 
52
 */
 
53
class KOTEXT_EXPORT KoTextView : public QObject, public KoTextFormatInterface
 
54
{
 
55
    Q_OBJECT
 
56
public:
 
57
    /**
 
58
     * Constructor. A KoTextView is associated to a KoTextObject.
 
59
     * Don't forget to call updateUI(true,true) in your derived constructor
 
60
     */
 
61
    KoTextView( KoTextObject *textobj );
 
62
    virtual ~KoTextView();
 
63
 
 
64
    void setBackSpeller( KoBgSpellCheck* backSpeller );
 
65
 
 
66
    virtual KoTextViewIface* dcopObject();
 
67
 
 
68
    /** Call this before deleting */
 
69
    /** don't remove selection when we made dnd between different frame*/
 
70
    void terminate( bool removeselection=true );
 
71
 
 
72
    KoTextObject * textObject() const { return m_textobj; }
 
73
    KoTextCursor * cursor() const { return m_cursor; }
 
74
    void setCursor( const KoTextCursor& cursor ) { *m_cursor = cursor; };
 
75
    KoTextDocument * textDocument() const;
 
76
 
 
77
    /** Return true if the view is allowed to modify the text object.
 
78
        This is the case by default */
 
79
    bool isReadWrite() const { return m_bReadWrite; }
 
80
    /** Call setReadWrite(false) to make the text view readonly */
 
81
    void setReadWrite( bool b ) { m_bReadWrite = b; }
 
82
 
 
83
    virtual KoTextFormat * currentFormat() const { return m_currentFormat; }
 
84
    void setCurrentFormat( KoTextFormat *fmt ) { m_currentFormat = fmt; }
 
85
 
 
86
    /**
 
87
     * Use this format for displaying the properties (Align/counter/...) of the object
 
88
     */
 
89
    virtual const KoParagLayout * currentParagLayoutFormat() const;
 
90
 
 
91
    virtual bool rtl() const;
 
92
 
 
93
    virtual KCommand *setChangeCaseOfTextCommand( KoChangeCaseDia::TypeOfCase _type );
 
94
 
 
95
    virtual KCommand* setParagLayoutFormatCommand( KoParagLayout *newLayout, int flags, int marginIndex = -1 );
 
96
 
 
97
    /** Implement the KoTextFormatInterface */
 
98
    virtual KCommand* setFormatCommand( const KoTextFormat * newFormat, int flags, bool zoomFont = false );
 
99
 
 
100
    // -- Paragraph settings --
 
101
    KCommand * setCounterCommand( const KoParagCounter & counter );
 
102
    KCommand * setAlignCommand( int align );
 
103
    KCommand * setPageBreakingCommand( int pageBreaking );
 
104
    KCommand * setLineSpacingCommand( double spacing, KoParagLayout::SpacingType _type );
 
105
    KCommand * setBordersCommand( const KoBorder& leftBorder, const KoBorder& rightBorder, const KoBorder& bottomBorder, const KoBorder& topBorder );
 
106
    KCommand * setJoinBordersCommand( bool join );
 
107
    KCommand * setMarginCommand( QStyleSheetItem::Margin m, double margin );
 
108
    KCommand * setTabListCommand( const KoTabulatorList & tabList );
 
109
    KCommand * setBackgroundColorCommand( const QColor & color );
 
110
    void applyStyle( const KoParagStyle * style );
 
111
 
 
112
    void dragStarted();
 
113
    void focusInEvent();
 
114
    void focusOutEvent();
 
115
    void handleKeyPressEvent( QKeyEvent * e, QWidget *, const QPoint& );
 
116
    void handleKeyReleaseEvent( QKeyEvent * e );
 
117
    void handleImStartEvent( QIMEvent * e );
 
118
    void handleImComposeEvent( QIMEvent * e );
 
119
    void handleImEndEvent( QIMEvent * e );
 
120
    // iPoint is in Layout Unit pixels
 
121
    // return true if we add new parag with "insert direct cursor"
 
122
    bool handleMousePressEvent( QMouseEvent* e, const QPoint& iPoint, bool canStartDrag = true, bool insertDirectCursor = false );
 
123
    void handleMouseMoveEvent( QMouseEvent* e, const QPoint& iPoint );
 
124
    void handleMouseReleaseEvent();
 
125
    void handleMouseDoubleClickEvent( QMouseEvent* e, const QPoint& iPoint );
 
126
    void handleMouseTripleClickEvent( QMouseEvent* e, const QPoint& /* Currently unused */ );
 
127
    bool maybeStartDrag( QMouseEvent* e );
 
128
 
 
129
    KoTextCursor selectWordUnderCursor( const KoTextCursor& cursor, int selectionId = KoTextDocument::Standard );
 
130
    KoTextCursor selectParagUnderCursor( const KoTextCursor& cursor, int selectionId = KoTextDocument::Standard, bool copyAndNotify = true );
 
131
    void extendParagraphSelection( const QPoint& iPoint );
 
132
 
 
133
    QString wordUnderCursor( const KoTextCursor& cursor );
 
134
 
 
135
    /** Return the list of actions from data-tools. Used to populate a RMB popupmenu usually. */
 
136
    QPtrList<KAction> dataToolActionList( KInstance * instance, const QString& word, bool & _singleWord );
 
137
 
 
138
    void insertSoftHyphen();
 
139
    void insertLineBreak();
 
140
    void insertNonbreakingSpace();
 
141
    void insertNonbreakingHyphen();
 
142
    void increaseNumberingLevel( const KoStyleCollection* styleCollection );
 
143
    void decreaseNumberingLevel( const KoStyleCollection* styleCollection );
 
144
    void insertSpecialChar( QChar _c, const QString& font );
 
145
    void changeCaseOfText( KoChangeCaseDia::TypeOfCase _type );
 
146
 
 
147
    void addBookmarks( const QString& );
 
148
 
 
149
    //return a pointer to the variable under the cursor, if any
 
150
    KoVariable *variable();
 
151
    //return a pointer to the link variable under the cursor, if any
 
152
    // (special case of variable())
 
153
    KoLinkVariable *linkVariable();
 
154
 
 
155
    /// A "drop move" will move the selected text to the given cursor position.
 
156
    /// This method only takes care of "removing the selected text"
 
157
    /// if the drop cursor isn't in the selected text (and !protectContent).
 
158
    KCommand *prepareDropMove( KoTextCursor dropCursor );
 
159
 
 
160
    void removeComment();
 
161
    void copyTextOfComment();
 
162
 
 
163
    // This is in fact "from selection or cursor"
 
164
    KoParagStyle * createStyleFromSelection( const QString & name );
 
165
    void updateStyleFromSelection( KoParagStyle* style );
 
166
 
 
167
    QString currentWordOrSelection() const;
 
168
 
 
169
    virtual void removeToolTipCompletion() {}
 
170
 
 
171
    // return true if we "insert direct cursor" and we insert new parag
 
172
    bool placeCursor( const QPoint &pos /* in internal coordinates */, bool insertDirectCursor=false );
 
173
    void setOverwriteMode( bool overwriteMode );
 
174
 
 
175
public slots:
 
176
    /** Show the current settings (those for the paragraph and character under the cursor), in the GUI.
 
177
     * The default implementation handles m_currentFormat and calls showCurrentFormat().
 
178
     * If you reimplement, make sure to call KoTextView::updateUI(updateFormat,force); */
 
179
    virtual void updateUI( bool updateFormat, bool force = false );
 
180
    virtual void ensureCursorVisible() = 0;
 
181
    void showCurrentFormat();
 
182
 
 
183
    // This allows KoTextObject to hide/show all the cursors before modifying anything
 
184
    void hideCursor() { drawCursor( false ); }
 
185
    void showCursor() { drawCursor( true ); }
 
186
 
 
187
    /** This is a slot so that it's accessible from DCOP */
 
188
    void insertText( const QString &text );
 
189
    void newParagraph();
 
190
 
 
191
    void copyLink();
 
192
    void removeLink();
 
193
    void completion();
 
194
 
 
195
    void setCursor( KoTextCursor * _cursor ) { *m_cursor = *_cursor; }
 
196
 
 
197
protected slots:
 
198
    /** Start a drag */
 
199
    virtual void startDrag() = 0;
 
200
    void slotToolActivated( const KDataToolInfo & info, const QString & command );
 
201
signals:
 
202
    void copy();
 
203
    void cut();
 
204
    void paste();
 
205
 
 
206
protected:
 
207
    /**
 
208
     * Called when a character (@p ch) has been inserted into @p parag, at the given @p index.
 
209
     * This is a virtual method rather than a signal for performance reasons.
 
210
     */
 
211
    virtual void doAutoFormat( KoTextCursor* /*cursor*/, KoTextParag * /*parag*/,
 
212
                               int /*index*/, QChar /*ch*/ ) { }
 
213
 
 
214
    virtual bool doCompletion( KoTextCursor* , KoTextParag *, int  ) { return false; }
 
215
    virtual bool doToolTipCompletion( KoTextCursor* , KoTextParag *, int, int  ) { return false; }
 
216
    virtual void showToolTipBox( KoTextParag *, int , QWidget *, const QPoint& ) {}
 
217
 
 
218
    virtual void textIncreaseIndent() {}
 
219
    virtual bool textDecreaseIndent() { return true; }
 
220
 
 
221
    //return true if we are a doubleSpace
 
222
    virtual bool doIgnoreDoubleSpace(KoTextParag * /*parag*/,
 
223
        int /*index*/,QChar /*ch*/ ) { return false;}
 
224
 
 
225
    /** Show the settings of this format in the GUI. Needs to be implemented in the application. */
 
226
    virtual void showFormat( KoTextFormat *format ) = 0;
 
227
 
 
228
    /** Draws the cursor (or hides it if b is false).
 
229
     * The default implementation only changes an internal flag, make sure to reimplement
 
230
     * and to call the parent implementation (in all cases)
 
231
     */
 
232
    virtual void drawCursor( bool b );
 
233
 
 
234
    /// Called when clicking on a link
 
235
    virtual bool openLink( KoLinkVariable* linkVariable );
 
236
 
 
237
    /** Reimplement this to handle PageUp. Example implementation:
 
238
        textView->cursor()->gotoPageUp( scrollview->visibleHeight() ); */
 
239
    virtual bool pgUpKeyPressed() = 0;
 
240
    /** Reimplement this to handle PageDown. Example implementation:
 
241
        textView->cursor()->gotoPageDown( scrollview->visibleHeight() ); */
 
242
    virtual bool pgDownKeyPressed() = 0;
 
243
    /** Reimplement this to handle CTRL+PageUp. Default implementation calls pgUpKeyPressed */
 
244
    virtual void ctrlPgUpKeyPressed() { pgUpKeyPressed(); }
 
245
    /** Reimplement this to handle CTRL+PageDown. Default implementation calls pgDownKeyPressed */
 
246
    virtual void ctrlPgDownKeyPressed() { pgDownKeyPressed(); }
 
247
 
 
248
    void deleteWordLeft();
 
249
    void deleteWordRight();
 
250
    bool insertParagraph( const QPoint &pos );
 
251
 
 
252
private slots:
 
253
    void blinkCursor();
 
254
    void tripleClickTimeout();
 
255
    void afterTripleClickTimeout();
 
256
protected:
 
257
    KoTextViewIface *dcop;
 
258
 public: // necessary to be public to allow script action in KoTextViewIface
 
259
    enum CursorAction { // keep in sync with QTextEdit
 
260
        MoveBackward,
 
261
        MoveForward,
 
262
        MoveWordBackward,
 
263
        MoveWordForward,
 
264
        MoveUp,
 
265
        MoveDown,
 
266
        MoveLineStart,
 
267
        MoveLineEnd,
 
268
        MoveHome,
 
269
        MoveEnd,
 
270
        MovePgUp,
 
271
        MovePgDown,
 
272
        MoveParagUp, // libkotext-specific
 
273
        MoveParagDown, // libkotext-specific
 
274
        MoveViewportUp, // KWord-specific
 
275
        MoveViewportDown // KWord-specific
 
276
    };
 
277
 
 
278
    void moveCursor( CursorAction action, bool select );
 
279
    bool moveCursor( CursorAction action );
 
280
 
 
281
private:
 
282
    KoTextObject *m_textobj;
 
283
    KoTextCursor *m_cursor;
 
284
 
 
285
    // Store the index of the variable on which we last clicked, to position m_cursor
 
286
    int m_variablePosition;
 
287
    bool m_overwriteMode;
 
288
 
 
289
    KoTextFormat *m_currentFormat;
 
290
    QTimer *blinkTimer, *dragStartTimer;
 
291
    class KoTextViewPrivate;
 
292
    KoTextViewPrivate *d;
 
293
    QPoint dragStartPos;
 
294
    bool m_cursorVisible;
 
295
    bool blinkCursorVisible;
 
296
    bool inDoubleClick;
 
297
    bool mightStartDrag;
 
298
    bool m_bReadWrite;
 
299
    bool possibleTripleClick;
 
300
    bool afterTripleClick;
 
301
 
 
302
    bool m_singleWord;
 
303
    QString m_wordUnderCursor;
 
304
};
 
305
 
 
306
#endif