1 module PixelPerfectEngine.concrete.elements.menubar;
2 
3 public import PixelPerfectEngine.concrete.elements.base;
4 public import PixelPerfectEngine.concrete.popup;
5 
6 /**
7  * Menubar containing menus in a tree-like structure.
8  */
9 public class MenuBar : WindowElement {
10 	private PopUpMenuElement[] menus;
11 	//private wstring[] menuNames;
12 	private int[] menuWidths;
13 	//private PopUpHandler popUpHandler;
14 	private int select, usedWidth;
15 	public EventDeleg onMenuEvent;
16 	public this(string source, Box position, PopUpMenuElement[] menus, StyleSheet customStyle = null){
17 		this.customStyle = customStyle;
18 		this.source = source;
19 		this.position = position;
20 		//this.popUpHandler = popUpHandler;
21 		this.menus = menus;
22 		select = -1;
23 		menuWidths.length = menus.length + 1;
24 		menuWidths[0] = position.left;
25 		const int spacing = 2 * getStyleSheet().drawParameters["MenuBarHorizPadding"];
26 		for (size_t i ; i < menus.length ; i++) {
27 			menuWidths[i + 1] = menuWidths[i] + menus[i].text.getWidth + spacing;
28 		}
29 	}
30 	public override void draw() {
31 		StyleSheet ss = getStyleSheet();
32 		with (parent) {
33 			drawFilledBox(position, ss.getColor("window"));
34 			drawLine(position.cornerUL, position.cornerUR, ss.getColor("windowascent"));
35 			drawLine(position.cornerUL, position.cornerLL, ss.getColor("windowascent"));
36 			drawLine(position.cornerLL, position.cornerLR, ss.getColor("windowdescent"));
37 			drawLine(position.cornerUR, position.cornerLR, ss.getColor("windowdescent"));
38 		}
39 		if (select > -1) {
40 			parent.drawFilledBox(Box(menuWidths[select], position.top + 1, menuWidths[select + 1], position.bottom - 1), 
41 					ss.getColor("selection"));
42 		}
43 		foreach (size_t i, PopUpMenuElement menuItem ; menus) {
44 			parent.drawTextSL(Box(menuWidths[i], position.top, menuWidths[i+1], position.bottom), menuItem.text, Point(0,0));
45 		}
46 	}
47 	private void redirectIncomingEvents(Event ev){
48 		if(onMenuEvent !is null){
49 			onMenuEvent(ev);
50 		}
51 	}
52 	///Passes mouse click event
53 	public override void passMCE(MouseEventCommons mec, MouseClickEvent mce) {
54 		if (mce.state) {
55 			for (int i ; i < menus.length ; i++) {
56 				if (menuWidths[i] < mce.x && menuWidths[i + 1] > mce.x) {
57 					select = i;
58 					draw;
59 					Coordinate c = parent.getAbsolutePosition(this);
60 					parent.addPopUpElement(new PopUpMenu(menus[i].getSubElements, source, &redirectIncomingEvents), c.left + 
61 							menuWidths[i], c.bottom);
62 					super.passMCE(mec, mce);
63 					return;
64 				}
65 			}
66 		}
67 		select = -1;
68 		super.passMCE(mec, mce);
69 	}
70 	///Passes mouse move event
71 	public override void passMME(MouseEventCommons mec, MouseMotionEvent mme) {
72 		if (position.isBetween(mme.x, mme.y)) {
73 			for (int i ; i < menus.length ; i++) {
74 				if (menuWidths[i] < mme.x && menuWidths[i + 1] > mme.x) {
75 					select = i;
76 					draw;
77 					return;
78 				}
79 			}
80 		}
81 		select = -1;
82 		draw;
83 		super.passMME(mec, mme);
84 	}
85 	///Called when an object loses focus.
86 	public override void focusTaken() {
87 		select = -1;
88 		super.focusTaken();
89 	}
90 }