OpenCPN Partial API docs
toolbar.h
1 /****************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: OpenCPN Toolbar
5  * Author: David Register
6  *
7  ***************************************************************************
8  * Copyright (C) 2010 by David S. Register *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24  **************************************************************************/
25 
26 #ifndef _TOOLBAR_H__
27 #define _TOOLBAR_H__
28 
29 #include <wx/tbarbase.h>
30 #include <wx/dynarray.h>
31 #include "styles.h"
32 #include <vector>
33 #include "ocpndc.h"
34 
36 
38 public:
41 
42  ToolbarItemContainer(int toolid, wxBitmap bmpNormal, wxBitmap bmpDisabled,
43  wxItemKind kind, wxString tooltip, wxString label) {
44  m_ID = toolid;
45  m_tipString = tooltip;
46  m_label = label;
47  m_toolKind = kind;
48  m_bmpNormal = bmpNormal;
49  m_bmpDisabled = bmpDisabled;
50  m_bRequired = false;
51  m_bPlugin = false;
52  }
53 
54  ToolbarItemContainer(int toolid, wxBitmap bmpNormal, wxItemKind kind,
55  wxString tooltip, wxString label) {
56  m_ID = toolid;
57  m_tipString = tooltip;
58  m_label = label;
59  m_toolKind = kind;
60  m_bmpNormal = bmpNormal;
61  m_bmpDisabled = wxNullBitmap;
62  m_bRequired = false;
63  m_bPlugin = false;
64  }
65 
66  int m_ID;
67  wxString m_tipString;
68  wxString m_label;
69  wxItemKind m_toolKind;
70  bool m_bRequired;
71  bool m_bPlugin;
72 
73  wxBitmap m_bmpNormal;
74  wxBitmap m_bmpDisabled;
75  wxToolBarToolBase *m_tool;
76 
77  // Supplemental SVG icons for plugin tools
78  wxString m_NormalIconSVG;
79  wxString m_RolloverIconSVG;
80  wxString m_ToggledIconSVG;
81 };
82 
83 typedef std::vector<ToolbarItemContainer *> ArrayOfToolbarItemContainer;
84 
85 #define TOOLTIPON_TIMER 10000
86 #define TOOLTIPOFF_TIMER 10001
87 
88 enum {
89  TOOLBAR_HIDE_TO_GRABBER = 0,
90  TOOLBAR_HIDE_TO_FIRST_TOOL,
91 };
92 
93 class ToolTipWin;
94 class ocpnToolBarTool;
95 
96 // ----------------------------------------------------------------------------
97 // ocpnToolBarSimple is a generic toolbar implementation in pure wxWidgets
98 // Adapted from wxToolBarSimple( deprecated )
99 // ----------------------------------------------------------------------------
100 
101 class ocpnToolBarSimple : public wxEvtHandler {
102 public:
103  // ctors and dtor
104  ocpnToolBarSimple() { Init(); }
105 
106  ocpnToolBarSimple(ocpnFloatingToolbarDialog *parent, wxWindowID winid,
107  const wxPoint &pos = wxDefaultPosition,
108  const wxSize &size = wxDefaultSize,
109  long style = wxNO_BORDER,
110  int orient = wxTB_HORIZONTAL)
111  : m_one_shot(500) {
112 
113  Init();
114 
115  Create(parent, winid, pos, size, style, orient);
116  }
117 
118  bool Create(ocpnFloatingToolbarDialog *parent, wxWindowID winid,
119  const wxPoint &pos = wxDefaultPosition,
120  const wxSize &size = wxDefaultSize,
121  long style = wxNO_BORDER,
122  int orient = wxTB_HORIZONTAL);
123 
124  virtual ~ocpnToolBarSimple();
125 
126  virtual void SetToggledBackgroundColour(wxColour c) {
127  m_toggle_bg_color = c;
128  };
129  virtual void SetBackgroundColour(wxColour c) {m_background_color = c; }
130  virtual wxColour GetBackgroundColour() { return m_background_color;}
131  virtual void SetColorScheme(ColorScheme cs);
132 
133  // event handlers
134  bool OnMouseEvent(wxMouseEvent &event, wxPoint &position);
135  void OnToolTipTimerEvent(wxTimerEvent &event);
136  void OnToolTipOffTimerEvent(wxTimerEvent &event);
137 
138  wxToolBarToolBase *AddTool(int toolid, const wxString &label,
139  const wxBitmap &bitmap,
140  const wxBitmap &bmpDisabled,
141  wxItemKind kind = wxITEM_NORMAL,
142  const wxString &shortHelp = wxEmptyString,
143  const wxString &longHelp = wxEmptyString,
144  wxObject *data = NULL);
145 
146  wxToolBarToolBase *AddTool(int toolid, const wxString &label,
147  const wxBitmap &bitmap,
148  const wxString &shortHelp = wxEmptyString,
149  wxItemKind kind = wxITEM_NORMAL) {
150  return AddTool(toolid, label, bitmap, wxNullBitmap, kind, shortHelp);
151  }
152 
153  wxToolBarToolBase *InsertTool(size_t pos, int id, const wxString &label,
154  const wxBitmap &bitmap,
155  const wxBitmap &bmpDisabled, wxItemKind kind,
156  const wxString &shortHelp,
157  const wxString &longHelp, wxObject *clientData);
158 
159  wxToolBarToolBase *InsertTool(size_t pos, wxToolBarToolBase *tool);
160 
161  // Only allow toggle if returns true. Call when left button up.
162  virtual bool OnLeftClick(int toolid, bool toggleDown);
163 
164  // Call when right button down.
165  virtual void OnRightClick(int toolid, long x, long y);
166 
167  virtual void DoPluginToolUp();
168 
169  bool IsDirty(){ return m_dirty; }
170  void SetDirty( bool value ){ m_dirty = value; }
171 
172  size_t GetToolsCount() const { return m_tools.GetCount(); }
173  void SetToolShowCount(int count) { m_nShowTools = count; }
174  int GetToolShowCount() { return m_nShowTools; }
175 
176  int GetNoRowsOrColumns() { return m_currentRowsOrColumns; };
177  int GetLineCount() { return m_LineCount; };
178  int GetVisibleToolCount();
179 
180  void SetToolNormalBitmapEx(wxToolBarToolBase *tool, const wxString &iconname);
181  void SetToolNormalBitmapSVG(wxToolBarToolBase *tool, wxString fileSVG);
182 
183  void EnableRolloverBitmaps(bool enable) {
184  m_tbenableRolloverBitmaps = enable;
185  }
186 
187  wxBitmap &GetBitmap(){ return m_bitmap; }
188 
189  // get the control with the given id or return NULL
190  virtual wxControl *FindControl(int toolid);
191 
192  // add a separator to the toolbar
193  virtual wxToolBarToolBase *AddSeparator();
194  virtual wxToolBarToolBase *InsertSeparator(size_t pos);
195 
196  // remove the tool from the toolbar: the caller is responsible for actually
197  // deleting the pointer
198  virtual wxToolBarToolBase *RemoveTool(int toolid);
199 
200  // delete tool either by index or by position
201  virtual bool DeleteToolByPos(size_t pos);
202  virtual bool DeleteTool(int toolid);
203 
204  // delete all tools
205  virtual void ClearTools();
206 
207  // must be called after all buttons have been created to finish toolbar
208  // initialisation
209  virtual bool Realize();
210 
211  // tools state
212  // -----------
213 
214  virtual void EnableTool(int toolid, bool enable);
215  virtual void ToggleTool(int toolid, bool toggle);
216 
217  virtual void SetToolBitmaps(int toolid, wxBitmap *bmp, wxBitmap *bmpRollover);
218  virtual void SetToolBitmapsSVG(int id, wxString fileSVGNormal,
219  wxString fileSVGRollover,
220  wxString fileSVGToggled);
221 
222  void InvalidateBitmaps();
223  wxBitmap &CreateBitmap(double display_scale = 1.0);
224 
225  // set/get tools client data (not for controls)
226  virtual wxObject *GetToolClientData(int toolid) const;
227  virtual void SetToolClientData(int toolid, wxObject *clientData);
228 
229  // returns tool pos, or wxNOT_FOUND if tool isn't found
230  virtual int GetToolPos(int id) const;
231 
232  // return true if the tool is toggled
233  virtual bool GetToolState(int toolid) const;
234 
235  virtual bool GetToolEnabled(int toolid) const;
236 
237  virtual void SetToolShortHelp(int toolid, const wxString &helpString);
238  virtual wxString GetToolShortHelp(int toolid) const;
239  virtual void SetToolLongHelp(int toolid, const wxString &helpString);
240  virtual wxString GetToolLongHelp(int toolid) const;
241 
242  virtual void SetToolTooltipHiViz(int id, bool b_hiviz);
243 
244  virtual void SetSizeFactor(float factor) {
245  m_sizefactor = factor;
246  InvalidateBitmaps();
247  }
248  // toolbar geometry
249  // ----------------
250 
251  // the toolbar can wrap - limit the number of columns or rows it may take
252  void SetMaxRowsCols(int rows, int cols) {
253  m_maxRows = rows;
254  m_maxCols = cols;
255  }
256  int GetMaxRows() const { return m_maxRows; }
257  int GetMaxCols() const { return m_maxCols; }
258 
259  // get/set the size of the bitmaps used by the toolbar: should be called
260  // before adding any tools to the toolbar
261  virtual void SetToolBitmapSize(const wxSize &size) {
262  m_defaultWidth = size.x;
263  m_defaultHeight = size.y;
264  }
265  virtual wxSize GetToolBitmapSize() const {
266  return wxSize(m_defaultWidth, m_defaultHeight);
267  }
268 
269  // the button size in some implementations is bigger than the bitmap size:
270  // get the total button size (by default the same as bitmap size)
271  virtual wxSize GetToolSize() const { return GetToolBitmapSize(); }
272 
273  virtual wxRect GetToolRect(int tool_id);
274 
275  // returns a (non separator) tool containing the point (x, y) or NULL if
276  // there is no tool at this point (corrdinates are client)
277  wxToolBarToolBase *FindToolForPosition(wxCoord x, wxCoord y);
278 
279  // find the tool by id
280  wxToolBarToolBase *FindById(int toolid) const;
281 
282  // return true if this is a vertical toolbar, otherwise false
283  bool IsVertical() const { return m_orient == wxTB_VERTICAL; }
284 
285  // the list of all our tools
286  wxToolBarToolsList m_tools;
287 
288  ocpnFloatingToolbarDialog * m_parentContainer;
289 
290  // the maximum number of toolbar rows/columns
291  int m_maxRows;
292  int m_maxCols;
293 
294  // the size of the toolbar bitmaps
295  wxCoord m_defaultWidth, m_defaultHeight;
296 
297  // The size of the Realizeed toolbar
298  wxCoord m_maxWidth, m_maxHeight;
299 
300  void HideTooltip();
301  void KillTooltip();
302  void EnableTooltips();
303  void DisableTooltips();
304 
305 protected:
306  // common part of all ctors
307  void Init();
308 
309  // implement base class pure virtuals
310  virtual wxToolBarToolBase *DoAddTool(
311  int toolid, const wxString &label, const wxBitmap &bitmap,
312  const wxBitmap &bmpDisabled, wxItemKind kind,
313  const wxString &shortHelp = wxEmptyString,
314  const wxString &longHelp = wxEmptyString, wxObject *clientData = NULL,
315  wxCoord xPos = wxDefaultCoord, wxCoord yPos = wxDefaultCoord);
316 
317  virtual bool DoInsertTool(size_t pos, wxToolBarToolBase *tool);
318  virtual bool DoDeleteTool(size_t pos, wxToolBarToolBase *tool);
319 
320  virtual void DoEnableTool(wxToolBarToolBase *tool, bool enable);
321  virtual void DoToggleTool(wxToolBarToolBase *tool, bool toggle);
322 
323  virtual wxToolBarToolBase *CreateTool(int winid, const wxString &label,
324  const wxBitmap &bmpNormal,
325  const wxBitmap &bmpDisabled,
326  wxItemKind kind, wxObject *clientData,
327  const wxString &shortHelp,
328  const wxString &longHelp);
329 
330  // helpers
331  void DrawTool(wxToolBarToolBase *tool);
332  virtual void DrawTool(wxDC &dc, wxToolBarToolBase *tool);
333  void CreateToolBitmap(wxToolBarToolBase *toolBase);
334 
335  bool m_dirty;
336  int m_currentRowsOrColumns;
337  int m_LineCount;
338 
339  int m_pressedTool, m_currentTool;
340 
341  wxCoord m_lastX, m_lastY;
342  wxCoord m_xPos, m_yPos;
343 
344  wxColour m_toggle_bg_color;
345  wxColour m_toolOutlineColour;
346  wxColour m_background_color;
347 
348  ToolTipWin *m_pToolTipWin;
349  ocpnToolBarTool *m_last_ro_tool;
350 
351  ColorScheme m_currentColorScheme;
352 
353  wxTimer m_tooltip_timer;
354  int m_one_shot;
355  wxTimer m_tooltipoff_timer;
356  int m_tooltip_off;
357  bool m_btooltip_show;
358 
359  bool m_btoolbar_is_zooming;
360 
361  ocpnStyle::Style *m_style;
362  int m_orient;
363 
364  float m_sizefactor;
365 
366  int m_last_plugin_down_id;
367  bool m_leftDown;
368  int m_nShowTools;
369  bool m_tbenableRolloverBitmaps;
370 
371  wxBitmap m_bitmap;
372 private:
373  DECLARE_EVENT_TABLE()
374 };
375 
376 //----------------------------------------------------------------------------------------------------------
377 // ocpnFloatingToolbarDialog Specification
378 //----------------------------------------------------------------------------------------------------------
379 
380 #define FADE_TIMER 2
381 #define DESTROY_TIMER 3
382 
383 class ocpnFloatingToolbarDialog : public wxEvtHandler{
384 
385 public:
386  ocpnFloatingToolbarDialog(wxWindow *parent, wxPoint position, long orient,
387  float size_factor);
389 
390  void OnClose(wxCloseEvent &event);
391  void OnWindowCreate(wxWindowCreateEvent &event);
392  void OnToolLeftClick(wxCommandEvent &event);
393  virtual void OnKeyDown(wxKeyEvent &event);
394  virtual void OnKeyUp(wxKeyEvent &event);
395  void OldMouseEvent(wxMouseEvent &event);
396  bool MouseEvent(wxMouseEvent &event);
397  void FadeTimerEvent(wxTimerEvent &event);
398  bool IsToolbarShown() { return (m_ptoolbar != 0); }
399  float GetScaleFactor() { return m_sizefactor; }
400  void DestroyTimerEvent(wxTimerEvent &event);
401  void DrawDC(ocpnDC &dc, double displayScale);
402  void DrawGL(ocpnDC &gldc, double displayScale);
403 
404  void RefreshFadeTimer();
405 
406  void EnableSubmerge(bool enable) { m_benableSubmerge = enable; }
407  void Realize();
408  ocpnToolBarSimple *GetToolbar();
409  ocpnToolBarSimple *CreateNewToolbar();
410  void SetToolbarHideMethod(int n_method) { n_toolbarHideMethod = n_method; }
411 
412  void SetToolConfigString(wxString string) { m_configString = string; }
413  wxString GetToolConfigString() { return m_configString; }
414 
415  float GetSizeFactor() { return m_sizefactor; }
416 
417  void CreateConfigMenu();
418  bool _toolbarConfigMenuUtil(ToolbarItemContainer *tic);
419 
420  void RefreshToolbar();
421 
422  void Submerge();
423  void Surface();
424  void HideTooltip();
425  void ShowTooltips();
426  void EnableTooltips() {
427  if (m_ptoolbar) m_ptoolbar->EnableTooltips();
428  }
429  void DisableTooltips() {
430  if (m_ptoolbar) m_ptoolbar->DisableTooltips();
431  }
432  void UpdateRecoveryWindow(bool b_toolbarEnable);
433  void EnableTool(int toolid, bool enable);
434  void SetToolShortHelp(int toolid, const wxString &helpString);
435 
436  void DestroyToolBar();
437  void ToggleOrientation();
438  void MoveDialogInScreenCoords(wxPoint posn, wxPoint posn_old);
439  void SetDefaultPosition();
440  void LockPosition(bool lock) { m_block = lock; }
441  virtual void SetColorScheme(ColorScheme cs);
442  ColorScheme GetColorScheme() { return m_cs; }
443  bool CheckSurfaceRequest(wxMouseEvent &event);
444  void GetFrameRelativePosition(int *x, int *y);
445  void RestoreRelativePosition(int x, int y);
446 
447  void SetGeometry(bool bAvoid, wxRect rectAvoid);
448  void SetMinX(int offset) { m_dock_min_x = offset; }
449  void SetMinY(int offset) { m_dock_min_y = offset; }
450  long GetOrient() { return m_orient; }
451  wxSize GetToolSize();
452  wxRect GetToolbarRect();
453  wxSize GetToolbarSize();
454  wxPoint GetToolbarPosition();
455 
456  void SetAutoHideTimer(int time);
457  void SetAutoHide(bool hide) { m_bAutoHideToolbar = hide; }
458 
459  size_t GetToolCount();
460  void SetToolShowMask(wxString mask);
461  wxString GetToolShowMask(void) { return m_toolShowMask; }
462 
463  void SetToolShowCount(int count);
464  int GetToolShowCount(void);
465 
466  bool CheckAndAddPlugInTool(ocpnToolBarSimple *tb);
467  bool AddDefaultPositionPlugInTools(ocpnToolBarSimple *tb);
468 
469  int GetDockX() { return m_dock_x; }
470  int GetDockY() { return m_dock_y; }
471  void SetDockX(int x) { m_dock_x = x; }
472  void SetDockY(int y) { m_dock_y = y; }
473 
474  void SetYAuxOffset(int offset) { m_auxOffsetY = offset; }
475 
476  void SetCanToggleOrientation(bool enable) { b_canToggleOrientation = enable; }
477  bool GetCanToggleOrientation() { return b_canToggleOrientation; }
478 
479  bool toolbarConfigChanged;
480 
481  wxMenu *m_FloatingToolbarConfigMenu;
482 
483  wxString m_tblastAISiconName;
484  wxToolBarToolBase *m_pTBAISTool;
485  bool m_toolbar_scale_tools_shown;
486  void SetBackGroundColorString(wxString colorRef);
487  void SetULDockPosition(wxPoint position);
488 
489  ArrayOfToolbarItemContainer m_Items;
490 
491  void AddToolItem(ToolbarItemContainer *item);
492  int RebuildToolbar();
493  void EnableRolloverBitmaps(bool bEnable);
494  bool GetEnableRolloverBitmaps() { return m_enableRolloverBitmaps; }
495 
496 protected:
497  ocpnToolBarSimple *m_ptoolbar;
498 
499 private:
500  void DoFade(int value);
501 
502  bool m_bsubmerged;
503 
504  wxWindow *m_pparent;
505  wxBoxSizer *m_topSizer;
506 
507  long m_orient;
508  wxTimer m_fade_timer;
509  int m_opacity;
510  ColorScheme m_cs;
511 
512  wxPoint m_position;
513  int m_dock_x;
514  int m_dock_y;
515  int m_dock_min_x;
516  int m_dock_min_y;
517 
518  ocpnStyle::Style *m_style;
519  bool m_block;
520 
521  bool m_marginsInvisible;
522  float m_sizefactor;
523  wxTimer m_destroyTimer;
524  wxSize m_recoversize;
525 
526  bool m_bAutoHideToolbar;
527  int m_nAutoHideToolbar;
528  bool m_benableSubmerge;
529 
530  wxString m_backcolorString;
531  int m_cornerRadius;
532  wxString m_toolShowMask;
533  int n_toolbarHideMethod;
534  bool b_canToggleOrientation;
535  wxString m_configString;
536  bool m_enableRolloverBitmaps;
537  int m_auxOffsetY;
538 
539  unsigned int m_texture;
540  wxImage m_toolbar_image;
541 };
542 
543 //---------------------------------------------------------------------------
544 
545 class ToolbarMOBDialog : public wxDialog {
546 private:
547  std::vector<wxRadioButton *> choices;
548 
549 public:
550  ToolbarMOBDialog(wxWindow *parent);
551  int GetSelection();
552 };
553 
554 #define SYMBOL_ToolbarChoices_STYLE \
555  wxCAPTION | wxRESIZE_BORDER | wxSYSTEM_MENU | wxCLOSE_BOX
556 
557 class ToolbarChoicesDialog : public wxDialog {
558  DECLARE_DYNAMIC_CLASS(ToolbarChoicesDialog)
559  DECLARE_EVENT_TABLE()
560 
561 public:
564  ToolbarChoicesDialog(wxWindow *parent, ocpnFloatingToolbarDialog *sponsor, wxWindowID id = -1,
565  const wxString &caption = _T(""),
566  const wxPoint &pos = wxDefaultPosition,
567  const wxSize &size = wxDefaultSize,
568  long style = SYMBOL_ToolbarChoices_STYLE);
569 
571 
572  void SetColorScheme(ColorScheme cs);
573  void RecalculateSize(void);
574  void CreateControls();
575 
576  void OnCancelClick(wxCommandEvent &event);
577  void OnOkClick(wxCommandEvent &event);
578 
579  wxButton *m_CancelButton;
580  wxButton *m_OKButton;
581 
582  std::vector<wxCheckBox *> cboxes;
583  wxMenu *m_configMenu;
584  ocpnFloatingToolbarDialog *m_ToolbarDialogAncestor;
585 };
586 
587 #endif
ToolbarChoicesDialog()
Constructors.
Definition: toolbar.cpp:2351
Definition: ocpndc.h:58