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