1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlHelpFrame
4 // Notes: Based on htmlhelp.cpp, implementing a monolithic
5 // HTML Help controller class, by Vaclav Slavik
6 // Author: Harm van der Heijden and Vaclav Slavik
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h"
18 #include "wx/wxprec.h"
31 #include "wx/html/helpfrm.h"
32 #include "wx/notebook.h"
33 #include "wx/imaglist.h"
34 #include "wx/treectrl.h"
35 #include "wx/tokenzr.h"
36 #include "wx/wfstream.h"
37 #include "wx/html/htmlwin.h"
38 #include "wx/busyinfo.h"
39 #include "wx/progdlg.h"
40 #include "wx/toolbar.h"
45 #include "bitmaps/panel.xpm"
46 #include "bitmaps/back.xpm"
47 #include "bitmaps/forward.xpm"
48 #include "bitmaps/book.xpm"
49 #include "bitmaps/folder.xpm"
50 #include "bitmaps/page.xpm"
51 #include "bitmaps/help.xpm"
52 #include "bitmaps/helproot.xpm"
55 #include "wx/stream.h"
57 // number of times that the contents/index creation progress dialog
59 #define PROGRESS_STEP 40
61 //--------------------------------------------------------------------------
62 // wxHtmlHelpTreeItemData
63 //--------------------------------------------------------------------------
65 class wxHtmlHelpTreeItemData
: public wxTreeItemData
71 wxHtmlHelpTreeItemData(wxHtmlContentsItem
*it
) : wxTreeItemData()
73 m_Page
= it
-> m_Book
-> GetBasePath() + it
-> m_Page
;
75 const wxString
& GetPage() { return m_Page
; }
78 //---------------------------------------------------------------------------
80 //---------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpFrame
, wxFrame
)
84 wxHtmlHelpFrame::wxHtmlHelpFrame(wxWindow
* parent
, wxWindowID id
, const wxString
& title
,
85 int style
, wxHtmlHelpData
* data
)
88 Create(parent
, id
, title
, style
);
91 void wxHtmlHelpFrame::Init(wxHtmlHelpData
* data
)
95 m_DataCreated
= FALSE
;
97 m_Data
= new wxHtmlHelpData();
101 m_ContentsImageList
= new wxImageList(16, 16);
102 m_ContentsImageList
-> Add(wxICON(book
));
103 m_ContentsImageList
-> Add(wxICON(folder
));
104 m_ContentsImageList
-> Add(wxICON(page
));
105 m_ContentsImageList
-> Add(wxICON(helproot
));
107 m_ContentsBox
= NULL
;
110 m_SearchButton
= NULL
;
112 m_SearchChoice
= NULL
;
117 m_ConfigRoot
= wxEmptyString
;
119 m_Cfg
.x
= m_Cfg
.y
= 0;
123 m_Cfg
.navig_on
= TRUE
;
126 // Create: builds the GUI components.
127 // with the style flag it's possible to toggle the toolbar, contents, index and search
129 // m_HtmlWin will *always* be created, but it's important to realize that
130 // m_ContentsBox, m_IndexBox, m_SearchList, m_SearchButton, m_SearchText and
131 // m_SearchButton may be NULL.
132 // moreover, if no contents, index or searchpage is needed, m_Splitter and
133 // m_NavigPan will be NULL too (with m_HtmlWin directly connected to the frame)
135 bool wxHtmlHelpFrame::Create(wxWindow
* parent
, wxWindowID id
, const wxString
& title
,
138 // Do the config in two steps. We read the HtmlWindow customization after we
139 // create the window.
141 ReadCustomization(m_Config
, m_ConfigRoot
);
143 wxFrame::Create(parent
, id
, _("Help"), wxPoint(m_Cfg
.x
, m_Cfg
.y
), wxSize(m_Cfg
.w
, m_Cfg
.h
));
145 #if defined(__WXMSW__) || (__WXPM__)
146 wxIcon
frameIcon("wxhelp", wxBITMAP_TYPE_ICO_RESOURCE
, 32, 32);
148 wxIcon
frameIcon(help_xpm
);
153 GetPosition(&m_Cfg
.x
, &m_Cfg
.y
);
155 SetIcon(wxICON(help
));
157 int notebook_page
= 0;
162 if (style
& wxHF_TOOLBAR
) {
163 wxToolBar
*toolBar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
| /*wxTB_FLAT | */
165 toolBar
-> SetMargins(2, 2);
166 wxBitmap
* toolBarBitmaps
[3];
168 #if defined(__WXMSW__) || (__WXPM__)
169 toolBarBitmaps
[0] = new wxBitmap("panel");
170 toolBarBitmaps
[1] = new wxBitmap("back");
171 toolBarBitmaps
[2] = new wxBitmap("forward");
174 toolBarBitmaps
[0] = new wxBitmap(panel_xpm
);
175 toolBarBitmaps
[1] = new wxBitmap(back_xpm
);
176 toolBarBitmaps
[2] = new wxBitmap(forward_xpm
);
182 toolBar
-> AddTool(wxID_HTML_PANEL
, *(toolBarBitmaps
[0]), wxNullBitmap
,
183 FALSE
, currentX
, -1, (wxObject
*) NULL
,
184 _("Show/hide navigation panel"));
185 currentX
+= width
+ 5;
186 toolBar
-> AddSeparator();
187 toolBar
-> AddTool(wxID_HTML_BACK
, *(toolBarBitmaps
[1]), wxNullBitmap
,
188 FALSE
, currentX
, -1, (wxObject
*) NULL
,
189 _("Go back to the previous HTML page"));
190 currentX
+= width
+ 5;
191 toolBar
-> AddTool(wxID_HTML_FORWARD
, *(toolBarBitmaps
[2]), wxNullBitmap
,
192 FALSE
, currentX
, -1, (wxObject
*) NULL
,
193 _("Go forward to the next HTML page"));
194 currentX
+= width
+ 5;
196 toolBar
-> Realize();
198 // Can delete the bitmaps since they're reference counted
199 for (int i
= 0; i
< 3; i
++)
200 delete toolBarBitmaps
[i
];
203 if (style
& (wxHF_CONTENTS
| wxHF_INDEX
| wxHF_SEARCH
)) {
204 // traditional help controller; splitter window with html page on the
205 // right and a notebook containing various pages on the left
206 m_Splitter
= new wxSplitterWindow(this);
208 m_HtmlWin
= new wxHtmlWindow(m_Splitter
);
209 m_NavigPan
= new wxNotebook(m_Splitter
, wxID_HTML_NOTEBOOK
,
210 wxDefaultPosition
, wxDefaultSize
);
211 } else { // only html window, no notebook with index,contents etc
212 m_HtmlWin
= new wxHtmlWindow(this);
215 m_HtmlWin
-> SetRelatedFrame(this, m_TitleFormat
);
216 m_HtmlWin
-> SetRelatedStatusBar(0);
218 m_HtmlWin
-> ReadCustomization(m_Config
, m_ConfigRoot
);
220 // contents tree panel?
221 if (style
& wxHF_CONTENTS
) {
222 m_ContentsBox
= new wxTreeCtrl(m_NavigPan
, wxID_HTML_TREECTRL
,
223 wxDefaultPosition
, wxDefaultSize
,
224 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
225 m_ContentsBox
-> SetImageList(m_ContentsImageList
);
226 m_NavigPan
-> AddPage(m_ContentsBox
, _("Contents"));
227 m_ContentsPage
= notebook_page
++;
230 // index listbox panel?
231 if (style
& wxHF_INDEX
) {
232 wxWindow
*dummy
= new wxPanel(m_NavigPan
, wxID_HTML_INDEXPAGE
);
233 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
234 b1
-> top
.SameAs (dummy
, wxTop
, 0);
235 b1
-> left
.SameAs (dummy
, wxLeft
, 0);
236 b1
-> width
.PercentOf (dummy
, wxWidth
, 100);
237 b1
-> bottom
.SameAs (dummy
, wxBottom
, 0);
238 m_IndexBox
= new wxListBox(dummy
, wxID_HTML_INDEXLIST
, wxDefaultPosition
,
239 wxDefaultSize
, 0, NULL
, wxLB_SINGLE
| wxLB_ALWAYS_SB
);
240 m_IndexBox
-> SetConstraints(b1
);
241 dummy
-> SetAutoLayout(TRUE
);
242 m_NavigPan
-> AddPage(dummy
, _("Index"));
243 m_IndexPage
= notebook_page
++;
246 // search list panel?
247 if (style
& wxHF_SEARCH
) {
248 wxWindow
*dummy
= new wxPanel(m_NavigPan
, wxID_HTML_SEARCHPAGE
);
250 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
251 m_SearchText
= new wxTextCtrl(dummy
, wxID_HTML_SEARCHTEXT
);
252 b1
-> top
.SameAs (dummy
, wxTop
, 10);
253 b1
-> left
.SameAs (dummy
, wxLeft
, 10);
254 b1
-> right
.SameAs (dummy
, wxRight
, 10);
256 m_SearchText
-> SetConstraints(b1
);
258 wxLayoutConstraints
*b2
= new wxLayoutConstraints
;
259 m_SearchButton
= new wxButton(dummy
, wxID_HTML_SEARCHBUTTON
, _("Search"));
260 b2
-> top
.Below (m_SearchText
, 10);
261 b2
-> left
.SameAs (dummy
, wxLeft
, 10);
264 m_SearchButton
-> SetConstraints(b2
);
266 wxLayoutConstraints
*b3
= new wxLayoutConstraints
;
267 m_SearchList
= new wxListBox(dummy
, wxID_HTML_SEARCHLIST
, wxDefaultPosition
, wxDefaultSize
, 0);
268 b3
-> top
.Below (m_SearchButton
, 10);
269 b3
-> left
.SameAs (dummy
, wxLeft
, 0);
270 b3
-> right
.SameAs (dummy
, wxRight
, 0);
271 b3
-> bottom
.SameAs (dummy
, wxBottom
, 0);
272 m_SearchList
-> SetConstraints(b3
);
274 wxLayoutConstraints
*b4
= new wxLayoutConstraints
;
275 m_SearchChoice
= new wxChoice(dummy
, wxID_HTML_SEARCHCHOICE
, wxDefaultPosition
,
277 b4
-> top
.Below (m_SearchText
, 10);
278 b4
-> left
.SameAs (m_SearchButton
, wxRight
, 10);
279 b4
-> right
.SameAs (dummy
, wxRight
, 10);
281 m_SearchChoice
-> SetConstraints(b4
);
283 dummy
-> SetAutoLayout(TRUE
);
285 m_NavigPan
-> AddPage(dummy
, _("Search"));
286 m_SearchPage
= notebook_page
++;
288 m_HtmlWin
-> Show(TRUE
);
293 if (m_NavigPan
&& m_Splitter
) {
294 m_Splitter
-> SetMinimumPaneSize(20);
295 if (m_Cfg
.navig_on
) {
296 m_NavigPan
-> Show(TRUE
);
297 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
300 m_NavigPan
-> Show(FALSE
);
301 m_Splitter
-> Initialize(m_HtmlWin
);
307 wxHtmlHelpFrame::~wxHtmlHelpFrame()
309 delete m_ContentsImageList
;
314 bool wxHtmlHelpFrame::Display(const wxString
& x
)
316 wxString url
= m_Data
->FindPageByName(x
);
317 if (! url
.IsEmpty()) {
318 m_HtmlWin
->LoadPage(url
);
324 bool wxHtmlHelpFrame::Display(const int id
)
326 wxString url
= m_Data
->FindPageById(id
);
327 if (! url
.IsEmpty()) {
328 m_HtmlWin
->LoadPage(url
);
336 bool wxHtmlHelpFrame::DisplayContents()
340 if (!m_Splitter
-> IsSplit()) {
341 m_NavigPan
-> Show(TRUE
);
342 m_HtmlWin
-> Show(TRUE
);
343 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
344 m_Cfg
.navig_on
= TRUE
;
346 m_NavigPan
-> SetSelection(0);
352 bool wxHtmlHelpFrame::DisplayIndex()
356 if (!m_Splitter
-> IsSplit()) {
357 m_NavigPan
-> Show(TRUE
);
358 m_HtmlWin
-> Show(TRUE
);
359 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
361 m_NavigPan
-> SetSelection(1);
365 bool wxHtmlHelpFrame::KeywordSearch(const wxString
& keyword
)
367 if (! (m_SearchList
&& m_SearchButton
&& m_SearchText
&& m_SearchChoice
))
372 wxString book
= wxEmptyString
;
374 if (!m_Splitter
-> IsSplit()) {
375 m_NavigPan
-> Show(TRUE
);
376 m_HtmlWin
-> Show(TRUE
);
377 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
379 m_NavigPan
-> SetSelection(m_SearchPage
);
380 m_SearchList
-> Clear();
381 m_SearchText
-> SetValue(keyword
);
382 m_SearchButton
-> Enable(FALSE
);
384 if (m_SearchChoice
->GetSelection() != 0)
385 book
= m_SearchChoice
->GetStringSelection();
387 wxHtmlSearchStatus
status(m_Data
, keyword
, book
);
389 wxProgressDialog
progress(_("Searching..."), _("No matching page found yet"),
390 status
.GetMaxIndex(), this,
391 wxPD_APP_MODAL
| wxPD_CAN_ABORT
| wxPD_AUTO_HIDE
);
393 while (status
.IsActive()) {
394 if (progress
.Update(status
.GetCurIndex()) == FALSE
)
396 if (status
.Search()) {
397 foundstr
.Printf(_("Found %i matches"), ++foundcnt
);
398 progress
.Update(status
.GetCurIndex(), foundstr
);
399 m_SearchList
-> Append(status
.GetName(), status
.GetContentsItem());
404 m_SearchButton
-> Enable(TRUE
);
405 m_SearchText
-> SetSelection(0, keyword
.Length());
406 m_SearchText
-> SetFocus();
408 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_SearchList
-> GetClientData(0);
409 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
411 return (foundcnt
> 0);
416 void wxHtmlHelpFrame::CreateContents(bool show_progress
)
421 wxProgressDialog
*progress
;
424 m_ContentsBox
->Clear();
426 int cnt
= m_Data
->GetContentsCnt();
427 int div
= (cnt
/ PROGRESS_STEP
) + 1;
430 wxHtmlContentsItem
*it
= m_Data
->GetContents();
433 progress
= new wxProgressDialog(_("Building contents tree..."), wxEmptyString
,
434 cnt
, this, wxPD_APP_MODAL
| wxPD_CAN_ABORT
|
437 wxTreeItemId roots
[MAX_ROOTS
];
438 bool imaged
[MAX_ROOTS
];
440 m_ContentsBox
-> DeleteAllItems();
441 roots
[0] = m_ContentsBox
-> AddRoot(_("(Help)"));
442 m_ContentsBox
-> SetItemImage(roots
[0], IMG_RootFolder
);
443 m_ContentsBox
-> SetItemSelectedImage(roots
[0], IMG_RootFolder
);
446 for (i
= 0; i
< cnt
; i
++, it
++) {
447 if (show_progress
&& ((i
% div
) == 0)) {
448 proginfo
.Printf(_("Added %d/%d items"), i
, cnt
);
449 if (! progress
->Update(i
, proginfo
))
453 roots
[it
-> m_Level
+ 1] = m_ContentsBox
-> AppendItem(
454 roots
[it
-> m_Level
], it
-> m_Name
, IMG_Page
, -1,
455 new wxHtmlHelpTreeItemData(it
));
457 if (it
-> m_Level
== 0) {
458 m_ContentsBox
-> SetItemBold(roots
[1], TRUE
);
459 m_ContentsBox
-> SetItemImage(roots
[1], IMG_Book
);
460 m_ContentsBox
-> SetItemSelectedImage(roots
[1], IMG_Book
);
462 } else imaged
[it
-> m_Level
+ 1] = FALSE
;
464 if (!imaged
[it
-> m_Level
]) {
465 m_ContentsBox
-> SetItemImage(roots
[it
-> m_Level
], IMG_Folder
);
466 m_ContentsBox
-> SetItemSelectedImage(roots
[it
-> m_Level
], IMG_Folder
);
467 imaged
[it
-> m_Level
] = TRUE
;
472 m_ContentsBox
-> Expand(roots
[0]);
476 void wxHtmlHelpFrame::CreateIndex(bool show_progress
)
481 wxProgressDialog
*progress
;
486 int cnt
= m_Data
->GetIndexCnt();
487 int div
= (cnt
/ PROGRESS_STEP
) + 1;
489 wxHtmlContentsItem
* index
= m_Data
->GetIndex();
492 progress
= new wxProgressDialog(_("Building index list..."), wxEmptyString
,
493 cnt
, this, wxPD_APP_MODAL
| wxPD_CAN_ABORT
|
495 for (int i
= 0; i
< cnt
; i
++) {
496 if (show_progress
&& ((i
% div
) == 0)) {
497 proginfo
.Printf(_("Added %d/%d items"), i
, cnt
);
498 if (! progress
->Update(i
, proginfo
))
502 m_IndexBox
-> Append(index
[i
].m_Name
, (char*)(index
+ i
));
509 void wxHtmlHelpFrame::CreateSearch()
511 if (! (m_SearchList
&& m_SearchChoice
))
513 m_SearchList
-> Clear();
514 m_SearchChoice
-> Clear();
515 m_SearchChoice
-> Append(_("all books"));
516 const wxHtmlBookRecArray
& bookrec
= m_Data
->GetBookRecArray();
517 int i
, cnt
= bookrec
.GetCount();
518 for (i
= 0; i
< cnt
; i
++)
519 m_SearchChoice
->Append(bookrec
[i
].GetTitle());
520 m_SearchChoice
->SetSelection(0);
524 void wxHtmlHelpFrame::RefreshLists(bool show_progress
)
526 CreateContents(show_progress
);
527 CreateIndex(show_progress
);
531 void wxHtmlHelpFrame::ReadCustomization(wxConfigBase
*cfg
, const wxString
& path
)
536 if (path
!= wxEmptyString
) {
537 oldpath
= cfg
-> GetPath();
538 cfg
-> SetPath(path
);
541 m_Cfg
.navig_on
= cfg
-> Read("hcNavigPanel", m_Cfg
.navig_on
) != 0;
542 m_Cfg
.sashpos
= cfg
-> Read("hcSashPos", m_Cfg
.sashpos
);
543 m_Cfg
.x
= cfg
-> Read("hcX", m_Cfg
.x
);
544 m_Cfg
.y
= cfg
-> Read("hcY", m_Cfg
.y
);
545 m_Cfg
.w
= cfg
-> Read("hcW", m_Cfg
.w
);
546 m_Cfg
.h
= cfg
-> Read("hcH", m_Cfg
.h
);
549 m_HtmlWin
->ReadCustomization(cfg
, path
);
551 if (path
!= wxEmptyString
)
552 cfg
-> SetPath(oldpath
);
555 void wxHtmlHelpFrame::WriteCustomization(wxConfigBase
*cfg
, const wxString
& path
)
560 if (path
!= wxEmptyString
) {
561 oldpath
= cfg
-> GetPath();
562 cfg
-> SetPath(path
);
565 cfg
-> Write("hcNavigPanel", m_Cfg
.navig_on
);
566 cfg
-> Write("hcSashPos", (long)m_Cfg
.sashpos
);
567 cfg
-> Write("hcX", (long)m_Cfg
.x
);
568 cfg
-> Write("hcY", (long)m_Cfg
.y
);
569 cfg
-> Write("hcW", (long)m_Cfg
.w
);
570 cfg
-> Write("hcH", (long)m_Cfg
.h
);
573 m_HtmlWin
->WriteCustomization(cfg
, path
);
575 if (path
!= wxEmptyString
)
576 cfg
-> SetPath(oldpath
);
585 void wxHtmlHelpFrame::OnToolbar(wxCommandEvent
& event
)
587 switch (event
.GetId()) {
588 case wxID_HTML_BACK
:
589 m_HtmlWin
-> HistoryBack();
591 case wxID_HTML_FORWARD
:
592 m_HtmlWin
-> HistoryForward();
594 case wxID_HTML_PANEL
:
595 if (! (m_Splitter
&& m_NavigPan
))
597 if (m_Splitter
-> IsSplit()) {
598 m_Cfg
.sashpos
= m_Splitter
-> GetSashPosition();
599 m_Splitter
-> Unsplit(m_NavigPan
);
600 m_Cfg
.navig_on
= FALSE
;
602 m_NavigPan
-> Show(TRUE
);
603 m_HtmlWin
-> Show(TRUE
);
604 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
605 m_Cfg
.navig_on
= TRUE
;
613 void wxHtmlHelpFrame::OnContentsSel(wxTreeEvent
& event
)
615 wxHtmlHelpTreeItemData
*pg
;
617 pg
= (wxHtmlHelpTreeItemData
*) m_ContentsBox
-> GetItemData(event
.GetItem());
618 if (pg
) m_HtmlWin
-> LoadPage(pg
-> GetPage());
623 void wxHtmlHelpFrame::OnIndexSel(wxCommandEvent
& WXUNUSED(event
))
625 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_IndexBox
-> GetClientData(m_IndexBox
-> GetSelection());
626 m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
631 void wxHtmlHelpFrame::OnSearchSel(wxCommandEvent
& WXUNUSED(event
))
633 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_SearchList
-> GetClientData(m_SearchList
-> GetSelection());
634 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
637 void wxHtmlHelpFrame::OnSearch(wxCommandEvent
& WXUNUSED(event
))
639 wxString sr
= m_SearchText
-> GetLineText(0);
641 if (sr
!= wxEmptyString
) KeywordSearch(sr
);
644 void wxHtmlHelpFrame::OnCloseWindow(wxCloseEvent
& evt
)
646 GetSize(&m_Cfg
.w
, &m_Cfg
.h
);
647 GetPosition(&m_Cfg
.x
, &m_Cfg
.y
);
649 if (m_Splitter
&& m_Cfg
.navig_on
) m_Cfg
.sashpos
= m_Splitter
-> GetSashPosition();
652 WriteCustomization(m_Config
, m_ConfigRoot
);
657 BEGIN_EVENT_TABLE(wxHtmlHelpFrame
, wxFrame
)
658 EVT_TOOL_RANGE(wxID_HTML_PANEL
, wxID_HTML_FORWARD
, wxHtmlHelpFrame::OnToolbar
)
659 EVT_TREE_SEL_CHANGED(wxID_HTML_TREECTRL
, wxHtmlHelpFrame::OnContentsSel
)
660 EVT_LISTBOX(wxID_HTML_INDEXLIST
, wxHtmlHelpFrame::OnIndexSel
)
661 EVT_LISTBOX(wxID_HTML_SEARCHLIST
, wxHtmlHelpFrame::OnSearchSel
)
662 EVT_BUTTON(wxID_HTML_SEARCHBUTTON
, wxHtmlHelpFrame::OnSearch
)
663 EVT_TEXT_ENTER(wxID_HTML_SEARCHTEXT
, wxHtmlHelpFrame::OnSearch
)
664 EVT_CLOSE(wxHtmlHelpFrame::OnCloseWindow
)