home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / tvpal / example.cpp next >
Encoding:
C/C++ Source or Header  |  1992-03-09  |  8.7 KB  |  326 lines

  1. //
  2. // Streamable Palette Example
  3. //
  4. // Copyright (c) 1992 CC Software, Inc.
  5. //
  6. //
  7. // REQUIRED FILES:
  8. //
  9. //      EXAMPLE.CPP
  10. //      TVPAL.CPP
  11. //      TVPAL.H
  12. //
  13. // This example program demonstrates the way the class TStreamPalette
  14. // can be used to store the application palette on a stream.  The
  15. // stream file, PALETTE.STM, is checked for existence.  If it exists,
  16. // the application palette is read from the stream using the class
  17. // TStreamPalette.  Normal operation follows.  Upon exit of the app
  18. // the palette is stored back on the stream.
  19. //
  20. // NOTES:
  21. //
  22. // Although the application palette is being stored here, ANY object
  23. // that uses a palette can make use of the TStreamPalette class as
  24. // a way of write/reading the object palette with fpstreams.
  25. //
  26.  
  27. #define Uses_TKeys
  28. #define Uses_TApplication
  29. #define Uses_TDeskTop
  30. #define Uses_TMenuBar
  31. #define Uses_TSubMenu
  32. #define Uses_TMenuItem
  33. #define Uses_TColorDialog
  34. #define Uses_TColorGroup
  35. #define Uses_TColorItem
  36. #define Uses_TStreamableClass
  37. #define Uses_TResourceCollection
  38. #define Uses_TResourceFile
  39. #define Uses_fpstream
  40. #include <tv.h>
  41. #include "TVPal.h"
  42.  
  43. __link(RStreamPalette);         // Link in the stream manager record
  44.                                 // from TVPAL.CPP.
  45.  
  46. __link(RResourceCollection);    // Link in the stream manager record
  47.                                 // for class TResourceCollection
  48.  
  49.  
  50. // Application Commands //
  51.  
  52. const int cmColorCmd    = 109;
  53.  
  54.  
  55. // Palette File Name //
  56.  
  57. #define RESOURCE   "example.rez"
  58.  
  59.  
  60. // Class TApp //
  61.  
  62. // Description --------------------------------------------------------
  63. //
  64. //      TApp provides the application class for the program.
  65. //
  66. // Member functions
  67. //
  68. //      initDeskTop
  69. //
  70. //      Creates a TDeskTop instance after reading the application
  71. //      palette from the palette file (if it exists).
  72. //
  73. //      initMenuBar
  74. //
  75. //      Initalizes the menu bar for the application operations.
  76. //
  77. //      handleEvent
  78. //
  79. //      Handles the application commands.
  80. //
  81. //      colors
  82. //
  83. //      Opens the TColorDialog instance to allow the user to change the
  84. //      application color palette.  Since the palette is saved during
  85. //      it's destructor, the colors will be persistent between program
  86. //      runs.
  87. //
  88. // END Description ----------------------------------------------------
  89.  
  90. class TApp : public TApplication {
  91. public:
  92.        TApp();
  93.        ~TApp();
  94.  
  95. protected:
  96.        static TDeskTop    *initDeskTop( TRect );
  97.        static TMenuBar    *initMenuBar( TRect );
  98.  
  99.        virtual void handleEvent( TEvent& );
  100.        virtual void colors();
  101. };
  102. // END TApp ===========================================================
  103.  
  104.  
  105.  
  106.  
  107. // Constructor //
  108.  
  109. TApp::TApp() : TApplication(), TProgInit( TApp::initStatusLine,
  110.                                           TApp::initMenuBar,
  111.                                           TApp::initDeskTop )
  112. // Summary ------------------------------------------------------------
  113. //
  114. //      Creates the application object.
  115. //
  116. // END Summary --------------------------------------------------------
  117. {
  118. };
  119. // --------------------------------------------------------------------
  120.  
  121.  
  122.  
  123. // Destructor //
  124.  
  125. TApp::~TApp()
  126. // Summary ------------------------------------------------------------
  127. //
  128. //      Destructor for the TApp class.  Here we attempt to write the
  129. //  application palette to the stream file.  Normal stream operation
  130. //  would need some kludgy code to do this with TPalette.  The class
  131. //  TStreamPalette makes for an ellegant solution for both reading and
  132. //  writing TPalette objects.
  133. //
  134. // END Summary --------------------------------------------------------
  135. {
  136.   fpstream *fp = new fpstream( RESOURCE, ios::out | ios::binary );
  137.   TStreamPalette *sp = new TStreamPalette( getPalette() );
  138.   TResourceFile *res;
  139.  
  140.   if ( fp->good() )
  141.   {
  142.      res = new TResourceFile( fp );
  143.      if ( res )
  144.         res->put( sp, "Palette" );
  145.   }
  146.   if ( sp ) delete sp;
  147.   if ( res ) destroy( res );
  148. }
  149. // --------------------------------------------------------------------
  150.  
  151.  
  152.  
  153.  
  154. // Member funciton //
  155.  
  156. TMenuBar *TApp::initMenuBar( TRect r )
  157. // Summary ------------------------------------------------------------
  158. //
  159. //      Creates the applicaiton menu bar.
  160. //
  161. // Parameters
  162. //
  163. //      r
  164. //
  165. //      A TRect object containing the bounds of the display.
  166. //
  167. // Return Value
  168. //
  169. //      Returns a TMenuBar object.
  170. //
  171. // END Summary --------------------------------------------------------
  172. {
  173.   r.b.y = r.a.y + 1;
  174.   return new TMenuBar( r,
  175.               *new TSubMenu( "~F~ile", kbAltF ) +
  176.               *new TMenuItem( "E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X" ) +
  177.               *new TSubMenu( "~O~ptions", kbAltO ) +
  178.               *new TMenuItem( "~C~olors", cmColorCmd, kbNoKey, hcNoContext )
  179.              );
  180. };
  181. // --------------------------------------------------------------------
  182.  
  183.  
  184.  
  185.  
  186. // Member function //
  187.  
  188. TDeskTop *TApp::initDeskTop( TRect bounds )
  189. {
  190.   fpstream *fp = new fpstream( RESOURCE, ios::nocreate | ios::in | ios::binary );
  191.   TResourceFile *res;
  192.   TStreamPalette *sp;
  193.  
  194.   if ( fp->good() )
  195.   {
  196.      res = new TResourceFile( fp );
  197.      if ( res )
  198.      {
  199.         sp = (TStreamPalette *)res->get( "Palette" );
  200.         if ( sp )
  201.         {
  202.            application->getPalette() = *sp;
  203.            delete sp;
  204.         };
  205.         delete res;
  206.      }
  207.      else delete fp;
  208.   };
  209.   bounds.grow( 0, -1 );
  210.   return new TDeskTop( bounds );
  211. };
  212. // --------------------------------------------------------------------
  213.  
  214.  
  215.  
  216.  
  217. // Member function //
  218.  
  219. void TApp::handleEvent( TEvent& event )
  220. {
  221.   TApplication::handleEvent( event );
  222.   if ( event.what == evCommand )
  223.      switch ( event.message.command )
  224.      {
  225.             case cmColorCmd:
  226.                  colors();
  227.                  clearEvent( event );
  228.                  return;
  229.      };
  230.   clearEvent( event );
  231. };
  232. // --------------------------------------------------------------------
  233.  
  234.  
  235.  
  236.  
  237. // Member function //
  238.  
  239. void TApp::colors()
  240. {
  241.     TColorGroup &group1 =
  242.         *new TColorGroup("Desktop") +
  243.             *new TColorItem("Color",             1)+
  244.  
  245.         *new TColorGroup("Menus") +
  246.             *new TColorItem("Normal",            2)+
  247.             *new TColorItem("Disabled",          3)+
  248.             *new TColorItem("Shortcut",          4)+
  249.             *new TColorItem("Selected",          5)+
  250.             *new TColorItem("Selected disabled", 6)+
  251.             *new TColorItem("Shortcut selected", 7);
  252.  
  253.     TColorGroup &group2 =
  254.         *new TColorGroup("Dialogs") +
  255.             *new TColorItem("Frame/background",  33)+
  256.             *new TColorItem("Frame icons",       34)+
  257.             *new TColorItem("Scroll bar page",   35)+
  258.             *new TColorItem("Scroll bar icons",  36)+
  259.             *new TColorItem("Static text",       37)+
  260.  
  261.             *new TColorItem("Label normal",      38)+
  262.             *new TColorItem("Label selected",    39)+
  263.             *new TColorItem("Label shortcut",    40);
  264.  
  265.     TColorItem &item_coll1 =
  266.         *new TColorItem("Button normal",     41)+
  267.         *new TColorItem("Button default",    42)+
  268.         *new TColorItem("Button selected",   43)+
  269.         *new TColorItem("Button disabled",   44)+
  270.         *new TColorItem("Button shortcut",   45)+
  271.         *new TColorItem("Button shadow",     46)+
  272.         *new TColorItem("Cluster normal",    47)+
  273.         *new TColorItem("Cluster selected",  48)+
  274.         *new TColorItem("Cluster shortcut",  49);
  275.  
  276.     TColorItem &item_coll2 =
  277.         *new TColorItem("Input normal",      50)+
  278.         *new TColorItem("Input selected",    51)+
  279.         *new TColorItem("Input arrow",       52)+
  280.  
  281.         *new TColorItem("History button",    53)+
  282.         *new TColorItem("History sides",     54)+
  283.         *new TColorItem("History bar page",  55)+
  284.         *new TColorItem("History bar icons", 56)+
  285.  
  286.         *new TColorItem("List normal",       57)+
  287.         *new TColorItem("List focused",      58)+
  288.         *new TColorItem("List selected",     59)+
  289.         *new TColorItem("List divider",      60)+
  290.  
  291.         *new TColorItem("Information pane",  61
  292.         );
  293.  
  294.      group2 = group2 + item_coll1 + item_coll2;
  295.  
  296.     TColorGroup &group3 = group1 + group2;
  297.  
  298.     TColorDialog *c = new TColorDialog(&getPalette(), &group3 );
  299.  
  300.     c->helpCtx = hcNoContext;
  301.     if( validView( c ) != 0 )
  302.     {
  303.       c->setData(&getPalette());
  304.       if( deskTop->execView( c ) != cmCancel )
  305.       {
  306.         getPalette() = *(c->pal);
  307.         deskTop->redraw();
  308.         redraw();
  309.       }
  310.       destroy( c );
  311.     }
  312. };
  313. // --------------------------------------------------------------------
  314.  
  315.  
  316.  
  317.  
  318. // Main program //
  319.  
  320. int main()
  321. {
  322.   TApp  app;
  323.   app.run();
  324.   return 0;
  325. };
  326.