]> git.saurik.com Git - wxWidgets.git/blob - src/generic/helpwxht.cpp
declared (not implemented yet) wxMimeTypesManager::AddFallbacks()
[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 # include "wx/combobox.h"
31 #endif
32
33 #include "wx/helpbase.h"
34 #include "wx/generic/helpwxht.h"
35 #include "wx/html/htmlwin.h"
36
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <sys/stat.h>
40
41 #ifndef __WINDOWS__
42 # include <unistd.h>
43 #endif
44
45 IMPLEMENT_CLASS(wxHelpControllerHtml, wxHTMLHelpControllerBase)
46
47 /**
48 This class implements help via wxHTML.
49 It requires the name of a directory containing the documentation
50 and a file mapping numerical Section numbers to relative URLS.
51 */
52
53 class wxForceHtmlFilter : public wxHtmlFilter
54 {
55 public:
56 virtual wxString ReadFile(const wxFSFile& file) const
57 {
58 wxInputStream *s = file.GetStream();
59 char *src;
60 wxString doc;
61
62 if (s == NULL) return wxEmptyString;
63 src = new char[s -> GetSize()+1];
64 src[s -> GetSize()] = 0;
65 s -> Read(src, s -> GetSize());
66 doc = src;
67 delete [] src;
68 return doc;
69 }
70
71 virtual bool CanRead(const wxFSFile& file) const
72 {
73 wxString filename = file.GetLocation();
74 if(filename.Length() >= 5 &&
75 (
76 filename.Right(4).MakeUpper() == ".HTM" ||
77 filename.Right(5).MakeUpper() == ".HTML"))
78 return TRUE;
79 else
80 return FALSE;
81 }
82 };
83
84 #define FRAME_WIDTH 500
85 #define FRAME_HEIGHT 400
86 #define LAYOUT_X_MARGIN 2
87 #define LAYOUT_Y_MARGIN 2
88 #define OFFSET 10
89 #define BUTTON_WIDTH 70
90 #define MAX_COMBO_ENTRIES 25
91
92 class wxHelpFrame : public wxFrame
93 {
94 public:
95 wxHelpFrame(wxWindow *parent, int id, const wxString &title,
96 const wxPoint &pos, const wxSize &size,
97 wxHelpControllerHtml *controller);
98 ~wxHelpFrame();
99 void OnClose(wxCloseEvent &ev);
100 void OnButton(wxCommandEvent &ev);
101 bool LoadPage(const wxString &url) { return m_htmlwin->LoadPage(url); }
102 private:
103 wxHelpControllerHtml *m_controller;
104 wxHtmlWindow *m_htmlwin;
105 wxHtmlFilter *m_filter;
106 wxComboBox *m_combo;
107 long m_IdBack, m_IdFwd, m_IdContents, m_IdCombo, m_IdSearch;
108 DECLARE_EVENT_TABLE()
109 };
110
111 BEGIN_EVENT_TABLE(wxHelpFrame, wxFrame)
112 EVT_CLOSE(wxHelpFrame::OnClose)
113 EVT_BUTTON(-1, wxHelpFrame::OnButton)
114 END_EVENT_TABLE()
115
116
117 void
118 wxHelpFrame::OnButton(wxCommandEvent &ev)
119 {
120 long id =ev.GetId();
121
122 if(id == m_IdBack)
123 m_htmlwin->HistoryBack();
124 else if(id == m_IdFwd)
125 m_htmlwin->HistoryForward();
126 else if(id == m_IdContents)
127 m_controller->DisplayContents();
128 else if(id == m_IdSearch)
129 {
130 wxString str = m_combo->GetValue();
131 if(m_combo->FindString(str) == -1 && m_combo->Number() < MAX_COMBO_ENTRIES)
132 m_combo->Append(str);
133 m_controller->KeywordSearch(str);
134 }
135 }
136
137 wxHelpFrame::wxHelpFrame(wxWindow *parent, int id,
138 const wxString &title,
139 const wxPoint &pos, const wxSize &size,
140 wxHelpControllerHtml *controller)
141 : wxFrame(parent, id, title, pos, size)
142 {
143
144 m_controller = controller;
145 m_htmlwin = new wxHtmlWindow(this,-1,wxDefaultPosition,wxSize(FRAME_WIDTH,
146 FRAME_HEIGHT));
147
148 m_IdBack = wxWindow::NewControlId();
149 m_IdFwd = wxWindow::NewControlId();
150 m_IdContents = wxWindow::NewControlId();
151 m_IdCombo = wxWindow::NewControlId();
152 m_IdSearch = wxWindow::NewControlId();
153
154 wxButton *btn_back = new wxButton(this, m_IdBack, _("Back"));
155 wxButton *btn_fwd = new wxButton(this, m_IdFwd, _("Forward"));
156 wxButton *btn_contents = new wxButton(this, m_IdContents, _("Contents"));
157 m_combo = new wxComboBox(this, m_IdCombo);
158 wxButton *btn_search = new wxButton(this, m_IdSearch, _("Search"));
159
160 m_filter = new wxForceHtmlFilter;
161
162 wxLayoutConstraints *c;
163
164 c = new wxLayoutConstraints;
165 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
166 c->width.Absolute(BUTTON_WIDTH);
167 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
168 c->height.AsIs();
169 btn_back->SetConstraints(c);
170
171 c = new wxLayoutConstraints;
172 c->left.SameAs(btn_back, wxRight, 2*LAYOUT_X_MARGIN);
173 c->width.Absolute(BUTTON_WIDTH);
174 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
175 c->height.AsIs();
176 btn_fwd->SetConstraints(c);
177
178 c = new wxLayoutConstraints;
179 c->left.SameAs(btn_fwd, wxRight, 2*LAYOUT_X_MARGIN);
180 c->width.Absolute(BUTTON_WIDTH);
181 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
182 c->height.AsIs();
183 btn_contents->SetConstraints(c);
184
185 c = new wxLayoutConstraints;
186 c->left.SameAs(btn_contents, wxRight, 2*LAYOUT_X_MARGIN);
187 c->width.Absolute(3*BUTTON_WIDTH);
188 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
189 c->height.AsIs();
190 m_combo->SetConstraints(c);
191
192 c = new wxLayoutConstraints;
193 c->left.SameAs(m_combo, wxRight, 2*LAYOUT_X_MARGIN);
194 c->width.Absolute(BUTTON_WIDTH);
195 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
196 c->height.AsIs();
197 btn_search->SetConstraints(c);
198
199
200 c = new wxLayoutConstraints;
201 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
202 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
203 c->top.SameAs(btn_back, wxBottom, 2*LAYOUT_Y_MARGIN);
204 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
205 m_htmlwin->SetConstraints(c);
206 SetAutoLayout(TRUE);
207 CreateStatusBar();
208
209 m_htmlwin->SetRelatedFrame(this, title);
210 m_htmlwin->SetRelatedStatusBar(0);
211 m_htmlwin->AddFilter(m_filter);
212
213 Show(TRUE);
214 }
215
216 wxHelpFrame::~wxHelpFrame()
217 {
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 wxFrame *
298 wxHelpControllerHtml::GetFrameParameters(wxSize *size,
299 wxPoint *pos,
300 bool *newframe)
301 {
302 if(size) *size = m_FrameSize;
303 if(pos) *pos = m_FramePosition;
304 if(newframe) *newframe = m_NewFrameEachTime;
305 return m_Frame;
306 }
307
308 #endif // wxUSE_HTML