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.popup.dialog.text;
8 import w4d.layout.lineup,
9        w4d.widget.popup.dialog.base,
10        w4d.widget.button,
11        w4d.widget.panel,
12        w4d.widget.text,
13        w4d.event,
14        w4d.exception;
15 import g4d.ft.font;
16 
17 /// An enum of dialog buttons.
18 enum DialogButton
19 {
20     Ok,
21     Cancel,
22 }
23 
24 /// Converts DialogButton to dstring.
25 @property dstring toDString ( DialogButton type )
26 {
27     final switch ( type ) with ( DialogButton ) {
28         case Ok    : return "OK"d;
29         case Cancel: return "Cancel"d;
30     }
31 }
32 
33 /// A handler that handles closing the dialog.
34 alias ResultHandler = EventHandler!( void, DialogButton );
35 
36 /// A widget of text dialog.
37 class PopupTextDialogWidget : PopupDialogWidget
38 {
39     protected class CustomButton : ButtonWidget
40     {
41         const DialogButton type;
42 
43         override void handleButtonPress ()
44         {
45             handleResult( type );
46         }
47 
48         this ( DialogButton t, FontFace face )
49         {
50             super();
51             type = t;
52 
53             loadText( t.toDString, face );
54         }
55     }
56 
57     protected TextWidget  _text;
58     protected PanelWidget _buttons;
59 
60     ///
61     ResultHandler onResult;
62 
63     ///
64     void handleResult ( DialogButton btn )
65     {
66         close();
67         onResult.call( btn );
68     }
69 
70     ///
71     this ()
72     {
73         super();
74         setLayout!VerticalLineupLayout;
75 
76         _text = new TextWidget;
77         addChild( _text );
78 
79         _buttons = new PanelWidget;
80         _buttons.setLayout!HorizontalLineupLayout;
81         addChild( _buttons );
82     }
83 
84     /// Changes text.
85     void loadText ( dstring v, FontFace face = null )
86     {
87         _text.loadText( v, face );
88     }
89 
90     /// Changes the buttons.
91     void setButtons ( DialogButton[] btns, FontFace face = null )
92     {
93         if ( !face ) {
94             face = _text.font;
95         }
96         enforce( face, "FontFace is not specified." );
97 
98         _buttons.removeAllChildren();
99         foreach ( type; btns ) {
100             _buttons.addChild( new CustomButton(type,face) );
101         }
102         requestLayout();
103     }
104 }