Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

katedocument.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                           katedocument.h  -  description
00003                              -------------------
00004     begin                : Mon Jan 15 2001
00005     copyright            : (C) 2001 by Christoph "Crossfire" Cullmann
00006                            (C) 2002 by Joseph Wenninger
00007     email                : crossfire@babylon2k.de
00008                            jowenn@kde.org
00009 
00010 ***************************************************************************/
00011 
00012 /***************************************************************************
00013  *                                                                         *
00014  *   This program is free software; you can redistribute it and/or modify  *
00015  *   it under the terms of the GNU General Public License as published by  *
00016  *   the Free Software Foundation; either version 2 of the License, or     *
00017  *   (at your option) any later version.                                   *
00018  *                                                                         *
00019  ***************************************************************************/
00020 
00021 /*
00022    Copyright (C) 1998, 1999 Jochen Wilhelmy
00023                             digisnap@cs.tu-berlin.de
00024 
00025     This library is free software; you can redistribute it and/or
00026     modify it under the terms of the GNU Library General Public
00027     License as published by the Free Software Foundation; either
00028     version 2 of the License, or (at your option) any later version.
00029 
00030     This library is distributed in the hope that it will be useful,
00031     but WITHOUT ANY WARRANTY; without even the implied warranty of
00032     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033     Library General Public License for more details.
00034 
00035     You should have received a copy of the GNU Library General Public License
00036     along with this library; see the file COPYING.LIB.  If not, write to
00037     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038     Boston, MA 02111-1307, USA.
00039 */
00040 
00041 #ifndef kate_document_h
00042 #define kate_document_h
00043 
00044 
00045 #include <qobject.h>
00046 #include <qlist.h>
00047 #include <qcolor.h>
00048 #include <qfont.h>
00049 #include <qfontmetrics.h>
00050 #include <qdatetime.h>
00051 #include <qfileinfo.h>
00052 
00053 #include "../view/kateview.h"
00054 #include "katehighlight.h"
00055 #include "katebuffer.h"
00056 #include "katetextline.h"
00057 
00058 
00059 #include <qptrdict.h>
00060 
00061 class KateCmd;
00062 
00063 class CachedFontMetrics : public QFontMetrics {
00064 private:
00065     short *warray[256];
00066 public:
00067     CachedFontMetrics(const QFont& f) : QFontMetrics(f) {
00068         for (int i=0; i<256; i++) warray[i]=0;
00069     }
00070     ~CachedFontMetrics() {
00071         for (int i=0; i<256; i++)
00072                 if (warray[i]) delete[] warray[i];
00073     }
00074     int width(QChar c) {
00075         uchar cell=c.cell();
00076         uchar row=c.row();
00077         short *wa=warray[row];
00078         if (!wa) {
00079                 // odebug << "create row: " << row << oendl;
00080                 wa=warray[row]=new short[256];
00081                 for (int i=0; i<256; i++) wa[i]=-1;
00082         }
00083         if (wa[cell]<0) wa[cell]=(short) QFontMetrics::width(c);
00084         return (int)wa[cell];
00085     }
00086     int width(QString s) { return QFontMetrics::width(s); }
00087 };
00088 
00089 class Attribute {
00090   public:
00091     Attribute() { ; };
00092 
00093     QColor col;
00094     QColor selCol;
00095     bool bold;
00096     bool italic;
00097 };
00098 
00099 class KateAction {
00100   public:
00101     enum Action {replace, wordWrap, wordUnWrap, newLine, delLine,
00102       insLine, killLine};//, doubleLine, removeLine};
00103 
00104     KateAction(Action, PointStruc &cursor, int len = 0,
00105       const QString &text = QString::null);
00106 
00107     Action action;
00108     PointStruc cursor;
00109     int len;
00110     QString text;
00111     KateAction *next;
00112 };
00113 
00114 class KateActionGroup {
00115   public:
00116     // the undo group types
00117     enum {  ugNone,         //
00118             ugPaste,        // paste
00119             ugDelBlock,     // delete/replace selected text
00120             ugIndent,       // indent
00121             ugUnindent,     // unindent
00122             ugComment,      // comment
00123             ugUncomment,    // uncomment
00124             ugReplace,      // text search/replace
00125             ugSpell,        // spell check
00126             ugInsChar,      // char type/deleting
00127             ugDelChar,      // ''  ''
00128             ugInsLine,      // line insert/delete
00129             ugDelLine       // ''  ''
00130          };
00131 
00132     KateActionGroup(PointStruc &aStart, int type = ugNone);
00133     ~KateActionGroup();
00134     void insertAction(KateAction *);
00135 
00136     static const char * typeName(int type);
00137 
00138     PointStruc start;
00139     PointStruc end;
00140     KateAction *action;
00141     int undoType;
00142 };
00143 
00153 class KateDocument: public Kate::Document
00154 {
00155     Q_OBJECT
00156     friend class KateViewInternal;
00157     friend class KateView;
00158     friend class KateIconBorder;
00159 
00160   public:
00161     KateDocument(bool bSingleViewMode=false, bool bBrowserView=false, QWidget *parentWidget = 0, const char *widgetName = 0, QObject * = 0, const char * = 0);
00162     ~KateDocument();
00163 
00164   protected:
00165     QFont myFont, myFontBold, myFontItalic, myFontBI;
00166     CachedFontMetrics myFontMetrics, myFontMetricsBold, myFontMetricsItalic, myFontMetricsBI;
00167 
00168   public:
00169     void setFont (QFont font);
00170     QFont getFont () { return myFont; };
00171     CachedFontMetrics getFontMetrics () { return myFontMetrics; };
00172 
00173     virtual bool saveFile();
00174 
00175     virtual KTextEditor::View *createView( QWidget *parent, const char *name );
00176     virtual QString textLine( int line ) const;
00177 
00178     virtual void insertLine( const QString &s, int line = -1 );
00179 
00180     void insert_Line(const QString& s,int line=-1, bool update=true);
00181     void remove_Line(int line,bool update=true);
00182     void replaceLine(const QString& s,int line=-1);
00183     virtual void insertAt( const QString &s, int line, int col, bool mark = FALSE );
00184     virtual void removeLine( int line );
00185     virtual int length() const;
00186 
00187     virtual void setSelection( int row_from, int col_from, int row_to, int col_t );
00188     virtual bool hasSelection() const;
00189     virtual QString selection() const;
00190 
00191     // only to make part work, don't change it !
00192     bool m_bSingleViewMode;
00193 
00194 // public interface
00198     virtual int numLines() const;
00199 
00203     int lastLine() const {return numLines()-1;}
00204 
00210     TextLine::Ptr getTextLine(int line) const;
00211 
00215     int textLength(int line);
00216 
00217     void setTabWidth(int);
00218     int tabWidth() {return tabChars;}
00219     void setReadOnly(bool);
00220     bool isReadOnly() const;
00221     void setNewDoc( bool );
00222     bool isNewDoc() const;
00223     virtual void setReadWrite( bool ){};
00224     virtual bool isReadWrite() const {return true;}
00225     virtual void setModified(bool);
00226     virtual bool isModified() const;
00227     void setSingleSelection(bool ss) {m_singleSelection = ss;}
00228     bool singleSelection() {return m_singleSelection;}
00229 
00230     void readConfig();
00231     void writeConfig();
00232     void readSessionConfig(KateConfig *);
00233     void writeSessionConfig(KateConfig *);
00234 
00235     bool hasBrowserExtension() const { return m_bBrowserView; }
00236 
00237   protected:
00238     bool m_bBrowserView;
00239 
00240   signals:
00241     void selectionChanged();
00242     void highlightChanged();
00243     void modifiedChanged ();
00244     void preHighlightChanged(long);
00245 
00246   // search stuff
00247   protected:
00248     static QStringList searchForList;
00249     static QStringList replaceWithList;
00250     static uint uniqueID;
00251 
00252   // highlight stuff
00253   public:
00254     Highlight *highlight() {return m_highlight;}
00255     int highlightNum() {return hlManager->findHl(m_highlight);}
00256     int numAttribs() {return m_numAttribs;}
00257     Attribute *attribs() {return m_attribs;}
00258     void setDontChangeHlOnSave();
00259 
00260   protected:
00261     void setHighlight(int n);
00262     void makeAttribs();
00263     void updateFontData();
00264 
00265   protected slots:
00266     void hlChanged();
00267 
00268 // view interaction
00269   public:
00270     virtual void addView(KTextEditor::View *);
00271     virtual void removeView(KTextEditor::View *);
00272     bool ownedView(KateView *);
00273     bool isLastView(int numViews);
00274 
00275     int getTextLineCount() {return numLines();}
00276 
00277     int textWidth(const TextLine::Ptr &, int cursorX);
00278     int textWidth(PointStruc &cursor);
00279     int textWidth(bool wrapCursor, PointStruc &cursor, int xPos);
00280     int textPos(const TextLine::Ptr &, int xPos);
00281 //    int textPos(TextLine::Ptr &, int xPos, int &newXPos);
00282     int textWidth();
00283     int textHeight();
00284 
00285     void insert(VConfig &, const QString &);
00286     void insertFile(VConfig &, QIODevice &);
00287 
00288     int currentColumn(PointStruc &cursor);
00289     bool insertChars(VConfig &, const QString &chars);
00290     void newLine(VConfig &);
00291     void killLine(VConfig &);
00292     void backspace(VConfig &);
00293     void del(VConfig &);
00294     void clear();
00295     void cut(VConfig &);
00296     void copy(int flags);
00297     void paste(VConfig &);
00298 
00299     void toggleRect(int, int, int, int);
00300     void selectTo(VConfig &c, PointStruc &cursor, int cXPos);
00301     void selectAll();
00302     void deselectAll();
00303     void invertSelection();
00304     void selectWord(PointStruc &cursor, int flags);
00305     void selectLength(PointStruc &cursor, int length, int flags);
00306 
00307     void indent(VConfig &c) {doIndent(c, 1);}
00308     void unIndent(VConfig &c) {doIndent(c, -1);}
00309     void cleanIndent(VConfig &c) {doIndent(c, 0);}
00310     // called by indent/unIndent/cleanIndent
00311     // just does some setup and then calls optimizeLeadingSpace()
00312     void doIndent(VConfig &, int change);
00313     // optimize leading whitespace on a single line - see kwdoc.cpp for full description
00314     void optimizeLeadingSpace(int line, int flags, int change);
00315 
00316     void comment(VConfig &c) {doComment(c, 1);}
00317     void unComment(VConfig &c) {doComment(c, -1);}
00318     void doComment(VConfig &, int change);
00319 
00320     virtual QString text() const;
00321     QString getWord(PointStruc &cursor);
00322 
00323   public slots:
00324     virtual void setText(const QString &);
00325 
00326   public:
00327     long needPreHighlight(long till);
00328     bool hasMarkedText() {return (selectEnd >= selectStart);}
00329     QString markedText(int flags);
00330     void delMarkedText(VConfig &/*, bool undo = true*/);
00331 
00332     void tagLineRange(int line, int x1, int x2);
00333     void tagLines(int start, int end);
00334     void tagAll();
00335     void updateLines(int startLine = 0, int endLine = 0xffffff, int flags = 0, int cursorY = -1);
00336     void updateMaxLength(TextLine::Ptr &);
00337     void updateViews(KateView *exclude = 0L);
00338 
00339     QColor &cursorCol(int x, int y);
00340     void paintTextLine(QPainter &, int line, int xStart, int xEnd, bool showTabs);
00341     void paintTextLine(QPainter &, int line, int y, int xStart, int xEnd, bool showTabs);
00342 
00343     bool doSearch(SConfig &s, const QString &searchFor);
00344 
00345 // internal
00346     void tagLine(int line);
00347     void insLine(int line);
00348     void delLine(int line);
00349     void optimizeSelection();
00350 
00351     void doAction(KateAction *);
00352     void doReplace(KateAction *);
00353     void doWordWrap(KateAction *);
00354     void doWordUnWrap(KateAction *);
00355     void doNewLine(KateAction *);
00356     void doDelLine(KateAction *);
00357     void doInsLine(KateAction *);
00358     void doKillLine(KateAction *);
00359     void newUndo();
00360 
00361     void recordStart(VConfig &, int newUndoType);
00362     void recordStart(KateView *, PointStruc &, int flags, int newUndoType, bool keepModal = false, bool mergeUndo = false);
00363     void recordAction(KateAction::Action, PointStruc &);
00364     void recordInsert(VConfig &, const QString &text);
00365     void recordReplace(VConfig &, int len, const QString &text);
00366     void recordInsert(PointStruc &, const QString &text);
00367     void recordDelete(PointStruc &, int len);
00368     void recordReplace(PointStruc &, int len, const QString &text);
00369     void recordEnd(VConfig &);
00370     void recordEnd(KateView *, PointStruc &, int flags);
00371     void doActionGroup(KateActionGroup *, int flags, bool undo = false);
00372 
00373     int nextUndoType();
00374     int nextRedoType();
00375     void undoTypeList(QValueList<int> &lst);
00376     void redoTypeList(QValueList<int> &lst);
00377     void undo(VConfig &, int count = 1);
00378     void redo(VConfig &, int count = 1);
00379     void clearRedo();
00380     void setUndoSteps(int steps);
00381 
00382     void setPseudoModal(QWidget *);
00383 
00384     void newBracketMark(PointStruc &, BracketMark &);
00385 
00386 
00387   protected slots:
00388     void clipboardChanged();
00389     void slotBufferChanged();
00390     void slotBufferHighlight(long,long);
00391     void doPreHighlight();
00392 
00393   private slots:
00394     void slotViewDestroyed();
00395 
00396 // member variables
00397   protected:
00398     long PreHighlightedTill;
00399     long RequestPreHighlightTill;
00400     KWBuffer *buffer;
00401     QColor colors[2];
00402     HlManager *hlManager;
00403     Highlight *m_highlight;
00404     int m_numAttribs;
00405     static const int maxAttribs;
00406     Attribute *m_attribs;
00407 
00408     int eolMode;
00409 
00410     int tabChars;
00411     int m_tabWidth;
00412     int fontHeight;
00413     int fontAscent;
00414 
00415     QList<KateView> views;
00416     bool newDocGeometry;
00417 
00418     TextLine::Ptr longestLine;
00419     float maxLength;
00420 
00421     PointStruc select;
00422     PointStruc anchor;
00423     int aXPos;
00424     int selectStart;
00425     int selectEnd;
00426     bool oldMarkState;
00427     bool m_singleSelection; // false: windows-like, true: X11-like
00428 
00429     bool readOnly;
00430     bool newDoc;          // True if the file is a new document (used to determine whether
00431                           // to check for overwriting files on save)
00432     bool modified;
00433 
00434     bool myWordWrap;
00435     uint myWordWrapAt;
00436 
00437     QList<KateActionGroup> undoList;
00438     int currentUndo;
00439     int undoState;
00440     int undoSteps;
00441     int tagStart;
00442     int tagEnd;
00443     int undoCount;          //counts merged undo steps
00444 
00445     QWidget *pseudoModal;   //the replace prompt is pseudo modal
00446 
00447     public:
00451     void isModOnHD(bool forceReload=false);
00452 
00453     uint docID () {return myDocID;};
00454     QString docName () {return myDocName;};
00455 
00456     void setDocName (QString docName);
00457     void setDocFile (QString docFile);
00458 
00459   public slots:
00461     void reloadFile();
00462 
00463   private slots:
00464     void slotModChanged ();
00465 
00466   private:
00469     void setMTime();
00470     uint myDocID;
00471     QFileInfo* fileInfo;
00472     QDateTime mTime;
00473     QString myDocName;
00474 
00475     QString m_url;
00476     QString m_file;
00477     void openURL(const QString &filename);
00478   private:
00479     KateCmd *myCmd;
00480 
00481   public:
00482     KateCmd *cmd () { return myCmd; };
00483 
00484   private:
00485     QString myEncoding;
00486 
00487   public:
00488     void setEncoding (QString e) { myEncoding = e; };
00489     QString encoding() { return myEncoding; };
00490 
00491     void setWordWrap (bool on);
00492     bool wordWrap () { return myWordWrap; };
00493 
00494     void setWordWrapAt (uint col);
00495     uint wordWrapAt () { return myWordWrapAt; };
00496 
00497   signals:
00498     void modStateChanged (KateDocument *doc);
00499     void nameChanged (KateDocument *doc);
00500 
00501   public:
00502     QList<Kate::Mark> marks ();
00503 
00504   public slots:
00505     // clear buffer/filename - update the views
00506     void flush ();
00507 
00508   signals:
00513     void fileNameChanged ();
00514 
00515   public:
00516     //end of line settings
00517     enum Eol_settings {eolUnix=0,eolDos=1,eolMacintosh=2};
00518 
00519   // for the DCOP interface
00520   public:
00521     void open (const QString &name=0);
00522 
00523   public:
00524     // wrap the text of the document at the column col
00525     void wrapText (uint col);
00526 
00527   public slots:
00528      void applyWordWrap ();
00529 
00530   private:
00531 
00532   class KateDocPrivate
00533   {
00534     public:
00535           bool hlSetByUser;
00536   };
00537 
00538 
00539 // BCI: Add a real d-pointer in the next BIC release
00540 static QPtrDict<KateDocPrivate>* d_ptr;
00541 static void cleanup_d_ptr()
00542       {
00543           delete d_ptr;
00544       }
00545 
00546 KateDocPrivate* d( const KateDocument* foo )
00547       {
00548            if ( !d_ptr ) {
00549                      d_ptr = new QPtrDict<KateDocPrivate>;
00550                      //qAddPostRoutine( cleanup_d_ptr );
00551                 }
00552                 KateDocPrivate* ret = d_ptr->find( (void*) foo );
00553                 if ( ! ret ) {
00554                         ret = new KateDocPrivate;
00555                         d_ptr->replace( (void*) foo, ret );
00556                 }
00557                 return ret;
00558       }
00559 
00560 void delete_d( const KateDocument* foo )
00561      {
00562           if ( d_ptr )
00563               d_ptr->remove( (void*) foo );
00564      }
00565 
00566 };
00567 
00568 #endif
00569 
00570 

Generated on Sat Nov 5 16:17:10 2005 for OPIE by  doxygen 1.4.2