Module: TkAutoPostMenu

Defined in:
lib/a-tkcommons.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(_widget) ⇒ Object



1810
1811
1812
# File 'lib/a-tkcommons.rb', line 1810

def self.extended(_widget)
  _widget.__initialize_posting(_widget)
end

.included(_widget) ⇒ Object



1814
1815
1816
# File 'lib/a-tkcommons.rb', line 1814

def self.included(_widget)
  _widget.__initialize_posting(_widget)
end

Instance Method Details

#__initialize_posting(_widget = self) ⇒ Object



1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
# File 'lib/a-tkcommons.rb', line 1829

def __initialize_posting(_widget=self)
  #    parent = TkWinfo.parent(_widget)
  #    parent.bind_append("Enter", proc{p "Enter parent"})
  #    parent.bind_append("Leave", proc{p "Leave parent"})

  chs = TkWinfo.children(_widget)
  hh = 22
  @last_post = nil
  @posting_on = true
  @event_posting_on = false
  @m_show = Hash.new
  chs.each{|ch|
    next if ch.kind_of?(String)
    ch.menu.bind_append("Map", proc{ @m_show[ch.menu] = true })
    ch.menu.bind_append("Unmap", proc{ @m_show[ch.menu] = false })
    ch.bind_append("Enter", proc{|x,y,rx,ry|
      if @posting_on
        if @last_post && @last_post != ch.menu
          @last_post.unpost
          @last_post=nil
        end
        if @last_clicked && @last_clicked != ch.menu
          @last_clicked.unpost
          @last_clicked = nil
        end
        
        ch.menu.post(x-rx,y-ry+hh)
        #just_posted = TkWinfo.containing(x, y+hh)

        chmenus = TkWinfo.children(ch)
        @last_menu_posted = chmenus[0]
        @last_menu_posted.set_focus

        @last_menu_posted.bind("Enter", proc{
          @last_menu_posted.bind("Leave", proc{
            if @posting_on
              @last_post.unpost if @last_post
              @last_post = nil
              @last_menu_posted.bind("Enter", proc{})
              @last_menu_posted.bind("Leave", proc{})
            end
          })
        })
        #@last_post = just_posted
      end

      #@last_post=ch.menu
    }, "%X %Y %x %y")
    ch.bind_append("Leave", proc{
      ch.configure("state"=>:normal, "relief"=>:flat)
      if @posting_on
        if @last_post
          _x = TkWinfo.x(@last_post)
          _y = TkWinfo.y(@last_post)
          ch.event_generate("KeyPress", :keysym=>"Escape")  if Tk.focus.kind_of?(TkMenu) &&  Tk.focus != ch.menu
          @last_post.post(_x,_y) if @last_clicked && @last_clicked == ch.menu
        end
        if @last_post!=ch.menu
          @last_post=ch.menu
        else
          @last_post=nil
        end
        if !Tk.focus.kind_of?(TkMenu)
          @last_post.unpost if @last_post
          @last_post=nil
        end
      end
    })
    ch.bind_append("1", proc{|x,y,rx,ry|
      @posting_on=true if @event_posting_on
      if @last_post && @last_post != ch.menu
        @last_post.unpost
     #   @last_post.bind_remove("1")
        @last_post = nil
      end
      @last_post=ch.menu #if ch.state == 'active'
      ch.configure('state'=>'normal')
      @last_clicked = ch.menu
      
#        ch.menu.bind_append("1", proc{|mx,my,mrx,mry|
#          if ch.menu.index("active").nil?
#            ch.menu.activate(ch.menu.index("@#{mry}")) 
#          end
#        }, "%X %Y %x %y")
      
      #@last_post.unpost
      #@last_post.post(0,0)
      #@last_post.set_focus
      @posting_on=true  if @event_posting_on

    }, "%X %Y %x %y")

  }
  _widget.bind_append("Leave", proc{
    if @posting_on  && Tk.focus != @last_menu_posted
      @last_post.unpost if @last_post
      @last_post=nil
      @posting_on = false if @event_posting_on
    end
    TkAfter.new(1000,1, proc{
      one_post = false
      @m_show.each{|m,v|
        one_post = v
        break if v
      }
      @posting_on = one_post if @event_posting_on
    }).start
  })

end

#event_posting_offObject



1823
1824
1825
1826
# File 'lib/a-tkcommons.rb', line 1823

def event_posting_off
  @event_posting_on = false
  @posting_on = true
end

#event_posting_onObject



1818
1819
1820
1821
# File 'lib/a-tkcommons.rb', line 1818

def event_posting_on
  @event_posting_on = true
  @posting_on = false
end