File: htmlwin.h

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (600 lines) | stat: -rw-r--r-- 19,059 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/////////////////////////////////////////////////////////////////////////////
// Name:        html/htmlwin.h
// Purpose:     interface of wxHtmlWindow
// Author:      wxWidgets team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// wxHtmlWindow flags:
#define wxHW_SCROLLBAR_NEVER    0x0002
#define wxHW_SCROLLBAR_AUTO     0x0004
#define wxHW_NO_SELECTION       0x0008

#define wxHW_DEFAULT_STYLE      wxHW_SCROLLBAR_AUTO


/// Enum for wxHtmlWindow::OnOpeningURL and wxHtmlWindowInterface::OnOpeningURL
enum wxHtmlOpeningStatus
{
    /// Open the requested URL
    wxHTML_OPEN,
    /// Do not open the URL
    wxHTML_BLOCK,
    /// Redirect to another URL (returned from OnOpeningURL)
    wxHTML_REDIRECT
};


/**
   @class wxHtmlWindowInterface
   
   Abstract interface to a HTML rendering window (such as wxHtmlWindow or
   wxHtmlListBox) that is passed to wxHtmlWinParser. It encapsulates all
   communication from the parser to the window.
 */
class wxHtmlWindowInterface
{
public:
    /// Ctor
    wxHtmlWindowInterface();
    virtual ~wxHtmlWindowInterface();

    /**
        Called by the parser to set window's title to given text.
     */
    virtual void SetHTMLWindowTitle(const wxString& title) = 0;

    /**
        Called when a link is clicked.

        @param link information about the clicked link
     */
    virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo& link) = 0;

    /**
        Called when the parser needs to open another URL (e.g. an image).

        @param type     Type of the URL request (e.g. image)
        @param url      URL the parser wants to open
        @param redirect If the return value is wxHTML_REDIRECT, then the
                        URL to redirect to will be stored in this variable
                        (the pointer must never be NULL)

        @return indicator of how to treat the request
     */
    virtual wxHtmlOpeningStatus OnHTMLOpeningURL(wxHtmlURLType type,
                                                 const wxString& url,
                                                 wxString *redirect) const = 0;

    /**
        Converts coordinates @a pos relative to given @a cell to
        physical coordinates in the window.
     */
    virtual wxPoint HTMLCoordsToWindow(wxHtmlCell *cell,
                                       const wxPoint& pos) const = 0;

    /// Returns the window used for rendering (may be NULL).
    virtual wxWindow* GetHTMLWindow() = 0;

    /// Returns background colour to use by default.
    virtual wxColour GetHTMLBackgroundColour() const = 0;

    /// Sets window's background to colour @a clr.
    virtual void SetHTMLBackgroundColour(const wxColour& clr) = 0;

    /// Sets window's background to given bitmap.
    virtual void SetHTMLBackgroundImage(const wxBitmap& bmpBg) = 0;

    /// Sets status bar text.
    virtual void SetHTMLStatusText(const wxString& text) = 0;

    /// Type of mouse cursor
    enum HTMLCursor
    {
        /// Standard mouse cursor (typically an arrow)
        HTMLCursor_Default,
        /// Cursor shown over links
        HTMLCursor_Link,
        /// Cursor shown over selectable text
        HTMLCursor_Text
    };

    /**
        Returns mouse cursor of given @a type.
     */
    virtual wxCursor GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor type) const = 0;
};



/**
    @class wxHtmlWindow

    wxHtmlWindow is probably the only class you will directly use unless you want
    to do something special (like adding new tag handlers or MIME filters).

    The purpose of this class is to display rich content pages (either local file or
    downloaded via HTTP protocol) in a window based on a subset of the HTML standard.
    The width of the window is constant - given in the constructor - and virtual height
    is changed dynamically depending on page size.
    Once the window is created you can set its content by calling SetPage() with raw HTML,
    LoadPage() with a wxFileSystem location or LoadFile() with a filename.

    @note
    If you want complete HTML/CSS support as well as a Javascript engine, see instead
    wxWebView.

    @note
    wxHtmlWindow uses the wxImage class for displaying images, as such you need to
    initialize the handlers for any image formats you use before loading a page.
    See ::wxInitAllImageHandlers and wxImage::AddHandler.

    @beginStyleTable
    @style{wxHW_SCROLLBAR_NEVER}
           Never display scrollbars, not even when the page is larger than the
           window.
    @style{wxHW_SCROLLBAR_AUTO}
           Display scrollbars only if page's size exceeds window's size.
    @style{wxHW_NO_SELECTION}
           Don't allow the user to select text.
    @endStyleTable


    @beginEventEmissionTable{wxHtmlCellEvent, wxHtmlLinkEvent}
    @event{EVT_HTML_CELL_CLICKED(id, func)}
        A wxHtmlCell was clicked.
    @event{EVT_HTML_CELL_HOVER(id, func)}
        The mouse passed over a wxHtmlCell.
    @event{EVT_HTML_LINK_CLICKED(id, func)}
        A wxHtmlCell which contains an hyperlink was clicked.
    @endEventTable

    @library{wxhtml}
    @category{html}

    @see wxHtmlLinkEvent, wxHtmlCellEvent
*/
class wxHtmlWindow : public wxScrolledWindow, public wxHtmlWindowInterface
{
public:
    /**
        Default ctor.
    */
    wxHtmlWindow();

    /**
        Constructor.
        The parameters are the same as wxScrolled::wxScrolled() constructor.
    */
    wxHtmlWindow(wxWindow *parent, wxWindowID id = wxID_ANY,
                 const wxPoint& pos = wxDefaultPosition,
                 const wxSize& size = wxDefaultSize,
                 long style = wxHW_DEFAULT_STYLE,
                 const wxString& name = "htmlWindow");

    /**
        Adds @ref overview_html_filters "input filter" to the static list of available
        filters. These filters are present by default:
        - @c text/html MIME type
        - @c image/* MIME types
        - Plain Text filter (this filter is used if no other filter matches)
    */
    static void AddFilter(wxHtmlFilter* filter);

    /**
        Appends HTML fragment to currently displayed text and refreshes the window.

        @param source
            HTML code fragment

        @return @false if an error occurred, @true otherwise.
    */
    bool AppendToPage(const wxString& source);

    /**
        Returns pointer to the top-level container.

        @see @ref overview_html_cells, @ref overview_printing
    */
    wxHtmlContainerCell* GetInternalRepresentation() const;

    /**
        Returns anchor within currently opened page (see wxHtmlWindow::GetOpenedPage).
        If no page is opened or if the displayed page wasn't produced by call to
        LoadPage(), empty string is returned.
    */
    wxString GetOpenedAnchor() const;

    /**
        Returns full location of the opened page.
        If no page is opened or if the displayed page wasn't produced by call to
        LoadPage(), empty string is returned.
    */
    wxString GetOpenedPage() const;

    /**
        Returns title of the opened page or wxEmptyString if the current page does not
        contain \<TITLE\> tag.
    */
    wxString GetOpenedPageTitle() const;

    /**
        Returns the related frame.
    */
    wxFrame* GetRelatedFrame() const;

    /**
        Moves back to the previous page. Only pages displayed using LoadPage()
        are stored in history list.
    */
    bool HistoryBack();

    /**
        Returns @true if it is possible to go back in the history
        i.e. HistoryBack() won't fail.
    */
    bool HistoryCanBack();

    /**
        Returns @true if it is possible to go forward in the history
        i.e. HistoryForward() won't fail.
    */
    bool HistoryCanForward();

    /**
        Clears history.
    */
    void HistoryClear();

    /**
        Moves to next page in history. Only pages displayed using LoadPage()
        are stored in history list.
    */
    bool HistoryForward();

    /**
        Loads an HTML page from a file and displays it.

        @return @false if an error occurred, @true otherwise

        @see LoadPage()
    */
    bool LoadFile(const wxFileName& filename);

    /**
        Unlike SetPage() this function first loads the HTML page from @a location
        and then displays it.

        @param location
            The address of the document.
            See the @ref overview_fs for details on the address format
            and wxFileSystem for a description of how the file is opened.

        @return @false if an error occurred, @true otherwise

        @see LoadFile()
    */
    virtual bool LoadPage(const wxString& location);

    /**
        Called when user clicks on hypertext link.
        Default behaviour is to emit a wxHtmlLinkEvent and, if the event was not
        processed or skipped, call LoadPage() and do nothing else.

        Overloading this method is deprecated; intercept the event instead.

        Also see wxHtmlLinkInfo.
    */
    virtual void OnLinkClicked(const wxHtmlLinkInfo& link);

    /**
        Called when an URL is being opened (either when the user clicks on a link or
        an image is loaded). The URL will be opened only if OnOpeningURL() returns
        @c wxHTML_OPEN. This method is called by wxHtmlParser::OpenURL.

        You can override OnOpeningURL() to selectively block some URLs
        (e.g. for security reasons) or to redirect them elsewhere.
        Default behaviour is to always return @c wxHTML_OPEN.

        @param type
            Indicates type of the resource. Is one of
            - wxHTML_URL_PAGE: Opening a HTML page.
            - wxHTML_URL_IMAGE: Opening an image.
            - wxHTML_URL_OTHER: Opening a resource that doesn't fall into
                                any other category.
        @param url
            URL being opened.
        @param redirect
            Pointer to wxString variable that must be filled with an
            URL if OnOpeningURL() returns @c wxHTML_REDIRECT.

        The return value is:
        - wxHTML_OPEN: Open the URL.
        - wxHTML_BLOCK: Deny access to the URL, wxHtmlParser::OpenURL will return @NULL.
        - wxHTML_REDIRECT: Don't open url, redirect to another URL.
          OnOpeningURL() must fill *redirect with the new URL.
          OnOpeningURL() will be called again on returned URL.
    */
    virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType type,
                                             const wxString& url,
                                             wxString* redirect) const;

    /**
        Called on parsing \<TITLE\> tag.
    */
    virtual void OnSetTitle(const wxString& title);

    /**
        This reads custom settings from wxConfig. It uses the path 'path'
        if given, otherwise it saves info into currently selected path.
        The values are stored in sub-path @c wxHtmlWindow.
        Read values: all things set by SetFonts(), SetBorders().

        @param cfg
            wxConfig from which you want to read the configuration.
        @param path
            Optional path in config tree. If not given current path is used.
    */
    virtual void ReadCustomization(wxConfigBase* cfg,
                                   wxString path = wxEmptyString);

    /**
        Selects all text in the window.

        @see SelectLine(), SelectWord()
    */
    void SelectAll();

    /**
        Selects the line of text that @a pos points at. Note that @e pos
        is relative to the top of displayed page, not to window's origin, use
        wxScrolled::CalcUnscrolledPosition()
        to convert physical coordinate.

        @see SelectAll(), SelectWord()
    */
    void SelectLine(const wxPoint& pos);

    /**
        Selects the word at position @a pos.
        Note that @a pos is relative to the top of displayed page, not to window's
        origin, use wxScrolled::CalcUnscrolledPosition() to convert physical coordinate.

        @see SelectAll(), SelectLine()
    */
    void SelectWord(const wxPoint& pos);

    /**
        Returns the current selection as plain text.
        Returns an empty string if no text is currently selected.
    */
    wxString SelectionToText();

    /**
        This function sets the space between border of window and HTML contents.
        See image:
        @image html htmlwin_border.png

        @param b
            indentation from borders in pixels
    */
    void SetBorders(int b);

    /**
        This function sets font sizes and faces. See wxHtmlDCRenderer::SetFonts
        for detailed description.

        @see SetSize()
    */
    void SetFonts(const wxString& normal_face, const wxString& fixed_face,
                  const int* sizes = NULL);

    /**
        Sets default font sizes and/or default font size. 
        See wxHtmlDCRenderer::SetStandardFonts for detailed description.
        @see SetFonts()
    */
    void SetStandardFonts(int size = -1,
                          const wxString& normal_face = wxEmptyString,
                          const wxString& fixed_face = wxEmptyString);

    /**
        Sets the source of a page and displays it, for example:
        @code
        htmlwin -> SetPage("<html><body>Hello, world!</body></html>");
        @endcode

        If you want to load a document from some location use LoadPage() instead.

        @param source
            The HTML to be displayed.

        @return @false if an error occurred, @true otherwise.
    */
    virtual bool SetPage(const wxString& source);

    /**
        Sets the frame in which page title will be displayed.
        @a format is the format of the frame title, e.g. "HtmlHelp : %s".
        It must contain exactly one %s.
        This %s is substituted with HTML page title.
    */
    void SetRelatedFrame(wxFrame* frame, const wxString& format);

    /**
        @b After calling SetRelatedFrame(), this sets statusbar slot where messages
        will be displayed. (Default is -1 = no messages.)

        @param index
            Statusbar slot number (0..n)
    */
    void SetRelatedStatusBar(int index);

    /**
        @b Sets the associated statusbar where messages will be displayed.
        Call this instead of SetRelatedFrame() if you want statusbar updates only,
        no changing of the frame title.

        @param statusbar
            Statusbar pointer
        @param index
            Statusbar slot number (0..n)

        @since 2.9.0
    */
    void SetRelatedStatusBar(wxStatusBar* statusbar, int index = 0);

    /**
        Returns content of currently displayed page as plain text.
    */
    wxString ToText();

    /**
        Saves custom settings into wxConfig.
        It uses the path 'path' if given, otherwise it saves info into currently
        selected path.
        Regardless of whether the path is given or not, the function creates
        sub-path @c wxHtmlWindow.

        Saved values: all things set by SetFonts(), SetBorders().

        @param cfg
            wxConfig to which you want to save the configuration.
        @param path
            Optional path in config tree. If not given, the current path is used.
    */
    virtual void WriteCustomization(wxConfigBase* cfg,
                                    wxString path = wxEmptyString);
    
protected:

    /**
        This method is called when a mouse button is clicked inside wxHtmlWindow.
        The default behaviour is to emit a wxHtmlCellEvent and, if the event was
        not processed or skipped, call OnLinkClicked() if the cell contains an
        hypertext link.

        Overloading this method is deprecated; intercept the event instead.

        @param cell
            The cell inside which the mouse was clicked, always a simple
            (i.e. non-container) cell
        @param x
            The logical x coordinate of the click point
        @param y
            The logical y coordinate of the click point
        @param event
            The mouse event containing other information about the click

        @return @true if a link was clicked, @false otherwise.
    */
    virtual bool OnCellClicked(wxHtmlCell* cell, wxCoord x, wxCoord y,
                               const wxMouseEvent& event);

    /**
        This method is called when a mouse moves over an HTML cell.
        Default behaviour is to emit a wxHtmlCellEvent.

        Overloading this method is deprecated; intercept the event instead.

        @param cell
            The cell inside which the mouse is currently, always a simple
            (i.e. non-container) cell
        @param x
            The logical x coordinate of the click point
        @param y
            The logical y coordinate of the click point
    */
    virtual void OnCellMouseHover(wxHtmlCell* cell, wxCoord x, wxCoord y);
};



wxEventType wxEVT_HTML_CELL_CLICKED;
wxEventType wxEVT_HTML_CELL_HOVER;
wxEventType wxEVT_HTML_LINK_CLICKED;


/**
    @class wxHtmlLinkEvent

    This event class is used for the events generated by wxHtmlWindow.

    @beginEventTable{wxHtmlLinkEvent}
    @event{EVT_HTML_LINK_CLICKED(id, func)}
        User clicked on an hyperlink.
    @endEventTable

    @library{wxhtml}
    @category{html}
*/
class wxHtmlLinkEvent : public wxCommandEvent
{
public:
    /**
        The constructor is not normally used by the user code.
    */
    wxHtmlLinkEvent(int id, const wxHtmlLinkInfo& linkinfo);

    /**
        Returns the wxHtmlLinkInfo which contains info about the cell clicked
        and the hyperlink it contains.
    */
    const wxHtmlLinkInfo& GetLinkInfo() const;
};



/**
    @class wxHtmlCellEvent

    This event class is used for the events generated by wxHtmlWindow.

    @beginEventTable{wxHtmlCellEvent}
    @event{EVT_HTML_CELL_HOVER(id, func)}
        User moved the mouse over a wxHtmlCell.
    @event{EVT_HTML_CELL_CLICKED(id, func)}
        User clicked on a wxHtmlCell. When handling this event, remember to use
        wxHtmlCell::SetLinkClicked(true) if the cell contains a link.
    @endEventTable

    @library{wxhtml}
    @category{html}
*/
class wxHtmlCellEvent : public wxCommandEvent
{
public:
    /**
        The constructor is not normally used by the user code.
    */
    wxHtmlCellEvent(wxEventType commandType, int id,
                    wxHtmlCell* cell,
                    const wxPoint& point,
                    const wxMouseEvent& ev);

    /**
        Returns the wxHtmlCellEvent associated with the event.
    */
    wxHtmlCell* GetCell() const;

    /**
        Returns @true if SetLinkClicked(@true) has previously been called;
        @false otherwise.
    */
    bool GetLinkClicked() const;

    /**
        Returns the wxPoint associated with the event.
    */
    wxPoint GetPoint() const;

    /**
        Call this function with @a linkclicked set to @true if the cell which has
        been clicked contained a link or @false otherwise (which is the default).

        With this function the event handler can return info to the wxHtmlWindow
        which sent the event.
    */
    void SetLinkClicked(bool linkclicked);
};