1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                    User Interface                     //
9 //                                                       //
10 //                    Program: SAGA                      //
11 //                                                       //
12 //-------------------------------------------------------//
13 //                                                       //
14 //                      SAGA.cpp                         //
15 //                                                       //
16 //          Copyright (C) 2005 by Olaf Conrad            //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'. SAGA is free software; you   //
22 // can redistribute it and/or modify it under the terms  //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the     //
25 // License, or (at your option) any later version.       //
26 //                                                       //
27 // SAGA is distributed in the hope that it will be       //
28 // useful, but WITHOUT ANY WARRANTY; without even the    //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
30 // PARTICULAR PURPOSE. See the GNU General Public        //
31 // License for more details.                             //
32 //                                                       //
33 // You should have received a copy of the GNU General    //
34 // Public License along with this program; if not, see   //
35 // <http://www.gnu.org/licenses/>.                       //
36 //                                                       //
37 //-------------------------------------------------------//
38 //                                                       //
39 //    contact:    Olaf Conrad                            //
40 //                Institute of Geography                 //
41 //                University of Hamburg                  //
42 //                Germany                                //
43 //                                                       //
44 //    e-mail:     oconrad@saga-gis.org                   //
45 //                                                       //
46 ///////////////////////////////////////////////////////////
47 
48 //---------------------------------------------------------
49 #include <wx/config.h>
50 #include <wx/fileconf.h>
51 #include <wx/image.h>
52 #include <wx/splash.h>
53 #include <wx/filename.h>
54 #include <wx/stdpaths.h>
55 #include <wx/wfstream.h>
56 
57 #include <saga_api/saga_api.h>
58 
59 #include "helper.h"
60 
61 #include "res_images.h"
62 
63 #include "saga.h"
64 #include "saga_frame.h"
65 
66 #include "wksp.h"
67 #include "wksp_data_manager.h"
68 
69 
70 ///////////////////////////////////////////////////////////
71 //														 //
72 //														 //
73 //														 //
74 ///////////////////////////////////////////////////////////
75 
76 //---------------------------------------------------------
77 CSAGA	*g_pSAGA	= NULL;
78 
79 //---------------------------------------------------------
80 IMPLEMENT_APP(CSAGA)
81 
82 //---------------------------------------------------------
BEGIN_EVENT_TABLE(CSAGA,wxApp)83 BEGIN_EVENT_TABLE(CSAGA, wxApp)
84 	EVT_KEY_DOWN(CSAGA::On_Key_Down)
85 END_EVENT_TABLE()
86 
87 
88 ///////////////////////////////////////////////////////////
89 //														 //
90 ///////////////////////////////////////////////////////////
91 
92 //---------------------------------------------------------
93 CSAGA::CSAGA(void)
94 {}
95 
96 //---------------------------------------------------------
~CSAGA(void)97 CSAGA::~CSAGA(void)
98 {}
99 
100 
101 ///////////////////////////////////////////////////////////
102 //														 //
103 ///////////////////////////////////////////////////////////
104 
105 //---------------------------------------------------------
OnInit(void)106 bool CSAGA::OnInit(void)
107 {
108 	g_pSAGA	= this;
109 
110 	SetVendorName("www.saga-gis.org");
111 	SetAppName   ("saga_gui");
112 
113 	wxInitAllImageHandlers();
114 
115 	wxFileName	App_Path(argv[0]);	App_Path.MakeAbsolute();
116 
117 	m_App_Path	= App_Path.GetPath();
118 
119 	#if !defined(_DEBUG)
120 		wxSetAssertHandler(NULL);	// disable all wx asserts in SAGA release builds
121 	#endif
122 
123 	/* workaround: wxwidgets 2.9.3 is complaining about setlocale
124 	 * mismatch between c setlocale and wxLocale. since saga has its own
125 	 * translation system, we use english as default. this assures
126 	 * using . as decimal separator in printf like formatting of
127 	 * floating point values. wxXLocale is currently not fully
128 	 * implemented (wxPrintf_l and similar still missing). */
129 	//setlocale(LC_NUMERIC, "C");
130 	m_wxLocale.Init(wxLANGUAGE_ENGLISH);
131 
132 	_Init_Config();
133 
134 	//-----------------------------------------------------
135 	long			lValue;
136 
137 	m_Process_bContinue	= true;
138 	m_Process_Frequency	= CONFIG_Read("/TOOLS", "PROCESS_UPDATE", lValue) ? lValue : 0;
139 
140 	//-----------------------------------------------------
141 	bool	bLogo	= argc <= 1 && CONFIG_Read("/TOOLS", "START_LOGO", bLogo) ? bLogo : true;
142 
143 	wxSplashScreen	*pLogo	= !bLogo ? NULL :
144 		new wxSplashScreen(IMG_Get_Bitmap(ID_IMG_SAGA_SPLASH),
145 			wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_NO_TIMEOUT, 0, NULL, -1,
146 			wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE
147 		);
148 
149 	wxYield();
150 
151 	//-----------------------------------------------------
152 	wxString	File;
153 
154 	if( !CONFIG_Read("/TOOLS", "LNG_FILE_DIC", File) || !wxFileExists(File) )
155 	{
156 		File	= wxFileName(Get_App_Path(), "saga", "lng").GetFullPath();
157 	}
158 
159 	if( !SG_Get_Translator().Create(&File, false) )
160 	{
161 		CONFIG_Delete("/TOOLS", "LNG_FILE_DIC");
162 	}
163 
164 	//-----------------------------------------------------
165 	SG_Initialize_Environment(false, true, NULL, false);
166 
167 	//-----------------------------------------------------
168 	SetTopWindow(new CSAGA_Frame());
169 
170 	//-----------------------------------------------------
171 	if( pLogo )
172 	{
173 		pLogo->Destroy();
174 
175 		wxYield();
176 	}
177 
178 	//-----------------------------------------------------
179 	if( argc > 1 )
180 	{
181 		for(int i=1; i<argc; i++)
182 		{
183 			g_pWKSP->Open(argv[i]);
184 		}
185 	}
186 	else
187 	{
188 		g_pData->Initialise();
189 	}
190 
191 	//-----------------------------------------------------
192 	g_pSAGA_Frame->Show_Tips(false);
193 
194 	return( true );
195 }
196 
197 //---------------------------------------------------------
OnExit(void)198 int CSAGA::OnExit(void)
199 {
200 	delete(wxConfigBase::Set((wxConfigBase *)NULL));
201 
202 	return( 0 );
203 }
204 
205 
206 ///////////////////////////////////////////////////////////
207 //														 //
208 ///////////////////////////////////////////////////////////
209 
210 //---------------------------------------------------------
_Init_Config(void)211 void CSAGA::_Init_Config(void)
212 {
213 	wxConfigBase	*pConfig;
214 
215 	#if defined(_SAGA_MSW)
216 		wxFileName	fLocal(Get_App_Path(), "saga_gui", "ini");
217 
218 		if( ( fLocal.FileExists() && (!fLocal.IsFileReadable() || !fLocal.IsFileWritable()))
219 		||  (!fLocal.FileExists() && (!fLocal.IsDirReadable () || !fLocal.IsDirWritable ())) )
220 		{
221 			wxFileName	fUser (wxGetHomeDir(), "saga_gui", "ini");
222 		//	wxFileName	fUser (wxStandardPaths::Get().GetUserConfigDir(), "saga_gui", "ini");
223 
224 			if(	fLocal.FileExists() && fLocal.IsFileReadable() && !fUser.FileExists() )	// create a copy in user's home directory
225 			{
226 				wxFileInputStream	is(fLocal.GetFullPath());
227 				wxFileOutputStream	os(fUser .GetFullPath());
228 				wxFileConfig		ic(is);	ic.Save(os);
229 			}
230 
231 			fLocal	= fUser;
232 		}
233 
234 		if( (fLocal.FileExists() && fLocal.IsFileWritable()) || (!fLocal.FileExists() && fLocal.IsDirWritable()) )
235 		{
236 			pConfig = new wxFileConfig(wxEmptyString, wxEmptyString, fLocal.GetFullPath(), fLocal.GetFullPath(), wxCONFIG_USE_LOCAL_FILE|wxCONFIG_USE_GLOBAL_FILE|wxCONFIG_USE_RELATIVE_PATH);
237 		}
238 		else
239 		{
240 			pConfig	= new wxConfig;	// this might go to registry !
241 		}
242 	#else
243 		pConfig	= new wxConfig;
244 	#endif
245 
246 	wxConfigBase::Set(pConfig);
247 }
248 
249 
250 ///////////////////////////////////////////////////////////
251 //														 //
252 ///////////////////////////////////////////////////////////
253 
254 //---------------------------------------------------------
On_Key_Down(wxKeyEvent & event)255 void CSAGA::On_Key_Down(wxKeyEvent &event)
256 {
257 	switch( event.GetKeyCode() )
258 	{
259 	default:
260 		event.Skip();
261 		break;
262 
263 	case WXK_ESCAPE:
264 		m_Process_bContinue	= false;
265 		break;
266 	}
267 }
268 
269 //---------------------------------------------------------
Process_Wait(bool bEnforce)270 bool CSAGA::Process_Wait(bool bEnforce)
271 {
272 	static bool			bYield	= false;
273 	static wxDateTime	tYield	= wxDateTime::UNow();
274 
275 	if( !bYield && (bEnforce || m_Process_Frequency <= 0 || m_Process_Frequency <= (wxDateTime::UNow() - tYield).GetMilliseconds()) )
276 	{
277 		bYield	= true;
278 
279 	//	Yield();
280 	//	wxSafeYield(g_pSAGA_Frame);
281 
282 		while( Pending() && Dispatch() );
283 
284 		if( m_Process_Frequency > 0 )
285 		{
286 			tYield	= wxDateTime::UNow();
287 		}
288 
289 		bYield	= false;
290 	}
291 
292 	return( true );
293 }
294 
295 //---------------------------------------------------------
Process_Set_Okay(bool bOkay)296 bool CSAGA::Process_Set_Okay(bool bOkay)
297 {
298 	m_Process_bContinue	= bOkay;
299 
300 	return( m_Process_bContinue );
301 }
302 
303 //---------------------------------------------------------
Process_Get_Okay(void)304 bool CSAGA::Process_Get_Okay(void)
305 {
306 	Process_Wait();
307 
308 	return( m_Process_bContinue );
309 }
310 
311 
312 ///////////////////////////////////////////////////////////
313 //														 //
314 //														 //
315 //														 //
316 ///////////////////////////////////////////////////////////
317 
318 //---------------------------------------------------------
319