Debian Codegraph @

Contrib/NSIS Menu/nsismenu/nsismenu.cpp

raw
1 /////////////////////////////////////////////////////////////////////////////
2 // NSIS MENU
3 //
4 // Reviewed for Unicode support by Jim Park -- 08/23/2007
5 // Basically, compiling wxWidgets as Unicode should do it.
6 /////////////////////////////////////////////////////////////////////////////
7
8 // For compilers that support precompilation, includes "wx/wx.h".
9 #include <wx/wxprec.h>
10
11 #ifdef __BORLANDC__
12 #pragma hdrstop
13 #endif
14
15 // for all others, include the necessary headers (this file is usually all you
16 // need because it includes almost all "standard" wxWindows headers
17 #ifndef WX_PRECOMP
18 #include <wx/wx.h>
19 #endif
20
21 #include <wx/event.h>
22 #include <wx/filefn.h>
23 #include <wx/image.h>
24 #include <wx/html/htmlwin.h>
25 #include <wx/html/htmlproc.h>
26 #include <wx/stdpaths.h>
27 #include <wx/utils.h>
28
29 #include <nsis-sconf.h>
30
31 // ----------------------------------------------------------------------------
32 // private classes
33 // ----------------------------------------------------------------------------
34
35 #if !wxCHECK_VERSION(2, 9, 0) && !defined(wxLaunchDefaultApplication)
36 #define wxLaunchDefaultApplication wxLaunchDefaultBrowser
37 #endif
38 #ifdef __WXMSW__
39 #define CanOpenChm() true
40 #else
41 #define CanOpenChm() false
42 #endif
43
44 typedef enum { SUT_UNKNOWN = 0, SUT_BIN = 0x14, SUT_DOC = 0x24, SUT_WEB = 0x34 } SPECIALURLTYPE; // The low nibble contains the "protocol" length!
45 SPECIALURLTYPE GetSpecialUrlType(const wxString&Url)
46 {
47 const wxString l4 = Url.Left(4);
48 if (0 == l4.CmpNoCase(wxT("BIN:"))) return SUT_BIN;
49 if (0 == l4.CmpNoCase(wxT("DOC:"))) return SUT_DOC;
50 if (0 == l4.CmpNoCase(wxT("WEB:"))) return SUT_WEB;
51 return SUT_UNKNOWN;
52 }
53
54 static bool PathExists(const wxString&Path) { return wxFileExists(Path) || wxDirExists(Path); }
55 static wxString BuildPathWorker(const wxChar*a, const wxChar*b)
56 {
57 wxString path(a);
58 if (path.Last() != wxFileName::GetPathSeparator()) path.Append(wxFileName::GetPathSeparator());
59 return (path.Append(b), path);
60 }
61 static const wxChar*GetCStr(const wxString&t) { return t.c_str(); }
62 static const wxChar*GetCStr(const wxChar*t) { return t; }
63 template<class A, class B> wxString BuildPath(const A&a, const B&b) { return BuildPathWorker(GetCStr(a), GetCStr(b)); }
64 template<class A, class B, class C> wxString BuildPath(const A&a, const B&b, const C&c) { return BuildPath(BuildPath(GetCStr(a), GetCStr(b)), GetCStr(c)); }
65
66 wxString GetMenuHtmlFile(const wxChar*file)
67 {
68 #ifdef __WXMSW__
69 wxString dataroot(wxPathOnly(wxStandardPaths::Get().GetExecutablePath()));
70 #else
71 const wxChar*dataroot = wxT(PREFIX_DATA);
72 #endif
73 return BuildPath(dataroot, wxT("Menu"), file);
74 }
75
76 SPECIALURLTYPE TransformUrl(wxString&Url)
77 {
78 SPECIALURLTYPE ut = GetSpecialUrlType(Url);
79 const wxString location = Url.Mid(ut & 0x0F);
80 const wxString exePath = wxPathOnly(wxStandardPaths::Get().GetExecutablePath());
81
82 if (SUT_BIN == ut)
83 {
84 #ifdef __WXMSW__
85 Url = BuildPath(exePath, location) + wxT(".exe");
86 if (!PathExists(Url))
87 Url = BuildPath(exePath, wxT("Bin"), location) + wxT(".exe");
88 #else
89 Url = BuildPath(exePath, location);
90 #endif
91 }
92 else if (SUT_DOC == ut)
93 {
94 #ifdef __WXMSW__
95 wxString path = BuildPath(exePath, location);
96 #else
97 wxString path = BuildPath(wxT(PREFIX_DOC), location);
98 #endif
99 if ((!CanOpenChm() || !wxFileExists(path)) && 0 == location.CmpNoCase(wxT("NSIS.chm")))
100 {
101 path = BuildPath(wxPathOnly(path), wxT("Docs"), wxT("Manual.html")); // DOCTYPES=htmlsingle
102 if (!wxFileExists(path))
103 path = BuildPath(wxPathOnly(path), wxT("Contents.html")); // DOCTYPES=html (Not adding /Docs/ because it has already been appended)
104 }
105 Url = path;
106 }
107 else if (SUT_WEB == ut)
108 {
109 Url = location;
110 }
111 return ut;
112 }
113
114
115 // Define a new application type, each program should derive a class from wxApp
116 class MyApp : public wxApp
117 {
118 public:
119 // override base class virtuals
120 // ----------------------------
121
122 // this one is called on application startup and is a good place for the app
123 // initialization (doing it here and not in the ctor allows to have an error
124 // return: if OnInit() returns false, the application terminates)
125 virtual bool OnInit();
126 };
127
128 // Define a new frame type: this is going to be our main frame
129 class MyFrame : public wxFrame
130 {
131 public:
132 // ctor(s)
133 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
134 // event handler(s)
135 void OnLink(wxHtmlLinkEvent& event);
136 void OnClose(wxCloseEvent& event);
137
138 private:
139 wxHtmlWindow *m_Html;
140
141 // any class wishing to process wxWindows events must use this macro
142 DECLARE_EVENT_TABLE()
143 };
144
145 // ----------------------------------------------------------------------------
146 // constants
147 // ----------------------------------------------------------------------------
148
149 #define HTMLW 598 // release.py generates a 598x45 header image
150 #define HTMLH 365
151
152 // IDs for the controls and the menu commands
153 enum
154 {
155 // controls start here (the numbers are, of course, arbitrary)
156 HtmlControl = 1000
157 };
158
159 // ----------------------------------------------------------------------------
160 // event tables and other macros for wxWindows
161 // ----------------------------------------------------------------------------
162
163 // the event tables connect the wxWindows events with the functions (event
164 // handlers) which process them. It can be also done at run-time, but for the
165 // simple menu events like this the static method is much simpler.
166 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
167 EVT_HTML_LINK_CLICKED(HtmlControl, MyFrame::OnLink)
168 END_EVENT_TABLE()
169
170 // Create a new application object: this macro will allow wxWindows to create
171 // the application object during program execution (it's better than using a
172 // static object for many reasons) and also declares the accessor function
173 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
174 // not wxApp)
175 IMPLEMENT_APP(MyApp)
176
177 // ============================================================================
178 // implementation
179 // ============================================================================
180
181 // ----------------------------------------------------------------------------
182 // the application class
183 // ----------------------------------------------------------------------------
184 // `Main program' equivalent: the program execution "starts" here
185 bool MyApp::OnInit()
186 {
187 wxInitAllImageHandlers();
188
189 // Create the main application window
190 MyFrame *frame = new MyFrame(_("NSIS Menu"),
191 wxPoint(50, 50), wxSize(HTMLW + wxSystemSettings::GetMetric(wxSYS_FRAMESIZE_X), HTMLH + wxSystemSettings::GetMetric(wxSYS_FRAMESIZE_Y)));
192
193 // Show it and tell the application that it's our main window
194
195 frame->SetClientSize(HTMLW, HTMLH);
196 frame->Show(TRUE);
197 SetTopWindow(frame);
198
199 // success: wxApp::OnRun() will be called which will enter the main message
200 // loop and the application will run. If we returned FALSE here, the
201 // application would exit immediately.
202 return TRUE;
203 }
204
205 // ----------------------------------------------------------------------------
206 // main frame
207 // ----------------------------------------------------------------------------
208 #ifdef NSISMENU_NOLINKTAGHANDLER
209 static wxHtmlWindow*g_pHtmlWindow = 0;
210 #endif
211
212 // frame constructor
213 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
214 : wxFrame((wxFrame *)NULL, -1, title, pos, size, wxCLOSE_BOX | wxMINIMIZE_BOX | wxSYSTEM_MENU | wxCAPTION,
215 wxT("nsis_menu"))
216 {
217 m_Html = new wxHtmlWindow(this, HtmlControl, wxPoint(0, 0), wxSize(HTMLW, HTMLH), wxHW_SCROLLBAR_NEVER|wxHW_NO_SELECTION);
218 m_Html->SetRelatedFrame(this, wxT("%s")); // Dialog caption comes from the html title element or filename
219 m_Html->SetBorders(0);
220 m_Html->EnableScrolling(false, false);
221 #ifdef NSISMENU_NOLINKTAGHANDLER
222 g_pHtmlWindow = m_Html;
223 #endif
224
225 // Set font size
226 wxSize DialogSize(1000, 1000);
227 DialogSize = this->ConvertDialogToPixels(DialogSize);
228 int fonts[7] = {0, 0, 14000 / (DialogSize.GetWidth()), 19000 / (DialogSize.GetWidth()), 0, 0, 0};
229 m_Html->SetFonts(wxString(), wxString(), fonts);
230
231 #ifdef __WXMSW__
232 this->SetIcon(wxICON(nsisicon));
233 #else
234 wxIcon icon(wxT("nsisicon.ico"), wxBITMAP_TYPE_ICO);
235 this->SetIcon(icon);
236 #endif
237 m_Html->LoadPage(GetMenuHtmlFile(wxT("index.html")));
238 this->Centre(wxBOTH);
239 }
240
241 // event handler
242
243 void MyFrame::OnLink(wxHtmlLinkEvent& event)
244 {
245 const wxMouseEvent *e = event.GetLinkInfo().GetEvent();
246 if (e == NULL || e->LeftUp())
247 {
248 int notinstalled = false;
249 wxString href = event.GetLinkInfo().GetHref(), url = href;
250 SPECIALURLTYPE ut = TransformUrl(url);
251 switch(ut)
252 {
253 case SUT_BIN:
254 if (PathExists(url)) wxExecute(url); else ++notinstalled;
255 break;
256 case SUT_DOC:
257 if (PathExists(url)) wxLaunchDefaultApplication(url); else ++notinstalled;
258 break;
259 case SUT_WEB:
260 wxLaunchDefaultBrowser(url);
261 break;
262 default:
263 event.Skip();
264 }
265 #ifdef NSISMENU_NOLINKTAGHANDLER
266 if (notinstalled && g_pHtmlWindow)
267 g_pHtmlWindow->LoadPage(GetMenuHtmlFile(wxT("notinstalled.html")));
268 #endif
269 }
270 }
271