1 /**
2 Copyright: Copyright (c) 2014 Andrey Penechko.
3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module anchovy.gui.behaviors.radiobehavior;
8 
9 import anchovy.gui;
10 import anchovy.gui.interfaces.iwidgetbehavior;
11 
12 //version = Radio_debug;
13 
14 class RadioBehavior : CheckBehavior
15 {
16 	override void attachPropertiesTo(Widget widget)
17 	{
18 		super.attachPropertiesTo(widget);
19 		widget.setProperty!("group", int) = 0;
20 		widget.property("group").valueChanged.connect(&onGroupChanged);
21 		widget.property("isChecked").valueChanged.connect(&onCheckedChanged);
22 	}
23 
24 	override void attachTo(Widget widget)
25 	{
26 		widget.addEventHandler(&handleGroupSelectionChanged);
27 		super.attachTo(widget);
28 	}
29 
30 	override bool onClick(Widget widget, PointerClickEvent event)
31 	{
32 		widget.setProperty!"isChecked"(true);
33 
34 		return true;
35 	}
36 
37 	void onCheckedChanged(FlexibleObject a, Variant b)
38 	{
39 		auto context = a.getPropertyAs!("context", GuiContext);
40 		auto groupId = a.getPropertyAs!("group", int);
41 
42 		auto selected = context.widgetManager.getGroupSelected(groupId);
43 
44 		if (b == false && selected is a)
45 		{
46 			context.widgetManager.setGroupSelected(groupId, null);
47 		}
48 		else if (b == true)
49 		{
50 			context.widgetManager.setGroupSelected(groupId, cast(Widget)a);
51 		}
52 	}
53 
54 	void onGroupChanged(FlexibleObject a, Variant b)
55 	{
56 		auto groupId = a.getPropertyAs!("group", int);
57 		version(Radio_debug) writefln("group changed %s", groupId);
58 		a.setProperty!("isChecked", bool)(false);
59 	}
60 
61 	bool handleGroupSelectionChanged(Widget widget, GroupSelectionEvent event)
62 	{
63 		version(Radio_debug) "selected changed %s %s".writefln(event.selected, event.selected is widget);
64 		widget.setProperty!("isChecked", bool)(event.selected is widget);
65 
66 		return true;
67 	}
68 }