summaryrefslogtreecommitdiffstats
path: root/kbabel/kbabel/mymultilineedit.h
blob: ba6a96e610e4269b5531f4859b7e116f0aa51eaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 1999-2000 by Matthias Kiefer
                            <matthias.kiefer@gmx.de>
		2001-2003 by Stanislav Visnovsky
			    <visnovsky@kde.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  In addition, as a special exception, the copyright holders give
  permission to link the code of this program with any edition of
  the TQt library by Trolltech AS, Norway (or with modified versions
  of TQt that use the same license as TQt), and distribute linked
  combinations including the two.  You must obey the GNU General
  Public License in all respects for all of the code used other than
  TQt. If you modify this file, you may extend this exception to
  your version of the file, but you are not obligated to do so.  If
  you do not wish to do so, delete this exception statement from
  your version.

**************************************************************************** */
#ifndef MYMULTILINEEDIT_H
#define MYMULTILINEEDIT_H

#include <ktextedit.h>
#include <tqptrvector.h>

namespace KBabel
{
    class EditCommand;
}

class KBabelHighlighter;
class KSpell;
class TQPixmap;

class MyMultiLineEdit : public KTextEdit
{
   Q_OBJECT
  TQ_OBJECT
public:
   MyMultiLineEdit(int ID,TQWidget* parent,const char* name=0);

   /**
      applies cmd to the displayed text, but does not emit
      signalUndoCommand
   */
   void processCommand(KBabel::EditCommand* cmd, bool undo=false);

   /** 
	* @returns the position in text, where the marked text begins
	* -1, if there is no marked text
	*/
   int beginOfMarkedText();

    /** 
    * @returns the position in text, where the last marked text began
    * or the current cursor position if there was no marked text.
    * This is used for getting the start position for a text replacement
    */
   int beginOfLastMarkedText();

    /** 
    * @returns the position in text, where the last marked text ended
    * or the current cursor position if there was no marked text.
    * This is used for getting the end position for a text replacement
    */
   int endOfLastMarkedText();

   virtual void insertAt ( const TQString & s, int line, int col, bool mark = false );
   virtual void removeLine ( int line );

   int pos2Offset(uint paragraph, uint index);
   void offset2Pos(int offset, int &row, int &col) const;
   /** 
	* @returns the current position in text, where the cursor is
	*/

   int currentIndex();
   /** 
   * processes Del key
   */
   void my_del();
   void my_backspace();
   
   /**
    * need to override deleting of popup menus :-(
    */
   void contentsContextMenuEvent( TQContextMenuEvent *e );
   
   /**
    * need to reimplement overwrite mode :-(
    */
   bool isOverwriteMode() { return _overwrite; }    

public slots:
   
   virtual void clear();
   virtual void paste();
   virtual void setReadOnly(bool on);
   virtual void setContextMenu( TQPopupMenu *menu );
   virtual void setText(const TQString& s);
   virtual void doKeyboardAction( KeyboardAction action );
   virtual void removeSelectedText(int selNum = 0);

   virtual void onSelectionChanged();
   
   /**
   reimplemented overwrite mode, since TQTextEdit handles this internally and does
   not use any accessible virtual methods :-((.
   */
   virtual void setOverwriteMode(bool b);

protected:

   virtual void focusInEvent(TQFocusEvent*);
   virtual TQPopupMenu *createPopupMenu();
   virtual TQPopupMenu *createPopupMenu(const TQPoint &pos);
   
   /* the parent handles this */
   virtual void wheelEvent(TQWheelEvent*);

   bool emitUndo;

   /* First and the last line of the last change. They are only approximate. Used for faster display
    * highlighting etc.
    */
   uint _firstChangedLine;
   uint _lastChangedLine;
   
   /* This is a cache. _lastPragraphOffset always correctly corresponds to _lastParagraphOffset
    */
   uint _lastParagraph;
   uint _lastParagraphOffset;

   /* We save the last selection positions. This is needed when a tag is inserted to get the
      left cursor position of the originally used selection */
   int _lastSelectionStart;
   int _lastSelectionEnd;

   /* flag to skip any work on updating, since it will be more changes */
   bool _dontUpdate;

protected slots:
   virtual void insert ( const TQString & text, bool indent = FALSE, bool checkNewLine = TRUE, bool removeSelected = TRUE );
   virtual void emitCursorPosition();

signals:
   void signalUndoCmd(KBabel::EditCommand*);
   void signalSyntaxHighlightingChanged (bool enable);

protected:
   int _myID;
   
private:
   TQPopupMenu *_menu;
   bool _overwrite;
};


class MsgMultiLineEdit : public MyMultiLineEdit
{   
   Q_OBJECT
  TQ_OBJECT
public:
   enum TextColor { NormalColor, ErrorColor };

   MsgMultiLineEdit(int ID, KSpell* spell=0, TQWidget* parent=0,const char* name=0);
   virtual ~MsgMultiLineEdit();

   /** is displaying surrounding quotes enabled? */
   bool quotes() const { return _quotes;}
   /** enable or disable displaying of surrounding quotes */
   void setQuotes(bool on);

   /** is clever editing enabled? */
   bool cleverEditing() const { return _cleverEditing; }
   /** enable or disable clever editing */
   void setCleverEditing(bool on);
   /** is highlighting background enabled? */
   bool highlightBg() const { return _highlightBg; }
   /** enable or disable highlighting background*/
   void setHighlightBg(bool on);
   TQColor bgColor() const { return _bgColor; }
   void setBgColor(const TQColor& color);

   bool spacePoints() const { return _spacePoints; }
   void setSpacePoints(bool on);

   bool highlightSyntax() const { return _hlSyntax; }
   void highlight();
   void setHighlightSyntax(bool on);
   void setHighlightColors(const TQColor& quoteColor, const TQColor& unquoteColor
             , const TQColor& cformatColor, const TQColor& accelColor, const TQColor& tagColor);

   void setFont(const TQFont& font);

   void setDiffMode(bool on);
   void setDiffDisplayMode(bool underlineAdded, bool strikeOutDeleted);
   void setDiffColors(const TQColor& addColor, const TQColor& delColor);
   
   void setTextColor(const TQColor &color);
   void setErrorColor(const TQColor &color);
   
   void setCurrentColor(const TextColor color);
   
   void setSpellChecker(KSpell* spell);
   
   void selectTag(int start, int length);
   
public slots:
   virtual void setText(const TQString& s);
   void paintSpacePoints();
   void paintSpacePoints( int para, int pos ); // overloaded for signal TQTextEdit::cursorPositionChanged

   /**
   *  reimplemented to call highlight()
   */
   void repaint(); 
   void forceUpdate();
   void emittedTextChanged();
   
   /**
   * reimplemented to skip in case of disabled updates
   */
   void ensureCursorVisible();
   
protected:
   virtual void paintEvent (TQPaintEvent * event );
   virtual void drawContents( TQPainter *painter, int clipx, int clipy, int clipw, int cliph );
   
   virtual void keyPressEvent(TQKeyEvent*);
   virtual void keyReleaseEvent(TQKeyEvent*);

private:
   /**
   * Computes the pixel position in line which corresponds to
   * character position xIndex
   */
   TQRect mapToView( int para, int index );

   /**
    * tests if the character in string str at position col is masked with
    * '\' by counting the number of '\' backwards
    */
   static bool isMasked(TQString *str,uint col);

private:
   bool _quotes;
   bool _cleverEditing;
   bool _highlightBg;
   bool _spacePoints;
   TQColor _bgColor;
   TQColor _textColor;
   TQColor _errorColor;
   TQColor _currentColor;
   
   TQPixmap* _whitespace;
   TQPixmap* _whitespaceNB;
   TQPixmap* _errorWhitespace;
   TQPixmap* _errorWhitespaceNB;
   
   int _wsOffsetX;
   int _wsOffsetY;

   bool _hlSyntax;
   TQColor _quoteColor;
   TQColor _unquoteColor;
   TQColor _cformatColor;
   TQColor _accelColor;
   TQColor _tagColor;

   struct DiffInfo
   {
       bool add;
       int begin;
       int end;
   };

   TQPtrVector< TQValueList<DiffInfo> > diffPos;
   bool _showDiff;
   bool _diffUnderlineAdd;
   bool _diffStrikeOutDel;
   TQColor _diffAddColor;
   TQColor _diffDelColor;
   
   // for Alt+123 feature
   int _currentUnicodeNumber;
   
   KBabelHighlighter * highlighter;
   
   // next tag highlighting
   int _tagStartPara, _tagStartIndex, _tagEndPara, _tagEndIndex;
};

#endif // MYMULTILINEEDIT_H