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