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