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 STextEdit_h
21 #define STextEdit_h
22 
23 #include "swidget/SYuditInput.h"
24 #include "swidget/SEditor.h"
25 #include "swidget/STextEditLS.h"
26 #include "swidget/SEditorIF.h"
27 #include "swidget/SPanel.h"
28 #include "swidget/STextView.h"
29 #include "swidget/SBorder.h"
30 #include "swidget/SCaret.h"
31 #include "stoolkit/SString.h"
32 #include "stoolkit/SProperties.h"
33 #include "stoolkit/SEvent.h"
34 #include "stoolkit/SStringVector.h"
35 #include "stoolkit/SBinVector.h"
36 #include "stoolkit/SCursorIndex.h"
37 
38 class SDClick : public SEventTarget
39 {
40 public:
41  SDClick(void);
42  ~SDClick();
43 
44   void              start (unsigned int millisec);
45   unsigned int      clickCount;
46   unsigned int      timeoutCount;
47 protected:
48   STimer*  timer;
49   bool timeout (const SEventSource* s);
50 };
51 
52 class STextEdit : public SPanel, public SEditorIF, public SPreEditor
53 {
54 public:
55   STextEdit (void);
56   STextEdit (const SString& utf8);
57   virtual ~STextEdit ();
58   // This should be the first thing you do
59   void setDoubleBuffer (bool yes);
60 
61   void setFocus();
62   bool isFocused () const;
63 
64   const SDimension& getPreferredSize();
65 
66   void addTextEditLS (STextEditLS* ls);
67   void removeTextEditLS (STextEditLS* ls);
68   void setEditor (const SEditor& editor);
69   void setXIMProperties (const SProperties& props);
addSyntaxListener(SSyntaxListener * _listener)70   void addSyntaxListener (SSyntaxListener* _listener)
71      { textView.addSyntaxListener (_listener); }
72   // only SSyntax.
getSyntaxName()73   SString getSyntaxName () const
74      { return SString (textView.getSyntaxName ()); }
75   // internal included
getHighlightName()76   SString getHighlightName () const
77      { return SString (textView.getHighlightName ()); }
78 
79   virtual void setBackground (const SColor& bg);
80   virtual void setTextBackground (const SColor& bg);
81   void setForeground (const SColor& lrfg, const SColor& rlfg);
82   void setCaretForeground (const SColor& lrfg, const SColor& rlfg);
83 
84   virtual void lostKeyboardFocus (SWindow* w);
85   virtual void lostClipSelection (SWindow* w);
86   virtual bool gainedKeyboardFocus (SWindow* w);
87   virtual void redraw (SCanvas *canvas, int x, int y,
88      unsigned int width, unsigned int height);
89 
90   virtual void keyPressed (SWindow * w, SKey key, const SString& s,
91           bool ctrl, bool shift, bool meta);
92   virtual void keyReleased (SWindow * w, SKey key, const SString& s,
93           bool ctrl, bool shift, bool meta);
94   virtual void buttonPressed (SWindow * w, int button, int x, int y);
95   virtual void buttonReleased (SWindow * w, int button, int x, int y);
96   virtual void buttonDragged (SWindow * w, int button, int x, int y);
97 
98   virtual void resize (const SDimension& size);
99   void   resizeTextView (bool scrool);
100   virtual void move (const SLocation& loc);
101 
102   void setAlignment (bool align);
103   void setMultiline (bool multiline);
104   bool isMultiline () const;
105   void setLineEndMark (bool lineend);
106   bool getLineEndMark () const;
107 
108   void scrollVertical (int value, bool notofy);
109   void scrollHorizontal (int value, bool notofy);
110 
111   virtual void setFont (const SString& font, double fontSize);
112   virtual void setFontSize (double size);
113 
114 
115   virtual void setFont (const SString& font);
116   virtual void setInputMethod (const SString& im);
117 
118   virtual void insertClipboardText ();
119   virtual void insertText (const SString& str, bool embed=true);
120   virtual void insertDirtyText (const SString& str, bool embed=true);
121   virtual void insertPreEditText (const SString& utf8t,
122         SPreEditor::SStyle style);
123 
124 
125   virtual SString eraseText (const STextIndex& till);
126 
127   virtual void pageUp ();
128   virtual void pageDown ();
129 
130   virtual const SCursorIndex& getCursorIndex () const;
131   virtual void setCursorIndex (const SCursorIndex& index);
132 
133   virtual void caretUp ();
134   virtual void caretDown ();
135   virtual void caretLeft ();
136   virtual void caretRight ();
137 
138   virtual void startSelect ();
139   virtual void endSelect ();
140   virtual void clipSelect ();
141 
142   virtual void selectText (const SCursorIndex& till);
143   virtual void selectWord ();
144   virtual void selectLine ();
145   virtual void deselectText ();
146 
147   virtual SString eraseSelectedText ();
148   virtual STextIndex getSelectedIndex (bool hi);
149   virtual STextIndex getIndexAfterLineBreak ();
150 
151   virtual SString backspace ();
152   virtual SString erase ();
153 
154   virtual bool addComposing(SS_UCS4 c);
155   virtual SS_UCS4 removeComposing();
156 
157   virtual void setDirection (SS_DR_Dir dir);
158   virtual SS_DR_Dir getDirection () const;
159   virtual bool changeDirection (SS_DR_Dir dir);
160 
161   virtual void focusOut ();
162   virtual void setEditable (bool editable);
163   virtual bool isEditable () const;
164   virtual bool isSelecting() const;
165 
166   virtual SString getText (const STextIndex& from, const STextIndex& till);
167   virtual void clear();
168   virtual bool clearState();
169 
170   virtual void setDocumentEmbedding (SS_Embedding e);
171   virtual SS_Embedding getDocumentEmbedding () const;
172 
173   void setUnderlineColor (const SColor& c);
174   void setText (const SString& text);
175 
176   SString getText ();
177   bool undo();
178   bool redo();
179   void setParagraphSeparator (const SString& lineBreak, bool change);
180 
181   void setSyntax (const SString& hlMode);
182   const SString& getSyntax () const;
183 
184   void setSyntaxColors (const SSyntaxColors& attr);
185   const SSyntaxColors&  getSyntaxColors () const;
186 
187   void setWordWrap (bool lbm);
188   bool getWordWrap () const;
189 
190   const SStringVector& getHistory();
191   void putHistory (const SString& str);
192   void setHistorySize (unsigned int siz);
193   void historyUp ();
194   void historyDown ();
195   void historyEnd ();
196   bool find(const SString& str);
197   bool replace(const SString& orig, const SString& str);
198 
199   const SGlyph* glyphAt(const STextIndex & ti) const;
200   STextIndex getCaretArrowFrom ();
201 
202   /* new text through editor */
203   void insertEditorText (const SString& text);
204 
205   bool isEmbedStateLR() const;
206 
207   /* encode with current keymap */
208   SString encode (const SV_UCS4& g) const;
209   SString getSelectedText ();
210 
211   void setAutoClipboard(bool is);
212   void setHideText(bool is);
213 
214 
215 protected:
216   bool autoClipboard;
217   void autoClipSelection ();
218 
219   unsigned int historySize;
220   unsigned int currentHistorySize;
221   SStringVector      history;
222   SProperties        ximProperties;
223 
224   SDClick    clicks;
225   bool       focused;
226   bool       editable;
227   bool       selecting;
228   STextIndex startSelection;
229   STextIndex endSelection;
230 
231   void atutoClipSelection ();
232   void setCaretLocationVisible ();
233   void setCaretVisibleVertical();
234   void setCaretVisibleHorizontal();
235 
236   void centerCaretVertical();
237 
238   void       updateCaretLocation(bool setvisible=true);
239   void       updateCaretPosition(bool updateColumn=true);
240 
241   SCaret     caret;
242   SBorder    border;
243   STextView  textView;
244 
245   /* This is from SSliderListener */
246   virtual void valueChanged (SSlidable* slidable, SSlideType type);
247 
248   /* This is from component */
249   virtual SSlidable* setSliderListener (SSliderListener* l);
250 
251   // SPreeditor
252   virtual void preEditClearMarkedText ();
253   virtual void preEditInsertMarkedText(const SString& utf8, SStyle style);
254   virtual SRectangle preEditGlyphRectangleUTF16(unsigned int charPos);
255 
256   STimer*  eventMergeTimer;
257 
258 
259 private:
260   SRectangle getGlyphRectangle (const STextIndex& ti);
261   //
262   SCursorIndex preEditStartIndex;
263   SCursorIndex preEditEndIndex;
264   SS_DR_Dir    preEditStartDirection;
265   unsigned int preEditSize;
266 
267   SString cleanEmbed(const SString str) const;
268 
269   SString          imWaiting;
270   SString          inputMethod;
271   SSlidable        slidable;
272   SSliderListener* sliderListener;
273 
274   SYuditInput* yuditInput;
275   void         notifySlider ();
276   void         fireTextChanged ();
277   void         fireCaretMoved ();
278   void         redrawInternal ();
279 
280   SBinVector<STextEditLS*> listeners;
281 
282   SEditor  editor;
283   SProperties  getProperties ();
284   void internalMoveCaret (const SCursorIndex& realIndex);
285 
286   void clip (bool on);
287   unsigned int column; /* this is to align up-down movements of cursor. */
288   bool resized;
289   unsigned int statusHeight; /* status area for 'over-the-spot-off'  */
290 };
291 
292 #endif /* STextEdit_h */
293