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 		if (onDraw !is null) {
47 			onDraw();
48 		}
49 	}
50 	private void redirectIncomingEvents(Event ev){
51 		if(onMenuEvent !is null){
52 			onMenuEvent(ev);
53 		}
54 	}
55 	///Passes mouse click event
56 	public override void passMCE(MouseEventCommons mec, MouseClickEvent mce) {
57 		if (state != ElementState.Enabled) return;
58 		if (mce.state) {
59 			for (int i ; i < menus.length ; i++) {
60 				if (menuWidths[i] < mce.x && menuWidths[i + 1] > mce.x) {
61 					select = i;
62 					draw;
63 					Coordinate c = parent.getAbsolutePosition(this);
64 					parent.addPopUpElement(new PopUpMenu(menus[i].getSubElements, source, &redirectIncomingEvents), c.left + 
65 							menuWidths[i], c.bottom);
66 					super.passMCE(mec, mce);
67 					return;
68 				}
69 			}
70 		}
71 		select = -1;
72 		super.passMCE(mec, mce);
73 	}
74 	///Passes mouse move event
75 	public override void passMME(MouseEventCommons mec, MouseMotionEvent mme) {
76 		if (position.isBetween(mme.x, mme.y)) {
77 			for (int i ; i < menus.length ; i++) {
78 				if (menuWidths[i] < mme.x && menuWidths[i + 1] > mme.x) {
79 					select = i;
80 					draw;
81 					return;
82 				}
83 			}
84 		}
85 		select = -1;
86 		draw;
87 		super.passMME(mec, mme);
88 	}
89 	///Called when an object loses focus.
90 	public override void focusTaken() {
91 		select = -1;
92 		super.focusTaken();
93 	}
94 }