home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / streams / stream / myapp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-25  |  9.6 KB  |  364 lines

  1. //========================================================================
  2. //  The following example routines have been provided by the Technical
  3. //  Support staff at Borland International.  They are provided as a
  4. //  courtesy and not as part of a Borland product, and as such, are
  5. //  provided without the assurance of technical support or any specific
  6. //  guarantees.
  7. //========================================================================
  8. //
  9. // myapp.cpp:  The member function definition for TMyApp.
  10. //
  11.  
  12. #define Uses_TApplication
  13. #define Uses_TRect
  14. #define Uses_TKeys
  15. #define Uses_TMenuBar
  16. #define Uses_TMenuItem
  17. #define Uses_TSubMenu
  18. #define Uses_TStatusLine
  19. #define Uses_TStatusDef
  20. #define Uses_TStatusItem
  21. #define Uses_TEvent
  22. #define Uses_TDialog
  23. #define Uses_TStaticText
  24. #define Uses_TButton
  25. #define Uses_TDeskTop
  26. #define Uses_TWindow
  27. #define Uses_TStreamable
  28. #define Uses_fpstream
  29. #define Uses_MsgBox
  30. #include <tv.h>
  31.  
  32. __link( RWindow );          // TWindow is used.
  33. __link( RStaticText );      // TStaticText is used.
  34.  
  35. #include <strstrea.h>       // String stream library
  36. #include <stdlib.h>         // Standard library
  37. #include <stdio.h>          // For remove()
  38. #include <dir.h>            // For findfirst()
  39.  
  40. #include "gadgets.h"        // TClockView and THeapView
  41. #include "cmds.h"           // Command set
  42. #include "myapp.h"          // Class definitions
  43. #include "funkview.h"       // Class definitions
  44.  
  45.  
  46. //
  47. // TMyApp - Constructor.
  48. //
  49.  
  50. TMyApp::TMyApp() :
  51.     TProgInit( &TMyApp::initStatusLine,
  52.                &TMyApp::initMenuBar,
  53.                &TMyApp::initDeskTop )
  54. {
  55.     TRect r = getExtent();                      // Create the clock view.
  56.     r.a.x = r.b.x - 9;      r.b.y = r.a.y + 1;
  57.     clock = new TClockView( r );
  58.     insert(clock);
  59.  
  60.     r = getExtent();                            // Create the heap view.
  61.     r.a.x = r.b.x - 13;     r.a.y = r.b.y - 1;
  62.     heap = new THeapView( r );
  63.     insert(heap);
  64. }
  65.  
  66.  
  67. //
  68. // initMenuBar - Initialize the menu bar. It will be called by the virtual base
  69. //               TProgInit constructor.
  70. //
  71.  
  72. TMenuBar *TMyApp::initMenuBar( TRect bounds )
  73. {
  74.     bounds.b.y = bounds.a.y + 1;
  75.  
  76.     TMenuBar *mainMenu = new TMenuBar (bounds,
  77.         *new TSubMenu ("~≡~", kbAltSpace ) +        // ≡ is \xF0
  78.             *new TMenuItem ("~A~bout...", cmAboutCmd, kbAltA, hcNoContext, "Alt-A") +
  79.              newLine() +
  80.             *new TMenuItem ("~E~xit", cmQuit, kbAltE, hcNoContext, "Alt-E") +
  81.         *new TSubMenu("~W~indow", kbAltW ) +
  82.             *new TMenuItem("~O~pen", cmOpenCmd, kbF2, hcNoContext, "F2") +
  83.             *new TMenuItem("~C~lose", cmClose, kbAltF3, hcNoContext, "Alt-F3") +
  84.              newLine() +
  85.             *new TMenuItem("~F~unky View", cmFunkCmd, kbNoKey) +
  86.         *new TSubMenu("~O~ptions", kbAltO ) +
  87.             *new TMenuItem("~S~ave desktop", cmSaveCmd, kbNoKey) +
  88.             *new TMenuItem("~R~estore desktop", cmRestoreCmd, kbNoKey)
  89.         );
  90.  
  91.     return( mainMenu );
  92. }
  93.  
  94.  
  95. //
  96. // initStatusLine - Initialize the status line. It will be called by the
  97. //                  virtual base TProgInit constructor.
  98. //
  99.  
  100. TStatusLine *TMyApp::initStatusLine( TRect bounds )
  101. {
  102.     bounds.a.y = bounds.b.y - 1;
  103.  
  104.     TStatusLine *statusLine = new TStatusLine (bounds,
  105.         *new TStatusDef(0, 0xFFFF) +
  106.             *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
  107.             *new TStatusItem("~Alt-A~ About", kbAltA, cmAboutCmd) +
  108.             *new TStatusItem("~F2~ Open", kbF2, cmOpenCmd) +
  109.             *new TStatusItem("~Alt-F3~ Close", kbF3, cmClose)
  110.         );
  111.  
  112.     return (statusLine);
  113. }
  114.  
  115.  
  116. //
  117. // handleEvent - Need to handle the event for the menu and status line choice
  118. //               cmAboutCmd.
  119. //
  120.  
  121. void TMyApp::handleEvent(TEvent& event)
  122. {
  123.     TApplication::handleEvent(event);
  124.  
  125.     switch(event.what)
  126.     {
  127.         case evCommand:             // handle COMMAND events
  128.         {
  129.             switch(event.message.command)
  130.             {
  131.                 case cmAboutCmd:
  132.                 {
  133.                     aboutDlg();     // Bring up the dialog box
  134.                     break;
  135.                 }
  136.  
  137.                 case cmOpenCmd:
  138.                 {
  139.                     window();       // Bring up the window
  140.                     break;
  141.                 }
  142.  
  143.                 case cmFunkCmd:
  144.                 {
  145.                     funkView();     // Bring up funky view
  146.                     break;
  147.                 }
  148.  
  149.                 case cmSaveCmd:
  150.                 {
  151.                     saveDeskTop();
  152.                     break;
  153.                 }
  154.  
  155.                 case cmRestoreCmd:
  156.                 {
  157.                     restoreDeskTop();
  158.                     break;
  159.                 }
  160.  
  161.                 default:
  162.                     return;
  163.             }
  164.             clearEvent(event);      // Clear the event we handled
  165.         }
  166.     }
  167. }
  168.  
  169.  
  170. //
  171. // aboutDlg - Creates a about dialog box and execute the dialog box.
  172. //
  173.  
  174. void TMyApp::aboutDlg()
  175. {
  176.     TDialog *aboutDlgBox = new TDialog(TRect(0, 0, 47, 10), "About");
  177.     char temp[200];
  178.  
  179. //
  180. // Use the strstream library to put the ABOUT text into a string.  We
  181. // don't need << operators on the intermediate lines because the compiler
  182. // will concatenate the strings automatically.  The ^C character begining
  183. // each line will force that line to be centered.  The ' ' character between
  184. // each pair of newlines is to avoid a quirk that two adjacent newlines are
  185. // treated as one.
  186. //
  187.  
  188.     ostrstream(temp,200) << "\003streams.cpp\n \n"
  189.                             "\003C++ Version 1.5\n \n"
  190.                             "\003Copyright (C) Borland International, 1991."
  191.                             "\n \n"
  192.                          << ends;
  193.  
  194.     aboutDlgBox->insert( new TStaticText(TRect(2,2,45,9), temp) );
  195.     aboutDlgBox->insert( new TButton(TRect(18,10,29,12), "OK", cmOK, bfDefault) );
  196.  
  197.     aboutDlgBox->options |= ofCentered;     // Centered on the screen
  198.  
  199.     deskTop->execView( aboutDlgBox );       // Bring up the box as modal
  200.  
  201.     destroy( aboutDlgBox );                 // Destroy the box
  202. }
  203.  
  204.  
  205. //
  206. // window() - Open a window with a view for my name in inside.
  207. //
  208.  
  209. void TMyApp::window()
  210. {
  211.     unsigned short x,y;
  212.  
  213.     x = random(79-30);
  214.     y = random(23-7);
  215.  
  216.     TWindow *win = new TWindow(TRect(x,y,x+30,y+7), "Static Text", wnNoNumber);
  217.  
  218.     win->insert(new TStaticText(TRect(3,2,27,6),"\x3This space intentionally\n\x3left blank"));
  219.  
  220.     deskTop->insert(win);
  221. }
  222.  
  223.  
  224. //
  225. // idle() - handles updating the heap and clock views when the application
  226. //      is not doing anything else.
  227. //
  228.  
  229. void TMyApp::idle()
  230. {
  231.     TProgram::idle();
  232.     clock->update();
  233.     heap->update();
  234. }
  235.  
  236.  
  237. static char *sayings[] = { "             42              ",
  238.                            "  what do you get when you   ",
  239.                            "    multiply 7 by 9?         ",
  240.                            "  What is the answer to the  ",
  241.                            "    to the great question?   "
  242.                          };
  243. const int numSayings = sizeof(sayings) / sizeof(char *);
  244.  
  245. void TMyApp::funkView()
  246. {
  247.     unsigned short x, y;
  248.  
  249.     x = random(79-FUNK_WIDTH);
  250.     y = random(23-FUNK_HEIGHT);
  251.  
  252.     FunkyWindow *v = new FunkyWindow( TRect(x, y, x+FUNK_WIDTH, y+FUNK_HEIGHT),
  253.                                       sayings[random(numSayings)]
  254.                                     );
  255.     deskTop->insert( v );
  256. }
  257.  
  258.  
  259. #define DESKTOP_FILE "stream.dst"
  260.  
  261. // Helper functions for implementation of Save/Restore desktop.
  262.  
  263. //
  264. // writeView() function ( writes a view object to a resource file )
  265. //
  266.  
  267. static void writeView(TView *p, void *strm)
  268. {
  269.    fpstream *s = (fpstream *) strm;
  270.    if (p != TProgram::deskTop->last)
  271.       *s << p;
  272. }
  273.  
  274.  
  275. //
  276. // closeView() function
  277. //
  278.  
  279. static void closeView(TView *p, void *p1)
  280. {
  281.     message(p, evCommand, cmClose, p1);
  282. }
  283.  
  284.  
  285. //
  286. // restoreDeskTop() function ( restores the previously stored DeskTop )
  287. //
  288.  
  289. void TMyApp::restoreDeskTop()
  290. {
  291.     struct ffblk ffblk;
  292.  
  293.     if (findfirst(DESKTOP_FILE, &ffblk, 0))
  294.         messageBox("Could not find desktop file", mfOKButton | mfError);
  295.     else 
  296.         {
  297.         fpstream *f = new fpstream(DESKTOP_FILE, ios::in|ios::binary);
  298.         if( !f )
  299.             messageBox("Could not open desktop file", mfOKButton | mfError);
  300.         else
  301.            {
  302.            TMyApp::loadDeskTop(*f);
  303.            if( !f )
  304.                messageBox("Error reading desktop file", mfOKButton | mfError);
  305.            }
  306.         delete f;
  307.         }
  308. }
  309.  
  310.  
  311. //
  312. // loadDeskTop() function
  313. //
  314.  
  315. void TMyApp::loadDeskTop(fpstream &s)
  316. {
  317.     TView  *p;
  318.  
  319.     if (deskTop->valid(cmClose))
  320.         { 
  321.         deskTop->forEach(::closeView, 0);  // Clear the desktop
  322.         do
  323.             {
  324.             s >> p;
  325.             deskTop->insertBefore(validView(p), deskTop->last);
  326.             }
  327.         while (p != 0);
  328.         }
  329. }
  330.  
  331.  
  332. //
  333. // saveDeskTop() function ( saves the DeskTop by calling storeDeskTop function )
  334. //
  335.  
  336. void TMyApp::saveDeskTop()
  337. {
  338.     fpstream *f = new fpstream(DESKTOP_FILE, ios::out|ios::binary);
  339.  
  340.     if( f )
  341.         {
  342.         TMyApp::storeDeskTop(*f);
  343.         if( !f )
  344.             {
  345.             messageBox("Could not create desktop file STREAM.DST", mfOKButton | mfError);
  346.             delete f;
  347.             ::remove(DESKTOP_FILE);
  348.             return;
  349.             }
  350.         }
  351.     delete f;
  352. }
  353.  
  354.  
  355. //
  356. // storeDeskTop() function ( stores the DeskTop in a resource file )
  357. //
  358.  
  359. void TMyApp::storeDeskTop(fpstream& s)                 // Lesson J
  360. {
  361.   deskTop->forEach(::writeView, &s);
  362.   s << 0;
  363. }
  364.