Stingray Studio : Objective Views User’s Guide : Chapter 3 Quick Tour : Integrate Objective Views into an Existing Project
Integrate Objective Views into an Existing Project
To add Objective Views functionality to an existing program, you need to complete the steps that Objective Views AppWizard automatically implements when it creates a skeleton application. In the following tutorial, you add code to a typical MDI application so you can use the Objective Views Library. If you have a different configuration, some of these steps may differ.
Including Objective Views Files
1. Let’s include the Objective Views header files. Open the file stdafx.h and add the following line near the end of the file:
 
#include “views\OdAll.h”
2. Now, add the Objective Views resources from the View menu and select Resource Includes.
3. In the Read-only symbol directives box, type the following code on the last line:
 
#include “views\OdRes.h”
4. In the Compile-time directives box, type the following line:
 
#include “views\OdRes.rc”
5. Click the OK button. A warning dialog appears. Click the OK button again.
6. Include header files that are common for all Stingray Studio products:
*Add #include <SupportedPlatforms.h> toward the top of the application’s stdafx.h file.
*Add #include <ManifestDefs.h> at the end of the include statements.
Adding MVC Architecture and OLE Support
If you have configured your application to include MVC architecture and OLE support, use the following steps to add them.
Add the Property Cache
1. Let’s add the global Property Cache. Open the source file that holds the implementation of your main application object. If you were working with a project named Foobar, this would be called Foobar.cpp.
2. After the declaration of the application object (theApp), add the declaration of the global property cache object:
 
CODPropertyCache thePropCache;
Add the Model and Support for OLE Drag-and-Drop
1. Now, add a Model to your document object. Open your document’s header file (for example, FoobarDoc.h).
2. Add your model or the default model CODModel as a member variable:
 
CODModel m_mdlCanvas;
3. Now, add an accessor method for your model:
 
CODModel* GetModel() { return &m_mdlCanvas; }
4. Open your document’s source file (for example, FoobarDoc.cpp).
5. Add the model to the Serialize() method.
 
void CFoobarDoc::Serialize(CArchive& ar)
{
m_mdlCanvas.Serialize(ar);
 
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
//TODO: add loading code here
}
}
6. Now, add support for OLE drag-and-drop. If you would like to use the OLE drag-and-drop features in addition to Objective Views drag-and-drop, you need to initialize the OLE libraries. Ensure that the following code is at the beginning of your application object’s InitInstance() method.
 
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
7. Ensure that your string table contains a string resource for IDR_OLE_INIT_FAILED. If it does not, add one.
Add the Viewport
1. Let’s add a viewport to your view object. Open your view’s header file (for example, FoobarView.h).
2. Add your viewport or one of the standard viewports supplied with Objective Views as a member variable. The CODScrollViewport has scrollbars and the CODViewport does not.
 
CODScrollViewport m_vpCanvas;
3. Now, you need to add the viewport to the CView::OnDraw() method: In your view’s source file, add a call to the Refresh() method in the viewport’s OnDraw() method:
 
m_vpCanvas.Refresh(pDC);
4. Add the OnCreate() method to your view object. Use the Class Wizard to add a handler to your view for the WM_CREATE message.
5. In the resulting OnCreate() method, add code to attach the model to the viewport.
 
int CFoobarView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
 
CFoobarDoc* pDoc = GetDocument();
CODModel* pModel = pDoc->GetModel();
m_vpCanvas.Create(this, NULL);
m_vpCanvas.SetModel(pModel);
 
return 0;
}
6. Now, add the OnInitialUpdate() method to your view object. Use the Class Wizard to add an override method for OnInitialUpdate().
7. In the OnInitialUpdate() method, call the OnInitialUpdate() method of the viewport.
 
void CFoobarView::OnInitialUpdate()
{
CView::OnInitialUpdate();
m_vpCanvas.OnInitialUpdate();
}
8. Let’s add the OnSize() method to your view object. Use the Class Wizard to add a handler for the WM_SIZE message.
9. On the OnSize() method, call the SetSize() method for the viewport.
 
void CFoobarView::OnSize(UINT nType, int cx, int cy)
{
m_vpCanvas.SetSize(cx, cy);
CView::OnSize(nType, cx, cy);
}
10. Override OnCmdMsg() in your view object. In the view’s header file, add a public declaration for OnCmdMsg():
 
virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo);
11. In the view’s source file, add an implementation of OnCmdMsg() that gives the viewport access to the messages:
 
BOOL CFoobarView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo)
{
// first pump through normal channels. This allows you to
// override the components default handling inside the view
// class.
if (CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
 
// then pump through the viewport
BOOL bHandled = FALSE;
if (m_pDocument != NULL)
bHandled = m_vpCanvas.OnCmdMsg(nID, nCode, pExtra,
pHandlerInfo);
 
return bHandled;
}
12. Let’s override OnWndMsg() in your view object. In the view’s header file, add a public declaration for OnWndMsg():
 
virtual BOOL OnWndMsg(UINT message, WPARAM wParam,
LPARAM lParam, LRESULT* pResult);
13. In the view’s source file, add an implementation of OnWndMsg() that gives the viewport access to the messages:
 
BOOL CFoobarView::OnWndMsg(UINT message, WPARAM wParam,
LPARAM lParam, LRESULT* pResult)
{
// give the viewport a chance to route the message to a
//controller.
BOOL bHandled = m_vpCanvas.OnWndMsg(message, wParam,
lParam, pResult);
 
if( !bHandled )
{
bHandled = CView::OnWndMsg(message, wParam, lParam,
pResult);
}
return bHandled;
}
At this point, you can build and run the application to verify that you did everything correctly so far. Make sure that you have built the OV libraries (they should be in the <stingray-installdir>\Lib directory). A view with a canvas should appear on the screen. There are no commands in place to do anything with it. If a grid of dots is visible, the application is working correctly.
Adding Menu Items and Accelerators
1. Now, add Objective Views menu items. If you want menu items that interact with the view, you can copy them from the Objective Views resources into your own. Make sure you do not move them from the Objective Views resources. Open your application’s resource file and then open Objective Views’s resource file, which is stored in the Objective Views Include directory.
2. Highlight the menu items you want to include in your application from Objective Views’s View menu and then copy them.
3. Select the empty space in your application’s View menu and paste in the items. Then, delete the string resources for these menu items from your string table. Each resource ID for the menu items will be prefixed by ID_OD_.
4. Now let’s add Objective Views accelerators. The OdRes.rc file contains a set of common accelerators used with Objective Views.
5. Select the accelerators you want to use in your application and then copy them.
6. Select the empty slot in your application’s accelerator table and then paste the accelerators.
Adding Objective Views Toolbars
Now, add Objective Views toolbars. The Objective Views resources include several defined toolbars that fire commands recognized by the CODController. They are divided by function:
Drawing Toolbar
IDR_OD_DRAWING
Rotation Toolbar
IDR_OD_ROTATE
Structure Toolbar
IDR_OD_STRUCTURE
Nudge Toolbar
IDR_OD_NUDGE
Alignment Toolbar
IDR_OD_ALIGN
Zoom/Pan Toolbar
IDR_OD_ZOOM_PAN
Link Toolbar
IDR_OD_LINK
Layout Toolbar
IDR_OD_LAYOUT
Canvas Toolbar
IDR_OD_CANVAS
You can include any combination of these toolbars. You can also choose individual buttons to create toolbars with specific functionality.
NOTE >> If you want to use the standard Objective Views toolbars, you do not need to copy them into your application’s resources because the toolbars are imported from OdRes.rc at compile time. If you want to make your own custom toolbars, you need to create new ones in your project and copy the buttons from OdRes.rc.
NOTE >> The following procedure is applicable if the MFC feature pack is not used. Otherwise, see the document Stingray Feature Pack Migration and the ShowcaseEx sample.
1. You add an OV toolbar to your frame window just like you would any other toolbar. In your main frame’s header file (for example, MainFrm.h), add a member variable for the toolbar:
 
CToolBar m_wndDrawingBar;
2. In your main frame’s source file (for example, MainFrm.cpp), add code to create the toolbar in the OnCreate() method:
 
if (!m_wndDrawingBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD |
WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS |
CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndDrawingBar.LoadToolBar(IDR_OD_DRAWING))
{
TRACE0("Failed to create drawingbar\n");
return -1; // fail to create
}
3. To make the toolbar dockable, type the following line to enable docking.
 
m_wndDrawingBar.EnableDocking(CBRS_ALIGN_ANY);
4. Set the name of the toolbar, which appears in the caption when it is floating.
 
m_wndDrawingBar.SetWindowText("Drawing");
5. If you want to dock toolbars to the frame window, add the following line to the OnCreate() method.
 
EnableDocking(CBRS_ALIGN_ANY);
6. Now, you can dock all the toolbars you created with calls like the following.
 
DockControlBar(&m_wndDrawingBar);
7. Objective Views’s CODGlobal class has a static utility method for docking one toolbar to the left of another. To use it, make a call like the following.
 
CODGlobal::DockControlBarLeftOf(this, &m_wndDrawingBar, &m_wndToolBar);
Now you can use your MDI application with Objective Views. With Objective Views, you can define your own symbols, add them to the toolbar, and then let Objective Views integrate them into the application.
To add symbols, you need to handle more messages than the default CODController contains. To add functionality to the controller, derive your own controller from it and add whatever behavior you need. When you derive the controller, you also need to derive a new class from CODViewport to create the new controller.