home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / wksinst / rwcdemo.pak / RWCDLG.CPP < prev    next >
C/C++ Source or Header  |  1991-09-09  |  7KB  |  320 lines

  1. // (C) Copyright 1991 by Borland International
  2.  
  3. #include <windows.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <dir.h>
  7. #if defined( BWCC)
  8. #include <bwcc.h>
  9. #else
  10. #define IDHELP 210
  11. #endif
  12. #include "rwcdemo.h"
  13. #include "rwcdlg.h"
  14.  
  15. int FAR PASCAL _export dlgProc(HWND hDlg, WORD msg, WORD wParam, LONG lParam)
  16. {
  17.     static TDialog *TheDialog;
  18.  
  19.     switch(msg)
  20.     {
  21.         case WM_INITDIALOG:
  22.             TheDialog = (TDialog*)lParam;
  23.             TheDialog->hWindow = hDlg;
  24.             TheDialog->setupDialog();
  25.             return 1;
  26.         break;
  27.         case WM_COMMAND:
  28.             return TheDialog->doCommands(wParam, lParam);
  29.         break;
  30.     }
  31.     return 0;
  32. }
  33.  
  34. TDialog::TDialog(int DlgName)
  35. {
  36.     dialogFunc = MakeProcInstance((FARPROC)::dlgProc, WinApp::hInstance);
  37.     name = MAKEINTRESOURCE(DlgName);
  38. }
  39.  
  40. TDialog::~TDialog()
  41. {
  42.     FreeProcInstance(dialogFunc);
  43. }
  44.  
  45. int TDialog::doCommands(WORD wParam, LONG)
  46. {
  47.     switch(wParam)
  48.     {
  49.         case IDOK:
  50.             retrieveInfo();
  51.             if (canClose())
  52.                 EndDialog(hWindow, wParam);
  53.             return 1;
  54.         case IDCANCEL:
  55.             EndDialog(hWindow, wParam);
  56.             return 1;
  57.  
  58.         case IDHELP:
  59. #if defined (BWCC)
  60.         BWCCMessageBox( hWindow,
  61.           "Call WinHelp here",
  62.           "Help",
  63.           MB_OK | MB_ICONINFORMATION);
  64. #else
  65.         MessageBox( hWindow,
  66.           "Call WinHelp here",
  67.           "Help",
  68.           MB_OK | MB_ICONINFORMATION);
  69. #endif
  70.         return 0;
  71.  
  72.     }
  73.     return 1;
  74. }
  75.  
  76. #define ScribbleExtension ".SCR"
  77. #define GraphExtension ".GRP"
  78.  
  79. TFileOpen::TFileOpen(int *aType, char *aFilePath) :
  80.     TDialog(dlg_Open)
  81. {
  82.     filePath = strdup(aFilePath);
  83.     fileType = aType;
  84. }
  85.  
  86. int TFileOpen::canClose()
  87. {
  88.     WORD pathLen;
  89.  
  90.   GetDlgItemText(hWindow, id_FName, pathName, fsPathName + 1);
  91.   fnmerge(pathName,"","",pathName,"");
  92.   pathLen = strlen(pathName);
  93.   if ((pathName[pathLen - 1] == '\\') || hasWildCards(pathName) ||
  94.     (GetFocus() == GetDlgItem(hWindow, id_DList)))
  95.   {
  96.     if (pathName[pathLen - 1] == '\\')
  97.       strncat(pathName, fileSpec, fsPathName);
  98.     if (!updateListBoxes())
  99.     {
  100.       MessageBeep(0);
  101.       selectFileName();
  102.     }
  103.     return 0;
  104.   }
  105.   strncat(strncat(pathName, "\\", fsPathName), fileSpec, fsPathName);
  106.   if (updateListBoxes())
  107.     return 0;
  108.   pathName[pathLen] = '\0';
  109.   if (getExtension(pathName)[0] == '\0')
  110.     strncat(pathName, extension, fsPathName);
  111.   AnsiLower(strcpy(filePath, pathName));
  112.   updateButtons();
  113.   if (IsDlgButtonChecked(hWindow, id_Text) == 1)
  114.     *fileType = FileWindow;
  115.   else
  116.   if (IsDlgButtonChecked(hWindow, id_Scribble) == 1)
  117.     *fileType = ScribbleWindow;
  118.   else
  119.   if (IsDlgButtonChecked(hWindow, id_Graph) == 1)
  120.     *fileType = GraphWindow;
  121.   else
  122.     return 0;
  123.   return 1;
  124. }
  125.  
  126. char *TFileOpen::getFileFirst(char *aFilePath)
  127. {
  128.     static char *P;
  129.     char *Q;
  130.  
  131.     P = getFileName(aFilePath);
  132.     Q = strchr(P, '.');
  133.     if (Q)
  134.         *Q = '\0';
  135.     return P;
  136. }
  137.  
  138. char *TFileOpen::getExtension(char *aFilePath)
  139. {
  140.   static char *P;
  141.   P = strchr(getFileName(aFilePath), '.');
  142.   if (!P)
  143.     return &(aFilePath[strlen(aFilePath)]);
  144.   else
  145.     return P;
  146. }
  147.  
  148. char *TFileOpen::getFileName(char *aFilePath)
  149. {
  150.     static char *P;
  151.     P = strchr(strrev(aFilePath), '\\');
  152.     if (!P)
  153.         P = strchr(aFilePath, ':');
  154.     strrev(aFilePath);
  155.     if (!P)
  156.         return aFilePath;
  157.     else
  158.         return (aFilePath + (strlen(aFilePath) - (P - aFilePath)));
  159. }
  160.  
  161. void TFileOpen::setupDialog()
  162. {
  163.   SendDlgItemMessage(hWindow, id_FName, EM_LIMITTEXT, fsPathName, 0);
  164.   strncpy(pathName, filePath, fsPathName);
  165.   strncpy(extension, getExtension(pathName), fsExtension);
  166.   if (hasWildCards(extension))
  167.     extension[0] = '\0';
  168.   if (!updateListBoxes())
  169.   {
  170.     strcpy(pathName, "*.*");
  171.     updateListBoxes();
  172.   }
  173.   selectFileName();
  174. }
  175.  
  176. void TFileOpen::selectFileName()
  177. {
  178.   SendDlgItemMessage(hWindow, id_FName, EM_SETSEL, 0, 0x7FFF0000L);
  179.   SetFocus(GetDlgItem(hWindow, id_FName));
  180. }
  181.  
  182. void TFileOpen::updateFileName()
  183. {
  184.   SetDlgItemText(hWindow, id_FName, AnsiLower(pathName));
  185.   SendDlgItemMessage(hWindow, id_FName, EM_SETSEL, 0, 0x7FFF0000L);
  186.   updateButtons();
  187. }
  188.  
  189. void TFileOpen::updateButtons()
  190. {
  191.   char *P;
  192.   int WhichButton;
  193.  
  194.   P = getExtension(pathName);
  195.   if (P)
  196.   {
  197.     if (!stricmp(P, ScribbleExtension))
  198.       WhichButton = id_Scribble;
  199.     else
  200.     if (!stricmp(P, GraphExtension))
  201.       WhichButton = id_Graph;
  202.     else
  203.       WhichButton = id_Text;
  204.     SendDlgItemMessage(hWindow, id_Text, BM_SETCHECK, 0, 0);
  205.     SendDlgItemMessage(hWindow, id_Graph, BM_SETCHECK, 0, 0);
  206.     SendDlgItemMessage(hWindow, id_Scribble, BM_SETCHECK, 0, 0);
  207.     SendDlgItemMessage(hWindow, WhichButton, BM_SETCHECK, 1, 0);
  208.   }
  209. }
  210.  
  211. int TFileOpen::updateListBoxes()
  212. {
  213.   int Result;
  214.   char Path[fsPathName+1];
  215.  
  216.   if (GetDlgItem(hWindow, id_FList))
  217.   {
  218.     strcpy(Path, pathName);
  219.     Result = DlgDirList(hWindow, Path, id_FList, id_FPath, 0);
  220.     if (Result)
  221.         DlgDirList(hWindow, "*.*", id_DList, 0, 0x0C010);
  222.   }
  223.   else
  224.   {
  225.     strncpy(Path, pathName, getFileName(pathName) - pathName);
  226.     strncat(Path, "*.*", fsPathName);
  227.     Result = DlgDirList(hWindow, Path, id_DList, id_FPath, 0x0C010);
  228.   }
  229.  
  230.   if (Result)
  231.   {
  232.     strncpy(fileSpec, getFileName(pathName), fsFileSpec);
  233.     strcpy(pathName, fileSpec);
  234.     updateFileName();
  235.   }
  236.   return Result;
  237. }
  238.  
  239. int TFileOpen::doCommands(WORD wParam, LONG lParam)
  240. {
  241.     switch(wParam)
  242.     {
  243.         case id_FName:
  244.             if (HIWORD(lParam) == EN_CHANGE)
  245.                 EnableWindow(GetDlgItem(hWindow, IDOK),
  246.                     SendMessage(LOWORD(lParam), WM_GETTEXTLENGTH, 0, 0));
  247.             return 0;
  248.         case id_FList:
  249.             switch(HIWORD(lParam))
  250.             {
  251.                 case LBN_DBLCLK:
  252.                     PostMessage(hWindow, WM_COMMAND, IDOK, 0);
  253.                     //Fall through
  254.                 case LBN_SELCHANGE:
  255.                     DlgDirSelect(hWindow, pathName, id_FList);
  256.                     updateFileName();
  257.                     return 0;
  258.                 case LBN_KILLFOCUS:
  259.                     SendMessage(LOWORD(lParam), LB_SETCURSEL, -1, 0);
  260.                     return 0;
  261.             }
  262.         case id_DList:
  263.             switch(HIWORD(lParam))
  264.             {
  265.                 case LBN_SELCHANGE:
  266.                 case LBN_DBLCLK:
  267.                     DlgDirSelect(hWindow, pathName, id_DList);
  268.                     strcat(pathName, fileSpec);
  269.                     if (HIWORD(lParam) == LBN_DBLCLK)
  270.                         updateListBoxes();
  271.                     else
  272.                         updateFileName();
  273.                     return 0;
  274.                 case LBN_KILLFOCUS:
  275.                     SendMessage(LOWORD(lParam), LB_SETCURSEL, -1, 0);
  276.                     return 0;
  277.             }
  278.  
  279.         case id_Scribble:
  280.             strcat(strcpy(pathName,getFileFirst(pathName)), ScribbleExtension);
  281.             updateFileName();
  282.             return 0;
  283.         case id_Graph:
  284.             strcat(strcpy(pathName,getFileFirst(pathName)), GraphExtension);
  285.             updateFileName();
  286.             return 0;
  287.         case id_Text:
  288.             if (strcmp(getExtension(pathName),"."))
  289.             {
  290.                 strcat(strcpy(pathName,getFileFirst(pathName)), ".TXT");
  291.                 updateFileName();
  292.             }
  293.             return 0;
  294.         default:
  295.             return TDialog::doCommands(wParam, lParam);
  296.     }
  297.     return 1;
  298. }
  299.  
  300. void TFileNew::setupDialog()
  301. {
  302.     TDialog::setupDialog();
  303.     SetFocus(GetDlgItem(hWindow, id_Text));
  304.     SendDlgItemMessage(hWindow, id_Text, BM_SETCHECK, 1, 0);
  305. }
  306.  
  307. int TFileNew::canClose()
  308. {
  309.     if (IsDlgButtonChecked(hWindow, id_Text))
  310.         *fileType = FileWindow;
  311.     else
  312.     if (IsDlgButtonChecked(hWindow, id_Scribble))
  313.         *fileType = ScribbleWindow;
  314.     else
  315.     if (IsDlgButtonChecked(hWindow, id_Graphics))
  316.         *fileType = GraphWindow;
  317.     else
  318.         return 0;
  319.     return 1;
  320. }