added default ctor and SetSelection to wxHtmlRenderingState
[wxWidgets.git] / include / wx / html / htmlcell.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlcell.h
3 // Purpose: wxHtmlCell class is used by wxHtmlWindow/wxHtmlWinParser
4 // as a basic visual element of HTML page
5 // Author: Vaclav Slavik
6 // RCS-ID: $Id$
7 // Copyright: (c) 1999 Vaclav Slavik
8 // Licence: wxWindows Licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 #ifndef _WX_HTMLCELL_H_
13 #define _WX_HTMLCELL_H_
14
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "htmlcell.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_HTML
22
23 #include "wx/html/htmltag.h"
24 #include "wx/html/htmldefs.h"
25 #include "wx/window.h"
26
27
28 class WXDLLEXPORT wxHtmlLinkInfo;
29 class WXDLLEXPORT wxHtmlCell;
30 class WXDLLEXPORT wxHtmlContainerCell;
31
32
33 // wxHtmlSelection is data holder with information about text selection.
34 // Selection is defined by two positions (beginning and end of the selection)
35 // and two leaf(!) cells at these positions.
36 class WXDLLEXPORT wxHtmlSelection
37 {
38 public:
39 wxHtmlSelection()
40 : m_fromPos(wxDefaultPosition), m_toPos(wxDefaultPosition),
41 m_fromCell(NULL), m_toCell(NULL) {}
42
43 void Set(const wxPoint& fromPos, wxHtmlCell *fromCell,
44 const wxPoint& toPos, wxHtmlCell *toCell)
45 {
46 m_fromCell = fromCell;
47 m_toCell = toCell;
48 m_fromPos = fromPos;
49 m_toPos = toPos;
50 }
51
52 const wxHtmlCell *GetFromCell() const { return m_fromCell; }
53 const wxHtmlCell *GetToCell() const { return m_toCell; }
54
55 // these values are *relative* to From/To cell's origin:
56 const wxPoint& GetFromPos() const { return m_fromPos; }
57 const wxPoint& GetToPos() const { return m_toPos; }
58
59 const bool IsEmpty() const
60 { return m_fromPos == wxDefaultPosition &&
61 m_toPos == wxDefaultPosition; }
62
63 private:
64 wxPoint m_fromPos, m_toPos;
65 wxHtmlCell *m_fromCell, *m_toCell;
66 };
67
68
69
70 enum wxHtmlSelectionState
71 {
72 wxHTML_SEL_OUT, // currently rendered cell is outside the selection
73 wxHTML_SEL_IN, // ... is inside selection
74 wxHTML_SEL_CHANGING // ... is the cell on which selection state changes
75 };
76
77 // Selection state is passed to wxHtmlCell::Draw so that it can render itself
78 // differently e.g. when inside text selection or outside it.
79 class WXDLLEXPORT wxHtmlRenderingState
80 {
81 public:
82 wxHtmlRenderingState(wxHtmlSelection *s = NULL)
83 : m_selection(s), m_selState(wxHTML_SEL_OUT) {}
84 void SetSelection(wxHtmlSelection *s) { m_selection = s; }
85 wxHtmlSelection *GetSelection() const { return m_selection; }
86
87 void SetSelectionState(wxHtmlSelectionState s) { m_selState = s; }
88 wxHtmlSelectionState GetSelectionState() const { return m_selState; }
89
90 void SetFgColour(const wxColour& c) { m_fgColour = c; }
91 const wxColour& GetFgColour() const { return m_fgColour; }
92 void SetBgColour(const wxColour& c) { m_bgColour = c; }
93 const wxColour& GetBgColour() const { return m_bgColour; }
94
95 private:
96 wxHtmlSelection *m_selection;
97 wxHtmlSelectionState m_selState;
98 wxColour m_fgColour, m_bgColour;
99 };
100
101 // Flags for wxHtmlCell::FindCellByPos
102 enum
103 {
104 wxHTML_FIND_TERMINAL = 0x0001,
105 wxHTML_FIND_NONTERMINAL = 0x0002
106 };
107
108
109 // ---------------------------------------------------------------------------
110 // wxHtmlCell
111 // Internal data structure. It represents fragments of parsed
112 // HTML page - a word, picture, table, horizontal line and so
113 // on. It is used by wxHtmlWindow to represent HTML page in
114 // memory.
115 // ---------------------------------------------------------------------------
116
117
118 class WXDLLEXPORT wxHtmlCell : public wxObject
119 {
120 public:
121 wxHtmlCell();
122 virtual ~wxHtmlCell();
123
124 void SetParent(wxHtmlContainerCell *p) {m_Parent = p;}
125 wxHtmlContainerCell *GetParent() const {return m_Parent;}
126
127 int GetPosX() const {return m_PosX;}
128 int GetPosY() const {return m_PosY;}
129 int GetWidth() const {return m_Width;}
130 int GetHeight() const {return m_Height;}
131 int GetDescent() const {return m_Descent;}
132
133 const wxString& GetId() const { return m_id; }
134 void SetId(const wxString& id) { m_id = id; }
135
136 // returns the link associated with this cell. The position is position
137 // within the cell so it varies from 0 to m_Width, from 0 to m_Height
138 virtual wxHtmlLinkInfo* GetLink(int WXUNUSED(x) = 0, int WXUNUSED(y) = 0) const
139 { return m_Link; }
140
141 // members access methods
142 wxHtmlCell *GetNext() const {return m_Next;}
143
144 // members writing methods
145 virtual void SetPos(int x, int y) {m_PosX = x, m_PosY = y;}
146 void SetLink(const wxHtmlLinkInfo& link);
147 void SetNext(wxHtmlCell *cell) {m_Next = cell;}
148
149 // 1. adjust cell's width according to the fact that maximal possible width
150 // is w. (this has sense when working with horizontal lines, tables
151 // etc.)
152 // 2. prepare layout (=fill-in m_PosX, m_PosY (and sometime m_Height)
153 // members) = place items to fit window, according to the width w
154 virtual void Layout(int w);
155
156 // renders the cell
157 virtual void Draw(wxDC& WXUNUSED(dc),
158 int WXUNUSED(x), int WXUNUSED(y),
159 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
160 wxHtmlRenderingState& WXUNUSED(state)) {}
161
162 // proceed drawing actions in case the cell is not visible (scrolled out of
163 // screen). This is needed to change fonts, colors and so on.
164 virtual void DrawInvisible(wxDC& WXUNUSED(dc),
165 int WXUNUSED(x), int WXUNUSED(y),
166 wxHtmlRenderingState& WXUNUSED(state)) {}
167
168 // This method returns pointer to the FIRST cell for that
169 // the condition
170 // is true. It first checks if the condition is true for this
171 // cell and then calls m_Next->Find(). (Note: it checks
172 // all subcells if the cell is container)
173 // Condition is unique condition identifier (see htmldefs.h)
174 // (user-defined condition IDs should start from 10000)
175 // and param is optional parameter
176 // Example : m_Cell->Find(wxHTML_COND_ISANCHOR, "news");
177 // returns pointer to anchor news
178 virtual const wxHtmlCell* Find(int condition, const void* param) const;
179
180 // This function is called when mouse button is clicked over the cell.
181 //
182 // Parent is pointer to wxHtmlWindow that generated the event
183 // HINT: if this handling is not enough for you you should use
184 // wxHtmlWidgetCell
185 virtual void OnMouseClick(wxWindow *parent, int x, int y, const wxMouseEvent& event);
186
187 // This method used to adjust pagebreak position. The parameter is variable
188 // that contains y-coordinate of page break (= horizontal line that should
189 // not be crossed by words, images etc.). If this cell cannot be divided
190 // into two pieces (each one on another page) then it moves the pagebreak
191 // few pixels up.
192 //
193 // Returned value : true if pagebreak was modified, false otherwise
194 // Usage : while (container->AdjustPagebreak(&p)) {}
195 virtual bool AdjustPagebreak(int *pagebreak,
196 int *known_pagebreaks = NULL,
197 int number_of_pages = 0) const;
198
199 // Sets cell's behaviour on pagebreaks (see AdjustPagebreak). Default
200 // is true - the cell can be split on two pages
201 void SetCanLiveOnPagebreak(bool can) { m_CanLiveOnPagebreak = can; }
202
203 // Returns y-coordinates that contraint the cell, i.e. left is highest
204 // and right lowest coordinate such that the cell lays between then.
205 // Note: this method does not return meaningful values if you haven't
206 // called Layout() before!
207 virtual void GetHorizontalConstraints(int *left, int *right) const;
208
209 // Returns true for simple == terminal cells, i.e. not composite ones.
210 // This if for internal usage only and may disappear in future versions!
211 virtual bool IsTerminalCell() const { return TRUE; }
212
213 // Find a cell inside this cell positioned at the given coordinates
214 // (relative to this's positions). Returns NULL if no such cell exists.
215 // The flag can be used to specify whether to look for terminal or
216 // nonterminal cells or both. In either case, returned cell is deepest
217 // cell in cells tree that contains [x,y].
218 virtual wxHtmlCell *FindCellByPos(wxCoord x, wxCoord y,
219 unsigned flags = wxHTML_FIND_TERMINAL) const;
220
221 protected:
222 wxHtmlCell *m_Next;
223 // pointer to the next cell
224 wxHtmlContainerCell *m_Parent;
225 // pointer to parent cell
226 long m_Width, m_Height, m_Descent;
227 // dimensions of fragment
228 // m_Descent is used to position text&images..
229 long m_PosX, m_PosY;
230 // position where the fragment is drawn
231 wxHtmlLinkInfo *m_Link;
232 // destination address if this fragment is hypertext link, NULL otherwise
233 bool m_CanLiveOnPagebreak;
234 // true if this cell can be placed on pagebreak, false otherwise
235 wxString m_id;
236 // unique identifier of the cell, generated from "id" property of tags
237
238 DECLARE_NO_COPY_CLASS(wxHtmlCell)
239 };
240
241
242
243
244 //--------------------------------------------------------------------------------
245 // Inherited cells:
246 //--------------------------------------------------------------------------------
247
248
249 //--------------------------------------------------------------------------------
250 // wxHtmlWordCell
251 // Single word in input stream.
252 //--------------------------------------------------------------------------------
253
254 class WXDLLEXPORT wxHtmlWordCell : public wxHtmlCell
255 {
256 public:
257 wxHtmlWordCell(const wxString& word, wxDC& dc);
258 void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
259 wxHtmlRenderingState& state);
260
261 protected:
262 wxString m_Word;
263 };
264
265
266
267
268
269 // Container contains other cells, thus forming tree structure of rendering
270 // elements. Basic code of layout algorithm is contained in this class.
271 class WXDLLEXPORT wxHtmlContainerCell : public wxHtmlCell
272 {
273 public:
274 wxHtmlContainerCell(wxHtmlContainerCell *parent);
275 ~wxHtmlContainerCell();
276
277 virtual void Layout(int w);
278 virtual void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
279 wxHtmlRenderingState& state);
280 virtual void DrawInvisible(wxDC& dc, int x, int y,
281 wxHtmlRenderingState& state);
282 virtual bool AdjustPagebreak(int *pagebreak, int *known_pagebreaks = NULL, int number_of_pages = 0) const;
283
284 // insert cell at the end of m_Cells list
285 void InsertCell(wxHtmlCell *cell);
286
287 // sets horizontal/vertical alignment
288 void SetAlignHor(int al) {m_AlignHor = al; m_LastLayout = -1;}
289 int GetAlignHor() const {return m_AlignHor;}
290 void SetAlignVer(int al) {m_AlignVer = al; m_LastLayout = -1;}
291 int GetAlignVer() const {return m_AlignVer;}
292
293 // sets left-border indentation. units is one of wxHTML_UNITS_* constants
294 // what is combination of wxHTML_INDENT_*
295 void SetIndent(int i, int what, int units = wxHTML_UNITS_PIXELS);
296 // returns the indentation. ind is one of wxHTML_INDENT_* constants
297 int GetIndent(int ind) const;
298 // returns type of value returned by GetIndent(ind)
299 int GetIndentUnits(int ind) const;
300
301 // sets alignment info based on given tag's params
302 void SetAlign(const wxHtmlTag& tag);
303 // sets floating width adjustment
304 // (examples : 32 percent of parent container,
305 // -15 pixels percent (this means 100 % - 15 pixels)
306 void SetWidthFloat(int w, int units) {m_WidthFloat = w; m_WidthFloatUnits = units; m_LastLayout = -1;}
307 void SetWidthFloat(const wxHtmlTag& tag, double pixel_scale = 1.0);
308 // sets minimal height of this container.
309 void SetMinHeight(int h, int align = wxHTML_ALIGN_TOP) {m_MinHeight = h; m_MinHeightAlign = align; m_LastLayout = -1;}
310
311 void SetBackgroundColour(const wxColour& clr) {m_UseBkColour = TRUE; m_BkColour = clr;}
312 // returns background colour (of wxNullColour if none set), so that widgets can
313 // adapt to it:
314 wxColour GetBackgroundColour();
315 void SetBorder(const wxColour& clr1, const wxColour& clr2) {m_UseBorder = TRUE; m_BorderColour1 = clr1, m_BorderColour2 = clr2;}
316 virtual wxHtmlLinkInfo* GetLink(int x = 0, int y = 0) const;
317 virtual const wxHtmlCell* Find(int condition, const void* param) const;
318 virtual void OnMouseClick(wxWindow *parent, int x, int y, const wxMouseEvent& event);
319 virtual void GetHorizontalConstraints(int *left, int *right) const;
320
321 // returns pointer to the first cell in container or NULL
322 wxHtmlCell* GetFirstCell() const {return m_Cells;}
323
324 // see comment in wxHtmlCell about this method
325 virtual bool IsTerminalCell() const { return FALSE; }
326
327 virtual wxHtmlCell *FindCellByPos(wxCoord x, wxCoord y,
328 unsigned flags = wxHTML_FIND_TERMINAL) const;
329
330 protected:
331 void UpdateRenderingStatePre(wxHtmlRenderingState& state,
332 wxHtmlCell *cell) const;
333 void UpdateRenderingStatePost(wxHtmlRenderingState& state,
334 wxHtmlCell *cell) const;
335
336 protected:
337 int m_IndentLeft, m_IndentRight, m_IndentTop, m_IndentBottom;
338 // indentation of subcells. There is always m_Indent pixels
339 // big space between given border of the container and the subcells
340 // it m_Indent < 0 it is in PERCENTS, otherwise it is in pixels
341 int m_MinHeight, m_MinHeightAlign;
342 // minimal height.
343 wxHtmlCell *m_Cells, *m_LastCell;
344 // internal cells, m_Cells points to the first of them, m_LastCell to the last one.
345 // (LastCell is needed only to speed-up InsertCell)
346 int m_AlignHor, m_AlignVer;
347 // alignment horizontal and vertical (left, center, right)
348 int m_WidthFloat, m_WidthFloatUnits;
349 // width float is used in adjustWidth
350 bool m_UseBkColour;
351 wxColour m_BkColour;
352 // background color of this container
353 bool m_UseBorder;
354 wxColour m_BorderColour1, m_BorderColour2;
355 // borders color of this container
356 int m_LastLayout;
357 // if != -1 then call to Layout may be no-op
358 // if previous call to Layout has same argument
359
360 DECLARE_NO_COPY_CLASS(wxHtmlContainerCell)
361 };
362
363
364
365
366
367 //--------------------------------------------------------------------------------
368 // wxHtmlColourCell
369 // Color changer.
370 //--------------------------------------------------------------------------------
371
372 class WXDLLEXPORT wxHtmlColourCell : public wxHtmlCell
373 {
374 public:
375 wxHtmlColourCell(const wxColour& clr, int flags = wxHTML_CLR_FOREGROUND) : wxHtmlCell() {m_Colour = clr; m_Flags = flags;}
376 virtual void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
377 wxHtmlRenderingState& state);
378 virtual void DrawInvisible(wxDC& dc, int x, int y,
379 wxHtmlRenderingState& state);
380
381 protected:
382 wxColour m_Colour;
383 unsigned m_Flags;
384 };
385
386
387
388
389 //--------------------------------------------------------------------------------
390 // wxHtmlFontCell
391 // Sets actual font used for text rendering
392 //--------------------------------------------------------------------------------
393
394 class WXDLLEXPORT wxHtmlFontCell : public wxHtmlCell
395 {
396 public:
397 wxHtmlFontCell(wxFont *font) : wxHtmlCell() { m_Font = (*font); }
398 virtual void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
399 wxHtmlRenderingState& state);
400 virtual void DrawInvisible(wxDC& dc, int x, int y,
401 wxHtmlRenderingState& state);
402
403 protected:
404 wxFont m_Font;
405 };
406
407
408
409
410
411
412 //--------------------------------------------------------------------------------
413 // wxHtmlwidgetCell
414 // This cell is connected with wxWindow object
415 // You can use it to insert windows into HTML page
416 // (buttons, input boxes etc.)
417 //--------------------------------------------------------------------------------
418
419 class WXDLLEXPORT wxHtmlWidgetCell : public wxHtmlCell
420 {
421 public:
422 // !!! wnd must have correct parent!
423 // if w != 0 then the m_Wnd has 'floating' width - it adjust
424 // it's width according to parent container's width
425 // (w is percent of parent's width)
426 wxHtmlWidgetCell(wxWindow *wnd, int w = 0);
427 ~wxHtmlWidgetCell() { m_Wnd->Destroy(); }
428 virtual void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
429 wxHtmlRenderingState& state);
430 virtual void DrawInvisible(wxDC& dc, int x, int y,
431 wxHtmlRenderingState& state);
432 virtual void Layout(int w);
433
434 protected:
435 wxWindow* m_Wnd;
436 int m_WidthFloat;
437 // width float is used in adjustWidth (it is in percents)
438
439 DECLARE_NO_COPY_CLASS(wxHtmlWidgetCell)
440 };
441
442
443
444 //--------------------------------------------------------------------------------
445 // wxHtmlLinkInfo
446 // Internal data structure. It represents hypertext link
447 //--------------------------------------------------------------------------------
448
449 class WXDLLEXPORT wxHtmlLinkInfo : public wxObject
450 {
451 public:
452 wxHtmlLinkInfo() : wxObject()
453 { m_Href = m_Target = wxEmptyString; m_Event = NULL, m_Cell = NULL; }
454 wxHtmlLinkInfo(const wxString& href, const wxString& target = wxEmptyString) : wxObject()
455 { m_Href = href; m_Target = target; m_Event = NULL, m_Cell = NULL; }
456 wxHtmlLinkInfo(const wxHtmlLinkInfo& l) : wxObject()
457 { m_Href = l.m_Href, m_Target = l.m_Target, m_Event = l.m_Event;
458 m_Cell = l.m_Cell; }
459 wxHtmlLinkInfo& operator=(const wxHtmlLinkInfo& l)
460 { m_Href = l.m_Href, m_Target = l.m_Target, m_Event = l.m_Event;
461 m_Cell = l.m_Cell; return *this; }
462
463 void SetEvent(const wxMouseEvent *e) { m_Event = e; }
464 void SetHtmlCell(const wxHtmlCell *e) { m_Cell = e; }
465
466 wxString GetHref() const { return m_Href; }
467 wxString GetTarget() const { return m_Target; }
468 const wxMouseEvent* GetEvent() const { return m_Event; }
469 const wxHtmlCell* GetHtmlCell() const { return m_Cell; }
470
471 private:
472 wxString m_Href, m_Target;
473 const wxMouseEvent *m_Event;
474 const wxHtmlCell *m_Cell;
475 };
476
477
478
479
480 #endif // wxUSE_HTML
481
482 #endif // _WX_HTMLCELL_H_
483