CodeView  Version0.4
 All Classes Functions Variables
inneredit.h
1 /* libcodeview.so - A programmers editor widget for Atheos
2  Copyright (c) Andreas Engh-Halstvedt
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public
15  License along with this library; if not, write to the Free
16  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17  MA 02111-1307, USA
18 */
19 
20 #ifndef _INNEREDIT_H_
21 #define _INNEREDIT_H_
22 
23 #include <vector>
24 
25 #include <gui/view.h>
26 #include <gui/control.h>
27 #include <gui/bitmap.h>
28 #include <gui/menu.h>
29 #include <util/looper.h>
30 #include <util/message.h>
31 
32 #include "format.h"
33 
34 using namespace std;
35 
36 namespace cv
37 {
38 
39 class InnerEdit;
40 
44 class Line
45 {
46 public:
47  os::String text;
48  os::String style;
50  float w;
52  Line(): cookie(0), w(0){}
53 };
54 
55 typedef vector<Line> buffer_type;
56 
60 class UndoNode
61 {
62 public:
63  enum {
64  ADDED,
65  REMOVED,
66  SET_LINE
67  };
68 
69  os::String text;
70  uint mode,
71  x,
72  y;
74  UndoNode *next,
75  *previous;
78  next = 0;
79  previous = 0;
80  }
81 };
82 
86 class Fold
87 {
88 public:
89  uint32 nStart;
90  uint32 nEnd;
92  Fold( uint32 s, uint32 e ) {
93  nStart = s;
94  nEnd = e;
95  }
96 };
97 
101 class InnerEdit : public os::View
102 {
103  friend class CodeView;
104 private:
105 
106  buffer_type buffer;
107  mutable list<Fold> cFoldedSections;
108  /* Line index = visible line number */
109  /* Buffer index = index in buffer */
110  uint _TranslateBufferIndex( uint32 nLineIndex ) const;
111  uint _TranslateLineNumber( uint32 nBufferIndex ) const;
112  void _FoldSection( uint32 nStart, uint32 nEnd );
113  void _UnfoldSection( uint32 nStart );
114  uint _LineIsFolded( uint32 nBufferIndex );
115  void _AdjustFoldedSections( uint nStart, int nLen );
116 
117  os::Control* control;
118  uint32 eventMask;
119  uint32 eventBuffer;
120 
121  bool mousePressed;
122  bool IcursorActive;
123 
124  Format* format;
125 
126  os::Bitmap* backBM;
127  os::View* backView;
128  void UpdateBackBuffer();
129 
130  float maxWidth;
131  float lineHeight;
132  float lineBase;
133  float spaceWidth;
134 
135  float cursorX; // horisontal pixel pos, NOT char index!
136  uint cursorY; // line number cursor is in
137  float old_cursorX;
138  uint old_cursorY;
139  uint tabSize;
140  bool useTab;
141  uint m_nMargin; // Margin in pixels (for line numbers)
142 
143  float GetW(uint x, uint y) const;
144  uint GetChar(float x, uint line) const;
145 
146  bool selectionValid;
147  os::IPoint selStart,
148  selEnd;
149 
150  void UpdateScrollbars();
151  void UpdateWidth(uint);
152  void UpdateAllWidths();
153  void InvalidateLines(int, int);
154 
155  void SplitLine(vector<os::String>&, const os::String&, const char splitter='\n');
156 
157  void PreMove(bool);
158  void PostMove(bool);
159 
160  void Reformat(uint first, uint last);
161 
162  void IndentSelection(bool unindent);
163 
164  bool enabled;
165  bool readOnly;
166 
167  void AddUndoNode(uint mode, const os::String &str, uint x, uint y);
168  uint maxUndo;
169  uint undoCount;
170  uint redoCount;
171  UndoNode *undoHead,
172  *undoTail,
173  *undoCurrent;
174 
175  os::Menu* m_pcContextMenu;
176  os::Color32_s sHighlight;
177  os::Color32_s m_sLineNumberFg;
178  os::Color32_s m_sLineNumberBg;
179  os::Color32_s m_sLineBackColor;
180 
181 public:
182 
183  InnerEdit(os::Control* c);
184  ~InnerEdit();
185 
186  void SetText(const os::String &);
187 
188  void GetText(os::String*, uint startx, uint starty, uint endx, uint endy) const;
189  void GetText(os::String *str, const os::IPoint &p0, const os::IPoint &p1) const{
190  GetText(str, p0.x, p0.y, p1.x, p1.y);
191  }
192  void InsertText(const os::String &, uint x, uint y, bool addUndo=true);
193  void InsertText(const os::String &str, const os::IPoint &p){
194  InsertText(str, p.x, p.y);
195  }
196  void RemoveText(uint startx, uint starty, uint startx, uint starty, bool addUndo=true);
197  void RemoveText(const os::IPoint &p0, const os::IPoint &p1){
198  RemoveText(p0.x, p0.y, p1.x, p1.y);
199  }
200  const os::String& GetLine(uint y)const { return buffer[y].text; }
201  void SetLine(const os::String &, uint y, bool addUndo=true);
202  uint GetLineCount() const{ return buffer.size()-1; }
203 
204  void SetFormat(Format *);
205  Format* GetFormat() const{ return format; }
206 
207  void SetTabSize(uint);
208  uint GetTabSize() const{ return tabSize; }
209  void SetUseTab(bool b){ useTab=b; }
210  bool GetUseTab(){ return useTab; }
211 
212  void SetCursor(uint x, uint y, bool select=false);
213  void SetCursor(const os::IPoint &p, bool select=false){
214  SetCursor(p.x, p.y, select);
215  }
216  os::IPoint GetCursor() const;
217  void ShowCursor();
218 
219  void MoveLeft(bool select=false);
220  void MoveRight(bool select=false);
221  void MoveUp(bool select=false);
222  void MoveDown(bool select=false);
223  void MoveTop(bool select=false);
224  void MoveBottom(bool select=false);
225  void MovePageUp(bool select=false);
226  void MovePageDown(bool select=false);
227  void MoveLineStart(bool select=false);
228  void MoveLineEnd(bool select=false);
229  void MoveWordLeft(bool select=false);
230  void MoveWordRight(bool select=false);
231 
232  void ScrollLeft();
233  void ScrollRight();
234  void ScrollUp();
235  void ScrollDown();
236  void ScrollPageUp();
237  void ScrollPageDown();
238 
239  void SetSelectionStart(uint x, uint y);
240  void SetSelectionStart(const os::IPoint &p){
241  SetSelectionStart(p.x, p.y);
242  }
243  void SetSelectionEnd(uint x, uint y);
244  void SetSelectionEnd(const os::IPoint &p){
245  SetSelectionEnd(p.x, p.y);
246  }
247  void SetSelection(const os::IPoint &p0, const os::IPoint &p1){
248  SetSelectionStart(p0.x, p0.y);
249  SetSelectionEnd(p1.x, p1.y);
250  }
251  void SelectAll(){
252  SetSelectionStart(0,0);
253  SetSelectionEnd(buffer.back().text.size(), buffer.size());
254  }
255  void ClearSelection();
256 
257  void Copy() const;
258  void Cut();
259  void Paste();
260  void Del();
261 
262  void SetEnable(bool b);
263  bool GetEnable(){ return enabled; }
264  void SetReadOnly(bool);
265  bool GetReadOnly() { return readOnly; }
266 
267  void SetShowLineNumbers( bool bShowLineNumbers );
268  bool GetShowLineNumbers( ) { return ( m_nMargin != 0 ); }
269 
270  size_t GetLength();
271 
272  void CommitEvents();
273  void SetEventMask( uint32 nMask ) { eventMask = nMask; }
274  uint32 GetEventMask() { return eventMask; }
275 
276  void SetMaxUndoSize(int i);
277  int GetMaxUndoSize();
278  bool Undo();
279  bool Redo();
280  bool UndoAvailable();
281  bool RedoAvailable();
282  void ClearUndo();
283 
284  virtual void Paint(const os::Rect&);
285  virtual void FrameSized(const os::Point &);
286  virtual void Activated(bool);
287  virtual void KeyDown(const char *, const char *, uint32);
288  virtual void FontChanged(os::Font *);
289  virtual void MouseDown(const os::Point &, uint32);
290  virtual void MouseUp(const os::Point &, uint32, os::Message*);
291  virtual void MouseMove(const os::Point &, int, uint32, os::Message*);
292  virtual void WheelMoved(const os::Point&);
293 
294  void FoldSection( uint nFirst, uint nLast );
295 
296  void SetContextMenu( os::Menu* );
297  os::Menu* GetContextMenu()
298  {
299  return m_pcContextMenu;
300  }
301 
302  void SetHighlightColor(os::Color32_s sHigh)
303  {
304  sHighlight = sHigh;
305  }
306 
307  os::Color32_s GetHighlightColor()
308  {
309  return sHighlight;
310  }
311 
312  void SetLineNumberBgColor(os::Color32_s sColor)
313  {
314  m_sLineNumberBg = sColor;
315  }
316 
317  os::Color32_s GetLineNumberBgColor()
318  {
319  return m_sLineNumberBg;
320  }
321 
322  void SetLineNumberFgColor(os::Color32_s sColor)
323  {
324  m_sLineNumberFg = sColor;
325  }
326 
327  os::Color32_s GetLineNumberFgColor()
328  {
329  return m_sLineNumberFg;
330  }
331 
332  void SetLineBackColor(os::Color32_s sColor)
333  {
334  m_sLineBackColor = sColor;
335  }
336 
337  os::Color32_s GetLineBackColor()
338  {
339  return m_sLineBackColor;
340  }
341 };
342 
343 } /* namespace cv */
344 
345 #endif
346