home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / RWCDEMO.ZIP / RWCDLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  8.7 KB  |  318 lines

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