move to the contrib location where the datatype resides
[AROS-Contrib.git] / fish / memomaster / deledadd.c
blobbeaf14f8e9e9efd8b2e2bea8e390c9e3dd91b9dc
1 #include <aros/oldprograms.h>
3 #include <intuition/intuition.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <exec/nodes.h>
9 #include <exec/lists.h>
10 #include <exec/memory.h>
12 #include "mm2.h"
14 extern int Month2Num(char *);
15 extern int DisplayC(char **);
16 extern void DisplayE(char **);
17 extern void DisplayH(char **);
18 extern BOOL CheckMemoDate(char *);
20 extern struct MinList *MemListPtr;
21 extern struct MinNode *DisplayFirst;
22 extern struct Remember *RK;
23 extern int Colour[4];
25 /*--------------------------------------------------------------------------
26 * Prototypes for functions in this module
29 BOOL Edit(void);
30 BOOL Add(void);
31 BOOL Del(void);
32 BOOL MemoReq(struct Memo_Item *);
33 BOOL MemIns(struct MI_Mem *);
34 BOOL MemoDel(struct MI_Mem *);
36 BOOL DataAmended = FALSE;
37 extern struct IntuitionBase *IntuitionBase;
38 extern void Display_Block(struct MinNode *);
39 BOOL MemoReq(struct Memo_Item *);
41 static struct IntuiText memo_string_text=
43 1, 0, JAM1, 0, -10, NULL, "Memo:", NULL
45 static struct IntuiText date_string_text=
47 1, 0, JAM1, -50, 0, NULL, "Date:", NULL
49 static struct IntuiText notice_string_text=
51 1, 0, JAM1, -80, 0, NULL, "Notice:", NULL
53 static struct IntuiText type_string_text=
55 1, 0, JAM1, -50, 0, NULL, "Type:", NULL
57 static struct IntuiText ok_text=
59 1, 0, JAM1, 4, 2, NULL, " OK ", NULL
61 static struct IntuiText help_text=
63 1, 0, JAM1, 4, 2, NULL, " HELP ", NULL
65 static struct IntuiText cancel_text=
67 1, 0, JAM1, 4, 2, NULL, "CANCEL", NULL
69 static struct IntuiText requester_text=
71 1, 0, JAM1, 14, 3, NULL, "Please enter your memo:", NULL
74 static UBYTE memo_buffer[61];
75 static UBYTE undo_buffer[61];
76 static UBYTE date_buffer[10];
77 static UBYTE notice_buffer[3];
78 static UBYTE type_buffer[2];
80 static struct StringInfo memo_string_info=
82 memo_buffer, undo_buffer, 0, 61,
84 static struct StringInfo date_string_info=
86 date_buffer, undo_buffer, 0, 10,
88 static struct StringInfo notice_string_info=
90 notice_buffer, undo_buffer, 0, 3,
92 static struct StringInfo type_string_info=
94 type_buffer, undo_buffer, 0, 2,
97 /*----------------------------------------------------------------------
98 * data for string gadgets etc
101 static SHORT type_bpts[]=
103 -2, -1,
104 DEA_TYPE_W-2, -1,
105 DEA_TYPE_W-2, DEA_SGAD_H,
106 -2, DEA_SGAD_H,
107 -2, -1
110 static SHORT memo_bpts[]=
112 -2, -1,
113 DEA_MEMO_W-2, -1,
114 DEA_MEMO_W-2, DEA_SGAD_H,
115 -2, DEA_SGAD_H,
116 -2, -1
119 static SHORT date_bpts[]=
121 -2, -1,
122 DEA_DATE_W-2, -1,
123 DEA_DATE_W-2, DEA_SGAD_H,
124 -2, DEA_SGAD_H,
125 -2, -1
128 static SHORT noti_bpts[]=
130 -2, -1,
131 DEA_NOTI_W-3, -1,
132 DEA_NOTI_W-3, DEA_SGAD_H,
133 -2, DEA_SGAD_H,
134 -2, -1
137 static struct Border type_brd=
139 0, 0, 0, 0, JAM1, 5, type_bpts, NULL
142 static struct Border memo_brd=
144 0, 0, 0, 0, JAM1, 5, memo_bpts, NULL
147 static struct Border date_brd=
149 0, 0, 0, 0, JAM1, 5, date_bpts, NULL
152 static struct Border noti_brd=
154 0, 0, 0, 0, JAM1, 5, noti_bpts, NULL
157 static struct Gadget memo_gadget=
159 NULL, 20, 32, DEA_MEMO_W, DEA_SGAD_H, GADGHCOMP, RELVERIFY,
160 STRGADGET|REQGADGET,(APTR)&memo_brd,NULL,&memo_string_text, 0,
161 (APTR) &memo_string_info,0,NULL
164 static struct Gadget date_gadget=
166 &memo_gadget,100, 14, DEA_DATE_W, DEA_SGAD_H, GADGHCOMP, RELVERIFY,
167 STRGADGET|REQGADGET, (APTR)&date_brd, NULL, &date_string_text, 0,
168 (APTR) &date_string_info, 0, NULL
170 static struct Gadget notice_gadget=
172 &date_gadget,300, 14, DEA_NOTI_W, DEA_SGAD_H, GADGHCOMP, RELVERIFY,
173 STRGADGET|REQGADGET, (APTR)&noti_brd, NULL, &notice_string_text, 0,
174 (APTR) &notice_string_info, 0, NULL
176 static struct Gadget type_gadget=
178 &notice_gadget,400, 14, DEA_TYPE_W, DEA_SGAD_H, GADGHCOMP, RELVERIFY,
179 STRGADGET|REQGADGET, (APTR)&type_brd, NULL, &type_string_text, 0,
180 (APTR) &type_string_info, 0, NULL
183 /*---------------------------------------------------------------------
184 * data for BOOLEAN gadgets
186 static SHORT bb_pts_tr[]= /*Top left points*/
188 0, 10,
189 0, 0,
190 54, 0
193 static SHORT bb_pts_bl[]= /*Bottom right points*/
195 54, 0,
196 54, 10,
197 0, 10
200 static struct Border bb0_tr=
202 0, 0, 0, 0, JAM1, 3, bb_pts_tr, NULL
205 static struct Border bb0_bl=
207 0, 0, 0, 0, JAM1, 3, bb_pts_bl, &bb0_tr
210 static struct Border bb1_tr=
212 0, 0, 0, 0, JAM1, 3, bb_pts_tr, NULL
215 static struct Border bb1_bl=
217 0, 0, 0, 0, JAM1, 3, bb_pts_bl, &bb1_tr
220 static struct Gadget ok_gadget=
222 &type_gadget, /* NextGadget */
223 14, /* LeftEdge */
224 47, /* TopEdge */
225 DEA_BGAD_W, /* Width */
226 DEA_BGAD_H,
227 GADGHIMAGE, /* Flags */
228 RELVERIFY| /* Activation */
229 ENDGADGET,
230 BOOLGADGET| /* GadgetType */
231 REQGADGET,
232 (APTR) &bb0_bl, /* GadgetRender */
233 (APTR) &bb1_bl, /* SelectRender */
234 &ok_text, /* GadgetText */
237 static struct Gadget help_gadget=
239 &ok_gadget,314, 47, DEA_BGAD_W, DEA_BGAD_H, GADGHIMAGE, RELVERIFY,
240 BOOLGADGET|REQGADGET, (APTR)&bb0_bl, (APTR)&bb1_bl, &help_text,
243 static struct Gadget cancel_gadget=
245 &help_gadget,514, 47, DEA_BGAD_W, DEA_BGAD_H, GADGHIMAGE, RELVERIFY|ENDGADGET,
246 BOOLGADGET|REQGADGET, (APTR)&bb0_bl, (APTR)&bb1_bl, &cancel_text,
249 static SHORT requester_border_points[]=
251 0, 0,
252 639, 0,
253 639, 64,
254 0, 64,
255 0, 0
258 static struct Border requester_border=
260 0, 0, /* LeftEdge, TopEdge. */
261 1, /* FrontPen */
262 0, /* BackPen */
263 JAM1, /* DrawMode */
264 5, /* Count */
265 requester_border_points, /* XY */
266 NULL, /* NextBorder */
270 static struct Requester my_requester=
272 NULL, /* OlderRequester */
273 0, 10, /* LeftEdge, TopEdge */
274 640, 65, /* Width, Height */
275 0, 0, /* RelLeft, RelTop */
276 &cancel_gadget, /* ReqGadget */
277 &requester_border, /* ReqBorder */
278 &requester_text, /* ReqText */
281 static struct Window *my_window;
283 static struct NewWindow my_new_window=
285 0, /* LeftEdge */
286 0, /* TopEdge */
287 640, /* Width */
288 75, /* Height */
289 0, /* DetailPen */
290 0, /* BlockPen */
291 GADGETUP| /* IDCMPFlags */
292 REQCLEAR,
293 SMART_REFRESH| /* Flags */
294 WINDOWDRAG|
295 WINDOWDEPTH|
296 ACTIVATE,
297 NULL, /* FirstGadget */
298 NULL, /* CheckMark */
299 "Amigaholics!!!", /* Title */
300 NULL, /* Screen */
301 NULL, /* BitMap */
302 0, /* MinWidth */
303 0, /* MinHeight */
304 0, /* MaxWidth */
305 0, /* MaxHeight */
306 WBENCHSCREEN /* Type */
309 static char *MAdd_Help[]={
311 " Date format is \"dd-mmm-yy\" ",
312 " eg \"21-JAN-91\" ",
313 " Notice is number, 1 to 99. The ",
314 " number of days notice you want",
315 " Type is A (or a) for Annual ",
316 " anything else is up to you ",
318 '\0'
320 static char *err_txt_reqfail[]={
322 " Failed to open ADD requester ",
323 '\0'
325 #if 0
326 static char *err_txt_fopenfail[]={
328 " Failed to open memo file ",
329 '\0'
331 #endif
332 static char *err_txt_dateinv2[]={
334 " Couldn\'t write since date was invalid ",
335 '\0'
337 /*--------------------------------------------------------------------------
338 * Function Edit() calls MemoReq() for each selected memo in the list. Prior
339 * to the call, Edit() sets up the current field values in a Memo_Item
340 * structure. Returns TRUE if no errors found, otherwise FALSE. (Actually,
341 * it currently returns TRUE under all circumstances.
343 BOOL Edit()
345 struct Memo_Item m;
346 struct MinNode *n;
347 BOOL result;
350 if (LISTEMPTY) return TRUE;
351 n=MemListPtr->mlh_Head;
352 while (n->mln_Succ)
354 if ( ((struct MI_Mem *)n)->mim_Select == 1)
356 strcpy(m.mi_Date , ((struct MI_Mem *)n)->mim_MI.mi_Date);
357 strcpy(m.mi_Notice , ((struct MI_Mem *)n)->mim_MI.mi_Notice);
358 strcpy(m.mi_Type , ((struct MI_Mem *)n)->mim_MI.mi_Type);
359 strcpy(m.mi_Text , ((struct MI_Mem *)n)->mim_MI.mi_Text);
360 result = MemoReq(&m);
361 if (result)
363 strcpy( ((struct MI_Mem *)n)->mim_MI.mi_Date, m.mi_Date);
364 strcpy( ((struct MI_Mem *)n)->mim_MI.mi_Notice, m.mi_Notice);
365 strcpy( ((struct MI_Mem *)n)->mim_MI.mi_Type, m.mi_Type);
366 strcpy( ((struct MI_Mem *)n)->mim_MI.mi_Text, m.mi_Text);
367 Remove((struct Node *)n);
368 MemIns((struct MI_Mem *)n);
369 DataAmended=TRUE;
372 ((struct MI_Mem *)n)->mim_Select=0;
373 n=n->mln_Succ;
375 Display_Block(DisplayFirst);
376 return TRUE;
379 /*--------------------------------------------------------------------------
380 * Function Add() calls MemoReq() to get data for new memo.
381 * Returns TRUE if no errors found, otherwise FALSE. (Actually,
382 * it currently returns TRUE under all circumstances.
384 BOOL Add()
386 struct Memo_Item m;
387 struct MI_Mem *mim;
388 BOOL result;
390 m.mi_Date[0]='\0';
391 m.mi_Notice[0]='\0';
392 m.mi_Type[0]='\0';
393 m.mi_Text[0]='\0';
394 result=MemoReq(&m);
395 if (result)
397 mim=(struct MI_Mem *)AllocRemember(&RK,sizeof(struct MI_Mem),MEMF_PUBLIC);
398 if (!mim) return FALSE;
399 strcpy( mim->mim_MI.mi_Date, m.mi_Date);
400 strcpy( mim->mim_MI.mi_Notice, m.mi_Notice);
401 strcpy( mim->mim_MI.mi_Type, m.mi_Type);
402 strcpy( mim->mim_MI.mi_Text, m.mi_Text);
403 mim->mim_Select=0;
404 result=MemIns(mim);
405 if (!result) return FALSE;
406 Display_Block(DisplayFirst);
407 DataAmended=TRUE;
409 return (TRUE);
412 /*--------------------------------------------------------------------------
413 * Function Del() reads through Memo list and, with the confirmation of the
414 * user, deletes selected memos. The memos are actually just removed from
415 * the list, the memory is not released until the program terminates. (Later
416 * deleted memos could be organised as a separate list and a process made
417 * available for the user to change his/her mind and reinsert any memos?)
418 * Returns TRUE if no errors found, otherwise FALSE. (Actually,
419 * it currently returns TRUE under all circumstances.
421 BOOL Del()
423 struct MinNode *n;
424 int result;
427 if (LISTEMPTY) return TRUE;
428 n=MemListPtr->mlh_Head;
429 while (n->mln_Succ)
431 if ( ((struct MI_Mem *)n)->mim_Select == 1)
433 result = MemoDel((struct MI_Mem *)n);
434 if (result == 1)
436 Remove((struct Node *)n);
437 DataAmended=TRUE;
438 if (( n = DisplayFirst))
440 if (LISTEMPTY)
441 DisplayFirst = NULL;
442 else
443 DisplayFirst = MemListPtr->mlh_Head;
445 Display_Block(DisplayFirst);
448 n=n->mln_Succ;
450 return TRUE;
453 /*--------------------------------------------------------------------------
454 * Function MemIns() allocates a new MI_Mem structure in memory, copies
455 * data into it from a Memo_Item structure whose address was given as a
456 * parameter and then returns TRUE if no errors found, otherwise FALSE.
458 BOOL MemIns(struct MI_Mem *m)
460 char d1[5],d2[5];
461 struct MinNode *n;
462 BOOL found;
463 int cmp,mnum;
465 if (LISTEMPTY)
467 AddHead((struct List *)MemListPtr,(struct Node *)m);
469 else
471 mnum = Month2Num((char *)&(m->mim_MI.mi_Date[3]));
472 sprintf(d1, "%02d", mnum);
473 d1[2]=m->mim_MI.mi_Date[0];
474 d1[3]=m->mim_MI.mi_Date[1];
475 d1[4]='\0';
476 n=MemListPtr->mlh_Head;
477 found=FALSE;
478 while ( (!found) && (n->mln_Succ) )
480 mnum = Month2Num((char *)&(((struct MI_Mem *)n)->mim_MI.mi_Date[3]));
481 sprintf(d2, "%02d", mnum);
482 d2[2]=((struct MI_Mem *)n)->mim_MI.mi_Date[0];
483 d2[3]=((struct MI_Mem *)n)->mim_MI.mi_Date[1];
484 d2[4]='\0';
485 cmp=strcmp(d1,d2);
486 if ( cmp > 0 )
488 n=n->mln_Succ;
490 else
492 Insert( (struct List *)MemListPtr,(struct Node *)m,(struct Node *)n->mln_Pred);
493 found=TRUE;
496 if (!found) AddTail( (struct List *)MemListPtr, (struct Node *)m );
498 return TRUE;
502 /* -------------------------------------------------------------------
503 * Function handling Requester allowing addition of new memos and
504 * editing of existing ones
506 BOOL MemoReq(struct Memo_Item *mim)
508 BOOL close_me;
509 ULONG class;
510 APTR address;
511 struct IntuiMessage *my_message;
512 BOOL result;
513 int MemoState,DateState;
514 char *err_txt_dateinv1[] = {
516 " Invalid date - try again ",
517 '\0'
520 my_new_window.DetailPen = Colour[0];
521 my_new_window.BlockPen = Colour[1];
522 requester_border.FrontPen = Colour[2];
523 requester_border.BackPen = Colour[0];
524 type_brd.FrontPen = Colour[2];
525 type_brd.BackPen = Colour[0];
526 memo_brd.FrontPen = Colour[2];
527 memo_brd.BackPen = Colour[0];
528 date_brd.FrontPen = Colour[2];
529 date_brd.BackPen = Colour[0];
530 noti_brd.FrontPen = Colour[2];
531 noti_brd.BackPen = Colour[0];
532 bb0_bl.FrontPen = Colour[2];
533 bb0_bl.BackPen = Colour[1];
534 bb0_tr.FrontPen = Colour[1];
535 bb0_tr.BackPen = Colour[2];
536 bb1_bl.FrontPen = Colour[1];
537 bb1_bl.BackPen = Colour[2];
538 bb1_tr.FrontPen = Colour[2];
539 bb1_tr.BackPen = Colour[1];
540 my_requester.BackFill = Colour[0];
541 strcpy(date_buffer,mim->mi_Date);
542 strcpy(memo_buffer,mim->mi_Text);
543 strcpy(notice_buffer,mim->mi_Notice);
544 strcpy(type_buffer,mim->mi_Type);
545 my_window = (struct Window *) OpenWindow( &my_new_window );
546 if(my_window == NULL)
548 CloseLibrary( (struct Library *)IntuitionBase );
549 exit(0);
552 result=Request( &my_requester, my_window );
553 if( !result )
555 DisplayE(err_txt_reqfail);
558 close_me = FALSE;
559 MemoState=0;
560 DateState=0;
561 ActivateGadget(&date_gadget,
562 my_window,
563 &my_requester);
564 while( !close_me )
566 Wait( 1 << my_window->UserPort->mp_SigBit );
567 while((my_message=(struct IntuiMessage *) GetMsg(my_window->UserPort)))
569 class = my_message->Class;
570 address = my_message->IAddress;
571 ReplyMsg( (struct Message *)my_message );
572 switch( class )
574 case GADGETUP:
575 if(address == (APTR) &ok_gadget )
577 MemoState=1;
579 if(address == (APTR) &help_gadget )
581 DisplayH(MAdd_Help);
582 ActivateGadget(&date_gadget,
583 my_window,
584 &my_requester);
586 if(address == (APTR) &cancel_gadget )
588 MemoState=0;
590 if(address == (APTR) &date_gadget )
592 if(CheckMemoDate(date_buffer) == TRUE )
594 DateState=1;
595 ActivateGadget(&notice_gadget,
596 my_window,
597 &my_requester);
599 else
601 DateState=0;
602 DisplayE(err_txt_dateinv1);
603 ActivateGadget(&date_gadget,
604 my_window,
605 &my_requester);
608 if(address == (APTR) &memo_gadget )
610 ActivateGadget(&date_gadget,
611 my_window,
612 &my_requester);
615 if(address == (APTR) &notice_gadget )
617 ActivateGadget(&type_gadget,
618 my_window,
619 &my_requester);
622 if(address == (APTR) &type_gadget )
625 ActivateGadget(&memo_gadget,
626 my_window,
627 &my_requester);
631 break;
633 case REQCLEAR:
634 close_me=TRUE;
639 if(CheckMemoDate(date_buffer) == TRUE )
640 DateState=1;
641 else
642 DateState=0;
643 CloseWindow( my_window );
644 if (MemoState==1)
646 if(DateState==1)
648 strcpy(mim->mi_Date,date_buffer);
649 strcpy(mim->mi_Notice,notice_buffer);
650 strcpy(mim->mi_Type,type_buffer);
651 strcpy(mim->mi_Text,memo_buffer);
652 return TRUE;
654 else
656 DisplayE(err_txt_dateinv2);
657 return FALSE;
660 else
662 return FALSE;
667 /*======================================================================*/
668 BOOL MemoDel(struct MI_Mem *memo)
670 char *Query[] = {
672 " Do you want to delete this memo? ",
674 "", /* Date */
675 "", /* Type */
676 "", /* Memo Text */
678 '\0' };
679 char dbuff[18]; /* "Date : dd-mmm-yy" */
680 char tbuff[10]; /* "Type : X" */
682 sprintf(tbuff, "Type : %s", memo->mim_MI.mi_Type);
683 sprintf(dbuff, "Date : %s", memo->mim_MI.mi_Date);
684 Query[3]=dbuff;
685 Query[4]=tbuff;
686 Query[5]=memo->mim_MI.mi_Text;
687 return DisplayC(Query);