Menu

Show tooltip when hovering over a shape

iwbnwif
2015-08-29
2016-08-05
  • iwbnwif

    iwbnwif - 2015-08-29

    Hi,

    I was wondering if it is interesting for anyone to have the option to be able to show a tooltip when the user hovers the mouse pointer over the top of a shape?

    I am interested in this as a new feature and welcome any thoughts or suggestions.

     
  • iwbnwif

    iwbnwif - 2016-08-05

    It turns out that this is very simple and very effective.

    In the ctor for wxSFShapeCanvas (or its derivative) add the following:

        // Initialise the shape hover timer and set up the tool tip. 
        // The hover timer will be started when the user first moves the mouse over the canvas.
        m_HoverTimer.SetOwner( this );
        SetToolTip( "" );
        GetToolTip()->SetDelay( 0 );
        Bind( wxEVT_TIMER, &wxSFShapeCanvas::OnHoverTimer, this );
    

    Then in the ::OnMouseMove method:

        // NOTE: On GTK, setting and unsetting the tooltip works well, however on Windows, the tooltip is not shown
        // until it is set AND THEN the mouse is moved. Therefore, for Windows at least, it is necessary to show and
        // hide by changing the text.
    
        if (GetToolTip())
        {
            // If the tooltip isn't being shown (text blank), reset the timer.
            if (GetToolTipText() == "")
                m_HoverTimer.Start (500, wxTIMER_ONE_SHOT);
    
            // Otherwise, if the tip is shown and the cursor is no longer over a shape, clear the text.
            else if (!GetShapeUnderCursor())
                GetToolTip()->SetTip ("");
        }
    

    Finally the hover timer event handler:

    void wxSFShapeCanvas::OnHoverTimer( wxTimerEvent& event )
    {
        wxSFShapeBase* shape = GetShapeUnderCursor();
    
        GetToolTip()->SetTip( shape->GetTipText() );
    }
    

    Of course this needs a corresponding modification to wxSFShapeBase to include a tip text getters and setters.

     

Log in to post a comment.