home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / slider / cslider / cslider.cpp next >
Text File  |  1996-10-29  |  6KB  |  219 lines

  1. //*********************************************************
  2. // Sliders - Circular Slider Example
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //*********************************************************
  8. #include <iapp.hpp>
  9. #include <icolor.hpp>
  10. #include <irect.hpp>
  11. #include <icconst.h>
  12. #include "cslider.hpp"
  13.  
  14. #define ID_BRIGHTNESS_CSLIDER  101
  15. #define ID_REDGREEN_CSLIDER   102
  16.  
  17. void main ( )
  18. {
  19.   // Create a window with two circular sliders.
  20.   ColorWindow
  21.     colorWindow;
  22.  
  23.   // Give the window the input focus and show it.
  24.   colorWindow
  25.    .setFocus()
  26.    .show();
  27.  
  28.   IApplication::current().run();
  29. }
  30.  
  31. ColorWindow::ColorWindow ( )
  32.   : IFrameWindow( "Circular Slider Example" ),
  33.     canvasClient( IC_FRAME_CLIENT_ID, this, this ),
  34.     colorWindow( 10, &canvasClient, &canvasClient ),
  35.     sliderCanvas( 20, &canvasClient, &canvasClient ),
  36.     brightnessSlider( ID_BRIGHTNESS_CSLIDER,
  37.                       &sliderCanvas, &sliderCanvas, IRectangle(),
  38.                       ICircularSlider::buttons
  39.                        | ICircularSlider::displayValue
  40.                        | ICircularSlider::jumpToPointer
  41.                        | ICircularSlider::label
  42.                        | ICircularSlider::midpoint
  43.                        | ICircularSlider::proportionalTicks
  44.                        | IControl::tabStop
  45.                        | IWindow::visible ),
  46.     redGreenSlider( ID_REDGREEN_CSLIDER,
  47.                     &sliderCanvas, &sliderCanvas, IRectangle(),
  48.                     ICircularSlider::circularArm
  49.                      | ICircularSlider::jumpToPointer
  50.                      | ICircularSlider::label
  51.                      | ICircularSlider::noTicks
  52.                      | IControl::tabStop
  53.                      | IWindow::visible ),
  54.     fArmHandler( this ),
  55.     fRed( 0 ),
  56.     fGreen( 0 ),
  57.     fBlue( 0 ),
  58.     fBrightness( 0 )
  59. {
  60.   canvasClient
  61.    .setOrientation( ISplitCanvas::horizontalSplit )
  62.    .setSplitWindowPercentage( &colorWindow,  70 )
  63.    .setSplitWindowPercentage( &sliderCanvas, 30 );
  64.  
  65.   // Set up the circular sliders.
  66.   brightnessSlider
  67.    .setBackgroundColor( sliderCanvas.backgroundColor() )
  68.    .setMinimumSize( ISize( 50, 50 ) );
  69.   brightnessSlider
  70.    .setArmRange( IRange( 0, 100 ) )
  71.    .setTickSpacing( 1 )
  72.    .setText( "Brightness" );
  73.  
  74.   redGreenSlider
  75.    .setBackgroundColor( sliderCanvas.backgroundColor() )
  76.    .setMinimumSize( ISize( 50, 50 ) );
  77.   redGreenSlider
  78.    .setArmRange( IRange( 0, 255 ) )
  79.    .setTickSpacing( 10 )
  80.    .setText( "Red    Green" );
  81.  
  82.   // Define the positioning rules for controls on the multicell
  83.   // canvas.
  84.   sliderCanvas
  85.    .addToCell( &brightnessSlider, 2, 2 )
  86.    .addToCell( &redGreenSlider,   4, 2 );
  87.   sliderCanvas
  88.    .setColumnWidth( 1, 10, true )
  89.    .setColumnWidth( 2, 10, true )
  90.    .setColumnWidth( 3, 10, true )
  91.    .setColumnWidth( 4, 10, true )
  92.    .setColumnWidth( 5, 10, true )
  93.    .setRowHeight( 1, 10 )
  94.    .setRowHeight( 2, 10, true )
  95.    .setRowHeight( 3, 10 );
  96.  
  97.   fArmHandler
  98.    .handleEventsFor( &brightnessSlider )
  99.    .handleEventsFor( &redGreenSlider );
  100.  
  101.   (*this)
  102.    .setGreen( 255 )
  103.    .setBrightness( 100 );
  104.  
  105.   (*this)
  106.    .setClient( &canvasClient );
  107. }
  108.  
  109. ColorWindow&
  110.   ColorWindow::setRed ( unsigned char red )
  111. {
  112.   this->setGreen( 255 - red );
  113.   return *this;
  114. }
  115.  
  116. ColorWindow&
  117.   ColorWindow::setGreen ( unsigned char green )
  118. {
  119.   redGreenSlider.setValue( green );
  120. #ifdef IC_PM
  121.   // Compensate for slider arm handler not being called.
  122.   this->updateColor( (unsigned char)( 255 - green ),
  123.                      green,
  124.                      0 );
  125. #endif
  126.   return *this;
  127. }
  128.  
  129. ColorWindow&
  130.   ColorWindow::setBlue ( unsigned char blue )
  131. {
  132.   fBlue = blue;
  133.   return *this;
  134. }
  135.  
  136. ColorWindow&
  137.   ColorWindow::setBrightness ( unsigned long brightness )
  138. {
  139.   if ( brightness > 100 )
  140.   {
  141.      brightness = 100;
  142.   }
  143.   brightnessSlider.setValue( brightness );
  144. #ifdef IC_PM
  145.   // Compensate for slider arm handler not being called.
  146.   this->updateBrightness( brightness );
  147. #endif
  148.   return *this;
  149. }
  150.  
  151. ColorWindow&
  152.   ColorWindow::updateColor ( unsigned char red,
  153.                              unsigned char green,
  154.                              unsigned char blue )
  155. {
  156.   fRed = red;
  157.   fGreen = green;
  158.   fBlue = blue;
  159.   return this->showColorAndBrightness();
  160. }
  161.  
  162. ColorWindow&
  163.   ColorWindow::updateBrightness ( unsigned long brightness )
  164. {
  165.   fBrightness = ( brightness > 100 ) ? 100 : brightness;
  166.   return this->showColorAndBrightness();
  167. }
  168.  
  169. ColorWindow&
  170.   ColorWindow::showColorAndBrightness ( )
  171. {
  172.   unsigned char
  173.     red   = fRed,
  174.     green = fGreen,
  175.     blue  = fBlue;
  176.  
  177.   if ( red )
  178.   {
  179.      red = (unsigned char)( red * fBrightness / 100 );
  180.   }
  181.   if ( green )
  182.   {
  183.      green = (unsigned char)( green * fBrightness / 100 );
  184.   }
  185.   if ( blue )
  186.   {
  187.      blue = (unsigned char)( blue * fBrightness / 100 );
  188.   }
  189.  
  190.   colorWindow
  191.    .setBackgroundColor( IColor( red, green, blue ) );
  192.   return *this;
  193. }
  194.  
  195. ColorWindow::ArmHandler::ArmHandler ( ColorWindow* window )
  196.   : ISliderArmHandler( ),
  197.     colorWindow( window )
  198. { }
  199.  
  200. IBase::Boolean
  201.   ColorWindow::ArmHandler::valueChange ( IControlEvent& event )
  202. {
  203.   ICircularSlider
  204.    *cslider = (ICircularSlider*)event.controlWindow();
  205.   if ( cslider->id() == ID_BRIGHTNESS_CSLIDER )
  206.   {
  207.      colorWindow->updateBrightness( cslider->value() );
  208.   }
  209.   else if ( cslider->id() == ID_REDGREEN_CSLIDER )
  210.   {
  211.      unsigned char
  212.        green = (unsigned char)( cslider->value() );
  213.      colorWindow->updateColor( (unsigned char)( 255 - green ),
  214.                                green,
  215.                                0 );
  216.   }
  217.   return false;
  218. }
  219.