- Move imagesoft to templates.
[reactos.git] / rosapps / dflat32 / msgbox.c
1 /* ------------------ msgbox.c ------------------ */
2
3 #include "dflat.h"
4
5 extern DF_DBOX MsgBox;
6 extern DF_DBOX InputBoxDB;
7 DFWINDOW CancelWnd;
8
9 static int ReturnValue;
10
11 int DfMessageBoxProc(DFWINDOW wnd, DFMESSAGE msg, DF_PARAM p1, DF_PARAM p2)
12 {
13 switch (msg) {
14 case DFM_CREATE_WINDOW:
15 DfGetClass(wnd) = DF_MESSAGEBOX;
16 DfInitWindowColors(wnd);
17 DfClearAttribute(wnd, DF_CONTROLBOX);
18 break;
19 case DFM_KEYBOARD:
20 if (p1 == '\r' || p1 == DF_ESC)
21 ReturnValue = (int)p1;
22 break;
23 default:
24 break;
25 }
26 return DfBaseWndProc(DF_MESSAGEBOX, wnd, msg, p1, p2);
27 }
28
29 int DfYesNoBoxProc(DFWINDOW wnd, DFMESSAGE msg, DF_PARAM p1, DF_PARAM p2)
30 {
31 switch (msg) {
32 case DFM_CREATE_WINDOW:
33 DfGetClass(wnd) = DF_MESSAGEBOX;
34 DfInitWindowColors(wnd);
35 DfClearAttribute(wnd, DF_CONTROLBOX);
36 break;
37 case DFM_KEYBOARD: {
38 int c = tolower((int)p1);
39 if (c == 'y')
40 DfSendMessage(wnd, DFM_COMMAND, DF_ID_OK, 0);
41 else if (c == 'n')
42 DfSendMessage(wnd, DFM_COMMAND, DF_ID_CANCEL, 0);
43 break;
44 }
45 default:
46 break;
47 }
48 return DfBaseWndProc(DF_MESSAGEBOX, wnd, msg, p1, p2);
49 }
50
51 int DfErrorBoxProc(DFWINDOW wnd, DFMESSAGE msg, DF_PARAM p1, DF_PARAM p2)
52 {
53 switch (msg) {
54 case DFM_CREATE_WINDOW:
55 DfGetClass(wnd) = DF_ERRORBOX;
56 DfInitWindowColors(wnd);
57 break;
58 case DFM_KEYBOARD:
59 if (p1 == '\r' || p1 == DF_ESC)
60 ReturnValue = (int)p1;
61 break;
62 default:
63 break;
64 }
65 return DfBaseWndProc(DF_ERRORBOX, wnd, msg, p1, p2);
66 }
67
68 int DfCancelBoxProc(DFWINDOW wnd, DFMESSAGE msg, DF_PARAM p1, DF_PARAM p2)
69 {
70 switch (msg) {
71 case DFM_CREATE_WINDOW:
72 CancelWnd = wnd;
73 DfSendMessage(wnd, DFM_CAPTURE_MOUSE, 0, 0);
74 DfSendMessage(wnd, DFM_CAPTURE_KEYBOARD, 0, 0);
75 break;
76 case DFM_COMMAND:
77 if ((int) p1 == DF_ID_CANCEL && (int) p2 == 0)
78 DfSendMessage(DfGetParent(wnd), msg, p1, p2);
79 return TRUE;
80 case DFM_CLOSE_WINDOW:
81 CancelWnd = NULL;
82 DfSendMessage(wnd, DFM_RELEASE_MOUSE, 0, 0);
83 DfSendMessage(wnd, DFM_RELEASE_KEYBOARD, 0, 0);
84 p1 = TRUE;
85 break;
86 default:
87 break;
88 }
89 return DfBaseWndProc(DF_MESSAGEBOX, wnd, msg, p1, p2);
90 }
91
92 void DfCloseCancelBox(void)
93 {
94 if (CancelWnd != NULL)
95 DfSendMessage(CancelWnd, DFM_CLOSE_WINDOW, 0, 0);
96 }
97
98 static char *InputText;
99 static int TextLength;
100
101 int InputBoxProc(DFWINDOW wnd, DFMESSAGE msg, DF_PARAM p1, DF_PARAM p2)
102 {
103 int rtn;
104 switch (msg) {
105 case DFM_CREATE_WINDOW:
106 rtn = DfDefaultWndProc(wnd, msg, p1, p2);
107 DfSendMessage(DfControlWindow(&InputBoxDB,DF_ID_INPUTTEXT),
108 DFM_SETTEXTLENGTH, TextLength, 0);
109 return rtn;
110 case DFM_COMMAND:
111 if ((int) p1 == DF_ID_OK && (int) p2 == 0)
112 DfGetItemText(wnd, DF_ID_INPUTTEXT,
113 InputText, TextLength);
114 break;
115 default:
116 break;
117 }
118 return DfDefaultWndProc(wnd, msg, p1, p2);
119 }
120
121 BOOL DfInputBox(DFWINDOW wnd,char *ttl,char *msg,char *text,int len)
122 {
123 InputText = text;
124 TextLength = len;
125 InputBoxDB.dwnd.title = ttl;
126 InputBoxDB.dwnd.w = 4 +
127 max(20, max(len, max((int)strlen(ttl), (int)strlen(msg))));
128 InputBoxDB.ctl[1].dwnd.x = (InputBoxDB.dwnd.w-2-len)/2;
129 InputBoxDB.ctl[0].dwnd.w = strlen(msg);
130 InputBoxDB.ctl[0].itext = msg;
131 InputBoxDB.ctl[1].dwnd.w = len;
132 InputBoxDB.ctl[2].dwnd.x = (InputBoxDB.dwnd.w - 20) / 2;
133 InputBoxDB.ctl[3].dwnd.x = InputBoxDB.ctl[2].dwnd.x + 10;
134 InputBoxDB.ctl[2].isetting = DF_ON;
135 InputBoxDB.ctl[3].isetting = DF_ON;
136 return DfDialogBox(wnd, &InputBoxDB, TRUE, InputBoxProc);
137 }
138
139 BOOL DfGenericMessage(DFWINDOW wnd,char *ttl,char *msg,int buttonct,
140 int (*wndproc)(struct DfWindow *,enum DfMessages,DF_PARAM,DF_PARAM),
141 char *b1, char *b2, int c1, int c2, int isModal)
142 {
143 BOOL rtn;
144 MsgBox.dwnd.title = ttl;
145 MsgBox.ctl[0].dwnd.h = DfMsgHeight(msg);
146 if (ttl)
147 MsgBox.ctl[0].dwnd.w = max(max(DfMsgWidth(msg),
148 (int)(buttonct*8 + buttonct + 2)), (int)strlen(ttl)+2);
149 else
150 MsgBox.ctl[0].dwnd.w = max(DfMsgWidth(msg), (int)(buttonct*8 + buttonct + 2));
151 MsgBox.dwnd.h = MsgBox.ctl[0].dwnd.h+6;
152 MsgBox.dwnd.w = MsgBox.ctl[0].dwnd.w+4;
153 if (buttonct == 1)
154 MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 10) / 2;
155 else {
156 MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 20) / 2;
157 MsgBox.ctl[2].dwnd.x = MsgBox.ctl[1].dwnd.x + 10;
158 MsgBox.ctl[2].class = DF_BUTTON;
159 }
160 MsgBox.ctl[1].dwnd.y = MsgBox.dwnd.h - 4;
161 MsgBox.ctl[2].dwnd.y = MsgBox.dwnd.h - 4;
162 MsgBox.ctl[0].itext = msg;
163 MsgBox.ctl[1].itext = b1;
164 MsgBox.ctl[2].itext = b2;
165 MsgBox.ctl[1].command = c1;
166 MsgBox.ctl[2].command = c2;
167 MsgBox.ctl[1].isetting = DF_ON;
168 MsgBox.ctl[2].isetting = DF_ON;
169 rtn = DfDialogBox(wnd, &MsgBox, isModal, wndproc);
170 MsgBox.ctl[2].class = 0;
171 return rtn;
172 }
173
174 DFWINDOW DfMomentaryMessage(char *msg)
175 {
176 DFWINDOW wnd = DfDfCreateWindow(
177 DF_TEXTBOX,
178 NULL,
179 -1,-1,DfMsgHeight(msg)+2,DfMsgWidth(msg)+2,
180 NULL,NULL,NULL,
181 DF_HASBORDER | DF_SHADOW | DF_SAVESELF);
182 DfSendMessage(wnd, DFM_SETTEXT, (DF_PARAM) msg, 0);
183 DfWindowClientColor(wnd, WHITE, GREEN);
184 DfWindowFrameColor(wnd, WHITE, GREEN);
185 DfSendMessage (wnd, DFM_SHOW_WINDOW, 0, 0);
186 return wnd;
187 }
188
189 int DfMsgHeight(char *msg)
190 {
191 int h = 1;
192
193 while ((msg = strchr(msg, '\n')) != NULL)
194 {
195 h++;
196 msg++;
197 }
198
199 return min(h, DfGetScreenHeight ()-10);
200 }
201
202 int DfMsgWidth(char *msg)
203 {
204 int w = 0;
205 char *cp = msg;
206
207 while ((cp = strchr(msg, '\n')) != NULL)
208 {
209 w = max(w, (int) (cp-msg));
210 msg = cp+1;
211 }
212
213 return min(max((int)strlen(msg), (int)w), DfGetScreenWidth()-10);
214 }
215
216 /* EOF */