Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 76 additions & 60 deletions lib/StandardGUI/menubar.rb
Original file line number Diff line number Diff line change
@@ -1,79 +1,95 @@
# coding: utf-8
require_relative './popupmenu'
require_relative './common'
require_relative './image'

module WS
# ウィンドウにくっつけるメニューバー
class WSMenuBar < WSContainer

class WSMenuBarItem < WSImage
attr_reader :item
attr_accessor :popup

def initialize(str, item, font=nil)
@str = str
@item = item
@font = font if font
w = @font.getWidth(@str)

super(nil,nil,w+10,@font.size+4)

@image = {true => Image.new(w + 10, @font.size + 4).draw(4, 0, Image.new(w + 2, @font.size + 4, [150,150,150])).draw_font_ex(5, 2, @str, @font, :color=>COLOR[:font],:aa=>false),
false => Image.new(w + 10, @font.size + 4).draw_font_ex(5, 2, @str, @font, :color=>COLOR[:font],:aa=>false)}
self.image = @image[false]

@mouse_on = false
@popup = false
end

def on_mouse_push(tx, ty)
@mouse_on = false

self.parent.popup(self)

super
end

def on_mouse_move(tx, ty)
@mouse_on = true

self.parent.popup(self) if @popup && WS.captured?(@popup)

super
end

def on_mouse_out
@mouse_on = false

super
end

def render
self.image = @image[@mouse_on]

super
end
end

def initialize(menuitems)
super(0, 0, 10, 16) # 数字テキトー。オートレイアウトで設定する。
@menuitems = menuitems
self.image.bgcolor = COLOR[:base]
super(nil, nil, nil, 16) #オートレイアウトで設定する。
@font = Font.new(12)
@selected = nil
@menuitems = ary = menuitems.map{|ary|
mbi = WSMenuBarItem.new(ary[0], ary[1], @font)
self.add_control(mbi)
mbi
}
self.image.bgcolor = COLOR[:base]
@popup = nil
end

# メニュークリックでポップアップメニューを表示する
def on_mouse_push(tx, ty)
x = 5
@selected = nil
@menuitems.each_with_index do |ary, i|
tmp = @font.get_width(ary[0])
if x - 5 <= tx and tx < x + tmp + 5
WS.desktop.remove_control(@popup) if @popup
tmpx, tmpy = self.get_global_vertex
@popup = WSPopupMenu.new(x + tmpx, 16 + tmpy, ary[1])
WS.desktop.add_control(@popup)
@popup.object = self
super
WS.capture(@popup)

layout(:left_ex) do
ary.each do |item|
add item
end
x += tmp + 10
end
super
end

# マウスの移動でどのメニューが選択されているかを判定し、選択する
def on_mouse_move(tx, ty)
x = 5
@selected = nil
@menuitems.each_with_index do |ary, i|
tmp = @font.get_width(ary[0])
if x - 5 <= tx and tx < x + tmp + 5
@selected = i
if @popup and WS.captured?(@popup)
WS.desktop.remove_control(@popup) if @popup
tmpx, tmpy = self.get_global_vertex
@popup = WSPopupMenu.new(x + tmpx, 16 + tmpy, ary[1])
WS.desktop.add_control(@popup)
@popup.object = self
super
WS.capture(@popup)
end
end
x += tmp + 10

def popup(item)
WS.desktop.remove_control(@popup) if @popup
tmpx, tmpy = item.get_global_vertex
@popup = WSPopupMenu.new(tmpx, @font.size + 4 + tmpy, item.item)
WS.desktop.add_control(@popup)
@popup.object = self
WS.capture(@popup)

@menuitems.each do |ctl|
ctl.popup = @popup
end
super
end

def on_mouse_out
@selected = nil
super
end

def render
x = 5
@menuitems.each_with_index do |ary, i|
tmp = @font.get_width(ary[0])
if @selected == i
image = Image.new(tmp + 2, 16, [150, 150, 150])
self.image.draw(x - 1, 0, image)
end
self.image.draw_font(x, 2, ary[0], @font, :color=>COLOR[:font])
x += tmp + 10
end

def resize(width, height)
super
self.height = @min_height
end
end
end
Loading