]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
d4744a77ba58f479582cc6a6026d70f61c4f2d7e
[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 /// return the background colour for setting colour of window
163 wxColour const *GetBGColour(void) const { return m_ColourBG; }
164 private:
165 /// the font to use
166 wxFont *m_font;
167 /// foreground colour
168 wxColour const *m_ColourFG;
169 /// background colour
170 wxColour const *m_ColourBG;
171 };
172
173 /// this object doesn't do anything at all
174 class wxLayoutObjectLineBreak : public wxLayoutObjectBase
175 {
176 public:
177 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_LINEBREAK; }
178 };
179
180
181 /**
182 This class provides a high level abstraction to the wxFText
183 classes.
184 It handles most of the character events with its own callback
185 functions, providing an editing ability. All events which cannot be
186 handled get passed to the parent window's handlers.
187 */
188 class wxLayoutList : public wxLayoutObjectList
189 {
190 public:
191 wxLayoutList();
192
193 /// Destructor.
194 ~wxLayoutList();
195
196 /// adds an object:
197 void AddObject(wxLayoutObjectBase *obj);
198 void AddText(String const &txt);
199
200 void LineBreak(void);
201 void SetFont(int family, int size, int style,
202 int weight, int underline,
203 wxColour const *fg,
204 wxColour const *bg);
205 void SetFont(int family=-1, int size = -1, int style=-1,
206 int weight=-1, int underline = -1,
207 char const *fg = NULL,
208 char const *bg = NULL);
209 inline void SetFontFamily(int family) { SetFont(family); }
210 inline void SetFontSize(int size) { SetFont(-1,size); }
211 inline void SetFontStyle(int style) { SetFont(-1,-1,style); }
212 inline void SetFontWeight(int weight) { SetFont(-1,-1,-1,weight); }
213 inline void SetFontUnderline(bool ul) { SetFont(-1,-1,-1,-1,(int)ul); }
214 inline void SetFontColour(char const *fg, char const *bg = NULL) { SetFont(-1,-1,-1,-1,-1,fg,bg); }
215
216
217 /** Draw the list on a given DC.
218 @param findObject if true, return the object occupying the
219 position specified by coords
220 @param coords position where to find the object
221 @return if findObject == true, the object or NULL
222 */
223 wxLayoutObjectBase *Draw(wxDC &dc, bool findObject = false,
224 wxPoint const &coords = wxPoint(0,0));
225
226 #ifdef WXLAYOUT_DEBUG
227 void Debug(void);
228 #endif
229
230
231 /// for access by wxLayoutWindow:
232 void GetSize(CoordType *max_x, CoordType *max_y,
233 CoordType *lineHeight);
234
235 /**@name Functionality for editing */
236 //@{
237 /// set list editable or read only
238 void SetEditable(bool editable = true) { m_Editable = true; }
239 /// move cursor
240 void MoveCursor(int dx = 0, int dy = 0);
241 void SetCursor(wxPoint const &p) { m_CursorPosition = p; }
242 /// delete one or more cursor positions
243 void Delete(CoordType count = 1);
244 void Insert(String const &text);
245 void Insert(wxLayoutObjectBase *obj);
246 void Clear(int family = wxROMAN, int size=12, int style=wxNORMAL, int weight=wxNORMAL,
247 int underline=0, char const *fg="black", char const *bg="white");
248
249 /// return a pointer to the default settings:
250 wxLayoutObjectCmd const *GetDefaults(void) const { return m_DefaultSetting ; }
251
252 //@}
253 protected:
254 /// font parameters:
255 int m_FontFamily, m_FontStyle, m_FontWeight;
256 int m_FontPtSize;
257 bool m_FontUnderline;
258 /// colours:
259 wxColour const * m_ColourFG;
260 wxColour const * m_ColourBG;
261 /// the default setting:
262 wxLayoutObjectCmd *m_DefaultSetting;
263
264 /// needs recalculation?
265 bool m_dirty;
266
267 // the currently updated line:
268 /// where do we draw next:
269 wxPoint m_Position;
270 /// the height of the current line:
271 CoordType m_LineHeight;
272 /// maximum drawn x position so far
273 CoordType m_MaxX;
274 /// maximum drawn y position:
275 CoordType m_MaxY;
276
277 //---- this is needed for editing:
278 /// where is the text cursor:
279 wxPoint m_CursorPosition;
280 /// which is the last line
281 CoordType m_MaxLine;
282 /// can we edit it?
283 bool m_Editable;
284 /// find the object to the cursor position and returns the offset
285 /// in there
286 wxLayoutObjectList::iterator FindObjectCursor(wxPoint const &cpos, CoordType *offset = NULL);
287 wxLayoutObjectList::iterator FindCurrentObject(CoordType *offset = NULL);
288 // get the length of the line with the object pointed to by i
289 CoordType GetLineLength(wxLayoutObjectList::iterator i);
290
291 };
292
293 #endif // WXLLIST_H