/*---------------------------------------------------------------------------- | | vecovrly.c | ---------------------------------------------------------------------------*/ #if !defined( STRICT ) #define STRICT #endif /* STRICT */ #include #include #include "..\..\..\include\l_bitmap.h" #include "resource.h" /* Module local section */ typedef struct tagMYWINDOWDATA { pBITMAPHANDLE pBitmap; /* The bitmap handle */ pVECTORHANDLE pVector; /* The vector handle */ L_INT cxClient; /* Width of client area */ L_INT cyClient; /* Height of client area */ L_INT cyHeight; /* Total height of image */ L_INT cxWidth; /* Total width of image */ L_INT nHScrollPos; /* x scroll position */ L_INT nVScrollPos; /* y scroll position */ L_INT nHScrollMax; /* Maximum x scroll position */ L_INT nVScrollMax; /* Maximum y scroll postion */ L_INT nHScrollStep; /* x scroll step value */ L_INT nVScrollStep; /* y scroll step value */ L_INT nScalar; /* Zoom value */ RECT rcView; /* Current view rectangle */ L_BOOL fInScroll; /* Scrolling is in action */ L_BOOL fSizeInUse; /* We are processing a WM_SIZE message */ L_BOOL fViewRaster; /* View raster image */ L_BOOL fViewVector; /* View vector image */ } MYWINDOWDATA, *LPMYWINDOWDATA; #define SETMYWINDOWDATA(hWnd, p) \ SetWindowLong((hWnd), GWL_USERDATA, (L_INT32)(p)) #define GETMYWINDOWDATA(hWnd) \ (LPMYWINDOWDATA)GetWindowLong((hWnd), GWL_USERDATA) #define ENABLEMENUITEM(hMenu, uId, b) \ EnableMenuItem((hMenu), (uId), MF_BYCOMMAND | ((b)) ? MF_ENABLED : MF_GRAYED) #define CHECKMENUITEM(hMenu, uId, b) \ CheckMenuItem((hMenu), (uId), MF_BYCOMMAND | ((b)) ? MF_CHECKED : MF_UNCHECKED) /* Maximum number of steps between scroll extremes */ #define SCROLL_RATIO (10) static const L_TCHAR g_cszTitle[] = TEXT("LEAD Vector Overlay Demo"); static L_CHAR szOpenFileFilter[] = { "All Files (*.*)\0" "*.*\0" "AutoCAD (*.dxf;*.dwg;*.dwf)\0" "*.dxf;*.dwg;*.dwf\0" "Computer Graphics Metafile (*.cgm)\0" "*.cgm\0" "HP-GL/HP-GL2 (*.plt)\0" "*.plt\0" "LEAD Vector (*.vec)\0" "*.vec\0" "Mac PICT (*.pct)\0" "*.pct\0" "Micrografx DRW (*.drw)\0" "*.drw\0" "MicroStation DGN (*.dgn)\0" "*.dgn\0" "Windows Metafile (*.wmf;*.emf)\0" "*.wmf;*.emf\0" }; static LRESULT WINAPI Main_WndProc( HWND, L_UINT, WPARAM, LPARAM ); static L_BOOL Main_OnCreate( HWND, LPCREATESTRUCT ); static L_BOOL Main_OnEraseBkgnd( HWND, HDC ); static L_VOID Main_OnSize( HWND, L_UINT, L_INT, L_INT ); static L_VOID Main_OnPaint( HWND ); static L_VOID Main_OnPaletteChanged( HWND, HWND ); static L_INT Main_OnQueryNewPalette( HWND ); static L_VOID Main_OnHScroll( HWND, HWND, L_UINT, L_INT ); static L_VOID Main_OnVScroll( HWND, HWND, L_UINT, L_INT ); static L_VOID Main_OnKeyDown( HWND, L_UINT, L_BOOL, L_INT, L_UINT ); static L_VOID Main_OnCommand( HWND, L_INT, HWND, L_UINT ); static L_VOID Main_OnInitMenuPopup( HWND, HMENU, L_UINT, L_BOOL ); static L_VOID Main_OnDestroy( HWND ); static L_INT InitMyWindowData( HWND ); static L_VOID FreeMyWindowData( HWND ); static L_VOID Main_OnFileOpenRaster( HWND ); static L_VOID Main_OnFileOpenVector( HWND ); static L_VOID Main_OnFileExit( HWND ); static L_VOID Main_OnViewRaster( HWND ); static L_VOID Main_OnViewVector( HWND ); static L_VOID Main_OnZoomIn( HWND, L_INT ); static L_VOID Main_OnZoomOut( HWND, L_INT ); static L_VOID Main_OnZoomNormal( HWND ); static L_VOID Main_OnHelpAbout( HWND ); static L_VOID ShowLeadError( HWND, L_INT ); static L_INT ScaleDimension( L_INT, L_INT ); static L_VOID ScaleImage( HWND, L_INT ); static L_VOID Hourglass( L_BOOL ); static L_INT WINAPI About_DlgProc( HWND, L_UINT, WPARAM, LPARAM ); /*---------------------------------------------------------------------------- | | WinMain | | Program entry point. | ---------------------------------------------------------------------------*/ L_INT WINAPI WinMain ( HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR pCmdLine, L_INT nCmdShow ) { static const L_TCHAR cszClassName[] = TEXT("LEAD_Overly_Demo"); WNDCLASSEX WndClass; HWND hWnd; L_BOOL fWindowCreated; L_INT nRet; MSG Msg; UNREFERENCED_PARAMETER( hInstPrev ); UNREFERENCED_PARAMETER( pCmdLine ); UNLOCKSUPPORT(); fWindowCreated = FALSE; nRet = 1; WndClass.cbSize = sizeof( WNDCLASSEX ); WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = Main_WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInst; WndClass.hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDI_MAIN ) ); WndClass.hCursor = LoadCursor( NULL, IDC_ARROW ); WndClass.hbrBackground = (HBRUSH) ( COLOR_WINDOW + 1 ); WndClass.lpszMenuName = MAKEINTRESOURCE( IDM_MAIN ); WndClass.lpszClassName = cszClassName; WndClass.hIconSm = NULL; if( RegisterClassEx( &WndClass ) != 0 ) { hWnd = CreateWindowEx( 0L, cszClassName, g_cszTitle, WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, &fWindowCreated ); if( hWnd != NULL ) { ShowWindow( hWnd, nCmdShow ); UpdateWindow( hWnd ); while( GetMessage( &Msg, NULL, 0, 0 ) != 0 ) { TranslateMessage( &Msg ); DispatchMessage( &Msg ); } nRet = Msg.wParam; } UnregisterClass( cszClassName, hInst ); } if( !fWindowCreated ) MessageBox( NULL, TEXT("Could not create main window!"), g_cszTitle, MB_ICONSTOP ); return nRet; } /*---------------------------------------------------------------------------- | | Main_WndProc | | Main window procedure. | ---------------------------------------------------------------------------*/ static LRESULT WINAPI Main_WndProc ( HWND hWnd, L_UINT uMessage, WPARAM wParam, LPARAM lParam ) { switch( uMessage ) { HANDLE_MSG( hWnd, WM_CREATE, Main_OnCreate ); HANDLE_MSG( hWnd, WM_ERASEBKGND, Main_OnEraseBkgnd ); HANDLE_MSG( hWnd, WM_SIZE, Main_OnSize ); HANDLE_MSG( hWnd, WM_PAINT, Main_OnPaint ); HANDLE_MSG( hWnd, WM_PALETTECHANGED, Main_OnPaletteChanged ); HANDLE_MSG( hWnd, WM_QUERYNEWPALETTE, Main_OnQueryNewPalette ); HANDLE_MSG( hWnd, WM_HSCROLL, Main_OnHScroll ); HANDLE_MSG( hWnd, WM_VSCROLL, Main_OnVScroll ); HANDLE_MSG( hWnd, WM_KEYDOWN, Main_OnKeyDown ); HANDLE_MSG( hWnd, WM_COMMAND, Main_OnCommand ); HANDLE_MSG( hWnd, WM_INITMENUPOPUP, Main_OnInitMenuPopup ); HANDLE_MSG( hWnd, WM_DESTROY, Main_OnDestroy ); default: return DefWindowProc( hWnd, uMessage, wParam, lParam ); } } /*---------------------------------------------------------------------------- | | Main_OnCreate | | WM_CREATE handler. | ---------------------------------------------------------------------------*/ static L_BOOL Main_OnCreate ( HWND hWnd, LPCREATESTRUCT pCreateStruct ) { L_INT nRet; * (L_BOOL*) pCreateStruct->lpCreateParams = TRUE; nRet = InitMyWindowData( hWnd ); if( nRet != SUCCESS ) ShowLeadError( hWnd, nRet ); nRet = L_DlgInit( 0 ); if( nRet != SUCCESS ) ShowLeadError( hWnd, nRet ); return ( nRet == SUCCESS ) ? TRUE : FALSE; } /*---------------------------------------------------------------------------- | | Main_OnEraseBkgnd | | WM_ERASEBKGND handler. | ---------------------------------------------------------------------------*/ static L_BOOL Main_OnEraseBkgnd ( HWND hWnd, HDC hDC ) { LPMYWINDOWDATA pWindowData; RECT rcClient; HRGN hRgn1, hRgn2, hRgn3; pWindowData = GETMYWINDOWDATA( hWnd ); /* Dont erase background while we are scrolling to minimize flickering */ if( !pWindowData->fInScroll ) { GetClientRect(hWnd, &rcClient); hRgn1 = CreateRectRgn(rcClient.left, rcClient.top, rcClient.right, rcClient.bottom); hRgn2 = CreateRectRgn(rcClient.left, rcClient.top, rcClient.right, rcClient.bottom); hRgn3 = CreateRectRgn(pWindowData->rcView.left, pWindowData->rcView.top, pWindowData->rcView.right, pWindowData->rcView.bottom); CombineRgn(hRgn1, hRgn2, hRgn3, RGN_XOR); SelectClipRgn(hDC, hRgn1); FillRect(hDC, &rcClient, GetSysColorBrush(COLOR_WINDOW)); SelectClipRgn(hDC, NULL); DeleteObject(hRgn1); DeleteObject(hRgn2); DeleteObject(hRgn3); } return TRUE; } /*---------------------------------------------------------------------------- | | Main_OnSize | | WM_SIZE handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnSize ( HWND hWnd, L_UINT uState, L_INT nCx, L_INT nCy ) { LPMYWINDOWDATA pWindowData; L_INT nWidth, nHeight, nVScroll, nHScroll; DWORD dwStyle; pWindowData = GETMYWINDOWDATA( hWnd ); if( pWindowData->pBitmap != NULL ) { if( !pWindowData->fSizeInUse ) { pWindowData->fSizeInUse = TRUE; if( uState != SIZEICONIC ) /* If window is normal */ { /* Scale the Bitmap Width to new Width */ nWidth = ScaleDimension( BITMAPWIDTH( pWindowData->pBitmap ), pWindowData->nScalar ); /* Scale the Bitmap Height to new Height */ nHeight = ScaleDimension( BITMAPHEIGHT( pWindowData->pBitmap ), pWindowData->nScalar ); /* Set client dimensions to current width and height */ pWindowData->cxClient = nCx; pWindowData->cyClient = nCy; dwStyle = GetWindowLong( hWnd, GWL_STYLE ); /* Get Vertical scroll arrow width */ nVScroll = GetSystemMetrics( SM_CXVSCROLL ); /* Get Horizontal scroll arrow width */ nHScroll = GetSystemMetrics( SM_CXHSCROLL ); /* If Vertical scroll */ if( WS_VSCROLL & dwStyle ) pWindowData->cxClient += nVScroll; /* If Horizontal scroll */ if( WS_HSCROLL & dwStyle ) pWindowData->cyClient += nHScroll; /* If client width < current width */ if( pWindowData->cxClient < nWidth ) { pWindowData->cyClient -= nHScroll; if( pWindowData->cyClient < nHeight ) pWindowData->cxClient -= nVScroll; } /* If client height < current height */ else if( pWindowData->cyClient < nHeight ) { pWindowData->cxClient -= nVScroll; if( pWindowData->cxClient < nWidth ) pWindowData->cyClient -= nHScroll; } /* Set Horizontal scroll step = 1/10 of client width, if > 1 */ pWindowData->nHScrollStep = max( 1, pWindowData->cxClient / SCROLL_RATIO ); /* Set Vertical scroll step = 1/10 of client height, if > 1 */ pWindowData->nVScrollStep = max( 1, pWindowData->cyClient / SCROLL_RATIO ); /* Set vetical scroll range */ pWindowData->nVScrollMax = max( 0, ( nHeight - pWindowData->cyClient ) ); /* Set vertical scroll position */ pWindowData->nVScrollPos = max( 0, min( pWindowData->nVScrollPos, pWindowData->nVScrollMax ) ); /* Set horizontal scroll range */ pWindowData->nHScrollMax = max( 0, ( nWidth - pWindowData->cxClient ) ); /* Set horizontal scroll position */ pWindowData->nHScrollPos = max( 0, min( pWindowData->nHScrollPos, pWindowData->nHScrollMax ) ); /* Set rcView to new width and new height */ SetRect( &pWindowData->rcView, 0, 0, nWidth, nHeight ); /* Set offset by current scroll position */ OffsetRect( &pWindowData->rcView, -pWindowData->nHScrollPos, -pWindowData->nVScrollPos ); /* Set horizontal scroll range = nHScrollMax */ SetScrollRange( hWnd, SB_HORZ, 0, pWindowData->nHScrollMax, FALSE ); /* Set horintal scroll position */ SetScrollPos( hWnd, SB_HORZ, pWindowData->nHScrollPos, TRUE ); /* Set vertical scroll range = nVScrollMax */ SetScrollRange( hWnd, SB_VERT, 0, pWindowData->nVScrollMax, FALSE ); /* Set vertical scroll position */ SetScrollPos( hWnd, SB_VERT, pWindowData->nVScrollPos, TRUE ); } else /* If window is iconized */ { /* Retain the x-client and y-client area */ pWindowData->cxClient = nCx; pWindowData->cyClient = nCy; /* set client area */ SetRect( &pWindowData->rcView, 0, 0, pWindowData->cxClient, pWindowData->cyClient ); } pWindowData->fSizeInUse = FALSE; } } else { SetScrollRange( hWnd, SB_VERT, 0, 0, FALSE ); SetScrollRange( hWnd, SB_HORZ, 0, 0, FALSE ); } } /*---------------------------------------------------------------------------- | | Main_OnPaint | | WM_PAINT handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnPaint ( HWND hWnd ) { PAINTSTRUCT PaintStruct; HDC hDC; LPMYWINDOWDATA pWindowData; HPALETTE hRasterOldPalette; HPALETTE hRasterPalette; HPALETTE hVectorPalette; HPALETTE hVectorOldPalette; HPEN hOldPen; HBRUSH hOldBrush; L_BOOL bPaletteUsed; hDC = BeginPaint( hWnd, &PaintStruct ); pWindowData = GETMYWINDOWDATA( hWnd ); hRasterOldPalette = hRasterPalette = NULL; hVectorOldPalette = hVectorPalette = NULL; bPaletteUsed = FALSE; if( pWindowData->pBitmap != NULL ) { hRasterPalette = L_CreatePaintPalette( hDC, pWindowData->pBitmap ); if( hRasterPalette != NULL ) { bPaletteUsed = TRUE; hRasterOldPalette = SelectPalette( hDC, hRasterPalette, FALSE ); RealizePalette( hDC ); } } if( pWindowData->pVector != NULL && !bPaletteUsed ) { hVectorPalette = L_VecGetPalette( pWindowData->pVector ); if( hVectorPalette != NULL ) { hVectorOldPalette = SelectPalette( hDC, hVectorPalette, TRUE ); RealizePalette( hDC ); } } hOldBrush = SelectBrush( hDC, GetStockBrush( WHITE_BRUSH ) ); hOldPen = SelectPen( hDC, GetStockPen( WHITE_PEN ) ); Rectangle( hDC, pWindowData->rcView.left, pWindowData->rcView.top, pWindowData->rcView.right, pWindowData->rcView.bottom ); SelectBrush( hDC, hOldBrush ); SelectPen( hDC, hOldPen ); if( pWindowData->fViewRaster && pWindowData->pBitmap != NULL ) L_PaintDC( hDC, pWindowData->pBitmap, NULL, NULL, &pWindowData->rcView, &PaintStruct.rcPaint, SRCCOPY ); if( pWindowData->fViewVector && pWindowData->pVector != NULL ) L_VecPaintDC( hDC, pWindowData->pVector, pWindowData->cxWidth, pWindowData->cyHeight, NULL, NULL, &pWindowData->rcView, &PaintStruct.rcPaint, !pWindowData->fViewRaster ); if( hRasterPalette != NULL ) { SelectPalette( hDC, hRasterOldPalette, FALSE ); DeleteObject( hRasterPalette ); } if( hVectorPalette != NULL ) SelectPalette(hDC, hVectorOldPalette, FALSE ); EndPaint( hWnd, &PaintStruct ); } /*---------------------------------------------------------------------------- | | Main_OnPaletteChanged | | WM_PALETTECHANGED handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnPaletteChanged ( HWND hWnd, HWND hWndPaletteChanged ) { LPMYWINDOWDATA pWindowData; HDC hDC; HPALETTE hRasterOldPalette; HPALETTE hRasterPalette; HPALETTE hVectorPalette; HPALETTE hVectorOldPalette; L_BOOL bPaletteUsed; hRasterOldPalette = hRasterPalette = NULL; hVectorOldPalette = hVectorPalette = NULL; bPaletteUsed = FALSE; if( hWnd != hWndPaletteChanged ) { pWindowData = GETMYWINDOWDATA( hWnd ); hDC = GetDC( hWnd ); if( pWindowData->pBitmap != NULL ) { hRasterPalette = L_CreatePaintPalette( hDC, pWindowData->pBitmap ); if( hRasterPalette != NULL ) { bPaletteUsed = TRUE; hRasterOldPalette = SelectPalette( hDC, hRasterPalette, FALSE ); } } if( pWindowData->pVector != NULL && !bPaletteUsed ) { hVectorPalette = L_VecGetPalette( pWindowData->pVector ); if( hVectorPalette != NULL ) { bPaletteUsed = TRUE; hVectorOldPalette = SelectPalette( hDC, hVectorPalette, TRUE ); } } if( bPaletteUsed ) { RealizePalette( hDC ); InvalidateRect( hWnd, 0, FALSE ); } if( hRasterPalette != NULL ) { SelectPalette( hDC, hRasterOldPalette, FALSE ); DeleteObject( hRasterPalette ); } if( hVectorPalette != NULL ) SelectPalette(hDC, hVectorOldPalette, FALSE ); ReleaseDC( hWnd, hDC ); } } /*---------------------------------------------------------------------------- | | Main_OnQueryNewPalette | | WM_QUERYNEWPALETTE handler. | ---------------------------------------------------------------------------*/ static L_INT Main_OnQueryNewPalette ( HWND hWnd ) { LPMYWINDOWDATA pWindowData; HDC hDC; L_INT nNoColors = 0; HPALETTE hRasterOldPalette; HPALETTE hRasterPalette; HPALETTE hVectorPalette; HPALETTE hVectorOldPalette; L_BOOL bPaletteUsed; hRasterOldPalette = hRasterPalette = NULL; hVectorOldPalette = hVectorPalette = NULL; pWindowData = GETMYWINDOWDATA( hWnd ); hDC = GetDC( hWnd ); if( pWindowData->pBitmap != NULL ) { hRasterPalette = L_CreatePaintPalette( hDC, pWindowData->pBitmap ); if( hRasterPalette != NULL ) { bPaletteUsed = TRUE; hRasterOldPalette = SelectPalette( hDC, hRasterPalette, FALSE ); } } if( pWindowData->pVector != NULL && !bPaletteUsed ) { hVectorPalette = L_VecGetPalette( pWindowData->pVector ); if( hVectorPalette != NULL ) { bPaletteUsed = TRUE; hVectorOldPalette = SelectPalette( hDC, hVectorPalette, TRUE ); } } if( bPaletteUsed ) nNoColors = RealizePalette( hDC ); if( nNoColors > 0 ) InvalidateRect( hWnd, NULL, FALSE ); if( hRasterPalette != NULL ) { SelectPalette( hDC, hRasterOldPalette, FALSE ); DeleteObject( hRasterPalette ); } if( hVectorPalette != NULL ) SelectPalette(hDC, hVectorOldPalette, FALSE ); ReleaseDC( hWnd, hDC ); return nNoColors; } /*---------------------------------------------------------------------------- | | Main_OnHScroll | | WM_HSCROLL handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnHScroll ( HWND hWnd, HWND hWndCtl, L_UINT uCode, L_INT nPos ) { LPMYWINDOWDATA pWindowData; L_INT nScrollInc; /* Horizontal scrolling increment */ UNREFERENCED_PARAMETER( hWndCtl ); pWindowData = GETMYWINDOWDATA( hWnd ); pWindowData->fInScroll = TRUE; /* To minimize flickering */ switch( uCode ) { /* Decrement by current scroll position */ case SB_LEFT: nScrollInc = -pWindowData->nHScrollPos; break; /* Increment by the difference between Max scroll and current scroll position */ case SB_RIGHT: nScrollInc = pWindowData->nHScrollMax - pWindowData->nHScrollPos; break; /* Decrement by one step */ case SB_LINELEFT: nScrollInc = -pWindowData->nHScrollStep; break; /* Increment by one step */ case SB_LINERIGHT: nScrollInc = pWindowData->nHScrollStep; break; /* Decrement by Maximum */ case SB_PAGELEFT: nScrollInc = -max( pWindowData->nHScrollStep, ( pWindowData->cxClient - pWindowData->nHScrollStep ) ); break; /* Increment by Maximum */ case SB_PAGERIGHT: nScrollInc = max( pWindowData->nHScrollStep, ( pWindowData->cxClient - pWindowData->nHScrollStep ) ); break; /* Increment by difference between current scroll position and THUMB position */ case SB_THUMBPOSITION: case SB_THUMBTRACK: nScrollInc = nPos - pWindowData->nHScrollPos; break; /* No Increment */ default: nScrollInc = 0; break; } nScrollInc = max( -pWindowData->nHScrollPos, min( nScrollInc, ( pWindowData->nHScrollMax - pWindowData->nHScrollPos ) ) ); if( nScrollInc != 0 ) { /* if Scroll increment is greater than nScrollInc, Increment scroll position by nScrollInc */ pWindowData->nHScrollPos += nScrollInc; /* Reposition the picture at (0, -nScrollInc) */ OffsetRect( &pWindowData->rcView, -nScrollInc, 0 ); /* Scroll left the window by nSrollInc */ ScrollWindow( hWnd, -nScrollInc, 0, NULL, NULL ); /* Set new scroll position at current scroll position */ SetScrollPos( hWnd, SB_HORZ, pWindowData->nHScrollPos, TRUE ); /* Update window */ UpdateWindow( hWnd ); } pWindowData->fInScroll = FALSE; } /*---------------------------------------------------------------------------- | | Main_OnVScroll | | WM_VSCROLL handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnVScroll ( HWND hWnd, HWND hWndCtl, L_UINT uCode, L_INT nPos ) { LPMYWINDOWDATA pWindowData; L_INT nScrollInc; /* Horizontal scrolling increment */ UNREFERENCED_PARAMETER( hWndCtl ); pWindowData = GETMYWINDOWDATA( hWnd ); pWindowData->fInScroll = TRUE; /* To minimize flickering */ switch( uCode ) { case SB_BOTTOM: /* Increment by Difference between Max scroll and current scroll position */ nScrollInc = pWindowData->nVScrollMax - pWindowData->nVScrollPos; break; case SB_TOP: /* Decrement by current scroll position */ nScrollInc = -pWindowData->nVScrollPos; break; case SB_LINEDOWN: /* Increment by one step */ nScrollInc = pWindowData->nVScrollStep; break; case SB_LINEUP: /* Decrement by one step */ nScrollInc = -pWindowData->nVScrollStep; break; case SB_PAGEDOWN: /* Increment by Maximum */ nScrollInc = max( pWindowData->nVScrollStep, ( pWindowData->cyClient - pWindowData->nVScrollStep ) ); break; case SB_PAGEUP: /* Decrement by Maximum */ nScrollInc = -max( pWindowData->nVScrollStep, ( pWindowData->cyClient - pWindowData->nVScrollStep ) ); break; case SB_THUMBPOSITION: case SB_THUMBTRACK: /* Increment by Difference between Current scroll position and THUMB position */ nScrollInc = nPos - pWindowData->nVScrollPos; break; default: /* No increment */ nScrollInc = 0; break; } nScrollInc = max( -pWindowData->nVScrollPos, min( nScrollInc, ( pWindowData->nVScrollMax - pWindowData->nVScrollPos ) ) ); if( nScrollInc != 0 ) { /* if Scroll increment is greater than nScrollInc, Increment scroll position by nScrollInc */ pWindowData->nVScrollPos += nScrollInc; /* Reposition the picture at (0, -nScrollInc) */ OffsetRect( &pWindowData->rcView, 0, -nScrollInc ); /* Scroll up the window by nSrollInc */ ScrollWindow( hWnd, 0, -nScrollInc, NULL, NULL ); /* Set new scroll position at current scroll position */ SetScrollPos( hWnd, SB_VERT, pWindowData->nVScrollPos, TRUE ); /* Update window */ UpdateWindow( hWnd ); } pWindowData->fInScroll = FALSE; } /*---------------------------------------------------------------------------- | | Main_OnKeyDown | | WM_KEYDOWN handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnKeyDown ( HWND hWnd, L_UINT uVK, L_BOOL fDown, L_INT nRepeat, L_UINT uFlags ) { LPMYWINDOWDATA pWindowData; L_INT nNewScalar; UNREFERENCED_PARAMETER( fDown ); UNREFERENCED_PARAMETER( uFlags ); if( nRepeat == 1 ) { switch( uVK ) { case VK_ADD: case VK_SUBTRACT: pWindowData = GETMYWINDOWDATA( hWnd ); if( uVK == VK_ADD ) nNewScalar = pWindowData->nScalar + 1; else nNewScalar = pWindowData->nScalar - 1; if( nNewScalar >= 0 && nNewScalar <= 8 ) ScaleImage( hWnd, nNewScalar ); break; default: break; } } } /*---------------------------------------------------------------------------- | | Main_OnCommand | | WM_COMMAND handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnCommand ( HWND hWnd, L_INT nCmd, HWND hWndCtl, L_UINT uCodeNotify ) { switch( nCmd ) { case IDM_FILE_OPENRASTER: Main_OnFileOpenRaster( hWnd ); break; case IDM_FILE_OPENVECTOR: Main_OnFileOpenVector( hWnd ); break; case IDM_FILE_EXIT: Main_OnFileExit( hWnd ); break; case IDM_VIEW_RASTER: Main_OnViewRaster( hWnd ); break; case IDM_VIEW_VECTOR: Main_OnViewVector( hWnd ); break; case IDM_ZOOM_IN_2X: case IDM_ZOOM_IN_3X: case IDM_ZOOM_IN_4X: case IDM_ZOOM_IN_5X: case IDM_ZOOM_IN_6X: case IDM_ZOOM_IN_7X: case IDM_ZOOM_IN_8X: Main_OnZoomIn( hWnd, nCmd - IDM_ZOOM_IN_2X + 1 ); break; case IDM_ZOOM_OUT_2X: case IDM_ZOOM_OUT_3X: case IDM_ZOOM_OUT_4X: case IDM_ZOOM_OUT_5X: case IDM_ZOOM_OUT_6X: case IDM_ZOOM_OUT_7X: case IDM_ZOOM_OUT_8X: Main_OnZoomOut( hWnd, IDM_ZOOM_OUT_2X - nCmd - 1 ); break; case IDM_ZOOM_NORMAL: Main_OnZoomNormal( hWnd ); break; case IDM_HELP_ABOUT: Main_OnHelpAbout( hWnd ); break; default: DefWindowProc( hWnd, WM_COMMAND, MAKEWPARAM( nCmd, uCodeNotify ), (LPARAM) hWndCtl ); } } /*---------------------------------------------------------------------------- | | Main_OnInitMenuPopup | | WM_INITMENUPOPUP handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnInitMenuPopup ( HWND hWnd, HMENU hMenu, L_UINT uPos, L_BOOL fSystemMenu ) { LPMYWINDOWDATA pWindowData; L_INT i; pWindowData = GETMYWINDOWDATA( hWnd ); ENABLEMENUITEM( hMenu, IDM_FILE_OPENVECTOR, pWindowData->pBitmap != NULL ); CHECKMENUITEM( hMenu, IDM_VIEW_RASTER, pWindowData->fViewRaster ); CHECKMENUITEM( hMenu, IDM_VIEW_VECTOR, pWindowData->fViewVector ); ENABLEMENUITEM( hMenu, IDM_VIEW_RASTER, ( pWindowData->pBitmap != NULL ) ); ENABLEMENUITEM( hMenu, IDM_VIEW_VECTOR, ( pWindowData->pVector != NULL ) ); for( i = IDM_ZOOM_IN_2X; i <= IDM_ZOOM_IN_8X; i++ ) ENABLEMENUITEM( hMenu, i, ( pWindowData->pBitmap != NULL ) ); for( i = IDM_ZOOM_OUT_2X; i <= IDM_ZOOM_OUT_8X; i++ ) ENABLEMENUITEM( hMenu, i, ( pWindowData->pBitmap != NULL ) ); ENABLEMENUITEM( hMenu, IDM_ZOOM_NORMAL, ( pWindowData->pBitmap != NULL ) ); } /*---------------------------------------------------------------------------- | | Main_OnDestroy | | WM_DESTROY handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnDestroy ( HWND hWnd ) { FreeMyWindowData( hWnd ); L_DlgFree(); PostQuitMessage( 0 ); } /*---------------------------------------------------------------------------- | | InitMyWindowData | | Called when main window created to initialize our program variables. | ---------------------------------------------------------------------------*/ static L_INT InitMyWindowData ( HWND hWnd ) { L_INT nRet; LPMYWINDOWDATA pWindowData; pWindowData = GlobalAllocPtr( GHND, sizeof( MYWINDOWDATA ) ); if( pWindowData != NULL ) { ZeroMemory( pWindowData, sizeof( MYWINDOWDATA ) ); SETMYWINDOWDATA( hWnd, pWindowData ); pWindowData->pBitmap = NULL; pWindowData->pVector = NULL; pWindowData->fInScroll = FALSE; pWindowData->fSizeInUse = FALSE; pWindowData->fViewRaster = TRUE; pWindowData->fViewVector = TRUE; nRet= SUCCESS; } else nRet = ERROR_NO_MEMORY; return nRet; } /*---------------------------------------------------------------------------- | | FreeMyWindowData | | Called when main window is destroyed to free our program variables. | ---------------------------------------------------------------------------*/ static L_VOID FreeMyWindowData ( HWND hWnd ) { LPMYWINDOWDATA pWindowData; pWindowData = GETMYWINDOWDATA( hWnd ); if( pWindowData != NULL ) { /* Free bitmap image */ if( pWindowData->pBitmap != NULL ) { if( pWindowData->pBitmap->Flags.Allocated ) L_FreeBitmap( pWindowData->pBitmap ); GlobalFreePtr( pWindowData->pBitmap ); } /* Free the vector image */ if( pWindowData->pVector != NULL ) { L_VecFree( pWindowData->pVector ); GlobalFreePtr( pWindowData->pVector ); } GlobalFreePtr( pWindowData ); /* Just to make sure calling this function twice wont crash us */ pWindowData = NULL; SETMYWINDOWDATA( hWnd, pWindowData ); } } /*---------------------------------------------------------------------------- | | bitmapOpen | ---------------------------------------------------------------------------*/ L_INT bitmapOpen ( HWND hWnd, pBITMAPHANDLE pBitmap, L_UINT uStructSize ) { static OPENFILENAME OpenFileName; static L_TCHAR szFileName[ _MAX_PATH ] = TEXT(""); static L_TCHAR szFileTitle[ _MAX_PATH ] = TEXT(""); static L_BOOL bEnablePreview = TRUE; static L_BOOL fLoadRotated = FALSE; static L_BOOL fLoadCompressed = FALSE; static OPENDLGPARAMS ODLGParm; static FILEINFO foFileInfo; static L_INT nOpenIndex = 0; static L_BOOL fShowPreview = TRUE; L_INT nRet; _fmemset( &ODLGParm, 0, sizeof( OPENDLGPARAMS ) ); ODLGParm.uStructSize = sizeof(OPENDLGPARAMS); ODLGParm.bPreviewEnabled = bEnablePreview; ODLGParm.uDlgFlags = DLG_OPEN_LOADBITMAP | DLG_OPEN_SHOW_LOADOPTIONS | DLG_OPEN_SHOW_RASTEROPTIONS | DLG_OPEN_SHOW_FILEINFO | ( fShowPreview ? DLG_OPEN_SHOW_PREVIEW : 0 ); OpenFileName.lStructSize = sizeof( OPENFILENAME ); OpenFileName.hwndOwner = hWnd; OpenFileName.lpstrFilter = TEXT(""); OpenFileName.lpstrCustomFilter = NULL; OpenFileName.nMaxCustFilter = 0; OpenFileName.nFilterIndex = nOpenIndex; OpenFileName.lpstrFile = szFileName; OpenFileName.nMaxFile = sizeof( szFileName ) / sizeof( szFileName[ 0 ] ); OpenFileName.lpstrFileTitle = szFileTitle; OpenFileName.nMaxFileTitle = lstrlen( szFileTitle ); OpenFileName.lpstrInitialDir = NULL; OpenFileName.lpstrTitle = TEXT("Open a File"); OpenFileName.lpstrDefExt = NULL; OpenFileName.Flags = 0; nRet = L_DlgOpen( hWnd, &OpenFileName, &ODLGParm ); if( SUCCESS_DLG_OK == nRet ) { bEnablePreview = ODLGParm.bPreviewEnabled; nOpenIndex = OpenFileName.nFilterIndex; nRet = L_CopyBitmap( pBitmap, ODLGParm.pFileData[0].pBitmap, uStructSize ); if ( SUCCESS == nRet ) { int i = 0 ; // After using the return data you should free allocated data // Check for everything for ( i = 0 ; i < ODLGParm.nNumOfFiles ; i++ ) { if ( NULL != ODLGParm.pFileData [ i ].pBitmap ) { if ( ODLGParm.pFileData [ i ].pBitmap->Flags.Allocated ) { L_FreeBitmap ( ODLGParm.pFileData [ i ].pBitmap ) ; } GlobalFree ( ODLGParm.pFileData [ i ].pBitmap ) ; } if ( NULL != ODLGParm.pFileData [ i ].pThumbnail ) { if ( ODLGParm.pFileData [ i ].pThumbnail->Flags.Allocated ) { L_FreeBitmap ( ODLGParm.pFileData [ i ].pThumbnail ) ; } GlobalFree ( ODLGParm.pFileData [ i ].pThumbnail ) ; } if ( NULL != ODLGParm.pFileData [ i ].pFileInfo ) { GlobalFree ( ODLGParm.pFileData [ i ].pFileInfo ) ; } if ( NULL != ODLGParm.pFileData [ i ].FileOptions.pOptions ) { GlobalFree ( ODLGParm.pFileData [ i ].FileOptions.pOptions ) ; } } if ( NULL != ODLGParm.pFileData ) { GlobalFree ( ODLGParm.pFileData ) ; } } } return nRet; } /*---------------------------------------------------------------------------- | | Main_OnFileOpenRaster | | File/Open Raster menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnFileOpenRaster ( HWND hWnd ) { L_TCHAR szFileName[ _MAX_PATH ] = TEXT(""); L_TCHAR szFileTitle[ _MAX_PATH ] = TEXT(""); L_INT nRet; pBITMAPHANDLE pTempBitmap; LPMYWINDOWDATA pWindowData; pTempBitmap = GlobalAllocPtr( GHND, sizeof( BITMAPHANDLE ) ); if( NULL == pTempBitmap ) { nRet = ERROR_NO_MEMORY; ShowLeadError( hWnd, nRet ); return; } nRet = bitmapOpen( hWnd, pTempBitmap, sizeof( BITMAPHANDLE ) ); if( SUCCESS_DLG_CANCEL == nRet ) return; if( SUCCESS == nRet ) { pWindowData = GETMYWINDOWDATA( hWnd ); if( pWindowData->pBitmap != NULL ) { L_FreeBitmap( pWindowData->pBitmap ); GlobalFreePtr( pWindowData->pBitmap ); } pWindowData->pBitmap = pTempBitmap; pWindowData->fViewRaster = TRUE; pWindowData->cxWidth = pWindowData->pBitmap->Width; pWindowData->cyHeight = pWindowData->pBitmap->Height; ScaleImage( hWnd, 0 ); } else GlobalFreePtr( pTempBitmap ); if( nRet != SUCCESS ) ShowLeadError( hWnd, nRet ); } /*---------------------------------------------------------------------------- | | Main_OnFileOpenVector | | File/Open Vector menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnFileOpenVector ( HWND hWnd ) { static OPENFILENAME OpenFileName; static L_TCHAR szFileName[ _MAX_PATH ] = TEXT(""); static L_TCHAR szFileTitle[ _MAX_PATH ] = TEXT(""); static L_TCHAR szOpenFileFilter[] = { TEXT("All Files (*.*)\0") TEXT("*.*\0") TEXT("AutoCAD (*.dxf;*.dwg;*.dwf)\0") TEXT("*.dxf;*.dwg;*.dwf\0") TEXT("Computer Graphics Metafile (*.cgm)\0") TEXT("*.cgm\0") TEXT("Corel Presentation Exchange (CMX)\0") TEXT("*.cmx\0") TEXT("HP-GL/HP-GL2 (*.plt)\0") TEXT("*.plt\0") TEXT("LEAD Vector (*.vec)\0") TEXT("*.vec\0") TEXT("Mac PICT (*.pct)\0") TEXT("*.pct\0") TEXT("Micrografx DRW (*.drw)\0") TEXT("*.drw\0") TEXT("MicroStation DGN (*.dgn)\0") TEXT("*.dgn\0") TEXT("Windows Metafile (*.wmf;*.emf)\0") TEXT("*.wmf;*.emf\0") TEXT("Gerber File Format (*.gbr)\0") TEXT("*.gbr\0") TEXT("E S R I (SHP)\0") TEXT("*.shp\0") TEXT("Printer Command Language(*.pcl;*.prn;*.prt)\0") TEXT("*.pcl;*.prn;*.prt\0") TEXT("Scalable Vector Graphics (*.svg)\0") TEXT("*.svg\0") TEXT("Word Perfect Graphics (*.wpg)\0") TEXT("*.wpg\0") TEXT("Compressed Windows Metafile (WMZ)\0") TEXT("*.wmz\0") }; L_INT nRet; pVECTORHANDLE pTempVector; LPMYWINDOWDATA pWindowData; ZeroMemory( &OpenFileName, sizeof( OPENFILENAME ) ); ZeroMemory( szFileName, sizeof( szFileName ) ); ZeroMemory( szFileTitle, sizeof( szFileTitle ) ); OpenFileName.lStructSize = sizeof( OPENFILENAME ); OpenFileName.hwndOwner = hWnd; OpenFileName.lpstrFilter = szOpenFileFilter; OpenFileName.lpstrFile = szFileName; OpenFileName.nMaxFile = sizeof( szFileName )/sizeof(L_TCHAR); OpenFileName.lpstrFileTitle = szFileTitle; OpenFileName.nMaxFileTitle = sizeof( szFileTitle )/sizeof(L_TCHAR); OpenFileName.lpstrTitle = TEXT("Open a Vector File"); OpenFileName.Flags = OFN_HIDEREADONLY; if( GetOpenFileName( &OpenFileName ) ) { pTempVector = GlobalAllocPtr( GHND, sizeof( VECTORHANDLE ) ); if( pTempVector != NULL ) { L_VecInit( pTempVector ); L_VecSetBackgroundColor( pTempVector, GetSysColor( COLOR_WINDOW ) ); Hourglass( TRUE ); nRet = L_VecLoadFile( szFileName, pTempVector, NULL, NULL ) ; Hourglass( FALSE ); if( nRet == SUCCESS ) { pWindowData = GETMYWINDOWDATA( hWnd ); if( pWindowData->pVector != NULL ) { L_VecFree( pWindowData->pVector ); GlobalFreePtr( pWindowData->pVector ); } pWindowData->pVector = pTempVector; pWindowData->fViewVector = TRUE; InvalidateRect( hWnd, NULL, TRUE ); } else { L_VecFree( pTempVector ); GlobalFreePtr( pTempVector ); } } else nRet = ERROR_NO_MEMORY; if( nRet != SUCCESS ) ShowLeadError( hWnd, nRet ); } } /*---------------------------------------------------------------------------- | | Main_OnFileExit | | File/Exit menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnFileExit ( HWND hWnd ) { FORWARD_WM_CLOSE( hWnd, PostMessage ); } /*---------------------------------------------------------------------------- | | Main_OnViewRaster | | View/Raster menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnViewRaster ( HWND hWnd ) { LPMYWINDOWDATA pWindowData; pWindowData = GETMYWINDOWDATA( hWnd ); pWindowData->fViewRaster = pWindowData->fViewRaster ? FALSE : TRUE; InvalidateRect( hWnd, NULL, TRUE ); } /*---------------------------------------------------------------------------- | | Main_OnViewVector | | View/Vector menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnViewVector ( HWND hWnd ) { LPMYWINDOWDATA pWindowData; pWindowData = GETMYWINDOWDATA( hWnd ); pWindowData->fViewVector = pWindowData->fViewVector ? FALSE : TRUE; InvalidateRect( hWnd, NULL, TRUE ); } /*---------------------------------------------------------------------------- | | Main_OnZoomIn | | Zoom/In/... menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnZoomIn ( HWND hWnd, L_INT nZoom ) { ScaleImage( hWnd, nZoom ); } /*---------------------------------------------------------------------------- | | Main_OnZoomOut | | Zoom/Out/... menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnZoomOut ( HWND hWnd, L_INT nZoom ) { ScaleImage( hWnd, nZoom ); } /*---------------------------------------------------------------------------- | | Main_OnZoomNormal | | Zoom/Normal menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnZoomNormal ( HWND hWnd ) { ScaleImage( hWnd, 0 ); } /*---------------------------------------------------------------------------- | | Main_OnHelpAbout | | Help/About menu handler. | ---------------------------------------------------------------------------*/ static L_VOID Main_OnHelpAbout ( HWND hWnd ) { DialogBox( GetWindowInstance( hWnd ), MAKEINTRESOURCE( IDD_ABOUT ), hWnd, About_DlgProc ); } /*---------------------------------------------------------------------------- | | ShowLeadError | | Shows a message box with LEAD error value. | ---------------------------------------------------------------------------*/ static L_VOID ShowLeadError ( HWND hWnd, L_INT nErrorCode ) { L_TCHAR szBuffer[ 80 ]; wsprintf( szBuffer, TEXT("Error %d\nPlease check Error Codes in the help file"), nErrorCode ); MessageBox( hWnd, szBuffer, NULL, MB_ICONEXCLAMATION ); } /*---------------------------------------------------------------------------- | | ScaleDimension | | This function will scale the image by a factor of nFactor. | ---------------------------------------------------------------------------*/ static L_INT ScaleDimension ( L_INT nWidth, L_INT nFactor ) { L_INT nRet; if( 0 < nFactor ) /* Zoom in Condition */ nRet = nWidth * ( nFactor + 1 ); else if( 0 > nFactor ) /* Zoom out Condition */ nRet = nWidth / ( 1 - nFactor ); else nRet = nWidth; return nRet; } /*---------------------------------------------------------------------------- | | ScaleImage | | This function processes the scaling of the image. | ---------------------------------------------------------------------------*/ static L_VOID ScaleImage ( HWND hWnd, L_INT nScalar ) { LPMYWINDOWDATA pWindowData; RECT rcClient; pWindowData = GETMYWINDOWDATA( hWnd ); /* Set the image scale */ if( pWindowData->pBitmap != NULL ) { pWindowData->nScalar = nScalar; InvalidateRect( hWnd, NULL, TRUE ); GetClientRect( hWnd, &rcClient ); /* Call Routine to display the resized image */ Main_OnSize( hWnd, IsIconic( hWnd ) ? SIZEICONIC : SIZENORMAL, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top ); } } /*---------------------------------------------------------------------------- | | Hourglass | ---------------------------------------------------------------------------*/ static L_VOID Hourglass ( L_BOOL bHourglass ) { static HCURSOR hCursor = NULL; if( bHourglass ) hCursor = SetCursor( LoadCursor( NULL, IDC_WAIT ) ); else SetCursor( hCursor ); } /*---------------------------------------------------------------------------- | | About_DlgProc | | The About dialog box window procedure. | ---------------------------------------------------------------------------*/ static L_INT WINAPI About_DlgProc ( HWND hWnd, L_UINT uMessage, WPARAM wParam, LPARAM lParam ) { UNREFERENCED_PARAMETER( lParam ); switch( uMessage ) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch( LOWORD( wParam ) ) { case IDOK: case IDCANCEL: EndDialog( hWnd, LOWORD( wParam ) ); return TRUE; default: break; } return FALSE; default: break; } return FALSE; } /* EOF ----------------------------------------------------------------------*/