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