#!/usr/bin/python

# tz_wx_widgets_hover_tip.py
#       --copyright--                   Copyright 2007 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       May 8, 2007             bar
#       November 18, 2007       bar     turn on doxygen
#       November 20, 2007       bar     comments
#       December 12, 2007       bar     put in tzpython 'cause it's generic
#       May 17, 2008            bar     email adr
#       May 27, 2012            bar     doxygen namespace
#       --eodstamps--
##      \file
#       \namespace              tzpython.tz_wx_widgets_hover_tip
#
#
#       Show a yellow mouse-hover tip over a window.
#
#

from    types                   import  FunctionType

import  wx


class a_hover_tip(wx.PopupWindow) :
    """ Class to manage mouse-hover tips. """

    def __init__(me, parent_win, cb_obj_or_fun = None) :
        """ Constructor. """

        me.cb_obj_or_fun    =   cb_obj_or_fun

        wx.PopupWindow.__init__(me, parent_win, wx.SIMPLE_BORDER)

        me.SetBackgroundColour("YELLOW")

        me.txt  =   wx.StaticText(me, -1, "")
        me.txt.SetPosition(wx.Point(5, 5))

        me.eh       =   wx.EvtHandler()
        wx.EVT_ENTER_WINDOW(me.eh,   me.on_mouse_enter)
        wx.EVT_MOTION(      me.eh,   me.on_mouse_move)
        wx.EVT_LEAVE_WINDOW(me.eh,   me.on_mouse_leave)

        parent_win.PushEventHandler(me.eh)




    def _callback(me) :
        """ If we have an owner, let's let him set the text of the tip. """

        if  me.cb_obj_or_fun :
            if  isinstance(me.cb_obj_or_fun, FunctionType) :
                me.txt.SetLabel(me.cb_obj_or_fun(              me, me.x, me.y))
            else :
                me.txt.SetLabel(me.cb_obj_or_fun.get_hover_tip(me, me.x, me.y))
            pass
        pass


    def _change(me, evt) :
        """ The mouse situation has changed. Update the tooltip appropriately. """

        me.x    = evt.GetX()
        me.y    = evt.GetY()

        me._callback()


        p       = evt.GetEventObject().ClientToScreen(evt.GetPosition())
        sz      = me.txt.GetBestSize()

        me.SetDimensions(p.x + 30, p.y + 30, sz.width + 10, sz.height + 10)

        if  me.txt.GetLabel() :
            me.Show(True)
        else :
            me.Show(False)
        pass


    def on_mouse_enter(me, evt = None) :
        """ Callback for when the mouse enters the area it needs a tooltip for. """

        if  (not evt.LeftIsDown()) and (not evt.MiddleIsDown()) and (not evt.RightIsDown()) :
            me._change(evt)
        evt.Skip()                  # do any event handling that the window has for itself


    def on_mouse_move(me, evt = None) :
        """ Callback for when the mouse moves inside the area it needs a tooltip for. """

        if  (not evt.LeftIsDown()) and (not evt.MiddleIsDown()) and (not evt.RightIsDown()) :
            me._change(evt)
        else :
            me.Show(False)
        evt.Skip()


    def on_mouse_leave(me, evt = None) :
        """ Callback for when the mouse leaves the area it needs a tooltip for. """

        me.Show(False)
        evt.Skip()


    def refresh(me) :
        """ Called by our owner object to refresh the mouse-hover tooltip. """
        if  me.IsShown() :
            me._callback()
        pass


    pass    # a_hover_tip


if  __name__ == '__main__' :
    pass

#
#
#
# eof
