File:  [OS/2 SDKs] / pmsdk / samples / newcard / cfdb.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

#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.                */
/*********************************************************************/


/*********************************************************************/
/* Dialog Conventions:                                               */
/*                                                                   */
/*    WINWORD - Word in Windows; Long in PM                          */
/*    LOUSHORT - NULL in Windows; Standard meaning in PM             */
/*    DLGRET - BOOL in Windows; Long in PM                           */
/*    DIALOGDEFAULT - return( FALSE ) in Windows;                    */
/*                    WinDefDlgProc in PM                            */
/*********************************************************************/


/*********************************************************************/
/* fnOpen -                                                          */
/*    The standard open dialog box.  Although this code is           */
/* complicated and messy, it illustrates that through the use of the */
/* dialog conventions defined in the shared code schema, dialog      */
/* procedures can be converted from Windows to PM with almost no     */
/* effort.                                                           */
/*                                                                   */
/*    Slightly different for PM and Windows                          */
/*********************************************************************/

DLGRET far PASCAL fnOpen(hwnd, msg, wParam, lParam)
HWND hwnd;
unsigned msg;
WINWORD wParam;
DWORD lParam;
    {
    int item;
    int cchFile, cchDir;
    char *pchFile;
    BOOL    fWild;
    char *pResultBuf = NULL;
    char szNewName[PATHMAX];
    int len;

    switch (msg)
        {
        case WM_INITDIALOG:
            SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)"*.CRD");
            SendDlgItemMessage(hwnd, IDD_EDIT, EM_LIMITTEXT, 128, 0L);

            /* For PM, this function is simulated in pmbind.c */
            DlgDirList(hwnd, (LPSTR)"*.CRD", IDD_LISTBOX, IDD_PATH, 
                       ATTRDIRLIST);
            break;

        case WM_COMMAND:
#ifndef  IN_WINDOWS
        /* The only place in this dialog procedure which is #ifdef'ed! */

        /* In PM, treat WM_CONTROL message the same as WM_COMMAND */
        /* message after moving Code from wParam to lParam */

        case WM_CONTROL:
            lParam = wParam;
#endif
            switch (LOUSHORT(wParam))
                {
                case IDOK:
LoadIt:
                    if (IsWindowEnabled(GetDlgItem(hwnd,IDOK)))
                        {
                        len = 7 + GetWindowTextLength(
                                    GetDlgItem(hwnd, IDD_EDIT));
                        if(pResultBuf = (char *)LocalAlloc(LPTR, len))
                            {
                            GetDlgItemText(hwnd, IDD_EDIT, 
                                           (LPSTR)pResultBuf, len);
                            Mylstrcpy((LPSTR)szNewName, (LPSTR)pResultBuf);

                            /* Append appropriate extension to */
                            /* user's entry */
                            DlgAddCorrectExtension(szNewName, TRUE);

                            /* Try to open directory.  If successful, fill */
                            /* listbox with contents of new directory. */
                            /* Otherwise, open datafile. */
                            if (FSearchSpec(szNewName))
                                {
                                if (DlgDirList(hwnd, (LPSTR)szNewName, 
                                    IDD_LISTBOX, IDD_PATH, ATTRDIRLIST))
                                    {
                                    SetDlgItemText(hwnd, IDD_EDIT, 
                                                   (LPSTR)szNewName);
                                    break;
                                    }
                                }

                            DlgAddCorrectExtension(pResultBuf, FALSE);
                            /* If no directory list and filename contained */
                            /* search spec, honk and don't try to open. */
                            if (FSearchSpec(pResultBuf))
                                {
                                MessageBeep(0);
                                break;
                                }
                            AnsiUpper((LPSTR)pResultBuf);
                            }
                        }
                    EndDialog(hwnd, (WORD)pResultBuf);
                    break;

                case IDCANCEL:
                    EndDialog(hwnd, NULL);
                    break;

                case IDD_LISTBOX:
                   switch (HIWORD(lParam))
                      {
                      case LBN_SELCHANGE:
                         len = GetWindowTextLength(
                                 GetDlgItem(hwnd, IDD_EDIT));
                         if(pResultBuf = (char *)LocalAlloc(LPTR, ++len))
                            {
                            GetDlgItemText(hwnd, IDD_EDIT, 
                                           (LPSTR)pResultBuf, len);
                            if (DlgDirSelect(hwnd, (LPSTR)szNewName, 
                                             IDD_LISTBOX))
                               {
                               cchDir = Mylstrlen((LPSTR)szNewName);
                               cchFile = Mylstrlen((LPSTR)pResultBuf);
                               pchFile = pResultBuf+cchFile;
                               fWild = (*pchFile == '*' || 
                                        *pchFile == ':');
                               while (pchFile > pResultBuf)
                                  {
                                  pchFile = FAR_TO_NEAR( AnsiPrev(
                                    (LPSTR)(pResultBuf), (LPSTR)pchFile));
                                  if (*pchFile == '*' || *pchFile == '?')
                                     fWild = TRUE;
                                  if (*pchFile == '\\' || *pchFile == ':')
                                     {
                                     pchFile = FAR_TO_NEAR( AnsiNext(
                                                         (LPSTR)pchFile) );
                                     break;
                                     }
                                  }
                               if (fWild)
                                  Mylstrcpy((LPSTR)szNewName + cchDir, 
                                             (LPSTR)pchFile);
                               else
                                  Mylstrcpy((LPSTR)szNewName + cchDir, 
                                            (LPSTR)"*.CRD");
                               }
                            SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)szNewName);
                            LocalFree((HANDLE)pResultBuf);
                            }
                         break;
                      case LBN_DBLCLK:
                         goto LoadIt;    /* go load it up */
                      }
                    break;
                case IDD_EDIT:
                    CheckOkEnable(hwnd, HIWORD(lParam));
                    break;
                default:
                    return(FALSE);
                }
            break;
        default:
            DIALOGDEFAULT( hwnd, msg, wParam, lParam );
        }
    return(0L);
    }


/*********************************************************************/
/* DlgAddCorrectExtension -                                          */
/*    Given filename or partial filename or search spec or partial   */
/* search spec, add appropriate extension.                           */
/*                                                                   */
/*    Same for PM and Windows                                        */
/*********************************************************************/

void DlgAddCorrectExtension(szEdit, fSearching)
PSTR    szEdit;
BOOL    fSearching;
{
    register char    *pchLast;
    register char    *pchT;
    int ichExt;
    BOOL    fDone = FALSE;
    int     cchEdit;

    pchT = FAR_TO_NEAR( AnsiPrev((LPSTR)szEdit, 
                  (LPSTR)(szEdit + (cchEdit = Mylstrlen((LPSTR)szEdit)))) );
    pchLast = pchT;

    if ((*pchLast == '.' && 
         *(AnsiPrev((LPSTR)szEdit, (LPSTR)pchLast)) == '.') && cchEdit == 2)
        ichExt = 0;
    else if (*pchLast == '\\' || *pchLast == ':')
        ichExt = 1;
    else {
        ichExt = fSearching ? 0 : 2;
        for (; pchT > szEdit; 
               pchT = FAR_TO_NEAR( AnsiPrev((LPSTR)szEdit, (LPSTR)pchT)) ) {
            /* If we're not searching and we encounter a period, don't add
               any extension.  If we are searching, period is assumed to be
               part of directory name, so go ahead and add extension. However,
               if we are searching and find a search spec, do not add any
               extension. */
            if (fSearching) {
                if (*pchT == '*' || *pchT == '?')
                    return;
            } else if (*pchT == '.'){
                return;
            }
            /* Quit when we get to beginning of last node. */
            if (*pchT == '\\')
                break;
        }
        /* Special case hack fix since AnsiPrev can not return value */
        /* less than szEdit. If first char is wild card, return without */
        /* appending. */
        if (fSearching && (*pchT == '*' || *pchT == '?'))
            return;
    }
    Mylstrcpy((LPSTR)(pchLast+1), (LPSTR)(szExtSave+ichExt));
}

/*********************************************************************/
/* FSearchSpec -                                                     */
/*    Return TRUE iff 0 terminated string contains a '*' or '\'      */
/*                                                                   */
/*    Same for PM and Windows                                        */
/*********************************************************************/

BOOL    FSearchSpec(sz)
register PSTR sz;
{
    for (; *sz;sz++) {
        if (*sz == '*' || *sz == '?')
            return TRUE;
    }
    return FALSE;
}

/*********************************************************************/
/* fnSave -                                                          */
/*    Dialog function for "Save as" .  User must specify new name    */
/* of file.                                                          */
/*                                                                   */
/*    Slightly different for PM and Windows                          */
/*********************************************************************/

DLGRET   far PASCAL fnSave(hwnd, msg, wParam, lParam)
HWND hwnd;
unsigned msg;
WINWORD wParam;
DWORD lParam;
    {
    char *pResultBuf;
    int len;
    char rgch[128];
    char    *pchFileName;
    char    *pchCmp;
    char    *pchTest;
    char    *PFileInPath();

    switch (msg)
        {
        case WM_INITDIALOG:
            /* Initialize Path field with current directory */
            DlgDirList(hwnd, (LPSTR)NULL, 0, IDD_PATH, 0);

            if (CurIFile[0])
                {
                /* rgch gets current directory string, terminated */
                /* with "\\\0" */
                len = GetDlgItemText(hwnd, IDD_PATH, (LPSTR)rgch, 128);
                if (rgch[len-1] != '\\')
                    {
                    rgch[len] = '\\';
                    rgch[++len] = 0;
                    }

                /* Now see if path in reopen buffer matches current */
                /* directory. */
                pchFileName = CurIFile;
                pchTest = PFileInPath(CurIFile);
                pchCmp = rgch;

                while (pchFileName < pchTest)
                    {
                    if (*pchFileName != *pchCmp)
                        break;
                     pchCmp = (PSTR) AnsiNext(pchCmp);
                     pchFileName = (PSTR)AnsiNext(pchFileName);
                    }

                /* If paths don't match, reset pchFileName to point to */
                /* fully qualified path. (Otherwise, pchFileName already */
                /* points to filename. */
                if (*pchCmp || pchFileName < pchTest)
                    pchFileName = CurIFile;

                SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)pchFileName);
                }
            else
                EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
            break;

        case WM_COMMAND:
#ifndef  IN_WINDOWS
        /* In PM, treat WM_CONTROL message the same as WM_COMMAND */
        /* message after moving Code from wParam to lParam */

        case WM_CONTROL:
            lParam = wParam;
#endif
            switch (LOUSHORT(wParam))
                {
                case IDOK:
                    if (IsWindowEnabled(GetDlgItem(hwnd, IDOK)))
                        {
                        len = 4+GetWindowTextLength(
                                             GetDlgItem(hwnd, IDD_EDIT));
                        if(pResultBuf = (char *)LocalAlloc(LPTR, ++len))
                            {
                            GetDlgItemText(hwnd, IDD_EDIT, 
                                           (LPSTR)pResultBuf, len);
                            AppendExtension(pResultBuf, pResultBuf);
                            }
                        EndDialog(hwnd, (WORD)pResultBuf);
                        }
                    break;

                case IDCANCEL:
                    EndDialog(hwnd, NULL);
                    break;
                case IDD_EDIT:
                    CheckOkEnable(hwnd, HIWORD(lParam));
                    break;
                default:
                    return(FALSE);
                }
            break;
        default:
            DIALOGDEFAULT( hwnd, msg, wParam, lParam );
        }
    return(0L);
    }

/*********************************************************************/
/* PFileInPath -                                                     */
/*    Given filename which may or maynot include path, return        */
/* pointer to filename (not including path part.)                    */
/*                                                                   */
/*    Same for PM and Windows                                        */
/*********************************************************************/

PSTR PFileInPath(sz)
PSTR sz;
{
    PSTR pch;

    /* Strip path/drive specification from name if there is one */
    pch = FAR_TO_NEAR( AnsiPrev((LPSTR)sz, 
                                (LPSTR)(sz + Mylstrlen((LPSTR)sz))) );
    while (pch > sz) {
        pch = FAR_TO_NEAR( AnsiPrev((LPSTR)sz, (LPSTR)pch) );
        if (*pch == '\\' || *pch == ':') {
            pch = FAR_TO_NEAR( AnsiNext((LPSTR)pch) );
            break;
        }
    }
    return(pch);
}


/*********************************************************************/
/* CheckOkEnable -                                                   */
/*    Either enables or disables OK button depending on whether edit */
/* line is empty.                                                    */
/*                                                                   */
/*    Same for PM and Windows                                        */
/*********************************************************************/

void CheckOkEnable(hwnd, message)
HWND    hwnd;
unsigned message;
    {
    if (message == EN_CHANGE)
        EnableWindow(GetDlgItem(hwnd, IDOK), 
                     GetWindowTextLength(GetDlgItem(hwnd, IDD_EDIT)) != 0);
    }

/*********************************************************************/
/* fnAbout -                                                         */
/*    The about dialog box: very simple, only shows the number of    */
/* cards in the file.                                                */
/*                                                                   */
/*    Same for PM and Windows                                        */
/*********************************************************************/

DLGRET   far PASCAL fnAbout(hwnd, msg, wParam, lParam)
HWND hwnd;
unsigned msg;
WINWORD wParam;
DWORD lParam;
    {
    char buf[40];
    int len;
    int id;

    switch (msg)
    if (msg == WM_INITDIALOG)
	{
	case WM_INITDIALOG:
	    len = IntegerToAscii(cCards, buf);
	    if (cCards == 1)
		id = IDS_CARD;
	    else
		id = IDS_CARDS;
	    LoadString(hCardfileInstance, id, (LPSTR)(buf+len), 40-len);
	    SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)buf);
	    return(TRUE);

	case WM_COMMAND:
#ifndef IN_WINDOWS
    /* In PM, treat WM_CONTROL message the same as WM_COMMAND */
    /* after moving Code from wParam to lParam */

	case WM_CONTROL:
	    lParam = wParam;
#endif
	    if (LOUSHORT(wParam) == IDOK)
		{
		EndDialog(hwnd, NULL);
		DIALOGDEFAULT( hwnd, msg, wParam, lParam );
		}
	    break;
	default:
	    DIALOGDEFAULT( hwnd, msg, wParam, lParam );
	}
    return(0L);
    }

/*********************************************************************/
/* IntegerToAscii -                                                  */
/*    Convert an int to ascii                                        */
/*                                                                   */
/*    Same for PM and Windows                                        */
/*********************************************************************/

int IntegerToAscii(n, psz)
unsigned n;
char *psz;
    {
    char *pch = psz;
    char ch;
    int len;

    do
        {
        *pch++ = (char) (n % 10 + '0');
        n /= 10;
        }
    while (n > 0);

    len = pch - psz;
    *pch-- = '\0';
    /* reverse the digits */

    while (psz < pch)
        {
        ch = *psz;
        *psz++ = *pch;
        *pch-- = ch;
        }
    return(len);
    }

/*********************************************************************/
/* DlgProc -                                                         */
/*    Generic dialog proc                                            */
/*                                                                   */
/*    Slightly different for PM and Windows                          */
/*********************************************************************/

DLGRET   far PASCAL DlgProc(hDB, message, wParam, lParam)
HWND hDB;
unsigned message;
WINWORD wParam;
DWORD lParam;
    {
    char *pResultBuf;
    char *pchInit;
    int len;

    switch (message)
        {
        case WM_INITDIALOG:
            /* initialize edit item based on which dialog it is */
            switch(DBcmd)
                {
                case DTHEADER:
                    pchInit = CurCardHead.line;
                    break;
                case DTFIND:
                    pchInit = CurIFind;
                    break;
                default:
                    pchInit = "";
                }
            SetDlgItemText(hDB, IDD_EDIT, (LPSTR)pchInit);
            SetFocus(GetDlgItem(hDB, IDD_EDIT));
            return(TRUE);
            break;

        case WM_COMMAND:
#ifndef  IN_WINDOWS
        /* In PM, treat WM_CONTROL message the same as WM_COMMAND */
        /* message after moving Code from wParam to lParam */

        case WM_CONTROL:
            lParam = wParam;
#endif
            /* all these get a single string */
            pResultBuf = NULL;
            switch (LOUSHORT(wParam))
                {
                case IDOK:
                    /* allocate buffer, read text and pass it back */
                    len = GetWindowTextLength(GetDlgItem(hDB, IDD_EDIT));                  
                    if (len || DBcmd == DTHEADER || DBcmd == DTADD)
                        if(pResultBuf = (char *)LocalAlloc(LPTR, ++len))
                            GetDlgItemText(hDB, IDD_EDIT, 
                                           (LPSTR)pResultBuf, len);
                    break;
                case IDCANCEL:
                    break;
                default:
                    return(FALSE);
                }
            /* return pointer to buffer */
            EndDialog(hDB, (int)pResultBuf);
            return(0L);
            break;
        default:
            DIALOGDEFAULT( hDB, message, wParam, lParam );
        }
    }



unix.superglobalmegacorp.com

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