home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / TProgram.cpp < prev    next >
C/C++ Source or Header  |  1998-07-21  |  8KB  |  349 lines

  1. /*
  2.  * TProgram.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #define Uses_TKeys
  13. #define Uses_TProgram
  14. #define Uses_TEvent
  15. #define Uses_TScreen
  16. #define Uses_TStatusLine
  17. #define Uses_TMenu
  18. #define Uses_TGroup
  19. #define Uses_TDeskTop
  20. #define Uses_TEventQueue
  21. #define Uses_TMenuBar
  22. #define Uses_TStatusDef
  23. #define Uses_TStatusItem
  24. #define Uses_TDialog
  25. #ifndef __UNPATCHED
  26. #define Uses_TVMemMgr
  27. #endif
  28. #include <tvision/tv.h>
  29.  
  30. // Public variables
  31.  
  32. TStatusLine * TProgram::statusLine = 0;
  33. TMenuBar * TProgram::menuBar = 0;
  34. TDeskTop * TProgram::deskTop = 0;
  35. TProgram * TProgram::application = 0;
  36. int TProgram::appPalette = apColor;
  37. TEvent TProgram::pending;
  38.  
  39. extern TPoint shadowSize;
  40.  
  41. TProgInit::TProgInit( TStatusLine *(*cStatusLine)( TRect ),
  42.                             TMenuBar *(*cMenuBar)( TRect ),
  43.                             TDeskTop *(*cDeskTop )( TRect )
  44.                           ) :
  45.     createStatusLine( cStatusLine ),
  46.     createMenuBar( cMenuBar ),
  47.     createDeskTop( cDeskTop )
  48. {
  49. }
  50.  
  51.  
  52. TProgram::TProgram() :
  53.     TProgInit( &TProgram::initStatusLine,
  54.                   &TProgram::initMenuBar,
  55.                   &TProgram::initDeskTop
  56.                 ),
  57.     TGroup( TRect( 0,0,TScreen::screenWidth,TScreen::screenHeight ) )
  58. {
  59.     application = this;
  60.     initScreen();
  61.     state = sfVisible | sfSelected | sfFocused | sfModal | sfExposed;
  62.     options = 0;
  63.     buffer = TScreen::screenBuffer;
  64.  
  65.     if( createDeskTop != 0 &&
  66.         (deskTop = createDeskTop( getExtent() )) != 0
  67.       )
  68.         insert(deskTop);
  69.  
  70.     if( createStatusLine != 0 &&
  71.         (statusLine = createStatusLine( getExtent() )) != 0
  72.       )
  73.         insert(statusLine);
  74.  
  75.     if( createMenuBar != 0 &&
  76.         (menuBar = createMenuBar( getExtent() )) != 0
  77.       )
  78.         insert(menuBar);
  79.  
  80. }
  81.  
  82. TProgram::~TProgram()
  83. {
  84.     application = 0;
  85. }
  86.  
  87. void TProgram::shutDown()
  88. {
  89.     statusLine = 0;
  90.     menuBar = 0;
  91.     deskTop = 0;
  92.     TGroup::shutDown();
  93. #ifndef __UNPATCHED
  94.     TVMemMgr::clearSafetyPool();    // Release the safety pool buffer.
  95. #endif
  96. }
  97.  
  98. Boolean TProgram::canMoveFocus()
  99. {
  100.     return deskTop->valid(cmReleasedFocus);
  101. }
  102.  
  103. ushort TProgram::executeDialog( TDialog* pD, void* data )
  104. {
  105.     ushort c=cmCancel;
  106.  
  107.     if (validView(pD))
  108.         {
  109.         if (data)
  110.         pD->setData(data);
  111.         c = deskTop->execView(pD);
  112.         if ((c != cmCancel) && (data))
  113.             pD->getData(data);
  114.         destroy(pD);
  115.         }
  116.  
  117.     return c;
  118. }
  119.  
  120. /*
  121.  * This patch works around a "GOT relocation burb" under FreeBSD.  The actual
  122.  * bug is in gcc-2.7.2.1, but there is no easy fix for it there.
  123.  * Patch from: John Polstra <jdp@polstra.com>
  124.  * Date: Wed, 28 May 1997 22:08:59 -0700
  125.  */
  126. #ifdef __FreeBSD__
  127. static Boolean hasMouse( TView *p, void *s )
  128. #else
  129. inline Boolean hasMouse( TView *p, void *s )
  130. #endif
  131. {
  132.     return Boolean( (p->state & sfVisible) != 0 &&
  133.                      p->mouseInView( ((TEvent *)s)->mouse.where ));
  134. }
  135.  
  136. void TProgram::getEvent(TEvent& event)
  137. {
  138.     if( pending.what != evNothing )
  139.         {
  140.         event = pending;
  141.         pending.what = evNothing;
  142.         }
  143.     else
  144.         {
  145.         /* SS: changed */
  146.  
  147.         TScreen::getEvent(event);
  148.         if (event.what == evCommand) switch (event.message.command)
  149.         {
  150.         case cmSysRepaint:
  151.             /*
  152.              * This command redraws all the screen.  Useful
  153.              * when the user restart the process after a ctrl-z.
  154.              */
  155.             redraw();
  156.             clearEvent(event);
  157.             break;
  158.         case cmSysResize:
  159.             /*
  160.              * Generated after a SIGWINCH signal.
  161.              */
  162.             buffer = TScreen::screenBuffer;
  163.             changeBounds(TRect(0, 0, TScreen::screenWidth,
  164.                 TScreen::screenHeight));
  165.             setState(sfExposed, False);
  166.             setState(sfExposed, True);
  167.             redraw();
  168.             clearEvent(event);
  169.             break;
  170.         case cmSysWakeup:
  171.             idle();
  172.             clearEvent(event);
  173.         }
  174.         }
  175.  
  176.     if( statusLine != 0 )
  177.         {
  178.         if( (event.what & evKeyDown) != 0 ||
  179.             ( (event.what & evMouseDown) != 0 &&
  180.               firstThat( hasMouse, &event ) == statusLine
  181.             )
  182.           )
  183.             statusLine->handleEvent( event );
  184.         }
  185. }
  186.  
  187. TPalette& TProgram::getPalette() const
  188. {
  189.     static TPalette color ( cpAppColor, sizeof( cpAppColor )-1 );
  190.     static TPalette blackwhite(cpAppBlackWhite, sizeof( cpAppBlackWhite )-1 );
  191.     static TPalette monochrome(cpAppMonochrome, sizeof( cpAppMonochrome )-1 );
  192.     static TPalette *palettes[] =
  193.         {
  194.         &color,
  195.         &blackwhite,
  196.         &monochrome
  197.         };
  198.     return *(palettes[appPalette]);
  199. }
  200.  
  201. void TProgram::handleEvent( TEvent& event )
  202. {
  203.     if( event.what == evKeyDown )
  204.         {
  205.         char c = getAltChar( event.keyDown.keyCode );
  206.         if( c >= '1' && c <= '9' )
  207.             {
  208. #ifndef __UNPATCHED
  209.             if(canMoveFocus())      //<--- Check valid first.
  210.             {
  211.                 if( message(deskTop, evBroadcast, cmSelectWindowNum,
  212.                   (void *)(c - '0')) != 0 )
  213.                     clearEvent( event );
  214.             }
  215.             else
  216.                 clearEvent(event);
  217. #else
  218.             if( message( deskTop,
  219.                          evBroadcast,
  220.                          cmSelectWindowNum,
  221.                          (void *)(c - '0')
  222.                        ) != 0 )
  223.                 clearEvent( event );
  224. #endif
  225.             }
  226.         }
  227.     TGroup::handleEvent( event );
  228.     if( event.what == evCommand && event.message.command == cmQuit )
  229.         {
  230.         endModal( cmQuit );
  231.         clearEvent( event );
  232.         }
  233. }
  234.  
  235. void TProgram::idle()
  236. {
  237.     if( statusLine != 0 )
  238.         statusLine->update();
  239.  
  240.     if( commandSetChanged == True )
  241.         {
  242.         message( this, evBroadcast, cmCommandSetChanged, 0 );
  243.         commandSetChanged = False;
  244.         }
  245. }
  246.  
  247. TDeskTop *TProgram::initDeskTop( TRect r )
  248. {
  249.     r.a.y++;
  250.     r.b.y--;
  251.     return new TDeskTop( r );
  252. }
  253.  
  254. TMenuBar *TProgram::initMenuBar( TRect r )
  255. {
  256.     r.b.y = r.a.y + 1;
  257.     return new TMenuBar( r, (TMenu *)0 );
  258. }
  259.  
  260. void TProgram::initScreen()
  261. {
  262.     if( (TScreen::screenMode & 0x00FF) != TDisplay::smMono )
  263.         {
  264.         if( (TScreen::screenMode & TDisplay::smFont8x8) != 0 )
  265.             shadowSize.x = 1;
  266.         else
  267.             shadowSize.x = 2;
  268.         shadowSize.y = 1;
  269.         showMarkers = False;
  270.         if( (TScreen::screenMode & 0x00FF) == TDisplay::smBW80 )
  271.             appPalette = apBlackWhite;
  272.         else
  273.             appPalette = apColor;
  274.         }
  275.     else
  276.         {
  277.  
  278.         shadowSize.x = 0;
  279.         shadowSize.y = 0;
  280.         showMarkers = True;
  281.         appPalette = apMonochrome;
  282.         }
  283. }
  284.  
  285. TStatusLine *TProgram::initStatusLine( TRect r )
  286. {
  287.     r.a.y = r.b.y - 1;
  288.     return new TStatusLine( r,
  289.         *new TStatusDef( 0, 0xFFFF ) +
  290.             *new TStatusItem( exitText, kbAltX, cmQuit ) +
  291.             *new TStatusItem( 0, kbF10, cmMenu ) +
  292.             *new TStatusItem( 0, kbAltF3, cmClose ) +
  293.             *new TStatusItem( 0, kbF5, cmZoom ) +
  294.             *new TStatusItem( 0, kbCtrlF5, cmResize )
  295.             );
  296. }
  297.  
  298. TWindow* TProgram::insertWindow(TWindow* pWin)
  299. {
  300.     if (validView(pWin))
  301.         if (canMoveFocus())
  302.             {
  303.             deskTop->insert(pWin);
  304.             return pWin;
  305.             }
  306.         else
  307.             destroy(pWin);
  308.  
  309.    return NULL;
  310. }
  311.  
  312.  
  313. void TProgram::outOfMemory()
  314. {
  315. }
  316.  
  317. void TProgram::putEvent( TEvent & event )
  318. {
  319.     pending = event;
  320. }
  321.  
  322. void TProgram::run()
  323. {
  324.     execute();
  325. }
  326.  
  327. void TProgram::setScreenMode( ushort mode )
  328. {
  329.     /* SS: code removed */
  330. }
  331.  
  332. TView* TProgram::validView(TView* p)
  333. {
  334.     if( p == 0 )
  335.         return 0;
  336.     if( lowMemory() )
  337.         {
  338.         destroy( p );
  339.         outOfMemory();
  340.         return 0;
  341.         }
  342.     if( !p->valid( cmValid ) )
  343.         {
  344.         destroy( p );
  345.         return 0;
  346.         }
  347.     return p;
  348. }
  349.