home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 9.ddi / TVSRC.ZIP / TPROGRAM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  7.1 KB  |  283 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       tprogram.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TProgram member functions                 */
  6. /*------------------------------------------------------------*/
  7.  
  8. /*------------------------------------------------------------*/
  9. /*                                                            */
  10. /*    Turbo Vision -  Version 1.0                             */
  11. /*                                                            */
  12. /*                                                            */
  13. /*    Copyright (c) 1991 by Borland International             */
  14. /*    All Rights Reserved.                                    */
  15. /*                                                            */
  16. /*------------------------------------------------------------*/
  17.  
  18. #define Uses_TKeys
  19. #define Uses_TProgram
  20. #define Uses_TEvent
  21. #define Uses_TScreen
  22. #define Uses_TStatusLine
  23. #define Uses_TMenu
  24. #define Uses_TGroup
  25. #define Uses_TDeskTop
  26. #define Uses_TEventQueue
  27. #define Uses_TMenuBar
  28. #define Uses_TStatusDef
  29. #define Uses_TStatusItem
  30. #include <tv.h>
  31.  
  32. // Public variables
  33.  
  34. TStatusLine * near TProgram::statusLine = 0;
  35. TMenuBar * near TProgram::menuBar = 0;
  36. TDeskTop * near TProgram::deskTop = 0;
  37. TProgram * near TProgram::application = 0;
  38. int near TProgram::appPalette = apColor;
  39. TEvent near TProgram::pending;
  40.  
  41. extern TPoint shadowSize;
  42.  
  43. TProgInit::TProgInit( TStatusLine *(*cStatusLine)( TRect ),
  44.                             TMenuBar *(*cMenuBar)( TRect ),
  45.                             TDeskTop *(*cDeskTop )( TRect )
  46.                           ) :
  47.     createStatusLine( cStatusLine ),
  48.     createMenuBar( cMenuBar ),
  49.     createDeskTop( cDeskTop )
  50. {
  51. }
  52.  
  53.  
  54. TProgram::TProgram() :
  55.     TProgInit( &TProgram::initStatusLine,
  56.                   &TProgram::initMenuBar,
  57.                   &TProgram::initDeskTop
  58.                 ),
  59.     TGroup( TRect( 0,0,TScreen::screenWidth,TScreen::screenHeight ) )
  60. {
  61.     application = this;
  62.     initScreen();
  63.     state = sfVisible | sfSelected | sfFocused | sfModal | sfExposed;
  64.     options = 0;
  65.     buffer = (ushort far *)TScreen::screenBuffer;
  66.  
  67.     if( createDeskTop != 0 &&
  68.         (deskTop = createDeskTop( getExtent() )) != 0
  69.       )
  70.         insert(deskTop);
  71.  
  72.     if( createStatusLine != 0 &&
  73.         (statusLine = createStatusLine( getExtent() )) != 0
  74.       )
  75.         insert(statusLine);
  76.  
  77.     if( createMenuBar != 0 &&
  78.         (menuBar = createMenuBar( getExtent() )) != 0
  79.       )
  80.         insert(menuBar);
  81.  
  82. }
  83.  
  84. TProgram::~TProgram()
  85. {
  86.     application = 0;
  87. }
  88.  
  89. void TProgram::shutDown()
  90. {
  91.     statusLine = 0;
  92.     menuBar = 0;
  93.     deskTop = 0;
  94.     TGroup::shutDown();
  95. }
  96.  
  97. inline Boolean hasMouse( TView *p, void *s )
  98. {
  99.     return Boolean( (p->state & sfVisible) != 0 &&
  100.                      p->mouseInView( ((TEvent *)s)->mouse.where ));
  101. }
  102.  
  103. void TProgram::getEvent(TEvent& event)
  104. {
  105.     if( pending.what != evNothing )
  106.         {
  107.         event = pending;
  108.         pending.what = evNothing;
  109.         }
  110.     else
  111.         {
  112.         event.getMouseEvent();
  113.         if( event.what == evNothing )
  114.             {
  115.             event.getKeyEvent();
  116.             if( event.what == evNothing )
  117.                 idle();
  118.             }
  119.         }
  120.  
  121.     if( statusLine != 0 )
  122.         {
  123.         if( (event.what & evKeyDown) != 0 ||
  124.             ( (event.what & evMouseDown) != 0 &&
  125.               firstThat( hasMouse, &event ) == statusLine
  126.             )
  127.           )
  128.             statusLine->handleEvent( event );
  129.         }
  130. }
  131.  
  132. TPalette& TProgram::getPalette() const
  133. {
  134.     static TPalette color ( cpColor, sizeof( cpColor )-1 );
  135.     static TPalette blackwhite( cpBlackWhite, sizeof( cpBlackWhite )-1 );
  136.     static TPalette monochrome( cpMonochrome, sizeof( cpMonochrome )-1 );
  137.     static TPalette *palettes[] =
  138.         {
  139.         &color,
  140.         &blackwhite,
  141.         &monochrome
  142.         };
  143.     return *(palettes[appPalette]);
  144. }
  145.  
  146. void TProgram::handleEvent( TEvent& event )
  147. {
  148.     if( event.what == evKeyDown )
  149.         {
  150.         char c = getAltChar( event.keyDown.keyCode );
  151.         if( c >= '1' && c <= '9' )
  152.             {
  153.             if( message( deskTop,
  154.                          evBroadcast,
  155.                          cmSelectWindowNum,
  156.                          (void *)(c - '0')
  157.                        ) != 0 )
  158.                 clearEvent( event );
  159.             }
  160.         }
  161.  
  162.     TGroup::handleEvent( event );
  163.     if( event.what == evCommand && event.message.command == cmQuit )
  164.         {
  165.         endModal( cmQuit );
  166.         clearEvent( event );
  167.         }
  168. }
  169.  
  170. void TProgram::idle()
  171. {
  172.     if( statusLine != 0 )
  173.         statusLine->update();
  174.  
  175.     if( commandSetChanged == True )
  176.         {
  177.         message( this, evBroadcast, cmCommandSetChanged, 0 );
  178.         commandSetChanged = False;
  179.         }
  180. }
  181.  
  182. TDeskTop *TProgram::initDeskTop( TRect r )
  183. {
  184.     r.a.y++;
  185.     r.b.y--;
  186.     return new TDeskTop( r );
  187. }
  188.  
  189. TMenuBar *TProgram::initMenuBar( TRect r )
  190. {
  191.     r.b.y = r.a.y + 1;
  192.     return new TMenuBar( r, (TMenu *)0 );
  193. }
  194.  
  195. void TProgram::initScreen()
  196. {
  197.     if( (TScreen::screenMode & 0x00FF) != TDisplay::smMono )
  198.         {
  199.         if( (TScreen::screenMode & TDisplay::smFont8x8) != 0 )
  200.             shadowSize.x = 1;
  201.         else
  202.             shadowSize.x = 2;
  203.         shadowSize.y = 1;
  204.         showMarkers = False;
  205.         if( (TScreen::screenMode & 0x00FF) == TDisplay::smBW80 )
  206.             appPalette = apBlackWhite;
  207.         else
  208.             appPalette = apColor;
  209.         }
  210.     else
  211.         {
  212.  
  213.         shadowSize.x = 0;
  214.         shadowSize.y = 0;
  215.         showMarkers = True;
  216.         appPalette = apMonochrome;
  217.         }
  218. }
  219.  
  220. TStatusLine *TProgram::initStatusLine( TRect r )
  221. {
  222.     r.a.y = r.b.y - 1;
  223.     return new TStatusLine( r,
  224.         *new TStatusDef( 0, 0xFFFF ) +
  225.             *new TStatusItem( exitText, kbAltX, cmQuit ) +
  226.             *new TStatusItem( 0, kbF10, cmMenu ) +
  227.             *new TStatusItem( 0, kbAltF3, cmClose ) +
  228.             *new TStatusItem( 0, kbF5, cmZoom ) +
  229.             *new TStatusItem( 0, kbCtrlF5, cmResize )
  230.             );
  231. }
  232.  
  233. void TProgram::outOfMemory()
  234. {
  235. }
  236.  
  237. void TProgram::putEvent( TEvent & event )
  238. {
  239.     pending = event;
  240. }
  241.  
  242. void TProgram::run()
  243. {
  244.     execute();
  245. }
  246.  
  247. void TProgram::setScreenMode( ushort mode )
  248. {
  249.     TRect  r;
  250.  
  251.     TEventQueue::mouse.hide(); //HideMouse();
  252.     TScreen::setVideoMode( mode );
  253.     initScreen();
  254.     buffer = (ushort far *)TScreen::screenBuffer;
  255.     r = TRect( 0, 0, TScreen::screenWidth, TScreen::screenHeight );
  256.     changeBounds( r );
  257.     setState(sfExposed, False);
  258.     redraw();
  259.     setState(sfExposed, True);
  260.     TEventQueue::mouse.show(); //ShowMouse();
  261. }
  262.  
  263. TView* TProgram::validView(TView* p)
  264. {
  265.     if( p == 0 )
  266.         return 0;
  267.     if( lowMemory() )
  268.         {
  269.         destroy( p );
  270.         outOfMemory();
  271.         return 0;
  272.         }
  273.     if( !p->valid( cmValid ) )
  274.         {
  275.         destroy( p );
  276.         return 0;
  277.         }
  278.     return p;
  279. }
  280.  
  281.  
  282.  
  283.