]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
512b9117df45e58a09cad99cd8c17468cc0f30ff
[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 /// 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 };
50
51 /// Type used for coordinates in drawing.
52 typedef long CoordType;
53
54 class wxLayoutList;
55 class wxLayoutObjectBase;
56
57 class wxDC;
58 class wxColour;
59 class wxFont;
60
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
63 its size.
64 */
65 class wxLayoutObjectBase
66 {
67 public:
68 struct UserData
69 {
70 virtual ~UserData() { }
71 };
72
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
79 */
80 virtual void Layout(wxDC & dc,
81 wxPoint position,
82 CoordType baseLine) {}
83
84 /** Draws an object.
85 @param dc the wxDC to draw on
86 */
87 virtual void Draw(wxDC & dc) {}
88
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
92 baseline)
93 @return the size of the object's box in pixels
94 */
95 virtual wxPoint GetSize(CoordType * baseLine = NULL) const
96 { return wxPoint(0,0); }
97
98 /** Calculates and returns the position of the object.
99 @return the size of the object's box in pixels
100 */
101 virtual wxPoint GetPosition(void) const { return m_Position; }
102
103 /// returns the number of cursor positions occupied by this object
104 virtual CoordType CountPositions(void) const { return 1; }
105
106 /// constructor
107 wxLayoutObjectBase() { m_UserData = NULL; }
108 /// delete the user data
109 virtual ~wxLayoutObjectBase() { delete m_UserData; }
110
111 #ifdef WXLAYOUT_DEBUG
112 virtual void Debug(void);
113 #endif
114
115 /// query whether coordinates have changed since last drawing
116 virtual bool IsDirty(void) const { return true; }
117
118 /** Tells the object about some user data. This data is associated
119 with the object and will be deleted at destruction time.
120 */
121 void SetUserData(UserData *data) { m_UserData = data; }
122 /** Return the user data. */
123 void * GetUserData(void) const { return m_UserData; }
124
125 private:
126 /// optional data for application's use
127 UserData *m_UserData;
128 protected:
129 wxPoint m_Position;
130 };
131
132 /// Define a list type of wxLayoutObjectBase pointers.
133 KBLIST_DEFINE(wxLayoutObjectList, wxLayoutObjectBase);
134
135
136 /// object for text block
137 class wxLayoutObjectText : public wxLayoutObjectBase
138 {
139 public:
140 wxLayoutObjectText(const String &txt);
141
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
147 align text objects.
148 */
149 virtual wxPoint GetSize(CoordType *baseLine = NULL) const;
150
151 #ifdef WXLAYOUT_DEBUG
152 virtual void Debug(void);
153 #endif
154
155 virtual CoordType CountPositions(void) const { return strlen(m_Text.c_str()); }
156 virtual bool IsDirty(void) const { return m_IsDirty; }
157
158 // for editing:
159 String & GetText(void) { return m_Text; }
160 void SetText(String const &text) { m_Text = text; }
161
162 private:
163 String m_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
167 long m_BaseLine;
168 /// coordinates have changed
169 bool m_IsDirty;
170 };
171
172 /// icon/pictures:
173 class wxLayoutObjectIcon : public wxLayoutObjectBase
174 {
175 public:
176 wxLayoutObjectIcon(wxIcon *icon);
177 wxLayoutObjectIcon(wxIcon const &icon);
178
179 ~wxLayoutObjectIcon() { delete m_Icon; }
180
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);
184
185 virtual wxPoint GetSize(CoordType *baseLine = NULL) const;
186 virtual bool IsDirty(void) const { return m_IsDirty; }
187
188 private:
189 wxIcon *m_Icon;
190 /// coordinates have changed
191 bool m_IsDirty;
192 };
193
194 /// for export to html:
195 struct wxLayoutStyleInfo
196 {
197 int size, family, style, weight;
198 bool underline;
199 unsigned fg_red, fg_green, fg_blue;
200 unsigned bg_red, bg_green, bg_blue;
201 };
202
203 /// pseudo-object executing a formatting command in Draw()
204 class wxLayoutObjectCmd : public wxLayoutObjectBase
205 {
206 public:
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,
211 bool underline,
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; }
218 private:
219 /// the font to use
220 wxFont *m_font;
221 /// foreground colour
222 wxColour const *m_ColourFG;
223 /// background colour
224 wxColour const *m_ColourBG;
225 };
226
227 /// this object doesn't do anything at all
228 class wxLayoutObjectLineBreak : public wxLayoutObjectBase
229 {
230 public:
231 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_LINEBREAK; }
232 };
233
234
235 class wxLayoutPrintout;
236
237 /**
238 This class provides a high level abstraction to the wxFText
239 classes.
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.
243 */
244 class wxLayoutList : public wxLayoutObjectList
245 {
246 public:
247 wxLayoutList();
248
249 /// Destructor.
250 ~wxLayoutList();
251
252 /// adds an object:
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,
261 wxColour const *fg,
262 wxColour const *bg);
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);
268 /// set font family
269 inline void SetFontFamily(int family) { SetFont(family); }
270 /// set font size
271 inline void SetFontSize(int size) { SetFont(-1,size); }
272 /// set font style
273 inline void SetFontStyle(int style) { SetFont(-1,-1,style); }
274 /// set font weight
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); }
280
281
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
287 printing)
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
292 */
293 void Layout(wxDC &dc);
294
295 /** Draw the list on a given DC.
296 @param pageNo if > 0, print only that page of a document (for
297 printing)
298 */
299 void Draw(wxDC &dc,
300 CoordType fromLine = -1,
301 CoordType toLine = -1,
302 iterator start = iterator(NULL));
303
304 /** Deletes at least to the end of line and redraws */
305 void EraseAndDraw(wxDC &dc, iterator start = iterator(NULL));
306
307 /** Finds the object occupying a certain screen position.
308 @return pointer to wxLayoutObjectBase or NULL if not found
309 */
310 wxLayoutObjectBase *Find(wxPoint coords) const;
311
312 #ifdef WXLAYOUT_DEBUG
313 void Debug(void);
314 void ShowCurrentObject();
315 #endif
316
317 /// dirty?
318 bool IsDirty() const { return m_bModified; }
319 bool CursorMoved(void) const { return m_CursorMoved; }
320
321 /// called after the contents is saved, for example
322 void ResetDirty() { m_bModified = FALSE; }
323
324 /// for access by wxLayoutWindow:
325 void GetSize(CoordType *max_x, CoordType *max_y,
326 CoordType *lineHeight);
327
328
329 /**@name Functionality for editing */
330 //@{
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);
339
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; }
344
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");
351
352 /// return a pointer to the default settings:
353 wxLayoutObjectCmd const *GetDefaults(void) const { return m_DefaultSetting ; }
354
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,
359 CoordType offs = 0);
360 wxLayoutPrintout *MakePrintout(wxString const &name);
361
362 /// Return maximum X,Y coordinates
363 wxPoint GetSize(void) const { return wxPoint(m_MaxX, m_MaxY); }
364 //@}
365 protected:
366 /// font parameters:
367 int m_FontFamily, m_FontStyle, m_FontWeight;
368 int m_FontPtSize;
369 bool m_FontUnderline;
370 /// colours:
371 wxColour const * m_ColourFG;
372 wxColour const * m_ColourBG;
373 /// the default setting:
374 wxLayoutObjectCmd *m_DefaultSetting;
375
376 /// needs recalculation?
377 bool m_dirty;
378 /// cursor moved
379 bool m_CursorMoved;
380
381 /// needs saving (i.e., was modified?)
382 bool m_bModified;
383
384 // the currently updated line:
385 /// where do we draw next:
386 wxPoint m_Position;
387 /// the height of the current line:
388 CoordType m_LineHeight;
389 /// maximum drawn x position so far
390 CoordType m_MaxX;
391 /// maximum drawn y position:
392 CoordType m_MaxY;
393
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;
401
402 /// to store content overwritten by cursor
403 wxMemoryDC m_CursorMemDC;
404
405 /// which is the last line
406 CoordType m_MaxLine;
407 /// can we edit it?
408 bool m_Editable;
409 /// find the object to the cursor position and returns the offset
410 /// in there
411 wxLayoutObjectList::iterator FindObjectCursor(wxPoint *cpos, CoordType *offset = NULL);
412 private:
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;
421 };
422
423 class wxLayoutPrintout: public wxPrintout
424 {
425 public:
426 wxLayoutPrintout(wxLayoutList &llist, wxString const & title = "My printout")
427 :wxPrintout(title)
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
433 *selPageTo);
434 void OnPreparePrinting(void);
435 private:
436 wxLayoutList *m_llist;
437 int m_maxPage;
438 };
439
440 #endif // WXLLIST_H