home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / NewsView 1.0.0 / source / StringDialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  7.9 KB  |  281 lines  |  [TEXT/KAHL]

  1. /* StringDialog.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Offline USENET News Viewer                                             */
  5. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  6. /*                                                                           */
  7. /*    This software is Public Domain; it may be used for any purpose         */
  8. /*    whatsoever without restriction.                                        */
  9. /*                                                                           */
  10. /*    This package is distributed in the hope that it will be useful,        */
  11. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  12. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  13. /*                                                                           */
  14. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  15. /*                                                                           */
  16. /*****************************************************************************/
  17.  
  18. #include "MiscInfo.h"
  19. #include "Audit.h"
  20. #include "Debug.h"
  21. #include "Definitions.h"
  22.  
  23. #include "StringDialog.h"
  24. #include "Screen.h"
  25. #include "EventLoop.h"
  26. #include "SimpleButton.h"
  27. #include "TextEdit.h"
  28. #include "Memory.h"
  29. #include "DataMunging.h"
  30. #include "Alert.h"
  31. #include "Menus.h"
  32.  
  33.  
  34. #define HEIGHT (76)
  35. #define WIDTH (350)
  36.  
  37. #define TEXTLEFT (10)
  38. #define TEXTTOP (13)
  39. #define TEXTWIDTH (120)
  40. #define TEXTHEIGHT (20)
  41.  
  42. #define EDITLEFT (140)
  43. #define EDITTOP (10)
  44. #define EDITWIDTH (200)
  45. #define EDITHEIGHT (20)
  46.  
  47. #define OKWIDTH (93)
  48. #define OKHEIGHT (21)
  49. #define OKLEFT (80)
  50. #define OKTOP (44)
  51.  
  52. #define CANCELWIDTH (93)
  53. #define CANCELHEIGHT (21)
  54. #define CANCELLEFT (80 + 93 + 20)
  55. #define CANCELTOP (44)
  56.  
  57.  
  58. typedef struct
  59.     {
  60.         WinType*                            ScreenID;
  61.         SimpleButtonRec*            OKButton;
  62.         TextEditRec*                    TextField;
  63.         char*                                    StaticPrompt;
  64.         SimpleButtonRec*            CancelButton;
  65.     } StringDialogRec;
  66.  
  67.  
  68. static void        LocalUpdateRoutine(StringDialogRec* Window)
  69.     {
  70.         CheckPtrExistence(Window);
  71.         SetClipRect(Window->ScreenID,0,0,WIDTH,HEIGHT);
  72.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->StaticPrompt,
  73.             StrLen(Window->StaticPrompt),TEXTLEFT,TEXTTOP,ePlain);
  74.         RedrawSimpleButton(Window->OKButton);
  75.         RedrawSimpleButton(Window->CancelButton);
  76.         TextEditFullRedraw(Window->TextField);
  77.     }
  78.  
  79.  
  80. /* present a dialog box displaying the string and allowing the user to make */
  81. /* changes. if the user clicks OK, then True is returned, else False is returned. */
  82. MyBoolean        DoStringDialog(char* Prompt, char** TheString,
  83.                             MenuItemType* MCut, MenuItemType* MPaste, MenuItemType* MCopy,
  84.                             MenuItemType* MUndo, MenuItemType* MSelectAll, MenuItemType* MClear)
  85.     {
  86.         StringDialogRec*        Window;
  87.         MyBoolean                        DoItFlag;
  88.  
  89.         /* make sure the string they gave us really exists */
  90.         CheckPtrExistence(*TheString);
  91.  
  92.         Window = (StringDialogRec*)AllocPtrCanFail(sizeof(StringDialogRec),"StringDialogRec");
  93.         if (Window == NIL)
  94.             {
  95.              FailurePoint1:
  96.                 AlertHalt("There is not enough memory to display the dialog box.",NIL);
  97.                 return False;
  98.             }
  99.  
  100.         Window->ScreenID = MakeNewWindow(eDialogWindow,eWindowNotClosable,eWindowNotZoomable,
  101.             eWindowNotResizable,DialogLeftEdge(WIDTH),DialogTopEdge(HEIGHT),WIDTH,HEIGHT,
  102.             (void (*)(void*))&LocalUpdateRoutine,Window);
  103.         if (Window->ScreenID == 0)
  104.             {
  105.              FailurePoint2:
  106.                 ReleasePtr((char*)Window);
  107.                 goto FailurePoint1;
  108.             }
  109.  
  110.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKLEFT,OKTOP,
  111.             OKWIDTH,OKHEIGHT);
  112.         if (Window->OKButton == NIL)
  113.             {
  114.              FailurePoint3:
  115.                 KillWindow(Window->ScreenID);
  116.                 goto FailurePoint2;
  117.             }
  118.         SetDefaultButtonState(Window->OKButton,True);
  119.  
  120.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELLEFT,
  121.             CANCELTOP,CANCELWIDTH,CANCELHEIGHT);
  122.         if (Window->CancelButton == NIL)
  123.             {
  124.              FailurePoint4:
  125.                 DisposeSimpleButton(Window->OKButton);
  126.                 goto FailurePoint3;
  127.             }
  128.  
  129.         Window->TextField = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),9,
  130.             EDITLEFT,EDITTOP,EDITWIDTH,EDITHEIGHT);
  131.         if (Window->TextField == NIL)
  132.             {
  133.              FailurePoint5:
  134.                 DisposeSimpleButton(Window->CancelButton);
  135.                 goto FailurePoint4;
  136.             }
  137.         TextEditNewRawData(Window->TextField,*TheString,""/*shouldn't be a linefeed*/);
  138.         TextEditDoMenuSelectAll(Window->TextField);
  139.         EnableTextEditSelection(Window->TextField);
  140.  
  141.         /* allocation is finished; copy over parameters */
  142.         Window->StaticPrompt = Prompt;
  143.  
  144.         /* do our local event loop */
  145.         while (1)
  146.             {
  147.                 OrdType                            X;
  148.                 OrdType                            Y;
  149.                 ModifierFlags                Modifiers;
  150.                 MenuItemType*                MenuItem;
  151.                 char                                KeyPress;
  152.  
  153.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  154.                     {
  155.                         default:
  156.                             break;
  157.                         case eCheckCursor:
  158.                             if (TextEditIBeamTest(Window->TextField,X,Y))
  159.                                 {
  160.                                     SetIBeamCursor();
  161.                                 }
  162.                              else
  163.                                 {
  164.                                     SetArrowCursor();
  165.                                 }
  166.                             goto NoEventPoint;
  167.                             break;
  168.                         case eNoEvent:
  169.                          NoEventPoint:
  170.                             TextEditUpdateCursor(Window->TextField);
  171.                             break;
  172.                         case eMenuStarting:
  173.                             EnableMenuItem(MPaste);
  174.                             if (TextEditIsThereValidSelection(Window->TextField))
  175.                                 {
  176.                                     EnableMenuItem(MCut);
  177.                                     EnableMenuItem(MCopy);
  178.                                     EnableMenuItem(MClear);
  179.                                 }
  180.                             EnableMenuItem(MSelectAll);
  181.                             if (TextEditCanWeUndo(Window->TextField))
  182.                                 {
  183.                                     EnableMenuItem(MUndo);
  184.                                 }
  185.                             break;
  186.                         case eMenuCommand:
  187.                             if (MenuItem == MPaste)
  188.                                 {
  189.                                     TextEditDoMenuPaste(Window->TextField);
  190.                                 }
  191.                             else if (MenuItem == MCut)
  192.                                 {
  193.                                     TextEditDoMenuCut(Window->TextField);
  194.                                 }
  195.                             else if (MenuItem == MCopy)
  196.                                 {
  197.                                     TextEditDoMenuCopy(Window->TextField);
  198.                                 }
  199.                             else if (MenuItem == MClear)
  200.                                 {
  201.                                     TextEditDoMenuClear(Window->TextField);
  202.                                 }
  203.                             else if (MenuItem == MUndo)
  204.                                 {
  205.                                     TextEditDoMenuUndo(Window->TextField);
  206.                                     TextEditShowSelection(Window->TextField);
  207.                                 }
  208.                             else if (MenuItem == MSelectAll)
  209.                                 {
  210.                                     TextEditDoMenuSelectAll(Window->TextField);
  211.                                 }
  212.                             else
  213.                                 {
  214.                                     EXECUTE(PRERR(AllowResume,
  215.                                         "DoStringDialog: Undefined menu option chosen"));
  216.                                 }
  217.                             break;
  218.                         case eKeyPressed:
  219.                             if (KeyPress == 13)
  220.                                 {
  221.                                     char*                                    RawTemp;
  222.  
  223.                                     FlashButton(Window->OKButton);
  224.                                  OKButtonClickedPoint:
  225.                                     RawTemp = TextEditGetRawData(Window->TextField,
  226.                                         ""/*shouldn't be a linefeed*/);
  227.                                     if (RawTemp != NIL)
  228.                                         {
  229.                                             ReleasePtr(*TheString);
  230.                                             *TheString = RawTemp;
  231.                                             DoItFlag = True;
  232.                                         }
  233.                                      else
  234.                                         {
  235.                                             DoItFlag = False;
  236.                                         }
  237.                                     goto AllDonePoint;
  238.                                 }
  239.                             else if (KeyPress == eCancelKey)
  240.                                 {
  241.                                     FlashButton(Window->CancelButton);
  242.                                     DoItFlag = False;
  243.                                     goto AllDonePoint;
  244.                                 }
  245.                             else
  246.                                 {
  247.                                     TextEditDoKeyPressed(Window->TextField,KeyPress,Modifiers);
  248.                                 }
  249.                             break;
  250.                         case eMouseDown:
  251.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  252.                                 {
  253.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  254.                                         {
  255.                                             goto OKButtonClickedPoint;
  256.                                         }
  257.                                 }
  258.                             else if (TextEditHitTest(Window->TextField,X,Y))
  259.                                 {
  260.                                     TextEditDoMouseDown(Window->TextField,X,Y,Modifiers);
  261.                                 }
  262.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  263.                                 {
  264.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  265.                                         {
  266.                                             DoItFlag = False;
  267.                                             goto AllDonePoint;
  268.                                         }
  269.                                 }
  270.                             break;
  271.                     }
  272.             }
  273.      AllDonePoint:
  274.         DisposeTextEdit(Window->TextField);
  275.         DisposeSimpleButton(Window->OKButton);
  276.         DisposeSimpleButton(Window->CancelButton);
  277.         KillWindow(Window->ScreenID);
  278.         ReleasePtr((char*)Window);
  279.         return DoItFlag;
  280.     }
  281.