1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //           Application Programming Interface           //
9 //                                                       //
10 //                  Library: SAGA_API                    //
11 //                                                       //
12 //-------------------------------------------------------//
13 //                                                       //
14 //                   api_callback.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'.                              //
22 //                                                       //
23 // This library is free software; you can redistribute   //
24 // it and/or modify it under the terms of the GNU Lesser //
25 // General Public License as published by the Free       //
26 // Software Foundation, either version 2.1 of the        //
27 // License, or (at your option) any later version.       //
28 //                                                       //
29 // This library is distributed in the hope that it will  //
30 // be useful, but WITHOUT ANY WARRANTY; without even the //
31 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
32 // PARTICULAR PURPOSE. See the GNU Lesser General Public //
33 // License for more details.                             //
34 //                                                       //
35 // You should have received a copy of the GNU Lesser     //
36 // General Public License along with this program; if    //
37 // not, see <http://www.gnu.org/licenses/>.              //
38 //                                                       //
39 //-------------------------------------------------------//
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Goettingen               //
44 //                Goldschmidtstr. 5                      //
45 //                37077 Goettingen                       //
46 //                Germany                                //
47 //                                                       //
48 //    e-mail:     oconrad@saga-gis.org                   //
49 //                                                       //
50 ///////////////////////////////////////////////////////////
51 
52 //---------------------------------------------------------
53 #include <wx/stdpaths.h>
54 
55 #include "api_core.h"
56 #include "grid.h"
57 #include "parameters.h"
58 
59 
60 ///////////////////////////////////////////////////////////
61 //														 //
62 //														 //
63 //														 //
64 ///////////////////////////////////////////////////////////
65 
66 //---------------------------------------------------------
67 #if defined(_SAGA_MSW)
68 	#define CONSOLE_STDIO      printf(        CSG_String::Format
69 	#define CONSOLE_STDERR    fprintf(stderr, CSG_String::Format
70 #else
71 	#define CONSOLE_STDIO   SG_Printf(        CSG_String::Format
72 	#define CONSOLE_STDERR SG_FPrintf(stderr, CSG_String::Format
73 #endif
74 
75 
76 ///////////////////////////////////////////////////////////
77 //														 //
78 //														 //
79 //														 //
80 ///////////////////////////////////////////////////////////
81 
82 //---------------------------------------------------------
83 TSG_PFNC_UI_Callback	gSG_UI_Callback	= NULL;
84 
85 //---------------------------------------------------------
SG_Set_UI_Callback(TSG_PFNC_UI_Callback Function)86 bool					SG_Set_UI_Callback(TSG_PFNC_UI_Callback Function)
87 {
88 	gSG_UI_Callback	= Function;
89 
90 	return( true );
91 }
92 
93 //---------------------------------------------------------
SG_Get_UI_Callback(void)94 TSG_PFNC_UI_Callback	SG_Get_UI_Callback(void)
95 {
96 	return( gSG_UI_Callback );
97 }
98 
99 
100 ///////////////////////////////////////////////////////////
101 //														 //
102 //														 //
103 //														 //
104 ///////////////////////////////////////////////////////////
105 
CSG_UI_Parameter(const CSG_UI_Parameter & Copy)106 CSG_UI_Parameter::CSG_UI_Parameter(const CSG_UI_Parameter &Copy)
107 {
108 	Boolean	= Copy.Boolean;
109 	Number	= Copy.Number ;
110 	Pointer	= Copy.Pointer;
111 	String	= Copy.String ;
112 }
113 
114 
115 ///////////////////////////////////////////////////////////
116 //														 //
117 //														 //
118 //														 //
119 ///////////////////////////////////////////////////////////
120 
121 //---------------------------------------------------------
122 int					gSG_UI_Progress_Lock	= 0;
123 
124 //---------------------------------------------------------
SG_UI_Progress_Lock(bool bOn)125 int			SG_UI_Progress_Lock(bool bOn)
126 {
127 	if( bOn )
128 	{
129 		gSG_UI_Progress_Lock++;
130 	}
131 	else if( gSG_UI_Progress_Lock > 0 )
132 	{
133 		gSG_UI_Progress_Lock--;
134 	}
135 
136 	return( gSG_UI_Progress_Lock );
137 }
138 
139 //---------------------------------------------------------
SG_UI_Progress_Reset(void)140 int			SG_UI_Progress_Reset(void)
141 {
142 	int Locked = gSG_UI_Progress_Lock;
143 
144 	gSG_UI_Progress_Lock = 0;
145 
146 	return( Locked );
147 }
148 
149 //---------------------------------------------------------
SG_UI_Process_Get_Okay(bool bBlink)150 bool		SG_UI_Process_Get_Okay(bool bBlink)
151 {
152 	if( gSG_UI_Callback )
153 	{
154 		CSG_UI_Parameter	p1(gSG_UI_Progress_Lock == 0 && bBlink), p2;
155 
156 		return( gSG_UI_Callback(CALLBACK_PROCESS_GET_OKAY, p1, p2) != 0 );
157 	}
158 
159 	if( gSG_UI_Progress_Lock == 0 && bBlink )
160 	{
161 		static const SG_Char	Buisy[4]	= {	'|', '/', '-', '\\'	};
162 
163 		static int	iBuisy	= 0;
164 
165 		CONSOLE_STDIO("\r%c   ", Buisy[iBuisy++]));
166 
167 		iBuisy	%= 4;
168 	}
169 
170 	return( true );
171 }
172 
173 //---------------------------------------------------------
SG_UI_Process_Set_Okay(bool bOkay)174 bool		SG_UI_Process_Set_Okay(bool bOkay)
175 {
176 	if( gSG_UI_Progress_Lock == 0 && gSG_UI_Callback )
177 	{
178 		CSG_UI_Parameter	p1(bOkay), p2;
179 
180 		return( gSG_UI_Callback(CALLBACK_PROCESS_SET_OKAY, p1, p2) != 0 );
181 	}
182 
183 	return( true );
184 }
185 
186 //---------------------------------------------------------
SG_UI_Process_Set_Progress(double Position,double Range)187 bool		SG_UI_Process_Set_Progress(double Position, double Range)
188 {
189 	if( gSG_UI_Progress_Lock > 0 )
190 	{
191 		return( SG_UI_Process_Get_Okay() );
192 	}
193 
194 	if( gSG_UI_Callback )
195 	{
196 		CSG_UI_Parameter	p1(Position), p2(Range);
197 
198 		return( gSG_UI_Callback(CALLBACK_PROCESS_SET_PROGRESS, p1, p2) != 0 );
199 	}
200 
201 	//-----------------------------------------------------
202 	static int	iPercent	= -1;
203 
204 	int	i	= Position < 0.0 ? -1 : Range > 0.0 ? 1 + (int)(100.0 * Position / Range) : 100;
205 
206 	if( iPercent != i )
207 	{
208 		if( iPercent < 0 || i < iPercent )
209 		{
210 			CONSOLE_STDIO("\n"));
211 		}
212 
213 		iPercent	= i;
214 
215 		if( iPercent >= 0 )
216 		{
217 			CONSOLE_STDIO("\r%3d%%", iPercent > 100 ? 100 : iPercent));
218 		}
219 	}
220 
221 	return( true );
222 }
223 
224 //---------------------------------------------------------
SG_UI_Process_Set_Ready(void)225 bool		SG_UI_Process_Set_Ready(void)
226 {
227 	if( gSG_UI_Callback )
228 	{
229 		if( gSG_UI_Progress_Lock == 0 )
230 		{
231 			CSG_UI_Parameter	p1, p2;
232 
233 			return( gSG_UI_Callback(CALLBACK_PROCESS_SET_READY, p1, p2) != 0 );
234 		}
235 	}
236 
237 	SG_UI_Process_Set_Progress(-1, -1);
238 
239 	return( true );
240 }
241 
242 //---------------------------------------------------------
SG_UI_Process_Set_Text(const CSG_String & Text)243 void		SG_UI_Process_Set_Text(const CSG_String &Text)
244 {
245 	if( gSG_UI_Progress_Lock == 0 )
246 	{
247 		if( gSG_UI_Callback )
248 		{
249 			CSG_UI_Parameter	p1(Text), p2;
250 
251 			gSG_UI_Callback(CALLBACK_PROCESS_SET_TEXT, p1, p2);
252 		}
253 		else
254 		{
255 			CONSOLE_STDIO("%s\n", Text.c_str()));
256 		}
257 	}
258 }
259 
260 
261 ///////////////////////////////////////////////////////////
262 //														 //
263 //														 //
264 //														 //
265 ///////////////////////////////////////////////////////////
266 
267 //---------------------------------------------------------
SG_UI_Stop_Execution(bool bDialog)268 bool		SG_UI_Stop_Execution(bool bDialog)
269 {
270 	if( gSG_UI_Callback )
271 	{
272 		CSG_UI_Parameter	p1(bDialog), p2;
273 
274 		return( gSG_UI_Callback(CALLBACK_STOP_EXECUTION, p1, p2) != 0 );
275 	}
276 
277 	return( false );
278 }
279 
280 
281 ///////////////////////////////////////////////////////////
282 //														 //
283 //														 //
284 //														 //
285 ///////////////////////////////////////////////////////////
286 
287 //---------------------------------------------------------
SG_UI_Dlg_Message(const CSG_String & Message,const CSG_String & Caption)288 void		SG_UI_Dlg_Message(const CSG_String &Message, const CSG_String &Caption)
289 {
290 	if( gSG_UI_Progress_Lock == 0 )
291 	{
292 		if( gSG_UI_Callback )
293 		{
294 			CSG_UI_Parameter	p1(Message), p2(Caption);
295 
296 			gSG_UI_Callback(CALLBACK_DLG_MESSAGE, p1, p2);
297 		}
298 		else
299 		{
300 			CONSOLE_STDIO("%s: %s\n", Caption.c_str(), Message.c_str()));
301 		}
302 	}
303 }
304 
305 //---------------------------------------------------------
SG_UI_Dlg_Continue(const CSG_String & Message,const CSG_String & Caption)306 bool		SG_UI_Dlg_Continue(const CSG_String &Message, const CSG_String &Caption)
307 {
308 	if( gSG_UI_Progress_Lock == 0 )
309 	{
310 		if( gSG_UI_Callback )
311 		{
312 			CSG_UI_Parameter	p1(Message), p2(Caption);
313 
314 			return( gSG_UI_Callback(CALLBACK_DLG_CONTINUE, p1, p2) != 0 );
315 		}
316 	}
317 
318 	return( true );
319 }
320 
321 //---------------------------------------------------------
SG_UI_Dlg_Error(const CSG_String & Message,const CSG_String & Caption)322 int			SG_UI_Dlg_Error(const CSG_String &Message, const CSG_String &Caption)
323 {
324 	if( gSG_UI_Progress_Lock != 0 )
325 	{
326 		SG_UI_Msg_Add_Error(Caption);
327 		SG_UI_Msg_Add_Error(Message);
328 
329 		return( 0 );
330 	}
331 
332 	if( gSG_UI_Callback )
333 	{
334 		CSG_UI_Parameter	p1(Message), p2(Caption);
335 
336 		return( gSG_UI_Callback(CALLBACK_DLG_ERROR, p1, p2) );
337 	}
338 
339 	return( 0 );
340 }
341 
342 
343 ///////////////////////////////////////////////////////////
344 //														 //
345 //														 //
346 //														 //
347 ///////////////////////////////////////////////////////////
348 
349 //---------------------------------------------------------
SG_UI_Dlg_Parameters(CSG_Parameters * pParameters,const CSG_String & Caption)350 bool		SG_UI_Dlg_Parameters(CSG_Parameters *pParameters, const CSG_String &Caption)
351 {
352 	if( gSG_UI_Callback && pParameters )
353 	{
354 		CSG_UI_Parameter	p1(pParameters), p2(Caption);
355 
356 		return( gSG_UI_Callback(CALLBACK_DLG_PARAMETERS, p1, p2) != 0 );
357 	}
358 
359 	return( true );
360 }
361 
362 
363 ///////////////////////////////////////////////////////////
364 //														 //
365 //														 //
366 //														 //
367 ///////////////////////////////////////////////////////////
368 
369 //---------------------------------------------------------
370 int			gSG_UI_Msg_Lock	= 0;
371 
372 //---------------------------------------------------------
SG_UI_Msg_Lock(bool bOn)373 int			SG_UI_Msg_Lock(bool bOn)
374 {
375 	if( bOn )
376 	{
377 		gSG_UI_Msg_Lock++;
378 	}
379 	else if( gSG_UI_Msg_Lock > 0 )
380 	{
381 		gSG_UI_Msg_Lock--;
382 	}
383 
384 	return( gSG_UI_Msg_Lock );
385 }
386 
387 //---------------------------------------------------------
SG_UI_Msg_Reset(void)388 int			SG_UI_Msg_Reset(void)
389 {
390 	int Locked = gSG_UI_Msg_Lock;
391 
392 	gSG_UI_Msg_Lock = 0;
393 
394 	return( Locked );
395 }
396 
397 //---------------------------------------------------------
SG_UI_Msg_Add(const CSG_String & Message,bool bNewLine,TSG_UI_MSG_STYLE Style)398 void		SG_UI_Msg_Add(const CSG_String &Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
399 {
400 	if( gSG_UI_Msg_Lock )
401 		return;
402 
403 	if( gSG_UI_Callback )
404 	{
405 		int		Parameters[2];
406 
407 		Parameters[0]	= bNewLine ? 1 : 0;
408 		Parameters[1]	= Style;
409 
410 		CSG_UI_Parameter	p1(Message), p2(Parameters);
411 
412 		gSG_UI_Callback(CALLBACK_MESSAGE_ADD, p1, p2);
413 	}
414 	else
415 	{
416 		CONSOLE_STDIO("%s\n", Message.c_str()));
417 	}
418 }
419 
420 //---------------------------------------------------------
SG_UI_Msg_Add_Error(const CSG_String & Message)421 void		SG_UI_Msg_Add_Error(const CSG_String &Message)
422 {
423 	if( gSG_UI_Msg_Lock )
424 		return;
425 
426 	if( gSG_UI_Callback )
427 	{
428 		CSG_UI_Parameter	p1(Message), p2;
429 
430 		gSG_UI_Callback(CALLBACK_MESSAGE_ADD_ERROR, p1, p2);
431 	}
432 	else
433 	{
434 		CONSOLE_STDERR("%s: %s\n", _TL("Error"), Message.c_str()));
435 	}
436 }
437 
438 //---------------------------------------------------------
SG_UI_Msg_Add_Execution(const CSG_String & Message,bool bNewLine,TSG_UI_MSG_STYLE Style)439 void		SG_UI_Msg_Add_Execution(const CSG_String &Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
440 {
441 	if( gSG_UI_Msg_Lock )
442 		return;
443 
444 	if( gSG_UI_Callback )
445 	{
446 		int		Parameters[2];
447 
448 		Parameters[0]	= bNewLine ? 1 : 0;
449 		Parameters[1]	= Style;
450 
451 		CSG_UI_Parameter	p1(Message), p2(Parameters);
452 
453 		gSG_UI_Callback(CALLBACK_MESSAGE_ADD_EXECUTION, p1, p2);
454 	}
455 	else
456 	{
457 		CONSOLE_STDIO("%s\n", Message.c_str()));
458 	}
459 }
460 
461 
462 ///////////////////////////////////////////////////////////
463 //														 //
464 //														 //
465 //														 //
466 ///////////////////////////////////////////////////////////
467 
468 //---------------------------------------------------------
SG_UI_ProgressAndMsg_Lock(bool bOn)469 void		SG_UI_ProgressAndMsg_Lock	(bool bOn)
470 {
471 	SG_UI_Progress_Lock(bOn);
472 	SG_UI_Msg_Lock     (bOn);
473 }
474 
475 //---------------------------------------------------------
SG_UI_ProgressAndMsg_Reset(void)476 void		SG_UI_ProgressAndMsg_Reset	(void)
477 {
478 	SG_UI_Progress_Reset();
479 	SG_UI_Msg_Reset     ();
480 }
481 
482 
483 ///////////////////////////////////////////////////////////
484 //														 //
485 //														 //
486 //														 //
487 ///////////////////////////////////////////////////////////
488 
489 //---------------------------------------------------------
SG_UI_DataObject_Add(CSG_Data_Object * pDataObject,int Show)490 bool		SG_UI_DataObject_Add(CSG_Data_Object *pDataObject, int Show)
491 {
492 	if( gSG_UI_Callback && pDataObject )
493 	{
494 		CSG_UI_Parameter	p1(pDataObject), p2(Show ? true : false);
495 
496 		return( gSG_UI_Callback(CALLBACK_DATAOBJECT_ADD, p1, p2) != 0 );
497 	}
498 
499 	return( false );
500 }
501 
502 //---------------------------------------------------------
SG_UI_DataObject_Update(CSG_Data_Object * pDataObject,int Show,CSG_Parameters * pParameters)503 bool		SG_UI_DataObject_Update(CSG_Data_Object *pDataObject, int Show, CSG_Parameters *pParameters)
504 {
505 	if( gSG_UI_Callback && pDataObject )
506 	{
507 		CSG_UI_Parameter	p1(pDataObject->Get_Owner() ? pDataObject->Get_Owner() : pDataObject), p2(pParameters);
508 
509 		if( gSG_UI_Callback(CALLBACK_DATAOBJECT_UPDATE, p1, p2) != 0 )
510 		{
511 			if( Show != SG_UI_DATAOBJECT_UPDATE_ONLY )
512 			{
513 				SG_UI_DataObject_Show(pDataObject, Show);
514 			}
515 
516 			return( true );
517 		}
518 	}
519 
520 	return( false );
521 }
522 
523 //---------------------------------------------------------
SG_UI_DataObject_Show(CSG_Data_Object * pDataObject,int Show)524 bool		SG_UI_DataObject_Show(CSG_Data_Object *pDataObject, int Show)
525 {
526 	if( gSG_UI_Callback && pDataObject )
527 	{
528 		CSG_UI_Parameter	p1(pDataObject), p2(Show);
529 
530 		return( gSG_UI_Callback(CALLBACK_DATAOBJECT_SHOW, p1, p2) != 0 );
531 	}
532 
533 	return( false );
534 }
535 
536 //---------------------------------------------------------
SG_UI_DataObject_asImage(CSG_Data_Object * pDataObject,CSG_Grid * pGrid)537 bool		SG_UI_DataObject_asImage(CSG_Data_Object *pDataObject, CSG_Grid *pGrid)
538 {
539 	if( gSG_UI_Callback && pDataObject )
540 	{
541 		CSG_UI_Parameter	p1(pDataObject), p2(pGrid);
542 
543 		return( gSG_UI_Callback(CALLBACK_DATAOBJECT_ASIMAGE, p1, p2) != 0 );
544 	}
545 
546 	return( false );
547 }
548 
549 //---------------------------------------------------------
SG_UI_DataObject_Colors_Get(CSG_Data_Object * pDataObject,CSG_Colors * pColors)550 bool		SG_UI_DataObject_Colors_Get(CSG_Data_Object *pDataObject, CSG_Colors *pColors)
551 {
552 	if( gSG_UI_Callback && pDataObject && pColors )
553 	{
554 		CSG_UI_Parameter	p1(pDataObject), p2(pColors);
555 
556 		return( gSG_UI_Callback(CALLBACK_DATAOBJECT_COLORS_GET, p1, p2) != 0 );
557 	}
558 
559 	return( false );
560 }
561 
562 //---------------------------------------------------------
SG_UI_DataObject_Colors_Set(CSG_Data_Object * pDataObject,CSG_Colors * pColors)563 bool		SG_UI_DataObject_Colors_Set(CSG_Data_Object *pDataObject, CSG_Colors *pColors)
564 {
565 	if( gSG_UI_Progress_Lock == 0 && gSG_UI_Callback && pDataObject && pColors )
566 	{
567 		CSG_UI_Parameter	p1(pDataObject), p2(pColors);
568 
569 		return( gSG_UI_Callback(CALLBACK_DATAOBJECT_COLORS_SET, p1, p2) != 0 );
570 	}
571 
572 	return( false );
573 }
574 
575 //---------------------------------------------------------
SG_UI_DataObject_Params_Get(CSG_Data_Object * pDataObject,CSG_Parameters * pParameters)576 bool		SG_UI_DataObject_Params_Get	(CSG_Data_Object *pDataObject, CSG_Parameters *pParameters)
577 {
578 	if( gSG_UI_Callback && pDataObject && pParameters )
579 	{
580 		CSG_UI_Parameter	p1(pDataObject), p2(pParameters);
581 
582 		return( gSG_UI_Callback(CALLBACK_DATAOBJECT_PARAMS_GET, p1, p2) != 0 );
583 	}
584 
585 	return( false );
586 }
587 
588 //---------------------------------------------------------
SG_UI_DataObject_Params_Set(CSG_Data_Object * pDataObject,CSG_Parameters * pParameters)589 bool		SG_UI_DataObject_Params_Set	(CSG_Data_Object *pDataObject, CSG_Parameters *pParameters)
590 {
591 	if( gSG_UI_Progress_Lock == 0 && gSG_UI_Callback && pDataObject && pParameters )
592 	{
593 		CSG_UI_Parameter	p1(pDataObject), p2(pParameters);
594 
595 		return( gSG_UI_Callback(CALLBACK_DATAOBJECT_PARAMS_SET, p1, p2) != 0 );
596 	}
597 
598 	return( false );
599 }
600 
601 //---------------------------------------------------------
SG_UI_ODBC_Update(const CSG_String & Server)602 bool		SG_UI_ODBC_Update		(const CSG_String &Server)
603 {
604 	if( gSG_UI_Callback )
605 	{
606 		CSG_UI_Parameter	p1(Server), p2;
607 
608 		return( gSG_UI_Callback(CALLBACK_DATABASE_UPDATE, p1, p2) != 0 );
609 	}
610 
611 	return( false );
612 }
613 
614 
615 ///////////////////////////////////////////////////////////
616 //														 //
617 //														 //
618 //														 //
619 ///////////////////////////////////////////////////////////
620 
621 //---------------------------------------------------------
SG_UI_Get_Window_Main(void)622 void *		SG_UI_Get_Window_Main(void)
623 {
624 	if( gSG_UI_Callback )
625 	{
626 		CSG_UI_Parameter	p1, p2;
627 
628 		gSG_UI_Callback(CALLBACK_GUI_GET_WINDOW, p1, p2);
629 
630 		return( p1.Pointer );
631 	}
632 
633 	return( NULL );
634 }
635 
636 //---------------------------------------------------------
SG_UI_Get_Application_Path(bool bPathOnly)637 CSG_String	SG_UI_Get_Application_Path(bool bPathOnly)
638 {
639 	CSG_String	App_Path(wxStandardPaths::Get().GetExecutablePath().wc_str());
640 
641 	if( bPathOnly )
642 	{
643 		App_Path = SG_File_Get_Path(App_Path);
644 	}
645 
646 	return( SG_File_Get_Path_Absolute(App_Path) );
647 }
648 
649 
650 ///////////////////////////////////////////////////////////
651 //														 //
652 //														 //
653 //														 //
654 ///////////////////////////////////////////////////////////
655 
656 //---------------------------------------------------------
657