1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlCell class is used by wxHtmlWindow/wxHtmlWinParser
4 // as a basic visual element of HTML page
5 // Author: Vaclav Slavik
7 // Copyright: (c) 1999 Vaclav Slavik
8 // Licence: wxWindows Licence
9 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_HTMLCELL_H_
13 #define _WX_HTMLCELL_H_
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "htmlcell.h"
23 #include "wx/html/htmltag.h"
24 #include "wx/html/htmldefs.h"
25 #include "wx/window.h"
28 class WXDLLEXPORT wxHtmlLinkInfo
;
29 class WXDLLEXPORT wxHtmlCell
;
30 class WXDLLEXPORT wxHtmlContainerCell
;
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
40 : m_fromPos(wxDefaultPosition
), m_toPos(wxDefaultPosition
),
41 m_fromCell(NULL
), m_toCell(NULL
) {}
43 void Set(const wxPoint
& fromPos
, wxHtmlCell
*fromCell
,
44 const wxPoint
& toPos
, wxHtmlCell
*toCell
);
45 void Set(wxHtmlCell
*fromCell
, wxHtmlCell
*toCell
);
47 const wxHtmlCell
*GetFromCell() const { return m_fromCell
; }
48 const wxHtmlCell
*GetToCell() const { return m_toCell
; }
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
; }
54 const bool IsEmpty() const
55 { return m_fromPos
== wxDefaultPosition
&&
56 m_toPos
== wxDefaultPosition
; }
59 wxPoint m_fromPos
, m_toPos
;
60 wxHtmlCell
*m_fromCell
, *m_toCell
;
65 enum wxHtmlSelectionState
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
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
77 wxHtmlRenderingState() : m_selState(wxHTML_SEL_OUT
) {}
79 void SetSelectionState(wxHtmlSelectionState s
) { m_selState
= s
; }
80 wxHtmlSelectionState
GetSelectionState() const { return m_selState
; }
82 void SetFgColour(const wxColour
& c
) { m_fgColour
= c
; }
83 const wxColour
& GetFgColour() const { return m_fgColour
; }
84 void SetBgColour(const wxColour
& c
) { m_bgColour
= c
; }
85 const wxColour
& GetBgColour() const { return m_bgColour
; }
88 wxHtmlSelectionState m_selState
;
89 wxColour m_fgColour
, m_bgColour
;
93 // HTML rendering customization. This class is used when rendering wxHtmlCells
95 class WXDLLEXPORT wxHtmlRenderingStyle
98 virtual wxColour
GetSelectedTextColour(const wxColour
& clr
) = 0;
99 virtual wxColour
GetSelectedTextBgColour(const wxColour
& clr
) = 0;
102 class WXDLLEXPORT wxDefaultHtmlRenderingStyle
: public wxHtmlRenderingStyle
105 virtual wxColour
GetSelectedTextColour(const wxColour
& clr
);
106 virtual wxColour
GetSelectedTextBgColour(const wxColour
& clr
);
110 // Information given to cells when drawing them. Contains rendering state,
111 // selection information and rendering style object that can be used to
112 // customize the output.
113 class WXDLLEXPORT wxHtmlRenderingInfo
116 wxHtmlRenderingInfo() : m_selection(NULL
), m_style(NULL
) {}
118 void SetSelection(wxHtmlSelection
*s
) { m_selection
= s
; }
119 wxHtmlSelection
*GetSelection() const { return m_selection
; }
121 void SetStyle(wxHtmlRenderingStyle
*style
) { m_style
= style
; }
122 wxHtmlRenderingStyle
& GetStyle() { return *m_style
; }
124 wxHtmlRenderingState
& GetState() { return m_state
; }
127 wxHtmlSelection
*m_selection
;
128 wxHtmlRenderingStyle
*m_style
;
129 wxHtmlRenderingState m_state
;
133 // Flags for wxHtmlCell::FindCellByPos
136 wxHTML_FIND_EXACT
= 1,
137 wxHTML_FIND_NEAREST_BEFORE
= 2,
138 wxHTML_FIND_NEAREST_AFTER
= 4,
144 // ---------------------------------------------------------------------------
146 // Internal data structure. It represents fragments of parsed
147 // HTML page - a word, picture, table, horizontal line and so
148 // on. It is used by wxHtmlWindow to represent HTML page in
150 // ---------------------------------------------------------------------------
153 class WXDLLEXPORT wxHtmlCell
: public wxObject
157 virtual ~wxHtmlCell();
159 void SetParent(wxHtmlContainerCell
*p
) {m_Parent
= p
;}
160 wxHtmlContainerCell
*GetParent() const {return m_Parent
;}
162 int GetPosX() const {return m_PosX
;}
163 int GetPosY() const {return m_PosY
;}
164 int GetWidth() const {return m_Width
;}
165 int GetHeight() const {return m_Height
;}
166 int GetDescent() const {return m_Descent
;}
168 const wxString
& GetId() const { return m_id
; }
169 void SetId(const wxString
& id
) { m_id
= id
; }
171 // returns the link associated with this cell. The position is position
172 // within the cell so it varies from 0 to m_Width, from 0 to m_Height
173 virtual wxHtmlLinkInfo
* GetLink(int WXUNUSED(x
) = 0,
174 int WXUNUSED(y
) = 0) const
177 // return next cell among parent's cells
178 wxHtmlCell
*GetNext() const {return m_Next
;}
179 // returns first child cell (if there are any, i.e. if this is container):
180 virtual wxHtmlCell
* GetFirstChild() const { return NULL
; }
182 // members writing methods
183 virtual void SetPos(int x
, int y
) {m_PosX
= x
, m_PosY
= y
;}
184 void SetLink(const wxHtmlLinkInfo
& link
);
185 void SetNext(wxHtmlCell
*cell
) {m_Next
= cell
;}
187 // 1. adjust cell's width according to the fact that maximal possible width
188 // is w. (this has sense when working with horizontal lines, tables
190 // 2. prepare layout (=fill-in m_PosX, m_PosY (and sometime m_Height)
191 // members) = place items to fit window, according to the width w
192 virtual void Layout(int w
);
195 virtual void Draw(wxDC
& WXUNUSED(dc
),
196 int WXUNUSED(x
), int WXUNUSED(y
),
197 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
198 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
200 // proceed drawing actions in case the cell is not visible (scrolled out of
201 // screen). This is needed to change fonts, colors and so on.
202 virtual void DrawInvisible(wxDC
& WXUNUSED(dc
),
203 int WXUNUSED(x
), int WXUNUSED(y
),
204 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
206 // This method returns pointer to the FIRST cell for that
208 // is true. It first checks if the condition is true for this
209 // cell and then calls m_Next->Find(). (Note: it checks
210 // all subcells if the cell is container)
211 // Condition is unique condition identifier (see htmldefs.h)
212 // (user-defined condition IDs should start from 10000)
213 // and param is optional parameter
214 // Example : m_Cell->Find(wxHTML_COND_ISANCHOR, "news");
215 // returns pointer to anchor news
216 virtual const wxHtmlCell
* Find(int condition
, const void* param
) const;
218 // This function is called when mouse button is clicked over the cell.
220 // Parent is pointer to wxHtmlWindow that generated the event
221 // HINT: if this handling is not enough for you you should use
223 virtual void OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
);
225 // This method used to adjust pagebreak position. The parameter is variable
226 // that contains y-coordinate of page break (= horizontal line that should
227 // not be crossed by words, images etc.). If this cell cannot be divided
228 // into two pieces (each one on another page) then it moves the pagebreak
231 // Returned value : true if pagebreak was modified, false otherwise
232 // Usage : while (container->AdjustPagebreak(&p)) {}
233 virtual bool AdjustPagebreak(int *pagebreak
,
234 int *known_pagebreaks
= NULL
,
235 int number_of_pages
= 0) const;
237 // Sets cell's behaviour on pagebreaks (see AdjustPagebreak). Default
238 // is true - the cell can be split on two pages
239 void SetCanLiveOnPagebreak(bool can
) { m_CanLiveOnPagebreak
= can
; }
241 // Returns y-coordinates that contraint the cell, i.e. left is highest
242 // and right lowest coordinate such that the cell lays between then.
243 // Note: this method does not return meaningful values if you haven't
244 // called Layout() before!
245 virtual void GetHorizontalConstraints(int *left
, int *right
) const;
247 // Returns true for simple == terminal cells, i.e. not composite ones.
248 // This if for internal usage only and may disappear in future versions!
249 virtual bool IsTerminalCell() const { return TRUE
; }
251 // Find a cell inside this cell positioned at the given coordinates
252 // (relative to this's positions). Returns NULL if no such cell exists.
253 // The flag can be used to specify whether to look for terminal or
254 // nonterminal cells or both. In either case, returned cell is deepest
255 // cell in cells tree that contains [x,y].
256 virtual wxHtmlCell
*FindCellByPos(wxCoord x
, wxCoord y
,
257 unsigned flags
= wxHTML_FIND_EXACT
) const;
259 // Returns absolute position of the cell on HTML canvas
260 wxPoint
GetAbsPos() const;
262 // Returns first (last) terminal cell inside this cell. It may return NULL,
263 // but it is rare -- only if there are no terminals in the tree.
264 virtual wxHtmlCell
*GetFirstTerminal() const
265 { return wxConstCast(this, wxHtmlCell
); }
266 virtual wxHtmlCell
*GetLastTerminal() const
267 { return wxConstCast(this, wxHtmlCell
); }
269 // Returns cell's depth, i.e. how far under the root cell it is
270 // (if it is the root, depth is 0)
271 unsigned GetDepth() const;
273 // Returns true if the cell appears before 'cell' in natural order of
274 // cells (= as they are read). If cell A is (grand)parent of cell B,
275 // then both A.IsBefore(B) and B.IsBefore(A) always return true.
276 bool IsBefore(wxHtmlCell
*cell
) const;
278 // Converts the cell into text representation. If sel != NULL then
279 // only part of the cell inside the selection is converted.
280 virtual wxString
ConvertToText(wxHtmlSelection
*WXUNUSED(sel
)) const
281 { return wxEmptyString
; }
285 // pointer to the next cell
286 wxHtmlContainerCell
*m_Parent
;
287 // pointer to parent cell
288 long m_Width
, m_Height
, m_Descent
;
289 // dimensions of fragment
290 // m_Descent is used to position text&images..
292 // position where the fragment is drawn
293 wxHtmlLinkInfo
*m_Link
;
294 // destination address if this fragment is hypertext link, NULL otherwise
295 bool m_CanLiveOnPagebreak
;
296 // true if this cell can be placed on pagebreak, false otherwise
298 // unique identifier of the cell, generated from "id" property of tags
300 DECLARE_NO_COPY_CLASS(wxHtmlCell
)
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 // Single word in input stream.
314 // ----------------------------------------------------------------------------
316 class WXDLLEXPORT wxHtmlWordCell
: public wxHtmlCell
319 wxHtmlWordCell(const wxString
& word
, wxDC
& dc
);
320 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
321 wxHtmlRenderingInfo
& info
);
322 wxString
ConvertToText(wxHtmlSelection
*sel
) const;
326 const wxPoint
& selFrom
, const wxPoint
& selTo
,
327 wxString
& s1
, wxString
& s2
, wxString
& s3
,
328 unsigned& pos1
, unsigned& pos2
);
337 // Container contains other cells, thus forming tree structure of rendering
338 // elements. Basic code of layout algorithm is contained in this class.
339 class WXDLLEXPORT wxHtmlContainerCell
: public wxHtmlCell
342 wxHtmlContainerCell(wxHtmlContainerCell
*parent
);
343 ~wxHtmlContainerCell();
345 virtual void Layout(int w
);
346 virtual void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
347 wxHtmlRenderingInfo
& info
);
348 virtual void DrawInvisible(wxDC
& dc
, int x
, int y
,
349 wxHtmlRenderingInfo
& info
);
350 virtual bool AdjustPagebreak(int *pagebreak
, int *known_pagebreaks
= NULL
, int number_of_pages
= 0) const;
352 // insert cell at the end of m_Cells list
353 void InsertCell(wxHtmlCell
*cell
);
355 // sets horizontal/vertical alignment
356 void SetAlignHor(int al
) {m_AlignHor
= al
; m_LastLayout
= -1;}
357 int GetAlignHor() const {return m_AlignHor
;}
358 void SetAlignVer(int al
) {m_AlignVer
= al
; m_LastLayout
= -1;}
359 int GetAlignVer() const {return m_AlignVer
;}
361 // sets left-border indentation. units is one of wxHTML_UNITS_* constants
362 // what is combination of wxHTML_INDENT_*
363 void SetIndent(int i
, int what
, int units
= wxHTML_UNITS_PIXELS
);
364 // returns the indentation. ind is one of wxHTML_INDENT_* constants
365 int GetIndent(int ind
) const;
366 // returns type of value returned by GetIndent(ind)
367 int GetIndentUnits(int ind
) const;
369 // sets alignment info based on given tag's params
370 void SetAlign(const wxHtmlTag
& tag
);
371 // sets floating width adjustment
372 // (examples : 32 percent of parent container,
373 // -15 pixels percent (this means 100 % - 15 pixels)
374 void SetWidthFloat(int w
, int units
) {m_WidthFloat
= w
; m_WidthFloatUnits
= units
; m_LastLayout
= -1;}
375 void SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
= 1.0);
376 // sets minimal height of this container.
377 void SetMinHeight(int h
, int align
= wxHTML_ALIGN_TOP
) {m_MinHeight
= h
; m_MinHeightAlign
= align
; m_LastLayout
= -1;}
379 void SetBackgroundColour(const wxColour
& clr
) {m_UseBkColour
= TRUE
; m_BkColour
= clr
;}
380 // returns background colour (of wxNullColour if none set), so that widgets can
382 wxColour
GetBackgroundColour();
383 void SetBorder(const wxColour
& clr1
, const wxColour
& clr2
) {m_UseBorder
= TRUE
; m_BorderColour1
= clr1
, m_BorderColour2
= clr2
;}
384 virtual wxHtmlLinkInfo
* GetLink(int x
= 0, int y
= 0) const;
385 virtual const wxHtmlCell
* Find(int condition
, const void* param
) const;
386 virtual void OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
);
387 virtual void GetHorizontalConstraints(int *left
, int *right
) const;
389 virtual wxHtmlCell
* GetFirstChild() const { return m_Cells
; }
390 #if WXWIN_COMPATIBILITY_2_4
391 wxDEPRECATED( wxHtmlCell
* GetFirstCell() const );
394 // see comment in wxHtmlCell about this method
395 virtual bool IsTerminalCell() const { return FALSE
; }
397 virtual wxHtmlCell
*FindCellByPos(wxCoord x
, wxCoord y
,
398 unsigned flags
= wxHTML_FIND_EXACT
) const;
400 virtual wxHtmlCell
*GetFirstTerminal() const;
401 virtual wxHtmlCell
*GetLastTerminal() const;
404 void UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
405 wxHtmlCell
*cell
) const;
406 void UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
407 wxHtmlCell
*cell
) const;
410 int m_IndentLeft
, m_IndentRight
, m_IndentTop
, m_IndentBottom
;
411 // indentation of subcells. There is always m_Indent pixels
412 // big space between given border of the container and the subcells
413 // it m_Indent < 0 it is in PERCENTS, otherwise it is in pixels
414 int m_MinHeight
, m_MinHeightAlign
;
416 wxHtmlCell
*m_Cells
, *m_LastCell
;
417 // internal cells, m_Cells points to the first of them, m_LastCell to the last one.
418 // (LastCell is needed only to speed-up InsertCell)
419 int m_AlignHor
, m_AlignVer
;
420 // alignment horizontal and vertical (left, center, right)
421 int m_WidthFloat
, m_WidthFloatUnits
;
422 // width float is used in adjustWidth
425 // background color of this container
427 wxColour m_BorderColour1
, m_BorderColour2
;
428 // borders color of this container
430 // if != -1 then call to Layout may be no-op
431 // if previous call to Layout has same argument
433 DECLARE_NO_COPY_CLASS(wxHtmlContainerCell
)
436 #if WXWIN_COMPATIBILITY_2_4
437 inline wxHtmlCell
* wxHtmlContainerCell
::GetFirstCell() const
438 { return GetFirstChild(); }
444 // ---------------------------------------------------------------------------
447 // ---------------------------------------------------------------------------
449 class WXDLLEXPORT wxHtmlColourCell
: public wxHtmlCell
452 wxHtmlColourCell(const wxColour
& clr
, int flags
= wxHTML_CLR_FOREGROUND
) : wxHtmlCell() {m_Colour
= clr
; m_Flags
= flags
;}
453 virtual void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
454 wxHtmlRenderingInfo
& info
);
455 virtual void DrawInvisible(wxDC
& dc
, int x
, int y
,
456 wxHtmlRenderingInfo
& info
);
466 //--------------------------------------------------------------------------------
468 // Sets actual font used for text rendering
469 //--------------------------------------------------------------------------------
471 class WXDLLEXPORT wxHtmlFontCell
: public wxHtmlCell
474 wxHtmlFontCell(wxFont
*font
) : wxHtmlCell() { m_Font
= (*font
); }
475 virtual void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
476 wxHtmlRenderingInfo
& info
);
477 virtual void DrawInvisible(wxDC
& dc
, int x
, int y
,
478 wxHtmlRenderingInfo
& info
);
489 //--------------------------------------------------------------------------------
491 // This cell is connected with wxWindow object
492 // You can use it to insert windows into HTML page
493 // (buttons, input boxes etc.)
494 //--------------------------------------------------------------------------------
496 class WXDLLEXPORT wxHtmlWidgetCell
: public wxHtmlCell
499 // !!! wnd must have correct parent!
500 // if w != 0 then the m_Wnd has 'floating' width - it adjust
501 // it's width according to parent container's width
502 // (w is percent of parent's width)
503 wxHtmlWidgetCell(wxWindow
*wnd
, int w
= 0);
504 ~wxHtmlWidgetCell() { m_Wnd
->Destroy(); }
505 virtual void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
506 wxHtmlRenderingInfo
& info
);
507 virtual void DrawInvisible(wxDC
& dc
, int x
, int y
,
508 wxHtmlRenderingInfo
& info
);
509 virtual void Layout(int w
);
514 // width float is used in adjustWidth (it is in percents)
516 DECLARE_NO_COPY_CLASS(wxHtmlWidgetCell
)
521 //--------------------------------------------------------------------------------
523 // Internal data structure. It represents hypertext link
524 //--------------------------------------------------------------------------------
526 class WXDLLEXPORT wxHtmlLinkInfo
: public wxObject
529 wxHtmlLinkInfo() : wxObject()
530 { m_Href
= m_Target
= wxEmptyString
; m_Event
= NULL
, m_Cell
= NULL
; }
531 wxHtmlLinkInfo(const wxString
& href
, const wxString
& target
= wxEmptyString
) : wxObject()
532 { m_Href
= href
; m_Target
= target
; m_Event
= NULL
, m_Cell
= NULL
; }
533 wxHtmlLinkInfo(const wxHtmlLinkInfo
& l
) : wxObject()
534 { m_Href
= l
.m_Href
, m_Target
= l
.m_Target
, m_Event
= l
.m_Event
;
536 wxHtmlLinkInfo
& operator=(const wxHtmlLinkInfo
& l
)
537 { m_Href
= l
.m_Href
, m_Target
= l
.m_Target
, m_Event
= l
.m_Event
;
538 m_Cell
= l
.m_Cell
; return *this; }
540 void SetEvent(const wxMouseEvent
*e
) { m_Event
= e
; }
541 void SetHtmlCell(const wxHtmlCell
*e
) { m_Cell
= e
; }
543 wxString
GetHref() const { return m_Href
; }
544 wxString
GetTarget() const { return m_Target
; }
545 const wxMouseEvent
* GetEvent() const { return m_Event
; }
546 const wxHtmlCell
* GetHtmlCell() const { return m_Cell
; }
549 wxString m_Href
, m_Target
;
550 const wxMouseEvent
*m_Event
;
551 const wxHtmlCell
*m_Cell
;
556 // ----------------------------------------------------------------------------
557 // wxHtmlTerminalCellsInterator
558 // ----------------------------------------------------------------------------
560 class WXDLLEXPORT wxHtmlTerminalCellsInterator
563 wxHtmlTerminalCellsInterator(const wxHtmlCell
*from
, const wxHtmlCell
*to
)
564 : m_to(to
), m_pos(from
) {}
566 operator bool() const { return m_pos
!= NULL
; }
567 const wxHtmlCell
* operator++();
568 const wxHtmlCell
* operator->() const { return m_pos
; }
569 const wxHtmlCell
* operator*() const { return m_pos
; }
572 const wxHtmlCell
*m_to
, *m_pos
;
579 #endif // _WX_HTMLCELL_H_