Смекни!
smekni.com

Протоколы и стандарты объектно-ориентированного программирования (стр. 6 из 6)

}

}

LPSTR TOleDocWindow::GetNextObjectName()

{

static char buffer[ MAXPATH ];

wsprintf( buffer, "object #%03d", nNextObjectNum++ );

return buffer;

}

void TOleDocWindow::Paint( HDC hdc, PAINTSTRUCT _FAR &)

{

LPOLEOBJECT lpObjectToDraw = NULL;

if ( bObjectLoaded )

lpObjectToDraw = lpObject;

else if ( lpUndoObject )

lpObjectToDraw = lpUndoObject;

if ( lpObjectToDraw ) {

RECT rect;

GetClientRect( HWindow, &rect );

// Замечание по OleDraw:

// OleDraw должен возвращать OLE_ERROR_OBJECT, если

// object был нарисован неверно.

ret = OleDraw( lpObjectToDraw , hdc, &rect ,

NULL, 0 );

wait( ret, lpObjectToDraw );

}

}

TOleDocWindow::TOleDocWindow( PTWindowsObject parent,

LPSTR title )

: TWindow( parent, title )

{

ret = OLE_OK;

lhClientDoc = 0;

bObjectLoaded = FALSE;

bUndoObjectLoaded = FALSE;

bUndoObjectLoaded = FALSE;

pOwlClient = NULL;

lpObject = NULL;

lpUndoObject = NULL;

strcpy( lpszDocName , "noname.ole" );

*lpszLastObjectName = 0;

*lpszObjectName = 0;

bDefDocName = TRUE;

}

void TOleDocWindow::SetupWindow() {

TWindow::SetupWindow();

RegisterClientDoc();

pOwlClient = new TOwlClient( this );

}

void TOleDocWindow::RegisterClientDoc() {

ret = OleRegisterClientDoc(

GetApplication()->Name,

lpszDocName,

0,

&lhClientDoc );

check( ret );

}

void TOleDocWindow::ShutDownWindow()

{

CloseCurrentOle();

if ( pOwlClient ) delete pOwlClient;

TWindow::ShutDownWindow();

}

void TOleDocWindow::RegFileName( LPSTR FileName )

{

lstrcpy( lpszDocName , FileName );

ret = OleRegisterClientDoc( GetApplication()->Name,

lpszDocName ,

0,

&lhClientDoc );

check ( ret );

}

void TOleDocWindow::CMActivate( RTMessage )

{

BackupObject();

RECT rect;

GetClientRect( HWindow, &rect );

ret = OleActivate( lpObject , OLEVERB_PRIMARY, TRUE,

TRUE ,

HWindow , &rect );

wait ( ret, lpObject );

PostMessage( HWindow , WM_U_REDRAW, 0, 0L );

}

void TOleDocWindow::WMInitMenu( RTMessage msg )

{

HMENU hMenu = (HMENU)msg.WParam;

WORD wEnableUndo;

if ( (lpObject != lpUndoObject) &&

( lpUndoObject != NULL ))

wEnableUndo = MF_ENABLED;

else wEnableUndo = MF_GRAYED;

EnableMenuItem( hMenu, CM_UNDO ,

wEnableUndo );

EnableMenuItem( hMenu, CM_COPY ,

( bObjectLoaded ? MF_ENABLED : MF_GRAYED ));

EnableMenuItem( hMenu, CM_CUT ,

( bObjectLoaded ? MF_ENABLED : MF_GRAYED ));

ret = OleQueryCreateFromClip( "StdFileEditing",

olerender_draw, 0 );

EnableMenuItem( hMenu, CM_PASTE ,

(( ret == OLE_OK ) ? MF_ENABLED : MF_GRAYED ));

EnableMenuItem( hMenu, CM_ACTIVATE ,

( bObjectLoaded ? MF_ENABLED : MF_GRAYED ));

EnableMenuItem( hMenu, CM_CLEAR ,

( bObjectLoaded ? MF_ENABLED : MF_GRAYED ));

DrawMenuBar( HWindow );

}

LPSTR TOleDocWindow::GetClassName() { return "OLEDOCWINDOW"; }

void TOleDocWindow::GetWindowClass(WNDCLASS _FAR &wc )

{

TWindow::GetWindowClass( wc );

wc.lpszMenuName = "MENU_DOCWINDOW";

}

void TOleDocWindow::CMClear( RTMessage )

{

CloseCurrentOle();

}

void TOleDocWindow::CloseCurrentOle()

{

// окончательное сохранение

if ( lpObject ) {

ret = OleDelete( lpObject );

wait( ret , lpObject );

}

if ( lpUndoObject ) {

ret = OleDelete( lpUndoObject );

wait( ret , lpObject );

}

lpObject = lpUndoObject = NULL;

bObjectLoaded = FALSE;

InvalidateRect( HWindow , NULL, TRUE );

UpdateWindow( HWindow );

}

void TOleApp::InitMainWindow()

{

MainWindow = new TOleDocWindow(NULL, "OWL OLE Application" );

}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmd, int nCmdShow)

{

TOleApp OleApp ("OleApp", hInstance, hPrevInstance,

lpCmd, nCmdShow);

OleApp.Run();

return (OleApp.Status);

}