home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / game / think / chaos / src / projectami.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  10.7 KB  |  433 lines

  1. /*  Chaos:            The Chess HAppening Organisation System    V5.2
  2.     Copyright (C)   1993    Jochen Wiedmann
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     $RCSfile: ProjectAmi.c,v $
  20.     $Revision: 2.3 $
  21.     $Date: 1993/12/12 13:22:15 $
  22.  
  23.     This file contains the system dependent functions that support the
  24.     Project menu.
  25.  
  26.     Computer:    Amiga 1200            Compiler:    Dice 2.07.54 (3.0)
  27.  
  28.     Author:    Jochen Wiedmann
  29.         Am Eisteich 9
  30.       72555 Metzingen
  31.         Tel. 07123 / 14881
  32.         Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  33. */
  34.  
  35.  
  36. #ifndef CHAOS_H
  37. #include "chaos.h"
  38. #endif
  39.  
  40. #ifdef AMIGA
  41. #ifndef LIBRARIES_ASL_H
  42. #include <libraries/asl.h>
  43. #endif
  44. #ifndef LIBRARIES_GADTOOLS_H
  45. #include <libraries/gadtools.h>
  46. #endif
  47. #ifndef DOS_DOS_H
  48. #include <libraries/dos.h>
  49. #endif
  50. #ifndef CLIB_ASL_PROTOS_H
  51. #include <clib/asl_protos.h>
  52. #endif
  53. #ifndef CLIB_ICON_PROTOS_H
  54. #include <clib/icon_protos.h>
  55. #endif
  56. #ifdef AZTEC_C
  57. #ifndef PRAGMAS_ASL_LIB_H
  58. #include <pragmas/asl_lib.h>
  59. #endif
  60. #ifndef PRAGMAS_ICON_LIB_H
  61. #include <pragmas/icon_lib.h>
  62. #endif
  63. #define strrchr rindex
  64. #endif    /*  AZTEC_C */
  65. #endif    /*  AMIGA   */
  66.  
  67.  
  68.  
  69.  
  70. /*
  71.     FileRequest() creates a file requester which reads a file name.
  72.  
  73.     Inputs: defaultfile a pointer to a string containing the default name
  74.         title    a pointer to a string containing the requester's
  75.             title. This may be NULL, in which case
  76.             MSG_CDAT_SELECTION is assumed.
  77.         ending    a pointer to a string containing the default ending.
  78.             This may be NULL, in which case "#?.cdat" is assumed.
  79.             Note, that this MUST be something like "#?.xxx" on
  80.             the Amiga!
  81.         savemode    TRUE, if non-existing files may be selected.
  82.  
  83.     Result: Full path name of the file, that was selected or NULL, if the
  84.         user cancelled.
  85. */
  86. char *FileRequest(char *defaultfile, char *title, char *ending, int savemode)
  87.  
  88. #ifdef AMIGA
  89. { struct FileRequester *requester;
  90.   char pattern[20];
  91.   char *result = NULL;
  92.   char *filename, *pathname, *endptr;
  93.   BPTR dir;
  94.   static char FileRequestName[TRNFILENAME_LEN+1];
  95.   static char PathName[TRNFILENAME_LEN+1];
  96.   struct Window *window;
  97.  
  98.  
  99.   /*
  100.       Bring up default settings, if needed.
  101.   */
  102.   if (title == NULL)
  103.   { title = (char *) GetChaosString(MSG_CDAT_SELECTION);
  104.   }
  105.   if (ending == NULL)
  106.   { ending = "#?.cdat";
  107.   }
  108.  
  109.  
  110.   /*
  111.       Get Intuition window pointer from MUI window, allocate Filerequester
  112.       and parse the ending for wildcards.
  113.   */
  114.   get(MainWnd, MUIA_Window_Window, &window);
  115.   if ((requester = (struct FileRequester *)
  116.            MUI_AllocAslRequest(ASL_FileRequest, NULL))  ==  NULL)
  117.   { MemError();
  118.     return(NULL);
  119.   }
  120.   ParsePatternNoCase((UBYTE *) ending, (UBYTE *) pattern, sizeof(pattern));
  121.  
  122.  
  123.   /*
  124.       Get default file- and drawername.
  125.   */
  126.   if (defaultfile  &&  *defaultfile != '\0')
  127.   { strcpy(FileRequestName, defaultfile);
  128.   }
  129.   else
  130.   { if (TrnFileName != '\0')
  131.     { strcpy(FileRequestName, TrnFileName);
  132.     }
  133.     else
  134.     { sprintf(FileRequestName, savemode ? "chaos.%d.cdat" : "", NumRounds);
  135.     }
  136.   }
  137.   filename = (char *) FilePart((STRPTR) FileRequestName);
  138.   strcpy(PathName, FileRequestName);
  139.   if ((pathname = (char *) PathPart((STRPTR) PathName)) != PathName)
  140.   { *pathname = '\0';
  141.   }
  142.  
  143.  
  144.   /*
  145.       Make the drawername absolute.
  146.   */
  147.   dir = Lock((STRPTR) PathName, SHARED_LOCK);
  148.   NameFromLock(dir, (STRPTR) PathName, sizeof(PathName));
  149.   UnLock(dir);
  150.  
  151.   /*
  152.      Ensure, that the default filename has the right ending.
  153.   */
  154.   if (ending != NULL  &&  (endptr = strrchr(filename, '.')) != NULL)
  155.   { strcpy(endptr, ending+2);
  156.   }
  157.  
  158.  
  159.   /*
  160.       Bring up the requester
  161.   */
  162. #ifdef V39_INCLUDES
  163.   if (MUI_AslRequestTags(requester,
  164.              ASLFR_Window, window,
  165.              ASLFR_PrivateIDCMP, TRUE,
  166.              ASLFR_SleepWindow, TRUE,
  167.              ASLFR_TitleText, title,
  168.              ASLFR_InitialFile, filename,
  169.              ASLFR_InitialDrawer, PathName,
  170.              ASLFR_InitialPattern, ending,
  171.              ASLFR_DoSaveMode, savemode,
  172.              ASLFR_RejectIcons, TRUE,
  173.              ASLFR_AcceptPattern, pattern,
  174.              TAG_DONE)  !=    FALSE    &&
  175.       requester->fr_File != NULL  &&  requester->fr_File != '\0')
  176.   { strcpy(FileRequestName, (char *) requester->fr_Drawer);
  177.     AddPart((STRPTR) FileRequestName, requester->fr_File,
  178.         sizeof(FileRequestName));
  179.     result = FileRequestName;
  180.   }
  181. #else
  182.   if (MUI_AslRequestTags(requester,
  183.              ASL_Window, window,
  184.              ASL_Hail, title
  185.              ASL_File, filename,
  186.              ASL_Dir, PathName,
  187.              TAG_DONE)  ==    FALSE    &&
  188.       requester->rf_File != NULL  &&  requester->rf_File != '\0')
  189.   { strcpy(FileRequestName, (char *) requester->rf_Dir);
  190.     AddPart((STRPTR) FileRequestName, (STRPTR) requester->rf_File,
  191.         sizeof(FileRequestName));
  192.     result = FileRequestName;
  193.   }
  194. #endif
  195.   MUI_FreeAslRequest((APTR) requester);
  196.   return (result);
  197. }
  198. #endif    /*  AMIGA   */
  199.  
  200.  
  201.  
  202.  
  203. /*
  204.     CreateIcon() puts an icon to a recently saved file.
  205.  
  206.     Inputs: name of the file just created; must not be NULL
  207. */
  208. void CreateIcon(char *name)
  209.  
  210. #ifdef AMIGA
  211. { extern int MakeIcons;
  212.  
  213.   /*
  214.       Does the user want to have an icon?
  215.   */
  216.   if (MakeIcons)
  217.   { /*
  218.     Yes, get a diskobject
  219.     */
  220.     struct DiskObject *dobj;
  221.     char *olddeftool;
  222.     int len = strlen(IconName);
  223.  
  224.     /*
  225.     Icon.library doesn't like a trailing ".info" when calling
  226.     GetDiskObject().
  227.     */
  228.     if (len >= 5  &&
  229.     Stricmp((STRPTR) IconName+len-5, (STRPTR) ".info") == 0)
  230.     { IconName[len-5] = '\0';
  231.     }
  232.  
  233.     if ((dobj = GetDiskObject((STRPTR) IconName))  !=  NULL  ||
  234.     (dobj = GetDiskObject((STRPTR) "s:Chaos_Project"))  !=  NULL  ||
  235.     (dobj = GetDefDiskObject(WBPROJECT))  !=  NULL)
  236.     { /*
  237.       Put the right settings into the diskobject and save it.
  238.       */
  239.       dobj->do_Type = WBPROJECT;
  240.       olddeftool = dobj->do_DefaultTool;
  241.       dobj->do_DefaultTool = ProgName;
  242.       dobj->do_CurrentX = dobj->do_CurrentY = NO_ICON_POSITION;
  243.       if (dobj->do_StackSize < 20000)
  244.       { dobj->do_StackSize = 20000;
  245.       }
  246.       PutDiskObject((STRPTR) name, dobj);
  247.       dobj->do_DefaultTool = olddeftool;
  248.       FreeDiskObject(dobj);
  249.     }
  250.   }
  251. }
  252. #else    /*  !AMIGA  */
  253. {   /*
  254.     There is nothing to be done on other systems.
  255.     */
  256. }
  257. #endif    /*  !AMIGA  */
  258.  
  259.  
  260.  
  261.  
  262. /*
  263.     AskSave brings up a requester asking the user, if he wants to save
  264.     first.
  265. */
  266. int AskSave(void)
  267.  
  268. #ifdef AMIGA
  269. {
  270.   switch (MUI_RequestA(App, MainWnd, 0,
  271.                (char *) GetChaosString(MSG_ATTENTION),
  272.                (char *) GetChaosString(MSG_YES_NO_CANCEL),
  273.                (char *) GetChaosString(MSG_CHANGES_MADE), NULL))
  274.   { case 2:
  275.       return(TRUE);
  276.     case 1:
  277.       return(SaveTournament(NULL));
  278.   }
  279.   return(FALSE);
  280. }
  281. #endif    /*  AMIGA   */
  282.  
  283.  
  284.  
  285.  
  286. /*
  287.     The TerminateTrnWnd() function closes the tournament input window.
  288. */
  289. #ifdef AMIGA
  290. static APTR TrnWnd = NULL; /*  Tournament window           */
  291. static APTR TrnOkGad;       /*  Ok gadget (tournament window)       */
  292. static APTR TrnCancelGad;  /*  Cancel gadget (tournament window)   */
  293. static APTR TrnNameGad;    /*  Tournament name gadget           */
  294.  
  295. void TerminateTrnWnd(void)
  296.  
  297. { if (TrnWnd)
  298.   { set(TrnWnd, MUIA_Window_Open, FALSE);
  299.     DoMethod(App, OM_REMMEMBER, TrnWnd);
  300.     MUI_DisposeObject(TrnWnd);
  301.     TrnWnd = NULL;
  302.   }
  303. }
  304. #endif    /*  AMIGA   */
  305.  
  306.  
  307.  
  308.  
  309. /*
  310.     The InitTrnWnd() function brings up a window, that allows to input
  311.     tournament data.
  312.  
  313.     Inputs: name    pointer to a buffer, that can hold the tournament name
  314.  
  315.     Results: TRUE, if successfull, FALSE otherwise
  316. */
  317. #ifdef AMIGA
  318. #define ID_TrnWnd_Cancel    201
  319. #define ID_TrnWnd_Ok        202
  320. int InitTrnWnd(char *buffer)
  321.  
  322. { ULONG open;
  323.  
  324.   /*
  325.       Open the window and check for success.
  326.   */
  327.   TrnWnd = WindowObject,
  328.         MUIA_Window_ID, MAKE_ID('T','R','N','I'),
  329.         MUIA_Window_Title, GetChaosString(MSG_TOURNAMENT_INPUT_TITLE),
  330.         MUIA_Window_Width, MUIV_Window_Width_MinMax(40),
  331.         WindowContents, VGroup,
  332.         Child, HGroup,
  333.             Child, Label2(GetChaosString(MSG_TOURNAMENT_NAME_OUTPUT)),
  334.             Child, TrnNameGad = StringObject,
  335.             StringFrame,
  336.             MUIA_String_MaxLen, TRNNAME_LEN+1,
  337.             MUIA_String_Contents, buffer,
  338.             End,
  339.         End,
  340.         Child, VSpace(10),
  341.         Child, HGroup,
  342.             MUIA_Group_SameSize, TRUE,
  343.             Child, TrnOkGad = KeyButton(GetChaosString(MSG_OK), *GetChaosString(MSG_OK_SC)),
  344.             Child, HSpace(0),
  345.             Child, TrnCancelGad = KeyButton(GetChaosString(MSG_CANCEL_INPUT), *GetChaosString(MSG_CANCEL_SC)),
  346.         End,
  347.         End,
  348.     End;
  349.   if (!TrnWnd)
  350.   { return(FALSE);
  351.   }
  352.   DoMethod(App, OM_ADDMEMBER, TrnWnd);
  353.   DoMethod(TrnWnd, MUIM_Window_SetCycleChain, TrnNameGad, TrnOkGad,
  354.        TrnCancelGad, NULL);
  355.   set(TrnWnd, MUIA_Window_ActiveObject, TrnNameGad);
  356.  
  357.  
  358.   /*
  359.       Setting up the notification events for the tournament input window:
  360.       CloseWindow, Ok- and Cancel Gadget
  361.   */
  362.   DoMethod(TrnWnd, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, App, 2,
  363.        MUIM_Application_ReturnID, ID_TrnWnd_Cancel);
  364.   DoMethod(TrnWnd, MUIM_Notify, MUIA_Window_InputEvent, "ctrl return",
  365.        App, 2, MUIM_Application_ReturnID, ID_TrnWnd_Ok);
  366.   DoMethod(TrnOkGad, MUIM_Notify, MUIA_Pressed, FALSE, App, 2,
  367.        MUIM_Application_ReturnID, ID_TrnWnd_Ok);
  368.   DoMethod(TrnCancelGad, MUIM_Notify, MUIA_Pressed, FALSE, App, 2,
  369.        MUIM_Application_ReturnID, ID_TrnWnd_Cancel);
  370.  
  371.  
  372.   set(TrnWnd, MUIA_Window_Open, TRUE);
  373.   get(TrnWnd, MUIA_Window_Open, &open);
  374.   if (!open)
  375.   { MUIError((char *) GetChaosString(ERRMSG_CANNOT_OPEN_WINDOW));
  376.     TerminateTrnWnd();
  377.     return(FALSE);
  378.   }
  379.   set(TrnWnd, MUIA_Window_ActiveObject, TrnNameGad);
  380.  
  381.   set(MainWnd, MUIA_Window_Open, FALSE);
  382.   return(TRUE);
  383. }
  384. #endif    /*  AMIGA   */
  385.  
  386.  
  387.  
  388.  
  389. /*
  390.     The ProcessTrnWnd() function waits for user actions concerning
  391.     the tournament input window.
  392.  
  393.     Inputs: buffer  pointer to a string, that holds the users input.
  394.             (Not needed on the Amiga.)
  395.  
  396.     Results:    0   Indicates, that the user has cancelled.
  397.         1   Indicates, that this has to be called again.
  398.         -1  Terminating via Ok-Gadget, okay
  399. */
  400. int ProcessTrnWnd(char *buffer)
  401.  
  402. #ifdef AMIGA
  403. { ULONG Signal;
  404.   char *name;
  405.  
  406.   /*
  407.       Check for user actions
  408.   */
  409.   switch (DoMethod(App, MUIM_Application_Input, &Signal))
  410.   { case MUIV_Application_ReturnID_Quit:
  411.       if (TestSaved())
  412.       { Cleanup(0, NULL);
  413.       }
  414.       break;
  415.     case ID_TrnWnd_Cancel:
  416.       return(0);
  417.     case ID_TrnWnd_Ok:
  418.       /*
  419.       Get the final state of the tournament name gadget.
  420.       */
  421.       if (buffer)
  422.       { get(TrnNameGad, MUIA_String_Contents, &name);
  423.     strcpy (buffer, name);
  424.       }
  425.       return(-1);
  426.   }
  427.   if (Signal)
  428.   { Wait(Signal);
  429.   }
  430.   return(1);
  431. }
  432. #endif
  433.