home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / TVDEMOS.ZIP / TVBGI.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  6.7 KB  |  274 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   Turbo Vision BGI Support Demo                         */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. #define Uses_TKeys
  10. #define Uses_TApplication
  11. #define Uses_TEvent
  12. #define Uses_TRect
  13. #define Uses_TDialog
  14. #define Uses_TButton
  15. #define Uses_TMenuBar
  16. #define Uses_TSubMenu
  17. #define Uses_TMenuItem
  18. #define Uses_TStatusLine
  19. #define Uses_TStatusItem
  20. #define Uses_TStatusDef
  21. #define Uses_TDeskTop
  22. #define Uses_MsgBox
  23. #define Uses_TChDirDialog
  24. #define Uses_THistory
  25. #define Uses_TWindow
  26. #define Uses_TInputLine
  27. #include <tv.h>
  28.  
  29. #include "Tvbgi.h"
  30. #include <stdlib.h>
  31. #include <graphics.h>
  32. #include <string.h>
  33. #include <strstrea.h>
  34.  
  35. TBGIApp::TBGIApp() :
  36.     TProgInit( &TBGIApp::initStatusLine,
  37.            &TBGIApp::initMenuBar,
  38.            &TBGIApp::initDeskTop )
  39. {
  40.     fexpand(pathToDrivers);
  41.     bgiPath = new char[sizeof(pathToDrivers)];
  42.     strcpy(bgiPath, pathToDrivers);
  43.     appDriver = DETECT;
  44.     appMode = 0;
  45.     if (graphAppInit(appDriver, appMode, bgiPath, True) == False)
  46.     messageBox("Cannot load graphics driver.", mfError | mfOKButton);
  47. }
  48.  
  49. TBGIApp::~TBGIApp()
  50. {
  51.     graphAppDone();
  52.     delete bgiPath;
  53. }
  54.  
  55. void TBGIApp::newWin()
  56. {
  57.     static ushort winNum = 0;
  58.     TWindow *p;
  59.  
  60.     TRect r(deskTop->getExtent());
  61.     r = TRect( winNum % (deskTop->size.y-1),
  62.                winNum % (deskTop->size.y-1),
  63.                deskTop->size.x,
  64.                deskTop->size.y);
  65.  
  66.     char msgStr[12];
  67.     ostrstream os( msgStr, 12 );
  68.     os << "Window " << winNum << ends;
  69.     p = new TWindow( r, msgStr, 0 );
  70.     p->options |= ofTileable;
  71.     deskTop->insert( validView(p) );
  72.     winNum++;
  73. }
  74.  
  75. // Draw random polygons with random fill styles on the screen }
  76.  
  77. void TBGIApp::doGraphics()
  78. {
  79.     const int maxPts = 5;
  80.     const int CLIP_ON = 1;
  81.     typedef TPoint PolygonType[maxPts];
  82.     char errorMsg[MAXSIZE];
  83.     TEvent event;
  84.     PolygonType poly;
  85.     ushort i, color;
  86.     ushort maxX, maxY;
  87.  
  88.     suspend();
  89.  
  90.     if (graphicsStart() == False)
  91.     {
  92.     strcpy(errorMsg,grapherrormsg(graphresult()));
  93.     strcat( errorMsg,"." );
  94.     messageBox(errorMsg, mfError | mfOKButton);
  95.     }
  96.     else
  97.     {
  98.     maxX = getmaxx();
  99.     maxY = getmaxy();
  100.     outtextxy(0, (maxY - textheight("M")),
  101.         "Press any key to return...");
  102.     setviewport(0, 0, maxX - 1, (maxY - (textheight("M") + 5)), CLIP_ON);
  103.     do {
  104.         color = random(getmaxcolor()) + 1;
  105.         setfillstyle(random(11) + 1, color);
  106.         setcolor(color);
  107.         for(i = 0; i < maxPts; ++i)
  108.         {
  109.         poly[i].x = random(maxX);
  110.         poly[i].y = random(maxY);
  111.         }
  112.         fillpoly(maxPts, (int *) poly);
  113.         event.getKeyEvent();
  114.        } while (event.what == evNothing);
  115.     graphicsStop();
  116.     }
  117.  
  118.     resume();
  119. }
  120.  
  121. void TBGIApp::setBGIPath()
  122. {
  123.     char s[MAXPATH];
  124.  
  125.     TDialog *d = new TDialog(TRect(0,0,35,8), "Path to BGI Files");
  126.     d->options |= ofCentered;
  127.  
  128.     // Buttons
  129.     d->insert(new TButton( TRect(23,5,33,7), "Cancel", cmCancel, bfNormal) );
  130.     d->insert(new TButton( TRect(12,5,22,7),"O~K~", cmOK, bfDefault) );
  131.  
  132.     // Input line, history list and label 
  133.     TInputLine *pathInput = new TInputLine( TRect(3,3,30,4), 68 );
  134.     d->insert( pathInput );
  135.     d->insert( new THistory(TRect(30,3,33,4), pathInput, hlSetBGIPath) );
  136.  
  137.     fexpand(bgiPath);
  138.     strcpy(s,bgiPath);
  139.     d->setData(s);
  140.     d = (TDialog *) validView(d);
  141.     if (d != NULL)
  142.     {
  143.     if (deskTop->execView(d) == cmOK)
  144.         {
  145.         d->getData(s);
  146.         delete bgiPath;
  147.         if ( (strlen(s) > 0) && (s[strlen(s)-1] != '\\') )
  148.         strcat(s,"\\");
  149.         bgiPath = new char [sizeof(s)];
  150.         strcpy(bgiPath, s);
  151.         if ( graphAppInit(appDriver, appMode, bgiPath, True) == False )
  152.         messageBox("Cannot load graphics driver.", mfError | mfOKButton);
  153.         }
  154.     destroy( d );
  155.     }
  156. }
  157.  
  158. void TBGIApp::changeDir()
  159. {
  160.     TView *p;
  161.  
  162.     p = validView(new TChDirDialog(0, hlChangeDir));
  163.     if (p != NULL)
  164.     {
  165.     deskTop->execView(p);
  166.     destroy( p );
  167.     }
  168. }
  169.  
  170. void TBGIApp::tile()
  171. {
  172.     TRect r(deskTop->getExtent());
  173.     deskTop->tile(r);
  174. }
  175.  
  176. void TBGIApp::cascade()
  177. {
  178.     TRect r(deskTop->getExtent());
  179.     deskTop->cascade(r);
  180. }
  181.  
  182. void TBGIApp::handleEvent(TEvent& event)
  183. {
  184.     TApplication::handleEvent(event);
  185.     switch (event.what)
  186.     {
  187.     case evCommand:
  188.     switch (event.message.command)
  189.         {
  190.         case cmNewWin:
  191.         newWin();
  192.         break;
  193.         case cmChangeDir:
  194.         changeDir();
  195.         break;
  196.         case cmSetBGIPath:
  197.         setBGIPath();
  198.         break;
  199.         case cmDoGraphics:
  200.         doGraphics();
  201.         break;
  202.         case cmTile:
  203.         tile();
  204.         break;
  205.         case cmCascade:
  206.         cascade();
  207.         break;
  208.         default :
  209.         return;
  210.  
  211.         }
  212.     break;
  213.     default:
  214.         return;
  215.     }
  216.     clearEvent(event);
  217. }
  218.  
  219. TMenuBar *TBGIApp::initMenuBar(TRect r)
  220. {
  221.  
  222.     r.b.y = r.a.y + 1;
  223.  
  224.     return new TMenuBar( r,
  225.        *new TSubMenu( "~T~est", hcNoContext ) +
  226.     //*new TMenuItem( "~B~GI settings...", 0, kbNoKey, hcNoContext ) +
  227.     *new TMenuItem( "~G~raph", cmDoGraphics, kbAltF5, hcNoContext, "Alt-F5" ) +
  228.     *new TMenuItem( "~S~et BGI path...", cmSetBGIPath,kbNoKey, hcNoContext ) +
  229.          newLine() +
  230.     *new TMenuItem( "~C~hange dir...", cmChangeDir, kbNoKey, hcNoContext ) +
  231.     *new TMenuItem( "E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X" ) +
  232.     *new TSubMenu( "~W~indows", hcNoContext ) +
  233.     *new TMenuItem( "~S~ize/move", cmResize, kbCtrlF5, hcNoContext, "Ctrl-F5" ) +
  234.     *new TMenuItem( "~Z~oom", cmZoom, kbF5, hcNoContext, "F5" ) +
  235.     *new TMenuItem( "~T~ile", cmTile, kbNoKey, hcNoContext ) +
  236.     *new TMenuItem( "C~a~scade", cmCascade, kbNoKey, hcNoContext ) +
  237.     *new TMenuItem( "~N~ext", cmNext, kbF6, hcNoContext, "F6" ) +
  238.     *new TMenuItem( "~P~revious", cmPrev, kbShiftF6, hcNoContext, "Shift-F6" ) +
  239.     *new TMenuItem( "~C~lose",cmClose, kbAltF3, hcNoContext, "Alt-F3" ) +
  240.          newLine() +
  241.     *new TMenuItem( "Add ~w~indow", cmNewWin, kbF4, hcNoContext, "F4" )
  242.     );
  243.  
  244. }
  245.  
  246. TStatusLine *TBGIApp::initStatusLine(TRect r)
  247. {
  248.     r.a.y = r.b.y - 1;
  249.     return new TStatusLine( r,
  250.     *new TStatusDef( 0, 0xFFFF ) +
  251.         *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  252.         *new TStatusItem( "~Alt-F5~ Graph", kbAltF5, cmDoGraphics ) +
  253.         *new TStatusItem( 0, kbF10, cmMenu ) +
  254.         *new TStatusItem( 0, kbAltF3, cmClose ) +
  255.         *new TStatusItem( 0, kbF5, cmZoom ) +
  256.         *new TStatusItem( 0, kbCtrlF5, cmResize ) +
  257.         *new TStatusItem("", kbF6, cmNext)
  258.         );
  259. }
  260.  
  261. void TBGIApp::outOfMemory()
  262. {
  263.     messageBox("Out of memory.", mfError | mfOKButton);
  264. }
  265.  
  266.  
  267. int main()
  268. {
  269.     TBGIApp bgiApp;
  270.  
  271.     bgiApp.run();
  272.     return 0;
  273. }
  274.