File:  [OS/2 SDKs] / pmsdk / samples / newcard / cfnew.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:28:12 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: pmsdk-1988, HEAD
Microsoft OS/2 SDK PM 08-08-1988

#define NOCOMM
#include "cardfile.h"


/*********************************************************************/
/*  Windows/PM Cardfile Shared Code                                  */
/*                                                                   */
/*  (c) Copyright Microsoft Corp. 1987,1988 - All Rights Reserved    */
/*********************************************************************/

/*********************************************************************/
/*    The following shared code was developed from the original      */
/* Cardfile application.  This code can be compiled to run under     */
/* either the Windows or the PM manager environment.  All            */
/* functionality associated with bitmaps or printing has been        */
/* deleted.  Some comments refering to these functions may still be  */
/* present in the code and should be disregarded. jw.                */
/*********************************************************************/



/*********************************************************************/
/* GetOldData -
/*    Gets data from a previous instance.  This routine is only 
/* applicable in Windows.
/*********************************************************************/

void FAR  GetOldData(hInstance)
HINST hInstance;
    {
    GetInstanceData(hInstance, (PSTR)&hbrWhite, 2);
    GetInstanceData(hInstance, (PSTR)&hbrBlack, 2);
    GetInstanceData(hInstance, (PSTR)&hbrGray, 2);
    GetInstanceData(hInstance, (PSTR)&hbrLine, 2);
    GetInstanceData(hInstance, (PSTR)&hbrBack, 2);
    GetInstanceData(hInstance, (PSTR)&CharFixHeight, 2);
    GetInstanceData(hInstance, (PSTR)&CharFixWidth, 2);
    GetInstanceData(hInstance, (PSTR)&ySpacing, 2);
    GetInstanceData(hInstance, (PSTR)&CardWidth, 2);
    GetInstanceData(hInstance, (PSTR)&CardHeight, 2);
    GetInstanceData(hInstance, (PSTR)&hAccel, 2);
    }


/*********************************************************************/
/* InitInstance -                                                    */
/*    Performs initialization for each instance, i.e., loads         */
/* resources, creates windows, ...                                   */
/*                                                                   */
/* Different in Windows and PM.                                      */
/*********************************************************************/

BOOL FAR InitInstance(hInstance, lpszCommandLine, cmdShow)
HINST hInstance;
LPSTR lpszCommandLine;
int cmdShow;
    {
    int i;
    LPCARDHEADER lpCards;
    HWND hwnd = NULL;
    LPSTR lpchTmp;
    PSTR pchTmp;
    FARPROC lpCardWndProc;
    char buf[3];
    RECT rect;
#ifndef IN_WINDOWS
    ULONG ctlData;
#endif

    /* get resident strings */
    LoadString(hInstance, IDS_EINSMEMORY, (LPSTR)NotEnoughMem, 50);
    LoadString(hInstance, IDS_UNTITLED, (LPSTR)rgchUntitled, 30);
    LoadString(hInstance, IDS_CARDDATA, (LPSTR)rgchCardData, 40);
    LoadString(hInstance, IDS_STRINGINSERT, (LPSTR)buf, 3);
    if (!LoadString(hInstance, IDS_CARDFILE, (LPSTR)rgchCardfile, 40))
        goto InitError;

    /* this is used in MergeStrings */
    wMerge = *(unsigned *)buf;

    /* make ProcInstances for all dialog procedures, and for the */
    /* filter proc for the edit control */
    lpDlgProc = MakeProcInstance( DlgProc, hInstance );
    lpfnOpen = MakeProcInstance( fnOpen, hInstance );
    lpfnSave = MakeProcInstance( fnSave, hInstance );
    lpfnAbout = MakeProcInstance( fnAbout, hInstance );
    lpCardWndProc = MakeProcInstance( (FARPROC) CardWndProc, hInstance);

    /* unlikely that last one will work but others won't */
    if (!lpCardWndProc)
        goto InitError;

    /* allocate the basic data structure for storing the card headers */
    hCards = GlobalAlloc(GHND, 
                         (long)sizeof(CARDHEADER)); /* alloc first card */

    if (!hCards)
        goto InitError;

    /* make a single blank card */
    CurIFile[0] = 0;        /* file is untitled */
    cCards = 1;
    CurCardHead.line[0] = 0;
    CurCard.hBitmap = 0;
    CurCardHead.flags = FNEW;
    lpCards = (LPCARDHEADER)GlobalLock(hCards);
    *lpCards = CurCardHead;
    GlobalUnlock(hCards);

    /* Note: PM mapping for CreateWindow is only */
    /*	     applicable for the main window */

    /* create the Cardfile window */
    hwnd = CreateWindow(
              (LPSTR) rgchCardfileClass,
              (LPSTR) "",
              WS_TILEDWINDOW | WS_HSCROLL | WS_VSCROLL,
              0, 0, 0, 100,
              NULL, NULL,
              hInstance,
              (LPSTR)NULL);

    if (!hwnd)
	goto InitError;

    /* Since the CreateWindow mapping is only applicable for the main */
    /* window, separate compilation must be used. */

#ifdef IN_WINDOWS
    /* create the edit control window */
    hCardWnd = CreateWindow(
                (LPSTR)"Edit",
                (LPSTR)"",
                WS_CHILD | WS_VISIBLE | ES_MULTILINE,
                0, 0, 0, 0,
                hwnd,
                IDM_EDITWINDOW,
                hInstance,
                (LPSTR)NULL);
#else
    /* Before creating child, make sure main window is updated. */
    WinUpdateWindow( FRAME(hwnd) );

    hCardWnd = WinCreateWindow( hwnd,
                                (PSZ) WC_ENTRYFIELD, 
                                (PSZ) "",
				WS_VISIBLE,
                                0, 0, 0, 0,
                                hwnd,
                                HWND_TOP,
                                IDM_EDITWINDOW,
                                (PSZ) NULL,
                                (PSZ) NULL );

    /* When a window is created, the initial size message has 0, 0 as  */
    /*  the size.  Get the real size and call the size routine as if  */
    /*  the proper size was sent.  This also positions the Cardfile   */
    /*  Edit window in the correct position in the main window        */

    WinQueryWindowRect( FRAME(hwnd), (PRECTL)&rect );
    CardfileSize( hwnd, rect.xRight, rect.yTop );

    /* Because the edit control window wasn't created when the main  */
    /* window was activated, the main window now has the focus.  Need */
    /* to give the focus to the edit window. */

    WinSetFocus( hAB, hCardWnd );
#endif    

    if (!hCardWnd)
        {
        /* some kind of failure */
InitError:
        MessageBox(hwnd, (LPSTR)NotEnoughMem, (LPSTR)NULL, MB_OK | MB_ICONEXCLAMATION);
        return(FALSE);
        }

    /* limit the number of characters that can be typed into the */
    /* edit control to the number that will fit on a card */
    SendMessage(hCardWnd, EM_LIMITTEXT, CARDTEXTSIZE, 0L);

    /* filter the messages going to the edit control */
    lpEditWndProc = (FARPROC)GetWindowLong(hCardWnd, GWL_WNDPROC);
    SetWindowLong(hCardWnd, GWL_WNDPROC, (LONG)lpCardWndProc);

    SetEditText((LPSTR)"");

    ShowWindow(FRAME(hwnd), cmdShow);

    MakeTmpFile();

    /* if a file was passed as a command line argument, try to read it in */
    for (lpchTmp = lpszCommandLine; *lpchTmp == ' '; lpchTmp++)
        ;
    for (pchTmp = CurIFile, i = 0; i < PATHMAX-1 && (BYTE)*lpchTmp > ' '; ++i)
        *pchTmp++ = *lpchTmp++;
    *pchTmp = 0;
    AnsiUpper((LPSTR)CurIFile);

    if (*CurIFile)
        if (!DoOpen(CurIFile))
            CurIFile[0] = 0;

    return(TRUE);
    }


/*********************************************************************/
/* CardfileInit -                                                    */
/*    Performs the one time initialization, such as class            */
/* registration.                                                     */
/*                                                                   */
/*    Different in PM and Windows                                    */
/*********************************************************************/

BOOL FAR CardfileInit()
    {
    PWNDCLASS   pClass;
    HANDLE      hIcon;
    TEXTMETRIC  Metrics;    /* structure filled with font info */
    LOGBRUSH   logBrush;
    HDC        hIC;

    /* In the PM version, brushes are treated as longs (the color).  */

    /* initialize the brushes */
#ifdef   IN_WINDOWS
    hbrWhite    = GetStockObject(WHITE_BRUSH);
    hbrBlack    = GetStockObject(BLACK_BRUSH);
    hbrGray     = GetStockObject(GRAY_BRUSH);
    GetObject(hbrGray, sizeof(LOGBRUSH), (LPSTR)&logBrush);
    if (!(hbrGray = CreateBrushIndirect((LPLOGBRUSH)&logBrush)))
        hbrGray = hbrWhite;
#else
    hbrWhite = WHITE_COLOR;
    hbrBlack = BLACK_COLOR;
    hbrGray = WHITE_COLOR;
#endif

    hIC = CreateIC((LPSTR)"DISPLAY", (LPSTR)NULL, (LPSTR)NULL, (LPSTR)NULL);
    if (!hIC)
        return(FALSE);

    /* For now, set the brushes to be blue and red in PM */

    /* background should be BLUE, and the line should be RED */
#ifdef   IN_WINDOWS
    if (!(hbrBack = (HBRUSH) CreateSolidBrush( CONVERT_RGB( RGBBLUE ))))
        hbrBack = hbrGray;
    if (!(hbrLine = (HBRUSH) CreateSolidBrush( CONVERT_RGB( RGBRED ))))
        hbrLine = hbrBlack;
#else
    hbrBack = BLUE_COLOR;
    hbrLine = RED_COLOR;
#endif



    /* Setup the font, and get metrics */
    GetTextMetrics(hIC, (LPTEXTMETRIC)(&Metrics));

    DeleteDC(hIC);

    /* In PM, application needs to specify the character height to the */
    /* binding system so that subsequent TextOut calls can be remapped to */
    /* have the proper y coordinate. */
    SET_CHARHEIGHT( (int) Metrics.tmHeight );

    CharFixHeight = (int) (Metrics.tmHeight+Metrics.tmExternalLeading); /* the height */
    ExtLeading = (int) Metrics.tmExternalLeading;
    CharFixWidth = (int) Metrics.tmAveCharWidth;   /* the average width */
    ySpacing = CharFixHeight+1;

    CardWidth = (LINELENGTH * CharFixWidth) + 3;
    CardHeight = (CARDLINES*CharFixHeight) + CharFixHeight + 1 + 2 + 2;

    /* Get the resource file info, such as icons, and accelerator tables */
    /* Register the class */

#ifdef   IN_WINDOWS
    hIcon  = LoadIcon(hCardfileInstance,(LPSTR)CFICON);
    hAccel = LoadAccelerators(hCardfileInstance, (LPSTR)CFACCEL);

    /* register the cardfile class */
    pClass = (PWNDCLASS) LocalAlloc(LPTR, sizeof(WNDCLASS));
    if (!pClass)
        return(FALSE);
    pClass->lpszClassName = (LPSTR)rgchCardfileClass;
    pClass->hCursor       = NULL;         /* normal cursor is arrow */
    pClass->hIcon         = hIcon;
    pClass->hbrBackground = NULL;
    pClass->style         = CS_VREDRAW | CS_DBLCLKS;
    pClass->lpfnWndProc   = CardfileWndProc;
    pClass->hInstance     = hCardfileInstance;
    pClass->lpszMenuName  = (LPSTR)CFMENU;
    if (!RegisterClass((LPWNDCLASS) pClass))
        return FALSE;
    LocalFree((HANDLE)pClass);
#else
    /* Register the PM class */

    if ( !WinRegisterClass( hAB,
                            (PSZ) rgchCardfileClass,
                            (PFNWP) CardfileWndProc,
                            CS_SIZEREDRAW,
                            NULL ) )
        return FALSE;
#endif


    return TRUE;
    }


unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.