]>
git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
512b9117df45e58a09cad99cd8c17468cc0f30ff
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 /// Types of currently supported layout objects.
48 enum wxLayoutObjectType
49 { WXLO_TYPE_INVALID
= 0, WXLO_TYPE_TEXT
, WXLO_TYPE_CMD
, WXLO_TYPE_ICON
, WXLO_TYPE_LINEBREAK
};
51 /// Type used for coordinates in drawing.
52 typedef long CoordType
;
55 class wxLayoutObjectBase
;
61 /** The base class defining the interface to each object which can be
62 part of the layout. Each object needs to draw itself and calculate
65 class wxLayoutObjectBase
70 virtual ~UserData() { }
73 /// return the type of this object
74 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_INVALID
; } ;
75 /** Calculates the position etc of an object.
76 @param dc the wxDC to draw on
77 @param position where to draw the top left corner
78 @param baseLine the baseline for alignment, from top of box
80 virtual void Layout(wxDC
& dc
,
82 CoordType baseLine
) {}
85 @param dc the wxDC to draw on
87 virtual void Draw(wxDC
& dc
) {}
89 /** Calculates and returns the size of the object.
90 @param baseLine pointer where to store the baseline position of
91 this object (i.e. the height from the top of the box to the
93 @return the size of the object's box in pixels
95 virtual wxPoint
GetSize(CoordType
* baseLine
= NULL
) const
96 { return wxPoint(0,0); }
98 /** Calculates and returns the position of the object.
99 @return the size of the object's box in pixels
101 virtual wxPoint
GetPosition(void) const { return m_Position
; }
103 /// returns the number of cursor positions occupied by this object
104 virtual CoordType
CountPositions(void) const { return 1; }
107 wxLayoutObjectBase() { m_UserData
= NULL
; }
108 /// delete the user data
109 virtual ~wxLayoutObjectBase() { delete m_UserData
; }
111 #ifdef WXLAYOUT_DEBUG
112 virtual void Debug(void);
115 /// query whether coordinates have changed since last drawing
116 virtual bool IsDirty(void) const { return true; }
118 /** Tells the object about some user data. This data is associated
119 with the object and will be deleted at destruction time.
121 void SetUserData(UserData
*data
) { m_UserData
= data
; }
122 /** Return the user data. */
123 void * GetUserData(void) const { return m_UserData
; }
126 /// optional data for application's use
127 UserData
*m_UserData
;
132 /// Define a list type of wxLayoutObjectBase pointers.
133 KBLIST_DEFINE(wxLayoutObjectList
, wxLayoutObjectBase
);
136 /// object for text block
137 class wxLayoutObjectText
: public wxLayoutObjectBase
140 wxLayoutObjectText(const String
&txt
);
142 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_TEXT
; }
143 virtual void Layout(wxDC
&dc
, wxPoint position
, CoordType baseLine
);
144 virtual void Draw(wxDC
&dc
);
145 /** This returns the height and in baseLine the position of the
146 text's baseline within it's box. This is needed to properly
149 virtual wxPoint
GetSize(CoordType
*baseLine
= NULL
) const;
151 #ifdef WXLAYOUT_DEBUG
152 virtual void Debug(void);
155 virtual CoordType
CountPositions(void) const { return strlen(m_Text
.c_str()); }
156 virtual bool IsDirty(void) const { return m_IsDirty
; }
159 String
& GetText(void) { return m_Text
; }
160 void SetText(String
const &text
) { m_Text
= text
; }
164 /// size of the box containing text
165 long m_Width
, m_Height
;
166 /// the position of the baseline counted from the top of the box
168 /// coordinates have changed
173 class wxLayoutObjectIcon
: public wxLayoutObjectBase
176 wxLayoutObjectIcon(wxIcon
*icon
);
177 wxLayoutObjectIcon(wxIcon
const &icon
);
179 ~wxLayoutObjectIcon() { delete m_Icon
; }
181 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_ICON
; }
182 virtual void Layout(wxDC
&dc
, wxPoint position
, CoordType baseLine
);
183 virtual void Draw(wxDC
&dc
);
185 virtual wxPoint
GetSize(CoordType
*baseLine
= NULL
) const;
186 virtual bool IsDirty(void) const { return m_IsDirty
; }
190 /// coordinates have changed
194 /// for export to html:
195 struct wxLayoutStyleInfo
197 int size
, family
, style
, weight
;
199 unsigned fg_red
, fg_green
, fg_blue
;
200 unsigned bg_red
, bg_green
, bg_blue
;
203 /// pseudo-object executing a formatting command in Draw()
204 class wxLayoutObjectCmd
: public wxLayoutObjectBase
207 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_CMD
; }
208 virtual void Draw(wxDC
&dc
);
209 virtual void Layout(wxDC
&dc
, wxPoint position
, CoordType baseLine
);
210 wxLayoutObjectCmd(int size
, int family
, int style
, int weight
,
212 wxColour
const *fg
, wxColour
const *bg
);
213 ~wxLayoutObjectCmd();
214 /// caller must free pointer:
215 wxLayoutStyleInfo
*GetStyle(void) const ;
216 /// return the background colour for setting colour of window
217 wxColour
const *GetBGColour(void) const { return m_ColourBG
; }
221 /// foreground colour
222 wxColour
const *m_ColourFG
;
223 /// background colour
224 wxColour
const *m_ColourBG
;
227 /// this object doesn't do anything at all
228 class wxLayoutObjectLineBreak
: public wxLayoutObjectBase
231 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_LINEBREAK
; }
235 class wxLayoutPrintout
;
238 This class provides a high level abstraction to the wxFText
240 It handles most of the character events with its own callback
241 functions, providing an editing ability. All events which cannot be
242 handled get passed to the parent window's handlers.
244 class wxLayoutList
: public wxLayoutObjectList
253 void AddObject(wxLayoutObjectBase
*obj
);
254 /// adds a text object
255 void AddText(String
const &txt
);
256 /// adds a line break
257 void LineBreak(void);
258 /// sets font parameters
259 void SetFont(int family
, int size
, int style
,
260 int weight
, int underline
,
263 /// sets font parameters, colours by name
264 void SetFont(int family
=-1, int size
= -1, int style
=-1,
265 int weight
=-1, int underline
= -1,
266 char const *fg
= NULL
,
267 char const *bg
= NULL
);
269 inline void SetFontFamily(int family
) { SetFont(family
); }
271 inline void SetFontSize(int size
) { SetFont(-1,size
); }
273 inline void SetFontStyle(int style
) { SetFont(-1,-1,style
); }
275 inline void SetFontWeight(int weight
) { SetFont(-1,-1,-1,weight
); }
276 /// toggle underline flag
277 inline void SetFontUnderline(bool ul
) { SetFont(-1,-1,-1,-1,(int)ul
); }
278 /// set font colours by name
279 inline void SetFontColour(char const *fg
, char const *bg
= NULL
) { SetFont(-1,-1,-1,-1,-1,fg
,bg
); }
282 /** Re-layouts the list on a DC.
283 @param findObject if true, return the object occupying the
284 position specified by coords
285 @param coords position where to find the object
286 @param pageNo if > 0, print only that page of a document (for
288 @param reallyDraw set this to false if you don't want to draw but
289 just calculate the coordinates
290 @param hasDrawn set to true if a page has been printed
291 @return if findObject == true, the object or NULL
293 void Layout(wxDC
&dc
);
295 /** Draw the list on a given DC.
296 @param pageNo if > 0, print only that page of a document (for
300 CoordType fromLine
= -1,
301 CoordType toLine
= -1,
302 iterator start
= iterator(NULL
));
304 /** Deletes at least to the end of line and redraws */
305 void EraseAndDraw(wxDC
&dc
, iterator start
= iterator(NULL
));
307 /** Finds the object occupying a certain screen position.
308 @return pointer to wxLayoutObjectBase or NULL if not found
310 wxLayoutObjectBase
*Find(wxPoint coords
) const;
312 #ifdef WXLAYOUT_DEBUG
314 void ShowCurrentObject();
318 bool IsDirty() const { return m_bModified
; }
319 bool CursorMoved(void) const { return m_CursorMoved
; }
321 /// called after the contents is saved, for example
322 void ResetDirty() { m_bModified
= FALSE
; }
324 /// for access by wxLayoutWindow:
325 void GetSize(CoordType
*max_x
, CoordType
*max_y
,
326 CoordType
*lineHeight
);
329 /**@name Functionality for editing */
331 /// set list editable or read only
332 void SetEditable(bool editable
= true) { m_Editable
= editable
; }
333 /// return true if list is editable
334 bool IsEditable(void) const { return m_Editable
; }
335 /// move cursor, returns true if it could move to the desired position
336 bool MoveCursor(int dx
= 0, int dy
= 0);
337 void SetCursor(wxPoint
const &p
) { m_CursorPosition
= p
; }
338 void DrawCursor(wxDC
&dc
, bool erase
= false);
340 /// Get current cursor position cursor coords
341 wxPoint
GetCursor(void) const { return m_CursorPosition
; }
342 /// Gets graphical coordinates of cursor
343 wxPoint
GetCursorCoords(void) const { return m_CursorCoords
; }
345 /// delete one or more cursor positions
346 void Delete(CoordType count
= 1);
347 void Insert(String
const &text
);
348 void Insert(wxLayoutObjectBase
*obj
);
349 void Clear(int family
= wxROMAN
, int size
=12, int style
=wxNORMAL
, int weight
=wxNORMAL
,
350 int underline
=0, char const *fg
="black", char const *bg
="white");
352 /// return a pointer to the default settings:
353 wxLayoutObjectCmd
const *GetDefaults(void) const { return m_DefaultSetting
; }
355 wxLayoutObjectList::iterator
FindCurrentObject(CoordType
*offset
= NULL
);
356 // get the length of the line with the object pointed to by i, offs
357 // only used to decide whether we are before or after linebreak
358 CoordType
GetLineLength(wxLayoutObjectList::iterator i
,
360 wxLayoutPrintout
*MakePrintout(wxString
const &name
);
362 /// Return maximum X,Y coordinates
363 wxPoint
GetSize(void) const { return wxPoint(m_MaxX
, m_MaxY
); }
367 int m_FontFamily
, m_FontStyle
, m_FontWeight
;
369 bool m_FontUnderline
;
371 wxColour
const * m_ColourFG
;
372 wxColour
const * m_ColourBG
;
373 /// the default setting:
374 wxLayoutObjectCmd
*m_DefaultSetting
;
376 /// needs recalculation?
381 /// needs saving (i.e., was modified?)
384 // the currently updated line:
385 /// where do we draw next:
387 /// the height of the current line:
388 CoordType m_LineHeight
;
389 /// maximum drawn x position so far
391 /// maximum drawn y position:
394 //---- this is needed for editing:
395 /// where is the text cursor (column,line):
396 wxPoint m_CursorPosition
;
397 /// where to draw the cursor
398 wxPoint m_CursorCoords
;
399 /// how large to draw it
400 wxPoint m_CursorSize
;
402 /// to store content overwritten by cursor
403 wxMemoryDC m_CursorMemDC
;
405 /// which is the last line
409 /// find the object to the cursor position and returns the offset
411 wxLayoutObjectList::iterator
FindObjectCursor(wxPoint
*cpos
, CoordType
*offset
= NULL
);
413 /// Resets the font settings etc to default values
414 void ResetSettings(wxDC
&dc
);
415 /// calculates current cursor coordinates, called in Layout()
416 void CalculateCursor(wxDC
&dc
);
417 /// remembers the last cursor position for which FindObjectCursor was called
418 wxPoint m_FoundCursor
;
419 /// remembers the iterator to the object related to m_FoundCursor
420 wxLayoutObjectList::iterator m_FoundIterator
;
423 class wxLayoutPrintout
: public wxPrintout
426 wxLayoutPrintout(wxLayoutList
&llist
, wxString
const & title
= "My printout")
428 { m_llist
= &llist
; m_maxPage
= 0; }
429 bool OnPrintPage(int page
);
430 bool HasPage(int page
);
431 bool OnBeginDocument(int startPage
, int endPage
);
432 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int
434 void OnPreparePrinting(void);
436 wxLayoutList
*m_llist
;