Annotation of pmsdk/samples/newcard/cfdb.c, revision 1.1.1.1

1.1       root        1: #include "cardfile.h"
                      2: 
                      3: /*********************************************************************/
                      4: /*  Windows/PM Cardfile Shared Code                                  */
                      5: /*                                                                   */
                      6: /*  (c) Copyright Microsoft Corp. 1987,1988 - All Rights Reserved    */
                      7: /*********************************************************************/
                      8: 
                      9: /*********************************************************************/
                     10: /*    The following shared code was developed from the original      */
                     11: /* Cardfile application.  This code can be compiled to run under     */
                     12: /* either the Windows or the PM manager environment.  All            */
                     13: /* functionality associated with bitmaps or printing has been        */
                     14: /* deleted.  Some comments refering to these functions may still be  */
                     15: /* present in the code and should be disregarded. jw.                */
                     16: /*********************************************************************/
                     17: 
                     18: 
                     19: /*********************************************************************/
                     20: /* Dialog Conventions:                                               */
                     21: /*                                                                   */
                     22: /*    WINWORD - Word in Windows; Long in PM                          */
                     23: /*    LOUSHORT - NULL in Windows; Standard meaning in PM             */
                     24: /*    DLGRET - BOOL in Windows; Long in PM                           */
                     25: /*    DIALOGDEFAULT - return( FALSE ) in Windows;                    */
                     26: /*                    WinDefDlgProc in PM                            */
                     27: /*********************************************************************/
                     28: 
                     29: 
                     30: /*********************************************************************/
                     31: /* fnOpen -                                                          */
                     32: /*    The standard open dialog box.  Although this code is           */
                     33: /* complicated and messy, it illustrates that through the use of the */
                     34: /* dialog conventions defined in the shared code schema, dialog      */
                     35: /* procedures can be converted from Windows to PM with almost no     */
                     36: /* effort.                                                           */
                     37: /*                                                                   */
                     38: /*    Slightly different for PM and Windows                          */
                     39: /*********************************************************************/
                     40: 
                     41: DLGRET far PASCAL fnOpen(hwnd, msg, wParam, lParam)
                     42: HWND hwnd;
                     43: unsigned msg;
                     44: WINWORD wParam;
                     45: DWORD lParam;
                     46:     {
                     47:     int item;
                     48:     int cchFile, cchDir;
                     49:     char *pchFile;
                     50:     BOOL    fWild;
                     51:     char *pResultBuf = NULL;
                     52:     char szNewName[PATHMAX];
                     53:     int len;
                     54: 
                     55:     switch (msg)
                     56:         {
                     57:         case WM_INITDIALOG:
                     58:             SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)"*.CRD");
                     59:             SendDlgItemMessage(hwnd, IDD_EDIT, EM_LIMITTEXT, 128, 0L);
                     60: 
                     61:             /* For PM, this function is simulated in pmbind.c */
                     62:             DlgDirList(hwnd, (LPSTR)"*.CRD", IDD_LISTBOX, IDD_PATH, 
                     63:                        ATTRDIRLIST);
                     64:             break;
                     65: 
                     66:         case WM_COMMAND:
                     67: #ifndef  IN_WINDOWS
                     68:         /* The only place in this dialog procedure which is #ifdef'ed! */
                     69: 
                     70:         /* In PM, treat WM_CONTROL message the same as WM_COMMAND */
                     71:         /* message after moving Code from wParam to lParam */
                     72: 
                     73:         case WM_CONTROL:
                     74:             lParam = wParam;
                     75: #endif
                     76:             switch (LOUSHORT(wParam))
                     77:                 {
                     78:                 case IDOK:
                     79: LoadIt:
                     80:                     if (IsWindowEnabled(GetDlgItem(hwnd,IDOK)))
                     81:                         {
                     82:                         len = 7 + GetWindowTextLength(
                     83:                                     GetDlgItem(hwnd, IDD_EDIT));
                     84:                         if(pResultBuf = (char *)LocalAlloc(LPTR, len))
                     85:                             {
                     86:                             GetDlgItemText(hwnd, IDD_EDIT, 
                     87:                                            (LPSTR)pResultBuf, len);
                     88:                             Mylstrcpy((LPSTR)szNewName, (LPSTR)pResultBuf);
                     89: 
                     90:                             /* Append appropriate extension to */
                     91:                             /* user's entry */
                     92:                             DlgAddCorrectExtension(szNewName, TRUE);
                     93: 
                     94:                             /* Try to open directory.  If successful, fill */
                     95:                             /* listbox with contents of new directory. */
                     96:                             /* Otherwise, open datafile. */
                     97:                             if (FSearchSpec(szNewName))
                     98:                                 {
                     99:                                 if (DlgDirList(hwnd, (LPSTR)szNewName, 
                    100:                                     IDD_LISTBOX, IDD_PATH, ATTRDIRLIST))
                    101:                                     {
                    102:                                     SetDlgItemText(hwnd, IDD_EDIT, 
                    103:                                                    (LPSTR)szNewName);
                    104:                                     break;
                    105:                                     }
                    106:                                 }
                    107: 
                    108:                             DlgAddCorrectExtension(pResultBuf, FALSE);
                    109:                             /* If no directory list and filename contained */
                    110:                             /* search spec, honk and don't try to open. */
                    111:                             if (FSearchSpec(pResultBuf))
                    112:                                 {
                    113:                                 MessageBeep(0);
                    114:                                 break;
                    115:                                 }
                    116:                             AnsiUpper((LPSTR)pResultBuf);
                    117:                             }
                    118:                         }
                    119:                     EndDialog(hwnd, (WORD)pResultBuf);
                    120:                     break;
                    121: 
                    122:                 case IDCANCEL:
                    123:                     EndDialog(hwnd, NULL);
                    124:                     break;
                    125: 
                    126:                 case IDD_LISTBOX:
                    127:                    switch (HIWORD(lParam))
                    128:                       {
                    129:                       case LBN_SELCHANGE:
                    130:                          len = GetWindowTextLength(
                    131:                                  GetDlgItem(hwnd, IDD_EDIT));
                    132:                          if(pResultBuf = (char *)LocalAlloc(LPTR, ++len))
                    133:                             {
                    134:                             GetDlgItemText(hwnd, IDD_EDIT, 
                    135:                                            (LPSTR)pResultBuf, len);
                    136:                             if (DlgDirSelect(hwnd, (LPSTR)szNewName, 
                    137:                                              IDD_LISTBOX))
                    138:                                {
                    139:                                cchDir = Mylstrlen((LPSTR)szNewName);
                    140:                                cchFile = Mylstrlen((LPSTR)pResultBuf);
                    141:                                pchFile = pResultBuf+cchFile;
                    142:                                fWild = (*pchFile == '*' || 
                    143:                                         *pchFile == ':');
                    144:                                while (pchFile > pResultBuf)
                    145:                                   {
                    146:                                   pchFile = FAR_TO_NEAR( AnsiPrev(
                    147:                                     (LPSTR)(pResultBuf), (LPSTR)pchFile));
                    148:                                   if (*pchFile == '*' || *pchFile == '?')
                    149:                                      fWild = TRUE;
                    150:                                   if (*pchFile == '\\' || *pchFile == ':')
                    151:                                      {
                    152:                                      pchFile = FAR_TO_NEAR( AnsiNext(
                    153:                                                          (LPSTR)pchFile) );
                    154:                                      break;
                    155:                                      }
                    156:                                   }
                    157:                                if (fWild)
                    158:                                   Mylstrcpy((LPSTR)szNewName + cchDir, 
                    159:                                              (LPSTR)pchFile);
                    160:                                else
                    161:                                   Mylstrcpy((LPSTR)szNewName + cchDir, 
                    162:                                             (LPSTR)"*.CRD");
                    163:                                }
                    164:                             SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)szNewName);
                    165:                             LocalFree((HANDLE)pResultBuf);
                    166:                             }
                    167:                          break;
                    168:                       case LBN_DBLCLK:
                    169:                          goto LoadIt;    /* go load it up */
                    170:                       }
                    171:                     break;
                    172:                 case IDD_EDIT:
                    173:                     CheckOkEnable(hwnd, HIWORD(lParam));
                    174:                     break;
                    175:                 default:
                    176:                     return(FALSE);
                    177:                 }
                    178:             break;
                    179:         default:
                    180:             DIALOGDEFAULT( hwnd, msg, wParam, lParam );
                    181:         }
                    182:     return(0L);
                    183:     }
                    184: 
                    185: 
                    186: /*********************************************************************/
                    187: /* DlgAddCorrectExtension -                                          */
                    188: /*    Given filename or partial filename or search spec or partial   */
                    189: /* search spec, add appropriate extension.                           */
                    190: /*                                                                   */
                    191: /*    Same for PM and Windows                                        */
                    192: /*********************************************************************/
                    193: 
                    194: void DlgAddCorrectExtension(szEdit, fSearching)
                    195: PSTR    szEdit;
                    196: BOOL    fSearching;
                    197: {
                    198:     register char    *pchLast;
                    199:     register char    *pchT;
                    200:     int ichExt;
                    201:     BOOL    fDone = FALSE;
                    202:     int     cchEdit;
                    203: 
                    204:     pchT = FAR_TO_NEAR( AnsiPrev((LPSTR)szEdit, 
                    205:                   (LPSTR)(szEdit + (cchEdit = Mylstrlen((LPSTR)szEdit)))) );
                    206:     pchLast = pchT;
                    207: 
                    208:     if ((*pchLast == '.' && 
                    209:          *(AnsiPrev((LPSTR)szEdit, (LPSTR)pchLast)) == '.') && cchEdit == 2)
                    210:         ichExt = 0;
                    211:     else if (*pchLast == '\\' || *pchLast == ':')
                    212:         ichExt = 1;
                    213:     else {
                    214:         ichExt = fSearching ? 0 : 2;
                    215:         for (; pchT > szEdit; 
                    216:                pchT = FAR_TO_NEAR( AnsiPrev((LPSTR)szEdit, (LPSTR)pchT)) ) {
                    217:             /* If we're not searching and we encounter a period, don't add
                    218:                any extension.  If we are searching, period is assumed to be
                    219:                part of directory name, so go ahead and add extension. However,
                    220:                if we are searching and find a search spec, do not add any
                    221:                extension. */
                    222:             if (fSearching) {
                    223:                 if (*pchT == '*' || *pchT == '?')
                    224:                     return;
                    225:             } else if (*pchT == '.'){
                    226:                 return;
                    227:             }
                    228:             /* Quit when we get to beginning of last node. */
                    229:             if (*pchT == '\\')
                    230:                 break;
                    231:         }
                    232:         /* Special case hack fix since AnsiPrev can not return value */
                    233:         /* less than szEdit. If first char is wild card, return without */
                    234:         /* appending. */
                    235:         if (fSearching && (*pchT == '*' || *pchT == '?'))
                    236:             return;
                    237:     }
                    238:     Mylstrcpy((LPSTR)(pchLast+1), (LPSTR)(szExtSave+ichExt));
                    239: }
                    240: 
                    241: /*********************************************************************/
                    242: /* FSearchSpec -                                                     */
                    243: /*    Return TRUE iff 0 terminated string contains a '*' or '\'      */
                    244: /*                                                                   */
                    245: /*    Same for PM and Windows                                        */
                    246: /*********************************************************************/
                    247: 
                    248: BOOL    FSearchSpec(sz)
                    249: register PSTR sz;
                    250: {
                    251:     for (; *sz;sz++) {
                    252:         if (*sz == '*' || *sz == '?')
                    253:             return TRUE;
                    254:     }
                    255:     return FALSE;
                    256: }
                    257: 
                    258: /*********************************************************************/
                    259: /* fnSave -                                                          */
                    260: /*    Dialog function for "Save as" .  User must specify new name    */
                    261: /* of file.                                                          */
                    262: /*                                                                   */
                    263: /*    Slightly different for PM and Windows                          */
                    264: /*********************************************************************/
                    265: 
                    266: DLGRET   far PASCAL fnSave(hwnd, msg, wParam, lParam)
                    267: HWND hwnd;
                    268: unsigned msg;
                    269: WINWORD wParam;
                    270: DWORD lParam;
                    271:     {
                    272:     char *pResultBuf;
                    273:     int len;
                    274:     char rgch[128];
                    275:     char    *pchFileName;
                    276:     char    *pchCmp;
                    277:     char    *pchTest;
                    278:     char    *PFileInPath();
                    279: 
                    280:     switch (msg)
                    281:         {
                    282:         case WM_INITDIALOG:
                    283:             /* Initialize Path field with current directory */
                    284:             DlgDirList(hwnd, (LPSTR)NULL, 0, IDD_PATH, 0);
                    285: 
                    286:             if (CurIFile[0])
                    287:                 {
                    288:                 /* rgch gets current directory string, terminated */
                    289:                 /* with "\\\0" */
                    290:                 len = GetDlgItemText(hwnd, IDD_PATH, (LPSTR)rgch, 128);
                    291:                 if (rgch[len-1] != '\\')
                    292:                     {
                    293:                     rgch[len] = '\\';
                    294:                     rgch[++len] = 0;
                    295:                     }
                    296: 
                    297:                 /* Now see if path in reopen buffer matches current */
                    298:                 /* directory. */
                    299:                 pchFileName = CurIFile;
                    300:                 pchTest = PFileInPath(CurIFile);
                    301:                 pchCmp = rgch;
                    302: 
                    303:                 while (pchFileName < pchTest)
                    304:                     {
                    305:                     if (*pchFileName != *pchCmp)
                    306:                         break;
                    307:                      pchCmp = (PSTR) AnsiNext(pchCmp);
                    308:                      pchFileName = (PSTR)AnsiNext(pchFileName);
                    309:                     }
                    310: 
                    311:                 /* If paths don't match, reset pchFileName to point to */
                    312:                 /* fully qualified path. (Otherwise, pchFileName already */
                    313:                 /* points to filename. */
                    314:                 if (*pchCmp || pchFileName < pchTest)
                    315:                     pchFileName = CurIFile;
                    316: 
                    317:                 SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)pchFileName);
                    318:                 }
                    319:             else
                    320:                 EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
                    321:             break;
                    322: 
                    323:         case WM_COMMAND:
                    324: #ifndef  IN_WINDOWS
                    325:         /* In PM, treat WM_CONTROL message the same as WM_COMMAND */
                    326:         /* message after moving Code from wParam to lParam */
                    327: 
                    328:         case WM_CONTROL:
                    329:             lParam = wParam;
                    330: #endif
                    331:             switch (LOUSHORT(wParam))
                    332:                 {
                    333:                 case IDOK:
                    334:                     if (IsWindowEnabled(GetDlgItem(hwnd, IDOK)))
                    335:                         {
                    336:                         len = 4+GetWindowTextLength(
                    337:                                              GetDlgItem(hwnd, IDD_EDIT));
                    338:                         if(pResultBuf = (char *)LocalAlloc(LPTR, ++len))
                    339:                             {
                    340:                             GetDlgItemText(hwnd, IDD_EDIT, 
                    341:                                            (LPSTR)pResultBuf, len);
                    342:                             AppendExtension(pResultBuf, pResultBuf);
                    343:                             }
                    344:                         EndDialog(hwnd, (WORD)pResultBuf);
                    345:                         }
                    346:                     break;
                    347: 
                    348:                 case IDCANCEL:
                    349:                     EndDialog(hwnd, NULL);
                    350:                     break;
                    351:                 case IDD_EDIT:
                    352:                     CheckOkEnable(hwnd, HIWORD(lParam));
                    353:                     break;
                    354:                 default:
                    355:                     return(FALSE);
                    356:                 }
                    357:             break;
                    358:         default:
                    359:             DIALOGDEFAULT( hwnd, msg, wParam, lParam );
                    360:         }
                    361:     return(0L);
                    362:     }
                    363: 
                    364: /*********************************************************************/
                    365: /* PFileInPath -                                                     */
                    366: /*    Given filename which may or maynot include path, return        */
                    367: /* pointer to filename (not including path part.)                    */
                    368: /*                                                                   */
                    369: /*    Same for PM and Windows                                        */
                    370: /*********************************************************************/
                    371: 
                    372: PSTR PFileInPath(sz)
                    373: PSTR sz;
                    374: {
                    375:     PSTR pch;
                    376: 
                    377:     /* Strip path/drive specification from name if there is one */
                    378:     pch = FAR_TO_NEAR( AnsiPrev((LPSTR)sz, 
                    379:                                 (LPSTR)(sz + Mylstrlen((LPSTR)sz))) );
                    380:     while (pch > sz) {
                    381:         pch = FAR_TO_NEAR( AnsiPrev((LPSTR)sz, (LPSTR)pch) );
                    382:         if (*pch == '\\' || *pch == ':') {
                    383:             pch = FAR_TO_NEAR( AnsiNext((LPSTR)pch) );
                    384:             break;
                    385:         }
                    386:     }
                    387:     return(pch);
                    388: }
                    389: 
                    390: 
                    391: /*********************************************************************/
                    392: /* CheckOkEnable -                                                   */
                    393: /*    Either enables or disables OK button depending on whether edit */
                    394: /* line is empty.                                                    */
                    395: /*                                                                   */
                    396: /*    Same for PM and Windows                                        */
                    397: /*********************************************************************/
                    398: 
                    399: void CheckOkEnable(hwnd, message)
                    400: HWND    hwnd;
                    401: unsigned message;
                    402:     {
                    403:     if (message == EN_CHANGE)
                    404:         EnableWindow(GetDlgItem(hwnd, IDOK), 
                    405:                      GetWindowTextLength(GetDlgItem(hwnd, IDD_EDIT)) != 0);
                    406:     }
                    407: 
                    408: /*********************************************************************/
                    409: /* fnAbout -                                                         */
                    410: /*    The about dialog box: very simple, only shows the number of    */
                    411: /* cards in the file.                                                */
                    412: /*                                                                   */
                    413: /*    Same for PM and Windows                                        */
                    414: /*********************************************************************/
                    415: 
                    416: DLGRET   far PASCAL fnAbout(hwnd, msg, wParam, lParam)
                    417: HWND hwnd;
                    418: unsigned msg;
                    419: WINWORD wParam;
                    420: DWORD lParam;
                    421:     {
                    422:     char buf[40];
                    423:     int len;
                    424:     int id;
                    425: 
                    426:     switch (msg)
                    427:     if (msg == WM_INITDIALOG)
                    428:        {
                    429:        case WM_INITDIALOG:
                    430:            len = IntegerToAscii(cCards, buf);
                    431:            if (cCards == 1)
                    432:                id = IDS_CARD;
                    433:            else
                    434:                id = IDS_CARDS;
                    435:            LoadString(hCardfileInstance, id, (LPSTR)(buf+len), 40-len);
                    436:            SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)buf);
                    437:            return(TRUE);
                    438: 
                    439:        case WM_COMMAND:
                    440: #ifndef IN_WINDOWS
                    441:     /* In PM, treat WM_CONTROL message the same as WM_COMMAND */
                    442:     /* after moving Code from wParam to lParam */
                    443: 
                    444:        case WM_CONTROL:
                    445:            lParam = wParam;
                    446: #endif
                    447:            if (LOUSHORT(wParam) == IDOK)
                    448:                {
                    449:                EndDialog(hwnd, NULL);
                    450:                DIALOGDEFAULT( hwnd, msg, wParam, lParam );
                    451:                }
                    452:            break;
                    453:        default:
                    454:            DIALOGDEFAULT( hwnd, msg, wParam, lParam );
                    455:        }
                    456:     return(0L);
                    457:     }
                    458: 
                    459: /*********************************************************************/
                    460: /* IntegerToAscii -                                                  */
                    461: /*    Convert an int to ascii                                        */
                    462: /*                                                                   */
                    463: /*    Same for PM and Windows                                        */
                    464: /*********************************************************************/
                    465: 
                    466: int IntegerToAscii(n, psz)
                    467: unsigned n;
                    468: char *psz;
                    469:     {
                    470:     char *pch = psz;
                    471:     char ch;
                    472:     int len;
                    473: 
                    474:     do
                    475:         {
                    476:         *pch++ = (char) (n % 10 + '0');
                    477:         n /= 10;
                    478:         }
                    479:     while (n > 0);
                    480: 
                    481:     len = pch - psz;
                    482:     *pch-- = '\0';
                    483:     /* reverse the digits */
                    484: 
                    485:     while (psz < pch)
                    486:         {
                    487:         ch = *psz;
                    488:         *psz++ = *pch;
                    489:         *pch-- = ch;
                    490:         }
                    491:     return(len);
                    492:     }
                    493: 
                    494: /*********************************************************************/
                    495: /* DlgProc -                                                         */
                    496: /*    Generic dialog proc                                            */
                    497: /*                                                                   */
                    498: /*    Slightly different for PM and Windows                          */
                    499: /*********************************************************************/
                    500: 
                    501: DLGRET   far PASCAL DlgProc(hDB, message, wParam, lParam)
                    502: HWND hDB;
                    503: unsigned message;
                    504: WINWORD wParam;
                    505: DWORD lParam;
                    506:     {
                    507:     char *pResultBuf;
                    508:     char *pchInit;
                    509:     int len;
                    510: 
                    511:     switch (message)
                    512:         {
                    513:         case WM_INITDIALOG:
                    514:             /* initialize edit item based on which dialog it is */
                    515:             switch(DBcmd)
                    516:                 {
                    517:                 case DTHEADER:
                    518:                     pchInit = CurCardHead.line;
                    519:                     break;
                    520:                 case DTFIND:
                    521:                     pchInit = CurIFind;
                    522:                     break;
                    523:                 default:
                    524:                     pchInit = "";
                    525:                 }
                    526:             SetDlgItemText(hDB, IDD_EDIT, (LPSTR)pchInit);
                    527:             SetFocus(GetDlgItem(hDB, IDD_EDIT));
                    528:             return(TRUE);
                    529:             break;
                    530: 
                    531:         case WM_COMMAND:
                    532: #ifndef  IN_WINDOWS
                    533:         /* In PM, treat WM_CONTROL message the same as WM_COMMAND */
                    534:         /* message after moving Code from wParam to lParam */
                    535: 
                    536:         case WM_CONTROL:
                    537:             lParam = wParam;
                    538: #endif
                    539:             /* all these get a single string */
                    540:             pResultBuf = NULL;
                    541:             switch (LOUSHORT(wParam))
                    542:                 {
                    543:                 case IDOK:
                    544:                     /* allocate buffer, read text and pass it back */
                    545:                     len = GetWindowTextLength(GetDlgItem(hDB, IDD_EDIT));                  
                    546:                     if (len || DBcmd == DTHEADER || DBcmd == DTADD)
                    547:                         if(pResultBuf = (char *)LocalAlloc(LPTR, ++len))
                    548:                             GetDlgItemText(hDB, IDD_EDIT, 
                    549:                                            (LPSTR)pResultBuf, len);
                    550:                     break;
                    551:                 case IDCANCEL:
                    552:                     break;
                    553:                 default:
                    554:                     return(FALSE);
                    555:                 }
                    556:             /* return pointer to buffer */
                    557:             EndDialog(hDB, (int)pResultBuf);
                    558:             return(0L);
                    559:             break;
                    560:         default:
                    561:             DIALOGDEFAULT( hDB, message, wParam, lParam );
                    562:         }
                    563:     }
                    564: 
                    565: 

unix.superglobalmegacorp.com

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