home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / dtx9302 / icon / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-16  |  8.6 KB  |  367 lines

  1. /* ====================================================== */
  2. /*                         FILE.C                         */
  3. /*  Hilfsroutinen für den Dateizugriff und Dialogbox-     */
  4. /*  funktionen zum Öffnen und Speichern von  Dateien      */
  5. /*          (C) 1993 Kay Glahn & DMV-Verlag               */
  6. /* (Teile dieser Routinen wurden aus Beispielen des       */
  7. /*   Microsoft Windows SDK-Handbuches übernommen.)        */
  8. /* ====================================================== */
  9.  
  10. #include <windows.h>
  11. #include <string.h>
  12. #include "icocon.h"
  13.  
  14.  
  15. char             DefExt[]=".ico",
  16.                  OpenName[128],
  17.                  DefPath[128];
  18.  
  19. BOOL             bSaveEnabled = FALSE;
  20.  
  21. extern char      FileName[2][128],
  22.                  DefSpec[2][13],
  23.                  str[255];
  24.  
  25. extern OFSTRUCT  OfStruct;
  26.  
  27. extern BOOL      change[2];
  28.  
  29. extern HANDLE    hInst;
  30.  
  31. extern WORD      WinOS2[2];
  32.  
  33. extern int       CurrFile;
  34.  
  35. HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
  36. HWND hDlg;
  37. unsigned message;
  38. WORD wParam;
  39. LONG lParam;
  40. {
  41.  
  42.   HANDLE hFile=1;
  43.  
  44.   switch (message)
  45.   {
  46.     case WM_COMMAND:
  47.       switch (wParam)
  48.       {
  49.         case IDC_LISTBOX:
  50.           switch (HIWORD(lParam))
  51.           {
  52.             case LBN_SELCHANGE:
  53.               if (DlgDirSelect(hDlg, str, IDC_LISTBOX))
  54.                 strcat(str, DefSpec[CurrFile]);
  55.               SetDlgItemText(hDlg, IDC_EDIT, str);
  56.               SendDlgItemMessage(hDlg,
  57.                                  IDC_EDIT,
  58.                                  EM_SETSEL,
  59.                                  NULL,
  60.                                  MAKELONG(0, 0x7fff));
  61.               break;
  62.             case LBN_DBLCLK:
  63.               goto openfile;
  64.           }
  65.           return (TRUE);
  66.         case IDOK:
  67. openfile:
  68.           GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  69.  
  70.           if (strchr(OpenName, '*') || strchr(OpenName, '?'))
  71.           {
  72.             SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec[CurrFile],(LPSTR) OpenName);
  73.  
  74.             if (str[0])
  75.               strcpy(DefPath, str);
  76.  
  77.             ChangeDefExt(DefExt, DefSpec[CurrFile]);
  78.             UpdateListBox(hDlg);
  79.             return (TRUE);
  80.           }
  81.  
  82.           if (!OpenName[0])
  83.           {
  84.             MessageBox(hDlg, "No filename specified.", NULL, MB_OK | MB_ICONHAND);
  85.             return (TRUE);
  86.           }
  87.  
  88.           AddExt(OpenName, DefExt);
  89.  
  90.           if ((hFile = OpenFile(OpenName,(LPOFSTRUCT) &OfStruct, OF_READ))<0)
  91.           {
  92.             wsprintf((LPSTR)str,"Fehler %d beim Öffnen von %s.",OfStruct.nErrCode, (LPSTR)OpenName);
  93.             MessageBox(hDlg,str,NULL,MB_OK |MB_ICONHAND);
  94.           }
  95.  
  96.           strcpy(FileName[CurrFile], OpenName);
  97.  
  98.           EndDialog(hDlg, hFile);
  99.           return (TRUE);
  100.         case IDCANCEL:
  101.           EndDialog(hDlg, NULL);
  102.           return (FALSE);
  103.       }
  104.       break;
  105.     case WM_INITDIALOG:
  106.       CenterWnd(hDlg, NULL);
  107.       UpdateListBox(hDlg);
  108.       SetDlgItemText(hDlg, IDC_EDIT, DefSpec[CurrFile]);
  109.  
  110.       SendDlgItemMessage(hDlg,
  111.                          IDC_EDIT,
  112.                          EM_SETSEL,
  113.                          NULL,
  114.                          MAKELONG(0, 0x7fff));
  115.  
  116.       SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  117.  
  118.       return (FALSE);
  119.   }
  120.  
  121.   return FALSE;
  122. }
  123.  
  124.  
  125. void UpdateListBox(hDlg)
  126. HWND hDlg;
  127. {
  128.   strcpy(str, DefPath);
  129.   strcat(str, DefSpec[CurrFile]);
  130.   DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  131.  
  132.   if (!strchr (DefPath, ':'))
  133.     DlgDirList(hDlg, DefSpec[CurrFile], IDC_LISTBOX, IDC_PATH, 0x4010);
  134.  
  135.   if (strstr (DefPath, ".."))
  136.     DefPath[0] = '\0';
  137.  
  138.   SetDlgItemText(hDlg, IDC_EDIT, DefSpec[CurrFile]);
  139. }
  140.  
  141.  
  142. void ChangeDefExt(Ext, Name)
  143. PSTR Ext, Name;
  144. {
  145.  
  146.   PSTR pTptr;
  147.  
  148.   pTptr = Name;
  149.  
  150.   while (*pTptr && *pTptr != '.')
  151.     pTptr++;
  152.  
  153.   if (*pTptr)
  154.     if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  155.       strcpy(Ext, pTptr);
  156. }
  157.  
  158.  
  159. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  160. HWND hDlg;
  161. LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  162. {
  163.  
  164.   LPSTR lpTmp;
  165.   char  cTmp;
  166.  
  167.   lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  168.  
  169.   while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  170.     lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  171.  
  172.   if (*lpTmp != ':' && *lpTmp != '\\')
  173.   {
  174.     lstrcpy(lpDestFileName, lpSrcFileName);
  175.     lpDestPath[0] = 0;
  176.     return;
  177.   }
  178.  
  179.   lstrcpy(lpDestFileName, lpTmp + 1);
  180.   cTmp = *(lpTmp + 1);
  181.   lstrcpy(lpDestPath, lpSrcFileName);
  182.   *(lpTmp + 1) = cTmp;
  183.   lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  184. }
  185.  
  186.  
  187. void AddExt(Name, Ext)
  188. PSTR Name, Ext;
  189. {
  190.  
  191.   PSTR pTptr;
  192.  
  193.   pTptr = Name;
  194.  
  195.   while (*pTptr && *pTptr != '.')
  196.     pTptr++;
  197.  
  198.   if (*pTptr != '.')
  199.     strcat(Name, Ext);
  200. }
  201.  
  202.  
  203. int FAR PASCAL SaveAsDlg(hDlg, message, wParam, lParam)
  204. HWND hDlg;
  205. unsigned message;
  206. WORD wParam;
  207. LONG lParam;
  208. {
  209.  
  210.   char TempName[128];
  211.  
  212.   switch (message)
  213.   {
  214.     case WM_INITDIALOG:
  215.       CenterWnd(hDlg, NULL);
  216.  
  217.       CheckDlgButton(hDlg, (WinOS2[CurrFile]==1)?IDC_RADIO1:IDC_RADIO2, 1);
  218.  
  219.       if (!FileName[CurrFile][0])
  220.         bSaveEnabled = FALSE;
  221.       else
  222.       {
  223.         bSaveEnabled = TRUE;
  224.         DlgDirList(hDlg, DefPath, NULL, IDC_PATH, 0x4010);
  225.         SetDlgItemText(hDlg, IDC_EDIT, FileName[CurrFile]);
  226.         SendDlgItemMessage(hDlg, IDC_EDIT, EM_SETSEL, 0, MAKELONG(0, 0x7fff));
  227.       }
  228.  
  229.       EnableWindow(GetDlgItem(hDlg, IDOK), bSaveEnabled);
  230.  
  231.       SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  232.       return (FALSE);
  233.     case WM_COMMAND:
  234.       switch (wParam)
  235.       {
  236.         case IDC_EDIT:
  237.           if (HIWORD(lParam) == EN_CHANGE && !bSaveEnabled)
  238.             EnableWindow(GetDlgItem(hDlg, IDOK), bSaveEnabled = TRUE);
  239.  
  240.           return (TRUE);
  241.         case IDOK:
  242.           GetDlgItemText(hDlg, IDC_EDIT, TempName, 128);
  243.  
  244.           if (CheckFileName(hDlg, FileName[CurrFile], TempName))
  245.           {
  246.             SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec[CurrFile], (LPSTR) FileName[CurrFile]);
  247.  
  248.             WinOS2[CurrFile]=IsDlgButtonChecked(hDlg, IDC_RADIO1);
  249.  
  250.             if (str[0])
  251.               strcpy(DefPath, str);
  252.  
  253.             EndDialog(hDlg, IDOK);
  254.           }
  255.           return (TRUE);
  256.         case IDCANCEL:
  257.           EndDialog(hDlg, IDCANCEL);
  258.           return (TRUE);
  259.       }
  260.       break;
  261.   }
  262.  
  263.   return (FALSE);
  264. }
  265.  
  266.  
  267. BOOL CheckFileName(hWnd, pDest, pSrc)
  268. HWND hWnd;
  269. PSTR pDest, pSrc;
  270. {
  271.  
  272.   PSTR pTmp;
  273.  
  274.   if (!pSrc[0])
  275.     return (FALSE);
  276.  
  277.   pTmp = pSrc;
  278.  
  279.   while (*pTmp)
  280.   {
  281.     switch (*pTmp++)
  282.     {
  283.       case '*':
  284.       case '?':
  285.         MessageBox(hWnd, "Wildcards nicht erlaubt !", NULL, MB_OK | MB_ICONEXCLAMATION);
  286.         return (FALSE);
  287.     }
  288.   }
  289.  
  290.   AddExt(pSrc, DefExt);
  291.  
  292.   if (OpenFile(pSrc, (LPOFSTRUCT) &OfStruct, OF_EXIST) >= 0)
  293.   {
  294.     wsprintf((LPSTR)str, "Bestehende Datei %s ersetzen ?", (LPSTR)pSrc);
  295.  
  296.     if (MessageBox(hWnd, str, "Icon Converter",MB_OKCANCEL | MB_ICONHAND) == IDCANCEL)
  297.       return (FALSE);
  298.   }
  299.  
  300.   strcpy(pDest, pSrc);
  301.   return (TRUE);
  302. }
  303.  
  304.  
  305. BOOL QuerySaveFile(hWnd, hXORBmpMem, hANDBmpMem, hPalMem, hResDirMem, Quantity, FileName, change)
  306. HWND hWnd;
  307. HANDLE hXORBmpMem;
  308. HANDLE hANDBmpMem;
  309. HANDLE hPalMem;
  310. HANDLE hResDirMem;
  311. int Quantity;
  312. LPSTR FileName;
  313. BOOL FAR * change;
  314. {
  315.   BOOL Result;
  316.   int Response;
  317.   FARPROC lpSaveAsDlg;
  318.   HANDLE hFile;
  319.  
  320.   if(Quantity>0)
  321.   {
  322.     if (*change)
  323.     {
  324.       wsprintf((LPSTR)str, "─nderungen speichern :  %s", FileName);
  325.       Response = MessageBox(hWnd, str,"Icon Converter",  MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  326.  
  327.       if (Response == IDYES)
  328.       {
  329.  
  330.         lpSaveAsDlg = MakeProcInstance(SaveAsDlg, hInst);
  331.         Response = DialogBox(hInst, "SaveAs",hWnd, lpSaveAsDlg);
  332.         FreeProcInstance(lpSaveAsDlg);
  333.  
  334.         if (Response != IDOK) return (FALSE);
  335.  
  336.         if ((hFile = OpenFile(FileName,(LPOFSTRUCT) &OfStruct, OF_CREATE)) >= 0)
  337.         {
  338.           if(WinOS2[CurrFile])
  339.             Result=SaveIconFile(&hXORBmpMem,&hANDBmpMem,&hPalMem,&hResDirMem,hFile, hWnd, Quantity);
  340.           else
  341.             Result=SaveIconFile2(&hXORBmpMem,&hANDBmpMem,&hPalMem,&hResDirMem,hFile, hWnd, Quantity);
  342.           _lclose(hFile);
  343.  
  344.           if (Result) *change = FALSE;
  345.         }
  346.       }
  347.       else if (Response == IDCANCEL)
  348.         return (FALSE);
  349.     }
  350.   }
  351.   return (TRUE);
  352. }
  353.  
  354. WORD CheckFileType(hFile)
  355. int hFile;
  356. {
  357.   WORD ID;
  358.   _llseek(hFile, 0, 0);
  359.   _lread(hFile, (LPSTR)&ID, 2);
  360.   if(ID==0x4142) return(0);
  361.   if(ID==0) return(1);
  362.   return(-1);
  363. }
  364.  
  365. /* ====================================================== */
  366. /*                    Ende von FILE.C                     */
  367.