Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / utilities / More / req.c
blob39ea02d8ce5a997bc5795f5e5822f2e1d0fa834a
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>
22 #include "global.h"
23 #include "req.h"
25 #define CATCOMP_NUMBERS
26 #include "strings.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
33 #define DEBUG 0
34 #include <aros/debug.h>
36 /****************************************************************************************/
39 #define BORDER_SPACING_X 4
40 #define BORDER_SPACING_Y 4
42 #define GAD_SPACING_X 8
43 #define GAD_SPACING_Y 4
45 #define GAD_EXTRA_WIDTH 16
46 #define GAD_EXTRA_HEIGHT 6
48 enum {GAD_FIND_TEXT = 1,
49 GAD_FIND_OK,
50 GAD_FIND_CANCEL};
52 enum {GAD_GOTO_STRING = 1,
53 GAD_GOTO_OK,
54 GAD_GOTO_CANCEL};
56 /****************************************************************************************/
59 static struct RastPort temprp;
60 static struct Window *gotowin, *findwin;
61 static struct Gadget *gotogad, *findgad, *gad, *gotogadlist, *findgadlist;
62 static struct NewGadget ng;
63 static WORD fontwidth, fontheight;
65 static char searchtext[256];
67 /****************************************************************************************/
69 static BOOL Init(void)
71 fontwidth = dri->dri_Font->tf_XSize;
72 fontheight = dri->dri_Font->tf_YSize;
74 InitRastPort(&temprp);
75 SetFont(&temprp, dri->dri_Font);
77 memset(&ng, 0, sizeof(ng));
78 ng.ng_VisualInfo = vi;
80 return TRUE;
83 /****************************************************************************************/
86 void CleanupRequesters(void)
88 if (gotowin) Kill_Goto_Requester();
89 if (findwin) Kill_Find_Requester();
92 /****************************************************************************************/
95 void Make_Goto_Requester(void)
97 WORD winwidth, winheight, gadwidth, gadheight;
98 WORD strwidth, w;
100 if (gotowin || !Init()) return;
102 gad = CreateContext(&gotogadlist);
104 gadheight = fontheight + GAD_EXTRA_HEIGHT;
106 winheight = scr->WBorTop + fontheight + 1 +
107 scr->WBorBottom +
108 BORDER_SPACING_Y * 2 +
109 gadheight * 2 +
110 GAD_SPACING_Y;
112 gadwidth = TextLength(&temprp, MSG(MSG_OK), strlen(MSG(MSG_OK)));
113 w = TextLength(&temprp, MSG(MSG_CANCEL), strlen(MSG(MSG_CANCEL)));
114 if (w > gadwidth) gadwidth = w;
116 gadwidth += GAD_EXTRA_WIDTH;
118 strwidth = gadwidth * 2 + GAD_SPACING_X;
120 winwidth = scr->WBorLeft +
121 scr->WBorRight +
122 BORDER_SPACING_X * 2 +
123 strwidth;
125 ng.ng_LeftEdge = scr->WBorLeft + BORDER_SPACING_X;
126 ng.ng_TopEdge = scr->WBorTop + fontheight + 1 + BORDER_SPACING_Y;
127 ng.ng_Width = strwidth;
128 ng.ng_Height = gadheight;
129 ng.ng_GadgetID = GAD_GOTO_STRING;
130 ng.ng_Flags = PLACETEXT_IN;
132 gotogad = CreateGadget(INTEGER_KIND, gad, &ng, GTIN_MaxChars, 8,
133 STRINGA_Justification, GACT_STRINGCENTER,
134 TAG_DONE);
136 ng.ng_TopEdge += gadheight + GAD_SPACING_Y;
137 ng.ng_Width = gadwidth;
138 ng.ng_GadgetText = MSG(MSG_OK);
139 ng.ng_GadgetID = GAD_GOTO_OK;
141 gad = CreateGadgetA(BUTTON_KIND, gotogad, &ng, 0);
143 ng.ng_LeftEdge += gadwidth + GAD_SPACING_X;
144 ng.ng_GadgetText = MSG(MSG_CANCEL);
145 ng.ng_GadgetID = GAD_GOTO_CANCEL;
147 gad = CreateGadgetA(BUTTON_KIND, gad, &ng, 0);
149 if (!gad)
151 FreeGadgets(gotogadlist);
152 gotogadlist = 0;
153 } else {
154 gotowin = OpenWindowTags(0, WA_CustomScreen, (IPTR)scr,
155 WA_Left, scr->MouseX - (winwidth / 2),
156 WA_Top, scr->MouseY - (winheight / 2),
157 WA_Width, winwidth,
158 WA_Height, winheight,
159 WA_AutoAdjust, TRUE,
160 WA_Title, (IPTR)MSG(MSG_JUMP_TITLE),
161 WA_CloseGadget, TRUE,
162 WA_DepthGadget, TRUE,
163 WA_DragBar, TRUE,
164 WA_Activate, TRUE,
165 WA_SimpleRefresh, TRUE,
166 WA_IDCMP, IDCMP_CLOSEWINDOW |
167 IDCMP_REFRESHWINDOW |
168 IDCMP_VANILLAKEY |
169 BUTTONIDCMP |
170 INTEGERIDCMP,
171 WA_Gadgets, (IPTR)gotogadlist,
172 TAG_DONE);
174 if (!gotowin)
176 FreeGadgets(gotogadlist);gotogadlist = 0;
177 } else {
178 gotomask = 1L << gotowin->UserPort->mp_SigBit;
179 GT_RefreshWindow(gotowin, 0);
180 ActivateGadget(gotogad, gotowin, 0);
185 /****************************************************************************************/
187 BOOL Handle_Goto_Requester(LONG *line)
189 struct IntuiMessage *msg;
190 IPTR l;
191 BOOL killreq = FALSE, rc = FALSE;
193 while ((msg = GT_GetIMsg(gotowin->UserPort)))
195 switch (msg->Class)
197 case IDCMP_REFRESHWINDOW:
198 GT_BeginRefresh(gotowin);
199 GT_EndRefresh(gotowin, TRUE);
200 break;
202 case IDCMP_CLOSEWINDOW:
203 killreq = TRUE;
204 break;
206 case IDCMP_GADGETUP:
207 switch (((struct Gadget *)msg->IAddress)->GadgetID)
209 case GAD_GOTO_CANCEL:
210 killreq = TRUE;
211 break;
213 case GAD_GOTO_STRING:
214 case GAD_GOTO_OK:
215 GT_GetGadgetAttrs(gotogad, gotowin, 0, GTIN_Number, (IPTR)&l,
216 TAG_DONE);
217 rc = TRUE;
218 break;
220 } /* switch (((struct Gadget *)msg->IAddress)->GadgetID) */
221 break;
223 case IDCMP_VANILLAKEY:
224 switch(toupper(msg->Code))
226 case 27:
227 killreq = TRUE;
228 break;
230 case 9:
231 case 'G':
232 ActivateGadget(gotogad, gotowin, 0);
233 break;
235 } /* switch(msg->Code) */
236 break;
238 } /* switch (msg->Class) */
239 GT_ReplyIMsg(msg);
241 } /* while ((msg = GT_GetIMsg(gotowin))) */
243 if (killreq) Kill_Goto_Requester();
245 if (rc) *line = l;
247 return rc;
250 /****************************************************************************************/
252 void Kill_Goto_Requester(void)
254 if (gotowin)
256 CloseWindow(gotowin);gotowin = 0;gotomask = 0;
259 if (gotogadlist)
261 FreeGadgets(gotogadlist);gotogadlist = 0;
265 /****************************************************************************************/
267 void Make_Find_Requester(void)
269 WORD winwidth, winheight, gadwidth, gadheight;
270 WORD strwidth, w;
272 if (findwin || !Init()) return;
274 gad = CreateContext(&findgadlist);
276 gadheight = fontheight + GAD_EXTRA_HEIGHT;
278 winheight = scr->WBorTop + fontheight + 1 +
279 scr->WBorBottom +
280 BORDER_SPACING_Y * 2 +
281 gadheight * 2 +
282 GAD_SPACING_Y;
284 gadwidth = TextLength(&temprp, MSG(MSG_OK), strlen(MSG(MSG_OK)));
285 w = TextLength(&temprp, MSG(MSG_CANCEL), strlen(MSG(MSG_CANCEL)));
286 if (w > gadwidth) gadwidth = w;
288 gadwidth += GAD_EXTRA_WIDTH;
290 strwidth = gadwidth * 2 + GAD_SPACING_X;
292 if (strwidth < 250) strwidth = 250;
294 winwidth = scr->WBorLeft +
295 scr->WBorRight +
296 BORDER_SPACING_X * 2 +
297 strwidth;
299 ng.ng_LeftEdge = scr->WBorLeft + BORDER_SPACING_X;
300 ng.ng_TopEdge = scr->WBorTop + fontheight + 1 + BORDER_SPACING_Y;
301 ng.ng_Width = strwidth;
302 ng.ng_Height = gadheight;
303 ng.ng_GadgetID = GAD_FIND_TEXT;
304 ng.ng_Flags = PLACETEXT_IN;
306 findgad = CreateGadget(STRING_KIND, gad, &ng, GTST_MaxChars, 256,
307 TAG_DONE);
309 ng.ng_TopEdge += gadheight + GAD_SPACING_Y;
310 ng.ng_Width = gadwidth;
311 ng.ng_GadgetText = MSG(MSG_OK);
312 ng.ng_GadgetID = GAD_FIND_OK;
314 gad = CreateGadgetA(BUTTON_KIND, findgad, &ng, 0);
316 ng.ng_LeftEdge = winwidth - scr->WBorRight - BORDER_SPACING_X - gadwidth;
317 ng.ng_GadgetText = MSG(MSG_CANCEL);
318 ng.ng_GadgetID = GAD_FIND_CANCEL;
320 gad = CreateGadgetA(BUTTON_KIND, gad, &ng, 0);
322 if (!gad)
324 FreeGadgets(findgadlist);
325 findgadlist = 0;
326 } else {
327 findwin = OpenWindowTags(0, WA_CustomScreen, (IPTR)scr,
328 WA_Left, scr->MouseX - (winwidth / 2),
329 WA_Top, scr->MouseY - (winheight / 2),
330 WA_Width, winwidth,
331 WA_Height, winheight,
332 WA_AutoAdjust, TRUE,
333 WA_Title, (IPTR)MSG(MSG_FIND_TITLE),
334 WA_CloseGadget, TRUE,
335 WA_DepthGadget, TRUE,
336 WA_DragBar, TRUE,
337 WA_Activate, TRUE,
338 WA_SimpleRefresh, TRUE,
339 WA_IDCMP, IDCMP_CLOSEWINDOW |
340 IDCMP_REFRESHWINDOW |
341 IDCMP_VANILLAKEY |
342 BUTTONIDCMP |
343 INTEGERIDCMP,
344 WA_Gadgets, (IPTR)findgadlist,
345 TAG_DONE);
347 if (!findwin)
349 FreeGadgets(findgadlist);findgadlist = 0;
350 } else {
351 findmask = 1L << findwin->UserPort->mp_SigBit;
352 GT_RefreshWindow(findwin, 0);
353 ActivateGadget(findgad, findwin, 0);
358 /****************************************************************************************/
360 WORD Handle_Find_Requester(char **text)
362 struct IntuiMessage *msg;
363 char *sp;
364 BOOL killreq = FALSE, rc = 0;
366 while ((msg = GT_GetIMsg(findwin->UserPort)))
368 switch (msg->Class)
370 case IDCMP_REFRESHWINDOW:
371 GT_BeginRefresh(findwin);
372 GT_EndRefresh(findwin, TRUE);
373 break;
375 case IDCMP_CLOSEWINDOW:
376 killreq = TRUE;
377 break;
379 case IDCMP_GADGETUP:
380 switch (((struct Gadget *)msg->IAddress)->GadgetID)
382 case GAD_FIND_CANCEL:
383 killreq = TRUE;
384 break;
386 case GAD_FIND_TEXT:
387 case GAD_FIND_OK:
388 GT_GetGadgetAttrs(findgad, findwin, 0, GTST_String, (IPTR)&sp,
389 TAG_DONE);
390 strcpy(searchtext, sp);
392 rc = TRUE;
393 break;
395 } /* switch (((struct Gadget *)msg->IAddress)->GadgetID) */
396 break;
398 case IDCMP_VANILLAKEY:
399 switch(toupper(msg->Code))
401 case 27:
402 killreq = TRUE;
403 break;
405 case 9:
406 case 'S':
407 case 'F':
408 ActivateGadget(findgad, findwin, 0);
409 break;
411 case 13:
412 case 'N':
413 rc = SEARCH_NEXT;
414 break;
416 case 'P':
417 rc = SEARCH_PREV;
418 break;
420 } /* switch(msg->Code) */
421 break;
423 } /* switch (msg->Class) */
424 GT_ReplyIMsg(msg);
426 } /* while ((msg = GT_GetIMsg(findwin))) */
428 if (killreq) Kill_Find_Requester();
430 if (rc) *text = searchtext;
432 return rc;
435 /****************************************************************************************/
437 void Kill_Find_Requester(void)
439 if (findwin)
441 CloseWindow(findwin);findwin = 0;findmask = 0;
444 if (findgadlist)
446 FreeGadgets(findgadlist);findgadlist = 0;
450 /****************************************************************************************/