home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 06 / dflat3 / memopad.c < prev    next >
Text File  |  1991-05-19  |  8KB  |  356 lines

  1. /* --------------- memopad.c ----------- */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys\types.h>
  7. #include <sys\stat.h>
  8. #include <dos.h>
  9. #ifndef MSC
  10. #include <dir.h>
  11. #endif
  12. #include "dflat.h"
  13.  
  14. extern DBOX FileOpen;
  15.  
  16. static char Untitled[] = "Untitled";
  17. static int wndpos = 0;
  18. static int DocumentCount = 0;
  19.  
  20. static int MemoPadProc(WINDOW, MESSAGE, PARAM, PARAM);
  21. static void NewFile(WINDOW);
  22. #ifdef INCLUDE_DIALOG_BOXES
  23. static void SelectFile(WINDOW);
  24. #endif
  25. static void PadWindow(WINDOW, char *);
  26. static void OpenPadWindow(WINDOW, char *);
  27. static void LoadFile(WINDOW, int);
  28. static void PrintPad(WINDOW);
  29. static void SaveFile(WINDOW, int);
  30. static int EditorProc(WINDOW, MESSAGE, PARAM, PARAM);
  31. static char *NameComponent(char *);
  32.  
  33. void main(int argc, char *argv[])
  34. {
  35.     int x,y;
  36.     WINDOW wnd;
  37.     init_messages();
  38.     SendMessage(NULLWND, CURRENT_KEYBOARD_CURSOR, LPARAM(&x), LPARAM(&y));
  39.     SendMessage(NULLWND, HIDE_CURSOR, 0, 0);
  40.  
  41.     wnd = CreateWindow(APPLICATION,
  42.                         "D-Flat MemoPad " VERSION,
  43.                         0, 0, -1, -1,
  44.                         MainMenu,
  45.                         NULL,
  46.                         MemoPadProc,
  47.                         MOVEABLE | SIZEABLE
  48. #ifdef INCLUDE_MULTIDOCS
  49.                                  | HASBORDER
  50. #endif
  51.                         );
  52.  
  53.     SendMessage(wnd, SETFOCUS, TRUE, 0);
  54.  
  55.     DeactivateCommand(MainMenu, ID_SAVE);
  56.     DeactivateCommand(MainMenu, ID_SAVEAS);
  57.     DeactivateCommand(MainMenu, ID_PRINT);
  58.  
  59. #ifdef INCLUDE_MULTIDOCS
  60.     while (argc > 1)    {
  61.         PadWindow(wnd, argv[1]);
  62.         --argc;
  63.         argv++;
  64.     }
  65. #else
  66.     if (argc > 1)
  67.         PadWindow(wnd, argv[1]);
  68. #endif
  69.  
  70.     while (dispatch_message())
  71.         ;
  72.  
  73.     SendMessage(NULLWND, KEYBOARD_CURSOR, x, y);
  74.     SendMessage(NULLWND, SHOW_CURSOR, 0, 0);
  75. }
  76.  
  77. static void PadWindow(WINDOW wnd, char *FileName)
  78. {
  79.     int ax, criterr = 1;
  80.     struct ffblk ff;
  81.     char path[64];
  82.     char *cp;
  83.     CreatePath(path, FileName, FALSE, FALSE);
  84.     cp = path+strlen(path);
  85.     CreatePath(path, FileName, TRUE, FALSE);
  86.  
  87.     while (criterr == 1)    {
  88.         ax = findfirst(path, &ff, 0);
  89.         criterr = TestCriticalError();
  90.     }
  91.     while (ax == 0 && !criterr)    {
  92.         strcpy(cp, ff.ff_name);
  93.         OpenPadWindow(wnd, path);
  94.         ax = findnext(&ff);
  95.     }
  96. }
  97.  
  98. static int MemoPadProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  99. {
  100.     switch (msg)    {
  101.         case COMMAND:
  102.             switch ((int)p1)    {
  103.                 case ID_NEW:
  104.                     NewFile(wnd);
  105.                     break;
  106. #ifdef INCLUDE_DIALOG_BOXES
  107.                 case ID_OPEN:
  108.                     SelectFile(wnd);
  109.                     break;
  110. #endif
  111.                 case ID_SAVE:
  112.                     SaveFile(inFocus, FALSE);
  113.                     break;
  114. #ifdef INCLUDE_DIALOG_BOXES
  115.                 case ID_SAVEAS:
  116.                     SaveFile(inFocus, TRUE);
  117.                     break;
  118. #endif
  119.                 case ID_PRINT:
  120.                     PrintPad(inFocus);
  121.                     break;
  122.                 case ID_ABOUT:
  123.                     MessageBox(
  124.                         "About D-Flat and the MemoPad",
  125.                         "D-Flat is an operating environment\n"
  126.                         "that implements the SAA/CUA user\n"
  127.                         "interface in a C language library.\n"
  128.                         "    ------------------------ \n"
  129.                         "The MemoPad is a multiple document\n"
  130.                         "text editor that demonstrates the\n"
  131.                         "use of D-Flat.");
  132.                     break;
  133.                 default:
  134.                     break;
  135.             }
  136.             break;
  137.         default:
  138.             break;
  139.     }
  140.     return DefaultWndProc(wnd, msg, p1, p2);
  141. }
  142.  
  143. /*
  144.  *  The New command. Open an empty editor window
  145.  */
  146. static void NewFile(WINDOW wnd)
  147. {
  148.     OpenPadWindow(wnd, Untitled);
  149. }
  150.  
  151. #ifdef INCLUDE_DIALOG_BOXES
  152. /*
  153.  *  The Open... command. Select a file 
  154.  */
  155. static void SelectFile(WINDOW wnd)
  156. {
  157.     char FileName[64];
  158.     if (DlgOpenFile("*.PAD", FileName))    {
  159. #ifdef INCLUDE_MULTIDOCS
  160.         /* --- see if the document is already in a window --- */
  161.         WINDOW wnd1 = GetFirstChild(wnd);
  162.         while (wnd1 != NULLWND)    {
  163.             if (stricmp(FileName, wnd1->extension) == 0)    {
  164.                 SendMessage(wnd1, SETFOCUS, TRUE, 0);
  165.                 SendMessage(wnd1, RESTORE, 0, 0);
  166.                 return;
  167.             }
  168.             wnd1 = GetNextChild(wnd);
  169.         }
  170. #endif
  171.         OpenPadWindow(wnd, FileName);
  172.     }
  173. }
  174. #endif
  175.  
  176. /*
  177.  *  open a document window and load a file
  178.  */
  179. static void OpenPadWindow(WINDOW wnd, char *FileName)
  180. {
  181.     static WINDOW wnd1 = NULLWND;
  182.     struct stat sb;
  183.     char *Fname = FileName;
  184.     char *ermsg;
  185.     if (strcmp(FileName, Untitled))    {
  186.         if (stat(FileName, &sb))    {
  187.             if ((ermsg = malloc(strlen(FileName)+20)) != NULL)    {
  188.                 strcpy(ermsg, "No such file as\n");
  189.                 strcat(ermsg, FileName);
  190.                 ErrorMessage(ermsg);
  191.                 free(ermsg);
  192.             }
  193.             return;
  194.         }
  195.         Fname = NameComponent(FileName);
  196.     }
  197. #ifdef INCLUDE_MULTIDOCS
  198.     wndpos += 2;
  199.     if (wndpos == 20)
  200.         wndpos = 2;
  201. #else
  202.     if (wnd1 != NULLWND)
  203.         SendMessage(wnd1, CLOSE_WINDOW, 0, 0);
  204. #endif
  205.  
  206.     wnd1 = CreateWindow(EDITBOX,
  207.                 Fname,
  208. #ifdef INCLUDE_MULTIDOCS
  209.                 (wndpos-1)*2, wndpos, 10, 40,
  210. #else
  211.                 GetClientLeft(wnd),GetClientTop(wnd),
  212.                 ClientHeight(wnd),ClientWidth(wnd),
  213. #endif
  214.                 NULL, wnd, EditorProc,
  215. #ifdef INCLUDE_MULTIDOCS
  216.                 SHADOW     |
  217.                 MINMAXBOX  |
  218.                 CONTROLBOX |
  219. #endif
  220.                 VSCROLLBAR |
  221.                 HSCROLLBAR |
  222.                 MOVEABLE   |
  223.                 HASBORDER  |
  224.                 SIZEABLE   |
  225.                 MULTILINE
  226.     );
  227.     if (strcmp(FileName, Untitled))    {
  228.         if ((wnd1->extension = malloc(strlen(FileName)+1)) != NULL)    {
  229.             strcpy(wnd1->extension, FileName);
  230.             LoadFile(wnd1, (int) sb.st_size);
  231.         }
  232.     }
  233.     SendMessage(wnd1, SETFOCUS, TRUE, 0);
  234. }
  235.  
  236. /*
  237.  *  Load the notepad file into the editor text buffer
  238.  */
  239. static void LoadFile(WINDOW wnd, int tLen)
  240. {
  241.     char *Buf;
  242.     FILE *fp;
  243.  
  244.     if ((Buf = malloc(tLen+1)) != NULL)    {
  245.         if ((fp = fopen(wnd->extension, "rt")) != NULL)    {
  246.             memset (Buf, 0, tLen+1);
  247.             fread(Buf, tLen, 1, fp);
  248.             SendMessage(wnd, SETTEXT, LPARAM(Buf), 0);
  249.             fclose(fp);
  250.         }
  251.         free(Buf);
  252.     }
  253. }
  254.  
  255. /*
  256.  *  print the current notepad
  257.  */
  258. static void PrintPad(WINDOW wnd)
  259. {
  260.     char *text;
  261.  
  262.     /* ---------- print the file name ---------- */
  263.     fputs("\r\n", stdprn);
  264.     fputs(GetTitle(wnd), stdprn);
  265.     fputs(":\r\n\n", stdprn);
  266.  
  267.     /* ---- get the address of the editor text ----- */
  268.     text = GetText(wnd);
  269.  
  270.     /* ------- print the notepad text --------- */
  271.     while (*text)    {
  272.         if (*text == '\n')
  273.             fputc('\r', stdprn);
  274.         fputc(*text++, stdprn);
  275.     }
  276.  
  277.     /* ------- follow with a form feed? --------- */
  278.     if (YesNoBox("Form Feed?"))
  279.         fputc('\f', stdprn);
  280. }
  281.  
  282. static void SaveFile(WINDOW wnd, int Saveas)
  283. {
  284.     FILE *fp;
  285. #ifdef INCLUDE_DIALOG_BOXES
  286.     if (wnd->extension == NULL || Saveas)    {
  287.         char FileName[64];
  288.         if (DlgSaveAs(FileName))    {
  289.             if (wnd->extension != NULL)
  290.                 free(wnd->extension);
  291.             if ((wnd->extension = malloc(strlen(FileName)+1)) != NULL)    {
  292.                 strcpy(wnd->extension, FileName);
  293.                 AddTitle(wnd, NameComponent(FileName));
  294.                 SendMessage(wnd, BORDER, 0, 0);
  295.             }
  296.         }
  297.     }
  298. #endif
  299.     if (wnd->extension != NULL)    {
  300.         if ((fp = fopen(wnd->extension, "wt")) != NULL)    {
  301.             fwrite(GetText(wnd), strlen(GetText(wnd)), 1, fp);
  302.             fclose(fp);
  303.             wnd->TextChanged = FALSE;
  304.         }
  305.     }
  306. }
  307.  
  308. static int EditorProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  309. {
  310.     switch (msg)    {
  311.         case CREATE_WINDOW:
  312.             if (DocumentCount == 0)    {
  313.                 ActivateCommand(MainMenu, ID_SAVE);
  314.                 ActivateCommand(MainMenu, ID_SAVEAS);
  315.                 ActivateCommand(MainMenu, ID_PRINT);
  316.             }
  317.             DocumentCount++;
  318.             break;
  319.         case CLOSE_WINDOW:
  320.             if (wnd->TextChanged)    {
  321.                 char *cp = malloc(25+strlen(GetTitle(wnd)));
  322.                 if (cp != NULL)    {
  323.                     strcpy(cp, GetTitle(wnd));
  324.                     strcat(cp, "\nText changed. Save it?");
  325.                     if (YesNoBox(cp))
  326.                         SendMessage(GetParent(wnd), COMMAND, ID_SAVE, 0);
  327.                     free(cp);
  328.                 }
  329.             }
  330.             if (--DocumentCount == 0)    {
  331.                 DeactivateCommand(MainMenu, ID_SAVE);
  332.                 DeactivateCommand(MainMenu, ID_SAVEAS);
  333.                 DeactivateCommand(MainMenu, ID_PRINT);
  334.             }
  335.             wndpos = 0;
  336.             if (wnd->extension != NULL)    {
  337.                 free(wnd->extension);
  338.                 wnd->extension = NULL;
  339.             }
  340.             break;
  341.         default:
  342.             break;
  343.     }
  344.     return DefaultWndProc(wnd, msg, p1, p2);
  345. }
  346.  
  347. static char *NameComponent(char *FileName)
  348. {
  349.     char *Fname;
  350.     if ((Fname = strrchr(FileName, '\\')) == NULL)
  351.         if ((Fname = strrchr(FileName, ':')) == NULL)
  352.             Fname = FileName-1;
  353.     return Fname + 1;
  354. }
  355.  
  356.