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 >
Wrap
Text File
|
1995-05-01
|
19KB
|
524 lines
#ifndef _IBASE_ //Make sure ibase.hpp is included
#include <ibase.hpp> // since that is where IC_<environ>
#endif // is defined.
#include <iapp.hpp>
#include <iframe.hpp>
#include <ifont.hpp>
#include <itbar.hpp>
#include <itbarbut.hpp>
#include <imle.hpp>
#include <icmdhdr.hpp>
#include <imenubar.hpp>
#include <iflytext.hpp>
#include <istattxt.hpp>
#include <iflyhhdr.hpp>
#include <icombobx.hpp>
#include <iselhdr.hpp>
#include <idmhndlr.hpp>
#include <isubmenu.hpp>
#include <ifontdlg.hpp>
#include <inotebk.hpp>
#include <ititle.hpp>
#include <imcelcv.hpp>
#include <iradiobt.hpp>
#include <icheckbx.hpp>
#include <igroupbx.hpp>
#include <ifiledlg.hpp>
#include <icolor.hpp>
#include "tbar2.hpp"
#include "tbar2.h"
//-------------------------------------------------------------------
// Main routine
//
// This routine creates and shows the editor window
//-------------------------------------------------------------------
int main()
{
Editor editor;
editor.show();
editor.setFocus();
IApplication::current().run();
return 0;
}
//-------------------------------------------------------------------
// Editor::Editor
//
// This constructor initializes all of the windows (including the
// tool bar and tool bar buttons).
//
// Note: The tool bar buttons are created with standard identifiers
// that are provided by the class library (defined in icconst.h) for
// standard operations.
//-------------------------------------------------------------------
Editor::Editor ()
:IFrameWindow("Tool Bar Sample 2", ID_MAIN_WINDOW),
fileToolBar(ID_FILE, this),
editToolBar(ID_EDIT, &fileToolBar, true),
fontToolBar(ID_FONT, &editToolBar, true),
flyText(ID_FLYTEXT, this),
infoText(ID_INFOTEXT, this, this),
flyHelpHandler(&flyText, &infoText, 0, 0),
editWindow(ID_EDITOR, *this),
commandHandler(*this),
fontSelectHandler(*this),
openButton (IC_ID_OPEN, &fileToolBar, &fileToolBar),
saveButton (IC_ID_SAVE, &fileToolBar, &fileToolBar),
cutButton (IC_ID_CUT, &editToolBar, &editToolBar),
copyButton (IC_ID_COPY, &editToolBar, &editToolBar),
pasteButton (IC_ID_PASTE, &editToolBar, &editToolBar),
boldButton (IC_ID_BOLD, &fontToolBar, &fontToolBar,
IRectangle(), IToolBarButton::defaultStyle() |
IToolBarButton::noDragDelete ),
italicButton (IC_ID_ITALIC, &fontToolBar, &fontToolBar,
IRectangle(), IToolBarButton::defaultStyle() |
IToolBarButton::noDragDelete ),
underscoreButton(IC_ID_UNDERSCORE, &fontToolBar, &fontToolBar,
IRectangle(), IToolBarButton::defaultStyle() |
IToolBarButton::noDragDelete ),
fontCombo(ID_FONTCOMBO, &fontToolBar, &fontToolBar, IRectangle(),
IComboBox::classDefaultStyle &~IComboBox::simpleType |
IComboBox::dropDownListType),
menu(ID_MAIN_WINDOW,this),
editFont()
{
// Set the icon
setIcon( id() );
// Add buttons to the file toolbar
fileToolBar.addAsLast(&openButton,true);
fileToolBar.addAsLast(&saveButton);
// Add buttons to the edit toolbar
editToolBar.addAsLast(&cutButton,true);
editToolBar.addAsLast(©Button);
editToolBar.addAsLast(&pasteButton);
// Add buttons to the font toolbar
fontToolBar.addAsLast(&boldButton,true);
fontToolBar.addAsLast(&italicButton);
fontToolBar.addAsLast(&underscoreButton);
fontToolBar.addAsLast(&fontCombo,true);
// Set up latchable style for font property buttons
boldButton.enableLatching();
italicButton.enableLatching();
underscoreButton.enableLatching();
// Load up font combo box with face names
fontCombo.setLimit(10);
IFont::FaceNameCursor fontCursor;
for ( fontCursor.setToFirst(); fontCursor.isValid(); fontCursor.setToNext())
{
IString faceName = IFont::faceNameAt(fontCursor);
fontCombo.addAsLast(faceName);
if (faceName.length() > fontCombo.limit())
fontCombo.setLimit(faceName.length());
}
updateFontToolBar();
// Set up titles for toolbars when floating
fileToolBar.setFloatingTitle(ID_FILE);
editToolBar.setFloatingTitle(ID_EDIT);
fontToolBar.setFloatingTitle(ID_FONT);
// Setup the editor
setClient(&editWindow);
editWindow.setFont(editFont);
editWindow.importFromFile("toolbar2.not");
editWindow.setTop(1);
// Add the Info frame extension
addExtension(&infoText, IFrameWindow::belowClient);
// Set up and add the help handler
flyHelpHandler.setLongStringTableOffset(OFFSET_INFOTEXT);
flyHelpHandler.setDefaultText("\0");
flyHelpHandler.handleEventsFor(&fileToolBar);
flyHelpHandler.handleEventsFor(&editToolBar);
flyHelpHandler.handleEventsFor(&fontToolBar);
// Attach the Command Handler to frame and toolbar
commandHandler.handleEventsFor(this);
commandHandler.handleEventsFor(&fileToolBar);
commandHandler.handleEventsFor(&editToolBar);
commandHandler.handleEventsFor(&fontToolBar);
// Add the handler to change the font
fontSelectHandler.handleEventsFor(&fontCombo);
// Set up drag from menu
fileSubmenu = new IWindow( menu.menuItem(ID_FILE).submenuHandle() );
IDMHandler::enableDragFrom( (ISubmenu*) fileSubmenu );
editSubmenu = new IWindow( menu.menuItem(ID_EDIT).submenuHandle() );
IDMHandler::enableDragFrom( (ISubmenu*) editSubmenu );
moveSizeToClient(IRectangle(IPoint(100,100),
ISize(editFont.avgCharWidth()*80,
editFont.maxCharHeight()*15)));
}
//-------------------------------------------------------------------
// Handle user menu bar selections
//-------------------------------------------------------------------
Boolean EditorCommandHandler::command(ICommandEvent &event)
{
switch (event.commandId())
{
case IC_ID_OPEN:
{
ITitle title(&editorFrame);
IFileDialog::Settings settings;
settings.setOpenDialog();
settings.setFileName(title.text());
IFileDialog openDlg(IWindow::desktopWindow(),&editorFrame,settings);
if ( openDlg.pressedOK() )
{
editorFrame.disableUpdate();
editorFrame.editorWindow().removeAll();
editorFrame.editorWindow().importFromFile(openDlg.fileName());
editorFrame.enableUpdate();
editorFrame.editorWindow().setTop(1);
title.setText(openDlg.fileName());
}
break;
}
case IC_ID_SAVE:
{
ITitle title(&editorFrame);
IFileDialog::Settings settings;
settings.setSaveAsDialog();
settings.setFileName(title.text());
IFileDialog saveDlg(IWindow::desktopWindow(),&editorFrame,settings);
if ( saveDlg.pressedOK() )
{
editorFrame.editorWindow().exportToFile(saveDlg.fileName());
title.setText(saveDlg.fileName());
}
break;
}
case IC_ID_CUT:
{
if(editorFrame.editorWindow().hasSelectedText())
editorFrame.editorWindow().cut();
break;
}
case IC_ID_COPY:
{
if(editorFrame.editorWindow().hasSelectedText())
editorFrame.editorWindow().copy();
break;
}
case IC_ID_PASTE:
{
if(editorFrame.editorWindow().clipboardHasTextFormat())
editorFrame.editorWindow().paste();
break;
}
case IC_ID_BOLD:
{
editorFrame.editorFont().setBold(!editorFrame.editorFont().isBold());
editorFrame.editorWindow().setFont(editorFrame.editorFont());
break;
}
case IC_ID_ITALIC:
{
editorFrame.editorFont().setItalic(!editorFrame.editorFont().isItalic());
editorFrame.editorWindow().setFont(editorFrame.editorFont());
break;
}
case IC_ID_UNDERSCORE:
{
editorFrame.editorFont().setUnderscore(!editorFrame.editorFont().isUnderscore());
editorFrame.editorWindow().setFont(editorFrame.editorFont());
break;
}
case ID_FONT:
{
IFontDialog::Settings settings(&editorFrame.editorFont());
IFontDialog dlg(IWindow::desktopWindow(),&editorFrame,settings);
if ( dlg.pressedOK() )
{
editorFrame.updateFontToolBar();
editorFrame.editorWindow().setFont(editorFrame.editorFont());
}
break;
}
case ID_TOOLBARS:
{
ToolBarNotebook nbk(editorFrame);
break;
}
}
return true;
}
//-------------------------------------------------------------------
// Update font toolbar to reflect the current font
//-------------------------------------------------------------------
Editor& Editor::updateFontToolBar()
{
// Update toolbar buttons (if they have not been dragged to the shredder)
if (boldButton.isValid())
boldButton.latch(editFont.isBold());
if (italicButton.isValid())
italicButton.latch(editFont.isItalic());
if (underscoreButton.isValid())
underscoreButton.latch(editFont.isUnderscore());
// Update the combo box with the correct face name
fontCombo.select(fontCombo.locateText(editFont.name()));
return *this;
}
//-------------------------------------------------------------------
// Get the toolbar
//-------------------------------------------------------------------
IToolBar& Editor::toolBar ( unsigned long id )
{
if ( id == ID_FILE )
return fileToolBar;
if ( id == ID_EDIT )
return editToolBar;
return fontToolBar;
}
//-------------------------------------------------------------------
// Handle combo box selections
//-------------------------------------------------------------------
Boolean FontSelectHandler::enter( IControlEvent& event)
{
IString fontChoice = ((IComboBox*)event.controlWindow())->text();
if(fontChoice.length())
{
// Set the new font
editorFrame.editorFont().setName(fontChoice);
editorFrame.editorWindow().setFont(editorFrame.editorFont());
}
return true;
}
//-------------------------------------------------------------------
// Check for font changes to the MLE
//-------------------------------------------------------------------
ITextControl& EditorMLE::setLayoutDistorted ( unsigned long layoutAttributesOn,
unsigned long layoutAttributesOff )
{
IMultiLineEdit::setLayoutDistorted(layoutAttributesOn,layoutAttributesOff);
if ( layoutAttributesOn & IWindow::fontChanged )
{
IFont newFont(editorFrame.editorWindow().presSpace());
editorFrame.editorFont() = newFont;
editorFrame.updateFontToolBar();
editorFrame.editorWindow().setFont(editorFrame.editorFont());
}
return *this;
}
//-------------------------------------------------------------------
// ToolBarNotebook::ToolBarNotebook
//-------------------------------------------------------------------
ToolBarNotebook::ToolBarNotebook ( Editor& editor )
:IFrameWindow ( ID_TOOLBAR_WINDOW, IWindow::desktopWindow(),
(IWindow*) &editor, IRectangle(),
IFrameWindow::titleBar |
IFrameWindow::systemMenu |
IFrameWindow::dialogBorder |
IFrameWindow::appDBCSStatus ),
editorFrame(editor),
notebook(ID_NOTEBOOK,this,this)
{
setClient(¬ebook);
ITitle title(this);
title.setText(ID_TOOLBARS);
notebook.setBinding(INotebook::spiral);
notebook.setMinorTabSize(ISize(0,0));
// Add notebook pages
ToolBarPage filePage(ID_FILE,¬ebook,editor);
ToolBarPage editPage(ID_EDIT,¬ebook,editor);
ToolBarPage fontPage(ID_FONT,¬ebook,editor);
// Center the notebook on the screen
IRectangle clientRect;
clientRect.sizeTo(notebook.minimumSize());
clientRect.moveTo(IPoint((IWindow::desktopWindow()->size().width() -
clientRect.width())/2,
(IWindow::desktopWindow()->size().height() -
clientRect.height())/2));
moveSizeToClient(clientRect);
setFocus();
showModally();
}
//-------------------------------------------------------------------
// ToolBarPage::ToolBarPage
//-------------------------------------------------------------------
ToolBarPage::ToolBarPage ( unsigned long id, INotebook* nbk, Editor& editor )
:IMultiCellCanvas(id,nbk,nbk),
editorFrame(editor),
locationBox(ID_LOCATION,this,this),
topButton(ID_TOP,this,this,IRectangle(),
IRadioButton::defaultStyle()|IControl::group),
leftButton(ID_LEFT,this,this),
bottomButton(ID_BOTTOM,this,this),
rightButton(ID_RIGHT,this,this),
groupCheckBox(ID_GROUP,this,this),
floatingButton(ID_FLOATING,this,this),
hiddenButton(ID_HIDDEN,this,this),
viewBox(ID_VIEW,this,this),
bitmapButton(ID_BITMAP,this,this,IRectangle(),
IRadioButton::defaultStyle()|IControl::group),
textButton(ID_TEXT,this,this),
bitmapAndTextButton(ID_BITMAP_AND_TEXT,this,this),
pageHandler(nbk,editor)
{
INotebook::PageSettings settings(INotebook::PageSettings::autoPageSize |
INotebook::PageSettings::majorTab);
settings.setTabText(id);
locationBox.setText(ID_LOCATION);
topButton.setText(ID_TOP);
leftButton.setText(ID_LEFT);
bottomButton.setText(ID_BOTTOM);
rightButton.setText(ID_RIGHT);
groupCheckBox.setText(ID_GROUP);
floatingButton.setText(ID_FLOATING);
hiddenButton.setText(ID_HIDDEN);
viewBox.setText(ID_VIEW);
bitmapButton.setText(ID_BITMAP);
textButton.setText(ID_TEXT);
bitmapAndTextButton.setText(ID_BITMAP_AND_TEXT);
addToCell(&locationBox,2,2,6,13);
addToCell(&topButton,4,4,2);
addToCell(&leftButton,4,5,2);
addToCell(&bottomButton,4,6,2);
addToCell(&rightButton,4,7,2);
addToCell(&groupCheckBox,5,8);
addToCell(&floatingButton,4,10,2);
addToCell(&hiddenButton,4,12,2);
addToCell(&viewBox,9,2,5,7);
addToCell(&bitmapButton,11,4);
addToCell(&textButton,11,5);
addToCell(&bitmapAndTextButton,11,6);
switch ( editorFrame.toolBar(this->id()).location() )
{
case IToolBar::aboveClient:
topButton.select();
break;
case IToolBar::belowClient:
bottomButton.select();
break;
case IToolBar::leftOfClient:
leftButton.select();
break;
case IToolBar::rightOfClient:
rightButton.select();
break;
case IToolBar::floating:
floatingButton.select();
break;
case IToolBar::hidden:
hiddenButton.select();
break;
}
switch ( editorFrame.toolBar(this->id()).buttonView() )
{
case IToolBarButton::bitmapView:
bitmapButton.select();
break;
case IToolBarButton::textView:
textButton.select();
break;
case IToolBarButton::bitmapAndTextView:
bitmapAndTextButton.select();
break;
}
if ( !editorFrame.toolBar(this->id()).isGroup() )
groupCheckBox.select();
pageHandler.handleEventsFor(this);
nbk->addLastPage(settings,this);
sizeTo(minimumSize());
}
//-------------------------------------------------------------------
// Handle selection events for controls on the page
//-------------------------------------------------------------------
Boolean PageHandler::selected ( IControlEvent& event )
{
switch ( event.controlWindow()->id() )
{
case ID_TOP:
editorFrame.toolBar(event.window()->id()).setLocation
(IToolBar::aboveClient);
break;
case ID_BOTTOM:
editorFrame.toolBar(event.window()->id()).setLocation
(IToolBar::belowClient);
break;
case ID_LEFT:
editorFrame.toolBar(event.window()->id()).setLocation
(IToolBar::leftOfClient);
break;
case ID_RIGHT:
editorFrame.toolBar(event.window()->id()).setLocation
(IToolBar::rightOfClient);
break;
case ID_FLOATING:
editorFrame.toolBar(event.window()->id()).setLocation
(IToolBar::floating);
break;
case ID_HIDDEN:
editorFrame.toolBar(event.window()->id()).setLocation
(IToolBar::hidden);
break;
case ID_GROUP:
{
ICheckBox* checkBox = (ICheckBox*) event.controlWindow();
editorFrame.toolBar(event.window()->id()).enableGroup
(!checkBox->isSelected());
switch (editorFrame.toolBar(event.window()->id()).location())
{
case IToolBar::aboveClient:
case IToolBar::belowClient:
case IToolBar::leftOfClient:
case IToolBar::rightOfClient:
editorFrame.toolBar(event.window()->id()).parent()->
setLayoutDistorted(IWindow::layoutChanged |
IWindow::immediateUpdate,0);
break;
}
}
break;
case ID_BITMAP:
editorFrame.toolBar(event.window()->id()).setButtonView
(IToolBarButton::bitmapView);
break;
case ID_TEXT:
editorFrame.toolBar(event.window()->id()).setButtonView
(IToolBarButton::textView);
break;
case ID_BITMAP_AND_TEXT:
editorFrame.toolBar(event.window()->id()).setButtonView
(IToolBarButton::bitmapAndTextView);
break;
}
return false;
}