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