OpenCPN Partial API docs
Loading...
Searching...
No Matches
ocpn_aui_manager.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2018 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
23#include "gl_headers.h" // Must come before anything using GL stuff
24#ifndef WX_PRECOMP
25#include <wx/wx.h>
26#endif
27
28#include "ocpn_aui_manager.h"
29#include "ocpn_plugin.h"
30
31#ifdef __WXMSW__
32#include <wx/msw/wrapwin.h>
33#include <wx/msw/private.h>
34#include <wx/msw/dc.h>
35#endif
36
37// -- static utility functions --
38
40
41static wxBitmap wxOPaneCreateStippleBitmap() {
42 unsigned char data[] = {0, 0, 0, 192, 192, 192, 192, 192, 192, 0, 0, 0};
43 wxImage img(2, 2, data, true);
44 return wxBitmap(img);
45}
46
47static void ODrawResizeHint(wxDC& dc, const wxRect& rect) {
48#if 1
49 wxBitmap stipple = wxOPaneCreateStippleBitmap();
50 wxBrush brush(stipple);
51 dc.SetBrush(brush);
52#ifdef __WXMSW__
53 wxMSWDCImpl* impl = (wxMSWDCImpl*)dc.GetImpl();
54 PatBlt(GetHdcOf(*impl), rect.GetX(), rect.GetY(), rect.GetWidth(),
55 rect.GetHeight(), PATINVERT);
56#else
57 dc.SetPen(*wxTRANSPARENT_PEN);
58
59 dc.SetLogicalFunction(wxXOR);
60 dc.DrawRectangle(rect);
61#endif
62#endif
63}
64
65// Convenience function
66static bool OAuiManager_HasLiveResize(wxAuiManager& manager) {
67 // With Core Graphics on Mac, it's not possible to show sash feedback,
68 // so we'll always use live update instead.
69#if defined(__WXMAC__)
70 wxUnusedVar(manager);
71 return true;
72#else
73 return (manager.GetFlags() & wxAUI_MGR_LIVE_RESIZE) == wxAUI_MGR_LIVE_RESIZE;
74#endif
75}
76
77// OCPN_AUIManager Implementation
78
79BEGIN_EVENT_TABLE(OCPN_AUIManager, wxEvtHandler)
80EVT_AUI_PANE_BUTTON(wxAuiManager::OnPaneButton)
81EVT_AUI_RENDER(wxAuiManager::OnRender)
82EVT_PAINT(OCPN_AUIManager::OnPaint)
83EVT_ERASE_BACKGROUND(OCPN_AUIManager::OnEraseBackground)
84EVT_SIZE(OCPN_AUIManager::OnSize)
85EVT_SET_CURSOR(OCPN_AUIManager::OnSetCursor)
86EVT_LEFT_DOWN(OCPN_AUIManager::OnLeftDown)
87EVT_LEFT_UP(OCPN_AUIManager::OnLeftUp)
88EVT_MOTION(OCPN_AUIManager::OnMotionx)
89EVT_LEAVE_WINDOW(OCPN_AUIManager::OnLeaveWindow)
90EVT_MOUSE_CAPTURE_LOST(OCPN_AUIManager::OnCaptureLost)
91EVT_CHILD_FOCUS(OCPN_AUIManager::OnChildFocus)
92EVT_AUI_FIND_MANAGER(OCPN_AUIManager::OnFindManager)
93END_EVENT_TABLE()
94
95OCPN_AUIManager::OCPN_AUIManager(wxWindow* managed_wnd, unsigned int flags)
96 : wxAuiManager(managed_wnd, flags)
97
98{}
99
100OCPN_AUIManager::~OCPN_AUIManager() {}
101
102void OCPN_AUIManager::OnMotionx(wxMouseEvent& event) {
103 // sometimes when Update() is called from inside this method,
104 // a spurious mouse move event is generated; this check will make
105 // sure that only real mouse moves will get anywhere in this method;
106 // this appears to be a bug somewhere, and I don't know where the
107 // mouse move event is being generated. only verified on MSW
108
109 wxPoint mouse_pos = event.GetPosition();
110 if (m_lastMouseMove == mouse_pos) return;
111 m_lastMouseMove = mouse_pos;
112
113 if (m_action == actionResize) {
114 // It's necessary to reset m_actionPart since it destroyed
115 // by the Update within DoEndResizeAction.
116 if (m_currentDragItem != -1)
117 m_actionPart = &(m_uiParts.Item(m_currentDragItem));
118 else
119 m_currentDragItem = m_uiParts.Index(*m_actionPart);
120
121 if (m_actionPart) {
122 wxPoint pos = m_actionPart->rect.GetPosition();
123 if (m_actionPart->orientation == wxHORIZONTAL)
124 pos.y = wxMax(0, event.m_y - m_actionOffset.y);
125 else
126 pos.x = wxMax(0, event.m_x - m_actionOffset.x);
127
128 wxSize client_size = m_frame->GetClientSize();
129 int used_width = 0, used_height = 0;
130
131 size_t dock_i, dock_count = m_docks.GetCount();
132 for (dock_i = 0; dock_i < dock_count; ++dock_i) {
133 wxAuiDockInfo& dock = m_docks.Item(dock_i);
134 if (dock.dock_direction == wxAUI_DOCK_TOP ||
135 dock.dock_direction == wxAUI_DOCK_BOTTOM) {
136 used_height += dock.size;
137 }
138 if (dock.dock_direction == wxAUI_DOCK_LEFT ||
139 dock.dock_direction == wxAUI_DOCK_RIGHT) {
140 used_width += dock.size;
141 }
142 // if (dock.resizable)
143 // used_width += sashSize;
144 }
145
146 if (OAuiManager_HasLiveResize(*this)) {
147 m_frame->ReleaseMouse();
148 if ((used_width < client_size.x * 9 / 10) &&
149 (used_width > client_size.x * 1 / 10))
150 DoEndResizeAction(event);
151
152 m_frame->CaptureMouse();
153 } else {
154 bool bhasMouse = m_frame->HasCapture();
155
156 if (bhasMouse) m_frame->ReleaseMouse();
157
158 // Tell MyFrame that the sash is moving, so that he
159 // may disable any top-level windows and so avoid mouse focus problems.
160
161 wxRect rect(m_frame->ClientToScreen(pos), m_actionPart->rect.GetSize());
162 wxScreenDC dc;
163
164 if (!m_0actionHintRect.IsEmpty()) {
165 // remove old resize hint
166 ODrawResizeHint(dc, m_0actionHintRect);
167 m_0actionHintRect = wxRect();
168 }
169
170 wxRect frameScreenRect = m_frame->GetScreenRect();
171
172 rect.x =
173 wxMax(rect.x, frameScreenRect.x + frameScreenRect.width * 1 / 10);
174 rect.x =
175 wxMin(rect.x, frameScreenRect.x + frameScreenRect.width * 9 / 10);
176
177 // draw new resize hint, if it's inside the managed frame
178 if (1 /*frameScreenRect.Contains(rect)*/) {
179 ODrawResizeHint(dc, rect);
180 m_0actionHintRect = rect;
181 }
182
183 if (bhasMouse) m_frame->CaptureMouse();
184 }
185 }
186 } else {
187 OnMotion(event);
188 }
189}
190
191bool OCPN_AUIManager::DoEndResizeAction(wxMouseEvent& event) {
192 // resize the dock or the pane
193 if (m_actionPart && m_actionPart->type == wxAuiDockUIPart::typeDockSizer) {
194 // first, we must calculate the maximum size the dock may be
195 int sashSize = m_art->GetMetric(wxAUI_DOCKART_SASH_SIZE);
196
197 int used_width = 0, used_height = 0;
198
199 wxSize client_size = m_frame->GetClientSize();
200
201 size_t dock_i, dock_count = m_docks.GetCount();
202 for (dock_i = 0; dock_i < dock_count; ++dock_i) {
203 wxAuiDockInfo& dock = m_docks.Item(dock_i);
204 if (dock.dock_direction == wxAUI_DOCK_TOP ||
205 dock.dock_direction == wxAUI_DOCK_BOTTOM) {
206 used_height += dock.size;
207 }
208 if (dock.dock_direction == wxAUI_DOCK_LEFT ||
209 dock.dock_direction == wxAUI_DOCK_RIGHT) {
210 used_width += dock.size;
211 }
212 if (dock.resizable) used_width += sashSize;
213 }
214
215 int available_width = client_size.GetWidth() - used_width;
216 int available_height = client_size.GetHeight() - used_height;
217
218#if wxUSE_STATUSBAR
219 // if there's a status control, the available
220 // height decreases accordingly
221 if (dynamic_cast<wxFrame*>(m_frame)) {
222 wxFrame* frame = static_cast<wxFrame*>(m_frame);
223 wxStatusBar* status = frame->GetStatusBar();
224 if (status) {
225 wxSize status_client_size = status->GetClientSize();
226 available_height -= status_client_size.GetHeight();
227 }
228 }
229#endif
230
231 wxRect& rect = m_actionPart->dock->rect;
232
233 wxPoint new_pos(event.m_x - m_actionOffset.x, event.m_y - m_actionOffset.y);
234 int new_size, old_size = m_actionPart->dock->size;
235
236 switch (m_actionPart->dock->dock_direction) {
237 case wxAUI_DOCK_LEFT:
238 new_size = new_pos.x - rect.x;
239 if (new_size - old_size > available_width)
240 new_size = old_size + available_width;
241 m_actionPart->dock->size = new_size;
242 break;
243 case wxAUI_DOCK_TOP:
244 new_size = new_pos.y - rect.y;
245 if (new_size - old_size > available_height)
246 new_size = old_size + available_height;
247 m_actionPart->dock->size = new_size;
248 break;
249 case wxAUI_DOCK_RIGHT:
250 new_size =
251 rect.x + rect.width - new_pos.x - m_actionPart->rect.GetWidth();
252 if (new_size - old_size > available_width)
253 new_size = old_size + available_width;
254 m_actionPart->dock->size = new_size;
255
256 m_actionPart->dock->size =
257 wxMax(m_actionPart->dock->size, client_size.x * 1 / 10);
258 m_actionPart->dock->size =
259 wxMin(m_actionPart->dock->size, client_size.x * 9 / 10);
260
261 break;
262 case wxAUI_DOCK_BOTTOM:
263 new_size =
264 rect.y + rect.height - new_pos.y - m_actionPart->rect.GetHeight();
265 if (new_size - old_size > available_height)
266 new_size = old_size + available_height;
267 m_actionPart->dock->size = new_size;
268 break;
269 }
270
271 Update();
272 Repaint(NULL);
273 } else if (m_actionPart &&
274 m_actionPart->type == wxAuiDockUIPart::typePaneSizer) {
275 wxAuiDockInfo& dock = *m_actionPart->dock;
276 wxAuiPaneInfo& pane = *m_actionPart->pane;
277
278 int total_proportion = 0;
279 int dock_pixels = 0;
280 int new_pixsize = 0;
281
282 int caption_size = m_art->GetMetric(wxAUI_DOCKART_CAPTION_SIZE);
283 int pane_borderSize = m_art->GetMetric(wxAUI_DOCKART_PANE_BORDER_SIZE);
284 int sashSize = m_art->GetMetric(wxAUI_DOCKART_SASH_SIZE);
285
286 wxPoint new_pos(event.m_x - m_actionOffset.x, event.m_y - m_actionOffset.y);
287
288 // determine the pane rectangle by getting the pane part
289 wxAuiDockUIPart* pane_part = GetPanePart(pane.window);
290 wxASSERT_MSG(pane_part, "Pane border part not found -- shouldn't happen");
291
292 // determine the new pixel size that the user wants;
293 // this will help us recalculate the pane's proportion
294 if (dock.IsHorizontal())
295 new_pixsize = new_pos.x - pane_part->rect.x;
296 else
297 new_pixsize = new_pos.y - pane_part->rect.y;
298
299 // determine the size of the dock, based on orientation
300 if (dock.IsHorizontal())
301 dock_pixels = dock.rect.GetWidth();
302 else
303 dock_pixels = dock.rect.GetHeight();
304
305 // determine the total proportion of all resizable panes,
306 // and the total size of the dock minus the size of all
307 // the fixed panes
308 int i, dock_pane_count = dock.panes.GetCount();
309 int pane_position = -1;
310 for (i = 0; i < dock_pane_count; ++i) {
311 wxAuiPaneInfo& p = *dock.panes.Item(i);
312 if (p.window == pane.window) pane_position = i;
313
314 // while we're at it, subtract the pane sash
315 // width from the dock width, because this would
316 // skew our proportion calculations
317 if (i > 0) dock_pixels -= sashSize;
318
319 // also, the whole size (including decorations) of
320 // all fixed panes must also be subtracted, because they
321 // are not part of the proportion calculation
322 if (p.IsFixed()) {
323 if (dock.IsHorizontal())
324 dock_pixels -= p.best_size.x;
325 else
326 dock_pixels -= p.best_size.y;
327 } else {
328 total_proportion += p.dock_proportion;
329 }
330 }
331
332 // new size can never be more than the number of dock pixels
333 if (new_pixsize > dock_pixels) new_pixsize = dock_pixels;
334
335 // find a pane in our dock to 'steal' space from or to 'give'
336 // space to -- this is essentially what is done when a pane is
337 // resized; the pane should usually be the first non-fixed pane
338 // to the right of the action pane
339 int borrow_pane = -1;
340 for (i = pane_position + 1; i < dock_pane_count; ++i) {
341 wxAuiPaneInfo& p = *dock.panes.Item(i);
342 if (!p.IsFixed()) {
343 borrow_pane = i;
344 break;
345 }
346 }
347
348 // demand that the pane being resized is found in this dock
349 // (this assert really never should be raised)
350 wxASSERT_MSG(pane_position != -1, "Pane not found in dock");
351
352 // prevent division by zero
353 if (dock_pixels == 0 || total_proportion == 0 || borrow_pane == -1) {
354 m_action = actionNone;
355 return false;
356 }
357
358 // calculate the new proportion of the pane
359 int new_proportion = (new_pixsize * total_proportion) / dock_pixels;
360
361 // default minimum size
362 int min_size = 0;
363
364 // check against the pane's minimum size, if specified. please note
365 // that this is not enough to ensure that the minimum size will
366 // not be violated, because the whole frame might later be shrunk,
367 // causing the size of the pane to violate it's minimum size
368 if (pane.min_size.IsFullySpecified()) {
369 min_size = 0;
370
371 if (pane.HasBorder()) min_size += (pane_borderSize * 2);
372
373 // calculate minimum size with decorations (border,caption)
374 if (pane_part->orientation == wxVERTICAL) {
375 min_size += pane.min_size.y;
376 if (pane.HasCaption()) min_size += caption_size;
377 } else {
378 min_size += pane.min_size.x;
379 }
380 }
381
382 // for some reason, an arithmatic error somewhere is causing
383 // the proportion calculations to always be off by 1 pixel;
384 // for now we will add the 1 pixel on, but we really should
385 // determine what's causing this.
386 min_size++;
387
388 int min_proportion = (min_size * total_proportion) / dock_pixels;
389
390 if (new_proportion < min_proportion) new_proportion = min_proportion;
391
392 int prop_diff = new_proportion - pane.dock_proportion;
393
394 // borrow the space from our neighbor pane to the
395 // right or bottom (depending on orientation);
396 // also make sure we don't make the neighbor too small
397 int prop_borrow = dock.panes.Item(borrow_pane)->dock_proportion;
398
399 if (prop_borrow - prop_diff < 0) {
400 // borrowing from other pane would make it too small,
401 // so cancel the resize operation
402 prop_borrow = min_proportion;
403 } else {
404 prop_borrow -= prop_diff;
405 }
406
407 dock.panes.Item(borrow_pane)->dock_proportion = prop_borrow;
408 pane.dock_proportion = new_proportion;
409
410 // repaint
411 Update();
412 Repaint(NULL);
413 }
414
415 return true;
416}
417
418void OCPN_AUIManager::OnLeftUp(wxMouseEvent& event) {
419 if (m_action == actionResize) {
420 m_frame->ReleaseMouse();
421
422 if (!OAuiManager_HasLiveResize(*this)) {
423 // get rid of the hint rectangle
424 wxScreenDC dc;
425 ODrawResizeHint(dc, m_0actionHintRect);
426 }
427 if (m_currentDragItem != -1 && OAuiManager_HasLiveResize(*this))
428 m_actionPart = &(m_uiParts.Item(m_currentDragItem));
429
430 DoEndResizeAction(event);
431
432 m_currentDragItem = -1;
433
434 } else if (m_action == actionClickButton) {
435 m_hoverButton = NULL;
436 m_frame->ReleaseMouse();
437
438 if (m_actionPart) {
439 UpdateButtonOnScreen(m_actionPart, event);
440
441 // make sure we're still over the item that was originally clicked
442 if (m_actionPart == HitTest(event.GetX(), event.GetY())) {
443 // fire button-click event
444 wxAuiManagerEvent e(wxEVT_AUI_PANE_BUTTON);
445 e.SetManager(this);
446 e.SetPane(m_actionPart->pane);
447
448#if wxCHECK_VERSION(3, 1, 4)
449 e.SetButton(m_actionPart->button);
450#else
451 e.SetButton(m_actionPart->button->button_id);
452#endif
453 ProcessMgrEvent(e);
454 }
455 }
456 } else if (m_action == actionClickCaption) {
457 m_frame->ReleaseMouse();
458 } else if (m_action == actionDragFloatingPane) {
459 m_frame->ReleaseMouse();
460 }
461#if 0
462 else if (m_action == actionDragToolbarPane)
463 {
464 m_frame->ReleaseMouse();
465
466 wxAuiPaneInfo& pane = GetPane(m_actionWindow);
467 wxASSERT_MSG(pane.IsOk(), "Pane window not found");
468
469 // save the new positions
470 wxAuiDockInfoPtrArray docks;
471 FindDocks(m_docks, pane.dock_direction,
472 pane.dock_layer, pane.dock_row, docks);
473 if (docks.GetCount() == 1)
474 {
475 wxAuiDockInfo& dock = *docks.Item(0);
476
477 wxArrayInt pane_positions, pane_sizes;
478 GetPanePositionsAndSizes(dock, pane_positions, pane_sizes);
479
480 int i, dock_pane_count = dock.panes.GetCount();
481 for (i = 0; i < dock_pane_count; ++i)
482 dock.panes.Item(i)->dock_pos = pane_positions[i];
483 }
484
485 pane.state &= ~wxAuiPaneInfo::actionPane;
486 Update();
487 }
488#endif
489 else {
490 event.Skip();
491 }
492
493 m_action = actionNone;
494 m_lastMouseMove = wxPoint(); // see comment in OnMotion()
495}
496
497// FindDocks() is an internal function that returns a list of docks which meet
498// the specified conditions in the parameters and returns a sorted array
499// (sorted by layer and then row)
500static void OCPNFindDocks(wxAuiDockInfoArray& docks, int dock_direction,
501 int dock_layer, int dock_row,
502 wxAuiDockInfoPtrArray& arr) {
503 int begin_layer = dock_layer;
504 int end_layer = dock_layer;
505 int begin_row = dock_row;
506 int end_row = dock_row;
507 int dock_count = docks.GetCount();
508 int layer, row, i, max_row = 0, max_layer = 0;
509
510 // discover the maximum dock layer and the max row
511 for (i = 0; i < dock_count; ++i) {
512 max_row = wxMax(max_row, docks.Item(i).dock_row);
513 max_layer = wxMax(max_layer, docks.Item(i).dock_layer);
514 }
515
516 // if no dock layer was specified, search all dock layers
517 if (dock_layer == -1) {
518 begin_layer = 0;
519 end_layer = max_layer;
520 }
521
522 // if no dock row was specified, search all dock row
523 if (dock_row == -1) {
524 begin_row = 0;
525 end_row = max_row;
526 }
527
528 arr.Clear();
529
530 for (layer = begin_layer; layer <= end_layer; ++layer)
531 for (row = begin_row; row <= end_row; ++row)
532 for (i = 0; i < dock_count; ++i) {
533 wxAuiDockInfo& d = docks.Item(i);
534 if (dock_direction == -1 || dock_direction == d.dock_direction) {
535 if (d.dock_layer == layer && d.dock_row == row) arr.Add(&d);
536 }
537 }
538}
539
540wxAuiDockInfo* OCPN_AUIManager::FindDock(wxAuiPaneInfo& pane) {
541 wxAuiDockInfoPtrArray arr;
542 OCPNFindDocks(m_docks, pane.dock_direction, pane.dock_layer, pane.dock_row,
543 arr);
544 if (arr.GetCount())
545 return arr.Item(0);
546 else
547 return NULL;
548}
549
550void OCPN_AUIManager::SetDockSize(wxAuiDockInfo* dock, int size) {
551 dock->size = size;
552
553 Update();
554 Repaint(NULL);
555}
556
557bool OCPN_AUIManager::ProcessDockResult(wxAuiPaneInfo& target,
558 const wxAuiPaneInfo& new_pos) {
559 // printf("DockResult direction: %d layer: %d position: %d %d\n" ,
560 // new_pos.dock_direction, new_pos.dock_layer, new_pos.dock_pos,
561 // GetCanvasIndexUnderMouse());
562
563 // If we are docking a Dashboard window, we restrict the spots that can accept
564 // the docking action
565 if (new_pos.window->GetName().IsSameAs("panel")) {
566 // Dashboards can not go on the left( interferes with global toolbar )
567 if (/*(new_pos.dock_layer != 1) ||*/ (new_pos.dock_direction ==
568 wxAUI_DOCK_LEFT))
569 return false;
570
571 // Also, in multi-canvas mode, the dashboard is restricted to layer 1 in
572 // right hand canvas. This forces it to dock at the far right only.
573 if (GetCanvasCount() > 1) {
574 if (GetCanvasIndexUnderMouse() > 0) {
575 if (new_pos.dock_layer == 0) return false;
576 }
577 }
578 }
579
580 return wxAuiManager::ProcessDockResult(target, new_pos);
581}
Platform independent GL includes.
OCPN_AUIManager * g_pauimgr
Global instance.
OCPN_AUIManager.
PlugIn Object Definition/API.
int GetCanvasCount()
Gets total number of chart canvases.
int GetCanvasIndexUnderMouse()
Gets index of chart canvas under mouse cursor.