]>
git.saurik.com Git - wxWidgets.git/blob - src/html/helpfrm.cpp
465670a1269222f3150da812c37cb2870e550a61
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(12, 12);
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
, "", 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_HtmlWin
-> Show(TRUE
);
287 wxHtmlHelpFrame::~wxHtmlHelpFrame()
289 delete m_ContentsImageList
;
294 bool wxHtmlHelpFrame::Display(const wxString
& x
)
296 wxString url
= m_Data
->FindPageByName(x
);
297 if (! url
.IsEmpty()) {
298 m_HtmlWin
->LoadPage(url
);
304 bool wxHtmlHelpFrame::Display(const int id
)
306 wxString url
= m_Data
->FindPageById(id
);
307 if (! url
.IsEmpty()) {
308 m_HtmlWin
->LoadPage(url
);
316 bool wxHtmlHelpFrame::DisplayContents()
320 if (!m_Splitter
-> IsSplit()) {
321 m_NavigPan
-> Show(TRUE
);
322 m_HtmlWin
-> Show(TRUE
);
323 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
325 m_NavigPan
-> SetSelection(0);
331 bool wxHtmlHelpFrame::DisplayIndex()
335 if (!m_Splitter
-> IsSplit()) {
336 m_NavigPan
-> Show(TRUE
);
337 m_HtmlWin
-> Show(TRUE
);
338 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
340 m_NavigPan
-> SetSelection(1);
344 bool wxHtmlHelpFrame::KeywordSearch(const wxString
& keyword
)
346 if (! (m_SearchList
&& m_SearchButton
&& m_SearchText
&& m_SearchChoice
))
351 wxString book
= wxEmptyString
;
353 if (!m_Splitter
-> IsSplit()) {
354 m_NavigPan
-> Show(TRUE
);
355 m_HtmlWin
-> Show(TRUE
);
356 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
358 m_NavigPan
-> SetSelection(m_SearchPage
);
359 m_SearchList
-> Clear();
360 m_SearchText
-> SetValue(keyword
);
361 m_SearchButton
-> Enable(FALSE
);
363 if (m_SearchChoice
->GetSelection() != 0)
364 book
= m_SearchChoice
->GetStringSelection();
366 wxHtmlSearchStatus
status(m_Data
, keyword
, book
);
368 wxProgressDialog
progress(_("Searching..."), _("No matching page found yet"),
369 status
.GetMaxIndex(), this,
370 wxPD_APP_MODAL
| wxPD_CAN_ABORT
| wxPD_AUTO_HIDE
);
372 while (status
.IsActive()) {
373 if (progress
.Update(status
.GetCurIndex()) == FALSE
)
375 if (status
.Search()) {
376 foundstr
.Printf(_("Found %i matches"), ++foundcnt
);
377 progress
.Update(status
.GetCurIndex(), foundstr
);
378 m_SearchList
-> Append(status
.GetName(), status
.GetContentsItem());
383 m_SearchButton
-> Enable(TRUE
);
384 m_SearchText
-> SetSelection(0, keyword
.Length());
385 m_SearchText
-> SetFocus();
387 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_SearchList
-> GetClientData(0);
388 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
390 return (foundcnt
> 0);
395 void wxHtmlHelpFrame::CreateContents(bool show_progress
)
400 wxProgressDialog
*progress
;
403 m_ContentsBox
->Clear();
405 int cnt
= m_Data
->GetContentsCnt();
406 int div
= (cnt
/ PROGRESS_STEP
) + 1;
409 wxHtmlContentsItem
*it
= m_Data
->GetContents();
412 progress
= new wxProgressDialog(_("Building contents tree..."), wxEmptyString
,
413 cnt
, this, wxPD_APP_MODAL
| wxPD_CAN_ABORT
|
416 wxTreeItemId roots
[MAX_ROOTS
];
417 bool imaged
[MAX_ROOTS
];
419 m_ContentsBox
-> DeleteAllItems();
420 roots
[0] = m_ContentsBox
-> AddRoot(_("(Help)"));
423 for (i
= 0; i
< cnt
; i
++, it
++) {
424 if (show_progress
&& ((i
% div
) == 0)) {
425 proginfo
.Printf(_("Added %d/%d items"), i
, cnt
);
426 if (! progress
->Update(i
, proginfo
))
430 roots
[it
-> m_Level
+ 1] = m_ContentsBox
-> AppendItem(
431 roots
[it
-> m_Level
], it
-> m_Name
, IMG_Page
, -1,
432 new wxHtmlHelpTreeItemData(it
));
434 if (it
-> m_Level
== 0) {
435 m_ContentsBox
-> SetItemBold(roots
[1], TRUE
);
436 m_ContentsBox
-> SetItemImage(roots
[1], IMG_Book
);
437 m_ContentsBox
-> SetItemSelectedImage(roots
[1], IMG_Book
);
439 } else imaged
[it
-> m_Level
+ 1] = FALSE
;
441 if (!imaged
[it
-> m_Level
]) {
442 m_ContentsBox
-> SetItemImage(roots
[it
-> m_Level
], IMG_Folder
);
443 m_ContentsBox
-> SetItemSelectedImage(roots
[it
-> m_Level
], IMG_Folder
);
444 imaged
[it
-> m_Level
] = TRUE
;
449 m_ContentsBox
-> Expand(roots
[0]);
453 void wxHtmlHelpFrame::CreateIndex(bool show_progress
)
458 wxProgressDialog
*progress
;
463 int cnt
= m_Data
->GetIndexCnt();
464 int div
= (cnt
/ PROGRESS_STEP
) + 1;
466 wxHtmlContentsItem
* index
= m_Data
->GetIndex();
469 progress
= new wxProgressDialog(_("Building index list..."), wxEmptyString
,
470 cnt
, this, wxPD_APP_MODAL
| wxPD_CAN_ABORT
|
472 for (int i
= 0; i
< cnt
; i
++) {
473 if (show_progress
&& ((i
% div
) == 0)) {
474 proginfo
.Printf(_("Added %d/%d items"), i
, cnt
);
475 if (! progress
->Update(i
, proginfo
))
479 m_IndexBox
-> Append(index
[i
].m_Name
, (char*)(index
+ i
));
486 void wxHtmlHelpFrame::CreateSearch()
488 if (! (m_SearchList
&& m_SearchChoice
))
490 m_SearchList
-> Clear();
491 m_SearchChoice
-> Clear();
492 m_SearchChoice
-> Append(_("all books"));
493 const wxHtmlBookRecArray
& bookrec
= m_Data
->GetBookRecArray();
494 int i
, cnt
= bookrec
.GetCount();
495 for (i
= 0; i
< cnt
; i
++)
496 m_SearchChoice
->Append(bookrec
[i
].GetTitle());
497 m_SearchChoice
->SetSelection(0);
501 void wxHtmlHelpFrame::RefreshLists(bool show_progress
)
503 CreateContents(show_progress
);
504 CreateIndex(show_progress
);
508 void wxHtmlHelpFrame::ReadCustomization(wxConfigBase
*cfg
, const wxString
& path
)
513 if (path
!= wxEmptyString
) {
514 oldpath
= cfg
-> GetPath();
515 cfg
-> SetPath(path
);
518 m_Cfg
.navig_on
= cfg
-> Read("hcNavigPanel", m_Cfg
.navig_on
) != 0;
519 m_Cfg
.sashpos
= cfg
-> Read("hcSashPos", m_Cfg
.sashpos
);
520 m_Cfg
.x
= cfg
-> Read("hcX", m_Cfg
.x
);
521 m_Cfg
.y
= cfg
-> Read("hcY", m_Cfg
.y
);
522 m_Cfg
.w
= cfg
-> Read("hcW", m_Cfg
.w
);
523 m_Cfg
.h
= cfg
-> Read("hcH", m_Cfg
.h
);
526 m_HtmlWin
->ReadCustomization(cfg
, path
);
528 if (path
!= wxEmptyString
)
529 cfg
-> SetPath(oldpath
);
532 void wxHtmlHelpFrame::WriteCustomization(wxConfigBase
*cfg
, const wxString
& path
)
537 if (path
!= wxEmptyString
) {
538 oldpath
= cfg
-> GetPath();
539 cfg
-> SetPath(path
);
542 cfg
-> Write("hcNavigPanel", m_Cfg
.navig_on
);
543 cfg
-> Write("hcSashPos", (long)m_Cfg
.sashpos
);
544 cfg
-> Write("hcX", (long)m_Cfg
.x
);
545 cfg
-> Write("hcY", (long)m_Cfg
.y
);
546 cfg
-> Write("hcW", (long)m_Cfg
.w
);
547 cfg
-> Write("hcH", (long)m_Cfg
.h
);
550 m_HtmlWin
->WriteCustomization(cfg
, path
);
552 if (path
!= wxEmptyString
)
553 cfg
-> SetPath(oldpath
);
562 void wxHtmlHelpFrame::OnToolbar(wxCommandEvent
& event
)
564 switch (event
.GetId()) {
565 case wxID_HTML_BACK
:
566 m_HtmlWin
-> HistoryBack();
568 case wxID_HTML_FORWARD
:
569 m_HtmlWin
-> HistoryForward();
571 case wxID_HTML_PANEL
:
572 if (! (m_Splitter
&& m_NavigPan
))
574 if (m_Splitter
-> IsSplit()) {
575 m_Cfg
.sashpos
= m_Splitter
-> GetSashPosition();
576 m_Splitter
-> Unsplit(m_NavigPan
);
578 m_NavigPan
-> Show(TRUE
);
579 m_HtmlWin
-> Show(TRUE
);
580 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
588 void wxHtmlHelpFrame::OnContentsSel(wxTreeEvent
& event
)
590 wxHtmlHelpTreeItemData
*pg
;
592 pg
= (wxHtmlHelpTreeItemData
*) m_ContentsBox
-> GetItemData(event
.GetItem());
593 if (pg
) m_HtmlWin
-> LoadPage(pg
-> GetPage());
598 void wxHtmlHelpFrame::OnIndexSel(wxCommandEvent
& WXUNUSED(event
))
600 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_IndexBox
-> GetClientData(m_IndexBox
-> GetSelection());
601 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
606 void wxHtmlHelpFrame::OnSearchSel(wxCommandEvent
& WXUNUSED(event
))
608 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_SearchList
-> GetClientData(m_SearchList
-> GetSelection());
609 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
612 void wxHtmlHelpFrame::OnSearch(wxCommandEvent
& WXUNUSED(event
))
614 wxString sr
= m_SearchText
-> GetLineText(0);
616 if (sr
!= wxEmptyString
) KeywordSearch(sr
);
619 void wxHtmlHelpFrame::OnCloseWindow(wxCloseEvent
& evt
)
622 WriteCustomization(m_Config
, m_ConfigRoot
);
626 BEGIN_EVENT_TABLE(wxHtmlHelpFrame
, wxFrame
)
627 EVT_TOOL_RANGE(wxID_HTML_PANEL
, wxID_HTML_FORWARD
, wxHtmlHelpFrame::OnToolbar
)
628 EVT_TREE_SEL_CHANGED(wxID_HTML_TREECTRL
, wxHtmlHelpFrame::OnContentsSel
)
629 EVT_LISTBOX(wxID_HTML_INDEXLIST
, wxHtmlHelpFrame::OnIndexSel
)
630 EVT_LISTBOX(wxID_HTML_SEARCHLIST
, wxHtmlHelpFrame::OnSearchSel
)
631 EVT_BUTTON(wxID_HTML_SEARCHBUTTON
, wxHtmlHelpFrame::OnSearch
)
632 EVT_TEXT_ENTER(wxID_HTML_SEARCHTEXT
, wxHtmlHelpFrame::OnSearch
)
633 EVT_CLOSE(wxHtmlHelpFrame::OnCloseWindow
)