]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
Added wxString version of wxStripExtension; changed OnClose to return TRUE
[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
19 // skip the following defines if embedded in M application
20 #ifndef MCONFIG_H
21 // for testing only:
22 # define WXLAYOUT_DEBUG
23 //# define USE_STD_STRING
24 #endif
25
26 #ifdef USE_STD_STRING
27 # include <string>
28 typedef std::string String;
29 # define Str(str)(str.c_str())
30 #else
31 typedef wxString String;
32 # define Str(str) str
33 #endif
34
35 enum wxLayoutObjectType { WXLO_TYPE_INVALID, WXLO_TYPE_TEXT, WXLO_TYPE_CMD, WXLO_TYPE_ICON, WXLO_TYPE_LINEBREAK };
36
37 typedef long CoordType;
38
39 class wxLayoutList;
40 class wxLayoutObjectBase;
41
42 KBLIST_DEFINE(wxLayoutObjectList, wxLayoutObjectBase);
43 KBLIST_DEFINE(wxLayoutOLinesList, wxLayoutObjectList::iterator);
44
45
46 class wxLayoutObjectBase
47 {
48 public:
49 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_INVALID; } ;
50 /** Draws an object.
51 @param dc the wxDC to draw on
52 @param position where to draw the top left corner
53 @param baseLine the baseline for alignment, from top of box
54 @draw if set to false, do not draw but just calculate sizes
55 */
56 virtual void Draw(wxDC &dc, wxPoint position, CoordType baseLine,
57 bool draw = true) {};
58
59 virtual wxPoint GetSize(CoordType *baseLine) const { return
60 wxPoint(0,0); };
61 /// returns the number of cursor positions occupied by this object
62 virtual CoordType CountPositions(void) const { return 1; }
63
64 wxLayoutObjectBase() { m_UserData = NULL; }
65 virtual ~wxLayoutObjectBase() { if(m_UserData) delete m_UserData; }
66 #ifdef WXLAYOUT_DEBUG
67 virtual void Debug(void);
68 #endif
69
70 void SetUserData(void *data) { m_UserData = data; }
71 void * GetUserData(void) const { return m_UserData; }
72 private:
73 /// optional data for application's use
74 void * m_UserData;
75 };
76
77 /// object for text block
78 class wxLayoutObjectText : public wxLayoutObjectBase
79 {
80 public:
81 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_TEXT; }
82 virtual void Draw(wxDC &dc, wxPoint position, CoordType baseLine,
83 bool draw = true);
84 /** This returns the height and in baseLine the position of the
85 text's baseline within it's box. This is needed to properly
86 align text objects.
87 */
88 virtual wxPoint GetSize(CoordType *baseLine) const;
89 #ifdef WXLAYOUT_DEBUG
90 virtual void Debug(void);
91 #endif
92
93 wxLayoutObjectText(const String &txt);
94 virtual CoordType CountPositions(void) const { return strlen(m_Text.c_str()); }
95
96 // for editing:
97 String & GetText(void) { return m_Text; }
98 void SetText(String const &text) { m_Text = text; }
99 private:
100 String m_Text;
101 /// size of the box containing text
102 long m_Width, m_Height;
103 /// the position of the baseline counted from the top of the box
104 long m_BaseLine;
105 };
106
107 /// icon/pictures:
108 class wxLayoutObjectIcon : public wxLayoutObjectBase
109 {
110 public:
111 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_ICON; }
112 virtual void Draw(wxDC &dc, wxPoint position, CoordType baseLine,
113 bool draw = true);
114 virtual wxPoint GetSize(CoordType *baseLine) const;
115 wxLayoutObjectIcon(wxIcon *icon);
116 private:
117 wxIcon * m_Icon;
118 };
119
120 /// for export to html:
121 struct wxLayoutStyleInfo
122 {
123 int size, family, style, weight;
124 bool underline;
125 unsigned fg_red, fg_green, fg_blue;
126 unsigned bg_red, bg_green, bg_blue;
127 };
128
129 /// pseudo-object executing a formatting command in Draw()
130 class wxLayoutObjectCmd : public wxLayoutObjectBase
131 {
132 public:
133 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_CMD; }
134 virtual void Draw(wxDC &dc, wxPoint position, CoordType baseLine,
135 bool draw = true);
136 wxLayoutObjectCmd(int size, int family, int style, int weight,
137 bool underline,
138 wxColour const *fg, wxColour const *bg);
139 ~wxLayoutObjectCmd();
140 // caller must free pointer:
141 wxLayoutStyleInfo *GetStyle(void) const ;
142 private:
143 /// the font to use
144 wxFont *m_font;
145 /// foreground colour
146 wxColour const *m_ColourFG;
147 /// background colour
148 wxColour const *m_ColourBG;
149 };
150
151 /// this object doesn't do anything at all
152 class wxLayoutObjectLineBreak : public wxLayoutObjectBase
153 {
154 public:
155 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_LINEBREAK; }
156 };
157
158
159 /**
160 This class provides a high level abstraction to the wxFText
161 classes.
162 It handles most of the character events with its own callback
163 functions, providing an editing ability. All events which cannot be
164 handled get passed to the parent window's handlers.
165 */
166 class wxLayoutList : public wxLayoutObjectList
167 {
168 public:
169 wxLayoutList();
170
171 /// Destructor.
172 ~wxLayoutList();
173
174 /// adds an object:
175 void AddObject(wxLayoutObjectBase *obj);
176 void AddText(String const &txt);
177
178 void LineBreak(void);
179 void SetFont(int family, int size, int style,
180 int weight, int underline,
181 wxColour const *fg,
182 wxColour const *bg);
183 void SetFont(int family=-1, int size = -1, int style=-1,
184 int weight=-1, int underline = -1,
185 char const *fg = NULL,
186 char const *bg = NULL);
187 inline void SetFontFamily(int family) { SetFont(family); }
188 inline void SetFontSize(int size) { SetFont(-1,size); }
189 inline void SetFontStyle(int style) { SetFont(-1,-1,style); }
190 inline void SetFontWeight(int weight) { SetFont(-1,-1,-1,weight); }
191 inline void SetFontUnderline(bool ul) { SetFont(-1,-1,-1,-1,(int)ul); }
192 inline void SetFontColour(char const *fg, char const *bg = NULL) { SetFont(-1,-1,-1,-1,-1,fg,bg); }
193
194
195 /** Draw the list on a given DC.
196 @param findObject if true, return the object occupying the
197 position specified by coords
198 @param coords position where to find the object
199 @return if findObject == true, the object or NULL
200 */
201 wxLayoutObjectBase *Draw(wxDC &dc, bool findObject = false,
202 wxPoint const &coords = wxPoint(0,0));
203
204 #ifdef WXLAYOUT_DEBUG
205 void Debug(void);
206 #endif
207
208
209 /// for access by wxLayoutWindow:
210 void GetSize(CoordType *max_x, CoordType *max_y,
211 CoordType *lineHeight);
212
213 /**@name Functionality for editing */
214 //@{
215 /// set list editable or read only
216 void SetEditable(bool editable = true) { m_Editable = true; }
217 /// move cursor
218 void MoveCursor(int dx = 0, int dy = 0);
219 void SetCursor(wxPoint const &p) { m_CursorPosition = p; }
220 /// delete one or more cursor positions
221 void Delete(CoordType count = 1);
222 void Insert(String const &text);
223 void Insert(wxLayoutObjectBase *obj);
224 void Clear(int family = wxROMAN, int size=12, int style=wxNORMAL, int weight=wxNORMAL,
225 int underline=0, char const *fg="black", char const *bg="white");
226
227 //@}
228 protected:
229 /// font parameters:
230 int m_FontFamily, m_FontStyle, m_FontWeight;
231 int m_FontPtSize;
232 bool m_FontUnderline;
233 /// colours:
234 wxColour const * m_ColourFG;
235 wxColour const * m_ColourBG;
236 /// the default setting:
237 wxLayoutObjectCmd *m_DefaultSetting;
238
239 /// needs recalculation?
240 bool m_dirty;
241
242 // the currently updated line:
243 /// where do we draw next:
244 wxPoint m_Position;
245 /// the height of the current line:
246 CoordType m_LineHeight;
247 /// maximum drawn x position so far
248 CoordType m_MaxX;
249 /// maximum drawn y position:
250 CoordType m_MaxY;
251
252 //---- this is needed for editing:
253 /// where is the text cursor:
254 wxPoint m_CursorPosition;
255 /// which is the last line
256 CoordType m_MaxLine;
257 /// can we edit it?
258 bool m_Editable;
259 /// find the object to the cursor position and returns the offset
260 /// in there
261 wxLayoutObjectList::iterator FindObjectCursor(wxPoint const &cpos, CoordType *offset = NULL);
262 wxLayoutObjectList::iterator FindCurrentObject(CoordType *offset = NULL);
263 // get the length of the line with the object pointed to by i
264 CoordType GetLineLength(wxLayoutObjectList::iterator i);
265
266 };
267
268 #endif // WXLLIST_H