Debian Codegraph @

Nasal/canvas/gui/styles/DefaultStyle.nas

raw
1 var DefaultStyle = {
2 new: func(name, name_icon_theme)
3 {
4 return {
5 parents: [ gui.Style.new(name, name_icon_theme),
6 DefaultStyle ]
7 };
8 },
9 createWidget: func(parent, type, cfg)
10 {
11 var factory = me.widgets[type];
12 if( factory == nil )
13 {
14 debug.warn("DefaultStyle: unknown widget type (" ~ type ~ ")");
15 return nil;
16 }
17
18 var w = {
19 parents: [factory],
20 _style: me
21 };
22 call(factory.new, [parent, cfg], w);
23 return w;
24 },
25 widgets: {}
26 };
27
28 # A button
29 DefaultStyle.widgets.button = {
30 new: func(parent, cfg)
31 {
32 me._root = parent.createChild("group", "button");
33 me._bg =
34 me._root.createChild("path");
35 me._border =
36 me._root.createChild("image", "button")
37 .set("slice", "10 12"); #"7")
38 me._label =
39 me._root.createChild("text")
40 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
41 .set("character-size", 14)
42 .set("alignment", "center-baseline");
43 },
44 setSize: func(model, w, h)
45 {
46 me._bg.reset()
47 .rect(3, 3, w - 6, h - 6, {"border-radius": 5});
48 me._border.setSize(w, h);
49 },
50 setText: func(model, text)
51 {
52 me._label.setText(text);
53
54 var min_width = text ? math.max(80, me._label.maxWidth() + 16) : 16;
55 model.setLayoutMinimumSize([min_width, 16]);
56 model.setLayoutSizeHint([min_width, 28]);
57
58 return me;
59 },
60 update: func(model)
61 {
62 var backdrop = !model._windowFocus();
63 var (w, h) = model._size;
64 var file = me._style._dir_widgets ~ "/";
65
66 # TODO unify color names with image names
67 var bg_color_name = "button_bg_color";
68 if( backdrop )
69 bg_color_name = "button_backdrop_bg_color";
70 else if( !model._enabled )
71 bg_color_name = "button_bg_color_insensitive";
72 else if( model._down )
73 bg_color_name = "button_bg_color_down";
74 else if( model._hover )
75 bg_color_name = "button_bg_color_hover";
76 me._bg.set("fill", me._style.getColor(bg_color_name));
77
78 if( backdrop )
79 {
80 file ~= "backdrop-";
81 me._label.set("fill", me._style.getColor("backdrop_fg_color"));
82 }
83 else
84 me._label.set("fill", me._style.getColor("fg_color"));
85 file ~= "button";
86
87 if( model._down )
88 {
89 file ~= "-active";
90 me._label.setTranslation(w / 2 + 1, h / 2 + 6);
91 }
92 else
93 me._label.setTranslation(w / 2, h / 2 + 5);
94
95 if( model._enabled )
96 {
97 if( model._focused and !backdrop )
98 file ~= "-focused";
99
100 if( model._hover and !model._down )
101 file ~= "-hover";
102 }
103 else
104 file ~= "-disabled";
105
106 me._border.set("src", file ~ ".png");
107 }
108 };
109
110 # A checkbox
111 DefaultStyle.widgets.checkbox = {
112 new: func(parent, cfg)
113 {
114 me._label_position = cfg.get("label-position", "right");
115 me._root = parent.createChild("group", "checkbox");
116 me._icon =
117 me._root.createChild("image", "checkbox-icon")
118 .setSize(18, 18);
119 me._label =
120 me._root.createChild("text")
121 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
122 .set("character-size", 14)
123 .set("alignment", "left-center");
124 },
125 setSize: func(model, w, h)
126 {
127 if (me._label_position == "left") {
128 me._label.setTranslation(3, int((h / 2) + 1));
129 me._icon.setTranslation(w - 24, int((h - 18) / 2));
130 } else {
131 me._icon.setTranslation(3, int((h - 18) / 2));
132 me._label.setTranslation(24, int(h / 2) + 1);
133 }
134
135 return me;
136 },
137 setText: func(model, text)
138 {
139 me._label.setText(text);
140
141 var min_width = me._label.maxWidth() + 3 + 24;
142 model.setLayoutMinimumSize([min_width, 18]);
143 model.setLayoutSizeHint([min_width, 24]);
144
145 return me;
146 },
147 update: func(model)
148 {
149 var backdrop = !model._windowFocus();
150 var (w, h) = model._size;
151 var file = me._style._dir_widgets ~ "/";
152
153 if( backdrop )
154 {
155 file ~= "backdrop-";
156 me._label.set("fill", me._style.getColor("backdrop_fg_color"));
157 }
158 else
159 me._label.set("fill", me._style.getColor("fg_color"));
160 file ~= "check";
161
162 if( model._down )
163 file ~= "-selected";
164 else
165 file ~= "-unselected";
166
167 if( model._enabled )
168 {
169 if( model._hover )
170 file ~= "-hover";
171 }
172 else
173 file ~= "-disabled";
174
175 me._icon.set("src", file ~ ".png");
176 }
177 };
178
179 DefaultStyle.widgets.switch = {
180 new: func(parent, cfg) {
181 me._root = parent.createChild("group", "switch");
182 me._bg = me._root.createChild("path", "switch-background");
183 me._thumb = me._root.createChild("path", "switch-thumb");
184 },
185
186 setText: func {},
187
188 setSize: func(model, w, h) {
189 me._bg.reset()
190 .moveTo(w / 4, 0.5)
191 .arcSmallCCWTo((w - 1) / 4, (h - 1) / 2, 0, w / 4, h - 0.5)
192 .horiz(w / 2)
193 .arcSmallCCWTo((w - 1) / 4, (h - 1) / 2, 0, w / 4 * 3, 0.5)
194 .close();
195 me._thumb.reset()
196 .ellipse((w - 1) / 4, (h - 1) / 2, w / 4, h / 2);
197 },
198
199 update: func(model) {
200 var bg_color = "switch_bg_color";
201 if (model._down) {
202 bg_color ~= "_checked";
203 } elsif (!model._enabled) {
204 bg_color ~= "_disabled";
205 }
206 me._bg.set("fill", me._style.getColor(bg_color));
207 me._bg.set("stroke", me._style.getColor("switch_bg_border_color"));
208 me._thumb.set("fill", me._style.getColor("switch_thumb_color"));
209 me._thumb.set("stroke", me._style.getColor("switch_thumb_border_color"));
210 me._thumb.setTranslation(model._down * 24, 0);
211 },
212 };
213
214 # A checkbox
215 DefaultStyle.widgets["radio-button"] = {
216 new: func(parent, cfg) {
217 me._root = parent.createChild("group", "radio-button");
218 me._icon = me._root.createChild("group", "radio-button-icon");
219 me._icon_background = me._icon.createChild("path", "radio-button-icon-border")
220 .circle(8.5, 9, 9);
221 me._icon_selected_indicator = me._icon.createChild("path", "radio-button-icon-selected-indicator")
222 .circle(6, 9, 9)
223 .set("stroke-width", 5);
224 me._icon_border = me._icon.createChild("path", "radio-button-icon-border")
225 .circle(8, 9, 9)
226 .set("stroke-width", 1);
227 me._label = me._root.createChild("text")
228 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
229 .set("character-size", 14)
230 .set("alignment", "left-center");
231 },
232 setSize: func(model, w, h) {
233 me._icon.setTranslation(3, int((h - 18) / 2));
234 me._label.setTranslation(24, int(h / 2) + 1);
235
236 return me;
237 },
238 setText: func(model, text) {
239 me._label.setText(text);
240
241 var min_width = text ? (me._label.maxWidth() + 3 + 24) : 0;
242 model.setLayoutMinimumSize([min_width, 24]);
243 model.setLayoutSizeHint([min_width, 24]);
244
245 return me;
246 },
247 update: func(model) {
248 var backdrop = !model._windowFocus();
249
250 me._icon_border.set("stroke", me._style.getColor("radio_button_selected_indicator_border_color"));
251 if (backdrop) {
252 me._label.set("fill", me._style.getColor("backdrop_fg_color"));
253 } else {
254 me._label.set("fill", me._style.getColor("fg_color"));
255 }
256
257 if (model._checked) {
258 me._icon_selected_indicator.show();
259 } else {
260 me._icon_selected_indicator.hide();
261 }
262
263 if (model._enabled) {
264 if (model._hover) {
265 me._icon_background.set("fill", me._style.getColor("radio_button_selected_indicator_bg_color_hovered"));
266 } else {
267 me._icon_background.set("fill", me._style.getColor("radio_button_selected_indicator_bg_color"));
268 }
269 me._icon_selected_indicator.set("stroke", me._style.getColor("radio_button_selected_indicator_color"));
270 } else {
271 me._icon_background.set("fill", me._style.getColor("radio_button_selected_indicator_bg_color_disabled"));
272 me._icon_selected_indicator.set("stroke", me._style.getColor("radio_button_selected_indicator_color_disabled"));
273 }
274 }
275 };
276
277 # A label
278 DefaultStyle.widgets.label = {
279 new: func(parent, cfg)
280 {
281 me._root = parent.createChild("group", "label");
282 me._text = me._root.createChild("text", "text")
283 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
284 .set("character-size", 14)
285 .set("alignment", "left-baseline")
286 .setVisible(0);
287 me._bg = me._root.createChild("path", "bg")
288 .setVisible(0);
289 me._img = me._root.createChild("image", "image")
290 .set("preserveAspectRatio", "xMidYMid slice")
291 .setVisible(0);
292 },
293 setSize: func(model, w, h)
294 {
295 me._bg.reset().rect(0, 0, w, h);
296 me._img.set("size[0]", w)
297 .set("size[1]", h);
298 if (model._text_align == "left") {
299 me._text.set("alignment", "left-baseline");
300 me._text.setTranslation(2, 2 + h / 2);
301 } elsif (model._text_align == "center") {
302 me._text.set("alignment", "center-baseline");
303 me._text.setTranslation(2 + w / 2, 2 + h / 2)
304 } elsif (model._text_align == "right") {
305 me._text.set("alignment", "right-baseline");
306 me._text.setTranslation(w - 2, 2 + h / 2);
307 }
308 me._text.set(
309 "max-width",
310 model._cfg.get("wordWrap", 0) ? (w - 4) : 0
311 );
312 return me;
313 },
314 setText: func(model, text)
315 {
316 if (!isstr(text) or size(text) == 0) {
317 model.setHeightForWidthFunc(nil);
318 me._text.setVisible(0);
319 return me;
320 }
321
322 me._text.setText(text);
323 me._text.setVisible(1);
324
325 var hfw_func = nil;
326 var min_width = me._text.maxWidth() + 4;
327 var width_hint = min_width;
328
329 if (model._cfg.get("wordWrap", 0)) {
330 var m = me;
331 hfw_func = func(w) m.heightForWidth(w);
332 min_width = math.min(32, min_width);
333
334 # prefer approximately quadratic text blocks
335 if( width_hint > 24 )
336 width_hint = int(math.sqrt(width_hint * 24));
337 }
338
339 model.setHeightForWidthFunc(hfw_func);
340 model.setLayoutMinimumSize([min_width, 14]);
341 model.setLayoutSizeHint([width_hint, 24]);
342
343 return me.update(model);
344 },
345 setImage: func(model, img)
346 {
347 if (img == nil or size(img) == 0) {
348 me._img.setVisible(0);
349 me._img.clear();
350 return me;
351 }
352
353 me._img.setVisible(1);
354 me._img.set("src", img);
355 return me;
356 },
357 # @description Set or clear the background color of the label
358 # @param bg scalar CSS color or 'none'
359 setBackground: func(model, bg)
360 {
361 if (bg == nil or bg == "none") {
362 me._bg.setVisible(0);
363 return me;
364 }
365
366 me._bg.setVisible(1);
367 me._bg.set("fill", bg);
368 return me;
369 },
370 setColor: func(model, color) {
371 me._text.set("fill", color);
372 },
373 heightForWidth: func(w)
374 {
375 if (!me._text.getVisible()) {
376 return -1;
377 }
378
379 return math.max(14, me._text.heightForWidth(w - 4));
380 },
381 update: func(model)
382 {
383 if (me._text.getVisible() and model._color == nil) {
384 var color_name = model._windowFocus() ? "fg_color" : "backdrop_fg_color";
385 me._text.set("fill", me._style.getColor(color_name));
386 }
387 },
388 # protected:
389 _createElement: func(name, type)
390 {
391 var mem = '_' ~ name;
392 if( me[ mem ] == nil )
393 {
394 me[ mem ] = me._root.createChild(type, "label-" ~ name);
395
396 if( type == "text" )
397 {
398 me[ mem ].set("font", "LiberationFonts/LiberationSans-Regular.ttf")
399 .set("character-size", 14)
400 .set("alignment", "left-center");
401 }
402 }
403 return me[ mem ];
404 },
405 _deleteElement: func(name)
406 {
407 name = '_' ~ name;
408 if( me[ name ] != nil )
409 {
410 me[ name ].del();
411 me[ name ] = nil;
412 }
413 return me;
414 }
415 };
416
417 # A one line text input field
418 DefaultStyle.widgets["line-edit"] = {
419 new: func(parent, cfg)
420 {
421 me._hpadding = cfg.get("hpadding", 8);
422
423 me._root = parent.createChild("group", "line-edit");
424 me._border =
425 me._root.createChild("image", "border")
426 .set("slice", "10 12"); #"7")
427 me._placeholder =
428 me._root.createChild("text", "placeholder")
429 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
430 .set("character-size", 14)
431 .set("alignment", "left-baseline")
432 .set("clip-frame", Element.PARENT);
433 me._text =
434 me._root.createChild("text", "input")
435 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
436 .set("character-size", 14)
437 .set("alignment", "left-baseline")
438 .set("clip-frame", Element.PARENT);
439 me._selection = me._root.createChild("path", "selection")
440 .set("clip-frame", Element.PARENT)
441 .set("fill", "#3333ff");
442 me._selected_text = me._root.createChild("text", "selected-text")
443 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
444 .set("character-size", 14)
445 .set("alignment", "left-baseline")
446 .set("clip-frame", Element.PARENT)
447 .set("fill", "#ffffff");
448 me._cursor =
449 me._root.createChild("path", "cursor")
450 .set("stroke", "#333")
451 .set("stroke-width", 1)
452 .moveTo(me._hpadding, 5)
453 .vert(10);
454 me._hscroll = 0;
455 me._cursor_blink = 1;
456 me._cursor_blink_timer = maketimer(0.5, func {
457 me._cursor_blink = !me._cursor_blink;
458 me._cursor.setVisible(me._cursor_visible and me._cursor_blink);
459 });
460 me._cursor_blink_timer.simulatedTime = 0;
461 me._cursor_blink_timer.start();
462 },
463 setSize: func(model, w, h)
464 {
465 me._border.setSize(w, h);
466 me._placeholder.set(
467 "clip",
468 "rect(0, " ~ (w - me._hpadding) ~ ", " ~ h ~ ", " ~ me._hpadding ~ ")"
469 );
470 me._text.set(
471 "clip",
472 "rect(0, " ~ (w - me._hpadding) ~ ", " ~ h ~ ", " ~ me._hpadding ~ ")"
473 );
474 me._selected_text.set(
475 "clip",
476 "rect(0, " ~ (w - me._hpadding) ~ ", " ~ h ~ ", " ~ me._hpadding ~ ")"
477 );
478 me._selection.set(
479 "clip",
480 "rect(0, " ~ (w - me._hpadding) ~ ", " ~ h ~ ", " ~ me._hpadding ~ ")"
481 );
482 me._cursor.setDouble("coord[2]", h - 10);
483
484 return me.update(model);
485 },
486 setText: func(model, text)
487 {
488 me._placeholder.setVisible(text or me._cursor_visible ? 0 : 1);
489 me._text.setText(text);
490 model._onStateChange();
491 },
492 setPlaceholder: func(model, placeholder)
493 {
494 me._placeholder.setText(placeholder);
495 model._onStateChange();
496 },
497 update: func(model)
498 {
499 var backdrop = !model._windowFocus();
500 var file = me._style._dir_widgets ~ "/";
501
502 if( backdrop )
503 file ~= "backdrop-";
504
505 file ~= "entry";
506
507 if( !model._enabled )
508 file ~= "-disabled";
509 else if( model._focused and !backdrop )
510 file ~= "-focused";
511
512 me._border.set("src", file ~ ".png");
513
514 var color_name = backdrop ? "backdrop_fg_color" : "fg_color";
515 me._placeholder.set("fill", me._style.getColor((backdrop ? "backdrop_" : "") ~ "placeholder_color"));
516 me._text.set("fill", me._style.getColor(color_name));
517 me._selected_text.set("fill", me._style.getColor("text_color_selected"));
518 me._selection.set("fill", me._style.getColor((backdrop ? "backdrop_" : "") ~ "text_color_bg_selected"));
519
520 me._cursor_visible = model._enabled and model._focused and !backdrop and model._selection_start == model._selection_end;
521 me._cursor.setVisible(me._cursor_visible and me._cursor_blink);
522 me._selection.reset()
523 .moveTo(me._text.getCursorPos(0, model._selection_start)[0], 0)
524 .vert(16)
525 .horizTo(me._text.getCursorPos(0, model._selection_end)[0])
526 .vert(-16);
527 me._selected_text.setText(model.selectedText());
528
529 me._placeholder.setVisible(model._text or me._cursor_visible ? 0 : 1);
530
531 var width = model._size[0] - 2 * me._hpadding;
532 var cursor_pos = me._text.getCursorPos(0, model._cursor)[0];
533 var text_width = me._text.getCursorPos(0, me._text.lineLength(0))[0];
534
535 if( text_width <= width )
536 # fit -> align left (TODO handle different alignment)
537 me._hscroll = 0;
538 else if( me._hscroll + cursor_pos > width )
539 # does not fit, cursor to the right
540 me._hscroll = width - cursor_pos;
541 else if( me._hscroll + cursor_pos < 0 )
542 # does not fit, cursor to the left
543 me._hscroll = -cursor_pos;
544 else if( me._hscroll + text_width < width )
545 # does not fit, limit scroll to align with right side
546 me._hscroll = width - text_width;
547
548 var text_pos = me._hscroll + me._hpadding;
549
550 me._placeholder
551 .setTranslation(text_pos, model._size[1] / 2 + 5)
552 .update();
553 me._text
554 .setTranslation(text_pos, model._size[1] / 2 + 5)
555 .update();
556 me._cursor
557 .setDouble("coord[0]", text_pos + cursor_pos)
558 .update();
559 me._selection.setTranslation(text_pos, model._size[1] / 2 - 8)
560 .update();
561 me._selected_text.setTranslation(text_pos + me._text.getCursorPos(0, model._selection_start)[0], model._size[1] / 2 + 5)
562 .update();
563 }
564 };
565
566 # ScrollArea
567 DefaultStyle.widgets["scroll-area"] = {
568 new: func(parent, cfg)
569 {
570 me._root = parent.createChild("group", "scroll-area");
571
572 me._bg = me._root.createChild("path", "background")
573 .set("fill", "#e0e0e0");
574 me.content = me._root.createChild("group", "scroll-content")
575 .set("clip-frame", Element.PARENT);
576 me.vert = me._newScroll(me._root, "vert");
577 me.horiz = me._newScroll(me._root, "horiz");
578 },
579 setColorBackground: func
580 {
581 if( size(arg) == 1 )
582 var arg = arg[0];
583 me._bg.setColorFill(arg);
584 },
585 update: func(model)
586 {
587 me.horiz.reset();
588 if( model._max_scroll[0] > 1 )
589 # only show scroll bar if horizontally scrollable
590 me.horiz.moveTo( model._scroller_offset[0] + model._scroller_pos[0],
591 model._size[1] - 2 )
592 .horiz(model._scroller_size[0]);
593
594 me.vert.reset();
595 if( model._max_scroll[1] > 1 )
596 # only show scroll bar if vertically scrollable
597 me.vert.moveTo( model._size[0] - 2,
598 model._scroller_offset[1] + model._scroller_pos[1] )
599 .vert(model._scroller_size[1]);
600
601 me._bg.reset()
602 .rect(0, 0, model._size[0], model._size[1]);
603 me.content.set(
604 "clip",
605 "rect(0, " ~ model._size[0] ~ ", " ~ model._size[1] ~ ", 0)"
606 );
607 },
608 # private:
609 _newScroll: func(el, orient)
610 {
611 return el.createChild("path", "scroll-" ~ orient)
612 .set("stroke", "#f07845")
613 .set("stroke-width", 4);
614 },
615 # Calculate size and limits of scroller
616 #
617 # @param model
618 # @param dir 0 for horizontal, 1 for vertical
619 # @return [scroller_size, min_pos, max_pos]
620 _updateScrollMetrics: func(model, dir)
621 {
622 if( model._content_size[dir] <= model._size[dir] )
623 return;
624
625 model._scroller_size[dir] =
626 math.max(
627 12,
628 model._size[dir] * (model._size[dir] / model._content_size[dir])
629 );
630 model._scroller_offset[dir] = 0;
631 model._scroller_delta[dir] = model._size[dir] - model._scroller_size[dir];
632 }
633 };
634
635 DefaultStyle.widgets["tab-widget"] = {
636 tabBarHeight: 36,
637 new: func(parent, cfg) {
638 me._root = parent.createChild("group", "tab-widget");
639 me.bg = me._root.createChild("path", "background")
640 .set("fill", "#e0e0e0");
641 me.tabBar = me._root.createChild("group", "tab-widget-tabbar");
642 me.content = me._root.createChild("group", "tab-widget-content");
643 },
644
645 update: func(model) {
646 me.bg.set("fill", me._style.getColor("bg_color"));
647 me.tabBar.update();
648 me.content.update();
649 },
650
651 setSize: func(model, w, h) {
652 me.bg.reset().rect(0, w, h, 0);
653 me.content.setTranslation(0, me.tabBarHeight);
654 },
655 };
656
657 # Tab button for the tab widget
658 DefaultStyle.widgets["tab-widget-tab-button"] = {
659 new: func(parent, cfg) {
660 me._root = parent.createChild("group", "tab-widget-tab-button");
661 me._bg = me._root.createChild("path")
662 .set("fill", me._style.getColor("tab_widget_tab_button_bg_focused"))
663 .set("stroke", me._style.getColor("tab_widget_tab_button_border"))
664 .set("stroke-width", 1);
665 me._selected_indicator = me._root.createChild("path")
666 .set("stroke-width", 4);
667 me._label = me._root.createChild("text")
668 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
669 .set("character-size", 14)
670 .set("alignment", "center-baseline");
671 },
672
673 setSize: func(model, w, h) {
674 me._bg.reset().rect(3, 0, w - 6, h);
675 me._selected_indicator.reset().moveTo(3, h - 2).horiz(w - 6);
676 me._label.setTranslation((w - 24 - 8) / 2, h / 2 + 5);
677 model._close_button.move(w - 24 - 8, h / 2 - 12);
678 },
679
680 setText: func(model, text) {
681 me._label.setText(text);
682
683 var min_width = math.max(80, me._label.maxWidth() + 12 + (model._tab_closeable ? 24 + 12 : 0));
684 model.setLayoutMinimumSize([min_width, 36]);
685 model.setLayoutSizeHint([min_width, 36]);
686
687 return me;
688 },
689
690 update: func(model) {
691 var backdrop = !model._windowFocus();
692 var (w, h) = model._size;
693
694 var bg_color_name = "tab_widget_tab_button_bg_focused";
695 if (backdrop) {
696 bg_color_name = "tab_widget_tab_button_bg_unfocused";
697 } else if (model._selected) {
698 bg_color_name = "tab_widget_tab_button_bg_selected";
699 } else if (model._hover) {
700 bg_color_name = "tab_widget_tab_button_bg_hovered";
701 }
702 me._bg.set("fill", me._style.getColor(bg_color_name));
703
704 me._selected_indicator.setVisible(model._selected or model._hover);
705 var selected_indicator_color_name = "tab_widget_tab_button_selected_indicator_selected";
706 if (!model._selected and model._hover) {
707 selected_indicator_color_name = "tab_widget_tab_button_selected_indicator_unselected_hovered";
708 }
709 me._selected_indicator.set("stroke", me._style.getColor(selected_indicator_color_name));
710
711 me._label.set("fill", me._style.getColor((backdrop ? "backdrop_" : "") ~ "fg_color"));
712 }
713 };
714
715 DefaultStyle.widgets["tab-button-close-button"] = {
716 new: func(parent, cfg) {
717 me._root = parent.createChild("group", "button");
718 me._bg = me._root.createChild("path");
719 me._border = me._root.createChild("image", "button")
720 .set("slice", "10 12"); #"7")
721 me._cross = me._root.createChild("text")
722 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
723 .set("character-size", 21)
724 .set("alignment", "center-baseline")
725 .setText("×");
726 },
727 setText: func {},
728 setSize: func(model, w, h) {
729 me._bg.reset().rect(3, 3, w - 6, h - 6, {"border-radius": 5});
730 me._border.setSize(w, h);
731 me._cross.setTranslation(w / 2, h / 2 + 7);
732 },
733 update: func(model) {
734 var backdrop = !model._windowFocus();
735 var file = me._style._dir_widgets ~ "/";
736
737 # TODO unify color names with image names
738 var bg_color_name = "button_bg_color";
739 if (backdrop) {
740 bg_color_name = "button_backdrop_bg_color";
741 } elsif (model._down) {
742 bg_color_name = "button_bg_color_down";
743 } elsif (model._hover) {
744 bg_color_name = "button_bg_color_hover";
745 }
746 me._bg.set("fill", me._style.getColor(bg_color_name));
747
748 if (model._hover or model._down) {
749 me._cross.set("fill", me._style.getColor("fg_color"));
750 me._border.show();
751 me._bg.show();
752 } else {
753 me._cross.set("fill", me._style.getColor("backdrop_fg_color"));
754 me._border.hide();
755 me._bg.hide();
756 }
757 if (backdrop) {
758 file ~= "backdrop-";
759 }
760 file ~= "button";
761
762 if (model._down) {
763 file ~= "-active";
764 }
765
766 if (model._focused and !backdrop) {
767 file ~= "-focused";
768 }
769 if (model._hover and !model._down) {
770 file ~= "-hover";
771 }
772 me._border.set("src", file ~ ".png");
773 }
774 };
775
776 # A horizontal or vertical rule line
777 # possibly with a text label embedded
778 DefaultStyle.widgets.rule = {
779 new: func(parent, cfg)
780 {
781 me._root = parent.createChild("group", "rule");
782 var bgColor = me._style.getColor("fg_color");
783 var shadowColor = me._style.getColor("fg_color_shadow");
784
785 me._bg = me._root.createChild("path")
786 .set("fill", bgColor)
787 .set("stroke", bgColor)
788 .set("stroke-width", 1);
789 me._shadow = me._root.createChild("path")
790 .set("fill", shadowColor)
791 .set("stroke", shadowColor)
792 .set("stroke-width", 1);
793
794 me._isVertical = cfg.get("isVertical");
795 },
796 setSize: func(model, w, h)
797 {
798 var firstWidth = w;
799 var firstHeight = h;
800 var hh = h * 0.5;
801
802 if( me['_text'] != nil )
803 {
804 firstHeight = firstWidth = 20; # TODO make this settable in Style.xml
805
806 # TODO handle eliding for translations?
807 me._text.setTranslation(firstWidth + 3, hh);
808 var maxW = model._cfg.get("maxTextWidth", -1);
809 if (maxW > 0) {
810 me._text.set("max-width", maxW);
811 }
812
813 # assume horizontal for now, with a label
814 var bg2Left = maxW > 0 ? maxW : me._text.maxWidth() + 22;
815 var bg2Width = w - bg2Left;
816 me._bg2.reset().rect(bg2Left, hh - 1, bg2Width, 2);
817 me._shadow2.reset().moveTo(bg2Left + 1, hh).horiz(bg2Width - 1);
818 }
819
820 if (me._isVertical ) {
821 me._bg.reset().rect(0, 0, 2, firstHeight);
822 me._shadow.reset().moveTo(1, 1).vert(firstHeight - 1);
823 } else {
824 me._bg.reset().rect(0, hh - 1, firstWidth, 2);
825 me._shadow.reset().moveTo(1, hh).horiz(firstWidth - 1);
826 }
827
828 return me;
829 },
830 setText: func(model, text)
831 {
832 if( text == nil or size(text) == 0 )
833 {
834 # force a resize?
835 me._deleteElement('bg2');
836 me._deleteElement('shadow2');
837 return me._deleteElement('text');
838 }
839
840 if (me._isVertical) {
841 logprint(LOG_DEVALERT, "Text label not supported for vertical rules, yet");
842 return;
843 }
844
845 me._createElement("text", "text")
846 .setText(text);
847
848 var width_hint = me._text.maxWidth() + 40;
849
850
851 var bgColor = me._style.getColor("fg_color");
852 var shadowColor = me._style.getColor("fg_color_shadow");
853
854 me._bg2 = me._root.createChild("path")
855 .set("fill", bgColor)
856 .set("stroke", bgColor)
857 .set("stroke-width", 1);
858 me._shadow2 = me._root.createChild("path")
859 .set("fill", shadowColor)
860 .set("stroke", shadowColor)
861 .set("stroke-width", 1);
862
863 model.setLayoutMinimumSize([40, 14]);
864 # TODO mark as expanding?
865 model.setLayoutSizeHint([width_hint, 24]);
866
867 return me.update(model);
868 },
869 update: func(model)
870 {
871
872 # different color if disabled?
873 if( me['_text'] != nil )
874 {
875 var color_name = model._windowFocus() ? "fg_color" : "backdrop_fg_color";
876 me._text.set("fill", me._style.getColor(color_name));
877 }
878 },
879 # protected:
880 _createElement: func(name, type)
881 {
882 var mem = '_' ~ name;
883 if( me[ mem ] == nil )
884 {
885 me[ mem ] = me._root.createChild(type, "rule-" ~ name);
886
887 if( type == "text" )
888 {
889 me[ mem ].set("font", "LiberationFonts/LiberationSans-Regular.ttf")
890 .set("character-size", 14)
891 .set("alignment", "left-center");
892 }
893 }
894 return me[ mem ];
895 },
896 _deleteElement: func(name)
897 {
898 name = '_' ~ name;
899 if( me[ name ] != nil )
900 {
901 me[ name ].del();
902 me[ name ] = nil;
903 }
904 return me;
905 }
906 };
907
908 # a frame (sometimes called a group box), with optional label
909 # and enable/disable checkbox
910 DefaultStyle.widgets.frame = {
911 new: func(parent, cfg)
912 {
913 me._root = parent.createChild("group", "frame-box");
914 me._createElement("bg", "image")
915 .set("slice", "8 8");
916 me.content = me._root.createChild("group", "frame-content");
917
918 # handle label + checkable flag
919 },
920
921 update: func(model)
922 {
923 var file = me._style._dir_widgets ~ "/";
924 file ~= "frame";
925
926 # if( !model._enabled )
927 # file ~= "-disabled";
928
929 me._bg.set("src", file ~ ".png");
930
931 me._bg.setSize(model._size[0], model._size[1]);
932 },
933 # protected:
934 _createElement: func(name, type)
935 {
936 var mem = '_' ~ name;
937 if( me[ mem ] == nil )
938 {
939 me[ mem ] = me._root.createChild(type, "frame-" ~ name);
940
941 if( type == "text" )
942 {
943 me[ mem ].set("font", "LiberationFonts/LiberationSans-Regular.ttf")
944 .set("character-size", 14)
945 .set("alignment", "left-center");
946 }
947 }
948 return me[ mem ];
949 },
950 _deleteElement: func(name)
951 {
952 name = '_' ~ name;
953 if( me[ name ] != nil )
954 {
955 me[ name ].del();
956 me[ name ] = nil;
957 }
958 return me;
959 }
960
961 };
962
963 # a horizontal or vertical slider, for selecting /
964 # dragging over a numerical range
965 DefaultStyle.widgets.slider = {
966 new: func(parent, cfg)
967 {
968 me._root = parent.createChild("group", "slider");
969 me._createElement("bg", "image")
970 .set("slice", "2 6");
971
972 me._createElement("fill", "image")
973 .set("slice", "2 6");
974
975 me._ticks = me._root.createChild("path")
976 .set("stroke-width", me._style.getSize("slider-ticks-width", 1));
977
978 me._fillHeight = me._fill.imageSize()[1];
979 me._createElement("thumb", "image");
980 me._thumbSize = me._thumb.imageSize();
981
982 me._value = me._root.createChild("text")
983 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
984 .set("character-size", me._style.getSize("slider-value-font-size", me._style.getSize("base-font-size")))
985 .set("alignment", "center-top");
986 },
987
988 _updateLayoutSizes: func(model) {
989 me.update(model);
990 var h = me._thumb.imageSize()[1] + 6;
991 if (model._valueDisplayPosition != model.ValuePosition.None) {
992 h += me._style.getSize("slider-value-font-size", me._style.getSize("base-font-size")) +
993 me._style.getSize("slider-thumb-value-margin", 8);
994 }
995 if (model._ticksPosition != model.TicksPosition.None and model._valueDisplayPosition != model._ticksPosition) {
996 h += me._style.getSize("slider-fill-ticks-margin", 3) + me._style.getSize("slider-tick-length", 10);
997 }
998 model.setLayoutMinimumSize([50, h]);
999 model.setLayoutSizeHint([(model._maxValue - model._minValue) / (model._stepSize or 1), h]);
1000 model.setLayoutMaximumSize([model._MAX_SIZE, h]);
1001 },
1002
1003 setNormValue: func(model, normValue)
1004 {
1005 var w = model._size[0];
1006 var halfThumbWidth = me._thumbSize[0] * 0.5;
1007 var availWidthPos = w - me._thumbSize[0];
1008 var thumbX = math.round(availWidthPos * normValue);
1009
1010 var valueX = 0;
1011 if (model._valueDisplayStyle == model.ValueStyle.Moving) {
1012 var startPos = me._value.maxWidth() / 2;
1013 var thumbPos = thumbX + me._thumbSize[0] * 0.5;
1014 var endPos = w - (me._value.maxWidth() / 2);
1015 valueX = math.clamp(thumbPos, startPos, endPos);
1016 } elsif (model._valueDisplayStyle == model.ValueStyle.Fixed) {
1017 valueX = w / 2;
1018 }
1019 me._value.setTranslation(valueX, me._value.getTranslation()[1]);
1020 me._thumb.setTranslation(thumbX, me._thumb.getTranslation()[1]);
1021 me._fill.setSize(thumbX, me._fillHeight);
1022 me._fill.setTranslation(halfThumbWidth, me._fill.getTranslation()[1]);
1023 me._value.setText(model._value);
1024 },
1025
1026 update: func(model)
1027 {
1028 var direction = "horizontal";
1029 # set background state
1030 var file = me._style._dir_widgets ~ "/";
1031 file ~= "scale-" ~ direction ~ "-trough";
1032 if( !model._enabled )
1033 file ~= "-disabled";
1034
1035 me._bg.set("src", file ~ ".png");
1036
1037 # fill state
1038 var file = me._style._dir_widgets ~ "/";
1039 file ~= "scale-" ~ direction ~ "-fill";
1040 if( !model._enabled ) {
1041 file ~= "-disabled";
1042 } else {
1043
1044 }
1045
1046 me._fill.set("src", file ~ ".png");
1047 me._fillHeight = me._fill.imageSize()[1];
1048
1049 # set thumb state
1050 file = me._style._dir_widgets ~ "/";
1051 file ~= "slider-" ~ direction;
1052 if( !model._enabled ) {
1053 file ~= "-disabled";
1054 } else {
1055 if (model._thumbDown)
1056 file ~= "-focused";
1057 if (model._hover)
1058 file ~= "-hover";
1059 }
1060
1061 me._thumb.set("src", file ~ ".png");
1062 me._thumbSize = me._thumb.imageSize();
1063
1064 var color_name = model._windowFocus() ? "fg_color" : "backdrop_fg_color";
1065 me._value.set("fill", me._style.getColor(color_name));
1066 if (model._valueDisplayPosition != model.ValuePosition.None) {
1067 me._value.show();
1068 } else {
1069 me._value.hide();
1070 }
1071
1072 me._ticks.set("stroke", me._style.getColor("slider_ticks"));
1073 if (model._ticksPosition != model.TicksPosition.None) {
1074 me._ticks.show();
1075 } else {
1076 me._ticks.hide();
1077 }
1078
1079 # update the position as well, since other stuff
1080 # may have changed
1081 me.setNormValue(model, model._normValue());
1082 },
1083
1084 setSize: func(model, w, h)
1085 {
1086 var valueFontSize = me._style.getSize("slider-value-font-size", me._style.getSize("base-font-size"));
1087 var thumbValueMargin = me._style.getSize("slider-thumb-value-margin", 8);
1088 var fillTicksMargin = me._style.getSize("slider-fill-ticks-margin", 3);
1089 var ticksOffset = fillTicksMargin + me._style.getSize("slider-tick-length", 10);
1090
1091 var thumbY = (h - me._thumbSize[1]) * 0.5;
1092 if (model._valueDisplayPosition != model.ValuePosition.None) {
1093 thumbY -= (valueFontSize + thumbValueMargin) * 0.5;
1094 }
1095 if (model._ticksPosition != model.TicksPosition.None and model._ticksPosition != model._valueDisplayPosition) {
1096 thumbY -= ticksOffset * 0.5;
1097 }
1098
1099 var valueY = thumbY;
1100 if (model._valueDisplayPosition == model.ValuePosition.Above) {
1101 thumbY += valueFontSize + thumbValueMargin;
1102 } elsif (model._valueDisplayPosition == model.ValuePosition.Below) {
1103 valueY += me._thumbSize[1] + thumbValueMargin;
1104 }
1105
1106 var fillY = thumbY + (me._thumbSize[1] - me._fillHeight) * 0.5;
1107
1108 var ticksY = fillY;
1109 if (model._ticksPosition == model.TicksPosition.Below) {
1110 ticksY += me._fillHeight + fillTicksMargin;
1111 } elsif (model._ticksPosition == model.TicksPosition.Above) {
1112 if (model._valueDisplayPosition == model.ValuePosition.Below) {
1113 thumbY += ticksOffset;
1114 fillY += ticksOffset;
1115 valueY += ticksOffset;
1116 } else {
1117 ticksY -= ticksOffset;
1118 }
1119 }
1120
1121 me._bg.setTranslation(me._thumbSize[0] / 2, fillY);
1122 me._fill.setTranslation(me._fill.getTranslation()[0], fillY);
1123 me._ticks.setTranslation(me._thumbSize[0] / 2, ticksY);
1124 me._thumb.setTranslation(me._thumb.getTranslation()[0], thumbY);
1125 me._value.setTranslation(me._value.getTranslation()[0], valueY);
1126 me._bg.setSize(w - me._thumbSize[0], me._fillHeight);
1127 me.setNormValue(model, model._normValue());
1128 me._drawTicks(model);
1129 },
1130
1131 _drawTicks: func(model) {
1132 me._ticks.reset();
1133 var range = model._maxValue - model._minValue;
1134 if (range <= 0 or model._tickStep <= 0) {
1135 return;
1136 }
1137 var availWidthPos = model._size[0] - me._thumbSize[0];
1138 var pixelsPerUnit = availWidthPos / range;
1139 var remainder = math.mod(range, model._tickStep);
1140 var numTicks = int((range - remainder) / model._tickStep);
1141 if (remainder == 0) {
1142 numTicks -= 1;
1143 }
1144 for (var i = 1; i <= numTicks; i += 1) {
1145 me._ticks.moveTo(i * pixelsPerUnit * model._tickStep, 0)
1146 .vert(me._style.getSize("slider-tick-length", 8));
1147 }
1148 },
1149
1150 # protected:
1151 _createElement: func(name, type)
1152 {
1153 var mem = '_' ~ name;
1154 if( me[ mem ] == nil )
1155 {
1156 me[ mem ] = me._root.createChild(type, "slider-" ~ name);
1157
1158 if( type == "text" )
1159 {
1160 me[ mem ].set("font", "LiberationFonts/LiberationSans-Regular.ttf")
1161 .set("character-size", me._style.getSize("slider-value-font-size", me._style.getSize("base-font-size")))
1162 .set("alignment", "left-center");
1163 }
1164 }
1165 return me[ mem ];
1166 },
1167 _deleteElement: func(name)
1168 {
1169 name = '_' ~ name;
1170 if( me[ name ] != nil )
1171 {
1172 me[ name ].del();
1173 me[ name ] = nil;
1174 }
1175 return me;
1176 }
1177 };
1178
1179 DefaultStyle.widgets.dial = {
1180 new: func(parent, cfg) {
1181 me._root = parent.createChild("group", "dial");
1182 me._knob = me._root.createChild("path", "dial-knob");
1183 me._handle = me._root.createChild("image", "dial-handle");
1184 me._handleTranslateTransform = me._handle.createTransform();
1185 me._value = me._root.createChild("text", "dial-value")
1186 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
1187 .set("character-size", me._style.getSize("dial-value-font-size", me._style.getSize("base-font-size", 14)))
1188 .set("alignment", "center-center")
1189 .setText(0);
1190 me._ticks = me._root.createChild("path", "dial-ticks");
1191
1192 me._maxValueWidth = 20;
1193 },
1194
1195 _updateLayoutSizes: func(model) {
1196 var handleSize = me._handle.imageSize()[1];
1197 var borderHandleMargin = math.clamp(
1198 math.min(model._size[0], model._size[1]) * 0.0625,
1199 2,
1200 me._style.getSize("dial-border-handle-margin", 8),
1201 );
1202 var valueSize = [0, 0];
1203 var valueHandleMargin = me._style.getSize("dial-value-handle-margin", 5);
1204 if (model._showValue) {
1205 valueSize = [
1206 me._maxValueWidth + valueHandleMargin * 2,
1207 me._style.getSize("dial-value-font-size", me._style.getSize("base-font-size", 14)) + valueHandleMargin * 2
1208 ];
1209 } else {
1210 valueSize = me._style.getSize("dial-center-handle-margin", 2) * 2;
1211 valueSize = [valueSize, valueSize];
1212 }
1213 var minW = borderHandleMargin * 2 + valueSize[0] + handleSize * 2;
1214 var minH = borderHandleMargin * 2 + valueSize[1] + handleSize * 2;
1215 var length = math.max(minW, minH);
1216 if (model._showTicks) {
1217 length += me._style.getSize("dial-knob-ticks-margin", 2) + me._style.getSize("dial-ticks-length", 10);
1218 }
1219 model.setLayoutMinimumSize([length, length]);
1220 },
1221
1222 setSize: func(model, length) {
1223 var knobTicksMargin = me._style.getSize("dial-knob-ticks-margin", 2);
1224 var ticksLength = me._style.getSize("dial-ticks-length", 10);
1225 var knobRadius = (length - me._style.getSize("dial-knob-border-width", 1)) / 2;
1226 var halfLength = length / 2;
1227 if (model._showTicks) {
1228 knobRadius -= knobTicksMargin + ticksLength;
1229 }
1230 me._knob.reset()
1231 .circle(knobRadius, halfLength, halfLength);
1232 me._value.setTranslation(
1233 halfLength,
1234 halfLength
1235 );
1236 var handleOffset = [
1237 halfLength - me._handle.imageSize()[0] / 2,
1238 math.clamp(knobRadius * 0.125, 2, me._style.getSize("dial-border-handle-margin", 8))
1239 ];
1240 if (model._showTicks) {
1241 handleOffset[1] += knobTicksMargin + ticksLength;
1242 }
1243 me._handleTranslateTransform.setTranslation(handleOffset[0], handleOffset[1]);
1244 me._handle.setCenter(
1245 me._handle.imageSize()[0] / 2,
1246 halfLength - handleOffset[1]
1247 );
1248 var degreesRange = 360;
1249 var nowrapMargin = me._style.getSize("dial-nowrap-margin-deg", 30);
1250 var offset = 0;
1251 if (!model._wraps) {
1252 degreesRange -= nowrapMargin;
1253 offset = nowrapMargin / 2;
1254 }
1255 me._handle.setRotation((model._normValue() * degreesRange + offset) * D2R);
1256 me._drawTicks(model);
1257 },
1258
1259 _drawTicks: func(model) {
1260 var length = math.min(model._size[0], model._size[1]);
1261 var halfLength = length / 2;
1262 var knobTicksMargin = me._style.getSize("dial-knob-ticks-margin", 2);
1263 var ticksLength = me._style.getSize("dial-ticks-length", 10);
1264 var knobRadius = (length - me._style.getSize("dial-knob-border-width", 1)) / 2 - knobTicksMargin - ticksLength;
1265
1266 var degreesRange = 360;
1267 var nowrapMargin = me._style.getSize("dial-nowrap-margin-deg", 30);
1268 var offset = 0;
1269 if (!model._wraps) {
1270 degreesRange -= nowrapMargin;
1271 offset = nowrapMargin / 2;
1272 }
1273 var range = model._maxValue - model._minValue;
1274 var center = [halfLength, halfLength];
1275 var degreesPerTickStep = (degreesRange / range) * model._tickStep;
1276 var lengthBegin = knobRadius + knobTicksMargin;
1277 var lengthEnd = lengthBegin + ticksLength;
1278 me._ticks.reset();
1279
1280 for (var i = 0; i < degreesRange / degreesPerTickStep; i += 1) {
1281 var radians = (i * degreesPerTickStep + offset) * D2R;
1282 var sin = math.sin(radians);
1283 var cos = -math.cos(radians);
1284 var start = [center[0] + sin * lengthBegin, center[1] + cos * lengthBegin];
1285 var tick = [center[0] + sin * lengthEnd, center[1] + cos * lengthEnd];
1286 me._ticks.moveTo(start[0], start[1]).lineTo(tick[0], tick[1]);
1287 }
1288 },
1289
1290 _updateMaxValueWidth: func(model) {
1291 if (model._valueFormat != nil) {
1292 me._value.setText(sprintf(model._valueFormat, model._minValue));
1293 var min = me._value.maxWidth();
1294 me._value.setText(sprintf(model._valueFormat, model._maxValue));
1295 var max = me._value.maxWidth();
1296 me._value.setText(sprintf(model._valueFormat, model._minValue + model._stepSize));
1297 var minStep = me._value.maxWidth();
1298 me._value.setText(sprintf(model._valueFormat, model._value));
1299 } else {
1300 me._value.setText(model._minValue);
1301 var min = me._value.maxWidth();
1302 me._value.setText(model._maxValue);
1303 var max = me._value.maxWidth();
1304 me._value.setText(model._minValue + model._stepSize);
1305 var minStep = me._value.maxWidth();
1306 me._value.setText(model._value);
1307 }
1308 me._maxValueWidth = math.max(min, max, minStep);
1309 },
1310
1311 setValue: func(model, value) {
1312 var degreesRange = 360;
1313 var nowrapMargin = me._style.getSize("dial-nowrap-margin-deg", 30);
1314 var offset = 0;
1315 if (!model._wraps) {
1316 degreesRange -= nowrapMargin;
1317 offset = nowrapMargin / 2;
1318 }
1319 me._handle.setRotation((model._normValue() * degreesRange + offset) * D2R);
1320 if (model._valueFormat != nil) {
1321 me._value.setText(sprintf(model._valueFormat, value));
1322 } else {
1323 me._value.setText(value);
1324 }
1325 },
1326
1327 update: func(model) {
1328 var color_name = model._windowFocus() ? "fg_color" : "backdrop_fg_color";
1329 me._value.set("fill", me._style.getColor(color_name));
1330
1331 color_name = "dial_knob_bg";
1332 if (!model._enabled) {
1333 color_name ~= "_disabled";
1334 } elsif (model._focused and model._windowFocus()) {
1335 color_name ~= "_focused";
1336 } elsif (!model._windowFocus()) {
1337 color_name ~= "_backdrop";
1338 }
1339 if (model._hover and model._enabled and model._windowFocus()) {
1340 color_name ~= "_hovered";
1341 }
1342 me._knob.set("fill", me._style.getColor(color_name));
1343
1344 color_name = "dial_knob_border";
1345 if (!model._enabled) {
1346 color_name ~= "_disabled";
1347 } elsif (model._focused and model._windowFocus()) {
1348 color_name ~= "_focused";
1349 }
1350 if (model._hover and model._enabled and model._windowFocus()) {
1351 color_name ~= "_hovered";
1352 }
1353 me._knob.set("stroke", me._style.getColor(color_name));
1354
1355 var file = me._style._dir_widgets ~ "/dial-handle";
1356 if (!model._enabled) {
1357 file ~= "-disabled";
1358 } elsif (model._handleDown) {
1359 file ~= "-down";
1360 }
1361 me._handle.set("src", file ~ ".png");
1362
1363 me._ticks.set("stroke", me._style.getColor("dial_ticks"));
1364
1365 if (model._showValue) {
1366 me._value.show();
1367 } else {
1368 me._value.hide();
1369 }
1370 if (model._showTicks) {
1371 me._ticks.show();
1372 } else {
1373 me._ticks.hide();
1374 }
1375 }
1376 };
1377
1378 DefaultStyle.widgets["menu-item"] = {
1379 new: func(parent, cfg) {
1380 me._root = parent.createChild("group", "menu-item");
1381 me._bg = me._root.createChild("path");
1382
1383 me._icon = me._root.createChild("image")
1384 .set("slice", "18 18");
1385
1386 me._label = me._root.createChild("text")
1387 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
1388 .set("character-size", 14)
1389 .set("alignment", "left-baseline");
1390
1391 me._shortcut = me._root.createChild("text")
1392 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
1393 .set("character-size", 14)
1394 .set("alignment", "right-baseline");
1395
1396 me._submenu_indicator = me._root.createChild("path")
1397 .vert(12).line(6, -7).close();
1398 },
1399
1400 setSize: func(model, w, h) {
1401 me._bg.reset().rect(0, 0, w, h);
1402 var offset = 0;
1403 if (!model._is_menubar_item) {
1404 offset += 5 + 18;
1405 }
1406 me._icon.setTranslation(5, int((h - 12) / 2));
1407 me._label.setTranslation(offset + 5, int(h / 2) + 4);
1408 me._shortcut.setTranslation(w - 5, int(h / 2) + 4);
1409 me._submenu_indicator.setTranslation(w - 12, int((h - 12) / 2));
1410 return me;
1411 },
1412
1413 _updateLayoutSizes: func(model) {
1414 var min_width = 5 + me._label.maxWidth() + 5;
1415 if (!model._is_menubar_item) {
1416 # add icon space
1417 min_width += 5 + 18;
1418 # add shortcut space
1419 min_width += me._shortcut.maxWidth() + 10;
1420 if (model._menu != nil) {
1421 # add submenu indicator space
1422 min_width += 12;
1423 }
1424 }
1425 model.setLayoutMinimumSize([min_width, 24]);
1426 model.setLayoutSizeHint([min_width, 24]);
1427
1428 return me;
1429 },
1430
1431 setText: func(model, text) {
1432 me._label.setText(text);
1433 return me._updateLayoutSizes(model);
1434 },
1435
1436 setShortcut: func(model, shortcut) {
1437 if (shortcut != nil) {
1438 me._shortcut.setText(shortcut);
1439 }
1440 return me._updateLayoutSizes(model);
1441 },
1442
1443 setIcon: func(model, icon) {
1444 if (!icon) {
1445 me._icon.hide();
1446 } else {
1447 me._icon.show();
1448 var file = me._style._dir_widgets ~ "/" ~ icon;
1449 me._icon.set("src", file);
1450 }
1451 return me;
1452 },
1453
1454 update: func(model) {
1455 me._bg.set("fill", me._style.getColor("menu_item_bg" ~ (model._hovered ? "_hovered" : "")));
1456 var text_color_name = "menu_item_fg";
1457 if (model._hovered) {
1458 text_color_name ~= "_hovered";
1459 } else if (!model._enabled) {
1460 text_color_name ~= "_disabled";
1461 }
1462 me._label.set("fill", me._style.getColor(text_color_name));
1463 me._shortcut.set("fill", me._style.getColor(text_color_name));
1464 me._submenu_indicator.set("fill", me._style.getColor("menu_item_submenu_indicator" ~ (model._hovered ? "_hovered" : "")));
1465 if (model._menu != nil) {
1466 if (!model._is_menubar_item) {
1467 me._submenu_indicator.show();
1468 }
1469 me._shortcut.hide();
1470 } else {
1471 me._submenu_indicator.hide();
1472 me._shortcut.show();
1473 }
1474
1475 return me;
1476 }
1477 };
1478
1479 DefaultStyle.widgets["menu-bar"] = {
1480 new: func(parent, cfg) {
1481 me._root = parent.createChild("group", "menu-bar");
1482 me._bg = me._root.createChild("path");
1483 me._items = me._root.createChild("group", "menu-bar-items");
1484 },
1485
1486 setSize: func(model, w, h) {
1487 me._bg.reset().rect(0, 0, w, 24);
1488 me._items.setTranslation(0, 0);
1489 return me;
1490 },
1491
1492 update: func(model) {
1493 me._bg.set("fill", me._style.getColor("bg_color"));
1494
1495 return me;
1496 }
1497 };
1498
1499 # A combo-box
1500 DefaultStyle.widgets["combo-box"] = {
1501 new: func(parent, cfg)
1502 {
1503 me._root = parent.createChild("group", "combo-box");
1504 me._bg =
1505 me._root.createChild("path");
1506 me._border =
1507 me._root.createChild("image", "border")
1508 .set("slice", "10 6"); #"7")
1509 me._buttonBorder =
1510 me._root.createChild("image", "border-button")
1511 .set("slice", "10 6"); #"7")
1512 me._arrowIcon = me._root.createChild("image", "arrow");
1513 me._label =
1514 me._root.createChild("text")
1515 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
1516 .set("character-size", 14)
1517 .set("alignment", "left-center");
1518 },
1519 setSize: func(model, w, h)
1520 {
1521 var halfWidth = int(w * 0.5);
1522 var m = me._style.getSize("margin");
1523 var inset = me._style.getSize("text-inset");
1524
1525 me._bg.reset()
1526 .rect(m, m, w - (m * 2), h - (m * 2), {"border-radius": me._style.getSize("frame-radius")});
1527
1528 # we split the two pieces
1529 me._border.setSize(halfWidth, h);
1530 me._buttonBorder.setTranslation(halfWidth, 0);
1531 me._buttonBorder.setSize(w - halfWidth, h);
1532
1533 var arrowSize = me._arrowIcon.imageSize();
1534 me._arrowIcon.setTranslation(w - (arrowSize[0] + inset), (h - arrowSize[1]) * 0.5);
1535
1536 me._label.setTranslation(inset, h * 0.5);
1537 },
1538 setText: func(model, text)
1539 {
1540 me._label.setText(text);
1541 var inset = me._style.getSize("text-inset");
1542 var min_width = math.max(80, me._label.maxWidth() + inset + me._arrowIcon.imageSize()[0]);
1543 model.setLayoutMinimumSize([min_width, 16]);
1544 model.setLayoutSizeHint([min_width, 28]);
1545
1546 return me;
1547 },
1548 update: func(model)
1549 {
1550 var backdrop = !model._windowFocus();
1551 var (w, h) = model._size;
1552 var file = me._style._dir_widgets ~ "/";
1553
1554 # TODO unify color names with image names
1555 var bg_color_name = "button_bg_color";
1556 if( backdrop )
1557 bg_color_name = "button_backdrop_bg_color";
1558 else if( !model._enabled )
1559 bg_color_name = "button_bg_color_insensitive";
1560 else if( model._down )
1561 bg_color_name = "button_bg_color_down";
1562 else if( model._hover )
1563 bg_color_name = "button_bg_color_hover";
1564 me._bg.set("fill", me._style.getColor(bg_color_name));
1565
1566 var arrowIconFile = file ~ "combobox-arrow";
1567
1568 if( backdrop )
1569 {
1570 file ~= "backdrop-";
1571 me._label.set("fill", me._style.getColor("backdrop_fg_color"));
1572 }
1573 else
1574 me._label.set("fill", me._style.getColor("fg_color"));
1575 file ~= "combobox";
1576
1577 var buttonFile = file ~ "-button";
1578 file ~= "-entry";
1579
1580 var suffix = "";
1581 if( model._down )
1582 { # no pressed image for the left half
1583 buttonFile ~= "-pressed";
1584 }
1585
1586 if( model._enabled ) {
1587 if( model._focused and !backdrop )
1588 suffix ~= "-focused";
1589 } else {
1590 suffix ~= "-disabled";
1591 arrowIconFile ~= "-disabled";
1592 }
1593
1594 me._border.set("src", file ~ suffix ~ ".png");
1595 me._buttonBorder.set("src", buttonFile ~ suffix ~ ".png");
1596 me._arrowIcon.set("src", arrowIconFile ~ ".png");
1597 }
1598 };
1599
1600 DefaultStyle.widgets["list-item"] = {
1601 new: func(parent, cfg) {
1602 me._root = parent.createChild("group", "list-item");
1603 me._bg = me._root.createChild("path");
1604 me._itemHeight = me._style.getSize("list-item-height");
1605
1606 me._label = me._root.createChild("text")
1607 .set("font", "LiberationFonts/LiberationSans-Regular.ttf")
1608 .set("character-size", me._style.getSize("list-font-size"))
1609 .set("alignment", "left-baseline");
1610 },
1611
1612 setSize: func(model, w, h) {
1613 me._bg.reset().rect(0, 0, w, me._itemHeight);
1614 var m = me._style.getSize("margin");
1615 me._label.setTranslation(m, int(h / 2) + m);
1616 return me;
1617 },
1618
1619 _updateLayoutSizes: func(model) {
1620 var m = me._style.getSize("margin");
1621 var min_width = m + me._label.maxWidth() + m;
1622 model.setLayoutMinimumSize([min_width, me._itemHeight]);
1623 model.setLayoutSizeHint([min_width, me._itemHeight]);
1624 model.setLayoutMaximumSize([model._MAX_SIZE, me._itemHeight]);
1625
1626 return me;
1627 },
1628
1629 setText: func(model, text) {
1630 me._label.setText(text);
1631 return me._updateLayoutSizes(model);
1632 },
1633
1634 update: func(model) {
1635 me._bg.set("fill", me._style.getColor("list_item_bg" ~ (model._selected ? "_selected" : "")));
1636 var text_color_name = "list_item_fg";
1637 if (model._selected) {
1638 text_color_name ~= "_selected";
1639 }
1640 me._label.set("fill", me._style.getColor(text_color_name));
1641
1642 return me;
1643 }
1644 };
1645
1646 DefaultStyle.widgets.list = {
1647 new: func(parent, cfg) {
1648 me._root = parent.createChild("group", "list");
1649 me._bg = me._root.createChild("path");
1650 },
1651
1652 setSize: func(model, w, h) {
1653 me._bg.reset().rect(0, 0, w, h);
1654 return me;
1655 },
1656
1657 update: func(model) {
1658 me._bg.set("fill", me._style.getColor("bg_color"));
1659
1660 return me;
1661 },
1662
1663 _updateLayoutSizes: func(model) {
1664 model.setLayoutMinimumSize([me._itemHeight * 2, me._itemHeight]);
1665 model.setLayoutMaximumSize([model._MAX_SIZE, me._itemHeight]);
1666 }
1667 };
1668