]>
git.saurik.com Git - wxWidgets.git/blob - src/html/helpfrm.cpp
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".
17 #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"
53 #include "wx/stream.h"
55 // number of times that the contents/index creation progress dialog
57 #define PROGRESS_STEP 40
59 //--------------------------------------------------------------------------
60 // wxHtmlHelpTreeItemData
61 //--------------------------------------------------------------------------
63 class wxHtmlHelpTreeItemData
: public wxTreeItemData
69 wxHtmlHelpTreeItemData(wxHtmlContentsItem
*it
) : wxTreeItemData()
71 m_Page
= it
-> m_Book
-> GetBasePath() + it
-> m_Page
;
73 const wxString
& GetPage() { return m_Page
; }
76 //---------------------------------------------------------------------------
78 //---------------------------------------------------------------------------
80 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpFrame
, wxFrame
)
82 wxHtmlHelpFrame::wxHtmlHelpFrame(wxWindow
* parent
, wxWindowID id
, const wxString
& title
,
83 int style
, wxHtmlHelpData
* data
)
86 Create(parent
, id
, title
, style
);
89 void wxHtmlHelpFrame::Init(wxHtmlHelpData
* data
)
93 m_DataCreated
= FALSE
;
95 m_Data
= new wxHtmlHelpData();
99 m_ContentsImageList
= new wxImageList(16, 16);
100 m_ContentsImageList
-> Add(wxICON(book
));
101 m_ContentsImageList
-> Add(wxICON(folder
));
102 m_ContentsImageList
-> Add(wxICON(page
));
104 m_ContentsBox
= NULL
;
107 m_SearchButton
= NULL
;
109 m_SearchChoice
= NULL
;
114 m_ConfigRoot
= wxEmptyString
;
116 m_Cfg
.x
= m_Cfg
.y
= 0;
120 m_Cfg
.navig_on
= TRUE
;
123 // Create: builds the GUI components.
124 // with the style flag it's possible to toggle the toolbar, contents, index and search
126 // m_HtmlWin will *always* be created, but it's important to realize that
127 // m_ContentsBox, m_IndexBox, m_SearchList, m_SearchButton, m_SearchText and
128 // m_SearchButton may be NULL.
129 // moreover, if no contents, index or searchpage is needed, m_Splitter and
130 // m_NavigPan will be NULL too (with m_HtmlWin directly connected to the frame)
132 bool wxHtmlHelpFrame::Create(wxWindow
* parent
, wxWindowID id
, const wxString
& title
,
135 // Do the config in two steps. We read the HtmlWindow customization after we
136 // create the window.
138 ReadCustomization(m_Config
, m_ConfigRoot
);
140 wxFrame::Create(parent
, id
, _("Help"), wxPoint(m_Cfg
.x
, m_Cfg
.y
), wxSize(m_Cfg
.w
, m_Cfg
.h
));
142 int notebook_page
= 0;
147 if (style
& wxHF_TOOLBAR
) {
148 wxToolBar
*toolBar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
| /*wxTB_FLAT | */
150 toolBar
-> SetMargins(2, 2);
151 wxBitmap
* toolBarBitmaps
[3];
154 toolBarBitmaps
[0] = new wxBitmap("panel");
155 toolBarBitmaps
[1] = new wxBitmap("back");
156 toolBarBitmaps
[2] = new wxBitmap("forward");
159 toolBarBitmaps
[0] = new wxBitmap(panel_xpm
);
160 toolBarBitmaps
[1] = new wxBitmap(back_xpm
);
161 toolBarBitmaps
[2] = new wxBitmap(forward_xpm
);
167 toolBar
-> AddTool(wxID_HTML_PANEL
, *(toolBarBitmaps
[0]), wxNullBitmap
,
168 FALSE
, currentX
, -1, (wxObject
*) NULL
,
169 _("Show/hide navigation panel"));
170 currentX
+= width
+ 5;
171 toolBar
-> AddSeparator();
172 toolBar
-> AddTool(wxID_HTML_BACK
, *(toolBarBitmaps
[1]), wxNullBitmap
,
173 FALSE
, currentX
, -1, (wxObject
*) NULL
,
174 _("Go back to the previous HTML page"));
175 currentX
+= width
+ 5;
176 toolBar
-> AddTool(wxID_HTML_FORWARD
, *(toolBarBitmaps
[2]), wxNullBitmap
,
177 FALSE
, currentX
, -1, (wxObject
*) NULL
,
178 _("Go forward to the next HTML page"));
179 currentX
+= width
+ 5;
181 toolBar
-> Realize();
183 // Can delete the bitmaps since they're reference counted
184 for (int i
= 0; i
< 3; i
++)
185 delete toolBarBitmaps
[i
];
188 if (style
& (wxHF_CONTENTS
| wxHF_INDEX
| wxHF_SEARCH
)) {
189 // traditional help controller; splitter window with html page on the
190 // right and a notebook containing various pages on the left
191 m_Splitter
= new wxSplitterWindow(this);
193 m_HtmlWin
= new wxHtmlWindow(m_Splitter
);
194 m_NavigPan
= new wxNotebook(m_Splitter
, wxID_HTML_NOTEBOOK
,
195 wxDefaultPosition
, wxDefaultSize
);
196 } else { // only html window, no notebook with index,contents etc
197 m_HtmlWin
= new wxHtmlWindow(this);
200 m_HtmlWin
-> SetRelatedFrame(this, m_TitleFormat
);
201 m_HtmlWin
-> SetRelatedStatusBar(0);
203 m_HtmlWin
-> ReadCustomization(m_Config
, m_ConfigRoot
);
205 // contents tree panel?
206 if (style
& wxHF_CONTENTS
) {
207 m_ContentsBox
= new wxTreeCtrl(m_NavigPan
, wxID_HTML_TREECTRL
,
208 wxDefaultPosition
, wxDefaultSize
,
209 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
210 m_ContentsBox
-> SetImageList(m_ContentsImageList
);
211 m_NavigPan
-> AddPage(m_ContentsBox
, _("Contents"));
212 m_ContentsPage
= notebook_page
++;
215 // index listbox panel?
216 if (style
& wxHF_INDEX
) {
217 wxWindow
*dummy
= new wxPanel(m_NavigPan
, wxID_HTML_INDEXPAGE
);
218 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
219 b1
-> top
.SameAs (dummy
, wxTop
, 0);
220 b1
-> left
.SameAs (dummy
, wxLeft
, 0);
221 b1
-> width
.PercentOf (dummy
, wxWidth
, 100);
222 b1
-> bottom
.SameAs (dummy
, wxBottom
, 0);
223 m_IndexBox
= new wxListBox(dummy
, wxID_HTML_INDEXLIST
, wxDefaultPosition
,
224 wxDefaultSize
, 0, NULL
, wxLB_SINGLE
| wxLB_ALWAYS_SB
);
225 m_IndexBox
-> SetConstraints(b1
);
226 dummy
-> SetAutoLayout(TRUE
);
227 m_NavigPan
-> AddPage(dummy
, _("Index"));
228 m_IndexPage
= notebook_page
++;
231 // search list panel?
232 if (style
& wxHF_SEARCH
) {
233 wxWindow
*dummy
= new wxPanel(m_NavigPan
, wxID_HTML_SEARCHPAGE
);
235 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
236 m_SearchText
= new wxTextCtrl(dummy
, wxID_HTML_SEARCHTEXT
);
237 b1
-> top
.SameAs (dummy
, wxTop
, 10);
238 b1
-> left
.SameAs (dummy
, wxLeft
, 10);
239 b1
-> right
.SameAs (dummy
, wxRight
, 10);
241 m_SearchText
-> SetConstraints(b1
);
243 wxLayoutConstraints
*b2
= new wxLayoutConstraints
;
244 m_SearchButton
= new wxButton(dummy
, wxID_HTML_SEARCHBUTTON
, _("Search"));
245 b2
-> top
.Below (m_SearchText
, 10);
246 b2
-> left
.SameAs (dummy
, wxLeft
, 10);
249 m_SearchButton
-> SetConstraints(b2
);
251 wxLayoutConstraints
*b3
= new wxLayoutConstraints
;
252 m_SearchList
= new wxListBox(dummy
, wxID_HTML_SEARCHLIST
, wxDefaultPosition
, wxDefaultSize
, 0);
253 b3
-> top
.Below (m_SearchButton
, 10);
254 b3
-> left
.SameAs (dummy
, wxLeft
, 0);
255 b3
-> right
.SameAs (dummy
, wxRight
, 0);
256 b3
-> bottom
.SameAs (dummy
, wxBottom
, 0);
257 m_SearchList
-> SetConstraints(b3
);
259 wxLayoutConstraints
*b4
= new wxLayoutConstraints
;
260 m_SearchChoice
= new wxChoice(dummy
, wxID_HTML_SEARCHCHOICE
, wxDefaultPosition
,
262 b4
-> top
.Below (m_SearchText
, 10);
263 b4
-> left
.SameAs (m_SearchButton
, wxRight
, 10);
264 b4
-> right
.SameAs (dummy
, wxRight
, 10);
266 m_SearchChoice
-> SetConstraints(b4
);
268 dummy
-> SetAutoLayout(TRUE
);
270 m_NavigPan
-> AddPage(dummy
, _("Search"));
271 m_SearchPage
= notebook_page
++;
277 if (m_NavigPan
&& m_Splitter
) {
278 m_NavigPan
-> Show(TRUE
);
279 m_Splitter
-> SetMinimumPaneSize(20);
281 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
283 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
284 m_Splitter
-> Unsplit();
287 m_HtmlWin
-> Show(TRUE
);
291 wxHtmlHelpFrame::~wxHtmlHelpFrame()
293 delete m_ContentsImageList
;
298 bool wxHtmlHelpFrame::Display(const wxString
& x
)
300 wxString url
= m_Data
->FindPageByName(x
);
301 if (! url
.IsEmpty()) {
302 m_HtmlWin
->LoadPage(url
);
308 bool wxHtmlHelpFrame::Display(const int id
)
310 wxString url
= m_Data
->FindPageById(id
);
311 if (! url
.IsEmpty()) {
312 m_HtmlWin
->LoadPage(url
);
320 bool wxHtmlHelpFrame::DisplayContents()
324 if (!m_Splitter
-> IsSplit()) {
325 m_NavigPan
-> Show(TRUE
);
326 m_HtmlWin
-> Show(TRUE
);
327 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
329 m_NavigPan
-> SetSelection(0);
335 bool wxHtmlHelpFrame::DisplayIndex()
339 if (!m_Splitter
-> IsSplit()) {
340 m_NavigPan
-> Show(TRUE
);
341 m_HtmlWin
-> Show(TRUE
);
342 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
344 m_NavigPan
-> SetSelection(1);
348 bool wxHtmlHelpFrame::KeywordSearch(const wxString
& keyword
)
350 if (! (m_SearchList
&& m_SearchButton
&& m_SearchText
&& m_SearchChoice
))
355 wxString book
= wxEmptyString
;
357 if (!m_Splitter
-> IsSplit()) {
358 m_NavigPan
-> Show(TRUE
);
359 m_HtmlWin
-> Show(TRUE
);
360 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
362 m_NavigPan
-> SetSelection(m_SearchPage
);
363 m_SearchList
-> Clear();
364 m_SearchText
-> SetValue(keyword
);
365 m_SearchButton
-> Enable(FALSE
);
367 if (m_SearchChoice
->GetSelection() != 0)
368 book
= m_SearchChoice
->GetStringSelection();
370 wxHtmlSearchStatus
status(m_Data
, keyword
, book
);
372 wxProgressDialog
progress(_("Searching..."), _("No matching page found yet"),
373 status
.GetMaxIndex(), this,
374 wxPD_APP_MODAL
| wxPD_CAN_ABORT
| wxPD_AUTO_HIDE
);
376 while (status
.IsActive()) {
377 if (progress
.Update(status
.GetCurIndex()) == FALSE
)
379 if (status
.Search()) {
380 foundstr
.Printf(_("Found %i matches"), ++foundcnt
);
381 progress
.Update(status
.GetCurIndex(), foundstr
);
382 m_SearchList
-> Append(status
.GetName(), status
.GetContentsItem());
387 m_SearchButton
-> Enable(TRUE
);
388 m_SearchText
-> SetSelection(0, keyword
.Length());
389 m_SearchText
-> SetFocus();
391 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_SearchList
-> GetClientData(0);
392 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
394 return (foundcnt
> 0);
399 void wxHtmlHelpFrame::CreateContents(bool show_progress
)
404 wxProgressDialog
*progress
;
407 m_ContentsBox
->Clear();
409 int cnt
= m_Data
->GetContentsCnt();
410 int div
= (cnt
/ PROGRESS_STEP
) + 1;
413 wxHtmlContentsItem
*it
= m_Data
->GetContents();
416 progress
= new wxProgressDialog(_("Building contents tree..."), wxEmptyString
,
417 cnt
, this, wxPD_APP_MODAL
| wxPD_CAN_ABORT
|
420 wxTreeItemId roots
[MAX_ROOTS
];
421 bool imaged
[MAX_ROOTS
];
423 m_ContentsBox
-> DeleteAllItems();
424 roots
[0] = m_ContentsBox
-> AddRoot(_("(Help)"));
427 for (i
= 0; i
< cnt
; i
++, it
++) {
428 if (show_progress
&& ((i
% div
) == 0)) {
429 proginfo
.Printf(_("Added %d/%d items"), i
, cnt
);
430 if (! progress
->Update(i
, proginfo
))
434 roots
[it
-> m_Level
+ 1] = m_ContentsBox
-> AppendItem(
435 roots
[it
-> m_Level
], it
-> m_Name
, IMG_Page
, -1,
436 new wxHtmlHelpTreeItemData(it
));
438 if (it
-> m_Level
== 0) {
439 m_ContentsBox
-> SetItemBold(roots
[1], TRUE
);
440 m_ContentsBox
-> SetItemImage(roots
[1], IMG_Book
);
441 m_ContentsBox
-> SetItemSelectedImage(roots
[1], IMG_Book
);
443 } else imaged
[it
-> m_Level
+ 1] = FALSE
;
445 if (!imaged
[it
-> m_Level
]) {
446 m_ContentsBox
-> SetItemImage(roots
[it
-> m_Level
], IMG_Folder
);
447 m_ContentsBox
-> SetItemSelectedImage(roots
[it
-> m_Level
], IMG_Folder
);
448 imaged
[it
-> m_Level
] = TRUE
;
453 m_ContentsBox
-> Expand(roots
[0]);
457 void wxHtmlHelpFrame::CreateIndex(bool show_progress
)
462 wxProgressDialog
*progress
;
467 int cnt
= m_Data
->GetIndexCnt();
468 int div
= (cnt
/ PROGRESS_STEP
) + 1;
470 wxHtmlContentsItem
* index
= m_Data
->GetIndex();
473 progress
= new wxProgressDialog(_("Building index list..."), wxEmptyString
,
474 cnt
, this, wxPD_APP_MODAL
| wxPD_CAN_ABORT
|
476 for (int i
= 0; i
< cnt
; i
++) {
477 if (show_progress
&& ((i
% div
) == 0)) {
478 proginfo
.Printf(_("Added %d/%d items"), i
, cnt
);
479 if (! progress
->Update(i
, proginfo
))
483 m_IndexBox
-> Append(index
[i
].m_Name
, (char*)(index
+ i
));
490 void wxHtmlHelpFrame::CreateSearch()
492 if (! (m_SearchList
&& m_SearchChoice
))
494 m_SearchList
-> Clear();
495 m_SearchChoice
-> Clear();
496 m_SearchChoice
-> Append(_("all books"));
497 const wxHtmlBookRecArray
& bookrec
= m_Data
->GetBookRecArray();
498 int i
, cnt
= bookrec
.GetCount();
499 for (i
= 0; i
< cnt
; i
++)
500 m_SearchChoice
->Append(bookrec
[i
].GetTitle());
501 m_SearchChoice
->SetSelection(0);
505 void wxHtmlHelpFrame::RefreshLists(bool show_progress
)
507 CreateContents(show_progress
);
508 CreateIndex(show_progress
);
512 void wxHtmlHelpFrame::ReadCustomization(wxConfigBase
*cfg
, const wxString
& path
)
517 if (path
!= wxEmptyString
) {
518 oldpath
= cfg
-> GetPath();
519 cfg
-> SetPath(path
);
522 m_Cfg
.navig_on
= cfg
-> Read("hcNavigPanel", m_Cfg
.navig_on
) != 0;
523 m_Cfg
.sashpos
= cfg
-> Read("hcSashPos", m_Cfg
.sashpos
);
524 m_Cfg
.x
= cfg
-> Read("hcX", m_Cfg
.x
);
525 m_Cfg
.y
= cfg
-> Read("hcY", m_Cfg
.y
);
526 m_Cfg
.w
= cfg
-> Read("hcW", m_Cfg
.w
);
527 m_Cfg
.h
= cfg
-> Read("hcH", m_Cfg
.h
);
530 m_HtmlWin
->ReadCustomization(cfg
, path
);
532 if (path
!= wxEmptyString
)
533 cfg
-> SetPath(oldpath
);
536 void wxHtmlHelpFrame::WriteCustomization(wxConfigBase
*cfg
, const wxString
& path
)
541 if (path
!= wxEmptyString
) {
542 oldpath
= cfg
-> GetPath();
543 cfg
-> SetPath(path
);
546 cfg
-> Write("hcNavigPanel", m_Cfg
.navig_on
);
547 cfg
-> Write("hcSashPos", (long)m_Cfg
.sashpos
);
548 cfg
-> Write("hcX", (long)m_Cfg
.x
);
549 cfg
-> Write("hcY", (long)m_Cfg
.y
);
550 cfg
-> Write("hcW", (long)m_Cfg
.w
);
551 cfg
-> Write("hcH", (long)m_Cfg
.h
);
554 m_HtmlWin
->WriteCustomization(cfg
, path
);
556 if (path
!= wxEmptyString
)
557 cfg
-> SetPath(oldpath
);
566 void wxHtmlHelpFrame::OnToolbar(wxCommandEvent
& event
)
568 switch (event
.GetId()) {
569 case wxID_HTML_BACK
:
570 m_HtmlWin
-> HistoryBack();
572 case wxID_HTML_FORWARD
:
573 m_HtmlWin
-> HistoryForward();
575 case wxID_HTML_PANEL
:
576 if (! (m_Splitter
&& m_NavigPan
))
578 if (m_Splitter
-> IsSplit()) {
579 m_Cfg
.sashpos
= m_Splitter
-> GetSashPosition();
580 m_Splitter
-> Unsplit(m_NavigPan
);
581 m_Cfg
.navig_on
= FALSE
;
583 m_NavigPan
-> Show(TRUE
);
584 m_HtmlWin
-> Show(TRUE
);
585 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
586 m_Cfg
.navig_on
= TRUE
;
594 void wxHtmlHelpFrame::OnContentsSel(wxTreeEvent
& event
)
596 wxHtmlHelpTreeItemData
*pg
;
598 pg
= (wxHtmlHelpTreeItemData
*) m_ContentsBox
-> GetItemData(event
.GetItem());
599 if (pg
) m_HtmlWin
-> LoadPage(pg
-> GetPage());
604 void wxHtmlHelpFrame::OnIndexSel(wxCommandEvent
& WXUNUSED(event
))
606 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_IndexBox
-> GetClientData(m_IndexBox
-> GetSelection());
607 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
612 void wxHtmlHelpFrame::OnSearchSel(wxCommandEvent
& WXUNUSED(event
))
614 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_SearchList
-> GetClientData(m_SearchList
-> GetSelection());
615 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
618 void wxHtmlHelpFrame::OnSearch(wxCommandEvent
& WXUNUSED(event
))
620 wxString sr
= m_SearchText
-> GetLineText(0);
622 if (sr
!= wxEmptyString
) KeywordSearch(sr
);
625 void wxHtmlHelpFrame::OnCloseWindow(wxCloseEvent
& evt
)
627 GetSize(&m_Cfg
.w
, &m_Cfg
.h
);
628 GetPosition(&m_Cfg
.x
, &m_Cfg
.y
);
629 if (m_Splitter
&& m_Cfg
.navig_on
) m_Cfg
.sashpos
= m_Splitter
-> GetSashPosition();
632 WriteCustomization(m_Config
, m_ConfigRoot
);
637 BEGIN_EVENT_TABLE(wxHtmlHelpFrame
, wxFrame
)
638 EVT_TOOL_RANGE(wxID_HTML_PANEL
, wxID_HTML_FORWARD
, wxHtmlHelpFrame::OnToolbar
)
639 EVT_TREE_SEL_CHANGED(wxID_HTML_TREECTRL
, wxHtmlHelpFrame::OnContentsSel
)
640 EVT_LISTBOX(wxID_HTML_INDEXLIST
, wxHtmlHelpFrame::OnIndexSel
)
641 EVT_LISTBOX(wxID_HTML_SEARCHLIST
, wxHtmlHelpFrame::OnSearchSel
)
642 EVT_BUTTON(wxID_HTML_SEARCHBUTTON
, wxHtmlHelpFrame::OnSearch
)
643 EVT_TEXT_ENTER(wxID_HTML_SEARCHTEXT
, wxHtmlHelpFrame::OnSearch
)
644 EVT_CLOSE(wxHtmlHelpFrame::OnCloseWindow
)