Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The Server View

Because the Server program is a full server, you can open it as a standalone program, which means that we should display the text in the view’s OnDraw() function. (The other server option, mini-servers, can’t be opened as standalone programs.)

void CServerView::OnDraw(CDC* pDC)
{
    CServerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDC->TextOut(0, 0, pDoc->text);                                 ⇐

}

We’ve displayed the server item’s text in both the Server Item class (used when the item is embedded) and the view (used when you edit the item). However, we haven’t allowed the user any way of editing the server item when it’s actually opened for editing. Let’s do that now.

Editing the Server Item in Place

To let the user change the text in the server item, we add a new item to the Edit menu in the IDR_SERVERTYPE_SRVR_IP menu resource. This menu takes over the container’s menu system when the server item is opened for editing.

Add a new item to the Edit menu now: Insert new text. When the user selects this item, we can change the text from “Hello from the server!” to “The new text...”

void CServerView::OnEditInsertnewtext()
{
    // TODO: Add your command handler code here
    CServerDoc* pDoc = GetDocument();                               ⇐
    ASSERT_VALID(pDoc);                                             ⇐

    pDoc->text = “The new text...”;                                 ⇐
        .
        .
        .
}


Handling Events in Servers

You can add event handlers to handle such messages as WM_LBUTTONDOWN to the Server program to handle those messages when the server item is open for editing.



Figure 12.6  Our server object open for in-place editing.


Figure 12.7  Updating our server item.

To install this new text in the embedded server item, we also call the document’s NotifyChanged() function as well as its SetModifiedFlag() function.

void CServerView::OnEditInsertnewtext()
{
    // TODO: Add your command handler code here
    CServerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDoc->text = “The new text...”;

    pDoc->NotifyChanged();                                          ⇐
    pDoc->SetModifiedFlag();                                        ⇐

}

Run the Server program once. Doing so registers the new server item type with Windows. Now we can embed our OLE item in, for example, Microsoft Word. To do so, select the Insert menu’s Object item and double-click the entry Server document in the Object type box to embed a new server item in Word, as shown in Figure 12.6.

The new OLE item is already open for in-place editing. To change the text in that item from “Hello from the server!” to “The new text...”, select the Insert new text item in Word’s Edit menu, which is now taken over by our server item’s IDR_SERVERTYPE_SRVR_IP menu. The result appears in Figure 12.7now we’re supporting servers and allowing the user to work with the data in our server. In this way, we’ve been able to support both OLE servers and containers. The process is straightforward using the code AppWizard gives us.

The code for this example, SrvrItem.h and SrvrItem.cpp, appears in Listing 12.3; the code for ServerView.h and ServerView.cpp appears in Listing 12.4.


Listing 12.3 SrvrItem.h and SrvrItem.cpp

// SrvrItem.h : interface of the CServerSrvrItem class
//

#if
!defined(AFX_SRVRITEM_H__EA3CE62D_A95C_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_SRVRITEM_H__EA3CE62D_A95C_11D1_887F_D42B07C10710__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CServerSrvrItem : public COleServerItem
{
    DECLARE_DYNAMIC(CServerSrvrItem)

// Constructors
public:
    CServerSrvrItem(CServerDoc* pContainerDoc);

// Attributes
    CServerDoc* GetDocument() const
    { return (CServerDoc*)COleServerItem::GetDocument(); }

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CServerSrvrItem)
    public:
    virtual BOOL OnDraw(CDC* pDC, CSize& rSize);
    virtual BOOL OnGetExtent(DVASPECT dwDrawAspect, CSize& rSize);
    //}}AFX_VIRTUAL

// Implementation
public:
    ~CServerSrvrItem();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

protected:
    virtual void Serialize(CArchive& ar); // overridden for document i/o
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif //
!defined(AFX_SRVRITEM_H__EA3CE62D_A95C_11D1_887F_D42B07C10710__INCLUDED_)


// SrvrItem.cpp : implementation of the CServerSrvrItem class
//

#include “stdafx.h”
#include “Server.h”

#include “ServerDoc.h”
#include “SrvrItem.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CServerSrvrItem implementation

IMPLEMENT_DYNAMIC(CServerSrvrItem, COleServerItem)

CServerSrvrItem::CServerSrvrItem(CServerDoc* pContainerDoc)
    : COleServerItem(pContainerDoc, TRUE)
{
    // TODO: add one-time construction code here
    // (eg, adding additional clipboard formats to the item's data source)
}
CServerSrvrItem::~CServerSrvrItem()
{
    // TODO: add cleanup code here
}

void CServerSrvrItem::Serialize(CArchive& ar)
{
    // CServerSrvrItem::Serialize will be called by the framework if
    // the item is copied to the clipboard. This can happen automatically
    // through the OLE callback OnGetClipboardData. A good default for
    // the embedded item is simply to delegate to the document's Serialize
    // function. If you support links, then you will want to serialize
    // just a portion of the document.

    if (!IsLinkedItem())
    {
    CServerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    pDoc->Serialize(ar);
    }
}

BOOL CServerSrvrItem::OnGetExtent(DVASPECT dwDrawAspect, CSize& rSize)
{
    // Most applications, like this one, only handle drawing the content
    // aspect of the item. If you wish to support other aspects, such
    // as DVASPECT_THUMBNAIL (by overriding OnDrawEx), then this
    // implementation of OnGetExtent should be modified to handle the
    // additional aspect(s).

    if (dwDrawAspect != DVASPECT_CONTENT)
    return COleServerItem::OnGetExtent(dwDrawAspect, rSize);

    // CServerSrvrItem::OnGetExtent is called to get the extent in
    // HIMETRIC units of the entire item. The default implementation
    // here simply returns a hard-coded number of units.

    CServerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    // TODO: replace this arbitrary size

    CClientDC nullDC(NULL);
    nullDC.SetMapMode(MM_TEXT);

    rSize = nullDC.GetTextExtent(pDoc->text, pDoc->text.GetLength());

    nullDC.DPtoHIMETRIC(&rSize);

    //rSize = CSize(3000, 3000); // 3000 x 3000 HIMETRIC units

    return TRUE;
}

BOOL CServerSrvrItem::OnDraw(CDC* pDC, CSize& rSize)
{
    // Remove this if you use rSize
    UNREFERENCED_PARAMETER(rSize);

    CServerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    // TODO: set mapping mode and extent
    // (The extent is usually the same as the size returned from OnGetExtent)
    //pDC->SetMapMode(MM_ANISOTROPIC);
    //pDC->SetWindowOrg(0,0);
    //pDC->SetWindowExt(3000, 3000);

    pDC->TextOut(0, 0, pDoc->text);
    // TODO: add drawing code here. Optionally, fill in the HIMETRIC extent.
    // All drawing takes place in the metafile device context (pDC).

    return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CServerSrvrItem diagnostics

#ifdef _DEBUG
void CServerSrvrItem::AssertValid() const
{
    COleServerItem::AssertValid();
}

void CServerSrvrItem::Dump(CDumpContext& dc) const
{
    COleServerItem::Dump(dc);
}
#endif


/////////////////////////////////////////////////////////////////////////////


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.