]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
Looks like I've fixed the editing
[wxWidgets.git] / user / wxLayout / wxllist.h
1 /*-*- c++ -*-********************************************************
2 * wxLayoutList.h - a formatted text rendering engine for wxWindows *
3 * *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8 #ifndef WXLLIST_H
9 #define WXLLIST_H
10
11 #ifdef __GNUG__
12 # pragma interface "wxllist.h"
13 #endif
14
15 #include "kbList.h"
16
17 #include "wx/wx.h"
18
19 // skip the following defines if embedded in M application
20 #ifdef M_BASEDIR
21 # ifdef DEBUG
22 //# define WXLAYOUT_DEBUG
23 # endif
24 #else
25 // for testing only:
26 # define WXLAYOUT_DEBUG
27 // The wxLayout classes can be compiled with std::string instead of wxString
28 //# define USE_STD_STRING
29 #endif
30
31 #ifdef USE_STD_STRING
32 # include <string>
33 typedef std::string String;
34 # define Str(str)(str.c_str())
35 #else
36 typedef wxString String;
37 # define Str(str) str
38 #endif
39
40 /// Types of currently supported layout objects.
41 enum wxLayoutObjectType
42 { WXLO_TYPE_INVALID = 0, WXLO_TYPE_TEXT, WXLO_TYPE_CMD, WXLO_TYPE_ICON, WXLO_TYPE_LINEBREAK };
43
44 /// Type used for coordinates in drawing.
45 typedef long CoordType;
46
47 class wxLayoutList;
48 class wxLayoutObjectBase;
49
50 class wxDC;
51 class wxColour;
52 class wxFont;
53
54 /** The base class defining the interface to each object which can be
55 part of the layout. Each object needs to draw itself and calculate
56 its size.
57 */
58 class wxLayoutObjectBase
59 {
60 public:
61 /// return the type of this object
62 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_INVALID; } ;
63 /** Draws an object.
64 @param dc the wxDC to draw on
65 @param position where to draw the top left corner
66 @param baseLine the baseline for alignment, from top of box
67 @draw if set to false, do not draw but just calculate sizes
68 */
69 virtual void Draw(wxDC &dc, wxPoint position, CoordType baseLine,
70 bool draw = true) {};
71
72 /** Calculates and returns the size of the object. May need to be
73 called twice to work.
74 @param baseLine pointer where to store the baseline position of
75 this object (i.e. the height from the top of the box to the
76 baseline)
77 @return the size of the object's box in pixels
78 */
79 virtual wxPoint GetSize(CoordType *baseLine) const { return
80 wxPoint(0,0); };
81 /// returns the number of cursor positions occupied by this object
82 virtual CoordType CountPositions(void) const { return 1; }
83
84 /// constructor
85 wxLayoutObjectBase() { m_UserData = NULL; }
86 /// note: any user data will be freed at the time the object is deleted
87 virtual ~wxLayoutObjectBase() { if(m_UserData) delete m_UserData; }
88 #ifdef WXLAYOUT_DEBUG
89 virtual void Debug(void);
90 #endif
91
92 /** Tells the object about some user data. This data is associated
93 with the object and will be deleted at destruction time.
94 */
95 void SetUserData(void *data) { m_UserData = data; }
96 /** Return the user data. */
97 void * GetUserData(void) const { return m_UserData; }
98 private:
99 /// optional data for application's use
100 void * m_UserData;
101 };
102
103 /// Define a list type of wxLayoutObjectBase pointers.
104 KBLIST_DEFINE(wxLayoutObjectList, wxLayoutObjectBase);
105
106
107 /// object for text block
108 class wxLayoutObjectText : public wxLayoutObjectBase
109 {
110 public:
111 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_TEXT; }
112 virtual void Draw(wxDC &dc, wxPoint position, CoordType baseLine,
113 bool draw = true);
114 /** This returns the height and in baseLine the position of the
115 text's baseline within it's box. This is needed to properly
116 align text objects.
117 */
118 virtual wxPoint GetSize(CoordType *baseLine) const;
119 #ifdef WXLAYOUT_DEBUG
120 virtual void Debug(void);
121 #endif
122
123 wxLayoutObjectText(const String &txt);
124 virtual CoordType CountPositions(void) const { return strlen(m_Text.c_str()); }
125
126 // for editing:
127 String & GetText(void) { return m_Text; }
128 void SetText(String const &text) { m_Text = text; }
129 private:
130 String m_Text;
131 /// size of the box containing text
132 long m_Width, m_Height;
133 /// the position of the baseline counted from the top of the box
134 long m_BaseLine;
135 };
136
137 /// icon/pictures:
138 class wxLayoutObjectIcon : public wxLayoutObjectBase
139 {
140 public:
141 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_ICON; }
142 virtual void Draw(wxDC &dc, wxPoint position, CoordType baseLine,
143 bool draw = true);
144 virtual wxPoint GetSize(CoordType *baseLine) const;
145 wxLayoutObjectIcon(wxIcon *icon);
146
147 private:
148 wxIcon *m_Icon;
149 };
150
151 /// for export to html:
152 struct wxLayoutStyleInfo
153 {
154 int size, family, style, weight;
155 bool underline;
156 unsigned fg_red, fg_green, fg_blue;
157 unsigned bg_red, bg_green, bg_blue;
158 };
159
160 /// pseudo-object executing a formatting command in Draw()
161 class wxLayoutObjectCmd : public wxLayoutObjectBase
162 {
163 public:
164 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_CMD; }
165 virtual void Draw(wxDC &dc, wxPoint position, CoordType baseLine,
166 bool draw = true);
167 wxLayoutObjectCmd(int size, int family, int style, int weight,
168 bool underline,
169 wxColour const *fg, wxColour const *bg);
170 ~wxLayoutObjectCmd();
171 /// caller must free pointer:
172 wxLayoutStyleInfo *GetStyle(void) const ;
173 /// return the background colour for setting colour of window
174 wxColour const *GetBGColour(void) const { return m_ColourBG; }
175 private:
176 /// the font to use
177 wxFont *m_font;
178 /// foreground colour
179 wxColour const *m_ColourFG;
180 /// background colour
181 wxColour const *m_ColourBG;
182 };
183
184 /// this object doesn't do anything at all
185 class wxLayoutObjectLineBreak : public wxLayoutObjectBase
186 {
187 public:
188 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_LINEBREAK; }
189 };
190
191
192 /**
193 This class provides a high level abstraction to the wxFText
194 classes.
195 It handles most of the character events with its own callback
196 functions, providing an editing ability. All events which cannot be
197 handled get passed to the parent window's handlers.
198 */
199 class wxLayoutList : public wxLayoutObjectList
200 {
201 public:
202 wxLayoutList();
203
204 /// Destructor.
205 ~wxLayoutList();
206
207 /// adds an object:
208 void AddObject(wxLayoutObjectBase *obj);
209 void AddText(String const &txt);
210
211 void LineBreak(void);
212 void SetFont(int family, int size, int style,
213 int weight, int underline,
214 wxColour const *fg,
215 wxColour const *bg);
216 void SetFont(int family=-1, int size = -1, int style=-1,
217 int weight=-1, int underline = -1,
218 char const *fg = NULL,
219 char const *bg = NULL);
220 inline void SetFontFamily(int family) { SetFont(family); }
221 inline void SetFontSize(int size) { SetFont(-1,size); }
222 inline void SetFontStyle(int style) { SetFont(-1,-1,style); }
223 inline void SetFontWeight(int weight) { SetFont(-1,-1,-1,weight); }
224 inline void SetFontUnderline(bool ul) { SetFont(-1,-1,-1,-1,(int)ul); }
225 inline void SetFontColour(char const *fg, char const *bg = NULL) { SetFont(-1,-1,-1,-1,-1,fg,bg); }
226
227
228 /** Draw the list on a given DC.
229 @param findObject if true, return the object occupying the
230 position specified by coords
231 @param coords position where to find the object
232 @return if findObject == true, the object or NULL
233 */
234 wxLayoutObjectBase *Draw(wxDC &dc, bool findObject = false,
235 wxPoint const &coords = wxPoint(0,0));
236
237 #ifdef WXLAYOUT_DEBUG
238 void Debug(void);
239 #endif
240
241
242 /// for access by wxLayoutWindow:
243 void GetSize(CoordType *max_x, CoordType *max_y,
244 CoordType *lineHeight);
245
246 /**@name Functionality for editing */
247 //@{
248 /// set list editable or read only
249 void SetEditable(bool editable = true) { m_Editable = editable; }
250 /// return true if list is editable
251 bool IsEditable(void) const { return m_Editable; }
252 /// move cursor, returns true if it could move to the desired position
253 bool MoveCursor(int dx = 0, int dy = 0);
254 void SetCursor(wxPoint const &p) { m_CursorPosition = p; }
255 wxPoint GetCursor(void) const { return m_CursorPosition; }
256 /// delete one or more cursor positions
257 void Delete(CoordType count = 1);
258 void Insert(String const &text);
259 void Insert(wxLayoutObjectBase *obj);
260 void Clear(int family = wxROMAN, int size=12, int style=wxNORMAL, int weight=wxNORMAL,
261 int underline=0, char const *fg="black", char const *bg="white");
262
263 /// return a pointer to the default settings:
264 wxLayoutObjectCmd const *GetDefaults(void) const { return m_DefaultSetting ; }
265
266 wxLayoutObjectList::iterator FindCurrentObject(CoordType *offset = NULL);
267 // get the length of the line with the object pointed to by i, offs
268 // only used to decide whether we are before or after linebreak
269 CoordType GetLineLength(wxLayoutObjectList::iterator i,
270 CoordType offs = 0);
271 //@}
272 protected:
273 /// font parameters:
274 int m_FontFamily, m_FontStyle, m_FontWeight;
275 int m_FontPtSize;
276 bool m_FontUnderline;
277 /// colours:
278 wxColour const * m_ColourFG;
279 wxColour const * m_ColourBG;
280 /// the default setting:
281 wxLayoutObjectCmd *m_DefaultSetting;
282
283 /// needs recalculation?
284 bool m_dirty;
285
286 // the currently updated line:
287 /// where do we draw next:
288 wxPoint m_Position;
289 /// the height of the current line:
290 CoordType m_LineHeight;
291 /// maximum drawn x position so far
292 CoordType m_MaxX;
293 /// maximum drawn y position:
294 CoordType m_MaxY;
295
296 //---- this is needed for editing:
297 /// where is the text cursor:
298 wxPoint m_CursorPosition;
299 /// which is the last line
300 CoordType m_MaxLine;
301 /// can we edit it?
302 bool m_Editable;
303 /// find the object to the cursor position and returns the offset
304 /// in there
305 wxLayoutObjectList::iterator FindObjectCursor(wxPoint *cpos, CoordType *offset = NULL);
306
307 };
308
309 #endif // WXLLIST_H