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_map_buttons.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
58 #include "helper.h"
59
60 #include "active.h"
61
62 #include "wksp_map_control.h"
63 #include "wksp_map_manager.h"
64 #include "wksp_map.h"
65 #include "wksp_map_layer.h"
66
67 #include "wksp_map_buttons.h"
68
69
70 ///////////////////////////////////////////////////////////
71 // //
72 // //
73 // //
74 ///////////////////////////////////////////////////////////
75
76 //---------------------------------------------------------
IMPLEMENT_CLASS(CWKSP_Map_Button,wxPanel)77 IMPLEMENT_CLASS(CWKSP_Map_Button, wxPanel)
78
79 //---------------------------------------------------------
80 BEGIN_EVENT_TABLE(CWKSP_Map_Button, wxPanel)
81 EVT_PAINT (CWKSP_Map_Button::On_Paint)
82 EVT_KEY_DOWN (CWKSP_Map_Button::On_Key)
83 EVT_LEFT_DOWN (CWKSP_Map_Button::On_Mouse_LDown)
84 EVT_LEFT_DCLICK(CWKSP_Map_Button::On_Mouse_LDClick)
85 EVT_RIGHT_DOWN (CWKSP_Map_Button::On_Mouse_RDown)
86 END_EVENT_TABLE()
87
88
89 ///////////////////////////////////////////////////////////
90 // //
91 ///////////////////////////////////////////////////////////
92
93 //---------------------------------------------------------
94 CWKSP_Map_Button::CWKSP_Map_Button(wxWindow *pParent, CWKSP_Map *pMap)
95 : wxPanel(pParent, -1, wxDefaultPosition, wxDefaultSize, wxRAISED_BORDER)
96 {
97 m_pMap = pMap;
98 }
99
100
101 ///////////////////////////////////////////////////////////
102 // //
103 ///////////////////////////////////////////////////////////
104
105 //---------------------------------------------------------
On_Paint(wxPaintEvent & event)106 void CWKSP_Map_Button::On_Paint(wxPaintEvent &event)
107 {
108 if( g_pMaps->Exists(m_pMap) )
109 {
110 if( !GetToolTip() || GetToolTip()->GetTip().Cmp(m_pMap->Get_Name()) )
111 {
112 SetToolTip(m_pMap->Get_Name());
113 }
114
115 //-------------------------------------------------
116 wxPaintDC dc(this);
117
118 wxRect r(GetClientRect());
119
120 dc.DrawBitmap(m_pMap->Get_Thumbnail(r.GetWidth() - 1, r.GetHeight() - 1),
121 r.GetLeft(), r.GetTop(), true
122 );
123
124 //-------------------------------------------------
125 if( m_pMap->is_Selected() )
126 {
127 dc.SetPen(wxPen(Get_Color_asWX(g_pMaps->Get_Parameter("THUMBNAIL_SELCOLOR")->asColor())));
128
129 Draw_Edge(dc, EDGE_STYLE_SIMPLE, r); r.Deflate(1);
130 Draw_Edge(dc, EDGE_STYLE_SIMPLE, r); r.Deflate(1);
131 Draw_Edge(dc, EDGE_STYLE_SIMPLE, r);
132 }
133 }
134 }
135
136
137 ///////////////////////////////////////////////////////////
138 // //
139 ///////////////////////////////////////////////////////////
140
141 //---------------------------------------------------------
On_Key(wxKeyEvent & event)142 void CWKSP_Map_Button::On_Key(wxKeyEvent &event)
143 {
144 wxCommandEvent Command;
145
146 switch( event.GetKeyCode() )
147 {
148 case WXK_RETURN:
149 Command.SetId(ID_CMD_WKSP_ITEM_RETURN);
150 g_pMap_Ctrl->On_Command(Command);
151 break;
152
153 case WXK_DELETE:
154 Command.SetId(ID_CMD_WKSP_ITEM_CLOSE);
155 g_pMap_Ctrl->On_Command(Command);
156 break;
157
158 default:
159 break;
160 }
161 }
162
163 //---------------------------------------------------------
On_Mouse_LDown(wxMouseEvent & event)164 void CWKSP_Map_Button::On_Mouse_LDown(wxMouseEvent &event)
165 {
166 _Set_Active();
167 }
168
169 //---------------------------------------------------------
On_Mouse_LDClick(wxMouseEvent & event)170 void CWKSP_Map_Button::On_Mouse_LDClick(wxMouseEvent &event)
171 {
172 if( _Set_Active() )
173 {
174 m_pMap->On_Command(ID_CMD_WKSP_ITEM_RETURN);
175 }
176 }
177
178 //---------------------------------------------------------
On_Mouse_RDown(wxMouseEvent & event)179 void CWKSP_Map_Button::On_Mouse_RDown(wxMouseEvent &event)
180 {
181 if( _Set_Active() )
182 {
183 wxMenu *pMenu = m_pMap->Get_Menu();
184
185 if( pMenu )
186 {
187 GetParent()->PopupMenu(pMenu, GetParent()->ScreenToClient(ClientToScreen(event.GetPosition())));
188
189 delete(pMenu);
190 }
191 }
192 }
193
194
195 ///////////////////////////////////////////////////////////
196 // //
197 ///////////////////////////////////////////////////////////
198
199 //---------------------------------------------------------
_Set_Active(void)200 bool CWKSP_Map_Button::_Set_Active(void)
201 {
202 if( g_pMaps->Exists(m_pMap) )
203 {
204 SetFocus();
205
206 return( g_pMap_Ctrl->Set_Item_Selected(m_pMap) );
207 }
208
209 m_pMap = NULL;
210
211 return( false );
212 }
213
214
215 ///////////////////////////////////////////////////////////
216 // //
217 // //
218 // //
219 ///////////////////////////////////////////////////////////
220
221 //---------------------------------------------------------
222 #define THUMBNAIL_DIST 5
223 #define SCROLL_RATE 5
224 #define SCROLL_BAR_DX wxSystemSettings::GetMetric(wxSYS_VSCROLL_X)
225 #define SCROLL_BAR_DY wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y)
226
227 //---------------------------------------------------------
228 CWKSP_Map_Buttons *g_pMap_Buttons = NULL;
229
230
231 ///////////////////////////////////////////////////////////
232 // //
233 ///////////////////////////////////////////////////////////
234
235 //---------------------------------------------------------
IMPLEMENT_CLASS(CWKSP_Map_Buttons,wxScrolledWindow)236 IMPLEMENT_CLASS(CWKSP_Map_Buttons, wxScrolledWindow)
237
238 //---------------------------------------------------------
239 BEGIN_EVENT_TABLE(CWKSP_Map_Buttons, wxScrolledWindow)
240 EVT_SIZE (CWKSP_Map_Buttons::On_Size)
241 EVT_LEFT_DOWN (CWKSP_Map_Buttons::On_Mouse_LDown)
242 EVT_RIGHT_DOWN(CWKSP_Map_Buttons::On_Mouse_RDown)
243 END_EVENT_TABLE()
244
245
246 ///////////////////////////////////////////////////////////
247 // //
248 ///////////////////////////////////////////////////////////
249
250 //---------------------------------------------------------
251 CWKSP_Map_Buttons::CWKSP_Map_Buttons(wxWindow *pParent)
252 : wxScrolledWindow(pParent, -1, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxFULL_REPAINT_ON_RESIZE)
253 {
254 g_pMap_Buttons = this;
255
256 m_xScroll = 0;
257 m_yScroll = 0;
258
259 m_Items = NULL;
260 m_nItems = 0;
261 }
262
263 //---------------------------------------------------------
~CWKSP_Map_Buttons(void)264 CWKSP_Map_Buttons::~CWKSP_Map_Buttons(void)
265 {
266 g_pMap_Buttons = NULL;
267
268 _Del_Items();
269 }
270
271
272 ///////////////////////////////////////////////////////////
273 // //
274 ///////////////////////////////////////////////////////////
275
276 //---------------------------------------------------------
On_Size(wxSizeEvent & event)277 void CWKSP_Map_Buttons::On_Size(wxSizeEvent &event)
278 {
279 _Set_Positions();
280
281 event.Skip();
282 }
283
284 //---------------------------------------------------------
On_Mouse_LDown(wxMouseEvent & event)285 void CWKSP_Map_Buttons::On_Mouse_LDown(wxMouseEvent &event)
286 {
287 g_pMap_Ctrl->Set_Item_Selected(g_pMaps);
288 }
289
290 //---------------------------------------------------------
On_Mouse_RDown(wxMouseEvent & event)291 void CWKSP_Map_Buttons::On_Mouse_RDown(wxMouseEvent &event)
292 {
293 wxMenu *pMenu = g_pMaps->Get_Menu();
294
295 if( pMenu )
296 {
297 PopupMenu(pMenu);
298
299 delete(pMenu);
300 }
301 }
302
303
304 ///////////////////////////////////////////////////////////
305 // //
306 ///////////////////////////////////////////////////////////
307
308 //---------------------------------------------------------
Update_Buttons(void)309 void CWKSP_Map_Buttons::Update_Buttons(void)
310 {
311 Freeze();
312
313 _Del_Items();
314
315 _Add_Items(g_pMaps);
316
317 Scroll(0, 0);
318
319 _Set_Positions();
320
321 Thaw();
322 }
323
324
325 ///////////////////////////////////////////////////////////
326 // //
327 ///////////////////////////////////////////////////////////
328
329 //---------------------------------------------------------
_Del_Items(void)330 bool CWKSP_Map_Buttons::_Del_Items(void)
331 {
332 if( m_nItems > 0 )
333 {
334 for(int i=0; i<m_nItems; i++)
335 {
336 delete(m_Items[i]);
337 }
338
339 SG_Free(m_Items);
340 }
341
342 m_Items = NULL;
343 m_nItems = 0;
344
345 return( true );
346 }
347
348 //---------------------------------------------------------
_Add_Items(CWKSP_Base_Item * pItem)349 bool CWKSP_Map_Buttons::_Add_Items(CWKSP_Base_Item *pItem)
350 {
351 if( pItem )
352 {
353 switch( pItem->Get_Type() )
354 {
355 default:
356 return( false );
357
358 case WKSP_ITEM_Map:
359 return( _Add_Item((CWKSP_Map *)pItem) );
360
361 case WKSP_ITEM_Map_Manager:
362 break;
363 }
364
365 for(int i=0; i<((CWKSP_Base_Manager *)pItem)->Get_Count(); i++)
366 {
367 _Add_Items(((CWKSP_Base_Manager *)pItem)->Get_Item(i));
368 }
369
370 return( true );
371 }
372
373 return( false );
374 }
375
376 //---------------------------------------------------------
_Add_Item(CWKSP_Map * pMap)377 bool CWKSP_Map_Buttons::_Add_Item(CWKSP_Map *pMap)
378 {
379 if( pMap )
380 {
381 m_Items = (CWKSP_Map_Button **)SG_Realloc(m_Items, (m_nItems + 1) * sizeof(CWKSP_Map_Button *));
382 m_Items[m_nItems++] = new CWKSP_Map_Button(this, pMap);
383
384 return( true );
385 }
386
387 return( false );
388 }
389
390
391 ///////////////////////////////////////////////////////////
392 // //
393 ///////////////////////////////////////////////////////////
394
395 //---------------------------------------------------------
_Set_Positions(void)396 void CWKSP_Map_Buttons::_Set_Positions(void)
397 {
398 int Size, xSize, ySize, xPos, yPos, xAdd, yAdd;
399
400 Size = g_pMaps->Get_Parameter("THUMBNAIL_SIZE")->asInt();
401
402 xSize = GetClientSize().x - SCROLL_BAR_DX;
403
404 if( xSize < Size + THUMBNAIL_DIST )
405 {
406 xSize = Size + THUMBNAIL_DIST;
407 }
408
409 xPos = THUMBNAIL_DIST;
410 yPos = THUMBNAIL_DIST;
411 xAdd = 0;
412 yAdd = 0;
413
414 //-----------------------------------------------------
415 for(int i=0, x, y; i<m_nItems; i++)
416 {
417 CWKSP_Map_Button *pItem = m_Items[i];
418
419 {
420 xAdd = Size;
421
422 if( xPos + xAdd >= xSize )
423 {
424 xPos = THUMBNAIL_DIST;
425 yPos += yAdd;
426 yAdd = THUMBNAIL_DIST + Size;
427 }
428
429 yAdd = Size + THUMBNAIL_DIST;
430
431 CalcScrolledPosition(xPos, yPos, &x, &y);
432 pItem->SetSize(x, y, Size, Size);
433
434 xPos += THUMBNAIL_DIST + xAdd;
435 }
436 }
437
438 //-----------------------------------------------------
439 xSize += SCROLL_BAR_DX;
440 ySize = SCROLL_BAR_DY + yPos + yAdd;
441
442 if( m_xScroll != xSize || m_yScroll != ySize )
443 {
444 m_xScroll = xSize;
445 m_yScroll = ySize;
446
447 SetScrollbars(SCROLL_RATE, SCROLL_RATE, m_xScroll / SCROLL_RATE, m_yScroll / SCROLL_RATE);
448 }
449 }
450
451
452 ///////////////////////////////////////////////////////////
453 // //
454 // //
455 // //
456 ///////////////////////////////////////////////////////////
457
458 //---------------------------------------------------------
459