Class: Tk::Tooltip

Inherits:
Struct
  • Object
show all
Defined in:
lib/ver/tooltip.rb

Overview

Translation from Tcl: wiki.tcl.tk/1954

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



3
4
5
# File 'lib/ver/tooltip.rb', line 3

def text
  @text
end

#tooltipObject

Returns the value of attribute tooltip

Returns:

  • (Object)

    the current value of tooltip



3
4
5
# File 'lib/ver/tooltip.rb', line 3

def tooltip
  @tooltip
end

Instance Method Details

#bind_to(widget) ⇒ Object



4
5
6
7
8
9
# File 'lib/ver/tooltip.rb', line 4

def bind_to(widget)
  widget.bind('<Any-Enter>'   ){ Tk::After.ms(500){ show(widget) }}
  widget.bind('<Any-Leave>'   ){ Tk::After.ms(500){ destroy }}
  widget.bind('<Any-KeyPress>'){ Tk::After.ms(500){ destroy }}
  widget.bind('<Any-Button>'  ){ Tk::After.ms(500){ destroy }}
end

#destroyObject



11
12
13
14
# File 'lib/ver/tooltip.rb', line 11

def destroy
  tooltip.destroy
rescue NoMethodError, RuntimeError => ex
end

#show_on(widget) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ver/tooltip.rb', line 16

def show_on(widget)
  root = Tk.root
  root_pointerx, root_pointery = root.winfo_pointerx, root.winfo_pointery
  return unless Tk::Winfo.containing(root_pointerx, root_pointery)

  destroy

  scrh, scrw = widget.winfo_screenheight, widget.winfo_screenwidth

  self.tooltip = tooltip = Tk::Toplevel.new(widget, bd: true, bg: 'black')
  tooltip.wm_geometry = "+#{scrh}+#{scrw}"
  tooltip.wm_overrideredirect = true
  tooltip.attributes(topmost: true) if tooltip.tk_windowingsystem == :win32

  label = Tk::Label.new(tooltip, bg: :lightyellow, fg: :black,
                                 text: text, justify: :left).pack

  width, height = label.winfo_reqwidth, label.winfo_reqheight

  position_x, position_y = root_pointerx, root_pointery + 25
  root_screen_width = root.winfo_screenwidth

  if (position_x + width) > root_screen_width
    position_x -= ((position_x + width) - root_screen_width)
  end

  tooltip.wm_geometry = "#{width}x#{height}+#{position_x}+#{position_y}"
  tooltip.raise

  tooltip.bind('Any-Enter'){ destroy }
  tooltip.bind('Any-Leave'){ destroy }
end