home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / c / fli106c / examples / yesno.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-25  |  3.7 KB  |  162 lines

  1. //
  2. // The Fusion Library Interface for DOS
  3. // Version 1.02
  4. // Copyright (C) 1990, 1991
  5. // Software Dimensions
  6. //
  7. // Dialog Development System
  8. //
  9.  
  10. #include "elements.h"
  11. #include "colors.h"
  12.  
  13. #include <alloc.h>
  14. #include <string.h>
  15.  
  16. #include "yesno.h"
  17.  
  18. //---------------------------------------------------------------------------
  19. //
  20. // YesNo()
  21. //
  22. // Constructor for YesNo class.  YesNo is sorta like InfoBox except that it
  23. // has a yes/no toggle capability.
  24. //
  25. //---------------------------------------------------------------------------
  26.  
  27. YesNo::YesNo()
  28. {
  29.   AllocationError=0;
  30.   MessageLineStorage=NULL;
  31.   NumberOfInfoLines=0;
  32.   TitleOfYesNo=NULL;
  33. }
  34.  
  35. //---------------------------------------------------------------------------
  36. //
  37. // ~YesNo()
  38. //
  39. // Destructor for YesNo class
  40. //
  41. //---------------------------------------------------------------------------
  42.  
  43. YesNo::~YesNo()
  44. {
  45.   if (MessageLineStorage)
  46.     free(MessageLineStorage);
  47. }
  48.  
  49. //---------------------------------------------------------------------------
  50. //
  51. // Title()
  52. //
  53. // Define a title
  54. //
  55. //---------------------------------------------------------------------------
  56.  
  57. void YesNo::Title(char *Title)
  58. {
  59.   TitleOfYesNo=Title;
  60. }
  61.  
  62. //---------------------------------------------------------------------------
  63. //
  64. // operator+
  65. //
  66. // Define a info box message line
  67. //
  68. //---------------------------------------------------------------------------
  69.  
  70. YesNo& YesNo::operator +(char *MessageLine)
  71. {
  72.   if (!AllocationError)
  73.   {
  74.     NumberOfInfoLines++;
  75.  
  76.     if (!(MessageLineStorage=(char **)realloc(MessageLineStorage,sizeof(char *)*NumberOfInfoLines)))
  77.       AllocationError++;
  78.  
  79.     if (AllocationError)
  80.       return *this;
  81.  
  82.     MessageLineStorage[NumberOfInfoLines-1]=MessageLine;
  83.   }
  84.   return *this;
  85. }
  86.  
  87. //---------------------------------------------------------------------------
  88. //
  89. // InfoDialog class
  90. //
  91. // Defines the event handler for a dialog
  92. //
  93. //---------------------------------------------------------------------------
  94.  
  95. const YesButton=5000;
  96. const NoButton=5001;
  97.  
  98. class YesNoDialog : public DialogClass
  99. {
  100. public:
  101.  
  102.   YesNoDialog(int Width,int Height,char *Title) :
  103.     DialogClass(Width,Height,Title) { }
  104.  
  105.   int EventHandler(int Event)
  106.   {
  107.     if (Event==kbEsc ||
  108.         Event==kbCr ||
  109.         Event==CloseEvent ||
  110.         Event==OutsideEvent ||
  111.         Event==YesButton ||
  112.         Event==NoButton)
  113.       return StopEvent;
  114.     return CompleteEvent;
  115.   }
  116. };
  117.  
  118. //---------------------------------------------------------------------------
  119. //
  120. // UseYesNo()
  121. //
  122. // Activate the menu and pass control to YesNo class.
  123. //
  124. //---------------------------------------------------------------------------
  125.  
  126. int YesNo::UseYesNo()
  127. {
  128.   if (AllocationError)
  129.     return 800;
  130.  
  131.   if (!NumberOfInfoLines)
  132.     return 900;
  133.  
  134.   for (register int i=0, Widest=0;i<NumberOfInfoLines;i++)
  135.     if (strlen(MessageLineStorage[i])>Widest)
  136.       Widest=strlen(MessageLineStorage[i]);
  137.  
  138.   YesNoDialog &Dialog=*new YesNoDialog(Widest+4,NumberOfInfoLines+6,TitleOfYesNo);
  139.  
  140.   Dialog.Element(new DiaPushButton((Widest-13)/2,NumberOfInfoLines+2,"~Yes",YesButton,'Y',1));
  141.   Dialog.Help("Yes, I acknowledge this (press Y for YES or N for NO)");
  142.  
  143.   Dialog.Element(new DiaPushButton(((Widest-13)/2)+9,NumberOfInfoLines+2,"~No",NoButton,'N'));
  144.   Dialog.Help("No, I reject this (press Y for YES or N for NO)");
  145.  
  146.   MouseHide();
  147.  
  148.   Dialog.Blaze << Colors.DiaInterior;
  149.  
  150.   for (i=0;i<NumberOfInfoLines;i++)
  151.     Dialog.Blaze ((Widest-strlen(MessageLineStorage[i])+2)/2,1+i) <<
  152.       MessageLineStorage[i];
  153.  
  154.   MouseShow();
  155.  
  156.   int ReturnCode=Dialog.UseDialog();
  157.  
  158.   delete &Dialog;
  159.  
  160.   return (ReturnCode==YesButton);
  161. }
  162.