1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                    User Interface                     //
9 //                                                       //
10 //                    Program: SAGA                      //
11 //                                                       //
12 //-------------------------------------------------------//
13 //                                                       //
14 //                 wksp_tool_library.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 Goettingen               //
42 //                Goldschmidtstr. 5                      //
43 //                37077 Goettingen                       //
44 //                Germany                                //
45 //                                                       //
46 //    e-mail:     oconrad@saga-gis.org                   //
47 //                                                       //
48 ///////////////////////////////////////////////////////////
49 
50 //---------------------------------------------------------
51 #include <wx/filename.h>
52 
53 #include <saga_api/saga_api.h>
54 
55 #include "res_commands.h"
56 
57 #include "helper.h"
58 
59 #include "wksp_tool_control.h"
60 #include "wksp_tool_manager.h"
61 #include "wksp_tool_library.h"
62 #include "wksp_tool.h"
63 
64 
65 ///////////////////////////////////////////////////////////
66 //														 //
67 //														 //
68 //														 //
69 ///////////////////////////////////////////////////////////
70 
71 //---------------------------------------------------------
CWKSP_Tool_Library(CSG_Tool_Library * pLibrary)72 CWKSP_Tool_Library::CWKSP_Tool_Library(CSG_Tool_Library *pLibrary)
73 {
74 	m_pLibrary	= pLibrary;
75 
76 	_Add_Tools();
77 }
78 
79 //---------------------------------------------------------
~CWKSP_Tool_Library(void)80 CWKSP_Tool_Library::~CWKSP_Tool_Library(void)
81 {
82 	_Del_Tools();
83 
84 	if( MDI_Get_Frame() )	// don't unload library, if gui is closing (i.e. main window == NULL)
85 	{
86 		SG_Get_Tool_Library_Manager().Del_Library(m_pLibrary);
87 	}
88 }
89 
90 
91 ///////////////////////////////////////////////////////////
92 //														 //
93 ///////////////////////////////////////////////////////////
94 
95 //---------------------------------------------------------
Update(void)96 void CWKSP_Tool_Library::Update(void)
97 {
98 	for(int i=Get_Count()-1; i>=0; i--)	// first remove tools that are not available anymore
99 	{
100 		CWKSP_Tool	*pItem	= Get_Tool(i);
101 
102 		if( !m_pLibrary->Get_Tool(pItem->Get_Tool()->Get_Name()) )
103 		{
104 			Del_Item(pItem);
105 
106 			Get_Control()->Delete(pItem->GetId());
107 		}
108 	}
109 
110 	for(int i=0; i<m_pLibrary->Get_Count(); i++)	// then add tools that are not yet present in the list
111 	{
112 		CSG_Tool	*pTool	= m_pLibrary->Get_Tool(i);
113 
114 		if( pTool != NULL && pTool != TLB_INTERFACE_SKIP_TOOL )
115 		{
116 			for(int j=0; j<Get_Count() && pTool; j++)
117 			{
118 				if( pTool == Get_Tool(j)->Get_Tool() )
119 				{
120 					Get_Control()->SetItemText(Get_Tool(j)->GetId(), Get_Tool(j)->Get_Name());	// just in case name has changed
121 
122 					pTool	= NULL;
123 				}
124 			}
125 
126 			if( pTool )
127 			{
128 				CWKSP_Tool	*pItem	= new CWKSP_Tool(pTool, m_pLibrary->Get_Menu().w_str());
129 
130 				Add_Item(pItem);
131 
132 				g_pTool_Ctrl->Add_Tool(GetId(), pItem);
133 			}
134 		}
135 	}
136 
137 	Get_Control()->SortChildren(GetId());
138 }
139 
140 //---------------------------------------------------------
_Add_Tools(void)141 void CWKSP_Tool_Library::_Add_Tools(void)
142 {
143 	for(int i=0; i<m_pLibrary->Get_Count(); i++)
144 	{
145 		CSG_Tool	*pTool	= m_pLibrary->Get_Tool(i);
146 
147 		if( pTool != NULL && pTool != TLB_INTERFACE_SKIP_TOOL )
148 		{
149 			CWKSP_Tool	*pItem	= new CWKSP_Tool(pTool, m_pLibrary->Get_Menu().w_str());
150 
151 			Add_Item(pItem);
152 		}
153 	}
154 }
155 
156 //---------------------------------------------------------
_Del_Tools(void)157 void CWKSP_Tool_Library::_Del_Tools(void)
158 {
159 	for(int i=Get_Count()-1; i>=0; i--)
160 	{
161 		CWKSP_Tool	*pItem	= Get_Tool(i);
162 
163 		Del_Item(pItem);
164 
165 	//	Get_Control()->Delete(pItem->GetId());
166 	//	delete(pItem);
167 	}
168 }
169 
170 
171 
172 ///////////////////////////////////////////////////////////
173 //														 //
174 ///////////////////////////////////////////////////////////
175 
176 //---------------------------------------------------------
Get_Name(void)177 wxString CWKSP_Tool_Library::Get_Name(void)
178 {
179 	return( m_pLibrary->Get_Name().c_str() );
180 }
181 
182 //---------------------------------------------------------
Get_Description(void)183 wxString CWKSP_Tool_Library::Get_Description(void)
184 {
185 	if( g_pTools->Get_Parameter("HELP_SOURCE")->asInt() == 1 )
186 	{
187 		wxString	Description	= Get_Online_Tool_Description(m_pLibrary->Get_File_Name().c_str());
188 
189 		if( !Description.IsEmpty() )
190 		{
191 			return( Description );
192 		}
193 	}
194 
195 	return( m_pLibrary->Get_Summary(SG_SUMMARY_FMT_HTML).c_str() );
196 }
197 
198 //---------------------------------------------------------
Get_Menu(void)199 wxMenu * CWKSP_Tool_Library::Get_Menu(void)
200 {
201 	wxMenu	*pMenu	= new wxMenu(Get_Name());
202 
203 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_OPEN);
204 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_RELOAD);
205 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_WKSP_ITEM_CLOSE);
206 	pMenu->AppendSeparator();
207 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_WKSP_ITEM_SEARCH);
208 
209 	return( pMenu );
210 }
211 
212 
213 ///////////////////////////////////////////////////////////
214 //														 //
215 ///////////////////////////////////////////////////////////
216 
217 //---------------------------------------------------------
On_Command(int Cmd_ID)218 bool CWKSP_Tool_Library::On_Command(int Cmd_ID)
219 {
220 	switch( Cmd_ID )
221 	{
222 	default:
223 		return( CWKSP_Base_Manager::On_Command(Cmd_ID) );
224 
225 	case ID_CMD_WKSP_ITEM_CLOSE:
226 		SG_Get_Tool_Library_Manager().Del_Library(m_pLibrary);
227 		break;
228 
229 	case ID_CMD_WKSP_ITEM_RETURN:
230 		break;
231 	}
232 
233 	return( true );
234 }
235 
236 //---------------------------------------------------------
On_Command_UI(wxUpdateUIEvent & event)237 bool CWKSP_Tool_Library::On_Command_UI(wxUpdateUIEvent &event)
238 {
239 	switch( event.GetId() )
240 	{
241 	default:
242 		return( CWKSP_Base_Manager::On_Command_UI(event) );
243 
244 	case ID_CMD_WKSP_ITEM_CLOSE:
245 		event.Enable(true);
246 		break;
247 	}
248 
249 	return( true );
250 }
251 
252 
253 ///////////////////////////////////////////////////////////
254 //														 //
255 ///////////////////////////////////////////////////////////
256 
257 //---------------------------------------------------------
Get_Tool(CWKSP_Tool * pTool)258 CWKSP_Tool * CWKSP_Tool_Library::Get_Tool(CWKSP_Tool *pTool)
259 {
260 	for(int i=0; i<Get_Count(); i++)
261 	{
262 		if( pTool	== Get_Tool(i) )
263 		{
264 			return( pTool );
265 		}
266 	}
267 
268 	return( NULL );
269 }
270 
271 //---------------------------------------------------------
Get_Tool_byID(int CMD_ID)272 CWKSP_Tool * CWKSP_Tool_Library::Get_Tool_byID(int CMD_ID)
273 {
274 	for(int i=0; i<Get_Count(); i++)
275 	{
276 		if( Get_Tool(i)->Get_Menu_ID() == CMD_ID )
277 		{
278 			return( Get_Tool(i) );
279 		}
280 	}
281 
282 	return( NULL );
283 }
284 
285 
286 ///////////////////////////////////////////////////////////
287 //														 //
288 ///////////////////////////////////////////////////////////
289 
290 //---------------------------------------------------------
is_Valid(void)291 bool CWKSP_Tool_Library::is_Valid(void)
292 {
293 	return( m_pLibrary->is_Valid() );
294 }
295 
296 //---------------------------------------------------------
Get_File_Name(void)297 wxString CWKSP_Tool_Library::Get_File_Name(void)
298 {
299 	return( m_pLibrary->Get_File_Name().c_str() );
300 }
301 
302 
303 ///////////////////////////////////////////////////////////
304 //														 //
305 //														 //
306 //														 //
307 ///////////////////////////////////////////////////////////
308 
309 //---------------------------------------------------------
310