]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
Use wxScrollWinEvent instead of wxScrollEvent in
[wxWidgets.git] / user / wxLayout / wxllist.h
1 /*-*- c++ -*-********************************************************
2 * wxLayoutList.h - a formatted text rendering engine for wxWindows *
3 * *
4 * (C) 1999 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8
9
10 #ifndef WXLLIST_H
11 #define WXLLIST_H
12
13 #ifdef __GNUG__
14 # pragma interface "wxllist.h"
15 #endif
16
17 #include "kbList.h"
18
19 #include "wx/wx.h"
20 #include "wx/print.h"
21 #include "wx/printdlg.h"
22 #include "wx/generic/printps.h"
23 #include "wx/generic/prntdlgg.h"
24 #include "wx/dataobj.h"
25
26 // skip the following defines if embedded in M application
27 #ifndef M_BASEDIR
28 # define WXMENU_LAYOUT_LCLICK 1111
29 # define WXMENU_LAYOUT_RCLICK 1112
30 # define WXMENU_LAYOUT_DBLCLICK 1113
31 #endif
32
33 // do not enable debug mode within Mahogany
34 #if defined(__WXDEBUG__) && ! defined(M_BASEDIR)
35 # define WXLAYOUT_DEBUG
36 #endif
37
38 #ifdef WXLAYOUT_DEBUG
39 # define WXLO_TRACE(x) wxLogDebug(x)
40 #else
41 # define WXLO_TRACE(x)
42 #endif
43
44 #define WXLO_DEBUG_URECT 0
45
46 #ifndef WXLO_DEFAULTFONTSIZE
47 # define WXLO_DEFAULTFONTSIZE 12
48 #endif
49
50 #ifdef __WXMSW__
51 # define WXLO_BITMAP_FORMAT wxBITMAP_TYPE_BMP
52 #else
53 # define WXLO_BITMAP_FORMAT wxBITMAP_TYPE_PNG
54 #endif
55
56
57 /// Types of currently supported layout objects.
58 enum wxLayoutObjectType
59 {
60 /// illegal object type, should never appear
61 WXLO_TYPE_INVALID = 0,
62 /// text object, containing normal text
63 WXLO_TYPE_TEXT,
64 /// command object, containing font or colour changes
65 WXLO_TYPE_CMD,
66 /// icon object, any kind of image
67 WXLO_TYPE_ICON
68 };
69
70 /// Type used for coordinates in drawing. Must be signed.
71 typedef long CoordType;
72
73 // Forward declarations.
74 class wxLayoutList;
75 class wxLayoutObject;
76 class wxDC;
77 class wxColour;
78 class wxFont;
79
80
81 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
82
83 The wxLayout objects which make up the lines.
84
85 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
86
87 /** The base class defining the interface to each object which can be
88 part of the layout. Each object needs to draw itself and calculate
89 its size.
90 */
91 class wxLayoutObject
92 {
93 public:
94 /** This structure can be used to contain data associated with the
95 object.
96 It is refcounted, so the caller has to do a DecRef() on it
97 instead of a delete.
98 */
99 struct UserData
100 {
101 UserData() { m_refcount = 1; }
102 void IncRef(void) { m_refcount++; }
103 void DecRef(void) { m_refcount--; if(m_refcount == 0) delete this;}
104 private:
105 int m_refcount;
106 protected:
107 virtual ~UserData() { wxASSERT(m_refcount == 0); }
108 /// prevents gcc from generating stupid warnings
109 friend class dummy_UserData;
110 };
111
112 /// return the type of this object
113 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_INVALID; }
114 /** Calculates the size of an object.
115 @param dc the wxDC to draw on
116 @param llist the wxLayoutList
117 */
118 virtual void Layout(wxDC &dc, class wxLayoutList *llist) = 0;
119
120 /** Draws an object.
121 @param dc the wxDC to draw on
122 @param coords where to draw the baseline of the object.
123 @param wxllist pointer to wxLayoutList
124 @param begin if !=-1, from which position on to highlight it
125 @param end if begin !=-1, how many positions to highlight it
126 */
127 virtual void Draw(wxDC & /* dc */,
128 wxPoint const & /* coords */,
129 class wxLayoutList *wxllist,
130 CoordType begin = -1,
131 CoordType end = -1) { }
132
133 /** Calculates and returns the size of the object.
134 @param top where to store height above baseline
135 @param bottom where to store height below baseline
136 @return the size of the object's box in pixels
137 */
138 virtual wxPoint GetSize(CoordType * top, CoordType *bottom) const
139 { *top = 0; *bottom = 0; return wxPoint(0,0); }
140
141 /// Return just the width of the object on the screen.
142 virtual CoordType GetWidth(void) const { return 0; }
143 /// returns the number of cursor positions occupied by this object
144 virtual CoordType GetLength(void) const { return 1; }
145 /** Returns the cursor offset relating to the screen x position
146 relative to begin of object.
147 @param dc the wxDC to use for calculations
148 @param xpos relative x position from head of object
149 @return cursor coordinate offset
150 */
151 virtual CoordType GetOffsetScreen(wxDC &dc, CoordType xpos) const { return 0; }
152
153 /// constructor
154 wxLayoutObject() { m_UserData = NULL; }
155 /// delete the user data
156 virtual ~wxLayoutObject() { if(m_UserData) m_UserData->DecRef(); }
157
158 #ifdef WXLAYOUT_DEBUG
159 virtual void Debug(void);
160 #endif
161
162 /** Tells the object about some user data. This data is associated
163 with the object and will be deleted at destruction time.
164 */
165 void SetUserData(UserData *data)
166 {
167 if(m_UserData)
168 m_UserData->DecRef();
169 m_UserData = data;
170 if(m_UserData)
171 m_UserData->IncRef();
172 }
173
174 /** Return the user data. */
175 void * GetUserData(void) const { if(m_UserData) m_UserData->IncRef(); return m_UserData; }
176
177 /** Makes a copy of this object.
178 */
179 virtual wxLayoutObject *Copy(void) = 0;
180
181 /** Clipboard support function. Read and write objects to
182 strings. */
183 //@{
184 /// Writes the object to the string.
185 virtual void Write(wxString &ostr) = 0;
186 /** Reads an object.
187 @param str stream to read from, will bee changed
188 @return true on success
189 */
190 static wxLayoutObject *Read(wxString &istr);
191 //@}
192 protected:
193 /// optional data for application's use
194 UserData *m_UserData;
195 };
196
197 /// Define a list type of wxLayoutObject pointers.
198 KBLIST_DEFINE(wxLayoutObjectList, wxLayoutObject);
199
200 /// An illegal iterator to save typing.
201 #define NULLIT (wxLayoutObjectList::iterator(NULL))
202 /// The iterator type.
203 #define wxLOiterator wxLayoutObjectList::iterator
204
205 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
206
207 wxLayoutObjectText
208
209 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
210 /** This class implements a wxLayoutObject holding plain text.
211 */
212 class wxLayoutObjectText : public wxLayoutObject
213 {
214 public:
215 wxLayoutObjectText(const wxString &txt = "");
216
217 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_TEXT; }
218 virtual void Layout(wxDC &dc, class wxLayoutList *llist);
219 virtual void Draw(wxDC &dc, wxPoint const &coords,
220 class wxLayoutList *wxllist,
221 CoordType begin = -1,
222 CoordType end = -1);
223 /** Calculates and returns the size of the object.
224 @param top where to store height above baseline
225 @param bottom where to store height below baseline
226 @return the size of the object's box in pixels
227 */
228 virtual wxPoint GetSize(CoordType * top, CoordType *bottom) const;
229 /// Return just the width of the object on the screen.
230 virtual CoordType GetWidth(void) const { return m_Width; }
231 /** Returns the cursor offset relating to the screen x position
232 relative to begin of object.
233 @param dc the wxDC to use for calculations
234 @param xpos relative x position from head of object
235 @return cursor coordinate offset
236 */
237 virtual CoordType GetOffsetScreen(wxDC &dc, CoordType xpos) const;
238
239 virtual void Write(wxString &ostr);
240 static wxLayoutObjectText *Read(wxString &istr);
241
242 #ifdef WXLAYOUT_DEBUG
243 virtual void Debug(void);
244 #endif
245
246 virtual CoordType GetLength(void) const { return strlen(m_Text.c_str()); }
247
248 // for editing:
249 wxString & GetText(void) { return m_Text; }
250 void SetText(wxString const &text) { m_Text = text; }
251 /** Makes a copy of this object.
252 */
253 virtual wxLayoutObject *Copy(void);
254 private:
255 wxString m_Text;
256 /// size of the box containing text
257 long m_Width, m_Height;
258 /// Height above baseline.
259 long m_Top;
260 /// Height below baseline.
261 long m_Bottom;
262 };
263
264 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
265
266 wxLayoutObjectIcon
267
268 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
269 /** This class implements a wxLayoutObject holding a graphic.
270 */
271 class wxLayoutObjectIcon : public wxLayoutObject
272 {
273 public:
274 wxLayoutObjectIcon(wxBitmap *icon = NULL);
275 wxLayoutObjectIcon(wxBitmap const &icon);
276
277 ~wxLayoutObjectIcon() { if(m_Icon) delete m_Icon; }
278
279 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_ICON; }
280 virtual void Layout(wxDC &dc, class wxLayoutList *llist);
281 virtual void Draw(wxDC &dc, wxPoint const &coords,
282 class wxLayoutList *wxllist,
283 CoordType begin = -1,
284 CoordType end = -1);
285
286 /** Calculates and returns the size of the object.
287 @param top where to store height above baseline
288 @param bottom where to store height below baseline
289 @return the size of the object's box in pixels
290 */
291 virtual wxPoint GetSize(CoordType * top, CoordType *bottom) const;
292 /// Return just the width of the object on the screen.
293 virtual CoordType GetWidth(void) const { return m_Icon->GetWidth(); }
294 // return a pointer to the icon
295 wxBitmap *GetIcon(void) const { return m_Icon; }
296 /** Makes a copy of this object.
297 */
298 virtual wxLayoutObject *Copy(void);
299 virtual void Write(wxString &ostr);
300 static wxLayoutObjectIcon *Read(wxString &istr);
301 private:
302 wxBitmap *m_Icon;
303 };
304
305 /** This structure holds all formatting information. Members which are
306 undefined (for a CmdObject this means: no change), are set to -1.
307 */
308 struct wxLayoutStyleInfo
309 {
310 wxLayoutStyleInfo(int ifamily = -1,
311 int isize = -1,
312 int istyle = -1,
313 int iweight = -1,
314 int iul = -1,
315 wxColour *fg = NULL,
316 wxColour *bg = NULL);
317 wxColour & GetBGColour()
318 {
319 return m_bg;
320 }
321 wxLayoutStyleInfo & operator=(const wxLayoutStyleInfo &right);
322 /// Font change parameters.
323 int size, family, style, weight, underline;
324 /// Colours
325 wxColour m_bg, m_fg;
326 int m_fg_valid, m_bg_valid; // bool, but must be int!
327 };
328
329
330 class wxFontCacheEntry
331 {
332 public:
333 wxFontCacheEntry(int family, int size, int style, int weight,
334 bool underline)
335 {
336 m_Family = family; m_Size = size; m_Style = style;
337 m_Weight = weight; m_Underline = underline;
338 m_Font = new wxFont(m_Size, m_Family,
339 m_Style, m_Weight, m_Underline);
340 }
341 bool Matches(int family, int size, int style, int weight,
342 bool underline) const
343 {
344 return size == m_Size && family == m_Family
345 && style == m_Style && weight == m_Weight
346 && underline == m_Underline;
347 }
348 wxFont & GetFont(void) { return *m_Font; }
349 ~wxFontCacheEntry()
350 {
351 delete m_Font;
352 }
353 private:
354 wxFont *m_Font;
355 int m_Family, m_Size, m_Style, m_Weight;
356 bool m_Underline;
357 };
358
359 KBLIST_DEFINE(wxFCEList, wxFontCacheEntry);
360
361 class wxFontCache
362 {
363 public:
364 wxFont & GetFont(int family, int size, int style, int weight,
365 bool underline);
366 wxFont & GetFont(wxLayoutStyleInfo const &si)
367 {
368 return GetFont(si.family, si.size, si.style, si.weight, si.underline);
369 }
370 private:
371 wxFCEList m_FontList;
372 };
373
374 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
375
376 wxLayoutObjectCmd
377
378 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
379 /** This class implements a wxLayoutObject holding style change commands.
380 */
381 class wxLayoutObjectCmd : public wxLayoutObject
382 {
383 public:
384 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_CMD; }
385 virtual void Layout(wxDC &dc, class wxLayoutList *llist);
386 virtual void Draw(wxDC &dc, wxPoint const &coords,
387 class wxLayoutList *wxllist,
388 CoordType begin = -1,
389 CoordType end = -1);
390 wxLayoutObjectCmd(int family = -1,
391 int size = -1,
392 int style = -1,
393 int weight = -1,
394 int underline = -1,
395 wxColour *fg = NULL,
396 wxColour *bg = NULL);
397 ~wxLayoutObjectCmd();
398 /** Stores the current style in the styleinfo structure */
399 wxLayoutStyleInfo * GetStyle(void) const;
400 /** Makes a copy of this object.
401 */
402 virtual wxLayoutObject *Copy(void);
403 virtual void Write(wxString &ostr);
404 static wxLayoutObjectCmd *Read(wxString &istr);
405 private:
406 wxLayoutStyleInfo *m_StyleInfo;
407 };
408
409 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
410
411 The wxLayoutLine object
412
413 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
414
415 /// forward declaration
416 class wxLayoutList;
417
418 /** This class represents a single line of objects to be displayed.
419 It knows its height and total size and whether it needs to be
420 redrawn or not.
421 It has pointers to its first and next line so it can automatically
422 update them as needed.
423 */
424 class wxLayoutLine
425 {
426 public:
427 /** Constructor.
428 @param prev pointer to previous line or NULL
429 @param next pointer to following line or NULL
430 @param llist pointer to layout list
431 */
432 wxLayoutLine(wxLayoutLine *prev, wxLayoutList *llist);
433 /** This function inserts a new object at cursor position xpos.
434 @param xpos where to insert new object
435 @param obj the object to insert
436 @return true if that xpos existed and the object was inserted
437 */
438 bool Insert(CoordType xpos, wxLayoutObject *obj);
439
440 /** This function inserts text at cursor position xpos.
441 @param xpos where to insert
442 @param text the text to insert
443 @return true if that xpos existed and the object was inserted
444 */
445 bool Insert(CoordType xpos, wxString text);
446
447 /** This function appends an object to the line.
448 @param obj the object to insert
449 */
450 void Append(wxLayoutObject * obj)
451 {
452 wxASSERT(obj);
453 m_ObjectList.push_back(obj);
454 m_Length += obj->GetLength();
455 }
456
457 /** This function appens the next line to this, i.e. joins the two
458 lines into one.
459 */
460 void MergeNextLine(wxLayoutList *llist);
461
462 /** This function deletes npos cursor positions from position xpos.
463 @param xpos where to delete
464 @param npos how many positions
465 @return number of positions still to be deleted
466 */
467 CoordType Delete(CoordType xpos, CoordType npos);
468
469 /** This function breaks the line at a given cursor position.
470 @param xpos where to break it
471 @return pointer to the new line object replacing the old one
472 */
473 wxLayoutLine *Break(CoordType xpos, wxLayoutList *llist);
474
475 /** Deletes the next word from this position, including leading
476 whitespace.
477 This function does not delete over font changes, i.e. a word
478 with formatting instructions in the middle of it is treated as
479 two (three actually!) words. In fact, if the cursor is on a non-text object, that
480 one is treated as a word.
481 @param xpos from where to delete
482 @return true if a word was deleted
483 */
484 bool DeleteWord(CoordType npos);
485
486 /** Finds a suitable position left to the given column to break the
487 line.
488 @param column we want to break the line to the left of this
489 @return column for breaking line or -1 if no suitable location found
490 */
491 CoordType GetWrapPosition(CoordType column);
492
493 /** Finds the object which covers the cursor position xpos in this
494 line.
495 @param xpos the column number
496 @param offset where to store the difference between xpos and
497 the object's head
498 @return iterator to the object or NULLIT
499 */
500 wxLayoutObjectList::iterator FindObject(CoordType xpos, CoordType
501 *offset) const ;
502
503 /** Finds the object which covers the screen position xpos in this
504 line.
505 @param dc the wxDC to use for calculations
506 @param xpos the screen x coordinate
507 @param offset where to store the difference between xpos and
508 the object's head
509 @return iterator to the object or NULLIT
510 */
511 wxLayoutObjectList::iterator FindObjectScreen(wxDC &dc,
512 CoordType xpos,
513 CoordType *offset,
514 bool *found = NULL) const ;
515
516 /** Finds text in this line.
517 @param needle the text to find
518 @param xpos the position where to start the search
519 @return the cursoor coord where it was found or -1
520 */
521 CoordType FindText(const wxString &needle, CoordType xpos = 0) const;
522
523 /** Get the first object in the list. This is used by the wxlparser
524 functions to export the list.
525 @return iterator to the first object
526 */
527 wxLayoutObjectList::iterator GetFirstObject(void)
528 {
529 return m_ObjectList.begin();
530 }
531
532 /** Deletes this line, returns pointer to next line.
533 @param update If true, update all following lines.
534 */
535 wxLayoutLine *DeleteLine(bool update, wxLayoutList *llist);
536
537 /**@name Cursor Management */
538 //@{
539 /** Return the line number of this line.
540 @return the line number
541 */
542 inline CoordType GetLineNumber(void) const { return m_LineNumber; }
543 /** Return the length of the line.
544 @return line lenght in cursor positions
545 */
546 inline CoordType GetLength(void) const { return m_Length; }
547 //@}
548
549 /**@name Drawing and Layout */
550 //@{
551 /** Draws the line on a wxDC.
552 @param dc the wxDC to draw on
553 @param llist the wxLayoutList
554 @param offset an optional offset to shift printout
555 */
556 void Draw(wxDC &dc,
557 wxLayoutList *llist,
558 const wxPoint &offset = wxPoint(0,0)) const;
559
560 /** Recalculates the positions of objects and the height of the
561 line.
562 @param dc the wxDC to draw on
563 @param llist th e wxLayoutList
564 @param cursorPos if not NULL, set cursor screen position in there
565 @param cursorSize if not cursorPos != NULL, set cursor size in there
566 @param cx if cursorPos != NULL, the cursor x position
567 */
568 void Layout(wxDC &dc,
569 wxLayoutList *llist,
570 wxPoint *cursorPos = NULL,
571 wxPoint *cursorSize = NULL,
572 int cx = 0);
573 /** This function finds an object belonging to a given cursor
574 position. It assumes that Layout() has been called before.
575 @param dc the wxDC to use for calculations
576 @param xpos screen x position
577 @param found if non-NULL set to false if we return the last
578 object before the cursor, to true if we really have an object
579 for that position
580 @return pointer to the object
581 */
582 wxLayoutObject * FindObjectScreen(wxDC &dc, CoordType xpos, bool
583 *found = NULL);
584
585 //@}
586
587 /**@name List traversal */
588 //@{
589 /// Returns pointer to next line.
590 wxLayoutLine *GetNextLine(void) const { return m_Next; }
591 /// Returns pointer to previous line.
592 wxLayoutLine *GetPreviousLine(void) const { return m_Previous; }
593 /// Sets the link to the next line.
594 void SetNext(wxLayoutLine *next)
595 { m_Next = next; if(next) next->m_Previous = this; }
596 /// Sets the link to the previous line.
597 void SetPrevious(wxLayoutLine *previous)
598 { m_Previous = previous; if(previous) previous->m_Next = this; }
599 //@}
600
601 /// Returns the position of this line on the canvas.
602 wxPoint GetPosition(void) const { return m_Position; }
603 /// Returns the height of this line.
604 CoordType GetHeight(void) const { return m_Height; }
605 /// Returns the width of this line.
606 CoordType GetWidth(void) const { return m_Width; }
607 /** This will recalculate the position and size of this line.
608 If called recursively it will abort if the position of an
609 object is unchanged, assuming that none of the following
610 objects need to move.
611 @param recurse if greater 0 then it will be used as the
612 minimum(!) recursion level, continue with all lines till the end of
613 the list or until the coordinates no longer changed.
614 */
615 void RecalculatePositions(int recurse, wxLayoutList *llist);
616 /// Recalculates the position of this line on the canvas.
617 wxPoint RecalculatePosition(wxLayoutList *llist);
618
619 /** Copies the contents of this line to another wxLayoutList
620 @param llist the wxLayoutList destination
621 @param from x cursor coordinate where to start
622 @param to x cursor coordinate where to stop, -1 for end of line
623 */
624 void Copy(wxLayoutList *llist,
625 CoordType from = 0,
626 CoordType to = -1);
627
628 #ifdef WXLAYOUT_DEBUG
629 void Debug(void);
630 #endif
631 wxLayoutStyleInfo &GetStyleInfo() { return m_StyleInfo; }
632
633 /// Returns dirty state
634 bool IsDirty(void) const { return m_Dirty; }
635 /// Marks line as diry.
636 void MarkDirty(void) { m_Dirty = true; }
637 private:
638 /// Destructor is private. Use DeleteLine() to remove it.
639 ~wxLayoutLine();
640
641 /**@name Functions to let the lines synchronise with each other. */
642 //@{
643 /** Sets the height of this line. Will mark following lines as
644 dirty.
645 @param height new height
646 */
647 void SetHeight(CoordType height, wxLayoutList *llist)
648 { m_Height = height; RecalculatePositions(true, llist); }
649
650 /** Moves the linenumbers one on, because a line has been inserted
651 or deleted.
652 @param delta either +1 or -1
653 */
654 void MoveLines(int delta)
655 {
656 m_LineNumber += delta;
657 if(m_Next) m_Next->MoveLines(delta);
658 }
659 //@}
660 private:
661 /// The line number.
662 CoordType m_LineNumber;
663 /// The line length in cursor positions.
664 CoordType m_Length;
665 /// The total height of the line.
666 CoordType m_Height;
667 /// The total width of the line on screen.
668 CoordType m_Width;
669 /// The baseline for drawing objects
670 CoordType m_BaseLine;
671 /// The position on the canvas.
672 wxPoint m_Position;
673 /// The list of objects
674 wxLayoutObjectList m_ObjectList;
675 /// Have we been changed since the last layout?
676 bool m_Dirty;
677 /// Pointer to previous line if it exists.
678 wxLayoutLine *m_Previous;
679 /// Pointer to next line if it exists.
680 wxLayoutLine *m_Next;
681 /// A StyleInfo structure, holding the current settings.
682 wxLayoutStyleInfo m_StyleInfo;
683 /// Just to suppress gcc compiler warnings.
684 friend class dummy;
685 private:
686 wxLayoutLine(const wxLayoutLine &);
687 };
688
689
690 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
691
692 The wxLayoutList object
693
694 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
695 /** The wxLayoutList is a list of wxLayoutLine objects. It provides a
696 higher level of abstraction for the text and can generally be considered
697 as representing "the text".
698 */
699 class wxLayoutList
700 {
701 public:
702 /// Constructor.
703 wxLayoutList();
704 /// Destructor.
705 ~wxLayoutList();
706
707 /// Clear the list.
708 void Clear(int family = wxROMAN,
709 int size=WXLO_DEFAULTFONTSIZE,
710 int style=wxNORMAL,
711 int weight=wxNORMAL,
712 int underline=0,
713 wxColour *fg=NULL,
714 wxColour *bg=NULL);
715 /// Empty: clear the list but leave font settings.
716 void Empty(void);
717
718 /**@name Cursor Management */
719 //@{
720 /** Set new cursor position.
721 @param p new position
722 @return bool if it could be set
723 */
724 bool MoveCursorTo(wxPoint const &p);
725 /** Move cursor up or down.
726 @param n
727 @return bool if it could be moved
728 */
729 bool MoveCursorVertically(int n);
730 /** Move cursor left or right.
731 @param n
732 @return bool if it could be moved
733 */
734 bool MoveCursorHorizontally(int n);
735
736 /// Move cursor to end of line.
737 void MoveCursorToEndOfLine(void)
738 {
739 wxASSERT(m_CursorLine);
740 MoveCursorHorizontally(m_CursorLine->GetLength()-m_CursorPos.x);
741 }
742
743 /// Move cursor to begin of line.
744 void MoveCursorToBeginOfLine(void)
745 { MoveCursorHorizontally(-m_CursorPos.x); }
746
747 /// Returns current cursor position.
748 wxPoint GetCursorPos(wxDC &dc) const { return m_CursorPos; }
749 wxPoint GetCursorPos() const { return m_CursorPos; }
750
751 //@}
752
753 /**@name Editing functions.
754 All of these functions return true on success and false on
755 failure. */
756 //@{
757 /// Insert text at current cursor position.
758 bool Insert(wxString const &text);
759 /// Insert some other object at current cursor position.
760 bool Insert(wxLayoutObject *obj);
761 /// Inserts objects at current cursor positions
762 bool Insert(wxLayoutList *llist);
763
764 /// Inserts a linebreak at current cursor position.
765 bool LineBreak(void);
766 /** Wraps the current line. Searches to the left of the cursor to
767 break the line. Does nothing if the cursor position is before
768 the break position parameter.
769 @param column the break position for the line, maximum length
770 @return true if line got broken
771 */
772 bool WrapLine(CoordType column);
773 /** This function deletes npos cursor positions.
774 @param npos how many positions
775 @return true if everything got deleted
776 */
777 bool Delete(CoordType npos);
778
779 /** Delete the next n lines.
780 @param n how many lines to delete
781 @return how many it could not delete
782 */
783 int DeleteLines(int n);
784
785 /// Delete to end of line.
786 void DeleteToEndOfLine(void)
787 {
788 wxASSERT(m_CursorLine);
789 Delete(m_CursorLine->GetLength()-m_CursorPos.x);
790 }
791 /// Delete to begin of line.
792 void DeleteToBeginOfLine(void)
793 {
794 wxASSERT(m_CursorLine);
795 CoordType n = m_CursorPos.x;
796 #ifdef WXLAYOUT_DEBUG
797 wxASSERT(MoveCursorHorizontally(-n));
798 #else
799 MoveCursorHorizontally(-n);
800 #endif
801 Delete(n);
802 }
803
804 /** Delete the next word.
805 */
806 void DeleteWord(void)
807 {
808 wxASSERT(m_CursorLine);
809 m_CursorLine->DeleteWord(m_CursorPos.x);
810 }
811
812 //@}
813
814 /** Finds text in this list.
815 @param needle the text to find
816 @param cpos the position where to start the search
817 @return the cursoor coord where it was found or (-1,-1)
818 */
819 wxPoint FindText(const wxString &needle, const wxPoint &cpos = wxPoint(0,0)) const;
820
821 /**@name Formatting options */
822 //@{
823 /// sets font parameters
824 void SetFont(int family, int size, int style,
825 int weight, int underline,
826 wxColour *fg,
827 wxColour *bg);
828 /// sets font parameters, colours by name
829 void SetFont(int family=-1, int size = -1, int style=-1,
830 int weight=-1, int underline = -1,
831 char const *fg = NULL,
832 char const *bg = NULL);
833 /// changes to the next larger font size
834 inline void SetFontLarger(void)
835 { SetFont(-1,(12*m_CurrentSetting.size)/10); }
836 /// changes to the next smaller font size
837 inline void SetFontSmaller(void)
838 { SetFont(-1,(10*m_CurrentSetting.size)/12); }
839
840 /// set font family
841 inline void SetFontFamily(int family) { SetFont(family); }
842 /// set font size
843 inline void SetFontSize(int size) { SetFont(-1,size); }
844 /// set font style
845 inline void SetFontStyle(int style) { SetFont(-1,-1,style); }
846 /// set font weight
847 inline void SetFontWeight(int weight) { SetFont(-1,-1,-1,weight); }
848 /// toggle underline flag
849 inline void SetFontUnderline(bool ul) { SetFont(-1,-1,-1,-1,(int)ul); }
850 /// set font colours by name
851 inline void SetFontColour(char const *fg, char const *bg = NULL)
852 { SetFont(-1,-1,-1,-1,-1,fg,bg); }
853 /// set font colours by colour
854 inline void SetFontColour(wxColour *fg, wxColour *bg = NULL)
855 { SetFont(-1,-1,-1,-1,-1,fg,bg); }
856
857
858 /**
859 Returns a pointer to the default settings.
860 This is only valid temporarily and should not be stored
861 anywhere.
862 @return the default settings of the list
863 */
864 wxLayoutStyleInfo *GetDefaults(void) { return &m_DefaultSetting ; }
865 wxLayoutStyleInfo *GetStyleInfo(void) { return &m_CurrentSetting ; }
866 //@}
867
868 /**@name Drawing */
869 //@{
870 /** Draws the complete list on a wxDC.
871 @param dc the wxDC to draw on
872 @param offset an optional offset to shift printout
873 @param top optional y coordinate where to start drawing
874 @param bottom optional y coordinate where to stop drawing
875 */
876 void Draw(wxDC &dc,
877 const wxPoint &offset = wxPoint(0,0),
878 CoordType top = -1, CoordType bottom = -1);
879
880 /** Calculates new layout for the list, like Draw() but does not
881 actually draw it.
882 @param dc the wxDC to draw on
883 @param bottom optional y coordinate where to stop calculating
884 @param forceAll force re-layout of all lines
885 */
886 void Layout(wxDC &dc, CoordType bottom = -1, bool forceAll = false);
887
888 /** Calculates new sizes for everything in the list, like Layout()
889 but this is needed after the list got changed.
890 @param dc the wxDC to draw on
891 @param bottom optional y coordinate where to stop calculating
892 */
893 void Recalculate(wxDC &dc, CoordType bottom = -1);
894
895 /** Returns the size of the list in screen coordinates.
896 The return value only makes sense after the list has been
897 drawn.
898 @return a wxPoint holding the maximal x/y coordinates used for
899 drawing
900 */
901 wxPoint GetSize(void) const;
902
903 /** Returns the cursor position on the screen.
904 @return cursor position in pixels
905 */
906 wxPoint GetCursorScreenPos(wxDC &dc);
907
908 /** Draws the cursor.
909 @param active If true, draw a bold cursor to mark window as
910 active.
911 @param translate optional translation of cursor coords on screen
912 */
913 void DrawCursor(wxDC &dc,
914 bool active = true,
915 const wxPoint & translate = wxPoint(0,0));
916
917 /** This function finds an object belonging to a given screen
918 position. It assumes that Layout() has been called before.
919 @param pos screen position
920 @param cursorPos if non NULL, store cursor position in there
921 @param found if used, set this to true if we really found an
922 object, to false if we had to take the object near to it
923 @return pointer to the object
924 */
925 wxLayoutObject * FindObjectScreen(wxDC &dc,
926 wxPoint const pos,
927 wxPoint *cursorPos = NULL,
928 bool *found = NULL);
929
930 /** Called by the objects to update the update rectangle.
931 @param x horizontal coordinate to include in rectangle
932 @param y vertical coordinate to include in rectangle
933 */
934 void SetUpdateRect(CoordType x, CoordType y);
935 /** Called by the objects to update the update rectangle.
936 @param p a point to include in it
937 */
938 inline void SetUpdateRect(const wxPoint &p)
939 { SetUpdateRect(p.x,p.y); }
940 /// Invalidates the update rectangle.
941 void InvalidateUpdateRect(void) { m_UpdateRectValid = false; }
942 /// Returns the update rectangle.
943 const wxRect *GetUpdateRect(void) const { return &m_UpdateRect; }
944 //@}
945
946 /**@name For exporting one object after another. */
947 //@{
948 /** Returns a pointer to the first line in the list. */
949 wxLayoutLine *GetFirstLine(void)
950 {
951 wxASSERT(m_FirstLine);
952 return m_FirstLine;
953 }
954 //@}
955
956 /// Begin selecting text.
957 void StartSelection(wxPoint cpos = wxPoint(-1,-1));
958 // Continue selecting text
959 void ContinueSelection(wxPoint cpos = wxPoint(-1,-1));
960 /// End selecting text.
961 void EndSelection(wxPoint cpos = wxPoint(-1,-1));
962 /// Are we still selecting text?
963 bool IsSelecting(void);
964 bool IsSelected(const wxPoint &cursor);
965
966 /** Return the selection as a wxLayoutList.
967 @param invalidate if true, the selection will be invalidated after this and can no longer be used.
968 @return Another layout list object holding the selection, must be freed by caller
969 */
970 wxLayoutList *GetSelection(class wxLayoutDataObject *wxldo = NULL, bool invalidate = TRUE);
971 /// Delete selected bit
972 void DeleteSelection(void);
973
974 wxLayoutList *Copy(const wxPoint &from = wxPoint(0,0),
975 const wxPoint &to = wxPoint(-1,-1));
976
977 /// starts highlighting of text for selections
978 void StartHighlighting(wxDC &dc);
979 /// ends highlighting of text for selections
980 void EndHighlighting(wxDC &dc);
981
982 /** Tests whether this layout line is selected and needs
983 highlighting.
984 @param line to test for
985 @param from set to first cursorpos to be highlighted (for returncode == -1)
986 @param to set to last cursorpos to be highlighted (for returncode == -1)
987 @return 0 = not selected, 1 = fully selected, -1 = partially
988 selected
989
990 */
991 int IsSelected(const wxLayoutLine *line, CoordType *from, CoordType *to);
992
993 void ApplyStyle(wxLayoutStyleInfo *si, wxDC &dc);
994 #ifdef WXLAYOUT_DEBUG
995 void Debug(void);
996 #endif
997 private:
998 /// Clear the list.
999 void InternalClear(void);
1000 /** Calculates the cursor position on the screen.
1001 */
1002 void UpdateCursorScreenPos(wxDC &dc);
1003
1004 /// The list of lines.
1005 wxLayoutLine *m_FirstLine;
1006 /// The update rectangle which needs to be refreshed:
1007 wxRect m_UpdateRect;
1008 /// Is the update rectangle valid?
1009 bool m_UpdateRectValid;
1010 /**@name Cursor Management */
1011 //@{
1012 /// Where the text cursor (column,line) is.
1013 wxPoint m_CursorPos;
1014 /// The size of the cursor.
1015 wxPoint m_CursorSize;
1016 /// Where the cursor should be drawn.
1017 wxPoint m_CursorScreenPos;
1018 /// The line where the cursor is.
1019 wxLayoutLine *m_CursorLine;
1020 //@}
1021
1022 /// A structure for the selection.
1023 struct Selection
1024 {
1025 Selection() { m_valid = false; m_selecting = false; }
1026 bool m_valid;
1027 bool m_selecting;
1028 wxPoint m_CursorA, m_CursorB;
1029 } m_Selection;
1030 /** @name Font parameters. */
1031 //@{
1032 /// this object manages the fonts for us
1033 wxFontCache m_FontCache;
1034 /// the default setting:
1035 wxLayoutStyleInfo m_DefaultSetting;
1036 /// the current setting:
1037 wxLayoutStyleInfo m_CurrentSetting;
1038 //@}
1039 };
1040
1041
1042 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1043
1044 The wxLayoutDataObject for exporting data to the clipboard in our
1045 own format.
1046
1047 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1048 class wxLayoutDataObject : public wxPrivateDataObject
1049 {
1050 public:
1051 wxLayoutDataObject(void)
1052 {
1053 SetId("application/wxlayoutlist");
1054 m_format.SetAtom((GdkAtom) 222222);
1055 }
1056 };
1057
1058 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1059
1060 The wxLayoutPrintout object for printing within the wxWindows print
1061 framework.
1062
1063 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1064 /** This class implements a wxPrintout for printing a wxLayoutList within
1065 the wxWindows printing framework.
1066 */
1067 class wxLayoutPrintout: public wxPrintout
1068 {
1069 public:
1070 /** Constructor.
1071 @param llist pointer to the wxLayoutList to be printed
1072 @param title title for PS file or windows
1073 */
1074 wxLayoutPrintout(wxLayoutList *llist,
1075 wxString const & title =
1076 "wxLayout Printout");
1077 /// Destructor.
1078 ~wxLayoutPrintout();
1079
1080 /** Function which prints the n-th page.
1081 @param page the page number to print
1082 @return bool true if we are not at end of document yet
1083 */
1084 bool OnPrintPage(int page);
1085 /** Checks whether page exists in document.
1086 @param page number of page
1087 @return true if page exists
1088 */
1089 bool HasPage(int page);
1090
1091 /** Gets called from wxWindows to find out which pages are existing.
1092 I'm not totally sure about the parameters though.
1093 @param minPage the first page in the document
1094 @param maxPage the last page in the document
1095 @param selPageFrom the first page to be printed
1096 @param selPageTo the last page to be printed
1097 */
1098 void GetPageInfo(int *minPage, int *maxPage,
1099 int *selPageFrom, int *selPageTo);
1100 protected:
1101 /** This little function scales the DC so that the printout has
1102 roughly the same size as the output on screen.
1103 @param dc the wxDC to scale
1104 @return the scale that was applied
1105 */
1106 float ScaleDC(wxDC *dc);
1107
1108 /* no longer used
1109 virtual void DrawHeader(wxDC &dc, wxPoint topleft, wxPoint bottomright, int pageno);
1110 */
1111 private:
1112 /// The list to print.
1113 wxLayoutList *m_llist;
1114 /// Title for PS file or window.
1115 wxString m_title;
1116 /// The real paper size.
1117 int m_PageHeight, m_PageWidth;
1118 /// How much we actually print per page.
1119 int m_PrintoutHeight;
1120 /// How many pages we need to print.
1121 int m_NumOfPages;
1122 /// Top left corner where we start printing.
1123 wxPoint m_Offset;
1124 };
1125
1126
1127 #endif // WXLLIST_H