added support for background bitmap specified in background attribute of the HTML...
[wxWidgets.git] / include / wx / html / htmlwin.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlwin.h
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifndef _WX_HTMLWIN_H_
12 #define _WX_HTMLWIN_H_
13
14 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
15 #pragma interface "htmlwin.h"
16 #endif
17
18 #include "wx/defs.h"
19 #if wxUSE_HTML
20
21 #include "wx/window.h"
22 #include "wx/scrolwin.h"
23 #include "wx/config.h"
24 #include "wx/html/winpars.h"
25 #include "wx/html/htmlcell.h"
26 #include "wx/filesys.h"
27 #include "wx/html/htmlfilt.h"
28 #include "wx/filename.h"
29
30 class wxHtmlProcessor;
31 class wxHtmlWinModule;
32 class wxHtmlHistoryArray;
33 class wxHtmlProcessorList;
34 class WXDLLIMPEXP_HTML wxHtmlWinAutoScrollTimer;
35
36
37 // wxHtmlWindow flags:
38 #define wxHW_SCROLLBAR_NEVER 0x0002
39 #define wxHW_SCROLLBAR_AUTO 0x0004
40 #define wxHW_NO_SELECTION 0x0008
41
42 #define wxHW_DEFAULT_STYLE wxHW_SCROLLBAR_AUTO
43
44
45 // enums for wxHtmlWindow::OnOpeningURL
46 enum wxHtmlOpeningStatus
47 {
48 wxHTML_OPEN,
49 wxHTML_BLOCK,
50 wxHTML_REDIRECT
51 };
52
53 // ----------------------------------------------------------------------------
54 // wxHtmlWindow
55 // (This is probably the only class you will directly use.)
56 // Purpose of this class is to display HTML page (either local
57 // file or downloaded via HTTP protocol) in a window. Width of
58 // window is constant - given in constructor - virtual height
59 // is changed dynamicly depending on page size. Once the
60 // window is created you can set it's content by calling
61 // SetPage(text) or LoadPage(filename).
62 // ----------------------------------------------------------------------------
63
64 class WXDLLIMPEXP_HTML wxHtmlWindow : public wxScrolledWindow
65 {
66 DECLARE_DYNAMIC_CLASS(wxHtmlWindow)
67 friend class wxHtmlWinModule;
68
69 public:
70 wxHtmlWindow() { Init(); }
71 wxHtmlWindow(wxWindow *parent, wxWindowID id = wxID_ANY,
72 const wxPoint& pos = wxDefaultPosition,
73 const wxSize& size = wxDefaultSize,
74 long style = wxHW_DEFAULT_STYLE,
75 const wxString& name = wxT("htmlWindow"))
76 {
77 Init();
78 Create(parent, id, pos, size, style, name);
79 }
80 ~wxHtmlWindow();
81
82 bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
83 const wxPoint& pos = wxDefaultPosition,
84 const wxSize& size = wxDefaultSize,
85 long style = wxHW_SCROLLBAR_AUTO,
86 const wxString& name = wxT("htmlWindow"));
87
88 // Set HTML page and display it. !! source is HTML document itself,
89 // it is NOT address/filename of HTML document. If you want to
90 // specify document location, use LoadPage() istead
91 // Return value : false if an error occured, true otherwise
92 bool SetPage(const wxString& source);
93
94 // Append to current page
95 bool AppendToPage(const wxString& source);
96
97 // Load HTML page from given location. Location can be either
98 // a) /usr/wxGTK2/docs/html/wx.htm
99 // b) http://www.somewhere.uk/document.htm
100 // c) ftp://ftp.somesite.cz/pub/something.htm
101 // In case there is no prefix (http:,ftp:), the method
102 // will try to find it itself (1. local file, then http or ftp)
103 // After the page is loaded, the method calls SetPage() to display it.
104 // Note : you can also use path relative to previously loaded page
105 // Return value : same as SetPage
106 virtual bool LoadPage(const wxString& location);
107
108 // Loads HTML page from file
109 bool LoadFile(const wxFileName& filename);
110
111 // Returns full location of opened page
112 wxString GetOpenedPage() const {return m_OpenedPage;}
113 // Returns anchor within opened page
114 wxString GetOpenedAnchor() const {return m_OpenedAnchor;}
115 // Returns <TITLE> of opened page or empty string otherwise
116 wxString GetOpenedPageTitle() const {return m_OpenedPageTitle;}
117
118 // Sets frame in which page title will be displayed. Format is format of
119 // frame title, e.g. "HtmlHelp : %s". It must contain exactly one %s
120 void SetRelatedFrame(wxFrame* frame, const wxString& format);
121 wxFrame* GetRelatedFrame() const {return m_RelatedFrame;}
122
123 #if wxUSE_STATUSBAR
124 // After(!) calling SetRelatedFrame, this sets statusbar slot where messages
125 // will be displayed. Default is -1 = no messages.
126 void SetRelatedStatusBar(int bar);
127 #endif // wxUSE_STATUSBAR
128
129 // Sets fonts to be used when displaying HTML page.
130 void SetFonts(wxString normal_face, wxString fixed_face,
131 const int *sizes = NULL);
132
133 // Sets font sizes to be relative to the given size or the system
134 // default size; use either specified or default font
135 void SetStandardFonts(int size = -1,
136 const wxString& normal_face = wxEmptyString,
137 const wxString& fixed_face = wxEmptyString);
138
139 // Sets space between text and window borders.
140 void SetBorders(int b) {m_Borders = b;}
141
142 // Sets the bitmap to use for background (currnetly it will be tiled,
143 // when/if we have CSS support we could add other possibilities...)
144 void SetBackgroundImage(const wxBitmap& bmpBg) { m_bmpBg = bmpBg; }
145
146 // Saves custom settings into cfg config. it will use the path 'path'
147 // if given, otherwise it will save info into currently selected path.
148 // saved values : things set by SetFonts, SetBorders.
149 virtual void ReadCustomization(wxConfigBase *cfg, wxString path = wxEmptyString);
150 // ...
151 virtual void WriteCustomization(wxConfigBase *cfg, wxString path = wxEmptyString);
152
153 // Goes to previous/next page (in browsing history)
154 // Returns true if successful, false otherwise
155 bool HistoryBack();
156 bool HistoryForward();
157 bool HistoryCanBack();
158 bool HistoryCanForward();
159 // Resets history
160 void HistoryClear();
161
162 // Returns pointer to conteiners/cells structure.
163 // It should be used ONLY when printing
164 wxHtmlContainerCell* GetInternalRepresentation() const {return m_Cell;}
165
166 // Adds input filter
167 static void AddFilter(wxHtmlFilter *filter);
168
169 // Returns a pointer to the parser.
170 wxHtmlWinParser *GetParser() const { return m_Parser; }
171
172 // Adds HTML processor to this instance of wxHtmlWindow:
173 void AddProcessor(wxHtmlProcessor *processor);
174 // Adds HTML processor to wxHtmlWindow class as whole:
175 static void AddGlobalProcessor(wxHtmlProcessor *processor);
176
177
178 // -- Callbacks --
179
180 // Sets the title of the window
181 // (depending on the information passed to SetRelatedFrame() method)
182 virtual void OnSetTitle(const wxString& title);
183
184 // Called when the mouse hovers over a cell: (x, y) are logical coords
185 // Default behaviour is to do nothing at all
186 virtual void OnCellMouseHover(wxHtmlCell *cell, wxCoord x, wxCoord y);
187
188 // Called when user clicks on a cell. Default behavior is to call
189 // OnLinkClicked() if this cell corresponds to a hypertext link
190 virtual void OnCellClicked(wxHtmlCell *cell,
191 wxCoord x, wxCoord y,
192 const wxMouseEvent& event);
193
194 // Called when user clicked on hypertext link. Default behavior is to
195 // call LoadPage(loc)
196 virtual void OnLinkClicked(const wxHtmlLinkInfo& link);
197
198 // Called when wxHtmlWindow wants to fetch data from an URL (e.g. when
199 // loading a page or loading an image). The data are downloaded if and only if
200 // OnOpeningURL returns true. If OnOpeningURL returns wxHTML_REDIRECT,
201 // it must set *redirect to the new URL
202 virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type),
203 const wxString& WXUNUSED(url),
204 wxString *WXUNUSED(redirect)) const
205 { return wxHTML_OPEN; }
206
207 #if wxUSE_CLIPBOARD
208 // Helper functions to select parts of page:
209 void SelectWord(const wxPoint& pos);
210 void SelectLine(const wxPoint& pos);
211 void SelectAll();
212
213 // Convert selection to text:
214 wxString SelectionToText() { return DoSelectionToText(m_selection); }
215
216 // Converts current page to text:
217 wxString ToText();
218 #endif // wxUSE_CLIPBOARD
219
220 protected:
221 void Init();
222
223 // Scrolls to anchor of this name. (Anchor is #news
224 // or #features etc. it is part of address sometimes:
225 // http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/index.html#news)
226 // Return value : true if anchor exists, false otherwise
227 bool ScrollToAnchor(const wxString& anchor);
228
229 // Prepares layout (= fill m_PosX, m_PosY for fragments) based on
230 // actual size of window. This method also setup scrollbars
231 void CreateLayout();
232
233 void OnEraseBackground(wxEraseEvent& event);
234 void OnPaint(wxPaintEvent& event);
235 void OnSize(wxSizeEvent& event);
236 void OnMouseMove(wxMouseEvent& event);
237 void OnMouseDown(wxMouseEvent& event);
238 void OnMouseUp(wxMouseEvent& event);
239 #if wxUSE_CLIPBOARD
240 void OnKeyUp(wxKeyEvent& event);
241 void OnDoubleClick(wxMouseEvent& event);
242 void OnCopy(wxCommandEvent& event);
243 void OnMouseEnter(wxMouseEvent& event);
244 void OnMouseLeave(wxMouseEvent& event);
245 #endif // wxUSE_CLIPBOARD
246
247 virtual void OnInternalIdle();
248
249 // Returns new filter (will be stored into m_DefaultFilter variable)
250 virtual wxHtmlFilter *GetDefaultFilter() {return new wxHtmlFilterPlainText;}
251
252 // cleans static variables
253 static void CleanUpStatics();
254
255 // Returns true if text selection is enabled (wxClipboard must be available
256 // and wxHW_NO_SELECTION not used)
257 bool IsSelectionEnabled() const;
258
259 enum ClipboardType
260 {
261 Primary,
262 Secondary
263 };
264
265 // Copies selection to clipboard if the clipboard support is available
266 //
267 // returns true if anything was copied to clipboard, false otherwise
268 bool CopySelection(ClipboardType t = Secondary);
269
270 #if wxUSE_CLIPBOARD
271 // Automatic scrolling during selection:
272 void StopAutoScrolling();
273 #endif // wxUSE_CLIPBOARD
274
275 protected:
276 wxString DoSelectionToText(wxHtmlSelection *sel);
277
278 // This is pointer to the first cell in parsed data. (Note: the first cell
279 // is usually top one = all other cells are sub-cells of this one)
280 wxHtmlContainerCell *m_Cell;
281 // parser which is used to parse HTML input.
282 // Each wxHtmlWindow has it's own parser because sharing one global
283 // parser would be problematic (because of reentrancy)
284 wxHtmlWinParser *m_Parser;
285 // contains name of actualy opened page or empty string if no page opened
286 wxString m_OpenedPage;
287 // contains name of current anchor within m_OpenedPage
288 wxString m_OpenedAnchor;
289 // contains title of actualy opened page or empty string if no <TITLE> tag
290 wxString m_OpenedPageTitle;
291 // class for opening files (file system)
292 wxFileSystem* m_FS;
293
294 wxFrame *m_RelatedFrame;
295 wxString m_TitleFormat;
296 #if wxUSE_STATUSBAR
297 // frame in which page title should be displayed & number of it's statusbar
298 // reserved for usage with this html window
299 int m_RelatedStatusBar;
300 #endif // wxUSE_STATUSBAR
301
302 // borders (free space between text and window borders)
303 // defaults to 10 pixels.
304 int m_Borders;
305
306 int m_Style;
307
308 // current text selection or NULL
309 wxHtmlSelection *m_selection;
310
311 // true if the user is dragging mouse to select text
312 bool m_makingSelection;
313
314 #if wxUSE_CLIPBOARD
315 // time of the last doubleclick event, used to detect tripleclicks
316 // (tripleclicks are used to select whole line):
317 wxLongLong m_lastDoubleClick;
318
319 // helper class to automatically scroll the window if the user is selecting
320 // text and the mouse leaves wxHtmlWindow:
321 wxHtmlWinAutoScrollTimer *m_timerAutoScroll;
322 #endif // wxUSE_CLIPBOARD
323
324 private:
325 // window content for double buffered rendering:
326 wxBitmap *m_backBuffer;
327
328 // background image, may be invalid
329 wxBitmap m_bmpBg;
330
331 // variables used when user is selecting text
332 wxPoint m_tmpSelFromPos;
333 wxHtmlCell *m_tmpSelFromCell;
334
335 // a flag indicated if mouse moved
336 // (if true we will try to change cursor in last call to OnIdle)
337 bool m_tmpMouseMoved;
338 // contains last link name
339 wxHtmlLinkInfo *m_tmpLastLink;
340 // contains the last (terminal) cell which contained the mouse
341 wxHtmlCell *m_tmpLastCell;
342 // if >0 contents of the window is not redrawn
343 // (in order to avoid ugly blinking)
344 int m_tmpCanDrawLocks;
345
346 // list of HTML filters
347 static wxList m_Filters;
348 // this filter is used when no filter is able to read some file
349 static wxHtmlFilter *m_DefaultFilter;
350
351 wxHtmlHistoryArray *m_History;
352 // browser history
353 int m_HistoryPos;
354 // if this FLAG is false, items are not added to history
355 bool m_HistoryOn;
356
357 // html processors array:
358 wxHtmlProcessorList *m_Processors;
359 static wxHtmlProcessorList *m_GlobalProcessors;
360
361 DECLARE_EVENT_TABLE()
362 DECLARE_NO_COPY_CLASS(wxHtmlWindow)
363 };
364
365
366 #endif // wxUSE_HTML
367
368 #endif // _WX_HTMLWIN_H_
369