Tabs => space
[AROS.git] / workbench / utilities / More / req.c
blob7b9424fc20c9cc148b1b7fc1c0ffaf33c29ed7e1
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <exec/memory.h>
7 #include <dos/dos.h>
8 #include <intuition/intuition.h>
9 #include <intuition/imageclass.h>
10 #include <intuition/gadgetclass.h>
11 #include <libraries/gadtools.h>
12 #include <graphics/gfx.h>
13 #include <utility/hooks.h>
15 #include <proto/exec.h>
16 #include <proto/dos.h>
17 #include <proto/intuition.h>
18 #include <proto/graphics.h>
19 #include <proto/gadtools.h>
20 #include <proto/alib.h>
21 #include <proto/utility.h>
23 #include "global.h"
24 #include "req.h"
26 #define CATCOMP_NUMBERS
27 #include "strings.h"
29 #define DEBUG 0
30 #include <aros/debug.h>
32 /****************************************************************************************/
35 #define BORDER_SPACING_X 4
36 #define BORDER_SPACING_Y 4
38 #define GAD_SPACING_X 8
39 #define GAD_SPACING_Y 4
41 #define GAD_EXTRA_WIDTH 16
42 #define GAD_EXTRA_HEIGHT 6
44 enum {GAD_FIND_TEXT = 1,
45 GAD_FIND_OK,
46 GAD_FIND_CANCEL};
48 enum {GAD_GOTO_STRING = 1,
49 GAD_GOTO_OK,
50 GAD_GOTO_CANCEL};
52 /****************************************************************************************/
55 static struct RastPort temprp;
56 static struct Window *gotowin, *findwin;
57 static struct Gadget *gotogad, *findgad, *gad, *gotogadlist, *findgadlist;
58 static struct NewGadget ng;
59 static WORD fontwidth, fontheight;
61 static char searchtext[256];
63 /****************************************************************************************/
65 static BOOL Init(void)
67 fontwidth = dri->dri_Font->tf_XSize;
68 fontheight = dri->dri_Font->tf_YSize;
70 InitRastPort(&temprp);
71 SetFont(&temprp, dri->dri_Font);
73 memset(&ng, 0, sizeof(ng));
74 ng.ng_VisualInfo = vi;
76 return TRUE;
79 /****************************************************************************************/
82 void CleanupRequesters(void)
84 if (gotowin) Kill_Goto_Requester();
85 if (findwin) Kill_Find_Requester();
88 /****************************************************************************************/
91 void Make_Goto_Requester(void)
93 WORD winwidth, winheight, gadwidth, gadheight;
94 WORD strwidth, w;
96 if (gotowin || !Init()) return;
98 gad = CreateContext(&gotogadlist);
100 gadheight = fontheight + GAD_EXTRA_HEIGHT;
102 winheight = scr->WBorTop + fontheight + 1 +
103 scr->WBorBottom +
104 BORDER_SPACING_Y * 2 +
105 gadheight * 2 +
106 GAD_SPACING_Y;
108 gadwidth = TextLength(&temprp, MSG(MSG_OK), strlen(MSG(MSG_OK)));
109 w = TextLength(&temprp, MSG(MSG_CANCEL), strlen(MSG(MSG_CANCEL)));
110 if (w > gadwidth) gadwidth = w;
112 gadwidth += GAD_EXTRA_WIDTH;
114 strwidth = gadwidth * 2 + GAD_SPACING_X;
116 winwidth = scr->WBorLeft +
117 scr->WBorRight +
118 BORDER_SPACING_X * 2 +
119 strwidth;
121 ng.ng_LeftEdge = scr->WBorLeft + BORDER_SPACING_X;
122 ng.ng_TopEdge = scr->WBorTop + fontheight + 1 + BORDER_SPACING_Y;
123 ng.ng_Width = strwidth;
124 ng.ng_Height = gadheight;
125 ng.ng_GadgetID = GAD_GOTO_STRING;
126 ng.ng_Flags = PLACETEXT_IN;
128 gotogad = CreateGadget(INTEGER_KIND, gad, &ng, GTIN_MaxChars, 8,
129 STRINGA_Justification, GACT_STRINGCENTER,
130 TAG_DONE);
132 ng.ng_TopEdge += gadheight + GAD_SPACING_Y;
133 ng.ng_Width = gadwidth;
134 ng.ng_GadgetText = MSG(MSG_OK);
135 ng.ng_GadgetID = GAD_GOTO_OK;
137 gad = CreateGadgetA(BUTTON_KIND, gotogad, &ng, 0);
139 ng.ng_LeftEdge += gadwidth + GAD_SPACING_X;
140 ng.ng_GadgetText = MSG(MSG_CANCEL);
141 ng.ng_GadgetID = GAD_GOTO_CANCEL;
143 gad = CreateGadgetA(BUTTON_KIND, gad, &ng, 0);
145 if (!gad)
147 FreeGadgets(gotogadlist);
148 gotogadlist = 0;
149 } else {
150 gotowin = OpenWindowTags(0, WA_CustomScreen, (IPTR)scr,
151 WA_Left, scr->MouseX - (winwidth / 2),
152 WA_Top, scr->MouseY - (winheight / 2),
153 WA_Width, winwidth,
154 WA_Height, winheight,
155 WA_AutoAdjust, TRUE,
156 WA_Title, (IPTR)MSG(MSG_JUMP_TITLE),
157 WA_CloseGadget, TRUE,
158 WA_DepthGadget, TRUE,
159 WA_DragBar, TRUE,
160 WA_Activate, TRUE,
161 WA_SimpleRefresh, TRUE,
162 WA_IDCMP, IDCMP_CLOSEWINDOW |
163 IDCMP_REFRESHWINDOW |
164 IDCMP_VANILLAKEY |
165 BUTTONIDCMP |
166 INTEGERIDCMP,
167 WA_Gadgets, (IPTR)gotogadlist,
168 TAG_DONE);
170 if (!gotowin)
172 FreeGadgets(gotogadlist);gotogadlist = 0;
173 } else {
174 gotomask = 1L << gotowin->UserPort->mp_SigBit;
175 GT_RefreshWindow(gotowin, 0);
176 ActivateGadget(gotogad, gotowin, 0);
181 /****************************************************************************************/
183 BOOL Handle_Goto_Requester(LONG *line)
185 struct IntuiMessage *msg;
186 IPTR l;
187 BOOL killreq = FALSE, rc = FALSE;
189 while ((msg = GT_GetIMsg(gotowin->UserPort)))
191 switch (msg->Class)
193 case IDCMP_REFRESHWINDOW:
194 GT_BeginRefresh(gotowin);
195 GT_EndRefresh(gotowin, TRUE);
196 break;
198 case IDCMP_CLOSEWINDOW:
199 killreq = TRUE;
200 break;
202 case IDCMP_GADGETUP:
203 switch (((struct Gadget *)msg->IAddress)->GadgetID)
205 case GAD_GOTO_CANCEL:
206 killreq = TRUE;
207 break;
209 case GAD_GOTO_STRING:
210 case GAD_GOTO_OK:
211 GT_GetGadgetAttrs(gotogad, gotowin, 0, GTIN_Number, (IPTR)&l,
212 TAG_DONE);
213 rc = TRUE;
214 break;
216 } /* switch (((struct Gadget *)msg->IAddress)->GadgetID) */
217 break;
219 case IDCMP_VANILLAKEY:
220 switch(ToUpper(msg->Code))
222 case 27:
223 killreq = TRUE;
224 break;
226 case 9:
227 case 'G':
228 ActivateGadget(gotogad, gotowin, 0);
229 break;
231 } /* switch(msg->Code) */
232 break;
234 } /* switch (msg->Class) */
235 GT_ReplyIMsg(msg);
237 } /* while ((msg = GT_GetIMsg(gotowin))) */
239 if (killreq) Kill_Goto_Requester();
241 if (rc) *line = l;
243 return rc;
246 /****************************************************************************************/
248 void Kill_Goto_Requester(void)
250 if (gotowin)
252 CloseWindow(gotowin);gotowin = 0;gotomask = 0;
255 if (gotogadlist)
257 FreeGadgets(gotogadlist);gotogadlist = 0;
261 /****************************************************************************************/
263 void Make_Find_Requester(void)
265 WORD winwidth, winheight, gadwidth, gadheight;
266 WORD strwidth, w;
268 if (findwin || !Init()) return;
270 gad = CreateContext(&findgadlist);
272 gadheight = fontheight + GAD_EXTRA_HEIGHT;
274 winheight = scr->WBorTop + fontheight + 1 +
275 scr->WBorBottom +
276 BORDER_SPACING_Y * 2 +
277 gadheight * 2 +
278 GAD_SPACING_Y;
280 gadwidth = TextLength(&temprp, MSG(MSG_OK), strlen(MSG(MSG_OK)));
281 w = TextLength(&temprp, MSG(MSG_CANCEL), strlen(MSG(MSG_CANCEL)));
282 if (w > gadwidth) gadwidth = w;
284 gadwidth += GAD_EXTRA_WIDTH;
286 strwidth = gadwidth * 2 + GAD_SPACING_X;
288 if (strwidth < 250) strwidth = 250;
290 winwidth = scr->WBorLeft +
291 scr->WBorRight +
292 BORDER_SPACING_X * 2 +
293 strwidth;
295 ng.ng_LeftEdge = scr->WBorLeft + BORDER_SPACING_X;
296 ng.ng_TopEdge = scr->WBorTop + fontheight + 1 + BORDER_SPACING_Y;
297 ng.ng_Width = strwidth;
298 ng.ng_Height = gadheight;
299 ng.ng_GadgetID = GAD_FIND_TEXT;
300 ng.ng_Flags = PLACETEXT_IN;
302 findgad = CreateGadget(STRING_KIND, gad, &ng, GTST_MaxChars, 256,
303 TAG_DONE);
305 ng.ng_TopEdge += gadheight + GAD_SPACING_Y;
306 ng.ng_Width = gadwidth;
307 ng.ng_GadgetText = MSG(MSG_OK);
308 ng.ng_GadgetID = GAD_FIND_OK;
310 gad = CreateGadgetA(BUTTON_KIND, findgad, &ng, 0);
312 ng.ng_LeftEdge = winwidth - scr->WBorRight - BORDER_SPACING_X - gadwidth;
313 ng.ng_GadgetText = MSG(MSG_CANCEL);
314 ng.ng_GadgetID = GAD_FIND_CANCEL;
316 gad = CreateGadgetA(BUTTON_KIND, gad, &ng, 0);
318 if (!gad)
320 FreeGadgets(findgadlist);
321 findgadlist = 0;
322 } else {
323 findwin = OpenWindowTags(0, WA_CustomScreen, (IPTR)scr,
324 WA_Left, scr->MouseX - (winwidth / 2),
325 WA_Top, scr->MouseY - (winheight / 2),
326 WA_Width, winwidth,
327 WA_Height, winheight,
328 WA_AutoAdjust, TRUE,
329 WA_Title, (IPTR)MSG(MSG_FIND_TITLE),
330 WA_CloseGadget, TRUE,
331 WA_DepthGadget, TRUE,
332 WA_DragBar, TRUE,
333 WA_Activate, TRUE,
334 WA_SimpleRefresh, TRUE,
335 WA_IDCMP, IDCMP_CLOSEWINDOW |
336 IDCMP_REFRESHWINDOW |
337 IDCMP_VANILLAKEY |
338 BUTTONIDCMP |
339 INTEGERIDCMP,
340 WA_Gadgets, (IPTR)findgadlist,
341 TAG_DONE);
343 if (!findwin)
345 FreeGadgets(findgadlist);findgadlist = 0;
346 } else {
347 findmask = 1L << findwin->UserPort->mp_SigBit;
348 GT_RefreshWindow(findwin, 0);
349 ActivateGadget(findgad, findwin, 0);
354 /****************************************************************************************/
356 WORD Handle_Find_Requester(char **text)
358 struct IntuiMessage *msg;
359 char *sp;
360 BOOL killreq = FALSE, rc = 0;
362 while ((msg = GT_GetIMsg(findwin->UserPort)))
364 switch (msg->Class)
366 case IDCMP_REFRESHWINDOW:
367 GT_BeginRefresh(findwin);
368 GT_EndRefresh(findwin, TRUE);
369 break;
371 case IDCMP_CLOSEWINDOW:
372 killreq = TRUE;
373 break;
375 case IDCMP_GADGETUP:
376 switch (((struct Gadget *)msg->IAddress)->GadgetID)
378 case GAD_FIND_CANCEL:
379 killreq = TRUE;
380 break;
382 case GAD_FIND_TEXT:
383 case GAD_FIND_OK:
384 GT_GetGadgetAttrs(findgad, findwin, 0, GTST_String, (IPTR)&sp,
385 TAG_DONE);
386 strcpy(searchtext, sp);
388 rc = TRUE;
389 break;
391 } /* switch (((struct Gadget *)msg->IAddress)->GadgetID) */
392 break;
394 case IDCMP_VANILLAKEY:
395 switch(ToUpper(msg->Code))
397 case 27:
398 killreq = TRUE;
399 break;
401 case 9:
402 case 'S':
403 case 'F':
404 ActivateGadget(findgad, findwin, 0);
405 break;
407 case 13:
408 case 'N':
409 rc = SEARCH_NEXT;
410 break;
412 case 'P':
413 rc = SEARCH_PREV;
414 break;
416 } /* switch(msg->Code) */
417 break;
419 } /* switch (msg->Class) */
420 GT_ReplyIMsg(msg);
422 } /* while ((msg = GT_GetIMsg(findwin))) */
424 if (killreq) Kill_Find_Requester();
426 if (rc) *text = searchtext;
428 return rc;
431 /****************************************************************************************/
433 void Kill_Find_Requester(void)
435 if (findwin)
437 CloseWindow(findwin);findwin = 0;findmask = 0;
440 if (findgadlist)
442 FreeGadgets(findgadlist);findgadlist = 0;
446 /****************************************************************************************/