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_Data_Layers.cpp                  //
15 //                                                       //
16 //          Copyright (C) 2006 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 //                Germany                                //
43 //                                                       //
44 //    e-mail:     oconrad@saga-gis.org                   //
45 //                                                       //
46 ///////////////////////////////////////////////////////////
47 
48 //---------------------------------------------------------
49 #include <wx/tooltip.h>
50 
51 #include <saga_api/saga_api.h>
52 #include <saga_gdi/sgdi_helper.h>
53 
54 #include "res_controls.h"
55 #include "res_commands.h"
56 #include "res_dialogs.h"
57 #include "res_images.h"
58 
59 #include "helper.h"
60 
61 #include "active.h"
62 
63 #include "wksp_data_control.h"
64 #include "wksp_data_manager.h"
65 #include "wksp_layer.h"
66 #include "wksp_map.h"
67 #include "wksp_map_layer.h"
68 
69 #include "wksp_data_layers.h"
70 
71 
72 ///////////////////////////////////////////////////////////
73 //														 //
74 //														 //
75 //														 //
76 ///////////////////////////////////////////////////////////
77 
78 //---------------------------------------------------------
IMPLEMENT_CLASS(CWKSP_Data_Button,wxPanel)79 IMPLEMENT_CLASS(CWKSP_Data_Button, wxPanel)
80 
81 //---------------------------------------------------------
82 BEGIN_EVENT_TABLE(CWKSP_Data_Button, wxPanel)
83 	EVT_PAINT      (CWKSP_Data_Button::On_Paint)
84 	EVT_KEY_DOWN   (CWKSP_Data_Button::On_Key)
85 	EVT_LEFT_DOWN  (CWKSP_Data_Button::On_Mouse_LDown)
86 	EVT_LEFT_DCLICK(CWKSP_Data_Button::On_Mouse_LDClick)
87 	EVT_RIGHT_DOWN (CWKSP_Data_Button::On_Mouse_RDown)
88 END_EVENT_TABLE()
89 
90 
91 ///////////////////////////////////////////////////////////
92 //														 //
93 ///////////////////////////////////////////////////////////
94 
95 //---------------------------------------------------------
96 CWKSP_Data_Button::CWKSP_Data_Button(wxWindow *pParent, CWKSP_Base_Item *pItem)
97 	: wxPanel(pParent, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_RAISED)
98 {
99 	m_pItem		= pItem;
100 
101 	if( is_Manager() )
102 	{
103 		wxClientDC	dc(this);
104 
105 		SetSize(wxDefaultSize.GetWidth(), dc.GetTextExtent(m_pItem->Get_Name()).GetHeight() + 10);
106 	}
107 }
108 
109 
110 ///////////////////////////////////////////////////////////
111 //														 //
112 ///////////////////////////////////////////////////////////
113 
114 //---------------------------------------------------------
is_Manager(void)115 bool CWKSP_Data_Button::is_Manager(void)
116 {
117 	return( m_pItem && m_pItem->is_Manager() );
118 }
119 
120 
121 ///////////////////////////////////////////////////////////
122 //														 //
123 ///////////////////////////////////////////////////////////
124 
125 //---------------------------------------------------------
On_Paint(wxPaintEvent & event)126 void CWKSP_Data_Button::On_Paint(wxPaintEvent &event)
127 {
128 	if( m_pItem )
129 	{
130 		wxPaintDC	dc(this);
131 
132 		//-------------------------------------------------
133 		if( is_Manager() )
134 		{
135 			dc.DrawText(m_pItem->Get_Name(), 2, 2);
136 		}
137 
138 		//-------------------------------------------------
139 		else if( ((CWKSP_Data_Item *)m_pItem)->Get_Object() )
140 		{
141 			if( !GetToolTip() || GetToolTip()->GetTip().Cmp(m_pItem->Get_Name()) )
142 			{
143 				SetToolTip(m_pItem->Get_Name());
144 			}
145 
146 			wxRect	r(GetClientRect());
147 
148 			if( m_pItem->Get_Type() == WKSP_ITEM_Table )
149 			{
150 				dc.DrawBitmap(wxBitmap(IMG_Get_Bitmap(ID_IMG_WKSP_TABLE, r.GetWidth() - 1)),
151 					r.GetLeft(), r.GetTop(), true
152 				);
153 			}
154 			else
155 			{
156 				dc.DrawBitmap(((CWKSP_Layer *)m_pItem)->Get_Thumbnail(r.GetWidth() - 1, r.GetHeight() - 1),
157 					r.GetLeft(), r.GetTop(), true
158 				);
159 			}
160 
161 			//---------------------------------------------
162 			if( m_pItem->is_Selected() )
163 			{
164 				dc.SetPen(wxPen(Get_Color_asWX(g_pData->Get_Parameter("THUMBNAIL_SELCOLOR")->asColor())));
165 
166 				Draw_Edge(dc, EDGE_STYLE_SIMPLE, r); r.Deflate(1);
167 				Draw_Edge(dc, EDGE_STYLE_SIMPLE, r); r.Deflate(1);
168 				Draw_Edge(dc, EDGE_STYLE_SIMPLE, r);
169 			}
170 		}
171 	}
172 }
173 
174 
175 ///////////////////////////////////////////////////////////
176 //														 //
177 ///////////////////////////////////////////////////////////
178 
179 //---------------------------------------------------------
On_Key(wxKeyEvent & event)180 void CWKSP_Data_Button::On_Key(wxKeyEvent &event)
181 {
182 	wxCommandEvent	Command;
183 
184 	switch( event.GetKeyCode() )
185 	{
186 	case WXK_RETURN:
187 		Command.SetId(ID_CMD_WKSP_ITEM_RETURN);
188 		g_pData_Ctrl->On_Command(Command);
189 		break;
190 
191 	case WXK_DELETE:
192 		Command.SetId(ID_CMD_WKSP_ITEM_CLOSE);
193 		g_pData_Ctrl->On_Command(Command);
194 		break;
195 
196 	default:
197 		break;
198 	}
199 }
200 
201 //---------------------------------------------------------
On_Mouse_LDown(wxMouseEvent & event)202 void CWKSP_Data_Button::On_Mouse_LDown(wxMouseEvent &event)
203 {
204 	_Set_Active(event.ControlDown());
205 }
206 
207 //---------------------------------------------------------
On_Mouse_LDClick(wxMouseEvent & event)208 void CWKSP_Data_Button::On_Mouse_LDClick(wxMouseEvent &event)
209 {
210 	if( _Set_Active(false) && m_pItem )
211 	{
212 		m_pItem->On_Command(ID_CMD_WKSP_ITEM_RETURN);
213 	}
214 }
215 
216 //---------------------------------------------------------
On_Mouse_RDown(wxMouseEvent & event)217 void CWKSP_Data_Button::On_Mouse_RDown(wxMouseEvent &event)
218 {
219 	if( g_pData_Ctrl->Get_Selection_Count() <= 1 )
220 	{
221 		_Set_Active(false);
222 	}
223 
224 	wxMenu	*pMenu	= g_pData_Ctrl->Get_Menu();
225 
226 	if( pMenu )
227 	{
228 		GetParent()->PopupMenu(pMenu);
229 
230 		delete(pMenu);
231 	}
232 }
233 
234 
235 ///////////////////////////////////////////////////////////
236 //														 //
237 ///////////////////////////////////////////////////////////
238 
239 //---------------------------------------------------------
_Set_Active(bool bKeepOthers)240 bool CWKSP_Data_Button::_Set_Active(bool bKeepOthers)
241 {
242 	if( is_Manager() )
243 	{
244 		SetFocus();
245 
246 		if( !bKeepOthers )
247 		{
248 			g_pData_Ctrl->SelectChildren(m_pItem->GetId());
249 		}
250 		else
251 		{
252 			for(int i=0; i<((CWKSP_Base_Manager *)m_pItem)->Get_Count(); i++)
253 			{
254 				g_pData_Ctrl->Set_Item_Selected(((CWKSP_Base_Manager *)m_pItem)->Get_Item(i), true);
255 			}
256 		}
257 
258 		g_pData_Buttons->Refresh(false);
259 
260 		return( true );
261 	}
262 
263 	if( m_pItem && SG_Get_Data_Manager().Exists(((CWKSP_Data_Item *)m_pItem)->Get_Object()) )
264 	{
265 		SetFocus();
266 
267 		if( g_pData_Ctrl->Set_Item_Selected(m_pItem, bKeepOthers) )
268 		{
269 			g_pData_Buttons->Refresh(false);
270 		}
271 
272 		return( true );
273 	}
274 
275 	m_pItem	= NULL;
276 
277 	return( false );
278 }
279 
280 
281 ///////////////////////////////////////////////////////////
282 //														 //
283 //														 //
284 //														 //
285 ///////////////////////////////////////////////////////////
286 
287 //---------------------------------------------------------
288 #define THUMBNAIL_DIST	5
289 #define SCROLL_RATE		5
290 #define SCROLL_BAR_DX	wxSystemSettings::GetMetric(wxSYS_VSCROLL_X)
291 #define SCROLL_BAR_DY	wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y)
292 
293 //---------------------------------------------------------
294 CWKSP_Data_Buttons	*g_pData_Buttons	= NULL;
295 
296 
297 ///////////////////////////////////////////////////////////
298 //														 //
299 ///////////////////////////////////////////////////////////
300 
301 //---------------------------------------------------------
IMPLEMENT_CLASS(CWKSP_Data_Buttons,wxScrolledWindow)302 IMPLEMENT_CLASS(CWKSP_Data_Buttons, wxScrolledWindow)
303 
304 //---------------------------------------------------------
305 BEGIN_EVENT_TABLE(CWKSP_Data_Buttons, wxScrolledWindow)
306 	EVT_SIZE      (CWKSP_Data_Buttons::On_Size)
307 	EVT_LEFT_DOWN (CWKSP_Data_Buttons::On_Mouse_LDown)
308 	EVT_RIGHT_DOWN(CWKSP_Data_Buttons::On_Mouse_RDown)
309 END_EVENT_TABLE()
310 
311 
312 ///////////////////////////////////////////////////////////
313 //														 //
314 ///////////////////////////////////////////////////////////
315 
316 //---------------------------------------------------------
317 CWKSP_Data_Buttons::CWKSP_Data_Buttons(wxWindow *pParent)
318 	: wxScrolledWindow(pParent, -1, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxFULL_REPAINT_ON_RESIZE)
319 {
320 	g_pData_Buttons	= this;
321 
322 	m_xScroll		= 0;
323 	m_yScroll		= 0;
324 
325 	m_Items			= NULL;
326 	m_nItems		= 0;
327 }
328 
329 //---------------------------------------------------------
~CWKSP_Data_Buttons(void)330 CWKSP_Data_Buttons::~CWKSP_Data_Buttons(void)
331 {
332 	g_pData_Buttons	= NULL;
333 
334 	_Del_Items();
335 }
336 
337 
338 ///////////////////////////////////////////////////////////
339 //														 //
340 ///////////////////////////////////////////////////////////
341 
342 //---------------------------------------------------------
On_Size(wxSizeEvent & event)343 void CWKSP_Data_Buttons::On_Size(wxSizeEvent &event)
344 {
345 	_Set_Positions();
346 
347 	event.Skip();
348 }
349 
350 //---------------------------------------------------------
On_Mouse_LDown(wxMouseEvent & event)351 void CWKSP_Data_Buttons::On_Mouse_LDown(wxMouseEvent &event)
352 {
353 	g_pData_Ctrl->Set_Item_Selected(g_pData);
354 }
355 
356 //---------------------------------------------------------
On_Mouse_RDown(wxMouseEvent & event)357 void CWKSP_Data_Buttons::On_Mouse_RDown(wxMouseEvent &event)
358 {
359 	wxMenu	*pMenu	= g_pData->Get_Menu();
360 
361 	if( pMenu )
362 	{
363 		PopupMenu(pMenu);
364 
365 		delete(pMenu);
366 	}
367 }
368 
369 
370 ///////////////////////////////////////////////////////////
371 //														 //
372 ///////////////////////////////////////////////////////////
373 
374 //---------------------------------------------------------
Update_Buttons(void)375 void CWKSP_Data_Buttons::Update_Buttons(void)
376 {
377 	Freeze();
378 
379 	_Del_Items();
380 
381 	_Add_Items(g_pData);
382 
383 	Scroll(0, 0);
384 
385 	_Set_Positions();
386 
387 	Thaw();
388 }
389 
390 
391 ///////////////////////////////////////////////////////////
392 //														 //
393 ///////////////////////////////////////////////////////////
394 
395 //---------------------------------------------------------
_Del_Items(void)396 bool CWKSP_Data_Buttons::_Del_Items(void)
397 {
398 	if( m_nItems > 0 )
399 	{
400 		for(int i=0; i<m_nItems; i++)
401 		{
402 			delete(m_Items[i]);
403 		}
404 
405 		SG_Free(m_Items);
406 	}
407 
408 	m_Items		= NULL;
409 	m_nItems	= 0;
410 
411 	return( true );
412 }
413 
414 //---------------------------------------------------------
_Add_Items(CWKSP_Base_Item * pItem)415 bool CWKSP_Data_Buttons::_Add_Items(CWKSP_Base_Item *pItem)
416 {
417 	if( pItem )
418 	{
419 		switch( pItem->Get_Type() )
420 		{
421 		default:
422 			return( false );
423 
424 		case WKSP_ITEM_Table:
425 		case WKSP_ITEM_Shapes:
426 		case WKSP_ITEM_TIN:
427 		case WKSP_ITEM_PointCloud:
428 		case WKSP_ITEM_Grid:
429 		case WKSP_ITEM_Grids:
430 			return( _Add_Item((CWKSP_Data_Item *)pItem) );
431 
432 		case WKSP_ITEM_Table_Manager:
433 		case WKSP_ITEM_Shapes_Type:
434 		case WKSP_ITEM_TIN_Manager:
435 		case WKSP_ITEM_PointCloud_Manager:
436 		case WKSP_ITEM_Grid_System:
437 			if( g_pData->Get_Parameter("THUMBNAIL_CATEGORY")->asBool() )
438 			{
439 				_Add_Item((CWKSP_Base_Manager *)pItem);
440 			}
441 			break;
442 
443 		case WKSP_ITEM_Data_Manager:
444 		case WKSP_ITEM_Grid_Manager:
445 		case WKSP_ITEM_Shapes_Manager:
446 			break;
447 		}
448 
449 		for(int i=0; i<((CWKSP_Base_Manager *)pItem)->Get_Count(); i++)
450 		{
451 			_Add_Items(((CWKSP_Base_Manager *)pItem)->Get_Item(i));
452 		}
453 
454 		return( true );
455 	}
456 
457 	return( false );
458 }
459 
460 //---------------------------------------------------------
_Add_Item(CWKSP_Data_Item * pItem)461 bool CWKSP_Data_Buttons::_Add_Item(CWKSP_Data_Item *pItem)
462 {
463 	if( pItem )
464 	{
465 		m_Items	= (CWKSP_Data_Button **)SG_Realloc(m_Items, (m_nItems + 1) * sizeof(CWKSP_Data_Button *));
466 		m_Items[m_nItems++]	= new CWKSP_Data_Button(this, pItem);
467 
468 		return( true );
469 	}
470 
471 	return( false );
472 }
473 
474 //---------------------------------------------------------
_Add_Item(CWKSP_Base_Manager * pItem)475 bool CWKSP_Data_Buttons::_Add_Item(CWKSP_Base_Manager *pItem)
476 {
477 	if( pItem )
478 	{
479 		m_Items	= (CWKSP_Data_Button **)SG_Realloc(m_Items, (m_nItems + 1) * sizeof(CWKSP_Data_Button *));
480 		m_Items[m_nItems++]	= new CWKSP_Data_Button(this, pItem);
481 
482 		return( true );
483 	}
484 
485 	return( false );
486 }
487 
488 
489 ///////////////////////////////////////////////////////////
490 //														 //
491 ///////////////////////////////////////////////////////////
492 
493 //---------------------------------------------------------
_Set_Positions(void)494 void CWKSP_Data_Buttons::_Set_Positions(void)
495 {
496 	int		Size, xSize, ySize, xPos, yPos, xAdd, yAdd;
497 
498 	Size	= g_pData->Get_Parameter("THUMBNAIL_SIZE")->asInt();
499 
500 	xSize	= GetClientSize().x - SCROLL_BAR_DX;
501 
502 	if( xSize < Size + THUMBNAIL_DIST )
503 	{
504 		xSize	= Size + THUMBNAIL_DIST;
505 	}
506 
507 	xPos	= 0;
508 	yPos	= 0;
509 	xAdd	= 0;
510 	yAdd	= 0;
511 
512 	//-----------------------------------------------------
513 	for(int i=0, x, y; i<m_nItems; i++)
514 	{
515 		CWKSP_Data_Button	*pItem	= m_Items[i];
516 
517 		if( pItem->is_Manager() )
518 		{
519 			xPos	 = THUMBNAIL_DIST;
520 			yPos	+= yAdd;	if( yPos > 0 )	yPos	+= THUMBNAIL_DIST;
521 
522 			CalcScrolledPosition(0, yPos, &x, &y);
523 			pItem->SetSize(x, y, xSize + SCROLL_BAR_DX, -1, wxSIZE_USE_EXISTING);
524 
525 			yPos	+= THUMBNAIL_DIST + pItem->GetSize().y;
526 			yAdd	 = 0;
527 		}
528 		else
529 		{
530 			xAdd	= Size;
531 
532 			if( xPos + xAdd >= xSize )
533 			{
534 				xPos	 = THUMBNAIL_DIST;
535 				yPos	+= yAdd;
536 				yAdd	 = THUMBNAIL_DIST + Size;
537 			}
538 
539 			yAdd	= Size + THUMBNAIL_DIST;
540 
541 			CalcScrolledPosition(xPos, yPos, &x, &y);
542 			pItem->SetSize(x, y, Size, Size);
543 
544 			xPos	+= THUMBNAIL_DIST + xAdd;
545 		}
546 	}
547 
548 	//-----------------------------------------------------
549 	xSize	+= SCROLL_BAR_DX;
550 	ySize	 = SCROLL_BAR_DY + yPos + yAdd;
551 
552 	if(	m_xScroll != xSize || m_yScroll != ySize )
553 	{
554 		m_xScroll	= xSize;
555 		m_yScroll	= ySize;
556 
557 		SetScrollbars(SCROLL_RATE, SCROLL_RATE, m_xScroll / SCROLL_RATE, m_yScroll / SCROLL_RATE);
558 	}
559 }
560 
561 
562 ///////////////////////////////////////////////////////////
563 //														 //
564 //														 //
565 //														 //
566 ///////////////////////////////////////////////////////////
567 
568 //---------------------------------------------------------
569