from HaikuR1.ApplicationKit import Application, Message
from HaikuR1.InterfaceKit import Window, View, Menu, MenuItem, MenuBar, MenuField, PopUpMenu
from HaikuR1.InterfaceKit import B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE, B_SECONDARY_MOUSE_BUTTON, B_CONTROL_KEY, B_FOLLOW_ALL, B_FOLLOW_BOTTOM, B_FOLLOW_LEFT_RIGHT, B_WILL_DRAW
from HaikuR1.SupportKit import code_to_type
class ColorView(View):
def MouseDown(self, point):
# hook doesn't give us buttons, so use CurrentMessage to get that info
window = self.Window()
message = window.CurrentMessage()
button = message.FindInt32("buttons")
if button != B_SECONDARY_MOUSE_BUTTON:
return
popup = PopUpMenu(
name = 'ColorPopUp'
)
MenuWindow.populate_menu(popup)
popup.SetTargetForItems(window)
popup.Go(
where = self.ConvertToScreen(point),
deliversMessage = 1,
)
class MenuWindow(Window):
SET_COLOR = code_to_type('colr')
spacer = 5
view_width = 150
view_height = 150
control_height = 25
colors = [
[ 'Red', 255, 0, 0 ],
[ 'Green', 0, 255, 0 ],
[ 'Blue', 0, 0, 255 ],
[ 'Black', 0, 0, 0 ],
]
@staticmethod
def populate_menu(menu):
for color in MenuWindow.colors:
name = color[0]
rgb = color[1:4]
msg = Message(MenuWindow.SET_COLOR)
msg.AddColor("color", rgb)
item = MenuItem(
label = name,
message = msg,
)
menu.AddItem(item)
def __init__(self, *args, **kwargs):
menubar = MenuBar(
frame = [0,0,20,1],
name = 'MenuBar',
resizingMode = B_FOLLOW_LEFT_RIGHT,
)
self.AddChild(menubar)
menubar_height = menubar.Frame().Height()
window_width = MenuWindow.view_width + 2*MenuWindow.spacer
window_height = menubar_height + MenuWindow.view_height + MenuWindow.control_height + 3*MenuWindow.spacer
self.ResizeTo(window_width, window_height)
menu1 = Menu("Colors")
MenuWindow.populate_menu(menu1)
i = 0
shortcuts = ['R', 'G', 'B', 'K']
for shortcut in shortcuts:
item = menu1.ItemAt(i)
i = i+1
item.SetShortcut(shortcut, B_CONTROL_KEY)
menubar.AddItem(menu1)
left = MenuWindow.spacer
top = menubar_height + MenuWindow.spacer
self.view = ColorView(
frame = [left,top,left+MenuWindow.view_width,top+MenuWindow.view_height],
name = 'ColorView',
flags = B_WILL_DRAW,
resizingMode = B_FOLLOW_ALL,
)
self.AddChild(self.view)
self.view.SetViewColor([0,0,160])
top += MenuWindow.view_height + MenuWindow.spacer
menu2 = PopUpMenu(
name = "Colors"
)
MenuWindow.populate_menu(menu2)
menufield = MenuField(
frame = [left,top,left+MenuWindow.view_width,top+MenuWindow.control_height],
name = "MenuField",
menu = menu2,
label = 'Colors',
resizingMode = B_FOLLOW_BOTTOM,
fixedSize = 1,
)
self.AddChild(menufield)
def MessageReceived(self, message):
if message.what == MenuWindow.SET_COLOR:
color = message.FindColor("color")
self.view.SetViewColor(color)
self.view.Invalidate()
return
super(MenuWindow, self).MessageReceived(message)
app = Application("application/x-vnd.hab.python.Menus")
window = MenuWindow(
title = "Menus",
frame = [50,50,100,100],
type = B_TITLED_WINDOW,
flags = B_QUIT_ON_WINDOW_CLOSE,
)
window.Show()
app.Run()