1 /**
2  *  Yudit Unicode Editor Source File
3  *
4  *  GNU Copyright (C) 1997-2006  Gaspar Sinai <gaspar@yudit.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License, version 2,
8  *  dated June 1991. See file COPYYING for details.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #ifndef STextData_h
21 #define STextData_h
22 
23 #include "stoolkit/SVector.h"
24 #include "stoolkit/STypes.h"
25 #include "stoolkit/SString.h"
26 #include "stoolkit/STextIndex.h"
27 #include "stoolkit/SCharClass.h"
28 #include "swindow/SColor.h"
29 #include "stoolkit/SGlyph.h"
30 #include "stoolkit/SParagraph.h"
31 #include "stoolkit/SLineTracker.h"
32 
33 class STextDataEvent
34 {
35 public:
36   /* line per line events */
37   STextDataEvent (void);
38   STextDataEvent (const STextIndex& start, bool attribute=false);
39   ~STextDataEvent ();
40 
41   void clear();
42   void add (const STextDataEvent& evt);
43   void setRemaining (const STextIndex& remain);
44 
45   bool          valid;
46   bool          attribute; // only attribute of text has changed.
47 
48   STextIndex    start;
49   /* a reversely calcualted value */
50   STextIndex    remaining;
51 };
52 
53 class STextDataListener
54 {
55 public:
56   virtual void textChanged(void* src, const STextDataEvent& event) = 0;
~STextDataListener()57   virtual ~STextDataListener() {}
58 };
59 
60 /**
61  * This STextData has a notion about glyphs and compositions.
62  */
63 class STextData
64 {
65 public:
66   STextData (void);
67   STextData (const SString& utf8);
68   STextData operator = (const STextData & data);
69   virtual ~STextData ();
70 
71   SString getText () const;
72   SString getText (const STextIndex& index) const;
73   SString getText (const STextIndex& begin, const STextIndex& end) const;
74 
getChars(unsigned int line)75   SV_UCS4 getChars (unsigned int line) const
76    { return lines[line]->getChars (); }
77   STextIndex find (const SString& string);
78   const SGlyph& glyphAt (const STextIndex& index) const;
79   // we need only this
80   const SGlyph* peekGlyphAt (const STextIndex& index) const;
81 
82   /* these, till fireEvent all work with events */
83   void move (const STextIndex& index);
84 
85   /* moves the cursor to the end */
86   void setText (const SString& utf8);
87 
88   void insert (const SString& data);
89 
90   /* remove moves the cursor */
91   void remove (const STextIndex& index);
92 
93   /* add a composing character */
94   bool addComposing(SS_UCS4 c, bool toleft);
95 
96   /* remove a composing character */
97   SS_UCS4 removeComposing(bool toleft);
98 
99   /* These just change the text */
100   void select (const STextIndex& index, bool is=true);
101   void underline (const STextIndex& index, bool is=true);
102   bool setParagraphSeparator (const SString& str);
103 
104   bool isLR (unsigned int parag) const;
105   bool isLR (const STextIndex& index) const;
106   SEmbedState getEmbedState (const STextIndex& index) const;
107 
108   void clear ();
109   void fireEvent ();
110   void clearEvent ();
111 
112   unsigned int  size() const;
113   unsigned int  size(unsigned int line) const;
114   // For syntax, It will not expand line
115   unsigned int  softSize (unsigned int line) const;
116   SS_UCS4       softCharAt (unsigned int line, unsigned int pos) const;
117 
118   STextIndex getTextIndex (int charOffset=0, bool logical=false) const;
119   STextIndex getTextIndex(const STextIndex& base, int charOffset=0, bool logical=false) const;
120   STextIndex getMaxTextIndex(const STextDataEvent& evt) const;
121   STextIndex getMinTextIndex(const STextDataEvent& evt) const;
122 
123   unsigned int toLogical (unsigned int line, unsigned int index);
124   SV_UINT getLogicalMap(unsigned int line) const;
125 
126   // Set only
127   void addTextDataListener (STextDataListener* listener);
128   // Set only - lineExpanded is never called, it mmust be handled separately
129   void addLineTracker (SLineTracker* lt);
130 
131   bool isProperLine (unsigned int line) const;
132   bool isVisible(unsigned int line) const;
133   void setVisible(unsigned int line);
134 
135   bool isReordered (unsigned int line) const;
136   void setReordered(unsigned int line);
137 
138   void setDocumentEmbedding(SS_Embedding e);
139   SS_Embedding getDocumentEmbedding() const;
140 
141   bool isWhiteSpace (const STextIndex& index) const;
142   bool isDelimiter (const STextIndex& index) const;
143   bool canWrap (const STextIndex& index) const;
144   bool isNumber (const STextIndex& index) const;
145   bool isLetter (const STextIndex& index) const;
146   bool isTransparent (const STextIndex& index) const;
147 
148   void setLineBreaks (unsigned int line, const SV_UCS4& breaks);
149 
150   bool getInitialLR() const;
151   void setInitialLR(bool lr);
152 
getCurrentEvent()153   const STextDataEvent& getCurrentEvent () const { return  event; }
154 
isExpanded(unsigned int line)155   bool isExpanded (unsigned int line) const
156      { return lines[line]->isExpanded(); }
157 
getParagraphSeparator(unsigned int line)158   SS_ParaSep getParagraphSeparator (unsigned int line) const
159      { return lines[line]->getParagraphSeparator (); }
160 
161 private:
162   SS_Embedding         embedding;
163 
164   /* insert moves the cursor */
165   unsigned int properSize(unsigned int line) const;
166   void insert (const SGlyph& glyph);
167 
168   bool setParagraphSeparator (SS_ParaSep ps);
169   void reShape (const STextIndex& index);
170   void reShapeOne (const STextIndex& index);
171 
172   /* moves the cursor to the end */
173   void setText (const SV_UCS4& ucs4);
174 
175   void setMaxLimits (STextDataEvent* evt, const STextIndex& index);
176   STextIndex reorder (const STextIndex& index);
177 
178   /* These belong to the event being generated */
179   STextDataEvent     event;
180   STextIndex         textIndex;
181 
182   /* should be a linked list */
183   SBinVector<SParagraph*>  lines;
184   STextDataListener*     listener;
185   SLineTracker*          lineTracker;
186 };
187 
188 #endif /* STextData_h */
189