]> git.saurik.com Git - wxWidgets.git/blob - utils/configtool/src/wxconfigtool.cpp
glibc's vswprintf doesn't nul terminate on truncation.
[wxWidgets.git] / utils / configtool / src / wxconfigtool.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxconfigtool.cpp
3 // Purpose: Generic application class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2002-09-04
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence:
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20
21 #include "wx/laywin.h"
22 #include "wx/menuitem.h"
23 #include "wx/tooltip.h"
24 #include "wx/variant.h"
25
26 #endif
27
28 #include "wx/cshelp.h"
29 #include "wx/helphtml.h"
30 #include "wx/html/htmprint.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/image.h"
33 #include "wx/filesys.h"
34 #include "wx/fs_mem.h"
35 #include "wx/fs_zip.h"
36 #include "wx/wfstream.h"
37 #include "wx/config.h"
38 #include "wxconfigtool.h"
39 #include "configtooldoc.h"
40 #include "configtoolview.h"
41 #include "mainframe.h"
42 #include "utils.h"
43
44 IMPLEMENT_APP(ctApp)
45
46 BEGIN_EVENT_TABLE(ctApp, wxApp)
47 END_EVENT_TABLE()
48
49 ctApp::ctApp()
50 {
51 m_helpController = NULL;
52 m_helpControllerReference = NULL;
53 m_docManager = NULL;
54 }
55
56 bool ctApp::OnInit(void)
57 {
58
59 #if wxUSE_LOG
60 wxLog::SetTimestamp(NULL);
61 #endif // wxUSE_LOG
62
63 wxHelpProvider::Set(new wxSimpleHelpProvider);
64
65 #if wxUSE_LIBPNG
66 wxImage::AddHandler( new wxPNGHandler );
67 #endif
68
69 #if wxUSE_LIBJPEG
70 wxImage::AddHandler( new wxJPEGHandler );
71 #endif
72
73 #if wxUSE_GIF
74 wxImage::AddHandler( new wxGIFHandler );
75 #endif
76
77 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
78 m_helpController = new wxCHMHelpController;
79 m_helpControllerReference = new wxCHMHelpController;
80 #else
81 m_helpController = new wxHtmlHelpController;
82 m_helpControllerReference = new wxHtmlHelpController;
83 #endif
84
85 // Required for HTML help
86 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
87 wxFileSystem::AddHandler(new wxZipFSHandler);
88 #endif
89
90 wxString currentDir = wxGetCwd();
91
92 // Use argv to get current app directory
93 wxString argv0(argv[0]);
94 wxString appVariableName(wxT("WXCONFIGTOOLDIR"));
95 m_appDir = apFindAppPath(argv0, currentDir, appVariableName);
96
97 #ifdef __WXMSW__
98 // If the development version, go up a directory.
99 if ((m_appDir.Right(5).CmpNoCase(_T("DEBUG")) == 0) ||
100 (m_appDir.Right(11).CmpNoCase(_T("DEBUGSTABLE")) == 0) ||
101 (m_appDir.Right(7).CmpNoCase(_T("RELEASE")) == 0) ||
102 (m_appDir.Right(13).CmpNoCase(_T("RELEASESTABLE")) == 0) ||
103 (m_appDir.Right(10).CmpNoCase(_T("RELEASEDEV")) == 0) ||
104 (m_appDir.Right(8).CmpNoCase(_T("DEBUGDEV")) == 0)
105 )
106 m_appDir = wxPathOnly(m_appDir);
107 #endif
108
109 m_docManager = new wxDocManager;
110 m_docManager->SetMaxDocsOpen(1);
111
112 //// Create a template relating documents to their views
113 (void) new wxDocTemplate(m_docManager, wxGetApp().GetSettings().GetShortAppName(), wxT("*.wxs"), wxT(""), wxT("wxs"), wxT("Doc"), wxT("View"),
114 CLASSINFO(ctConfigToolDoc), CLASSINFO(ctConfigToolView));
115
116 m_settings.Init();
117
118 LoadConfig();
119
120 wxString helpFilePathReference(GetFullAppPath(_("wx")));
121 m_helpControllerReference->Initialize(helpFilePathReference);
122
123 wxString helpFilePath(GetFullAppPath(_("configtool")));
124 m_helpController->Initialize(helpFilePath);
125
126 ctMainFrame* frame = new ctMainFrame(m_docManager, NULL, wxID_ANY, wxGetApp().GetSettings().GetAppName(),
127 GetSettings().m_frameSize.GetPosition(), GetSettings().m_frameSize.GetSize(),
128 wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN);
129 SetTopWindow(frame);
130
131 switch (wxGetApp().GetSettings().m_frameStatus)
132 {
133 case ctSHOW_STATUS_MAXIMIZED:
134 {
135 frame->Maximize(true);
136 break;
137 }
138 case ctSHOW_STATUS_MINIMIZED:
139 {
140 frame->Iconize(true);
141 break;
142 }
143 default:
144 case ctSHOW_STATUS_NORMAL:
145 {
146 break;
147 }
148 }
149
150 // Load the file history. This has to be done AFTER the
151 // main frame has been created, so that the items
152 // will get appended to the file menu.
153 {
154 wxConfig config(wxGetApp().GetSettings().GetAppName(), wxT("Generic Organisation"));
155 GetFileHistory().Load(config);
156 }
157
158 if (argc > 1)
159 {
160 // Concatenate strings since it could have spaces (and quotes)
161 wxString arg;
162 int i;
163 for (i = 1; i < argc; i++)
164 {
165 arg += argv[i];
166 if (i < (argc - 1))
167 arg += wxString(wxT(" "));
168 }
169 if (arg[0u] == '"')
170 arg = arg.Mid(1);
171 if (arg.Last() == '"')
172 arg = arg.Mid(0, arg.Len() - 1);
173
174 // Load the file
175 wxDocument* doc = m_docManager->CreateDocument(arg, wxDOC_SILENT);
176 if (doc)
177 doc->SetDocumentSaved(true);
178 }
179 else
180 {
181 if (GetSettings().m_loadLastDocument)
182 {
183 // Load the file that was last loaded
184 wxDocument* doc = m_docManager->CreateDocument(GetSettings().m_lastFilename, wxDOC_SILENT);
185 if (doc)
186 doc->SetDocumentSaved(true);
187 }
188 }
189
190 GetTopWindow()->Show(true);
191
192 return true;
193 }
194
195 int ctApp::OnExit(void)
196 {
197 SaveConfig();
198
199 // Save the file history
200 {
201 wxConfig config(wxGetApp().GetSettings().GetAppName(), wxT("Generic Organisation"));
202 GetFileHistory().Save(config);
203 }
204
205 delete m_docManager;
206 m_docManager = NULL;
207
208 return 0;
209 }
210
211 void ctApp::ClearHelpControllers()
212 {
213 delete m_helpController;
214 m_helpController = NULL;
215
216 delete m_helpControllerReference;
217 m_helpControllerReference = NULL;
218 }
219
220
221 // Prepend the current program directory to the name
222 wxString ctApp::GetFullAppPath(const wxString& filename) const
223 {
224 wxString path(m_appDir);
225 if (path.Last() != wxFILE_SEP_PATH && filename[0] != wxFILE_SEP_PATH)
226 path += wxFILE_SEP_PATH;
227 path += filename;
228
229 return path;
230 }
231
232 // Load config info
233 bool ctApp::LoadConfig()
234 {
235 return m_settings.LoadConfig();
236 }
237
238 // Save config info
239 bool ctApp::SaveConfig()
240 {
241 return m_settings.SaveConfig();
242 }
243
244 bool ctApp::UsingTooltips()
245 {
246 return GetSettings().m_useToolTips;
247 }
248
249 /// Returns the main frame
250 ctMainFrame* ctApp::GetMainFrame()
251 {
252 return wxDynamicCast(wxTheApp->GetTopWindow(), ctMainFrame);
253 }