1 /* --------------- memopad.c ----------- */
5 extern DF_DBOX PrintSetup
;
7 char DFlatApplication
[] = "MemoPad";
9 static char Untitled
[] = "Untitled";
12 static int MemoPadProc(DFWINDOW
, DFMESSAGE
, DF_PARAM
, DF_PARAM
);
13 static void NewFile(DFWINDOW
);
14 static void SelectFile(DFWINDOW
);
15 static void PadWindow(DFWINDOW
, char *);
16 static void OpenPadWindow(DFWINDOW
, char *);
17 static void LoadFile(DFWINDOW
);
18 static void PrintPad(DFWINDOW
);
19 static void SaveFile(DFWINDOW
, int);
20 static void MemoPadDeleteFile(DFWINDOW
);
21 static int EditorProc(DFWINDOW
, DFMESSAGE
, DF_PARAM
, DF_PARAM
);
22 static char *NameComponent(char *);
23 static int PrintSetupProc(DFWINDOW
, DFMESSAGE
, DF_PARAM
, DF_PARAM
);
24 static void FixTabMenu(void);
26 void Calendar(DFWINDOW
);
28 //void BarChart(DFWINDOW);
34 void main(int argc
, char *argv
[])
41 // if (!DfLoadConfig())
42 // DfCfg.ScreenLines = DF_SCREENHEIGHT;
43 wnd
= DfDfCreateWindow(DF_APPLICATION
,
44 "FreeDos Edit " DF_VERSION
,
57 DfSendMessage(wnd
, DFM_SETFOCUS
, TRUE
, 0);
59 PadWindow(wnd
, argv
[1]);
63 while (dispatch_message())
67 /* ------ open text files and put them into editboxes ----- */
68 static void PadWindow(DFWINDOW wnd
, char *FileName
)
71 struct _finddata_t ff
;
75 DfCreatePath(path
, FileName
, FALSE
, FALSE
);
76 cp
= path
+strlen(path
);
77 DfCreatePath(path
, FileName
, TRUE
, FALSE
);
78 ax
= _findfirst(path
, &ff
);
84 OpenPadWindow(wnd
, path
);
86 while (_findnext (ax
, &ff
) == 0);
91 /* ------- window processing module for the
92 memopad application window ----- */
93 static int MemoPadProc(DFWINDOW wnd
,DFMESSAGE msg
,DF_PARAM p1
,DF_PARAM p2
)
97 case DFM_CREATE_WINDOW
:
98 rtn
= DfDefaultWndProc(wnd
, msg
, p1
, p2
);
100 DfSetCommandToggle(&DfMainMenu
, DF_ID_INSERT
);
102 DfSetCommandToggle(&DfMainMenu
, DF_ID_WRAP
);
114 SaveFile(DfInFocus
, FALSE
);
117 SaveFile(DfInFocus
, TRUE
);
119 case DF_ID_DELETEFILE
:
120 MemoPadDeleteFile(DfInFocus
);
122 case DF_ID_PRINTSETUP
:
123 DfDialogBox(wnd
, &PrintSetup
, TRUE
, PrintSetupProc
);
129 if (!DfYesNoBox("Exit Memopad?"))
133 DfCfg
.WordWrap
= DfGetCommandToggle(&DfMainMenu
, DF_ID_WRAP
);
136 DfCfg
.InsertMode
= DfGetCommandToggle(&DfMainMenu
, DF_ID_INSERT
);
159 // case DF_ID_BARCHART:
164 "About D-Flat and the MemoPad",
165 " ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n"
171 " ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n"
172 "D-Flat implements the SAA/CUA\n"
173 "interface in a public domain\n"
174 "C language library originally\n"
175 "published in Dr. Dobb's Journal\n"
176 " ------------------------ \n"
177 "MemoPad is a multiple document\n"
178 "editor that demonstrates D-Flat");
187 return DfDefaultWndProc(wnd
, msg
, p1
, p2
);
189 /* --- The New command. Open an empty editor window --- */
190 static void NewFile(DFWINDOW wnd
)
192 OpenPadWindow(wnd
, Untitled
);
194 /* --- The Open... command. Select a file --- */
195 static void SelectFile(DFWINDOW wnd
)
198 if (DfOpenFileDialogBox("*.PAD", FileName
)) {
199 /* --- see if the document is already in a window --- */
200 DFWINDOW wnd1
= DfFirstWindow(wnd
);
201 while (wnd1
!= NULL
) {
202 if (stricmp(FileName
, wnd1
->extension
) == 0) {
203 DfSendMessage(wnd1
, DFM_SETFOCUS
, TRUE
, 0);
204 DfSendMessage(wnd1
, DFM_RESTORE
, 0, 0);
207 wnd1
= DfNextWindow(wnd1
);
209 OpenPadWindow(wnd
, FileName
);
213 /* --- open a document window and load a file --- */
214 static void OpenPadWindow(DFWINDOW wnd
, char *FileName
)
216 static DFWINDOW wnd1
= NULL
;
219 char *Fname
= FileName
;
221 if (strcmp(FileName
, Untitled
)) {
222 if (stat(FileName
, &sb
)) {
223 ermsg
= DfMalloc(strlen(FileName
)+20);
224 strcpy(ermsg
, "No such file as\n");
225 strcat(ermsg
, FileName
);
226 DfErrorMessage(ermsg
);
230 Fname
= NameComponent(FileName
);
232 wwnd
= DfWatchIcon();
236 wnd1
= DfDfCreateWindow(DF_EDITBOX
,
238 (wndpos
-1)*2, wndpos
, 10, 40,
239 NULL
, wnd
, EditorProc
,
250 if (strcmp(FileName
, Untitled
)) {
251 wnd1
->extension
= DfMalloc(strlen(FileName
)+1);
252 strcpy(wnd1
->extension
, FileName
);
255 DfSendMessage(wwnd
, DFM_CLOSE_WINDOW
, 0, 0);
256 DfSendMessage(wnd1
, DFM_SETFOCUS
, TRUE
, 0);
257 DfSendMessage(wnd1
, DFM_MAXIMIZE
, 0, 0);
260 /* --- Load the notepad file into the editor text buffer --- */
261 static void LoadFile(DFWINDOW wnd
)
267 if ((fp
= fopen(wnd
->extension
, "rt")) != NULL
) {
270 Buf
= DfRealloc(Buf
, recptr
+150);
271 memset(Buf
+recptr
, 0, 150);
272 fgets(Buf
+recptr
, 150, fp
);
273 recptr
+= strlen(Buf
+recptr
);
277 DfSendMessage(wnd
, DFM_SETTEXT
, (DF_PARAM
) Buf
, 0);
286 /* ------- print a character -------- */
287 static void PrintChar(FILE *prn
, int c
)
290 if (c
== '\n' || CharCtr
== DfCfg
.RightMargin
) {
293 if (LineCtr
== DfCfg
.BottomMargin
) {
295 for (i
= 0; i
< DfCfg
.TopMargin
; i
++)
297 LineCtr
= DfCfg
.TopMargin
;
304 for (i
= 0; i
< DfCfg
.LeftMargin
; i
++) {
313 /* --- print the current notepad --- */
314 static void PrintPad(DFWINDOW wnd
)
316 if (*DfCfg
.PrinterPort
) {
318 if ((prn
= fopen(DfCfg
.PrinterPort
, "wt")) != NULL
) {
320 BOOL KeepPrinting
= TRUE
;
321 unsigned char *text
= DfGetText(wnd
);
322 unsigned oldpct
= 100, cct
= 0, len
= strlen(text
);
323 DFWINDOW swnd
= DfSliderBox(20, DfGetTitle(wnd
), "Printing");
324 /* ------- print the notepad text --------- */
325 LineCtr
= CharCtr
= 0;
326 while (KeepPrinting
&& *text
) {
327 PrintChar(prn
, *text
++);
328 percent
= ((long) ++cct
* 100) / len
;
329 if ((int)percent
!= (int)oldpct
) {
330 oldpct
= (int) percent
;
331 KeepPrinting
= DfSendMessage(swnd
, DFM_PAINT
, 0, oldpct
);
335 /* ---- user did not cancel ---- */
337 DfSendMessage(swnd
, DFM_PAINT
, 0, 100);
338 /* ------- follow with a form feed? --------- */
339 if (DfYesNoBox("Form Feed?"))
344 DfErrorMessage("Cannot open printer file");
347 DfErrorMessage("No printer selected");
350 /* ---------- save a file to disk ------------ */
351 static void SaveFile(DFWINDOW wnd
, int Saveas
)
354 if (wnd
->extension
== NULL
|| Saveas
) {
356 if (DfSaveAsDialogBox(FileName
)) {
357 if (wnd
->extension
!= NULL
)
358 free(wnd
->extension
);
359 wnd
->extension
= DfMalloc(strlen(FileName
)+1);
360 strcpy(wnd
->extension
, FileName
);
361 DfAddTitle(wnd
, NameComponent(FileName
));
362 DfSendMessage(wnd
, DFM_BORDER
, 0, 0);
367 if (wnd
->extension
!= NULL
) {
368 DFWINDOW mwnd
= DfMomentaryMessage("Saving the file");
369 if ((fp
= fopen(wnd
->extension
, "wt")) != NULL
) {
370 fwrite(DfGetText(wnd
), strlen(DfGetText(wnd
)), 1, fp
);
372 wnd
->TextChanged
= FALSE
;
374 DfSendMessage(mwnd
, DFM_CLOSE_WINDOW
, 0, 0);
377 /* -------- delete a file ------------ */
378 static void MemoPadDeleteFile(DFWINDOW wnd
)
380 if (wnd
->extension
!= NULL
) {
381 if (strcmp(wnd
->extension
, Untitled
)) {
382 char *fn
= NameComponent(wnd
->extension
);
385 sprintf(msg
, "Delete %s?", fn
);
386 if (DfYesNoBox(msg
)) {
387 unlink(wnd
->extension
);
388 DfSendMessage(wnd
, DFM_CLOSE_WINDOW
, 0, 0);
395 /* ------ display the row and column in the statusbar ------ */
396 static void ShowPosition(DFWINDOW wnd
)
399 sprintf(status
, "Line:%4d Column: %2d",
400 wnd
->CurrLine
, wnd
->CurrCol
);
401 DfSendMessage(DfGetParent(wnd
), DFM_ADDSTATUS
, (DF_PARAM
) status
, 0);
404 /* ----- window processing module for the editboxes ----- */
405 static int EditorProc(DFWINDOW wnd
,DFMESSAGE msg
,DF_PARAM p1
,DF_PARAM p2
)
411 wnd
->InsertMode
= DfGetCommandToggle(&DfMainMenu
, DF_ID_INSERT
);
412 wnd
->WordWrapMode
= DfGetCommandToggle(&DfMainMenu
, DF_ID_WRAP
);
414 rtn
= DfDefaultWndProc(wnd
, msg
, p1
, p2
);
415 if ((int)p1
== FALSE
)
416 DfSendMessage(DfGetParent(wnd
), DFM_ADDSTATUS
, 0, 0);
420 case DFM_KEYBOARD_CURSOR
:
421 rtn
= DfDefaultWndProc(wnd
, msg
, p1
, p2
);
432 case DF_ID_SEARCHNEXT
:
436 DfCopyToClipboard(wnd
);
437 DfSendMessage(wnd
, DFM_COMMAND
, DF_ID_DELETETEXT
, 0);
438 DfSendMessage(wnd
, DFM_PAINT
, 0, 0);
441 DfCopyToClipboard(wnd
);
442 DfClearTextBlock(wnd
);
443 DfSendMessage(wnd
, DFM_PAINT
, 0, 0);
446 DfPasteFromClipboard(wnd
);
447 DfSendMessage(wnd
, DFM_PAINT
, 0, 0);
449 case DF_ID_DELETETEXT
:
451 rtn
= DfDefaultWndProc(wnd
, msg
, p1
, p2
);
452 DfSendMessage(wnd
, DFM_PAINT
, 0, 0);
455 DfDisplayHelp(wnd
, "MEMOPADDOC");
458 DfSendMessage(DfGetParent(wnd
), DFM_COMMAND
, DF_ID_WRAP
, 0);
459 wnd
->WordWrapMode
= DfCfg
.WordWrap
;
462 DfSendMessage(DfGetParent(wnd
), DFM_COMMAND
, DF_ID_INSERT
, 0);
463 wnd
->InsertMode
= DfCfg
.InsertMode
;
464 DfSendMessage(NULL
, DFM_SHOW_CURSOR
, wnd
->InsertMode
, 0);
470 case DFM_CLOSE_WINDOW
:
471 if (wnd
->TextChanged
) {
472 char *cp
= DfMalloc(25+strlen(DfGetTitle(wnd
)));
473 DfSendMessage(wnd
, DFM_SETFOCUS
, TRUE
, 0);
474 strcpy(cp
, DfGetTitle(wnd
));
475 strcat(cp
, "\nText changed. Save it?");
477 DfSendMessage(DfGetParent(wnd
),
478 DFM_COMMAND
, DF_ID_SAVE
, 0);
482 if (wnd
->extension
!= NULL
) {
483 free(wnd
->extension
);
484 wnd
->extension
= NULL
;
490 return DfDefaultWndProc(wnd
, msg
, p1
, p2
);
492 /* -- point to the name component of a file specification -- */
493 static char *NameComponent(char *FileName
)
496 if ((Fname
= strrchr(FileName
, '\\')) == NULL
)
497 if ((Fname
= strrchr(FileName
, ':')) == NULL
)
502 static char *ports
[] = {
503 "Lpt1", "Lpt2", "Lpt3",
504 "Com1", "Com2", "Com3", "Com4",
508 static int PrintSetupProc(DFWINDOW wnd
, DFMESSAGE msg
, DF_PARAM p1
, DF_PARAM p2
)
514 case DFM_CREATE_WINDOW
:
515 rtn
= DfDefaultWndProc(wnd
, msg
, p1
, p2
);
516 DfPutItemText(wnd
, DF_ID_PRINTERPORT
, DfCfg
.PrinterPort
);
517 while (ports
[i
] != NULL
)
518 DfPutComboListText(wnd
, DF_ID_PRINTERPORT
, ports
[i
++]);
519 for (mar
= CHARSLINE
; mar
>= 0; --mar
) {
520 sprintf(marg
, "%3d", mar
);
521 DfPutItemText(wnd
, DF_ID_LEFTMARGIN
, marg
);
522 DfPutItemText(wnd
, DF_ID_RIGHTMARGIN
, marg
);
524 for (mar
= LINESPAGE
; mar
>= 0; --mar
) {
525 sprintf(marg
, "%3d", mar
);
526 DfPutItemText(wnd
, DF_ID_TOPMARGIN
, marg
);
527 DfPutItemText(wnd
, DF_ID_BOTTOMMARGIN
, marg
);
529 cwnd
= DfControlWindow(&PrintSetup
, DF_ID_LEFTMARGIN
);
530 DfSendMessage(cwnd
, DFM_LB_SETSELECTION
,
531 CHARSLINE
-DfCfg
.LeftMargin
, 0);
532 cwnd
= DfControlWindow(&PrintSetup
, DF_ID_RIGHTMARGIN
);
533 DfSendMessage(cwnd
, DFM_LB_SETSELECTION
,
534 CHARSLINE
-DfCfg
.RightMargin
, 0);
535 cwnd
= DfControlWindow(&PrintSetup
, DF_ID_TOPMARGIN
);
536 DfSendMessage(cwnd
, DFM_LB_SETSELECTION
,
537 LINESPAGE
-DfCfg
.TopMargin
, 0);
538 cwnd
= DfControlWindow(&PrintSetup
, DF_ID_BOTTOMMARGIN
);
539 DfSendMessage(cwnd
, DFM_LB_SETSELECTION
,
540 LINESPAGE
-DfCfg
.BottomMargin
, 0);
543 if ((int) p1
== DF_ID_OK
&& (int) p2
== 0) {
544 DfGetItemText(wnd
, DF_ID_PRINTERPORT
, DfCfg
.PrinterPort
, 4);
545 cwnd
= DfControlWindow(&PrintSetup
, DF_ID_LEFTMARGIN
);
546 DfCfg
.LeftMargin
= CHARSLINE
-
547 DfSendMessage(cwnd
, DFM_LB_CURRENTSELECTION
, 0, 0);
548 cwnd
= DfControlWindow(&PrintSetup
, DF_ID_RIGHTMARGIN
);
549 DfCfg
.RightMargin
= CHARSLINE
-
550 DfSendMessage(cwnd
, DFM_LB_CURRENTSELECTION
, 0, 0);
551 cwnd
= DfControlWindow(&PrintSetup
, DF_ID_TOPMARGIN
);
552 DfCfg
.TopMargin
= LINESPAGE
-
553 DfSendMessage(cwnd
, DFM_LB_CURRENTSELECTION
, 0, 0);
554 cwnd
= DfControlWindow(&PrintSetup
, DF_ID_BOTTOMMARGIN
);
555 DfCfg
.BottomMargin
= LINESPAGE
-
556 DfSendMessage(cwnd
, DFM_LB_CURRENTSELECTION
, 0, 0);
562 return DfDefaultWndProc(wnd
, msg
, p1
, p2
);
565 static void FixTabMenu(void)
567 char *cp
= DfGetCommandText(&DfMainMenu
, DF_ID_TABS
);
569 cp
= strchr(cp
, '(');
571 *(cp
+1) = DfCfg
.Tabs
+ '0';
572 if (DfGetClass(DfInFocus
) == DF_POPDOWNMENU
)
573 DfSendMessage(DfInFocus
, DFM_PAINT
, 0, 0);
578 void DfPrepFileMenu(void *w
, struct DfMenu
*mnu
)
581 DfDeactivateCommand(&DfMainMenu
, DF_ID_SAVE
);
582 DfDeactivateCommand(&DfMainMenu
, DF_ID_SAVEAS
);
583 DfDeactivateCommand(&DfMainMenu
, DF_ID_DELETEFILE
);
584 DfDeactivateCommand(&DfMainMenu
, DF_ID_PRINT
);
585 if (wnd
!= NULL
&& DfGetClass(wnd
) == DF_EDITBOX
) {
586 if (DfIsMultiLine(wnd
)) {
587 DfActivateCommand(&DfMainMenu
, DF_ID_SAVE
);
588 DfActivateCommand(&DfMainMenu
, DF_ID_SAVEAS
);
589 DfActivateCommand(&DfMainMenu
, DF_ID_DELETEFILE
);
590 DfActivateCommand(&DfMainMenu
, DF_ID_PRINT
);
595 void DfPrepSearchMenu(void *w
, struct DfMenu
*mnu
)
598 DfDeactivateCommand(&DfMainMenu
, DF_ID_SEARCH
);
599 DfDeactivateCommand(&DfMainMenu
, DF_ID_REPLACE
);
600 DfDeactivateCommand(&DfMainMenu
, DF_ID_SEARCHNEXT
);
601 if (wnd
!= NULL
&& DfGetClass(wnd
) == DF_EDITBOX
) {
602 if (DfIsMultiLine(wnd
)) {
603 DfActivateCommand(&DfMainMenu
, DF_ID_SEARCH
);
604 DfActivateCommand(&DfMainMenu
, DF_ID_REPLACE
);
605 DfActivateCommand(&DfMainMenu
, DF_ID_SEARCHNEXT
);
610 void DfPrepEditMenu(void *w
, struct DfMenu
*mnu
)
613 DfDeactivateCommand(&DfMainMenu
, DF_ID_CUT
);
614 DfDeactivateCommand(&DfMainMenu
, DF_ID_COPY
);
615 DfDeactivateCommand(&DfMainMenu
, DF_ID_CLEAR
);
616 DfDeactivateCommand(&DfMainMenu
, DF_ID_DELETETEXT
);
617 DfDeactivateCommand(&DfMainMenu
, DF_ID_PARAGRAPH
);
618 DfDeactivateCommand(&DfMainMenu
, DF_ID_PASTE
);
619 DfDeactivateCommand(&DfMainMenu
, DF_ID_UNDO
);
620 if (wnd
!= NULL
&& DfGetClass(wnd
) == DF_EDITBOX
) {
621 if (DfIsMultiLine(wnd
)) {
622 if (DfTextBlockMarked(wnd
)) {
623 DfActivateCommand(&DfMainMenu
, DF_ID_CUT
);
624 DfActivateCommand(&DfMainMenu
, DF_ID_COPY
);
625 DfActivateCommand(&DfMainMenu
, DF_ID_CLEAR
);
626 DfActivateCommand(&DfMainMenu
, DF_ID_DELETETEXT
);
628 DfActivateCommand(&DfMainMenu
, DF_ID_PARAGRAPH
);
629 if (!DfTestAttribute(wnd
, DF_READONLY
) &&
631 DfActivateCommand(&DfMainMenu
, DF_ID_PASTE
);
632 if (wnd
->DeletedText
!= NULL
)
633 DfActivateCommand(&DfMainMenu
, DF_ID_UNDO
);