Prompt.cpp

Zbigniew Rebacz, 01/16/2014 01:08 AM

Download (7.72 KB)

 
1
#include "CtrlLib.h"
2

    
3
NAMESPACE_UPP
4

    
5
struct PromptDlgWnd__ : TopWindow {
6
        bool    esc;
7
        Button *b;
8

    
9
        virtual bool HotKey(dword key) {
10
                if(TopWindow::HotKey(key))
11
                        return true;
12
                if(IsAlpha(key))
13
                        return TopWindow::HotKey(K_ALT_A + ToUpper((int)key) - 'A');
14
                if(key == K_ESCAPE && esc)
15
                        b->PseudoPush();
16
                return false;
17
        }
18
};
19

    
20
static void sAdd(Ctrl& dlg, int fcy, int bcy, int& bx, int bcx, int gap, Button& b, const char *button, const Image& img)
21
{
22
        if(button) {
23
                dlg << b.BottomPos(fcy, bcy).LeftPos(bx, bcx);
24
                b.SetLabel(button);
25
                if(!IsNull(img))
26
                        b.SetImage(img);
27
                bx += gap + bcx;
28
        }
29
}
30

    
31
void sExecutePrompt(PromptDlgWnd__ *dlg, int *result)
32
{
33
        dlg->Open();
34
        Vector<Ctrl *> wins = Ctrl::GetTopWindows();
35
        for(int i = 0; i < wins.GetCount(); i++) {
36
                TopWindow *w = dynamic_cast<TopWindow *>(wins[i]);
37
                if(w && w->IsTopMost()) {
38
                        dlg->TopMost();
39
                        break;
40
                }
41
        }
42
        *result = dlg->RunAppModal();
43
}
44

    
45
int Prompt(Callback1<const String&> WhenLink,
46
           const char *title, const Image& iconbmp, const char *qtf, bool okcancel,
47
           const char *button1, const char *button2, const char *button3,
48
                   int cx,
49
                   Image im1, Image im2, Image im3)
50
{
51
        int fcy = Draw::GetStdFontCy();
52
        PromptDlgWnd__ dlg;
53
        RichTextCtrl qtfctrl;
54
        Icon         icon;
55
        qtfctrl.WhenLink = WhenLink;
56
        icon.SetImage(iconbmp);
57
        Button b1, b2, b3;
58
        qtfctrl.SetQTF(String("[G1 ") + qtf, GetRichTextStdScreenZoom());
59
        int bcy = Ctrl::VertLayoutZoom(24);
60
        int bcx = Ctrl::HorzLayoutZoom(72);
61
        if(button1)
62
                bcx = max(2 * fcy + GetTextSize(button1, Draw::GetStdFont()).cx, bcx);
63
        if(button2)
64
                bcx = max(2 * fcy + GetTextSize(button2, Draw::GetStdFont()).cx, bcx);
65
        if(button3)
66
                bcx = max(2 * fcy + GetTextSize(button3, Draw::GetStdFont()).cx, bcx);
67
        Size bsz = icon.GetStdSize();
68
        if(cx == 0) {
69
                cx = qtfctrl.GetWidth();
70
                if(!cx)
71
                        cx = 350;
72
                cx += 2 * fcy;
73
                if(bsz.cx)
74
                        cx += bsz.cx + fcy;
75
        }
76
        int nbtn = !!button1 + !!button2 + !!button3;
77
        dlg.esc = okcancel && nbtn == 1;
78
        cx = min(550, max(nbtn * bcx + (1 + nbtn) * fcy, cx));
79
        int qcx = cx - 2 * fcy;
80
        if(bsz.cx)
81
                qcx -= bsz.cx + fcy;
82
        int ccy = qtfctrl.GetHeight(qcx);
83
        int qcy = min(400, ccy);
84
        if(qcy <= ccy) {
85
                qcx += ScrollBarSize() + fcy;
86
                cx += ScrollBarSize() + fcy;
87
        }
88
        int mcy = max(qcy, bsz.cy);
89
        int cy = mcy + 48 * fcy / 10;
90
        dlg.SetRect(Size(cx, cy));
91
        dlg << icon.TopPos(fcy, bsz.cy).LeftPos(fcy, bsz.cx);
92
        dlg << qtfctrl.TopPos(fcy + (mcy - qcy) / 2, qcy).RightPos(fcy, qcx);
93
        if(okcancel) {
94
                b1.Ok();
95
                if(nbtn == 2)
96
                        b2.Cancel();
97
                if(nbtn == 3)
98
                        b3.Cancel();
99
        }
100
        b1.WhenAction = dlg.Breaker(1);
101
        b2.WhenAction = dlg.Breaker(0);
102
        b3.WhenAction = dlg.Breaker(-1);
103
        dlg.b = &b1;
104
        int bx = bcx;
105
        int gap = fcy / 2;
106
        fcy = 8 * fcy / 10;
107
        if(button2)
108
                bx += gap + bcx;
109
        if(button3)
110
                bx += gap + bcx;
111
        bx = (cx - bx) / 2;
112
        if(SwapOKCancel()) {
113
                sAdd(dlg, fcy, bcy, bx, bcx, gap, b2, button2, im2);
114
                sAdd(dlg, fcy, bcy, bx, bcx, gap, b3, button3, im3);
115
                sAdd(dlg, fcy, bcy, bx, bcx, gap, b1, button1, im1);
116
        }
117
        else {
118
                sAdd(dlg, fcy, bcy, bx, bcx, gap, b1, button1, im1);
119
                sAdd(dlg, fcy, bcy, bx, bcx, gap, b2, button2, im2);
120
                sAdd(dlg, fcy, bcy, bx, bcx, gap, b3, button3, im3);
121
        }
122
        dlg.WhenClose = dlg.Breaker(button3 ? -1 : 0);
123
        dlg.Title(title);
124
        int result;
125
        Ctrl::Call(callback2(sExecutePrompt, &dlg, &result));
126
        return result;
127
}
128

    
129
int Prompt(Callback1<const String&> WhenLink,
130
           const char *title, const Image& icon, const char *qtf, bool okcancel,
131
           const char *button1, const char *button2, const char *button3,
132
                   int cx)
133
{
134
        return Prompt(WhenLink, title, icon, qtf, okcancel, button1, button2, button3, cx, Null, Null, Null);
135
}
136

    
137
int Prompt(const char *title, const Image& icon, const char *qtf, bool okcancel,
138
           const char *button1, const char *button2, const char *button3,
139
                   int cx)
140
{
141
        return Prompt(callback(LaunchWebBrowser), title,
142
                      icon, qtf, okcancel, button1, button2, button3, cx, Null, Null, Null);
143
}
144

    
145
int Prompt(const char *title, const Image& icon, const char *qtf,
146
           const char *button1, const char *button2, const char *button3,
147
                   int cx)
148
{
149
        return Prompt(title, icon, qtf, true, button1, button2, button3, cx);
150
}
151

    
152
void PromptOK(const char *qtf) {
153
        BeepInformation();
154
        Prompt(Ctrl::GetAppName(), CtrlImg::information(), qtf, t_("OK"));
155
}
156

    
157
void Exclamation(const char *qtf) {
158
        BeepExclamation();
159
        Prompt(Ctrl::GetAppName(), CtrlImg::exclamation(), qtf, t_("OK"));
160
}
161

    
162
void ShowExc(const Exc& exc) {
163
        BeepExclamation();
164
        Prompt(Ctrl::GetAppName(), CtrlImg::exclamation(), DeQtf(exc), t_("OK"));
165
}
166

    
167
void ErrorOK(const char *qtf) {
168
        BeepError();
169
        Prompt(Ctrl::GetAppName(), CtrlImg::error(), qtf, t_("OK"));
170
}
171

    
172
int PromptOKCancel(const char *qtf) {
173
        BeepQuestion();
174
        return Prompt(Ctrl::GetAppName(), CtrlImg::question(), qtf, t_("OK"), t_("Cancel"));
175
}
176

    
177
int ErrorOKCancel(const char *qtf) {
178
        BeepError();
179
        return Prompt(Ctrl::GetAppName(), CtrlImg::error(), qtf, t_("OK"), t_("Cancel"));
180
}
181

    
182
CH_IMAGE(YesButtonImage, Null);
183
CH_IMAGE(NoButtonImage, Null);
184
CH_IMAGE(AbortButtonImage, Null);
185
CH_IMAGE(RetryButtonImage, Null);
186

    
187
int PromptYesNo(const char *qtf) {
188
        BeepQuestion();
189
        return Prompt(callback(LaunchWebBrowser),
190
                      Ctrl::GetAppName(), CtrlImg::question(), qtf, false,
191
                      t_("&Yes"), t_("&No"), NULL, 0,
192
                      YesButtonImage(), NoButtonImage(), Null);
193
}
194

    
195
int ErrorYesNo(const char *qtf) {
196
        BeepError();
197
        return Prompt(callback(LaunchWebBrowser),
198
                      Ctrl::GetAppName(), CtrlImg::error(), qtf, false,
199
                      t_("&Yes"), t_("&No"), NULL, 0,
200
                      YesButtonImage(), NoButtonImage(), Null);
201
}
202

    
203
int PromptYesNoCancel(const char *qtf) {
204
        BeepQuestion();
205
        return Prompt(callback(LaunchWebBrowser),
206
                      Ctrl::GetAppName(), CtrlImg::question(), qtf, true,
207
                      t_("&Yes"), t_("&No"), t_("Cancel"), 0,
208
                      YesButtonImage(), NoButtonImage(), Null);
209
}
210

    
211
int ErrorYesNoCancel(const char *qtf) {
212
        BeepError();
213
        return Prompt(callback(LaunchWebBrowser),
214
                      Ctrl::GetAppName(), CtrlImg::error(), qtf, true,
215
                      t_("&Yes"), t_("&No"), t_("Cancel"), 0,
216
                      YesButtonImage(), NoButtonImage(), Null);
217
}
218

    
219
int PromptAbortRetry(const char *qtf) {
220
        BeepExclamation();
221
        return Prompt(callback(LaunchWebBrowser),
222
                      Ctrl::GetAppName(), CtrlImg::exclamation(), qtf, false,
223
                      t_("&Abort"), t_("&Retry"), NULL, 0,
224
                      AbortButtonImage(), RetryButtonImage(), Null);
225
}
226

    
227
int ErrorAbortRetry(const char *qtf) {
228
        BeepError();
229
        return Prompt(callback(LaunchWebBrowser),
230
                      Ctrl::GetAppName(), CtrlImg::error(), qtf, false,
231
                      t_("&Abort"), t_("&Retry"), NULL, 0,
232
                      AbortButtonImage(), RetryButtonImage(), Null);
233
}
234

    
235
int PromptRetryCancel(const char *qtf) {
236
        BeepExclamation();
237
        return Prompt(callback(LaunchWebBrowser),
238
                      Ctrl::GetAppName(), CtrlImg::exclamation(), qtf, true,
239
                      t_("&Retry"), t_("Cancel"), NULL, 0,
240
                      RetryButtonImage(), Null, Null);
241
}
242

    
243
int ErrorRetryCancel(const char *qtf) {
244
        BeepError();
245
        return Prompt(callback(LaunchWebBrowser),
246
                      Ctrl::GetAppName(), CtrlImg::error(), qtf, true,
247
                      t_("&Retry"), t_("Cancel"), NULL, 0,
248
                      RetryButtonImage(), Null, Null);
249
}
250

    
251
int PromptAbortRetryIgnore(const char *qtf) {
252
        BeepExclamation();
253
        return Prompt(callback(LaunchWebBrowser),
254
                      Ctrl::GetAppName(), CtrlImg::exclamation(), qtf, false,
255
                      t_("&Abort"), t_("&Retry"), t_("&Ignore"), 0,
256
                      AbortButtonImage(), RetryButtonImage(), Null);
257
}
258

    
259
int ErrorAbortRetryIgnore(const char *qtf) {
260
        BeepError();
261
        return Prompt(callback(LaunchWebBrowser),
262
                      Ctrl::GetAppName(), CtrlImg::error(), qtf, false,
263
                      t_("&Abort"), t_("&Retry"), t_("&Ignore"), 0,
264
                      AbortButtonImage(), RetryButtonImage(), Null);
265
}
266

    
267
END_UPP_NAMESPACE