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.
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" |
Now, add the Objective Views resources from the View menu and select Resource Includes.
In the Read-only symbol directives box, type the following code on the last line:
#include "views\OdRes.h" |
In the Compile-time directives box, type the following line:
#include "views\OdRes.rc" |
Click the OK button. A warning dialog appears. Click the OK button again.
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.
If you have configured your application to include MVC architecture and OLE support, use the following steps to add them.
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.
After the declaration of the application object (theApp), add the declaration of the global property cache object:
CODPropertyCache thePropCache; |
Now, add a Model to your document object. Open your document's header file (for example, FoobarDoc.h).
Add your model or the default model CODModel as a member variable:
CODModel m_mdlCanvas; |
Now, add an accessor method for your model:
CODModel* GetModel() { return &m_mdlCanvas; } |
Open your document's source file (for example, FoobarDoc.cpp).
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 } } |
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; } |
Ensure that your string table contains a string resource for IDR_OLE_INIT_FAILED. If it does not, add one.
Let's add a viewport to your view object. Open your view's header file (for example, FoobarView.h).
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; |
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); |
Add the OnCreate() method to your view object. Use the Class Wizard to add a handler to your view for the WM_CREATE message.
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; } |
Now, add the OnInitialUpdate() method to your view object. Use the Class Wizard to add an override method for OnInitialUpdate().
In the OnInitialUpdate() method, call the OnInitialUpdate() method of the viewport.
void CFoobarView::OnInitialUpdate() { CView::OnInitialUpdate(); m_vpCanvas.OnInitialUpdate(); } |
Let's add the OnSize() method to your view object. Use the Class Wizard to add a handler for the WM_SIZE message.
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); } |
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); |
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; } |
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); |
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.
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.
Highlight the menu items you want to include in your application from Objective Views's View menu and then copy them.
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_.
Now let's add Objective Views accelerators. The OdRes.rc file contains a set of common accelerators used with Objective Views.
Select the accelerators you want to use in your application and then copy them.
Select the empty slot in your application's accelerator table and then paste the accelerators.
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.
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.
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.
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; |
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 } |
To make the toolbar dockable, type the following line to enable docking.
m_wndDrawingBar.EnableDocking(CBRS_ALIGN_ANY); |
Set the name of the toolbar, which appears in the caption when it is floating.
m_wndDrawingBar.SetWindowText("Drawing"); |
If you want to dock toolbars to the frame window, add the following line to the OnCreate() method.
EnableDocking(CBRS_ALIGN_ANY); |
Now, you can dock all the toolbars you created with calls like the following.
DockControlBar(&m_wndDrawingBar); |
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.
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.