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