Objective Views : Chapter 3 Quick Tour : Creating Code for the Toolbar
Creating Code for the Toolbar
Now we need to add some new toolbar commands and write handlers for those commands.
NOTE >> The following procedure is applicable if the MFC feature pack is not used. Otherwise, see the document <stingray-installdir>\Docs\Stingray Feature Pack Migration, and the sample <stingray-installdir>\Samples\FoundationEx\ShowcaseEx.
1. First, let’s add the toolbar to the frame window. Declare the new toolbar in the CMainFrame class:
 
class CMainFrame : public CFrameWnd
{
. . .
protected: // control bar embedded members
. . .
CToolBar m_wndSymbolBar; // Symbol toolbar
. . .
};
2. To create the toolbar, you need to add some code to the frame window class in the source file. Find the CMainFrame::OnCreate() method and update it as follows. Make sure you add this code before the EnableDocking(CBRS_ALIGN_ANY) call.
 
if (!m_wndSymbolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD |
WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS |
CBRS_FLYBY | CBRS_SIZE_DYNAMIC)||
!m_wndSymbolBar.LoadToolBar(IDR_SYMBOLBAR))
{
TRACE0 ("Failed to create symbol toolbar \n");
return -1; //fail to create
}
3. The EnableDocking member function of the newly created toolbar must be called:
 
m_wndSymbolBar.EnableDocking(CBRS_ALIGN_ANY);
4. Add the following code after the EnableDocking(CBRS_ALIGN_ANY) call:
 
DockControlBar(&m_wndSymbolBar);
5. Now add command handlers for the commands on the toolbar. The ID_OD_DRAW_SELECT and ID_OD_LINK_SYMBOLS commands already have handlers in the Objective Views CODController class. The controller is the piece of the canvas that handles events and translates them into actions. The AppWizard generated a controller class called CSimpleController for our project, derived from the CODController class. You need to add command handlers to the CSimpleController class to insert your custom symbols.
Add the following function declarations to CSimpleController:
 
afx_msg void OnCircle();
afx_msg void OnStingray();
. . .
Add the following entries to the CSimpleController message map:
 
BEGIN_MESSAGE_MAP(CSimpleController, CODController)
//{{AFX_MSG_MAP(CSimpleController)
ON_COMMAND(ID_CIRCLE, OnCircle)
ON_COMMAND(ID_STINGRAY, OnStingray)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
. . .
Add the implementation of the command handlers to the CSimpleController .cpp file.
 
. . .
void CSimpleController::OnCircle()
{
CODSymbolComponent* pSymbol = new CODSymbolComponent();
if(NULL!=pSymbol)
pSymbol->Create(IDR_CIRCLE);
OnInsertSymbol(pSymbol);
}
 
void CSimpleController::OnStingray()
{
CODSymbolComponent* pSymbol = new CODSymbolComponent();
if(NULL!=pSymbol)
pSymbol->Create(IDR_STINGRAY);
OnInsertSymbol(pSymbol);
}
6. Now you’re ready to compile the application and execute it. Your compiled application should appear as in Figure 29.
Figure 29 – Sample compiled application
 
Experiment with your new application. Create the canvas in Figure 30 using the tool buttons you just created.
Figure 30 – Sample canvas