home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / dflat.zip / MEMOPAD.C < prev    next >
Text File  |  1991-02-18  |  7KB  |  289 lines

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