1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                    User Interface                     //
12 //                                                       //
13 //                    Program: SAGA                      //
14 //                                                       //
15 //-------------------------------------------------------//
16 //                                                       //
17 //                 WKSP_Base_Manager.cpp                 //
18 //                                                       //
19 //          Copyright (C) 2005 by Olaf Conrad            //
20 //                                                       //
21 //-------------------------------------------------------//
22 //                                                       //
23 // This file is part of 'SAGA - System for Automated     //
24 // Geoscientific Analyses'. SAGA is free software; you   //
25 // can redistribute it and/or modify it under the terms  //
26 // of the GNU General Public License as published by the //
27 // Free Software Foundation, either version 2 of the     //
28 // License, or (at your option) any later version.       //
29 //                                                       //
30 // SAGA is distributed in the hope that it will be       //
31 // useful, but WITHOUT ANY WARRANTY; without even the    //
32 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
33 // PARTICULAR PURPOSE. See the GNU General Public        //
34 // License for more details.                             //
35 //                                                       //
36 // You should have received a copy of the GNU General    //
37 // Public License along with this program; if not, see   //
38 // <http://www.gnu.org/licenses/>.                       //
39 //                                                       //
40 //-------------------------------------------------------//
41 //                                                       //
42 //    contact:    Olaf Conrad                            //
43 //                Institute of Geography                 //
44 //                University of Goettingen               //
45 //                Goldschmidtstr. 5                      //
46 //                37077 Goettingen                       //
47 //                Germany                                //
48 //                                                       //
49 //    e-mail:     oconrad@saga-gis.org                   //
50 //                                                       //
51 ///////////////////////////////////////////////////////////
52 
53 //---------------------------------------------------------
54 
55 
56 ///////////////////////////////////////////////////////////
57 //														 //
58 //														 //
59 //														 //
60 ///////////////////////////////////////////////////////////
61 
62 //---------------------------------------------------------
63 #include <wx/window.h>
64 #include <wx/debug.h>
65 
66 #include "active.h"
67 #include "active_parameters.h"
68 
69 #include "wksp_tool_control.h"
70 #include "wksp_data_control.h"
71 #include "wksp_data_manager.h"
72 #include "wksp_map_control.h"
73 
74 #include "wksp_base_manager.h"
75 
76 
77 ///////////////////////////////////////////////////////////
78 //														 //
79 //														 //
80 //														 //
81 ///////////////////////////////////////////////////////////
82 
83 //---------------------------------------------------------
CWKSP_Base_Manager(void)84 CWKSP_Base_Manager::CWKSP_Base_Manager(void)
85 {
86 	m_bManager	= true;
87 	m_nItems	= 0;
88 	m_Items		= NULL;
89 	m_Item_ID	= 0;
90 }
91 
92 //---------------------------------------------------------
~CWKSP_Base_Manager(void)93 CWKSP_Base_Manager::~CWKSP_Base_Manager(void)
94 {
95 	wxASSERT_MSG(m_nItems == 0, wxT("CWKSP_Base_Manager: Could not kill all items on destruction!"));
96 }
97 
98 
99 ///////////////////////////////////////////////////////////
100 //														 //
101 ///////////////////////////////////////////////////////////
102 
103 //---------------------------------------------------------
Add_Item(CWKSP_Base_Item * pItem)104 bool CWKSP_Base_Manager::Add_Item(CWKSP_Base_Item *pItem)
105 {
106 	if( pItem )
107 	{
108 		m_Items				= (CWKSP_Base_Item **)realloc(m_Items, (m_nItems + 1) * sizeof(CWKSP_Base_Item *));
109 		m_Items[m_nItems++]	= pItem;
110 		pItem->m_pManager	= this;
111 		pItem->m_ID			= m_Item_ID++;
112 
113 		//-------------------------------------------------
114 		switch( Get_Type() )
115 		{
116 		default:
117 			break;
118 
119 		case WKSP_ITEM_Tool_Manager:
120 			g_pTool_Ctrl->Add_Group((CWKSP_Tool_Group *)pItem);
121 			break;
122 
123 		case WKSP_ITEM_Tool_Group:
124 			g_pTool_Ctrl->Add_Library(GetId(), (CWKSP_Tool_Library *)pItem);
125 			break;
126 
127 		case WKSP_ITEM_Data_Manager:
128 		case WKSP_ITEM_Table_Manager:
129 		case WKSP_ITEM_Shapes_Manager:
130 		case WKSP_ITEM_Shapes_Type:
131 		case WKSP_ITEM_TIN_Manager:
132 		case WKSP_ITEM_PointCloud_Manager:
133 		case WKSP_ITEM_Grid_Manager:
134 		case WKSP_ITEM_Grid_System:
135 			g_pData_Ctrl->Add_Item(this, pItem);
136 			g_pActive->Update_DataObjects();
137 			break;
138 
139 		case WKSP_ITEM_Map_Manager:
140 		case WKSP_ITEM_Map:
141 			g_pMap_Ctrl->Add_Item(this, pItem);
142 			break;
143 		}
144 
145 		return( true );
146 	}
147 
148 	return( false );
149 }
150 
151 //---------------------------------------------------------
Del_Item(int iItem)152 bool CWKSP_Base_Manager::Del_Item(int iItem)
153 {
154 	return( Del_Item(Get_Item(iItem)) );
155 }
156 
Del_Item(CWKSP_Base_Item * pItem)157 bool CWKSP_Base_Manager::Del_Item(CWKSP_Base_Item *pItem)
158 {
159 	int	iItem	= pItem ? pItem->Get_Index() : -1;
160 
161 	if( iItem >= 0 && iItem < m_nItems )
162 	{
163 		m_nItems--;
164 
165 		for( ; iItem<m_nItems; iItem++)
166 		{
167 			m_Items[iItem]	= m_Items[iItem + 1];
168 		}
169 
170 		m_Items	= (CWKSP_Base_Item **)realloc(m_Items, m_nItems * sizeof(CWKSP_Base_Item *));
171 
172 		//-------------------------------------------------
173 		switch( Get_Type() )
174 		{
175 		default:
176 			break;
177 
178 		case WKSP_ITEM_Data_Manager:
179 			g_pData->Del_Manager(pItem);
180 			break;
181 
182 		case WKSP_ITEM_Table_Manager:
183 		case WKSP_ITEM_Shapes_Type:
184 		case WKSP_ITEM_TIN_Manager:
185 		case WKSP_ITEM_PointCloud_Manager:
186 		case WKSP_ITEM_Grid_Manager:
187 		case WKSP_ITEM_Grid_System:
188 			if( g_pActive )
189 			{
190 				g_pActive->Update_DataObjects();
191 			}
192 			break;
193 		}
194 
195 		return( true );
196 	}
197 
198 	return( false );
199 }
200 
201 
202 ///////////////////////////////////////////////////////////
203 //														 //
204 ///////////////////////////////////////////////////////////
205 
206 //---------------------------------------------------------
On_Data_Deletion(CSG_Data_Object * pObject)207 bool CWKSP_Base_Manager::On_Data_Deletion(CSG_Data_Object *pObject)
208 {
209 	CWKSP_Base_Item::On_Data_Deletion(pObject);
210 
211 	for(int iItem=0; iItem<m_nItems; iItem++)
212 	{
213 		m_Items[iItem]->On_Data_Deletion(pObject);
214 	}
215 
216 	return( true );
217 }
218 
219 
220 ///////////////////////////////////////////////////////////
221 //														 //
222 ///////////////////////////////////////////////////////////
223 
224 //---------------------------------------------------------
Move_Top(CWKSP_Base_Item * pItem)225 bool CWKSP_Base_Manager::Move_Top(CWKSP_Base_Item *pItem)
226 {
227 	int		Index, i;
228 
229 	if( pItem && (Index = pItem->Get_Index()) > 0 )
230 	{
231 		for(i=Index; i>0; i--)
232 		{
233 			m_Items[i]	= m_Items[i - 1];
234 		}
235 
236 		m_Items[0]		= pItem;
237 
238 		Get_Control()->SortChildren(GetId());
239 
240 		return( true );
241 	}
242 
243 	return( false );
244 }
245 
246 //---------------------------------------------------------
Move_Bottom(CWKSP_Base_Item * pItem)247 bool CWKSP_Base_Manager::Move_Bottom(CWKSP_Base_Item *pItem)
248 {
249 	int		Index, i;
250 
251 	if( pItem && (Index = pItem->Get_Index()) < Get_Count() - 1 )
252 	{
253 		for(i=Index; i<Get_Count()-1; i++)
254 		{
255 			m_Items[i]	= m_Items[i + 1];
256 		}
257 
258 		m_Items[Get_Count() - 1]	= pItem;
259 
260 		Get_Control()->SortChildren(GetId());
261 
262 		return( true );
263 	}
264 
265 	return( false );
266 }
267 
268 //---------------------------------------------------------
Move_Up(CWKSP_Base_Item * pItem)269 bool CWKSP_Base_Manager::Move_Up(CWKSP_Base_Item *pItem)
270 {
271 	int		Index;
272 
273 	if( pItem && (Index = pItem->Get_Index()) > 0 )
274 	{
275 		m_Items[Index]		= m_Items[Index - 1];
276 		m_Items[Index - 1]	= pItem;
277 
278 		Get_Control()->SortChildren(GetId());
279 
280 		return( true );
281 	}
282 
283 	return( false );
284 }
285 
286 //---------------------------------------------------------
Move_Down(CWKSP_Base_Item * pItem)287 bool CWKSP_Base_Manager::Move_Down(CWKSP_Base_Item *pItem)
288 {
289 	int		Index;
290 
291 	if( pItem && (Index = pItem->Get_Index()) < Get_Count() - 1 )
292 	{
293 		m_Items[Index]		= m_Items[Index + 1];
294 		m_Items[Index + 1]	= pItem;
295 
296 		Get_Control()->SortChildren(GetId());
297 
298 		return( true );
299 	}
300 
301 	return( false );
302 }
303 
304 //---------------------------------------------------------
Move_To(CWKSP_Base_Item * pItem,CWKSP_Base_Item * pItem_Dst)305 bool CWKSP_Base_Manager::Move_To(CWKSP_Base_Item *pItem, CWKSP_Base_Item *pItem_Dst)
306 {
307 	if( pItem && pItem_Dst && pItem != pItem_Dst && pItem->Get_Manager() == this && pItem_Dst->Get_Manager() == this )
308 	{
309 		int		Index, Index_Dst, i;
310 
311 		Index		= pItem		->Get_Index();
312 		Index_Dst	= pItem_Dst	->Get_Index();
313 
314 		if( Index < Index_Dst )
315 		{
316 			for(i=Index; i<Index_Dst; i++)
317 			{
318 				m_Items[i]	= m_Items[i + 1];
319 			}
320 		}
321 		else
322 		{
323 			for(i=Index; i>Index_Dst; i--)
324 			{
325 				m_Items[i]	= m_Items[i - 1];
326 			}
327 		}
328 
329 		m_Items[Index_Dst]	= pItem;
330 
331 		Get_Control()->SortChildren(GetId());
332 
333 		return( true );
334 	}
335 
336 	return( false );
337 }
338 
339 
340 ///////////////////////////////////////////////////////////
341 //														 //
342 ///////////////////////////////////////////////////////////
343 
344 //---------------------------------------------------------
Get_Items_Count(void)345 int CWKSP_Base_Manager::Get_Items_Count(void)
346 {
347 	int	nCount	= 0;
348 
349 	for(int iItem=0; iItem<m_nItems; iItem++)
350 	{
351 		if( m_Items[iItem]->m_bManager )
352 		{
353 			nCount	+= ((CWKSP_Base_Manager *)m_Items[iItem])->Get_Count();
354 		}
355 	}
356 
357 	return( nCount );
358 }
359 
360 
361 ///////////////////////////////////////////////////////////
362 //														 //
363 //														 //
364 //														 //
365 ///////////////////////////////////////////////////////////
366 
367 //---------------------------------------------------------
368