home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / magnify / magnify.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  9.4 KB  |  183 lines

  1. /******************************************************************************
  2. * .FILE:         magnify.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION:  Magnify Sample Program:  Class Implementation                *
  5. *                                                                             *
  6. * .CLASSES:      MagnifyWindow                                                *
  7. *                MagnifyHandler                                               *
  8. *                                                                             *
  9. * .COPYRIGHT:                                                                 *
  10. *    Licensed Material - Program-Property of IBM                              *
  11. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  12. *                                                                             *
  13. * .DISCLAIMER:                                                                *
  14. *   The following [enclosed] code is sample code created by IBM               *
  15. *   Corporation.  This sample code is not part of any standard IBM product    *
  16. *   and is provided to you solely for the purpose of assisting you in the     *
  17. *   development of your applications.  The code is provided 'AS IS',          *
  18. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  19. *   arising out of your use of the sample code, even if they have been        *
  20. *   advised of the possibility of such damages.                               *
  21. *                                                                             *
  22. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  23. *                                                                             *
  24. ******************************************************************************/
  25. #include <ibase.hpp>
  26. #include <iapp.hpp>
  27. #include <iframe.hpp>
  28. #include <icmdhdr.hpp>
  29. #include <igbitmap.hpp>
  30. #include <igrafctx.hpp>
  31. #include <ibmpctl.hpp>
  32. #include <itimer.hpp>
  33. #include <isetcv.hpp>
  34. #include <icustbut.hpp>
  35. #include <ispinnum.hpp>
  36. #include <icoordsy.hpp>
  37. #include "magnify.hpp"
  38.  
  39. /******************************************************************************
  40. * main - Application entry point                                              *
  41. ******************************************************************************/
  42. int main()
  43. {
  44.   ICoordinateSystem::setApplicationOrientation(
  45.           ICoordinateSystem::originLowerLeft );
  46.   MagnifyWindow win;
  47.   IApplication::current().run();
  48.  
  49.   return 0;
  50. }
  51.  
  52. /******************************************************************************
  53. * Class MagnifyWindow::MagnifyWindow - Constructor for the main window        *
  54. *                                                                             *
  55. * Define itself as an IFrameWindow                                            *
  56. * Create the title for the window                                             *
  57. * Define a control for a bitmap                                               *
  58. * Define a canvas to put the spin buttons, pushbuttons and text.              *
  59. * Define the spin buttons, pushbuttons, and the text.                         *
  60. ******************************************************************************/
  61. MagnifyWindow::MagnifyWindow ()
  62.               :IFrameWindow (WND_MAIN),
  63.                title(this),
  64.                bitmapControl(0,this,this),
  65.                canvas(100,this,this),
  66.                button(101,&canvas,&canvas),
  67.                xText(102,&canvas,&canvas),
  68.                xSpin(103,&canvas,&canvas),
  69.                yText(104,&canvas,&canvas),
  70.                ySpin(105,&canvas,&canvas),
  71.                bitmap(0)
  72. {
  73. /*----------------------------------------------------------------------------|
  74. | Set the icon and title for the main window                                  |
  75. -----------------------------------------------------------------------------*/
  76.   setIcon( id() );
  77.   title.setTitleText(TITLE_MAGNIFY);
  78.  
  79. /*----------------------------------------------------------------------------|
  80. | Sets the bitmap as the client area                                          |
  81. -----------------------------------------------------------------------------*/
  82.   setClient(&bitmapControl);
  83.  
  84. /*----------------------------------------------------------------------------|
  85. | Begin handling events for the push button                                   |
  86. -----------------------------------------------------------------------------*/
  87.   handler.handleEventsFor(this);
  88.  
  89. /*----------------------------------------------------------------------------|
  90. | Enable latching for the button                                              |
  91. | Set the text of the button                                                  |
  92. | And. set it as a tab stop                                                   |
  93. -----------------------------------------------------------------------------*/
  94.   button.enableLatching().setText(STR_MAGNIFY).enableTabStop();
  95.  
  96. /*----------------------------------------------------------------------------|
  97. | Set up the spin buttons with their text and the range of values             |
  98. -----------------------------------------------------------------------------*/
  99.   xText.setText(STR_X);
  100.   yText.setText(STR_Y);
  101.   xSpin.setRange(IRange(1,desktopWindow()->size().width())).setLimit(4);
  102.   xSpin.setValue(32).enableTabStop();
  103.   ySpin.setRange(IRange(1,desktopWindow()->size().height())).setLimit(4);
  104.   ySpin.setValue(32).enableTabStop();
  105.  
  106. /*----------------------------------------------------------------------------|
  107. | Define the margins and alignment of the set cavas                           |
  108. | Add the set canvas as an extension of the main frame window                 |
  109. | Resize the client area (the bitmap control) to fit the fram window          |
  110. -----------------------------------------------------------------------------*/
  111.   canvas.setMargin(ISize(2,2)).setAlignment(ISetCanvas::centerCenter);
  112.   addExtension(&canvas,aboveClient,(unsigned long) (canvas.minimumSize().height()));
  113.   moveSizeToClient(IRectangle(50,50,50+canvas.minimumSize().width(),
  114.                                     50+canvas.minimumSize().width()));
  115.  
  116. /*----------------------------------------------------------------------------|
  117. |  Set focus and show the window.                                             |
  118. -----------------------------------------------------------------------------*/
  119.   show();
  120.   button.setFocus();
  121. }
  122.  
  123.  
  124. /******************************************************************************
  125. * class MagnifyWindow::timerFunction - Called when the timer expires and grabs*
  126. *   a new bitmap of the section of the window that is being magnified.        *
  127. ******************************************************************************/
  128. void MagnifyWindow::timerFunction ()
  129. {
  130. /*----------------------------------------------------------------------------|
  131. | Create a rectangle with the size specified by the spin buttons              |
  132. -----------------------------------------------------------------------------*/
  133.   unsigned long xSize = xSpin.value();
  134.   unsigned long ySize = ySpin.value();
  135.   IRectangle rect(0,0,xSize,ySize);
  136.  
  137. /*----------------------------------------------------------------------------|
  138. | Reposition the rectangle so that the mouse is at the center of the rectangle|
  139. -----------------------------------------------------------------------------*/
  140.   ISize desktopSize = desktopWindow()->size();
  141.   IPoint mousePosition = IWindow::pointerPosition();
  142.   rect.centerAt(mousePosition);
  143.   if (rect.left()<0)
  144.     rect.moveTo(IPoint(0,rect.bottom()));
  145.   if (rect.bottom()<0)
  146.     rect.moveTo(IPoint(rect.left(),0));
  147.   if (rect.right()>=desktopSize.width())
  148.     rect.moveTo(IPoint(desktopSize.width()-xSize,rect.bottom()));
  149.   if (rect.top()>= desktopSize.height())
  150.     rect.moveTo(IPoint(rect.left(),desktopSize.height()-ySize));
  151.  
  152.  
  153. /*----------------------------------------------------------------------------|
  154. | Convert the rectangle to native coordinates.                                |
  155. | For graphics, this function should be used for portable graphics.           |
  156. -----------------------------------------------------------------------------*/
  157.   rect = ICoordinateSystem::convertToNative( rect, desktopSize );
  158.  
  159. /*----------------------------------------------------------------------------|
  160. | Create a new bitmap from the desktop in the area specified by the rectangle.|
  161. | Delete the old bitmap and reset its pointer to the new bitmap.              |
  162. -----------------------------------------------------------------------------*/
  163.   IGBitmap* newBitmap = new IGBitmap(IGraphicContext(desktopWindow()->handle()),rect);
  164.   bitmapControl.setBitmap(newBitmap->handle());
  165.   if (bitmap)
  166.     delete bitmap;
  167.   bitmap = newBitmap;
  168. }
  169.  
  170. /******************************************************************************
  171. * MagnifyHandler::command - Command handler that turns the timer off and on.  *
  172. ******************************************************************************/
  173. IBase::Boolean MagnifyHandler::command(ICommandEvent& event)
  174. {
  175.   MagnifyWindow *win = (MagnifyWindow*) event.window();
  176.   if (win->button.isLatched())
  177.     win->timer.start(new ITimerMemberFn0<MagnifyWindow>(*win,MagnifyWindow::timerFunction),200);
  178.   else
  179.     win->timer.stop();
  180.   return true;
  181. }
  182.  
  183.