home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / dos / listz21s.exe / LZSET10S.RAR / LZS_CLRS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-25  |  8.1 KB  |  364 lines

  1. /*
  2.  * This file is part of LZSETUP (Configuration program for Listerz)
  3.  *
  4.  * Copyright (c) 1997 Branislav L. Slantchev (gargoyle)
  5.  * A fine product of Silicon Creations, Inc.
  6.  *
  7.  * This file is released under the terms and conditions of the GNU
  8.  * General Public License Version 2. The full text of the license is
  9.  * supplied in the Copying.Doc file included with this archive. This
  10.  * free software comes with absolutely no warranty, as outlined in the
  11.  * licensing text. You are not allowed to remove this copyright notice.
  12.  *
  13.  * Contact: Branislav L. Slantchev at 73023.262@compuserve.com
  14. */
  15. #include <stdio.h>
  16. #include <opcrt.h>
  17. #include <opwindow.h>
  18. #include "geometry.h"
  19.  
  20. extern ColorSet         lz_Colors;
  21. extern boolean          lz_Dirty;
  22. extern FrameArray       lz_FrameType;
  23. extern CommandProcessor lz_Commands;
  24.  
  25. #define ccToggleBlink   201
  26. #define ModuleCode      40
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // color selector class
  30. /////////////////////////////////////////////////////////////////////////////
  31. class ColorSelector: public CommandWindow
  32. {
  33.     byte     m_MaxColor;
  34.     byte     m_MaxBg;
  35.     byte     m_Width;
  36.     byte     m_Color;
  37.     byte     m_BoxColor;
  38.     byte     m_BoxMono;
  39.     byte     m_BoxAttr;
  40.     byte     m_index;
  41.     boolean  m_blink;
  42.     // THE SELECTOR VALUES
  43.     zRect    m_rect;
  44.     boolean  m_selInited;
  45.     char     m_selBuf[50];
  46.  
  47.     static const char  m_BoxChars[3][3];
  48.     static const byte  m_Height;
  49.     static const char *m_ColorChar;
  50.     static const char  m_BlinkChar;
  51.  
  52. public:
  53.     boolean      Init(byte X1, byte Y1);
  54.     boolean      InitCustom(byte X1, byte Y1, ColorSet &Colors, long Options);
  55.     void         SetColor(byte Color);
  56.     byte         GetColor();
  57.  
  58. private:
  59.     virtual void UpdateContents();
  60.     virtual void ProcessSelf();
  61.     void         SetBoxAttr(byte color, byte mMono);
  62.     void         ColorToPos(byte attr, byte &row, byte &col);
  63.     byte         PosToColor(byte row, byte col);
  64.     void         DrawSelector(byte attr, byte row, byte col);
  65.     void         UpdateSelector(byte attr);
  66. };
  67.  
  68. const char*  ColorSelector::m_ColorChar      = "X";
  69. const char   ColorSelector::m_BlinkChar      = 'B';
  70. const byte   ColorSelector::m_Height         = 10;
  71. const char   ColorSelector::m_BoxChars[3][3] =
  72.                 {
  73.                     {'┌', '─', '┐'},
  74.                     {'│', 'X', '│'},
  75.                     {'└', '─', '┘'}
  76.                 };
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // ColorSelector methods
  80. /////////////////////////////////////////////////////////////////////////////
  81. boolean
  82. ColorSelector::Init(byte col, byte row)
  83. {
  84.     return ColorSelector::InitCustom(col, row, lz_Colors, DefWindowOptions);
  85. }
  86.  
  87. boolean
  88. ColorSelector::InitCustom(byte x1, byte y1, ColorSet &Colors, long Options)
  89. {
  90.     byte    x2, y2;
  91.     boolean retval = FALSE;
  92.  
  93.     m_selInited = FALSE;
  94.     m_blink     = FALSE;
  95.  
  96.     m_MaxColor = 0x7F;
  97.     m_MaxBg    = 0x70;
  98.     m_Width    = 18;
  99.  
  100.     x2 = x1 + m_Width  - 1;
  101.     y2 = y1 + m_Height - 1;
  102.  
  103.     Options &= ~wResizeable;
  104.     Options |= (wClear | wUserContents | wBordered);
  105.  
  106.     lz_Commands.AddCommand(ccToggleBlink, 1, 0x3000, 0);
  107.  
  108.     if( CommandWindow::InitCustom(x1, y1, x2, y2, Colors, Options,
  109.                                   lz_Commands, ModuleCode) )
  110.     {
  111.         wFrame.AddHeader(" 00 ", heBC);
  112.         if( RawError() != 0 )
  113.         {
  114.             Done();
  115.             InitStatus = RawError();
  116.         }
  117.         else
  118.         {
  119.             m_index = wFrame.GetLastHeaderIndex();
  120.             SetBoxAttr(Colors.FrameColor, Colors.FrameMono);
  121.             retval = TRUE;
  122.         }
  123.     }
  124.  
  125.     return retval;
  126. }
  127.  
  128. void
  129. ColorSelector::SetBoxAttr(byte attrColor, byte attrMono)
  130. {
  131.     m_BoxColor = attrColor;
  132.     m_BoxMono  = MapMono(attrColor, attrMono);
  133.     m_BoxAttr  = ColorMono(m_BoxColor, m_BoxMono);
  134. }
  135.  
  136. byte
  137. ColorSelector::PosToColor(byte row, byte col)
  138. {
  139.     row -= wYL;
  140.     col -= wXL;
  141.  
  142.     if(    (row == 0)
  143.         || (col == 0)
  144.         || (row == m_Height - 1)
  145.         || (col == m_Width - 1) )
  146.     {
  147.         return m_BoxAttr;
  148.     }
  149.     else
  150.     {
  151.         return ((row - 1) << 4) + (m_BoxAttr & 0x0F);
  152.     }
  153. }
  154.  
  155. void
  156. ColorSelector::ColorToPos(byte attr, byte &row, byte &col)
  157. {
  158.     col = (wXL + 1) + (attr & 0xF);
  159.     row = (wYL + 1) + (attr >> 4);
  160. }
  161.  
  162. void
  163. ColorSelector::UpdateContents()
  164. {
  165.     byte row, col, attrib;
  166.  
  167.     for( attrib = 0; attrib <= m_MaxColor; attrib++ )
  168.     {
  169.         ColorToPos(attrib, row, col);
  170.         FastWrite((char*)m_ColorChar, row, col, attrib);
  171.     }
  172.  
  173.     StackWindow::UpdateContents();
  174. }
  175.  
  176. void
  177. ColorSelector::SetColor(byte aColor)
  178. {
  179.     m_Color = aColor;
  180.     m_blink = !!(aColor & 0x80);
  181. }
  182.  
  183. byte
  184. ColorSelector::GetColor()
  185. {
  186.     return m_Color | (m_blink ? 0x80 : 0x00);
  187. }
  188.  
  189. void
  190. ColorSelector::DrawSelector(byte attrib, byte row, byte col)
  191. {
  192.     char s[2];
  193.     byte attr;
  194.     int  x, y, rowDelta, colDelta;
  195.  
  196.     s[1] = '\0';  // to make a string later
  197.     for( rowDelta = -1; rowDelta < 2; rowDelta++ )
  198.     {
  199.         y = rowDelta + row;
  200.         for( colDelta = -1; colDelta < 2; colDelta++ )
  201.         {
  202.             s[0] = m_BoxChars[rowDelta+1][colDelta+1];
  203.             x    = colDelta + col;
  204.  
  205.             if( (rowDelta == 0) && (colDelta == 0) )
  206.             {    // leave attrib of X alone
  207.                 attr = attrib;
  208.                 if( m_blink )
  209.                 {    // change to blink and char
  210.                     attr |= 0x80;
  211.                     s[0] = m_BlinkChar;
  212.                 }
  213.             }
  214.             else attr = m_BoxAttr;
  215.  
  216.             FastWrite(s, y, x, attr);
  217.         }
  218.     }
  219. }
  220.  
  221. void
  222. ColorSelector::UpdateSelector(byte attr)
  223. {
  224.     byte     row, col;
  225.     boolean  redraw;
  226.     char     s[20];
  227.     void    *p = m_selBuf;
  228.  
  229.     if( m_selInited )
  230.     {    // not first call, restore last area
  231.         RestoreWindow(m_rect.a.x,m_rect.a.y,m_rect.b.x,m_rect.b.y, FALSE, p);
  232.     }
  233.     else
  234.     {    // first call, mark for restore
  235.         m_selInited = TRUE;
  236.     }
  237.  
  238.     ColorToPos(attr, row, col);
  239.     m_rect.a.x = col - 1;
  240.     m_rect.a.y = row - 1;
  241.     m_rect.b.x = col + 1;
  242.     m_rect.b.y = row + 1;
  243.     SaveWindow(m_rect.a.x, m_rect.a.y, m_rect.b.x, m_rect.b.y, FALSE, p);
  244.     DrawSelector(attr, row, col);
  245.     sprintf(s, " %02X ", attr | (m_blink ? 0x80 : 0x00));
  246.     wFrame.ChangeHeaderString(m_index, s, redraw);
  247.     wFrame.DrawHeader(m_index);
  248. }
  249.  
  250. void
  251. ColorSelector::ProcessSelf()
  252. {
  253.     byte     saveAttr, attr;
  254.     boolean  done, saveBlink;
  255.     void    *p = m_selBuf;
  256.  
  257.     ClearErrors();
  258.     Draw();
  259.     if( RawError() != 0 ) return;
  260.  
  261.     SetCursor(cuHidden);
  262.     done         = FALSE;
  263.     attr         = m_Color & m_MaxColor;
  264.     UpdateSelector(attr);
  265.  
  266.     do
  267.     {
  268.         GetNextCommand();
  269.  
  270.         saveAttr  = attr;
  271.         saveBlink = m_blink;
  272.  
  273.         switch( cwCmd )
  274.         {
  275.             case ccLeft:
  276.                 if( (attr & 0x0F) == 0 ) attr += 0x0F;
  277.                 else attr--;
  278.                 break;
  279.  
  280.             case ccRight:
  281.                 if( (attr & 0x0F) == 0x0F ) attr -= 0x0F;
  282.                 else attr++;
  283.                 break;
  284.  
  285.             case ccUp:
  286.                 if( attr <= 0x0F ) attr += m_MaxBg;
  287.                 else attr -= 0x10;
  288.                 break;
  289.  
  290.             case ccDown:
  291.                 if( attr >= m_MaxBg ) attr -= m_MaxBg;
  292.                 else attr += 0x10;
  293.                 break;
  294.  
  295.             case ccPageUp: attr = attr & 0x0F;            break;
  296.             case ccPageDn: attr = (attr & 0x0F) + m_MaxBg; break;
  297.             case ccHome  : attr = attr & m_MaxBg;          break;
  298.             case ccEnd   : attr = (attr & m_MaxBg) + 0x0F; break;
  299.  
  300.             case ccSelect:
  301.                 m_Color = attr;
  302.                 done = TRUE;
  303.                 break;
  304.  
  305.             case ccQuit:
  306.                 attr = m_Color;
  307.                 done = TRUE;
  308.                 break;
  309.  
  310.             case ccToggleBlink:
  311.                 m_blink = !m_blink;
  312.                 break;
  313.  
  314.             default:
  315.                 if( cwCmd >= ccUser0 )
  316.                 {
  317.                     m_Color = attr;
  318.                     done = TRUE;
  319.                 }
  320.                 else cwCmd = ccNone;
  321.         }
  322.  
  323.         if( attr != saveAttr || m_blink != saveBlink )
  324.         { // draw new choice
  325.             UpdateSelector(attr);
  326.         }
  327.     }while( !done );
  328.  
  329.     RestoreWindow(m_rect.a.x, m_rect.a.y, m_rect.b.x, m_rect.b.y, FALSE, p);
  330. }
  331.  
  332. /////////////////////////////////////////////////////////////////////////////
  333. // the interface function called by the program
  334. /////////////////////////////////////////////////////////////////////////////
  335. void
  336. SelectColor( byte *attrib, char *header )
  337. {
  338.     ColorSelector CS;
  339.     long          options = wBordered | wClear | wUserContents;
  340.  
  341.     if( CS.InitCustom(57, 6, lz_Colors, options) )
  342.     {
  343.         byte newAttrib;
  344.  
  345.         CS.EnableExplosions(15);
  346.         CS.wFrame.SetFrameType(lz_FrameType);
  347.         CS.wFrame.AddHeader(header, heTR);
  348.         CS.wFrame.AddShadow(shBR, shSeeThru);
  349.         CS.SetColor(*attrib);
  350.         SetBlink(TRUE);
  351.         CS.Draw();
  352.         CS.Process();
  353.         newAttrib = CS.GetColor();
  354.         CS.Erase();
  355.         CS.Done();
  356.  
  357.         if( *attrib != newAttrib )
  358.         {
  359.             *attrib = newAttrib;
  360.             lz_Dirty = TRUE;
  361.         }
  362.     }
  363. }
  364.