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