home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / TBAR2 / TBAR2.CPP < prev    next >
Text File  |  1995-05-01  |  19KB  |  524 lines

  1. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  2.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  3. #endif                                  //  is defined.
  4. #include <iapp.hpp>
  5. #include <iframe.hpp>
  6. #include <ifont.hpp>
  7. #include <itbar.hpp>
  8. #include <itbarbut.hpp>
  9. #include <imle.hpp>
  10. #include <icmdhdr.hpp>
  11. #include <imenubar.hpp>
  12. #include <iflytext.hpp>
  13. #include <istattxt.hpp>
  14. #include <iflyhhdr.hpp>
  15. #include <icombobx.hpp>
  16. #include <iselhdr.hpp>
  17. #include <idmhndlr.hpp>
  18. #include <isubmenu.hpp>
  19. #include <ifontdlg.hpp>
  20. #include <inotebk.hpp>
  21. #include <ititle.hpp>
  22. #include <imcelcv.hpp>
  23. #include <iradiobt.hpp>
  24. #include <icheckbx.hpp>
  25. #include <igroupbx.hpp>
  26. #include <ifiledlg.hpp>
  27. #include <icolor.hpp>
  28.  
  29. #include "tbar2.hpp"
  30. #include "tbar2.h"
  31.  
  32. //-------------------------------------------------------------------
  33. // Main routine
  34. //
  35. // This routine creates and shows the editor window
  36. //-------------------------------------------------------------------
  37. int main()
  38. {
  39.    Editor editor;
  40.    editor.show();
  41.    editor.setFocus();
  42.    IApplication::current().run();
  43.    return 0;
  44. }
  45.  
  46. //-------------------------------------------------------------------
  47. // Editor::Editor
  48. //
  49. // This constructor initializes all of the windows (including the
  50. // tool bar and tool bar buttons).
  51. //
  52. // Note: The tool bar buttons are created with standard identifiers
  53. // that are provided by the class library (defined in icconst.h) for
  54. // standard operations.
  55. //-------------------------------------------------------------------
  56. Editor::Editor ()
  57.        :IFrameWindow("Tool Bar Sample 2", ID_MAIN_WINDOW),
  58.         fileToolBar(ID_FILE, this),
  59.         editToolBar(ID_EDIT, &fileToolBar, true),
  60.         fontToolBar(ID_FONT, &editToolBar, true),
  61.         flyText(ID_FLYTEXT, this),
  62.         infoText(ID_INFOTEXT, this, this),
  63.         flyHelpHandler(&flyText, &infoText, 0, 0),
  64.         editWindow(ID_EDITOR, *this),
  65.         commandHandler(*this),
  66.         fontSelectHandler(*this),
  67.         openButton      (IC_ID_OPEN,       &fileToolBar, &fileToolBar),
  68.         saveButton      (IC_ID_SAVE,       &fileToolBar, &fileToolBar),
  69.         cutButton       (IC_ID_CUT,        &editToolBar, &editToolBar),
  70.         copyButton      (IC_ID_COPY,       &editToolBar, &editToolBar),
  71.         pasteButton     (IC_ID_PASTE,      &editToolBar, &editToolBar),
  72.         boldButton      (IC_ID_BOLD,       &fontToolBar, &fontToolBar,
  73.                          IRectangle(), IToolBarButton::defaultStyle() |
  74.                          IToolBarButton::noDragDelete ),
  75.         italicButton    (IC_ID_ITALIC,     &fontToolBar, &fontToolBar,
  76.                          IRectangle(), IToolBarButton::defaultStyle() |
  77.                          IToolBarButton::noDragDelete ),
  78.         underscoreButton(IC_ID_UNDERSCORE, &fontToolBar, &fontToolBar,
  79.                          IRectangle(), IToolBarButton::defaultStyle() |
  80.                          IToolBarButton::noDragDelete ),
  81.         fontCombo(ID_FONTCOMBO, &fontToolBar, &fontToolBar, IRectangle(),
  82.                   IComboBox::classDefaultStyle &~IComboBox::simpleType |
  83.                   IComboBox::dropDownListType),
  84.         menu(ID_MAIN_WINDOW,this),
  85.         editFont()
  86. {
  87.    // Set the icon
  88.    setIcon( id() );
  89.  
  90.    // Add buttons to the file toolbar
  91.    fileToolBar.addAsLast(&openButton,true);
  92.    fileToolBar.addAsLast(&saveButton);
  93.  
  94.    // Add buttons to the edit toolbar
  95.    editToolBar.addAsLast(&cutButton,true);
  96.    editToolBar.addAsLast(©Button);
  97.    editToolBar.addAsLast(&pasteButton);
  98.  
  99.    // Add buttons to the font toolbar
  100.    fontToolBar.addAsLast(&boldButton,true);
  101.    fontToolBar.addAsLast(&italicButton);
  102.    fontToolBar.addAsLast(&underscoreButton);
  103.    fontToolBar.addAsLast(&fontCombo,true);
  104.  
  105.    // Set up latchable style for font property buttons
  106.    boldButton.enableLatching();
  107.    italicButton.enableLatching();
  108.    underscoreButton.enableLatching();
  109.  
  110.    // Load up font combo box with face names
  111.    fontCombo.setLimit(10);
  112.    IFont::FaceNameCursor fontCursor;
  113.    for ( fontCursor.setToFirst(); fontCursor.isValid(); fontCursor.setToNext())
  114.    {
  115.      IString faceName = IFont::faceNameAt(fontCursor);
  116.      fontCombo.addAsLast(faceName);
  117.      if (faceName.length() > fontCombo.limit())
  118.        fontCombo.setLimit(faceName.length());
  119.    }
  120.    updateFontToolBar();
  121.  
  122.    // Set up titles for toolbars when floating
  123.    fileToolBar.setFloatingTitle(ID_FILE);
  124.    editToolBar.setFloatingTitle(ID_EDIT);
  125.    fontToolBar.setFloatingTitle(ID_FONT);
  126.  
  127.    // Setup the editor
  128.    setClient(&editWindow);
  129.    editWindow.setFont(editFont);
  130.    editWindow.importFromFile("toolbar2.not");
  131.    editWindow.setTop(1);
  132.  
  133.    // Add the Info frame extension
  134.    addExtension(&infoText, IFrameWindow::belowClient);
  135.  
  136.    // Set up and add the help handler
  137.    flyHelpHandler.setLongStringTableOffset(OFFSET_INFOTEXT);
  138.    flyHelpHandler.setDefaultText("\0");
  139.    flyHelpHandler.handleEventsFor(&fileToolBar);
  140.    flyHelpHandler.handleEventsFor(&editToolBar);
  141.    flyHelpHandler.handleEventsFor(&fontToolBar);
  142.  
  143.    // Attach the Command Handler to frame and toolbar
  144.    commandHandler.handleEventsFor(this);
  145.    commandHandler.handleEventsFor(&fileToolBar);
  146.    commandHandler.handleEventsFor(&editToolBar);
  147.    commandHandler.handleEventsFor(&fontToolBar);
  148.  
  149.    // Add the handler to change the font
  150.    fontSelectHandler.handleEventsFor(&fontCombo);
  151.  
  152.    // Set up drag from menu
  153.    fileSubmenu = new IWindow( menu.menuItem(ID_FILE).submenuHandle() );
  154.    IDMHandler::enableDragFrom( (ISubmenu*) fileSubmenu );
  155.    editSubmenu = new IWindow( menu.menuItem(ID_EDIT).submenuHandle() );
  156.    IDMHandler::enableDragFrom( (ISubmenu*) editSubmenu );
  157.  
  158.    moveSizeToClient(IRectangle(IPoint(100,100),
  159.                                ISize(editFont.avgCharWidth()*80,
  160.                                      editFont.maxCharHeight()*15)));
  161. }
  162.  
  163. //-------------------------------------------------------------------
  164. // Handle user menu bar selections
  165. //-------------------------------------------------------------------
  166. Boolean EditorCommandHandler::command(ICommandEvent &event)
  167. {
  168.    switch (event.commandId())
  169.    {
  170.      case IC_ID_OPEN:
  171.      {
  172.         ITitle title(&editorFrame);
  173.         IFileDialog::Settings settings;
  174.         settings.setOpenDialog();
  175.         settings.setFileName(title.text());
  176.         IFileDialog openDlg(IWindow::desktopWindow(),&editorFrame,settings);
  177.         if ( openDlg.pressedOK() )
  178.         {
  179.            editorFrame.disableUpdate();
  180.            editorFrame.editorWindow().removeAll();
  181.            editorFrame.editorWindow().importFromFile(openDlg.fileName());
  182.            editorFrame.enableUpdate();
  183.            editorFrame.editorWindow().setTop(1);
  184.            title.setText(openDlg.fileName());
  185.         }
  186.         break;
  187.      }
  188.      case IC_ID_SAVE:
  189.      {
  190.         ITitle title(&editorFrame);
  191.         IFileDialog::Settings settings;
  192.         settings.setSaveAsDialog();
  193.         settings.setFileName(title.text());
  194.         IFileDialog saveDlg(IWindow::desktopWindow(),&editorFrame,settings);
  195.         if ( saveDlg.pressedOK() )
  196.         {
  197.            editorFrame.editorWindow().exportToFile(saveDlg.fileName());
  198.            title.setText(saveDlg.fileName());
  199.         }
  200.         break;
  201.      }
  202.      case IC_ID_CUT:
  203.      {
  204.          if(editorFrame.editorWindow().hasSelectedText())
  205.             editorFrame.editorWindow().cut();
  206.          break;
  207.      }
  208.      case IC_ID_COPY:
  209.      {
  210.          if(editorFrame.editorWindow().hasSelectedText())
  211.             editorFrame.editorWindow().copy();
  212.          break;
  213.      }
  214.      case IC_ID_PASTE:
  215.      {
  216.          if(editorFrame.editorWindow().clipboardHasTextFormat())
  217.             editorFrame.editorWindow().paste();
  218.          break;
  219.      }
  220.      case IC_ID_BOLD:
  221.      {
  222.          editorFrame.editorFont().setBold(!editorFrame.editorFont().isBold());
  223.          editorFrame.editorWindow().setFont(editorFrame.editorFont());
  224.          break;
  225.      }
  226.      case IC_ID_ITALIC:
  227.      {
  228.          editorFrame.editorFont().setItalic(!editorFrame.editorFont().isItalic());
  229.          editorFrame.editorWindow().setFont(editorFrame.editorFont());
  230.          break;
  231.      }
  232.      case IC_ID_UNDERSCORE:
  233.      {
  234.          editorFrame.editorFont().setUnderscore(!editorFrame.editorFont().isUnderscore());
  235.          editorFrame.editorWindow().setFont(editorFrame.editorFont());
  236.          break;
  237.      }
  238.      case ID_FONT:
  239.      {
  240.          IFontDialog::Settings settings(&editorFrame.editorFont());
  241.  
  242.          IFontDialog dlg(IWindow::desktopWindow(),&editorFrame,settings);
  243.          if ( dlg.pressedOK() )
  244.          {
  245.            editorFrame.updateFontToolBar();
  246.            editorFrame.editorWindow().setFont(editorFrame.editorFont());
  247.          }
  248.          break;
  249.      }
  250.      case ID_TOOLBARS:
  251.      {
  252.          ToolBarNotebook nbk(editorFrame);
  253.          break;
  254.      }
  255.    }
  256.    return true;
  257. }
  258.  
  259. //-------------------------------------------------------------------
  260. // Update font toolbar to reflect the current font
  261. //-------------------------------------------------------------------
  262. Editor& Editor::updateFontToolBar()
  263. {
  264.    // Update toolbar buttons (if they have not been dragged to the shredder)
  265.    if (boldButton.isValid())
  266.      boldButton.latch(editFont.isBold());
  267.    if (italicButton.isValid())
  268.      italicButton.latch(editFont.isItalic());
  269.    if (underscoreButton.isValid())
  270.      underscoreButton.latch(editFont.isUnderscore());
  271.  
  272.    // Update the combo box with the correct face name
  273.    fontCombo.select(fontCombo.locateText(editFont.name()));
  274.    return *this;
  275. }
  276.  
  277. //-------------------------------------------------------------------
  278. // Get the toolbar
  279. //-------------------------------------------------------------------
  280. IToolBar& Editor::toolBar ( unsigned long id )
  281. {
  282.    if ( id == ID_FILE )
  283.       return fileToolBar;
  284.    if ( id == ID_EDIT )
  285.       return editToolBar;
  286.    return fontToolBar;
  287. }
  288.  
  289. //-------------------------------------------------------------------
  290. // Handle combo box selections
  291. //-------------------------------------------------------------------
  292. Boolean FontSelectHandler::enter( IControlEvent& event)
  293. {
  294.   IString fontChoice =  ((IComboBox*)event.controlWindow())->text();
  295.   if(fontChoice.length())
  296.   {
  297.      // Set the new font
  298.      editorFrame.editorFont().setName(fontChoice);
  299.      editorFrame.editorWindow().setFont(editorFrame.editorFont());
  300.   }
  301.   return true;
  302. }
  303.  
  304. //-------------------------------------------------------------------
  305. // Check for font changes to the MLE
  306. //-------------------------------------------------------------------
  307. ITextControl& EditorMLE::setLayoutDistorted ( unsigned long layoutAttributesOn,
  308.                                               unsigned long layoutAttributesOff )
  309. {
  310.    IMultiLineEdit::setLayoutDistorted(layoutAttributesOn,layoutAttributesOff);
  311.    if ( layoutAttributesOn & IWindow::fontChanged )
  312.    {
  313.      IFont newFont(editorFrame.editorWindow().presSpace());
  314.      editorFrame.editorFont() = newFont;
  315.      editorFrame.updateFontToolBar();
  316.      editorFrame.editorWindow().setFont(editorFrame.editorFont());
  317.    }
  318.    return *this;
  319. }
  320.  
  321. //-------------------------------------------------------------------
  322. // ToolBarNotebook::ToolBarNotebook
  323. //-------------------------------------------------------------------
  324. ToolBarNotebook::ToolBarNotebook ( Editor&   editor )
  325.                 :IFrameWindow ( ID_TOOLBAR_WINDOW, IWindow::desktopWindow(),
  326.                                 (IWindow*) &editor, IRectangle(),
  327.                                 IFrameWindow::titleBar |
  328.                                 IFrameWindow::systemMenu |
  329.                                 IFrameWindow::dialogBorder |
  330.                                 IFrameWindow::appDBCSStatus ),
  331.                  editorFrame(editor),
  332.                  notebook(ID_NOTEBOOK,this,this)
  333. {
  334.   setClient(¬ebook);
  335.  
  336.   ITitle title(this);
  337.   title.setText(ID_TOOLBARS);
  338.  
  339.   notebook.setBinding(INotebook::spiral);
  340.   notebook.setMinorTabSize(ISize(0,0));
  341.  
  342.   // Add notebook pages
  343.   ToolBarPage filePage(ID_FILE,¬ebook,editor);
  344.   ToolBarPage editPage(ID_EDIT,¬ebook,editor);
  345.   ToolBarPage fontPage(ID_FONT,¬ebook,editor);
  346.  
  347.   // Center the notebook on the screen
  348.   IRectangle  clientRect;
  349.   clientRect.sizeTo(notebook.minimumSize());
  350.   clientRect.moveTo(IPoint((IWindow::desktopWindow()->size().width() -
  351.                             clientRect.width())/2,
  352.                            (IWindow::desktopWindow()->size().height() -
  353.                             clientRect.height())/2));
  354.  
  355.   moveSizeToClient(clientRect);
  356.   setFocus();
  357.   showModally();
  358.  
  359. }
  360.  
  361. //-------------------------------------------------------------------
  362. // ToolBarPage::ToolBarPage
  363. //-------------------------------------------------------------------
  364. ToolBarPage::ToolBarPage ( unsigned long id, INotebook* nbk, Editor& editor )
  365.             :IMultiCellCanvas(id,nbk,nbk),
  366.              editorFrame(editor),
  367.              locationBox(ID_LOCATION,this,this),
  368.              topButton(ID_TOP,this,this,IRectangle(),
  369.                        IRadioButton::defaultStyle()|IControl::group),
  370.              leftButton(ID_LEFT,this,this),
  371.              bottomButton(ID_BOTTOM,this,this),
  372.              rightButton(ID_RIGHT,this,this),
  373.              groupCheckBox(ID_GROUP,this,this),
  374.              floatingButton(ID_FLOATING,this,this),
  375.              hiddenButton(ID_HIDDEN,this,this),
  376.              viewBox(ID_VIEW,this,this),
  377.              bitmapButton(ID_BITMAP,this,this,IRectangle(),
  378.                           IRadioButton::defaultStyle()|IControl::group),
  379.              textButton(ID_TEXT,this,this),
  380.              bitmapAndTextButton(ID_BITMAP_AND_TEXT,this,this),
  381.              pageHandler(nbk,editor)
  382. {
  383.    INotebook::PageSettings settings(INotebook::PageSettings::autoPageSize |
  384.                                     INotebook::PageSettings::majorTab);
  385.  
  386.    settings.setTabText(id);
  387.  
  388.    locationBox.setText(ID_LOCATION);
  389.    topButton.setText(ID_TOP);
  390.    leftButton.setText(ID_LEFT);
  391.    bottomButton.setText(ID_BOTTOM);
  392.    rightButton.setText(ID_RIGHT);
  393.    groupCheckBox.setText(ID_GROUP);
  394.    floatingButton.setText(ID_FLOATING);
  395.    hiddenButton.setText(ID_HIDDEN);
  396.    viewBox.setText(ID_VIEW);
  397.    bitmapButton.setText(ID_BITMAP);
  398.    textButton.setText(ID_TEXT);
  399.    bitmapAndTextButton.setText(ID_BITMAP_AND_TEXT);
  400.  
  401.    addToCell(&locationBox,2,2,6,13);
  402.    addToCell(&topButton,4,4,2);
  403.    addToCell(&leftButton,4,5,2);
  404.    addToCell(&bottomButton,4,6,2);
  405.    addToCell(&rightButton,4,7,2);
  406.    addToCell(&groupCheckBox,5,8);
  407.    addToCell(&floatingButton,4,10,2);
  408.    addToCell(&hiddenButton,4,12,2);
  409.  
  410.    addToCell(&viewBox,9,2,5,7);
  411.    addToCell(&bitmapButton,11,4);
  412.    addToCell(&textButton,11,5);
  413.    addToCell(&bitmapAndTextButton,11,6);
  414.  
  415.    switch ( editorFrame.toolBar(this->id()).location() )
  416.    {
  417.       case IToolBar::aboveClient:
  418.          topButton.select();
  419.          break;
  420.       case IToolBar::belowClient:
  421.          bottomButton.select();
  422.          break;
  423.       case IToolBar::leftOfClient:
  424.          leftButton.select();
  425.          break;
  426.       case IToolBar::rightOfClient:
  427.          rightButton.select();
  428.          break;
  429.       case IToolBar::floating:
  430.          floatingButton.select();
  431.          break;
  432.       case IToolBar::hidden:
  433.          hiddenButton.select();
  434.          break;
  435.    }
  436.  
  437.    switch ( editorFrame.toolBar(this->id()).buttonView() )
  438.    {
  439.       case IToolBarButton::bitmapView:
  440.          bitmapButton.select();
  441.          break;
  442.       case IToolBarButton::textView:
  443.          textButton.select();
  444.          break;
  445.       case IToolBarButton::bitmapAndTextView:
  446.          bitmapAndTextButton.select();
  447.          break;
  448.    }
  449.  
  450.    if ( !editorFrame.toolBar(this->id()).isGroup() )
  451.       groupCheckBox.select();
  452.  
  453.    pageHandler.handleEventsFor(this);
  454.    nbk->addLastPage(settings,this);
  455.    sizeTo(minimumSize());
  456. }
  457.  
  458. //-------------------------------------------------------------------
  459. // Handle selection events for controls on the page
  460. //-------------------------------------------------------------------
  461. Boolean PageHandler::selected ( IControlEvent& event )
  462. {
  463.    switch ( event.controlWindow()->id() )
  464.    {
  465.       case ID_TOP:
  466.          editorFrame.toolBar(event.window()->id()).setLocation
  467.                             (IToolBar::aboveClient);
  468.          break;
  469.       case ID_BOTTOM:
  470.          editorFrame.toolBar(event.window()->id()).setLocation
  471.                             (IToolBar::belowClient);
  472.          break;
  473.       case ID_LEFT:
  474.          editorFrame.toolBar(event.window()->id()).setLocation
  475.                             (IToolBar::leftOfClient);
  476.          break;
  477.       case ID_RIGHT:
  478.          editorFrame.toolBar(event.window()->id()).setLocation
  479.                             (IToolBar::rightOfClient);
  480.          break;
  481.       case ID_FLOATING:
  482.          editorFrame.toolBar(event.window()->id()).setLocation
  483.                             (IToolBar::floating);
  484.          break;
  485.       case ID_HIDDEN:
  486.          editorFrame.toolBar(event.window()->id()).setLocation
  487.                             (IToolBar::hidden);
  488.          break;
  489.       case ID_GROUP:
  490.          {
  491.            ICheckBox* checkBox = (ICheckBox*) event.controlWindow();
  492.            editorFrame.toolBar(event.window()->id()).enableGroup
  493.                               (!checkBox->isSelected());
  494.  
  495.            switch (editorFrame.toolBar(event.window()->id()).location())
  496.            {
  497.              case IToolBar::aboveClient:
  498.              case IToolBar::belowClient:
  499.              case IToolBar::leftOfClient:
  500.              case IToolBar::rightOfClient:
  501.                 editorFrame.toolBar(event.window()->id()).parent()->
  502.                             setLayoutDistorted(IWindow::layoutChanged |
  503.                                                IWindow::immediateUpdate,0);
  504.                 break;
  505.            }
  506.          }
  507.          break;
  508.       case ID_BITMAP:
  509.          editorFrame.toolBar(event.window()->id()).setButtonView
  510.                             (IToolBarButton::bitmapView);
  511.          break;
  512.       case ID_TEXT:
  513.          editorFrame.toolBar(event.window()->id()).setButtonView
  514.                             (IToolBarButton::textView);
  515.          break;
  516.       case ID_BITMAP_AND_TEXT:
  517.          editorFrame.toolBar(event.window()->id()).setButtonView
  518.                             (IToolBarButton::bitmapAndTextView);
  519.          break;
  520.    }
  521.    return false;
  522. }
  523.  
  524.