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

  1. /******************************************************************************
  2. * .FILE:         tbar1.hpp                                                    *
  3. *                                                                             *
  4. * .DESCRIPTION:  Tool Bar Example 1:  Class Implementation                    *
  5. *                                                                             *
  6. * .CLASSES:      FontSelectHandler                                            *
  7. *                ACommandHandler                                              *
  8. *                Editor                                                       *
  9. *                                                                             *
  10. * .COPYRIGHT:                                                                 *
  11. *    Licensed Material - Program-Property of IBM                              *
  12. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  13. *                                                                             *
  14. * .DISCLAIMER:                                                                *
  15. *   The following [enclosed] code is sample code created by IBM               *
  16. *   Corporation.  This sample code is not part of any standard IBM product    *
  17. *   and is provided to you solely for the purpose of assisting you in the     *
  18. *   development of your applications.  The code is provided 'AS IS',          *
  19. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  20. *   arising out of your use of the sample code, even if they have been        *
  21. *   advised of the possibility of such damages.                               *
  22. *                                                                             *
  23. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  24. *                                                                             *
  25. ******************************************************************************/
  26. #include <ibase.hpp>
  27. #include <iapp.hpp>
  28. #include <iframe.hpp>
  29. #include <ifont.hpp>
  30. #include <itbar.hpp>
  31. #include <itbarbut.hpp>
  32. #include <imle.hpp>
  33. #include <icmdhdr.hpp>
  34. #include <imenubar.hpp>
  35. #include <iflytext.hpp>
  36. #include <istattxt.hpp>
  37. #include <iflyhhdr.hpp>
  38. #include <icombobx.hpp>
  39. #include <iselhdr.hpp>
  40. #include <icoordsy.hpp>
  41. #include "tbar1.hpp"
  42. #include "tbar1.h"
  43.  
  44. /******************************************************************************
  45. * main - Application entry point                                              *
  46. ******************************************************************************/
  47. int main()
  48. {
  49.   ICoordinateSystem::setApplicationOrientation(
  50.           ICoordinateSystem::originLowerLeft );
  51.    Editor editor;
  52.    editor.show();
  53.    editor.setFocus();
  54.    IApplication::current().run();
  55.    return 0;
  56. }
  57.  
  58. /******************************************************************************
  59. * class Editor::Editor - Constructor for main windows                         *
  60. *                                                                             *
  61. * Initialize the main window.                                                 *
  62. * Initialize the title bar                                                    *
  63. * Initialize the fly over help and text                                       *
  64. * Initialize the multi line editor                                            *
  65. * Initialize the toolbar and all of the controls associated with the toolbar  *
  66. * Initialize the menu                                                         *
  67. * Initialize the fontSelectHandler and the command Handler                    *
  68. ******************************************************************************/
  69.  
  70. Editor::Editor ()
  71.        :IFrameWindow(ID_MAIN_WINDOW),
  72.         title(this),
  73.         flyText(ID_FLYTEXT, &toolBar),
  74.         infoText(ID_INFOTEXT, this, this),
  75.         flyHelpHandler(&flyText, &infoText, 0, 0),
  76.         editWindow(ID_EDITOR, this, this),
  77.         toolBar(ID_TOOLBAR, this),
  78.         cutButton       (IC_ID_CUT,        &toolBar, &toolBar),
  79.         copyButton      (IC_ID_COPY,       &toolBar, &toolBar),
  80.         pasteButton     (IC_ID_PASTE,      &toolBar, &toolBar),
  81.         boldButton      (IC_ID_BOLD,       &toolBar, &toolBar),
  82.         italicButton    (IC_ID_ITALIC,     &toolBar, &toolBar),
  83.         underscoreButton(IC_ID_UNDERSCORE, &toolBar, &toolBar),
  84.         fontCombo(ID_FONTCOMBO, &toolBar, &toolBar, IRectangle(),
  85.                   IComboBox::classDefaultStyle |
  86.                   IComboBox::simpleType |
  87.                   IComboBox::readOnlyDropDownType),
  88.         menu(ID_MAIN_WINDOW,this),
  89.         fontSelectHandler(*this),
  90.         editFont(),
  91.         commandhandler(&editFont,&toolBar,&editWindow)
  92. {
  93. /*----------------------------------------------------------------------------|
  94. | Set the icon and title text                                                 |
  95. -----------------------------------------------------------------------------*/
  96.    setIcon( id() );
  97.    title.setTitleText(ID_MAIN_WINDOW);
  98.  
  99.  
  100. /*----------------------------------------------------------------------------|
  101. | Add the buttons to the toolbar                                              |
  102. -----------------------------------------------------------------------------*/
  103.    toolBar.addAsLast(&cutButton,true);
  104.    toolBar.addAsLast(©Button);
  105.    toolBar.addAsLast(&pasteButton);
  106.    toolBar.addAsLast(&boldButton,true);
  107.    toolBar.addAsLast(&italicButton);
  108.    toolBar.addAsLast(&underscoreButton);
  109.    toolBar.addAsLast(&fontCombo,true);
  110.  
  111. /*----------------------------------------------------------------------------|
  112. | Set up latchable style for font property buttons                            |
  113. -----------------------------------------------------------------------------*/
  114.    boldButton.enableLatching();
  115.    italicButton.enableLatching();
  116.    underscoreButton.enableLatching();
  117.  
  118. /*----------------------------------------------------------------------------|
  119. | Load up font combo box with face names                                      |
  120. -----------------------------------------------------------------------------*/
  121.    fontCombo.setLimit(10);
  122.    IFont::FaceNameCursor fontCursor;
  123.    for ( fontCursor.setToFirst(); fontCursor.isValid(); fontCursor.setToNext())
  124.    {
  125.      IString faceName = IFont::faceNameAt(fontCursor);
  126.      fontCombo.addAsLast(faceName);
  127.      if (faceName.length() > fontCombo.limit())
  128.        fontCombo.setLimit(faceName.length());
  129.    }
  130.  
  131. /*----------------------------------------------------------------------------|
  132. | Set up title for toolbar when floating                                      |
  133. -----------------------------------------------------------------------------*/
  134.    toolBar.setFloatingTitle(STR_TOOLBAR);
  135.  
  136. /*----------------------------------------------------------------------------|
  137. | Setup the editor                                                            |
  138. -----------------------------------------------------------------------------*/
  139.    setClient(&editWindow);
  140.    editWindow.setFont(editFont);
  141.    editWindow.importFromFile("toolbar.not");
  142.    editWindow.setTop(1);
  143.  
  144. /*----------------------------------------------------------------------------|
  145. | Add the info frame extension                                                |
  146. -----------------------------------------------------------------------------*/
  147.    addExtension(&infoText, IFrameWindow::belowClient);
  148.  
  149. /*----------------------------------------------------------------------------|
  150. | Set up and add the help handler                                             |
  151. -----------------------------------------------------------------------------*/
  152.    flyHelpHandler.setLongStringTableOffset(OFFSET_INFOTEXT);
  153.    flyHelpHandler.handleEventsFor(&toolBar);
  154.  
  155. /*----------------------------------------------------------------------------|
  156. | Attach the Command Handler to the frame and toolbar                         |
  157. -----------------------------------------------------------------------------*/
  158.    commandhandler.handleEventsFor(this);
  159.    commandhandler.handleEventsFor(&toolBar);
  160.  
  161. /*----------------------------------------------------------------------------|
  162. | Allow the font select handler to handle events for font combo box.          |
  163. -----------------------------------------------------------------------------*/
  164.    fontSelectHandler.handleEventsFor(&fontCombo);
  165.  
  166.    moveSizeToClient(IRectangle(IPoint(100,100),
  167.                                ISize(editFont.avgCharWidth()*80,
  168.                                      editFont.maxCharHeight()*15)));
  169. }
  170.  
  171.  
  172. /******************************************************************************
  173. * class ACommandHandler::command - Handles menu bar selections and toolbar    *
  174. *   selections.                                                               *
  175. ******************************************************************************/
  176. IBase::Boolean ACommandHandler::command(ICommandEvent &event)
  177. {
  178.    switch (event.commandId())
  179.    {
  180.      case IC_ID_CUT:
  181.      {
  182.          if(editWindow->hasSelectedText())
  183.             editWindow->cut();
  184.          break;
  185.      }
  186.      case IC_ID_COPY:
  187.      {
  188.          if(editWindow->hasSelectedText())
  189.             editWindow->copy();
  190.          break;
  191.      }
  192.      case IC_ID_PASTE:
  193.      {
  194.          if(editWindow->clipboardHasTextFormat())
  195.             editWindow->paste();
  196.          break;
  197.      }
  198.      case IC_ID_BOLD:
  199.      {
  200.          bold=!bold;
  201.          editFont->setBold(bold/*Button.isLatched()*/);
  202.          editWindow->setFont(*editFont);
  203.          break;
  204.      }
  205.      case IC_ID_ITALIC:
  206.      {
  207.          italic=!italic;
  208.          editFont->setItalic(italic/*Button.isLatched()*/);
  209.          editWindow->setFont(*editFont);
  210.          break;
  211.      }
  212.      case IC_ID_UNDERSCORE:
  213.      {
  214.          under=!under;
  215.          editFont->setUnderscore(under/*scoreButton.isLatched()*/);
  216.          editWindow->setFont(*editFont);
  217.          break;
  218.      }
  219.      case ID_SHOWTEXT:
  220.      {
  221.          toolBar->setButtonView(IToolBarButton::textView);
  222.          break;
  223.      }
  224.      case ID_SHOWBITMAPS:
  225.      {
  226.          toolBar->setButtonView(IToolBarButton::bitmapView);
  227.          break;
  228.      }
  229.      case ID_SHOWTEXTANDBITMAPS:
  230.      {
  231.          toolBar->setButtonView(IToolBarButton::bitmapAndTextView);
  232.          break;
  233.      }
  234.      case ID_TOOLBAR_TOP:
  235.      {
  236.          toolBar->setLocation(IToolBar::aboveClient);
  237.          break;
  238.      }
  239.      case ID_TOOLBAR_BOTTOM:
  240.      {
  241.          toolBar->setLocation(IToolBar::belowClient);
  242.          break;
  243.      }
  244.      case ID_TOOLBAR_LEFT:
  245.      {
  246.          toolBar->setLocation(IToolBar::leftOfClient);
  247.          break;
  248.      }
  249.      case ID_TOOLBAR_RIGHT:
  250.      {
  251.          toolBar->setLocation(IToolBar::rightOfClient);
  252.          break;
  253.      }
  254.      case ID_TOOLBAR_FLOATING:
  255.      {
  256.          toolBar->setLocation(IToolBar::floating);
  257.          break;
  258.      }
  259.    }
  260.    return true;
  261. }
  262.  
  263. /******************************************************************************
  264. * class FontSelectHandler::enter - Handle combo box selections
  265. ******************************************************************************/
  266. IBase::Boolean FontSelectHandler::enter( IControlEvent& event)
  267. {
  268.   IString fontChoice =  ((IComboBox*)event.controlWindow())->text();
  269.   if(fontChoice.length())
  270.   {
  271. /*----------------------------------------------------------------------------|
  272. | Set the new font                                                            |
  273. -----------------------------------------------------------------------------*/
  274.      editorFrame.editorFont().setName(fontChoice);
  275.      editorFrame.editorWindow().setFont(editorFrame.editorFont());
  276.   }
  277.   return true;
  278. }
  279.  
  280.