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