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
)
46 m_fromCell
= fromCell
;
52 const wxHtmlCell
*GetFromCell() const { return m_fromCell
; }
53 const wxHtmlCell
*GetToCell() const { return m_toCell
; }
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
; }
59 const bool IsEmpty() const
60 { return m_fromPos
== wxDefaultPosition
&&
61 m_toPos
== wxDefaultPosition
; }
64 wxPoint m_fromPos
, m_toPos
;
65 wxHtmlCell
*m_fromCell
, *m_toCell
;
70 enum wxHtmlSelectionState
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
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
82 wxHtmlRenderingState(wxHtmlSelection
*s
)
83 : m_selection(s
), m_selState(wxHTML_SEL_OUT
) {}
84 wxHtmlSelection
*GetSelection() const { return m_selection
; }
86 void SetSelectionState(wxHtmlSelectionState s
) { m_selState
= s
; }
87 wxHtmlSelectionState
GetSelectionState() const { return m_selState
; }
89 void SetFgColour(const wxColour
& c
) { m_fgColour
= c
; }
90 const wxColour
& GetFgColour() const { return m_fgColour
; }
91 void SetBgColour(const wxColour
& c
) { m_bgColour
= c
; }
92 const wxColour
& GetBgColour() const { return m_bgColour
; }
95 wxHtmlSelection
*m_selection
;
96 wxHtmlSelectionState m_selState
;
97 wxColour m_fgColour
, m_bgColour
;
100 // Flags for wxHtmlCell::FindCellByPos
103 wxHTML_FIND_TERMINAL
= 0x0001,
104 wxHTML_FIND_NONTERMINAL
= 0x0002
108 // ---------------------------------------------------------------------------
110 // Internal data structure. It represents fragments of parsed
111 // HTML page - a word, picture, table, horizontal line and so
112 // on. It is used by wxHtmlWindow to represent HTML page in
114 // ---------------------------------------------------------------------------
117 class WXDLLEXPORT wxHtmlCell
: public wxObject
121 virtual ~wxHtmlCell();
123 void SetParent(wxHtmlContainerCell
*p
) {m_Parent
= p
;}
124 wxHtmlContainerCell
*GetParent() const {return m_Parent
;}
126 int GetPosX() const {return m_PosX
;}
127 int GetPosY() const {return m_PosY
;}
128 int GetWidth() const {return m_Width
;}
129 int GetHeight() const {return m_Height
;}
130 int GetDescent() const {return m_Descent
;}
132 const wxString
& GetId() const { return m_id
; }
133 void SetId(const wxString
& id
) { m_id
= id
; }
135 // returns the link associated with this cell. The position is position
136 // within the cell so it varies from 0 to m_Width, from 0 to m_Height
137 virtual wxHtmlLinkInfo
* GetLink(int WXUNUSED(x
) = 0, int WXUNUSED(y
) = 0) const
140 // members access methods
141 wxHtmlCell
*GetNext() const {return m_Next
;}
143 // members writing methods
144 virtual void SetPos(int x
, int y
) {m_PosX
= x
, m_PosY
= y
;}
145 void SetLink(const wxHtmlLinkInfo
& link
);
146 void SetNext(wxHtmlCell
*cell
) {m_Next
= cell
;}
148 // 1. adjust cell's width according to the fact that maximal possible width
149 // is w. (this has sense when working with horizontal lines, tables
151 // 2. prepare layout (=fill-in m_PosX, m_PosY (and sometime m_Height)
152 // members) = place items to fit window, according to the width w
153 virtual void Layout(int w
);
156 virtual void Draw(wxDC
& WXUNUSED(dc
),
157 int WXUNUSED(x
), int WXUNUSED(y
),
158 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
159 wxHtmlRenderingState
& WXUNUSED(state
)) {}
161 // proceed drawing actions in case the cell is not visible (scrolled out of
162 // screen). This is needed to change fonts, colors and so on.
163 virtual void DrawInvisible(wxDC
& WXUNUSED(dc
),
164 int WXUNUSED(x
), int WXUNUSED(y
),
165 wxHtmlRenderingState
& WXUNUSED(state
)) {}
167 // This method returns pointer to the FIRST cell for that
169 // is true. It first checks if the condition is true for this
170 // cell and then calls m_Next->Find(). (Note: it checks
171 // all subcells if the cell is container)
172 // Condition is unique condition identifier (see htmldefs.h)
173 // (user-defined condition IDs should start from 10000)
174 // and param is optional parameter
175 // Example : m_Cell->Find(wxHTML_COND_ISANCHOR, "news");
176 // returns pointer to anchor news
177 virtual const wxHtmlCell
* Find(int condition
, const void* param
) const;
179 // This function is called when mouse button is clicked over the cell.
181 // Parent is pointer to wxHtmlWindow that generated the event
182 // HINT: if this handling is not enough for you you should use
184 virtual void OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
);
186 // This method used to adjust pagebreak position. The parameter is variable
187 // that contains y-coordinate of page break (= horizontal line that should
188 // not be crossed by words, images etc.). If this cell cannot be divided
189 // into two pieces (each one on another page) then it moves the pagebreak
192 // Returned value : true if pagebreak was modified, false otherwise
193 // Usage : while (container->AdjustPagebreak(&p)) {}
194 virtual bool AdjustPagebreak(int *pagebreak
,
195 int *known_pagebreaks
= NULL
,
196 int number_of_pages
= 0) const;
198 // Sets cell's behaviour on pagebreaks (see AdjustPagebreak). Default
199 // is true - the cell can be split on two pages
200 void SetCanLiveOnPagebreak(bool can
) { m_CanLiveOnPagebreak
= can
; }
202 // Returns y-coordinates that contraint the cell, i.e. left is highest
203 // and right lowest coordinate such that the cell lays between then.
204 // Note: this method does not return meaningful values if you haven't
205 // called Layout() before!
206 virtual void GetHorizontalConstraints(int *left
, int *right
) const;
208 // Returns true for simple == terminal cells, i.e. not composite ones.
209 // This if for internal usage only and may disappear in future versions!
210 virtual bool IsTerminalCell() const { return TRUE
; }
212 // Find a cell inside this cell positioned at the given coordinates
213 // (relative to this's positions). Returns NULL if no such cell exists.
214 // The flag can be used to specify whether to look for terminal or
215 // nonterminal cells or both. In either case, returned cell is deepest
216 // cell in cells tree that contains [x,y].
217 virtual wxHtmlCell
*FindCellByPos(wxCoord x
, wxCoord y
,
218 unsigned flags
= wxHTML_FIND_TERMINAL
) const;
222 // pointer to the next cell
223 wxHtmlContainerCell
*m_Parent
;
224 // pointer to parent cell
225 long m_Width
, m_Height
, m_Descent
;
226 // dimensions of fragment
227 // m_Descent is used to position text&images..
229 // position where the fragment is drawn
230 wxHtmlLinkInfo
*m_Link
;
231 // destination address if this fragment is hypertext link, NULL otherwise
232 bool m_CanLiveOnPagebreak
;
233 // true if this cell can be placed on pagebreak, false otherwise
235 // unique identifier of the cell, generated from "id" property of tags
237 DECLARE_NO_COPY_CLASS(wxHtmlCell
)
243 //--------------------------------------------------------------------------------
245 //--------------------------------------------------------------------------------
248 //--------------------------------------------------------------------------------
250 // Single word in input stream.
251 //--------------------------------------------------------------------------------
253 class WXDLLEXPORT wxHtmlWordCell
: public wxHtmlCell
256 wxHtmlWordCell(const wxString
& word
, wxDC
& dc
);
257 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
258 wxHtmlRenderingState
& state
);
268 // Container contains other cells, thus forming tree structure of rendering
269 // elements. Basic code of layout algorithm is contained in this class.
270 class WXDLLEXPORT wxHtmlContainerCell
: public wxHtmlCell
273 wxHtmlContainerCell(wxHtmlContainerCell
*parent
);
274 ~wxHtmlContainerCell();
276 virtual void Layout(int w
);
277 virtual void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
278 wxHtmlRenderingState
& state
);
279 virtual void DrawInvisible(wxDC
& dc
, int x
, int y
,
280 wxHtmlRenderingState
& state
);
281 virtual bool AdjustPagebreak(int *pagebreak
, int *known_pagebreaks
= NULL
, int number_of_pages
= 0) const;
283 // insert cell at the end of m_Cells list
284 void InsertCell(wxHtmlCell
*cell
);
286 // sets horizontal/vertical alignment
287 void SetAlignHor(int al
) {m_AlignHor
= al
; m_LastLayout
= -1;}
288 int GetAlignHor() const {return m_AlignHor
;}
289 void SetAlignVer(int al
) {m_AlignVer
= al
; m_LastLayout
= -1;}
290 int GetAlignVer() const {return m_AlignVer
;}
292 // sets left-border indentation. units is one of wxHTML_UNITS_* constants
293 // what is combination of wxHTML_INDENT_*
294 void SetIndent(int i
, int what
, int units
= wxHTML_UNITS_PIXELS
);
295 // returns the indentation. ind is one of wxHTML_INDENT_* constants
296 int GetIndent(int ind
) const;
297 // returns type of value returned by GetIndent(ind)
298 int GetIndentUnits(int ind
) const;
300 // sets alignment info based on given tag's params
301 void SetAlign(const wxHtmlTag
& tag
);
302 // sets floating width adjustment
303 // (examples : 32 percent of parent container,
304 // -15 pixels percent (this means 100 % - 15 pixels)
305 void SetWidthFloat(int w
, int units
) {m_WidthFloat
= w
; m_WidthFloatUnits
= units
; m_LastLayout
= -1;}
306 void SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
= 1.0);
307 // sets minimal height of this container.
308 void SetMinHeight(int h
, int align
= wxHTML_ALIGN_TOP
) {m_MinHeight
= h
; m_MinHeightAlign
= align
; m_LastLayout
= -1;}
310 void SetBackgroundColour(const wxColour
& clr
) {m_UseBkColour
= TRUE
; m_BkColour
= clr
;}
311 // returns background colour (of wxNullColour if none set), so that widgets can
313 wxColour
GetBackgroundColour();
314 void SetBorder(const wxColour
& clr1
, const wxColour
& clr2
) {m_UseBorder
= TRUE
; m_BorderColour1
= clr1
, m_BorderColour2
= clr2
;}
315 virtual wxHtmlLinkInfo
* GetLink(int x
= 0, int y
= 0) const;
316 virtual const wxHtmlCell
* Find(int condition
, const void* param
) const;
317 virtual void OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
);
318 virtual void GetHorizontalConstraints(int *left
, int *right
) const;
320 // returns pointer to the first cell in container or NULL
321 wxHtmlCell
* GetFirstCell() const {return m_Cells
;}
323 // see comment in wxHtmlCell about this method
324 virtual bool IsTerminalCell() const { return FALSE
; }
326 virtual wxHtmlCell
*FindCellByPos(wxCoord x
, wxCoord y
,
327 unsigned flags
= wxHTML_FIND_TERMINAL
) const;
330 void UpdateRenderingStatePre(wxHtmlRenderingState
& state
,
331 wxHtmlCell
*cell
) const;
332 void UpdateRenderingStatePost(wxHtmlRenderingState
& state
,
333 wxHtmlCell
*cell
) const;
336 int m_IndentLeft
, m_IndentRight
, m_IndentTop
, m_IndentBottom
;
337 // indentation of subcells. There is always m_Indent pixels
338 // big space between given border of the container and the subcells
339 // it m_Indent < 0 it is in PERCENTS, otherwise it is in pixels
340 int m_MinHeight
, m_MinHeightAlign
;
342 wxHtmlCell
*m_Cells
, *m_LastCell
;
343 // internal cells, m_Cells points to the first of them, m_LastCell to the last one.
344 // (LastCell is needed only to speed-up InsertCell)
345 int m_AlignHor
, m_AlignVer
;
346 // alignment horizontal and vertical (left, center, right)
347 int m_WidthFloat
, m_WidthFloatUnits
;
348 // width float is used in adjustWidth
351 // background color of this container
353 wxColour m_BorderColour1
, m_BorderColour2
;
354 // borders color of this container
356 // if != -1 then call to Layout may be no-op
357 // if previous call to Layout has same argument
359 DECLARE_NO_COPY_CLASS(wxHtmlContainerCell
)
366 //--------------------------------------------------------------------------------
369 //--------------------------------------------------------------------------------
371 class WXDLLEXPORT wxHtmlColourCell
: public wxHtmlCell
374 wxHtmlColourCell(const wxColour
& clr
, int flags
= wxHTML_CLR_FOREGROUND
) : wxHtmlCell() {m_Colour
= clr
; m_Flags
= flags
;}
375 virtual void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
376 wxHtmlRenderingState
& state
);
377 virtual void DrawInvisible(wxDC
& dc
, int x
, int y
,
378 wxHtmlRenderingState
& state
);
388 //--------------------------------------------------------------------------------
390 // Sets actual font used for text rendering
391 //--------------------------------------------------------------------------------
393 class WXDLLEXPORT wxHtmlFontCell
: public wxHtmlCell
396 wxHtmlFontCell(wxFont
*font
) : wxHtmlCell() { m_Font
= (*font
); }
397 virtual void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
398 wxHtmlRenderingState
& state
);
399 virtual void DrawInvisible(wxDC
& dc
, int x
, int y
,
400 wxHtmlRenderingState
& state
);
411 //--------------------------------------------------------------------------------
413 // This cell is connected with wxWindow object
414 // You can use it to insert windows into HTML page
415 // (buttons, input boxes etc.)
416 //--------------------------------------------------------------------------------
418 class WXDLLEXPORT wxHtmlWidgetCell
: public wxHtmlCell
421 // !!! wnd must have correct parent!
422 // if w != 0 then the m_Wnd has 'floating' width - it adjust
423 // it's width according to parent container's width
424 // (w is percent of parent's width)
425 wxHtmlWidgetCell(wxWindow
*wnd
, int w
= 0);
426 ~wxHtmlWidgetCell() { m_Wnd
->Destroy(); }
427 virtual void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
428 wxHtmlRenderingState
& state
);
429 virtual void DrawInvisible(wxDC
& dc
, int x
, int y
,
430 wxHtmlRenderingState
& state
);
431 virtual void Layout(int w
);
436 // width float is used in adjustWidth (it is in percents)
438 DECLARE_NO_COPY_CLASS(wxHtmlWidgetCell
)
443 //--------------------------------------------------------------------------------
445 // Internal data structure. It represents hypertext link
446 //--------------------------------------------------------------------------------
448 class WXDLLEXPORT wxHtmlLinkInfo
: public wxObject
451 wxHtmlLinkInfo() : wxObject()
452 { m_Href
= m_Target
= wxEmptyString
; m_Event
= NULL
, m_Cell
= NULL
; }
453 wxHtmlLinkInfo(const wxString
& href
, const wxString
& target
= wxEmptyString
) : wxObject()
454 { m_Href
= href
; m_Target
= target
; m_Event
= NULL
, m_Cell
= NULL
; }
455 wxHtmlLinkInfo(const wxHtmlLinkInfo
& l
) : wxObject()
456 { m_Href
= l
.m_Href
, m_Target
= l
.m_Target
, m_Event
= l
.m_Event
;
458 wxHtmlLinkInfo
& operator=(const wxHtmlLinkInfo
& l
)
459 { m_Href
= l
.m_Href
, m_Target
= l
.m_Target
, m_Event
= l
.m_Event
;
460 m_Cell
= l
.m_Cell
; return *this; }
462 void SetEvent(const wxMouseEvent
*e
) { m_Event
= e
; }
463 void SetHtmlCell(const wxHtmlCell
*e
) { m_Cell
= e
; }
465 wxString
GetHref() const { return m_Href
; }
466 wxString
GetTarget() const { return m_Target
; }
467 const wxMouseEvent
* GetEvent() const { return m_Event
; }
468 const wxHtmlCell
* GetHtmlCell() const { return m_Cell
; }
471 wxString m_Href
, m_Target
;
472 const wxMouseEvent
*m_Event
;
473 const wxHtmlCell
*m_Cell
;
481 #endif // _WX_HTMLCELL_H_