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