home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 08 / dflat5 / notepad.c < prev    next >
Text File  |  1991-06-16  |  4KB  |  186 lines

  1. /* --------------- notepad.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. #include "dflat.h"
  10.  
  11. static char Untitled[] = "Untitled";
  12. static int DocumentCount = 0;
  13.  
  14. static int MemoPadProc(WINDOW, MESSAGE, PARAM, PARAM);
  15. static void OpenPadWindow(WINDOW, char *);
  16. static void LoadFile(WINDOW, int);
  17. static void SaveFile(WINDOW);
  18. static int EditorProc(WINDOW, MESSAGE, PARAM, PARAM);
  19. static char *NameComponent(char *);
  20.  
  21. char **Argv;
  22.  
  23. void main(int argc, char *argv[])
  24. {
  25.     WINDOW wnd;
  26.     init_messages();
  27.     Argv = argv;
  28.     wnd = CreateWindow(APPLICATION,
  29.                         "D-Flat MemoPad " VERSION,
  30.                         0, 0, -1, -1,
  31.                         MainMenu,
  32.                         NULL,
  33.                         MemoPadProc,
  34.                         MOVEABLE | SIZEABLE
  35.                         );
  36.  
  37.     SendMessage(wnd, SETFOCUS, TRUE, 0);
  38.     if (argc > 1)
  39.         OpenPadWindow(wnd, argv[1]);
  40.     while (dispatch_message())
  41.         ;
  42. }
  43.  
  44. static int MemoPadProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  45. {
  46.     switch (msg)    {
  47.         case COMMAND:
  48.             switch ((int)p1)    {
  49.                 case ID_NEW:
  50.                     OpenPadWindow(wnd, Untitled);
  51.                     return TRUE;
  52.                 case ID_SAVE:
  53.                     SaveFile(inFocus);
  54.                     return TRUE;
  55.                 case ID_ABOUT:
  56.                     MessageBox(
  57.                         "About D-Flat and the MemoPad",
  58.                         "D-Flat is an operating environment\n"
  59.                         "that implements the SAA/CUA user\n"
  60.                         "interface in a public domain\n"
  61.                         "C language library originally\n"
  62.                         "published in Dr. Dobb's Journal.\n"
  63.                         "    ------------------------ \n"
  64.                         "The NotePad is a single document\n"
  65.                         "text editor that demonstrates the\n"
  66.                         "use of D-Flat.");
  67.                     return TRUE;
  68.                 default:
  69.                     break;
  70.             }
  71.             break;
  72.         default:
  73.             break;
  74.     }
  75.     return DefaultWndProc(wnd, msg, p1, p2);
  76. }
  77.  
  78.  
  79. /*
  80.  *  open a document window and load a file
  81.  */
  82. static void OpenPadWindow(WINDOW wnd, char *FileName)
  83. {
  84.     static WINDOW wnd1 = NULLWND;
  85.     struct stat sb;
  86.     char *Fname = FileName;
  87.     char *ermsg;
  88.     if (strcmp(FileName, Untitled))    {
  89.         if (stat(FileName, &sb))    {
  90.             if ((ermsg = malloc(strlen(FileName)+20)) != NULL)    {
  91.                 strcpy(ermsg, "No such file as\n");
  92.                 strcat(ermsg, FileName);
  93.                 ErrorMessage(ermsg);
  94.                 free(ermsg);
  95.             }
  96.             return;
  97.         }
  98.         Fname = NameComponent(FileName);
  99.     }
  100.     if (wnd1 != NULLWND)
  101.         SendMessage(wnd1, CLOSE_WINDOW, 0, 0);
  102.     wnd1 = CreateWindow(EDITBOX,
  103.                 Fname,
  104.                 GetClientLeft(wnd),GetClientTop(wnd),
  105.                 ClientHeight(wnd),ClientWidth(wnd),
  106.                 NULL, wnd, EditorProc,
  107.                 VSCROLLBAR |
  108.                 HSCROLLBAR |
  109.                 MOVEABLE   |
  110.                 HASBORDER  |
  111.                 SIZEABLE   |
  112.                 MULTILINE
  113.     );
  114.     if (strcmp(FileName, Untitled))    {
  115.         if ((wnd1->extension = malloc(strlen(FileName)+1)) != NULL)    {
  116.             strcpy(wnd1->extension, FileName);
  117.             LoadFile(wnd1, (int) sb.st_size);
  118.         }
  119.     }
  120.     SendMessage(wnd1, SETFOCUS, TRUE, 0);
  121. }
  122.  
  123. /*
  124.  *  Load the notepad file into the editor text buffer
  125.  */
  126. static void LoadFile(WINDOW wnd, int tLen)
  127. {
  128.     char *Buf;
  129.     FILE *fp;
  130.  
  131.     if ((Buf = malloc(tLen+1)) != NULL)    {
  132.         if ((fp = fopen(wnd->extension, "rt")) != NULL)    {
  133.             memset (Buf, 0, tLen+1);
  134.             fread(Buf, tLen, 1, fp);
  135.             SendMessage(wnd, SETTEXT, (PARAM) Buf, 0);
  136.             fclose(fp);
  137.         }
  138.         free(Buf);
  139.     }
  140. }
  141.  
  142. static void SaveFile(WINDOW wnd)
  143. {
  144.     FILE *fp;
  145.     if (wnd->extension != NULL)    {
  146.         WINDOW mwnd = MomentaryMessage("Saving the file");
  147.         if ((fp = fopen(wnd->extension, "wt")) != NULL)    {
  148.             fwrite(GetText(wnd), strlen(GetText(wnd)), 1, fp);
  149.             fclose(fp);
  150.             wnd->TextChanged = FALSE;
  151.         }
  152.         SendMessage(mwnd, CLOSE_WINDOW, 0, 0);
  153.     }
  154. }
  155.  
  156. static int EditorProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  157. {
  158.     switch (msg)    {
  159.         case CREATE_WINDOW:
  160.             if (DocumentCount == 0)
  161.                 ActivateCommand(MainMenu, ID_SAVE);
  162.             DocumentCount++;
  163.             break;
  164.         case CLOSE_WINDOW:
  165.             if (wnd->extension != NULL)    {
  166.                 free(wnd->extension);
  167.                 wnd->extension = NULL;
  168.             }
  169.             break;
  170.         default:
  171.             break;
  172.     }
  173.     return DefaultWndProc(wnd, msg, p1, p2);
  174. }
  175.  
  176. static char *NameComponent(char *FileName)
  177. {
  178.     char *Fname;
  179.     if ((Fname = strrchr(FileName, '\\')) == NULL)
  180.         if ((Fname = strrchr(FileName, ':')) == NULL)
  181.             Fname = FileName-1;
  182.     return Fname + 1;
  183. }
  184.  
  185.  
  186.