wxHtmlWindow

team bbl


wxHtmlWindow is used by wxWidgets' built-in help system, and it is also a great control to use in your applications whenever you need to display formatted text and graphics, such as reports. It can display a useful subset of HTML, including tables, but not frames. Features include animated GIFs, highlighted links, fonts, background color, nested lists, centering, right-alignment, horizontal rules, character encoding support, and more. It doesn't support style sheets, but you can normally achieve the effects you want by writing or generating the appropriate tags. HTML text is selectable and can be copied to the clipboard or returned to the application as plain text.

Figure 12-4 shows the wxHtmlWindow demo program that you can compile and run in samples/html/test.

Figure 12-4. The wxHtmlWindow demo program


Because wxHtmlWindow is small and fast (unlike a full web browser), you can use it liberally in your application. Figure 12-5 shows an example of wxHtmlWindow in an "About" box.

Figure 12-5. wxHtmlWindow in an About box


The code to create this dialog is shown in Listing 12-1. In this example, the HTML control is sized to fit the HTML that has been loaded, and then the dialog sizers set the dialog to fit around the wxHtmlWindow.

Listing 12-1. Code to Create an HTML About Box
 #include "wx/html/htmlwin.h" void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) {     wxBoxSizer *topsizer;     wxHtmlWindow *html;     wxDialog dlg(this, wxID_ANY, wxString(_("About")));     topsizer = new wxBoxSizer(wxVERTICAL);     html = new wxHtmlWindow(&dlg, wxID_ANY, wxDefaultPosition,             wxSize(380, 160), wxHW_SCROLLBAR_NEVER);     html->SetBorders(0);     html->LoadPage(wxT("data/about.htm"));     // Fit the HTML window to the size of its contents     html->SetSize(html->GetInternalRepresentation()->GetWidth(),                   html->GetInternalRepresentation()->GetHeight());     topsizer->Add(html, 1, wxALL, 10);     topsizer->Add(new wxStaticLine(&dlg, wxID_ANY), 0, wxEXPAND | wxLEFT | wxRIGHT, 10);     wxButton *but = new wxButton(&dlg, wxID_OK, _("OK"));     but->SetDefault();     topsizer->Add(but, 0, wxALL | wxALIGN_RIGHT, 15);     dlg.SetSizer(topsizer);     topsizer->Fit(&dlg);     dlg.ShowModal(); } 

Listing 12-2 shows the HTML that displays as in the sample screenshot.

Listing 12-2. HTML for the About Box Sample
 <html> <body bgcolor="#FFFFFF"> <table cellspacing=3 cellpadding=4 width="100%">   <tr>     <td bgcolor="#101010">     <center>     <font size=+2 color="#FFFFFF"><b><br>wxHTML Library Sample 0.2.0<br></b>     </font>     </center>     </td>   </tr>   <tr>     <td bgcolor="#73A183">     <b><font size=+1>Copyright (C) 1999 Vaclav Slavik</font></b><p>     <font size=-1>       <table cellpadding=0 cellspacing=0 width="100%">         <tr>           <td width="65%">             Vaclav Slavik<p>           </td>           <td valign=top>             <img src="/books/3/138/1/html/2/logo.png">           </td>         </tr>       </table>     <font size=1>     Licenced under wxWindows Library Licence, Version 3.     </font>     </font>     </td>   </tr> </table> </body> </html> 

See also the wxHtmlListBox class as described in the section "wxListBox and wxCheckListBox and wxCheckListBox" in Chapter 4, "Window Basics."

wxHtmlWindow Styles

Table 12-8 lists the styles you can pass to the wxHtmlWindow constructor or Create function. Also refer to the available styles for wxWindow in Table 4-1.

Table 12-8. wxHtmlWindow Window Styles

wxHW_SCROLLBAR_NEVER

Never displays scrollbars.

wxHW_SCROLLBAR_AUTO

Displays scrollbars only if the page size exceeds the window size.

wxHW_NO_SELECTION

Prevents the user from selecting text. Normally, text can be selected.


wxHtmlWindow Member Functions

These are the main member functions for wxHtmlWindow.

GetInternalRepresentation returns the top-level wxHtmlContainerCell object, whose size you can query with GetWidth and GetHeight to get the overall size of the HTML.

LoadFile loads HTML from a file and displays it. LoadPage takes a location, which may be a URL. Examples of locations include:

 http://www.wxwindows.org/front.htm  # A URL file:myapp.zip#zip:html/index.htm   # Gets index.htm from myapp.zip 

SetPage sets the page using an HTML string rather than a file name or location.

OnCellClicked is called when there was a mouse click inside a cell. It takes a wxHtmlCell pointer, x and y coordinates, and a wxMouseEvent reference. The default behavior is to call OnLinkClicked if the cell contains a hyperlink.

OnLinkClicked takes a wxHtmlLinkInfo reference, and its default behavior is to load the linked page with LoadPage. You can override this behavior, for example to show the application's home page in the default browser when the user clicks on the link in your "About" box.

You can also override OnOpeningURL, which is called when a URL is being opened, and OnCellMouseHover, called when the mouse moves over an HTML cell.

ReadCustomization and WriteCustomization are used to preserve fonts and borders, and they take a wxConfig* argument and optional path to use in the configuration object.

You can select text with the functions SelectAll, SelectLine, and SelectWord. SelectionToText returns a string with the plain text in the selection. ToText returns the entire page as plain text.

Call SetBorders to set the space around the HTML. SetFonts enables you to set the faces for normal and fixed fonts, and optionally you can pass an array of seven integers specifying the point sizes of the seven font sizes.

AppendToPage can be used to add HTML to the current page and refresh the window.

You can write a custom wxHtmlFilter to read special files, and you can call AddFilter to register it with wxHtmlWindow. For example, you might write a filter to decrypt an encrypted HTML e-book.

GetOpenedAnchor, GetOpenedPage, and GetOpenedPageTitle return information about the currently loaded page.

wxHtmlWindow has a history mechanism, which you can access with HistoryBack, HistoryForward, HistoryCanBack, HistoryCanForward, and HistoryClear.

Embedding Windows in HTML Pages

You can also add windows, including your own custom controls, to an HTML page, as Figure 12-6 shows. This is done by writing a custom "tag handler," which parses the HTML fragment for a specified tag name and creates and inserts a window.

Figure 12-6. Embedded HTML widget demo


This is part of the HTML page shown in the screenshot:

 <mybind name="(small one)" x=150 y=30> <hr> <mybind name="a widget with floating width" float=y x="50" y=50> <hr> Here you can find multiple widgets at the same line:<br>  here <mybind name="widget_1" x="100" y=30> ...and here: <mybind name="widget_2" x="150" y=30> 

The code to implement the custom HTML tag mybind looks like this:

 #include "wx/html/m_templ.h" TAG_HANDLER_BEGIN(MYBIND, "MYBIND") TAG_HANDLER_PROC(tag) {     wxWindow *wnd;     int ax, ay;     int fl = 0;     tag.ScanParam(wxT("X"), wxT("%i"), &ax);     tag.ScanParam(wxT("Y"), wxT("%i"), &ay);     if (tag.HasParam(wxT("FLOAT"))) fl = ax;     wnd = new wxTextCtrl(m_WParser->GetWindow(), wxID_ANY, tag.GetParam(wxT("NAME")),         wxPoint(0,0), wxSize(ax, ay), wxTE_MULTILINE);     wnd->Show(true);     m_WParser->GetContainer()->InsertCell(new wxHtmlWidgetCell(wnd, fl));     return false; } TAG_HANDLER_END(MYBIND) TAGS_MODULE_BEGIN(MyBind)     TAGS_MODULE_ADD(MYBIND) TAGS_MODULE_END(MyBind) 

This technique might be useful if you wanted to create a whole user interface around wxHtmlWindow, possibly using scripts to generate the HTML and respond to input, like a web form. Another example is a registration dialog, with text controls for the user to enter his or her details and a Register button that sends the details to your organization. Or you might want to generate a report with items that can be selected and viewed in more detail by toggling a check box next to each item.

For more details of how to write your own tag handlers, see samples/ html/widget and the wxWidgets reference manual.

HTML Printing

It's likely that if you use wxHtmlWindow extensively within your application, you'll want to print HTML files, too. wxWidgets provides a class to make this easy, appropriately called wxHtmlEasyPrinting. Create one instance of this class for the lifetime of your application and then call PreviewFile and PrintFile with the name of the local HTML file to print. You can also call PageSetup to show the page setup dialog, and you can retrieve the print and page setup settings using GetPrintData and GetPageSetupData. Customize the header and footer of the printout with SetHeader and SetFooter, which can contain the keywords @PAGENUM@ (the current page) and @PAGESCNT@ (the total number of pages).

This fragment from the sample in samples/html/printing demonstrates the basic principles and shows how to change the font sizes.

 #include "wx/html/htmlwin.h" #include "wx/html/htmprint.h" MyFrame::MyFrame(const wxString& title,                  const wxPoint& pos, const wxSize& size)         : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) {     ...     m_Name = wxT("testfile.htm");     m_Prn = new wxHtmlEasyPrinting(_("Easy Printing Demo"), this);     m_Prn->SetHeader(m_Name + wxT("(@PAGENUM@/@PAGESCNT@)<hr>"),                      wxPAGE_ALL); } MyFrame::~MyFrame() {     delete m_Prn; } void MyFrame::OnPageSetup(wxCommandEvent& event) {     m_Prn->PageSetup(); } void MyFrame::OnPrint(wxCommandEvent& event) {     m_Prn->PrintFile(m_Name); } void MyFrame::OnPreview(wxCommandEvent& event) {     m_Prn->PreviewFile(m_Name); } void MyFrame::OnPrintSmall(wxCommandEvent& event) {     int fontsizes[] = { 4, 6, 8, 10, 12, 20, 24 };      m_Prn->SetFonts(wxEmptyString, wxEmptyString, fontsizes); } void MyFrame::OnPrintNormal(wxCommandEvent& event) {     m_Prn->SetFonts(wxEmptyString, wxEmptyString, 0); } void MyFrame::OnPrintHuge(wxCommandEvent& event) {     int fontsizes[] = { 20, 26, 28, 30, 32, 40, 44 };      m_Prn->SetFonts(wxEmptyString, wxEmptyString, fontsizes); } 

For examples of all the preceding wxHTML topics, see the samples under samples/html in your wxWidgets distribution.

    team bbl



    Cross-Platform GUI Programming with wxWidgets
    Cross-Platform GUI Programming with wxWidgets
    ISBN: 0131473816
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net