]> git.saurik.com Git - wxWidgets.git/blob - src/generic/helpwxht.cpp
Much improved wxHelpControllerHtml, works under Solaris, too now.
[wxWidgets.git] / src / generic / helpwxht.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpext.cpp
3 // Purpose: an external help controller for wxWindows
4 // Author: Karsten Ballueder
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballueder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 # pragma implementation "helpwxht.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 # pragma hdrstop
20 #endif
21
22 #if wxUSE_HTML
23
24 #ifndef WX_PRECOMP
25 # include "wx/string.h"
26 # include "wx/utils.h"
27 # include "wx/list.h"
28 # include "wx/intl.h"
29 # include "wx/layout.h"
30 #endif
31
32 #include "wx/helpbase.h"
33 #include "wx/generic/helpwxht.h"
34 #include "wx/html/htmlwin.h"
35
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <sys/stat.h>
39
40 #ifndef __WINDOWS__
41 # include <unistd.h>
42 #endif
43
44 IMPLEMENT_CLASS(wxHelpControllerHtml, wxHTMLHelpControllerBase)
45
46 /**
47 This class implements help via wxHTML.
48 It requires the name of a directory containing the documentation
49 and a file mapping numerical Section numbers to relative URLS.
50 */
51
52 class wxForceHtmlFilter : public wxHtmlFilter
53 {
54 public:
55 virtual wxString ReadFile(const wxFSFile& file)
56 {
57 wxInputStream *s = file.GetStream();
58 char *src;
59 wxString doc;
60
61 if (s == NULL) return wxEmptyString;
62 src = new char[s -> StreamSize()+1];
63 src[s -> StreamSize()] = 0;
64 s -> Read(src, s -> StreamSize());
65 doc = src;
66 delete [] src;
67 return doc;
68 }
69
70 virtual bool CanRead(const wxFSFile& file)
71 {
72 wxString filename = file.GetLocation();
73 if(filename.Length() >= 5 &&
74 (
75 filename.Right(4).MakeUpper() == ".HTM" ||
76 filename.Right(5).MakeUpper() == ".HTML"))
77 return TRUE;
78 else
79 return FALSE;
80 }
81 };
82
83 #define FRAME_WIDTH 500
84 #define FRAME_HEIGHT 400
85 #define LAYOUT_X_MARGIN 2
86 #define LAYOUT_Y_MARGIN 2
87 #define OFFSET 10
88 #define BUTTON_WIDTH 70
89 #define MAX_COMBO_ENTRIES 25
90
91 class wxHelpFrame : public wxFrame
92 {
93 public:
94 wxHelpFrame(wxWindow *parent, int id, const wxString &title,
95 const wxPoint &pos, const wxSize &size,
96 wxHelpControllerHtml *controller);
97 ~wxHelpFrame();
98 void OnClose(wxCloseEvent &ev);
99 void OnButton(wxCommandEvent &ev);
100 bool LoadPage(const wxString &url) { return m_htmlwin->LoadPage(url); }
101 private:
102 wxHelpControllerHtml *m_controller;
103 wxHtmlWindow *m_htmlwin;
104 wxHtmlFilter *m_filter;
105 wxComboBox *m_combo;
106 long m_IdBack, m_IdFwd, m_IdContents, m_IdCombo, m_IdSearch;
107 DECLARE_EVENT_TABLE()
108 };
109
110 BEGIN_EVENT_TABLE(wxHelpFrame, wxFrame)
111 EVT_CLOSE(wxHelpFrame::OnClose)
112 EVT_BUTTON(-1, wxHelpFrame::OnButton)
113 END_EVENT_TABLE()
114
115
116 void
117 wxHelpFrame::OnButton(wxCommandEvent &ev)
118 {
119 long id =ev.GetId();
120
121 if(id == m_IdBack)
122 m_htmlwin->HistoryBack();
123 else if(id == m_IdFwd)
124 m_htmlwin->HistoryForward();
125 else if(id == m_IdContents)
126 m_controller->DisplayContents();
127 else if(id == m_IdSearch)
128 {
129 wxString str = m_combo->GetValue();
130 if(m_combo->FindString(str) == -1 && m_combo->Number() < MAX_COMBO_ENTRIES)
131 m_combo->Append(str);
132 m_controller->KeywordSearch(str);
133 }
134 }
135
136 wxHelpFrame::wxHelpFrame(wxWindow *parent, int id,
137 const wxString &title,
138 const wxPoint &pos, const wxSize &size,
139 wxHelpControllerHtml *controller)
140 : wxFrame(parent, id, title, pos, size)
141 {
142
143 m_controller = controller;
144 m_htmlwin = new wxHtmlWindow(this,-1,wxDefaultPosition,wxSize(FRAME_WIDTH,
145 FRAME_HEIGHT));
146
147 m_IdBack = wxWindow::NewControlId();
148 m_IdFwd = wxWindow::NewControlId();
149 m_IdContents = wxWindow::NewControlId();
150 m_IdCombo = wxWindow::NewControlId();
151 m_IdSearch = wxWindow::NewControlId();
152
153 wxButton *btn_back = new wxButton(this, m_IdBack, _("Back"));
154 wxButton *btn_fwd = new wxButton(this, m_IdFwd, _("Forward"));
155 wxButton *btn_contents = new wxButton(this, m_IdContents, _("Contents"));
156 m_combo = new wxComboBox(this, m_IdCombo);
157 wxButton *btn_search = new wxButton(this, m_IdSearch, _("Search"));
158
159 m_filter = new wxForceHtmlFilter;
160
161 wxLayoutConstraints *c;
162
163 c = new wxLayoutConstraints;
164 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
165 c->width.Absolute(BUTTON_WIDTH);
166 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
167 c->height.AsIs();
168 btn_back->SetConstraints(c);
169
170 c = new wxLayoutConstraints;
171 c->left.SameAs(btn_back, wxRight, 2*LAYOUT_X_MARGIN);
172 c->width.Absolute(BUTTON_WIDTH);
173 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
174 c->height.AsIs();
175 btn_fwd->SetConstraints(c);
176
177 c = new wxLayoutConstraints;
178 c->left.SameAs(btn_fwd, wxRight, 2*LAYOUT_X_MARGIN);
179 c->width.Absolute(BUTTON_WIDTH);
180 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
181 c->height.AsIs();
182 btn_contents->SetConstraints(c);
183
184 c = new wxLayoutConstraints;
185 c->left.SameAs(btn_contents, wxRight, 2*LAYOUT_X_MARGIN);
186 c->width.Absolute(3*BUTTON_WIDTH);
187 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
188 c->height.AsIs();
189 m_combo->SetConstraints(c);
190
191 c = new wxLayoutConstraints;
192 c->left.SameAs(m_combo, wxRight, 2*LAYOUT_X_MARGIN);
193 c->width.Absolute(BUTTON_WIDTH);
194 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
195 c->height.AsIs();
196 btn_search->SetConstraints(c);
197
198
199 c = new wxLayoutConstraints;
200 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
201 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
202 c->top.SameAs(btn_back, wxBottom, 2*LAYOUT_Y_MARGIN);
203 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
204 m_htmlwin->SetConstraints(c);
205 SetAutoLayout(TRUE);
206 CreateStatusBar();
207
208 m_htmlwin->SetRelatedFrame(this, title);
209 m_htmlwin->SetRelatedStatusBar(0);
210 m_htmlwin->AddFilter(m_filter);
211
212 Show(TRUE);
213 }
214
215 wxHelpFrame::~wxHelpFrame()
216 {
217 // delete m_filter;
218 }
219
220 void
221 wxHelpFrame::OnClose(wxCloseEvent &ev)
222 {
223 wxASSERT(m_controller);
224 m_controller->m_Frame = NULL;
225 bool newFrame;
226 int x,y;
227 GetPosition(&x,&y);
228
229 m_controller->GetFrameParameters(NULL, NULL, &newFrame);
230 m_controller->SetFrameParameters(GetTitle(), GetSize(),
231 wxPoint(x,y),
232 newFrame);
233 Destroy();
234 }
235
236 wxHelpControllerHtml::wxHelpControllerHtml(void)
237 {
238 m_Frame = NULL;
239 m_offset = 0;
240
241 SetFrameParameters(_("Help: %s"),
242 wxSize(FRAME_WIDTH, FRAME_HEIGHT),
243 wxDefaultPosition);
244 }
245
246 wxHelpControllerHtml::~wxHelpControllerHtml(void)
247 {
248 if(m_Frame && ! m_NewFrameEachTime)
249 m_Frame->Close();
250 }
251
252
253 #ifdef __WXMSW__
254 # define SEP '\\'
255 #else
256 # define SEP '/'
257 #endif
258
259 bool
260 wxHelpControllerHtml::DisplayHelp(wxString const &relativeURL)
261 {
262 wxBusyCursor b; // display a busy cursor
263
264 wxString url;
265 url << m_MapFile << SEP<< relativeURL;
266 if(! m_Frame || m_NewFrameEachTime)
267 {
268 m_Frame = new wxHelpFrame(NULL, -1, m_FrameTitle,
269 m_FramePosition+wxPoint(m_offset,m_offset),
270 m_FrameSize,
271 this);
272 if(m_NewFrameEachTime)
273 {
274 m_offset += OFFSET;
275 if(m_offset > 200)
276 m_offset = 0;
277 }
278
279 }
280 m_Frame->Raise();
281 return m_Frame->LoadPage(url);
282 }
283
284
285 void
286 wxHelpControllerHtml::SetFrameParameters(const wxString &title,
287 const wxSize &size,
288 const wxPoint &pos,
289 bool newFrame)
290 {
291 m_FrameTitle = title;
292 m_FrameSize = size;
293 m_FramePosition = pos;
294 m_NewFrameEachTime = newFrame;
295 }
296
297 void
298 wxHelpControllerHtml::GetFrameParameters(wxSize *size = NULL,
299 wxPoint *pos = NULL,
300 bool *newframe = NULL)
301 {
302 if(size) *size = m_FrameSize;
303 if(pos) *pos = m_FramePosition;
304 if(newframe) *newframe = m_NewFrameEachTime;
305 }
306
307 #endif // wxUSE_HTML