]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
faa0fcfa15a8d5e68f95916b27a935253e58fba7
[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 # define WXMENU_LAYOUT_LCLICK 1111
34 # define WXMENU_LAYOUT_RCLICK 1112
35 # define WXMENU_LAYOUT_DBLCLICK 1113
36 #endif
37
38 #ifdef USE_STD_STRING
39 # include <string>
40 typedef std::string String;
41 # define Str(str)(str.c_str())
42 #else
43 typedef wxString String;
44 # define Str(str) str
45 #endif
46
47 #define WXLO_DEFAULTFONTSIZE 12
48
49 /// Types of currently supported layout objects.
50 enum wxLayoutObjectType
51 { WXLO_TYPE_INVALID = 0, WXLO_TYPE_TEXT, WXLO_TYPE_CMD, WXLO_TYPE_ICON, WXLO_TYPE_LINEBREAK };
52
53 /// Type used for coordinates in drawing.
54 typedef long CoordType;
55
56 class wxLayoutList;
57 class wxLayoutObjectBase;
58
59 class wxDC;
60 class wxColour;
61 class wxFont;
62
63 /** The base class defining the interface to each object which can be
64 part of the layout. Each object needs to draw itself and calculate
65 its size.
66 */
67 class wxLayoutObjectBase
68 {
69 public:
70 struct UserData
71 {
72 virtual ~UserData() { }
73 };
74
75 /// return the type of this object
76 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_INVALID; } ;
77 /** Calculates the position etc of an object.
78 @param dc the wxDC to draw on
79 @param position where to draw the top left corner
80 @param baseLine the baseline for alignment, from top of box
81 */
82 virtual void Layout(wxDC & dc,
83 wxPoint position,
84 CoordType baseLine) {}
85
86 /** Draws an object.
87 @param dc the wxDC to draw on
88 @param translation to be added to coordinates
89 */
90 virtual void Draw(wxDC & dc, wxPoint const &translate) {}
91
92 /** Calculates and returns the size of the object.
93 @param baseLine pointer where to store the baseline position of
94 this object (i.e. the height from the top of the box to the
95 baseline)
96 @return the size of the object's box in pixels
97 */
98 virtual wxPoint GetSize(CoordType * baseLine = NULL) const
99 { return wxPoint(0,0); }
100
101 /** Calculates and returns the position of the object.
102 @return the size of the object's box in pixels
103 */
104 virtual wxPoint GetPosition(void) const { return m_Position; }
105
106 /// returns the number of cursor positions occupied by this object
107 virtual CoordType CountPositions(void) const { return 1; }
108
109 /// constructor
110 wxLayoutObjectBase() { m_UserData = NULL; }
111 /// delete the user data
112 virtual ~wxLayoutObjectBase() { delete m_UserData; }
113
114 #ifdef WXLAYOUT_DEBUG
115 virtual void Debug(void);
116 #endif
117
118 /// query whether coordinates have changed since last drawing
119 virtual bool IsDirty(void) const { return true; }
120
121 /** Tells the object about some user data. This data is associated
122 with the object and will be deleted at destruction time.
123 */
124 void SetUserData(UserData *data) { m_UserData = data; }
125 /** Return the user data. */
126 void * GetUserData(void) const { return m_UserData; }
127
128 private:
129 /// optional data for application's use
130 UserData *m_UserData;
131 protected:
132 wxPoint m_Position;
133 };
134
135 /// Define a list type of wxLayoutObjectBase pointers.
136 KBLIST_DEFINE(wxLayoutObjectList, wxLayoutObjectBase);
137
138
139 /// object for text block
140 class wxLayoutObjectText : public wxLayoutObjectBase
141 {
142 public:
143 wxLayoutObjectText(const String &txt);
144
145 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_TEXT; }
146 virtual void Layout(wxDC &dc, wxPoint position, CoordType
147 baseLine);
148
149 virtual void Draw(wxDC &dc, wxPoint const &translate);
150 /** This returns the height and in baseLine the position of the
151 text's baseline within it's box. This is needed to properly
152 align text objects.
153 */
154 virtual wxPoint GetSize(CoordType *baseLine = NULL) const;
155
156 #ifdef WXLAYOUT_DEBUG
157 virtual void Debug(void);
158 #endif
159
160 virtual CoordType CountPositions(void) const { return strlen(m_Text.c_str()); }
161 virtual bool IsDirty(void) const { return m_IsDirty; }
162
163 // for editing:
164 String & GetText(void) { return m_Text; }
165 void SetText(String const &text) { m_Text = text; }
166
167 private:
168 String m_Text;
169 /// size of the box containing text
170 long m_Width, m_Height;
171 /// the position of the baseline counted from the top of the box
172 long m_BaseLine;
173 /// coordinates have changed
174 bool m_IsDirty;
175 };
176
177 /// icon/pictures:
178 class wxLayoutObjectIcon : public wxLayoutObjectBase
179 {
180 public:
181 wxLayoutObjectIcon(wxIcon *icon);
182 wxLayoutObjectIcon(wxIcon const &icon);
183
184 ~wxLayoutObjectIcon() { delete m_Icon; }
185
186 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_ICON; }
187 virtual void Layout(wxDC &dc, wxPoint position, CoordType baseLine);
188 virtual void Draw(wxDC &dc, wxPoint const &translate);
189
190 virtual wxPoint GetSize(CoordType *baseLine = NULL) const;
191 virtual bool IsDirty(void) const { return m_IsDirty; }
192
193 private:
194 wxIcon *m_Icon;
195 /// coordinates have changed
196 bool m_IsDirty;
197 };
198
199 /// for export to html:
200 struct wxLayoutStyleInfo
201 {
202 int size, family, style, weight;
203 bool underline;
204 unsigned fg_red, fg_green, fg_blue;
205 unsigned bg_red, bg_green, bg_blue;
206 };
207
208 /// pseudo-object executing a formatting command in Draw()
209 class wxLayoutObjectCmd : public wxLayoutObjectBase
210 {
211 public:
212 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_CMD; }
213 virtual void Draw(wxDC &dc, wxPoint const &translate);
214 virtual void Layout(wxDC &dc, wxPoint position, CoordType baseLine);
215 wxLayoutObjectCmd(int size, int family, int style, int weight,
216 bool underline,
217 wxColour const *fg, wxColour const *bg);
218 ~wxLayoutObjectCmd();
219 /// caller must free pointer:
220 wxLayoutStyleInfo *GetStyle(void) const ;
221 /// return the background colour for setting colour of window
222 wxColour const *GetBGColour(void) const { return m_ColourBG; }
223 private:
224 /// the font to use
225 wxFont *m_font;
226 /// foreground colour
227 wxColour const *m_ColourFG;
228 /// background colour
229 wxColour const *m_ColourBG;
230 };
231
232 /// this object doesn't do anything at all
233 class wxLayoutObjectLineBreak : public wxLayoutObjectBase
234 {
235 public:
236 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_LINEBREAK; }
237 };
238
239
240 class wxLayoutPrintout;
241
242 class wxLayoutMargins
243 {
244 public:
245 wxLayoutMargins() { top = left = 0; bottom = right = -1; }
246 int top;
247 int left;
248 int bottom;
249 int right;
250 };
251
252 /**
253 This class provides a high level abstraction to the wxFText
254 classes.
255 It handles most of the character events with its own callback
256 functions, providing an editing ability. All events which cannot be
257 handled get passed to the parent window's handlers.
258 */
259 class wxLayoutList : public wxLayoutObjectList
260 {
261 public:
262 wxLayoutList();
263
264 /// Destructor.
265 ~wxLayoutList();
266
267 /// adds an object:
268 void AddObject(wxLayoutObjectBase *obj);
269 /// adds a text object
270 void AddText(String const &txt);
271 /// adds a line break
272 void LineBreak(void);
273 /// sets font parameters
274 void SetFont(int family, int size, int style,
275 int weight, int underline,
276 wxColour const *fg,
277 wxColour const *bg);
278 /// sets font parameters, colours by name
279 void SetFont(int family=-1, int size = -1, int style=-1,
280 int weight=-1, int underline = -1,
281 char const *fg = NULL,
282 char const *bg = NULL);
283 /// changes to the next larger font size
284 inline void SetFontLarger(void)
285 { SetFont(-1,(12*m_FontPtSize)/10); }
286 /// changes to the next smaller font size
287 inline void SetFontSmaller(void)
288 { SetFont(-1,(10*m_FontPtSize)/12); }
289
290 /// set font family
291 inline void SetFontFamily(int family) { SetFont(family); }
292 /// set font size
293 inline void SetFontSize(int size) { SetFont(-1,size); }
294 /// set font style
295 inline void SetFontStyle(int style) { SetFont(-1,-1,style); }
296 /// set font weight
297 inline void SetFontWeight(int weight) { SetFont(-1,-1,-1,weight); }
298 /// toggle underline flag
299 inline void SetFontUnderline(bool ul) { SetFont(-1,-1,-1,-1,(int)ul); }
300 /// set font colours by name
301 inline void SetFontColour(char const *fg, char const *bg = NULL) { SetFont(-1,-1,-1,-1,-1,fg,bg); }
302
303
304 /** Re-layouts the list on a DC.
305 @param dc the dc to layout for
306 @param margins if not NULL, use these top and left margins
307 */
308 void Layout(wxDC &dc, wxLayoutMargins *margins = NULL);
309
310 /** Draw the list on a given DC.
311 @param dc the dc to layout for
312 @param fromLine the first graphics line from where to draw
313 @param toLine the last line at which to draw
314 @param start if != iterator(NULL) start drawing from here
315 */
316 void Draw(wxDC &dc,
317 CoordType fromLine = -1,
318 CoordType toLine = -1,
319 iterator start = iterator(NULL),
320 wxPoint const &translate = wxPoint(0,0));
321
322 /** Deletes at least to the end of line and redraws */
323 void EraseAndDraw(wxDC &dc, iterator start = iterator(NULL));
324
325 /** Finds the object occupying a certain screen position.
326 @return pointer to wxLayoutObjectBase or NULL if not found
327 */
328 wxLayoutObjectBase *Find(wxPoint coords) const;
329
330 #ifdef WXLAYOUT_DEBUG
331 void Debug(void);
332 void ShowCurrentObject();
333 #endif
334
335 /// dirty?
336 bool IsDirty() const { return m_bModified; }
337 bool CursorMoved(void) const { return m_CursorMoved; }
338
339 /// called after the contents is saved, for example
340 void ResetDirty() { m_bModified = FALSE; }
341
342 /// for access by wxLayoutWindow:
343 void GetSize(CoordType *max_x, CoordType *max_y,
344 CoordType *lineHeight);
345
346
347 /**@name Functionality for editing */
348 //@{
349 /// set list editable or read only
350 void SetEditable(bool editable = true) { m_Editable = editable; }
351 /// return true if list is editable
352 bool IsEditable(void) const { return m_Editable; }
353 /// move cursor, returns true if it could move to the desired position
354 bool MoveCursor(int dx = 0, int dy = 0);
355 void SetCursor(wxPoint const &p) { m_CursorPos = p; }
356 void DrawCursor(wxDC &dc, bool erase = false);
357
358 /// Get current cursor position cursor coords
359 wxPoint GetCursor(void) const { return m_CursorPos; }
360 /// Gets graphical coordinates of cursor
361 wxPoint GetCursorCoords(void) const { return m_CursorCoords; }
362
363 /// delete one or more cursor positions
364 void Delete(CoordType count = 1);
365 void Insert(String const &text);
366 void Insert(wxLayoutObjectBase *obj);
367 void Clear(int family = wxROMAN, int size=WXLO_DEFAULTFONTSIZE, int style=wxNORMAL, int weight=wxNORMAL,
368 int underline=0, char const *fg="black", char const *bg="white");
369
370 /// return a pointer to the default settings (dangerous, why?) FIXME:
371 wxLayoutObjectCmd const *GetDefaults(void) const { return m_DefaultSetting ; }
372
373 wxLayoutObjectList::iterator FindCurrentObject(CoordType *offset = NULL);
374 // get the length of the line with the object pointed to by i, offs
375 // only used to decide whether we are before or after linebreak
376 CoordType GetLineLength(wxLayoutObjectList::iterator i,
377 CoordType offs = 0);
378 wxLayoutPrintout *MakePrintout(wxString const &name);
379
380 /// Return maximum X,Y coordinates
381 wxPoint GetSize(void) const { return wxPoint(m_MaxX, m_MaxY); }
382 //@}
383 protected:
384 /// font parameters:
385 int m_FontFamily, m_FontStyle, m_FontWeight;
386 int m_FontPtSize;
387 bool m_FontUnderline;
388 /// colours:
389 wxColour const * m_ColourFG;
390 wxColour const * m_ColourBG;
391 /// the default setting:
392 wxLayoutObjectCmd *m_DefaultSetting;
393
394 /// needs recalculation?
395 bool m_dirty;
396 /// cursor moved
397 bool m_CursorMoved;
398
399 /// needs saving (i.e., was modified?)
400 bool m_bModified;
401
402 // the currently updated line:
403 /// where do we draw next:
404 wxPoint m_Position;
405 /// the height of the current line:
406 CoordType m_LineHeight;
407 /// maximum drawn x position so far
408 CoordType m_MaxX;
409 /// maximum drawn y position:
410 CoordType m_MaxY;
411
412 //---- this is needed for editing:
413 /// where is the text cursor (column,line):
414 wxPoint m_CursorPos;
415 /// where to draw the cursor
416 wxPoint m_CursorCoords;
417 /// how large to draw it
418 wxPoint m_CursorSize;
419 /// object iterator for current cursor position:
420 iterator m_CursorObject;
421 /// position of cursor within m_CursorObject:
422 int m_CursorOffset;
423
424 /// to store content overwritten by cursor
425 wxMemoryDC m_CursorMemDC;
426
427 /// which is the last line
428 CoordType m_MaxLine;
429 /// can we edit it?
430 bool m_Editable;
431 /// find the object to the cursor position and returns the offset
432 /// in there
433 wxLayoutObjectList::iterator FindObjectCursor(wxPoint *cpos, CoordType *offset = NULL);
434 private:
435 /// Resets the font settings etc to default values
436 void ResetSettings(wxDC &dc);
437 /// calculates current cursor coordinates, called in Layout()
438 void CalculateCursor(wxDC &dc);
439 /// remembers the last cursor position for which FindObjectCursor was called
440 wxPoint m_FoundCursor;
441 /// remembers the iterator to the object related to m_FoundCursor
442 wxLayoutObjectList::iterator m_FoundIterator;
443 };
444
445 class wxLayoutPrintout: public wxPrintout
446 {
447 public:
448 wxLayoutPrintout(wxLayoutList &llist, wxString const & title =
449 "wxLayout Printout");
450 bool OnPrintPage(int page);
451 bool HasPage(int page);
452 bool OnBeginDocument(int startPage, int endPage);
453 void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int
454 *selPageTo);
455 void OnPreparePrinting(void);
456 protected:
457 virtual void DrawHeader(wxDC &dc, wxPoint topleft, wxPoint bottomright, int pageno);
458
459 private:
460 wxLayoutList *m_llist;
461 wxString m_title;
462 int m_PageHeight, m_PageWidth;
463 // how much we actually print per page
464 int m_PrintoutHeight;
465 wxLayoutMargins m_Margins;
466 int m_NumOfPages;
467 };
468
469 #endif // WXLLIST_H