1 /*-*- c++ -*-********************************************************
2 * wxLayoutList.h - a formatted text rendering engine for wxWindows *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
7 *******************************************************************/
12 # pragma interface "wxllist.h"
19 #include "wx/printdlg.h"
20 #include "wx/generic/printps.h"
21 #include "wx/generic/prntdlgg.h"
23 // skip the following defines if embedded in M application
26 //# define WXLAYOUT_DEBUG
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
40 typedef std::string String
;
41 # define Str(str)(str.c_str())
43 typedef wxString String
;
47 #define WXLO_DEFAULTFONTSIZE 12
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
};
53 /// Type used for coordinates in drawing.
54 typedef long CoordType
;
57 class wxLayoutObjectBase
;
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
67 class wxLayoutObjectBase
72 virtual ~UserData() { }
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
82 virtual void Layout(wxDC
& dc
,
84 CoordType baseLine
) {}
87 @param dc the wxDC to draw on
88 @param translation to be added to coordinates
90 virtual void Draw(wxDC
& dc
, wxPoint
const &translate
) {}
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
96 @return the size of the object's box in pixels
98 virtual wxPoint
GetSize(CoordType
* baseLine
= NULL
) const
99 { return wxPoint(0,0); }
101 /** Calculates and returns the position of the object.
102 @return the size of the object's box in pixels
104 virtual wxPoint
GetPosition(void) const { return m_Position
; }
106 /// returns the number of cursor positions occupied by this object
107 virtual CoordType
CountPositions(void) const { return 1; }
110 wxLayoutObjectBase() { m_UserData
= NULL
; }
111 /// delete the user data
112 virtual ~wxLayoutObjectBase() { delete m_UserData
; }
114 #ifdef WXLAYOUT_DEBUG
115 virtual void Debug(void);
118 /// query whether coordinates have changed since last drawing
119 virtual bool IsDirty(void) const { return true; }
121 /** Tells the object about some user data. This data is associated
122 with the object and will be deleted at destruction time.
124 void SetUserData(UserData
*data
) { m_UserData
= data
; }
125 /** Return the user data. */
126 void * GetUserData(void) const { return m_UserData
; }
129 /// optional data for application's use
130 UserData
*m_UserData
;
135 /// Define a list type of wxLayoutObjectBase pointers.
136 KBLIST_DEFINE(wxLayoutObjectList
, wxLayoutObjectBase
);
139 /// object for text block
140 class wxLayoutObjectText
: public wxLayoutObjectBase
143 wxLayoutObjectText(const String
&txt
);
145 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_TEXT
; }
146 virtual void Layout(wxDC
&dc
, wxPoint position
, CoordType
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
154 virtual wxPoint
GetSize(CoordType
*baseLine
= NULL
) const;
156 #ifdef WXLAYOUT_DEBUG
157 virtual void Debug(void);
160 virtual CoordType
CountPositions(void) const { return strlen(m_Text
.c_str()); }
161 virtual bool IsDirty(void) const { return m_IsDirty
; }
164 String
& GetText(void) { return m_Text
; }
165 void SetText(String
const &text
) { m_Text
= 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
173 /// coordinates have changed
178 class wxLayoutObjectIcon
: public wxLayoutObjectBase
181 wxLayoutObjectIcon(wxIcon
*icon
);
182 wxLayoutObjectIcon(wxIcon
const &icon
);
184 ~wxLayoutObjectIcon() { delete m_Icon
; }
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
);
190 virtual wxPoint
GetSize(CoordType
*baseLine
= NULL
) const;
191 virtual bool IsDirty(void) const { return m_IsDirty
; }
195 /// coordinates have changed
199 /// for export to html:
200 struct wxLayoutStyleInfo
202 int size
, family
, style
, weight
;
204 unsigned fg_red
, fg_green
, fg_blue
;
205 unsigned bg_red
, bg_green
, bg_blue
;
208 /// pseudo-object executing a formatting command in Draw()
209 class wxLayoutObjectCmd
: public wxLayoutObjectBase
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
,
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
; }
226 /// foreground colour
227 wxColour
const *m_ColourFG
;
228 /// background colour
229 wxColour
const *m_ColourBG
;
232 /// this object doesn't do anything at all
233 class wxLayoutObjectLineBreak
: public wxLayoutObjectBase
236 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_LINEBREAK
; }
240 class wxLayoutPrintout
;
242 class wxLayoutMargins
245 wxLayoutMargins() { top
= left
= 0; bottom
= right
= -1; }
253 This class provides a high level abstraction to the wxFText
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.
259 class wxLayoutList
: public wxLayoutObjectList
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
,
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
);
284 inline void SetFontFamily(int family
) { SetFont(family
); }
286 inline void SetFontSize(int size
) { SetFont(-1,size
); }
288 inline void SetFontStyle(int style
) { SetFont(-1,-1,style
); }
290 inline void SetFontWeight(int weight
) { SetFont(-1,-1,-1,weight
); }
291 /// toggle underline flag
292 inline void SetFontUnderline(bool ul
) { SetFont(-1,-1,-1,-1,(int)ul
); }
293 /// set font colours by name
294 inline void SetFontColour(char const *fg
, char const *bg
= NULL
) { SetFont(-1,-1,-1,-1,-1,fg
,bg
); }
297 /** Re-layouts the list on a DC.
298 @param dc the dc to layout for
299 @param margins if not NULL, use these top and left margins
301 void Layout(wxDC
&dc
, wxLayoutMargins
*margins
= NULL
);
303 /** Draw the list on a given DC.
304 @param dc the dc to layout for
305 @param fromLine the first graphics line from where to draw
306 @param toLine the last line at which to draw
307 @param start if != iterator(NULL) start drawing from here
310 CoordType fromLine
= -1,
311 CoordType toLine
= -1,
312 iterator start
= iterator(NULL
),
313 wxPoint
const &translate
= wxPoint(0,0));
315 /** Deletes at least to the end of line and redraws */
316 void EraseAndDraw(wxDC
&dc
, iterator start
= iterator(NULL
));
318 /** Finds the object occupying a certain screen position.
319 @return pointer to wxLayoutObjectBase or NULL if not found
321 wxLayoutObjectBase
*Find(wxPoint coords
) const;
323 #ifdef WXLAYOUT_DEBUG
325 void ShowCurrentObject();
329 bool IsDirty() const { return m_bModified
; }
330 bool CursorMoved(void) const { return m_CursorMoved
; }
332 /// called after the contents is saved, for example
333 void ResetDirty() { m_bModified
= FALSE
; }
335 /// for access by wxLayoutWindow:
336 void GetSize(CoordType
*max_x
, CoordType
*max_y
,
337 CoordType
*lineHeight
);
340 /**@name Functionality for editing */
342 /// set list editable or read only
343 void SetEditable(bool editable
= true) { m_Editable
= editable
; }
344 /// return true if list is editable
345 bool IsEditable(void) const { return m_Editable
; }
346 /// move cursor, returns true if it could move to the desired position
347 bool MoveCursor(int dx
= 0, int dy
= 0);
348 void SetCursor(wxPoint
const &p
) { m_CursorPosition
= p
; }
349 void DrawCursor(wxDC
&dc
, bool erase
= false);
351 /// Get current cursor position cursor coords
352 wxPoint
GetCursor(void) const { return m_CursorPosition
; }
353 /// Gets graphical coordinates of cursor
354 wxPoint
GetCursorCoords(void) const { return m_CursorCoords
; }
356 /// delete one or more cursor positions
357 void Delete(CoordType count
= 1);
358 void Insert(String
const &text
);
359 void Insert(wxLayoutObjectBase
*obj
);
360 void Clear(int family
= wxROMAN
, int size
=WXLO_DEFAULTFONTSIZE
, int style
=wxNORMAL
, int weight
=wxNORMAL
,
361 int underline
=0, char const *fg
="black", char const *bg
="white");
363 /// return a pointer to the default settings (dangerous, why?) FIXME:
364 wxLayoutObjectCmd
const *GetDefaults(void) const { return m_DefaultSetting
; }
366 wxLayoutObjectList::iterator
FindCurrentObject(CoordType
*offset
= NULL
);
367 // get the length of the line with the object pointed to by i, offs
368 // only used to decide whether we are before or after linebreak
369 CoordType
GetLineLength(wxLayoutObjectList::iterator i
,
371 wxLayoutPrintout
*MakePrintout(wxString
const &name
);
373 /// Return maximum X,Y coordinates
374 wxPoint
GetSize(void) const { return wxPoint(m_MaxX
, m_MaxY
); }
378 int m_FontFamily
, m_FontStyle
, m_FontWeight
;
380 bool m_FontUnderline
;
382 wxColour
const * m_ColourFG
;
383 wxColour
const * m_ColourBG
;
384 /// the default setting:
385 wxLayoutObjectCmd
*m_DefaultSetting
;
387 /// needs recalculation?
392 /// needs saving (i.e., was modified?)
395 // the currently updated line:
396 /// where do we draw next:
398 /// the height of the current line:
399 CoordType m_LineHeight
;
400 /// maximum drawn x position so far
402 /// maximum drawn y position:
405 //---- this is needed for editing:
406 /// where is the text cursor (column,line):
407 wxPoint m_CursorPosition
;
408 /// where to draw the cursor
409 wxPoint m_CursorCoords
;
410 /// how large to draw it
411 wxPoint m_CursorSize
;
413 /// to store content overwritten by cursor
414 wxMemoryDC m_CursorMemDC
;
416 /// which is the last line
420 /// find the object to the cursor position and returns the offset
422 wxLayoutObjectList::iterator
FindObjectCursor(wxPoint
*cpos
, CoordType
*offset
= NULL
);
424 /// Resets the font settings etc to default values
425 void ResetSettings(wxDC
&dc
);
426 /// calculates current cursor coordinates, called in Layout()
427 void CalculateCursor(wxDC
&dc
);
428 /// remembers the last cursor position for which FindObjectCursor was called
429 wxPoint m_FoundCursor
;
430 /// remembers the iterator to the object related to m_FoundCursor
431 wxLayoutObjectList::iterator m_FoundIterator
;
434 class wxLayoutPrintout
: public wxPrintout
437 wxLayoutPrintout(wxLayoutList
&llist
, wxString
const & title
=
440 { m_llist
= &llist
; m_title
= title
;}
441 bool OnPrintPage(int page
);
442 bool HasPage(int page
);
443 bool OnBeginDocument(int startPage
, int endPage
);
444 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int
446 void OnPreparePrinting(void);
448 virtual void DrawHeader(wxDC
&dc
, wxPoint topleft
, wxPoint bottomright
, int pageno
);
451 wxLayoutList
*m_llist
;
453 int m_PageHeight
, m_PageWidth
;
454 // how much we actually print per page
455 int m_PrintoutHeight
;
456 wxLayoutMargins m_Margins
;