1 // Written in the D programming language.
2 /++
3  + Authors: KanzakiKino
4  + Copyright: KanzakiKino 2018
5  + License: LGPL-3.0
6 ++/
7 module w4d.widget.base.context;
8 
9 /// A template that declares WindowContext.
10 template Context ()
11 {
12     enum Modkey
13     {
14         None  = 0b00,
15 
16         Ctrl  = 0b01,
17         Shift = 0b10,
18     }
19 
20     class WindowContext
21     {
22         this ()
23         {
24             _tracked = null;
25             _focused = null;
26             _popup   = null;
27         }
28 
29         protected bool _needRedraw;
30         const @property needRedraw () { return _needRedraw; }
31 
32         void requestRedraw ()
33         {
34             _needRedraw = true;
35         }
36         void setNoNeedRedraw ()
37         {
38             _needRedraw = false;
39         }
40 
41 
42         protected uint _modkey;
43 
44         const @property ctrl ()
45         {
46             return !!( _modkey & Modkey.Ctrl );
47         }
48         const @property shift ()
49         {
50             return !!( _modkey & Modkey.Shift );
51         }
52 
53         void setModkeyStatus ( Modkey key, bool press )
54         {
55             if ( press ) {
56                 _modkey |= key;
57             } else {
58                 _modkey &= ~key;
59             }
60         }
61 
62 
63         protected Widget _tracked;
64 
65         inout @property tracked ()
66         {
67             return _popup? _popup: _tracked;
68         }
69         void setTracked ( Widget w )
70         {
71             auto temp = _tracked;
72             _tracked = w;
73 
74             if ( w !is temp ) {
75                 if ( temp ) temp.handleTracked( false );
76                 if ( w    ) w   .handleTracked( true  );
77             }
78         }
79 
80 
81         protected Widget _focused;
82 
83         inout @property focused ()
84         {
85             return _popup? _popup: _focused;
86         }
87         void setFocused ( Widget w )
88         {
89             auto temp = _focused;
90             _focused = w;
91 
92             if ( w !is temp ) {
93                 if ( temp ) temp.handleFocused( false );
94                 if ( w    ) w   .handleFocused( true  );
95             }
96         }
97 
98 
99         protected Widget _popup;
100 
101         inout @property popup () { return _popup; }
102 
103         void setPopup ( Widget w )
104         {
105             auto temp = _popup;
106             _popup = w;
107 
108             if ( w !is temp ) {
109                 if ( temp ) temp.handlePopup( false, this );
110                 if ( w    ) w   .handlePopup( true , this );
111             }
112         }
113 
114 
115         void forget ( in Widget w )
116         {
117             if ( tracked is w ) {
118                 setTracked( null );
119             }
120             if ( focused is w ) {
121                 setFocused( null );
122             }
123             if ( popup is w ) {
124                 setPopup( null );
125             }
126         }
127     }
128 }