home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / dtx9302 / tvision / tvcpp / popup.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-05  |  6.6 KB  |  243 lines

  1. /*========================================================*/
  2. /*                        POPUP.CPP                       */
  3. /*         (C) 1993 Michael Maier & DMV-Verlag            */
  4. /* Das Programm demonstriert, wie ein POPUP-Menü bei      */
  5. /* einem Klick auf die rechte Maustaste auf dem Desktop   */
  6. /* an der Position des Mauscursors angezeigt werden kann. */
  7. /*       Compiler: Turbo C++ 3.0, Borland C++ 3.1         */
  8. /*========================================================*/
  9.                                           
  10. #define Uses_TEventQueue
  11. #define Uses_TEvent
  12. #define Uses_TApplication
  13. #define Uses_TKeys
  14. #define Uses_TRect
  15. #define Uses_TMenu
  16. #define Uses_TMenuBar
  17. #define Uses_TSubMenu
  18. #define Uses_TMenuItem
  19. #define Uses_TMenuBox
  20. #define Uses_TStatusLine
  21. #define Uses_TStatusItem
  22. #define Uses_TStatusDef
  23. #define Uses_TDeskTop
  24. #define Uses_TView
  25. #define Uses_TWindow
  26. #define Uses_MsgBox
  27. #include <tv.h>
  28.  
  29. #include <stdio.h>
  30. #include <dos.h>
  31.  
  32. const int cmNew    = 200;
  33. const int cmOpen   = 201;
  34. const int cmSave   = 202;
  35. const int cmPopUp  = 203;
  36. const int cmHelpme = 204;
  37.  
  38. const int hcNew    = 1000;
  39. const int hcOpen   = 1001;
  40. const int hcSave   = 1002;
  41. const int hcExit   = 1003;
  42. const int hcZoom   = 1004;
  43. const int hcNext   = 1005;
  44. const int hcClose  = 1006;
  45. const int hcFile   = 1007;
  46. const int hcWindow = 1008;
  47. const int hcCopy   = 1009;
  48. const int hcCut    = 1010;
  49. const int hcPaste  = 1011;
  50. const int hcHelpme = 1012;
  51.  
  52. const char *apstrHilfetexte[] =
  53. {
  54.   "Legt eine neue Datei an",
  55.   "Lädt eine bestehende Datei in den Speicher",
  56.   "Speichert die Datei auf Diskette",
  57.   "Verläßt das Programm",
  58.   "Vergrößert das aktive Fenster",
  59.   "Wechselt zum nächsten Fenster",
  60.   "Schließt das aktive Fenster",
  61.   "Enthält Funktionen zum Dateihandling/Programmende",
  62.   "Enthält Funktionen zum Fensterhandling",
  63.   "Kopiert markierten Block in das Clipboard",
  64.   "Schneidet markierten Bereich aus",
  65.   "Fügt Block aus dem Clipboard ein",
  66.   "Gibt einen Hilfetext aus",
  67.   NULL
  68. };
  69.  
  70. void MausPosButtonStatus(MouseEventType& event)
  71. {
  72.   union REGS regs;
  73.  
  74.   regs.x.ax = 0x03; /*     Funktionnummer ins AX-Register */
  75.   regs.x.bx = 0x00; /* BX-Register löschen, da in ihm der */
  76.                  /* Status der Mausbuttons übergeben wird */
  77.   int86(0x33, ®s, ®s);    /* und Interrupt aufrufen */
  78.  
  79.   event.buttons =  regs.x.bx;  /* Mausbuttonstatus merken */
  80.   event.where.x = regs.x.cx >> 3; /* Koordinaten in Text- */
  81.   event.where.y = regs.x.dx >> 3; /*      modus umrechnen */
  82.   event.doubleClick = False;
  83. }
  84.  
  85. class THintStatusLine: public TStatusLine
  86. {
  87.   public:
  88.     THintStatusLine(const TRect& bounds, TStatusDef& aDefs);
  89.             virtual const char* hint (ushort aHelpCtx);
  90. };
  91.  
  92.  
  93. THintStatusLine::THintStatusLine(const TRect& bounds,
  94.     TStatusDef& aDefs): TStatusLine(bounds, aDefs)
  95. {
  96. };
  97.  
  98.  
  99. const char *THintStatusLine::hint (ushort aHelpCtx)
  100. {
  101.   if (aHelpCtx >= hcNew && aHelpCtx <= hcHelpme)
  102.     return(apstrHilfetexte[aHelpCtx - 1000]);
  103.   else
  104.     return("");
  105. };
  106.  
  107. class THintStatusApp : public TApplication
  108. {
  109.   public:
  110.     THintStatusApp();
  111.     static       TStatusLine *initStatusLine(TRect r);
  112.     static       TMenuBar *initMenuBar(TRect r);
  113.     virtual void handleEvent(TEvent& event);
  114.     void         PopUpMenu(ushort x, ushort y);
  115. };
  116.  
  117. THintStatusApp::THintStatusApp():
  118.   TProgInit(&THintStatusApp::initStatusLine,
  119.             &THintStatusApp::initMenuBar,
  120.             &THintStatusApp::initDeskTop)
  121. {
  122. }
  123.  
  124. TMenuBar *THintStatusApp::initMenuBar(TRect r)
  125. {
  126.   r.b.y = r.a.y + 1;
  127.  
  128.   TSubMenu& sub1 =
  129.    *new TSubMenu("~D~atei", kbAltF ) +
  130.     *new TMenuItem("~N~eu", cmNew, kbF4, hcNew, "F4") +
  131.      *new TMenuItem("~Ö~ffnen", cmOpen, kbF3, hcOpen, "F3")+
  132.       *new TMenuItem("~S~peichern", cmSave, kbF2, hcSave,
  133.                      "F2") + newLine() +
  134.        *new TMenuItem("Quit", cmQuit, cmQuit, hcExit,
  135.                       "Alt-X");
  136.  
  137.   sub1.helpCtx = hcFile;
  138.  
  139.   TSubMenu& sub2 =
  140.    *new TSubMenu("~F~enster", kbAltW) +
  141.     *new TMenuItem("~N~ächstes", cmNext, kbF6, hcNext,"F6")+
  142.      *new TMenuItem("~V~ergrößern", cmZoom, kbF5, hcZoom,
  143.                     "F5") +
  144.       *new TMenuItem("~S~chließen", cmClose, kbAltF3,
  145.                      hcClose, "Alt-F3");
  146.  
  147.   sub2.helpCtx = hcWindow;
  148.  
  149.   return(new TMenuBar(r, sub1 + sub2));
  150. }
  151.  
  152. TStatusLine *THintStatusApp::initStatusLine(TRect r)
  153. {
  154.   r.a.y = r.b.y - 1;
  155.   return new THintStatusLine(r, *new TStatusDef(0, 0xFFFF) +
  156.    *new TStatusItem("~F10~ Menu", kbF10, cmMenu) +
  157.     *new TStatusItem("", kbAltX, cmQuit));
  158. }
  159.  
  160. int main()
  161. {
  162.   THintStatusApp HintStatusApp;
  163.   HintStatusApp.run();
  164.   return(0);
  165. }
  166.  
  167. void THintStatusApp::PopUpMenu(ushort x, ushort y)
  168. {
  169.   TMenuItem& ItemList =
  170.    *new TMenuItem("~C~opy", cmCopy, kbAltC, hcCopy, "",
  171.      new TMenuItem("C~u~t", cmCut, kbAltU, hcCut, "",
  172.       new TMenuItem("~P~aste", cmPaste, kbAltP, hcPaste, "",
  173.        new TMenuItem("~H~ilfe", cmHelpme, kbAltH, hcHelpme,
  174.                                                      ""))));
  175.  
  176.   TMenu* PopUp = new TMenu(ItemList);
  177.  
  178.   TRect& r = getExtent();
  179.   --r.a.x +=  x;
  180.   --r.a.y +=  y;
  181.  
  182.   TMenuBox* Box = (TMenuBox*)
  183.                    validView(new TMenuBox(r, PopUp, 0));
  184.   if (Box)
  185.   {
  186.     int RetCode = execView(Box);
  187.     destroy(Box);
  188.     TEvent event;
  189.     event.what = evCommand;
  190.     event.message.command = RetCode;
  191.     putEvent(event);
  192.   }
  193. }
  194.  
  195. void THintStatusApp::handleEvent(TEvent& event)
  196. {
  197.   MouseEventType MouseEvent;
  198.  
  199.   if (event.what == evMouseDown)
  200.   {
  201.     MausPosButtonStatus(MouseEvent);
  202.     if (MouseEvent.buttons & mbRightButton)
  203.     {
  204.       event.what = evCommand;
  205.       event.message.command = cmPopUp;
  206.     }
  207.   }
  208.  
  209.   TApplication::handleEvent(event);
  210.  
  211.   if (event.what == evCommand)
  212.   {
  213.     switch(event.message.command)
  214.     {
  215.       case cmPopUp:
  216.         PopUpMenu(MouseEvent.where.x, MouseEvent.where.y);
  217.         break;
  218.       case cmCopy:
  219.         messageBox("Menüpunkt COPY wurde gewählt",
  220.                     mfInformation | mfOKButton);
  221.         break;
  222.       case cmCut:
  223.         messageBox("Menüpunkt CUT wurde gewählt",
  224.                     mfInformation | mfOKButton);
  225.         break;
  226.       case cmPaste:
  227.         messageBox("Menüpunkt PASTE wurde gewählt",
  228.                     mfInformation | mfOKButton);
  229.         break;
  230.       case cmHelpme:
  231.         messageBox("Menüpunkt HILFE wurde gewählt",
  232.                     mfInformation | mfOKButton);
  233.         break;
  234.       default:
  235.         return;
  236.     }
  237.     clearEvent(event);
  238.   }
  239. }
  240.  
  241. /*========================================================*/
  242. /*                      Ende von POPUP.CPP                */
  243.