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