1 // Written in the D programming language.
2 
3 /**
4 
5 This module implements support of tool bars.
6 
7 ToolBarHost is layout to hold one or more toolbars.
8 
9 ToolBar is bar with tool buttons and other controls arranged horizontally.
10 
11 Synopsis:
12 
13 ----
14 import dlangui.widgets.toolbars;
15 ----
16 
17 
18 Copyright: Vadim Lopatin, 2015
19 License:   Boost License 1.0
20 Authors:   Vadim Lopatin, coolreader.org@gmail.com
21 */
22 module dlangui.widgets.toolbars;
23 
24 import dlangui.widgets.widget;
25 import dlangui.widgets.layouts;
26 import dlangui.widgets.controls;
27 import dlangui.widgets.combobox;
28 
29 /// Layout with several toolbars
30 class ToolBarHost : HorizontalLayout {
31     this(string ID) {
32         super(ID);
33     }
34     this() {
35         this("TOOLBAR_HOST");
36         styleId = STYLE_TOOLBAR_HOST;
37     }
38     /// create and add new toolbar (returns existing one if already exists)
39     ToolBar getOrAddToolbar(string ID) {
40         ToolBar res = getToolbar(ID);
41         if (!res) {
42             res = new ToolBar(ID);
43             addChild(res);
44         }
45         return res;
46     }
47     /// get toolbar by id; null if not found
48     ToolBar getToolbar(string ID) {
49         Widget res = childById(ID);
50         if (res) {
51             ToolBar tb = cast(ToolBar)res;
52             return tb;
53         }
54         return null;
55     }
56     /// override to handle specific actions
57     override bool handleAction(const Action a) {
58         // route to focused control first, then to main widget
59         return window.dispatchAction(a);
60     }
61 
62     /// map key to action
63     override Action findKeyAction(uint keyCode, uint flags) {
64         for (int i = 0; i < childCount; i++) {
65             auto a = child(i).findKeyAction(keyCode, flags);
66             if (a)
67                 return a;
68         }
69         return null;
70     }
71 }
72 
73 /// image button for toolbar
74 class ToolBarImageButton : ImageButton {
75     this(const Action a) {
76         super(a);
77         styleId = STYLE_TOOLBAR_BUTTON;
78         focusable = false;
79     }
80     mixin ActionTooltipSupport;
81 }
82 
83 /// separator for toolbars
84 class ToolBarSeparator : ImageWidget {
85     this() {
86         super("separator", "toolbar_separator");
87         styleId = STYLE_TOOLBAR_SEPARATOR;
88     }
89 }
90 
91 /// separator for toolbars
92 class ToolBarComboBox : ComboBox {
93     this(string ID, dstring[] items) {
94         super(ID, items);
95         styleId = STYLE_TOOLBAR_CONTROL;
96         if (items.length > 0)
97             selectedItemIndex = 0;
98     }
99     mixin ActionTooltipSupport;
100 }
101 
102 /// Layout with buttons
103 class ToolBar : HorizontalLayout {
104     this(string ID) {
105         super(ID);
106         styleId = STYLE_TOOLBAR;
107     }
108     this() {
109         this("TOOLBAR");
110     }
111     void addCustomControl(Widget widget) {
112         addChild(widget);
113     }
114     /// adds image button to toolbar
115     void addButtons(const Action[] actions...) {
116         foreach(a; actions) {
117             if (a.isSeparator) {
118                 addChild(new ToolBarSeparator());
119             } else {
120                 Widget btn;
121                 if (a.iconId) {
122                     btn = new ToolBarImageButton(a);
123                 } else {
124                     btn = new Button(a);
125                     btn.styleId = STYLE_TOOLBAR_BUTTON;
126                 }
127                 addChild(btn);
128             }
129         }
130     }
131 
132     void addControl(Widget widget) {
133         addChild(widget);
134     }
135 
136     /// map key to action
137     override Action findKeyAction(uint keyCode, uint flags) {
138         for (int i = 0; i < childCount; i++) {
139             auto a = child(i).findKeyAction(keyCode, flags);
140             if (a)
141                 return a;
142         }
143         return null;
144     }
145 }