summaryrefslogtreecommitdiff
path: root/noncore/apps/tinykate/libkate/document/katedocument.h
Unidiff
Diffstat (limited to 'noncore/apps/tinykate/libkate/document/katedocument.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/tinykate/libkate/document/katedocument.h569
1 files changed, 569 insertions, 0 deletions
diff --git a/noncore/apps/tinykate/libkate/document/katedocument.h b/noncore/apps/tinykate/libkate/document/katedocument.h
new file mode 100644
index 0000000..220d188
--- a/dev/null
+++ b/noncore/apps/tinykate/libkate/document/katedocument.h
@@ -0,0 +1,569 @@
1/***************************************************************************
2 katedocument.h - description
3 -------------------
4 begin : Mon Jan 15 2001
5 copyright : (C) 2001 by Christoph "Crossfire" Cullmann
6 (C) 2002 by Joseph Wenninger
7 email : crossfire@babylon2k.de
8 jowenn@kde.org
9
10***************************************************************************/
11
12/***************************************************************************
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 ***************************************************************************/
20
21/*
22 Copyright (C) 1998, 1999 Jochen Wilhelmy
23 digisnap@cs.tu-berlin.de
24
25 This library is free software; you can redistribute it and/or
26 modify it under the terms of the GNU Library General Public
27 License as published by the Free Software Foundation; either
28 version 2 of the License, or (at your option) any later version.
29
30 This library is distributed in the hope that it will be useful,
31 but WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 Library General Public License for more details.
34
35 You should have received a copy of the GNU Library General Public License
36 along with this library; see the file COPYING.LIB. If not, write to
37 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
38 Boston, MA 02111-1307, USA.
39*/
40
41#ifndef kate_document_h
42#define kate_document_h
43
44
45#include <qobject.h>
46#include <qlist.h>
47#include <qcolor.h>
48#include <qfont.h>
49#include <qfontmetrics.h>
50#include <qdatetime.h>
51#include <qfileinfo.h>
52
53#include "../view/kateview.h"
54#include "katehighlight.h"
55#include "katebuffer.h"
56#include "katetextline.h"
57
58
59#include <qptrdict.h>
60
61class KateCmd;
62
63class CachedFontMetrics : public QFontMetrics {
64private:
65 short *warray[256];
66public:
67 CachedFontMetrics(const QFont& f) : QFontMetrics(f) {
68 for (int i=0; i<256; i++) warray[i]=0;
69 }
70 ~CachedFontMetrics() {
71 for (int i=0; i<256; i++)
72 if (warray[i]) delete[] warray[i];
73 }
74 int width(QChar c) {
75 uchar cell=c.cell();
76 uchar row=c.row();
77 short *wa=warray[row];
78 if (!wa) {
79 // qDebug("create row: %d",row);
80 wa=warray[row]=new short[256];
81 for (int i=0; i<256; i++) wa[i]=-1;
82 }
83 if (wa[cell]<0) wa[cell]=(short) QFontMetrics::width(c);
84 return (int)wa[cell];
85 }
86 int width(QString s) { return QFontMetrics::width(s); }
87};
88
89class Attribute {
90 public:
91 Attribute() { ; };
92
93 QColor col;
94 QColor selCol;
95 bool bold;
96 bool italic;
97};
98
99class KateAction {
100 public:
101 enum Action {replace, wordWrap, wordUnWrap, newLine, delLine,
102 insLine, killLine};//, doubleLine, removeLine};
103
104 KateAction(Action, PointStruc &cursor, int len = 0,
105 const QString &text = QString::null);
106
107 Action action;
108 PointStruc cursor;
109 int len;
110 QString text;
111 KateAction *next;
112};
113
114class KateActionGroup {
115 public:
116 // the undo group types
117 enum { ugNone, //
118 ugPaste, // paste
119 ugDelBlock, // delete/replace selected text
120 ugIndent, // indent
121 ugUnindent, // unindent
122 ugComment, // comment
123 ugUncomment, // uncomment
124 ugReplace, // text search/replace
125 ugSpell, // spell check
126 ugInsChar, // char type/deleting
127 ugDelChar, // '' ''
128 ugInsLine, // line insert/delete
129 ugDelLine // '' ''
130 };
131
132 KateActionGroup(PointStruc &aStart, int type = ugNone);
133 ~KateActionGroup();
134 void insertAction(KateAction *);
135
136 static const char * typeName(int type);
137
138 PointStruc start;
139 PointStruc end;
140 KateAction *action;
141 int undoType;
142};
143
144/**
145 The text document. It contains the textlines, controls the
146 document changing operations and does undo/redo. WARNING: do not change
147 the text contents directly in methods where this is not explicitly
148 permitted. All changes have to be made with some basic operations,
149 which are recorded by the undo/redo system.
150 @see TextLine
151 @author Jochen Wilhelmy
152*/
153class KateDocument: public Kate::Document
154{
155 Q_OBJECT
156 friend class KateViewInternal;
157 friend class KateView;
158 friend class KateIconBorder;
159
160 public:
161 KateDocument(bool bSingleViewMode=false, bool bBrowserView=false, QWidget *parentWidget = 0, const char *widgetName = 0, QObject * = 0, const char * = 0);
162 ~KateDocument();
163
164 protected:
165 QFont myFont, myFontBold, myFontItalic, myFontBI;
166 CachedFontMetrics myFontMetrics, myFontMetricsBold, myFontMetricsItalic, myFontMetricsBI;
167
168 public:
169 void setFont (QFont font);
170 QFont getFont () { return myFont; };
171 CachedFontMetrics getFontMetrics () { return myFontMetrics; };
172
173 virtual bool saveFile();
174
175 virtual KTextEditor::View *createView( QWidget *parent, const char *name );
176 virtual QString textLine( int line ) const;
177
178 virtual void insertLine( const QString &s, int line = -1 );
179
180 void insert_Line(const QString& s,int line=-1, bool update=true);
181 void remove_Line(int line,bool update=true);
182 void replaceLine(const QString& s,int line=-1);
183 virtual void insertAt( const QString &s, int line, int col, bool mark = FALSE );
184 virtual void removeLine( int line );
185 virtual int length() const;
186
187 virtual void setSelection( int row_from, int col_from, int row_to, int col_t );
188 virtual bool hasSelection() const;
189 virtual QString selection() const;
190
191 // only to make part work, don't change it !
192 bool m_bSingleViewMode;
193
194// public interface
195 /**
196 * gets the number of lines
197 */
198 virtual int numLines() const;
199
200 /**
201 * gets the last line number (numLines() -1)
202 */
203 int lastLine() const {return numLines()-1;}
204
205 /**
206 gets the given line
207 @return the TextLine object at the given line
208 @see TextLine
209 */
210 TextLine::Ptr getTextLine(int line) const;
211
212 /**
213 get the length in pixels of the given line
214 */
215 int textLength(int line);
216
217 void setTabWidth(int);
218 int tabWidth() {return tabChars;}
219 void setReadOnly(bool);
220 bool isReadOnly() const;
221 void setNewDoc( bool );
222 bool isNewDoc() const;
223 virtual void setReadWrite( bool ){};
224 virtual bool isReadWrite() const {return true;}
225 virtual void setModified(bool);
226 virtual bool isModified() const;
227 void setSingleSelection(bool ss) {m_singleSelection = ss;}
228 bool singleSelection() {return m_singleSelection;}
229
230 void readConfig();
231 void writeConfig();
232 void readSessionConfig(KConfig *);
233 void writeSessionConfig(KConfig *);
234
235 bool hasBrowserExtension() const { return m_bBrowserView; }
236
237 protected:
238 bool m_bBrowserView;
239
240 signals:
241 void selectionChanged();
242 void highlightChanged();
243 void modifiedChanged ();
244 void preHighlightChanged(long);
245
246 // search stuff
247 protected:
248 static QStringList searchForList;
249 static QStringList replaceWithList;
250 static uint uniqueID;
251
252 // highlight stuff
253 public:
254 Highlight *highlight() {return m_highlight;}
255 int highlightNum() {return hlManager->findHl(m_highlight);}
256 int numAttribs() {return m_numAttribs;}
257 Attribute *attribs() {return m_attribs;}
258 void setDontChangeHlOnSave();
259
260 protected:
261 void setHighlight(int n);
262 void makeAttribs();
263 void updateFontData();
264
265 protected slots:
266 void hlChanged();
267
268// view interaction
269 public:
270 virtual void addView(KTextEditor::View *);
271 virtual void removeView(KTextEditor::View *);
272 bool ownedView(KateView *);
273 bool isLastView(int numViews);
274
275 int getTextLineCount() {return numLines();}
276
277 int textWidth(const TextLine::Ptr &, int cursorX);
278 int textWidth(PointStruc &cursor);
279 int textWidth(bool wrapCursor, PointStruc &cursor, int xPos);
280 int textPos(const TextLine::Ptr &, int xPos);
281// int textPos(TextLine::Ptr &, int xPos, int &newXPos);
282 int textWidth();
283 int textHeight();
284
285 void insert(VConfig &, const QString &);
286 void insertFile(VConfig &, QIODevice &);
287
288 int currentColumn(PointStruc &cursor);
289 bool insertChars(VConfig &, const QString &chars);
290 void newLine(VConfig &);
291 void killLine(VConfig &);
292 void backspace(VConfig &);
293 void del(VConfig &);
294 void clear();
295 void cut(VConfig &);
296 void copy(int flags);
297 void paste(VConfig &);
298
299 void toggleRect(int, int, int, int);
300 void selectTo(VConfig &c, PointStruc &cursor, int cXPos);
301 void selectAll();
302 void deselectAll();
303 void invertSelection();
304 void selectWord(PointStruc &cursor, int flags);
305 void selectLength(PointStruc &cursor, int length, int flags);
306
307 void indent(VConfig &c) {doIndent(c, 1);}
308 void unIndent(VConfig &c) {doIndent(c, -1);}
309 void cleanIndent(VConfig &c) {doIndent(c, 0);}
310 // called by indent/unIndent/cleanIndent
311 // just does some setup and then calls optimizeLeadingSpace()
312 void doIndent(VConfig &, int change);
313 // optimize leading whitespace on a single line - see kwdoc.cpp for full description
314 void optimizeLeadingSpace(int line, int flags, int change);
315
316 void comment(VConfig &c) {doComment(c, 1);}
317 void unComment(VConfig &c) {doComment(c, -1);}
318 void doComment(VConfig &, int change);
319
320 virtual QString text() const;
321 QString getWord(PointStruc &cursor);
322
323 public slots:
324 virtual void setText(const QString &);
325
326 public:
327 long needPreHighlight(long till);
328 bool hasMarkedText() {return (selectEnd >= selectStart);}
329 QString markedText(int flags);
330 void delMarkedText(VConfig &/*, bool undo = true*/);
331
332 void tagLineRange(int line, int x1, int x2);
333 void tagLines(int start, int end);
334 void tagAll();
335 void updateLines(int startLine = 0, int endLine = 0xffffff, int flags = 0, int cursorY = -1);
336 void updateMaxLength(TextLine::Ptr &);
337 void updateViews(KateView *exclude = 0L);
338
339 QColor &cursorCol(int x, int y);
340 void paintTextLine(QPainter &, int line, int xStart, int xEnd, bool showTabs);
341 void paintTextLine(QPainter &, int line, int y, int xStart, int xEnd, bool showTabs);
342
343 bool doSearch(SConfig &s, const QString &searchFor);
344
345// internal
346 void tagLine(int line);
347 void insLine(int line);
348 void delLine(int line);
349 void optimizeSelection();
350
351 void doAction(KateAction *);
352 void doReplace(KateAction *);
353 void doWordWrap(KateAction *);
354 void doWordUnWrap(KateAction *);
355 void doNewLine(KateAction *);
356 void doDelLine(KateAction *);
357 void doInsLine(KateAction *);
358 void doKillLine(KateAction *);
359 void newUndo();
360
361 void recordStart(VConfig &, int newUndoType);
362 void recordStart(KateView *, PointStruc &, int flags, int newUndoType, bool keepModal = false, bool mergeUndo = false);
363 void recordAction(KateAction::Action, PointStruc &);
364 void recordInsert(VConfig &, const QString &text);
365 void recordReplace(VConfig &, int len, const QString &text);
366 void recordInsert(PointStruc &, const QString &text);
367 void recordDelete(PointStruc &, int len);
368 void recordReplace(PointStruc &, int len, const QString &text);
369 void recordEnd(VConfig &);
370 void recordEnd(KateView *, PointStruc &, int flags);
371 void doActionGroup(KateActionGroup *, int flags, bool undo = false);
372
373 int nextUndoType();
374 int nextRedoType();
375 void undoTypeList(QValueList<int> &lst);
376 void redoTypeList(QValueList<int> &lst);
377 void undo(VConfig &, int count = 1);
378 void redo(VConfig &, int count = 1);
379 void clearRedo();
380 void setUndoSteps(int steps);
381
382 void setPseudoModal(QWidget *);
383
384 void newBracketMark(PointStruc &, BracketMark &);
385
386
387 protected slots:
388 void clipboardChanged();
389 void slotBufferChanged();
390 void slotBufferHighlight(long,long);
391 void doPreHighlight();
392
393 private slots:
394 void slotViewDestroyed();
395
396// member variables
397 protected:
398 long PreHighlightedTill;
399 long RequestPreHighlightTill;
400 KWBuffer *buffer;
401 QColor colors[2];
402 HlManager *hlManager;
403 Highlight *m_highlight;
404 int m_numAttribs;
405 static const int maxAttribs;
406 Attribute *m_attribs;
407
408 int eolMode;
409
410 int tabChars;
411 int m_tabWidth;
412 int fontHeight;
413 int fontAscent;
414
415 QList<KateView> views;
416 bool newDocGeometry;
417
418 TextLine::Ptr longestLine;
419 float maxLength;
420
421 PointStruc select;
422 PointStruc anchor;
423 int aXPos;
424 int selectStart;
425 int selectEnd;
426 bool oldMarkState;
427 bool m_singleSelection; // false: windows-like, true: X11-like
428
429 bool readOnly;
430 bool newDoc; // True if the file is a new document (used to determine whether
431 // to check for overwriting files on save)
432 bool modified;
433
434 bool myWordWrap;
435 uint myWordWrapAt;
436
437 QList<KateActionGroup> undoList;
438 int currentUndo;
439 int undoState;
440 int undoSteps;
441 int tagStart;
442 int tagEnd;
443 int undoCount; //counts merged undo steps
444
445 QWidget *pseudoModal; //the replace prompt is pseudo modal
446
447 public:
448 /** Tjecks if the file on disk is newer than document contents.
449 If forceReload is true, the document is reloaded without asking the user,
450 otherwise [default] the user is asked what to do. */
451 void isModOnHD(bool forceReload=false);
452
453 uint docID () {return myDocID;};
454 QString docName () {return myDocName;};
455
456 void setDocName (QString docName);
457
458 public slots:
459 /** Reloads the current document from disk if possible */
460 void reloadFile();
461
462 private slots:
463 void slotModChanged ();
464
465 private:
466 /** updates mTime to reflect file on fs.
467 called from constructor and from saveFile. */
468 void setMTime();
469 uint myDocID;
470 QFileInfo* fileInfo;
471 QDateTime mTime;
472 QString myDocName;
473
474 QString m_url;
475 QString m_file;
476 void openURL(const QString &filename);
477 private:
478 KateCmd *myCmd;
479
480 public:
481 KateCmd *cmd () { return myCmd; };
482
483 private:
484 QString myEncoding;
485
486 public:
487 void setEncoding (QString e) { myEncoding = e; };
488 QString encoding() { return myEncoding; };
489
490 void setWordWrap (bool on);
491 bool wordWrap () { return myWordWrap; };
492
493 void setWordWrapAt (uint col);
494 uint wordWrapAt () { return myWordWrapAt; };
495
496 signals:
497 void modStateChanged (KateDocument *doc);
498 void nameChanged (KateDocument *doc);
499
500 public:
501 QList<Kate::Mark> marks ();
502
503 public slots:
504 // clear buffer/filename - update the views
505 void flush ();
506
507 signals:
508 /**
509 The file has been saved (perhaps the name has changed). The main window
510 can use this to change its caption
511 */
512 void fileNameChanged ();
513
514 public:
515 //end of line settings
516 enum Eol_settings {eolUnix=0,eolDos=1,eolMacintosh=2};
517
518 // for the DCOP interface
519 public:
520 void open (const QString &name=0);
521
522 public:
523 // wrap the text of the document at the column col
524 void wrapText (uint col);
525
526 public slots:
527 void applyWordWrap ();
528
529 private:
530
531 class KateDocPrivate
532 {
533 public:
534 bool hlSetByUser;
535 };
536
537
538// BCI: Add a real d-pointer in the next BIC release
539static QPtrDict<KateDocPrivate>* d_ptr;
540static void cleanup_d_ptr()
541 {
542 delete d_ptr;
543 }
544
545KateDocPrivate* d( const KateDocument* foo )
546 {
547 if ( !d_ptr ) {
548 d_ptr = new QPtrDict<KateDocPrivate>;
549 //qAddPostRoutine( cleanup_d_ptr );
550 }
551 KateDocPrivate* ret = d_ptr->find( (void*) foo );
552 if ( ! ret ) {
553 ret = new KateDocPrivate;
554 d_ptr->replace( (void*) foo, ret );
555 }
556 return ret;
557 }
558
559void delete_d( const KateDocument* foo )
560 {
561 if ( d_ptr )
562 d_ptr->remove( (void*) foo );
563 }
564
565};
566
567#endif
568
569