-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcalim_memo.c
703 lines (631 loc) · 16.4 KB
/
xcalim_memo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/***
* module name:
xcalim_memo.c
* function:
Deal with popup memo file
A single popup file is stored in a file called
memo on the Calendar directory
***/
#include <stdio.h>
#include <ctype.h>
#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Text.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Dialog.h>
#include "xcalim.h"
static XtCallbackRec callbacks[] = {
{NULL, NULL},
{NULL, NULL}
};
#define ClearCallbacks() memset((void *)callbacks, '\0', sizeof (callbacks))
#define argLD(N,V) { XtSetArg(args[nargs], N, V); nargs++; }
static String leaveTransl =
"<Key>n :PressAButton(3)\n\
<Key>y :PressAButton(4)\n\
<Key>Return:PressAButton(4)\n\
<Key>c :PressAButton(9)\n\
<Key>Escape:PressAButton(3)";
/*
* Structure for storing relavant data about the memo Edit
*/
typedef struct memoEdit {
Widget m_button; /* widget of the control button */
Widget m_popup; /* widget of editor popup */
Widget m_quit; /* widget of quit button */
Widget m_edit; /* widget of edit button */
Widget m_help; /* widget of help button */
Widget m_save; /* widget of save button */
Boolean m_savesens; /* state of the save button */
Widget m_display; /* widget of display title area */
Widget m_text; /* the text area */
Widget m_today; /* today's data */
Widget m_weekly; /* widget of text image of weekly */
/* events */
String m_weeklytext; /* weekly text */
Cardinal m_size; /* size of the buffer */
char *m_data; /* pointer to malloc'ed data buffer */
} MemoEdit;
static MemoEdit memo;
static String memoContents;
extern void MemoHelp(); /* look in xcalim_help.c */
/*
* Internal routines
*/
void MemoPopup();
void CloseMemo();
static void CleanMemo();
static void MemoCheckExit();
static void MCheckDia();
static Boolean WriteMemoFile();
static void EditToday();
static int NewlineCount();
static String GetMemoFile();
static void SaveMemoEdits();
static void MemoTextChanged();
static void FinishMemoEditing();
static void YesCheck();
static void NoCheck();
static void CancelCheck();
static void AdjustTitleHeight();
/*
* Callback routine to display the memo file
*/
void
DoMemo(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
static Arg args[1];
Pixel bg, fg;
/* Make the button become a finish button */
memo.m_button = w;
callbacks[0].callback = FinishMemoEditing;
callbacks[0].closure = NULL;
XtVaGetValues(w, XtNbackground, &bg, XtNforeground, &fg, NULL);
XtVaSetValues(w, XtNcallback, callbacks, XtNbackground, fg, XtNforeground,
bg, NULL);
/*
* Get existing memo contents
* if the user is polling then re-read the file
*/
if (appResources.update && memoContents != NULL) {
XtFree(memoContents);
memoContents = NULL;
}
if (memoContents == NULL)
memoContents = GetMemoFile();
/*
* Set up the popup widget for editing
*/
MemoPopup();
}
/*
* Get old contents from a memo file if any
*/
static String
GetMemoFile()
{
if (FoundCalendarDir && access(appResources.memoFile, F_OK) == 0)
return ReadCalendarFile(NULL, appResources.memoFile);
return NULL;
}
/*
* Do the biz to popup an edit style window
*/
void
MemoPopup()
{
Widget et, lw;
Widget frame;
Arg args[10];
Cardinal nargs;
String str;
MonthEntry *me;
Dimension charHeight;
/*
* set up edit buffer
*/
if (memoContents)
memo.m_size = appResources.textbufsz + strlen(memoContents) + 1;
else
memo.m_size = appResources.textbufsz;
memo.m_data = XtMalloc(memo.m_size);
if (memoContents)
strcpy(memo.m_data, memoContents);
else
*memo.m_data = '\0';
memo.m_popup = XtCreatePopupShell("memo", topLevelShellWidgetClass, toplevel, NULL, 0);
/*
* The first title line
*/
et = XtCreateManagedWidget("memoPanel", panedWidgetClass, memo.m_popup, NULL, 0);
nargs = 0;
argLD(XtNshowGrip, False);
argLD(XtNskipAdjust, True);
argLD(XtNdefaultDistance, 1);
frame = XtCreateManagedWidget("title", formWidgetClass, et, args, nargs);
/*
* containing some buttons for controlling the world
*/
/*
* Take label "quit" from resources
*/
callbacks[0].callback = FinishMemoEditing;
callbacks[0].closure = NULL;
nargs = 0;
argLD(XtNcallback, callbacks);
argLD(XtNfromHoriz, NULL);
argLD(XtNleft, XtChainLeft);
argLD(XtNright, XtChainLeft);
lw = memo.m_quit = XtCreateManagedWidget("quit", commandWidgetClass, frame, args, nargs);
XtUnmanageChild(lw);
/*
* Edit todays file from here as well
* Take label from resources
*/
if (MyCalendar) {
callbacks[0].callback = EditToday;
callbacks[0].closure = (void *) 0;
nargs = 0;
argLD(XtNcallback, callbacks);
argLD(XtNfromHoriz, NULL);
argLD(XtNleft, XtChainLeft);
argLD( XtNright, XtChainLeft);
memo.m_edit = lw = XtCreateManagedWidget("edit", commandWidgetClass, frame, args, nargs);
}
/*
* If we are dealing with help then do it now
*/
if (appResources.giveHelp) {
/* Take label "help" from resources */
callbacks[0].callback = MemoHelp;
callbacks[0].closure = (void *) 0;
nargs = 0;
argLD(XtNcallback, callbacks);
argLD(XtNfromHoriz, lw);
argLD(XtNleft, XtChainLeft);
argLD( XtNright, XtChainLeft);
memo.m_help = lw = XtCreateManagedWidget("help", commandWidgetClass, frame, args, nargs);
}
/*
* The remaining bit here is a date label
*/
nargs = 0;
argLD(XtNlabel, date_area);
argLD(XtNborderWidth, 0);
argLD(XtNfromHoriz, lw);
argLD(XtNfromVert, NULL);
argLD(XtNvertDistance, 2);
argLD(XtNleft, XtChainLeft);
argLD(XtNright, XtChainRight);
lw = memo.m_display = XtCreateManagedWidget("date", labelWidgetClass, frame, args, nargs);
/*
* Details for today
*/
me = GetMonthEntry(today.year, today.month);
nargs = 0;
str = me->me_have[today.day];
if (str == NULL)
str = "";
argLD(XtNstring, str);
argLD(XtNdisplayCaret, False);
argLD(XtNeditType, XawtextRead);
memo.m_today = XtCreateManagedWidget("display", asciiTextWidgetClass, et, args, nargs);
{
Dimension height;
XtSetArg(args[0], XtNheight, &height);
XtGetValues(memo.m_today, args, 1);
charHeight = height;
height = height * NewlineCount(str);
XtSetArg(args[0], XtNheight, height);
XtSetValues(memo.m_today, args, 1);
}
AdjustTitleHeight(memo.m_quit,
MyCalendar ? memo.m_edit : NULL,
appResources.giveHelp ? memo.m_help : NULL,
memo.m_display);
/*
* Weekly details - the data for today + an edit button
* The header to this is a form
*/
nargs = 0;
argLD(XtNshowGrip, False);
argLD(XtNskipAdjust, True);
argLD(XtNdefaultDistance, 1);
frame = XtCreateManagedWidget("weeklyMemo", formWidgetClass, et, args, nargs);
/*
* Take label "edit" from resources
*/
if (MyCalendar) {
callbacks[0].callback = DoWeekly;
callbacks[0].closure = (void *) & memo;
nargs = 0;
argLD(XtNcallback, callbacks);
argLD(XtNfromHoriz, NULL);
argLD(XtNleft, XtChainLeft);
argLD(XtNright, XtChainLeft);
lw = XtCreateManagedWidget("weeklyEdit", commandWidgetClass, frame, args, nargs);
}
/*
* Say this is a weekly commitment
*/
nargs = 0;
argLD(XtNshowGrip, True);
argLD(XtNborderWidth, 0);
argLD(XtNfromHoriz, MyCalendar ? lw : NULL);
argLD(XtNfromVert, NULL);
argLD(XtNvertDistance, 2);
argLD(XtNleft, XtChainLeft);
argLD(XtNright, XtChainRight);
lw = XtCreateManagedWidget("weeklyTitle", labelWidgetClass, frame, args, nargs);
/*
* Details for today
*/
nargs = 0;
if (memo.m_weeklytext)
XtFree(memo.m_weeklytext);
memo.m_weeklytext = str = GetWeeklyFile(today.wday);
if (str == NULL)
str = "";
argLD(XtNstring, str);
argLD(XtNdisplayCaret, False);
argLD(XtNeditType, XawtextRead);
if (charHeight)
argLD(XtNheight, NewlineCount(str) * charHeight);
memo.m_weekly = XtCreateManagedWidget("display", asciiTextWidgetClass, et, args, nargs);
/*
* Another form with some buttons
*/
nargs = 0;
argLD(XtNshowGrip, False);
argLD(XtNskipAdjust, True);
argLD(XtNdefaultDistance, 1);
frame = XtCreateManagedWidget("memoMiddle", formWidgetClass, et, args, nargs);
if (MyCalendar) {
/*
* Take label "save" from resources
*/
callbacks[0].callback = SaveMemoEdits;
callbacks[0].closure = (void *) & memo;
nargs = 0;
argLD(XtNcallback, callbacks);
argLD(XtNfromHoriz, NULL);
argLD(XtNleft, XtChainLeft);
argLD(XtNright, XtChainLeft);
argLD(XtNsensitive, False);
lw = memo.m_save = XtCreateManagedWidget("save", commandWidgetClass, frame, args, nargs);
memo.m_savesens = False;
}
/*
* Say this is a memo edit
*/
nargs = 0;
argLD(XtNshowGrip, True);
argLD(XtNborderWidth, 0);
argLD(XtNfromHoriz, MyCalendar ? lw : NULL);
argLD(XtNfromVert, NULL);
argLD(XtNvertDistance, 2);
argLD(XtNleft, XtChainLeft);
argLD(XtNright, XtChainRight);
lw = XtCreateManagedWidget("memoTitle", labelWidgetClass, frame, args, nargs);
/*
* The text widget is in the pane below
* The Scroll Attributes are controlled from the application
* defaults file
*/
callbacks[0].callback = MemoTextChanged;
callbacks[0].closure = (void *) & memo;
nargs = 0;
argLD(XtNstring, memo.m_data);
argLD(XtNeditType, XawtextEdit);
argLD(XtNlength, memo.m_size);
argLD(XtNuseStringInPlace, True);
argLD(XtNcallback, callbacks);
memo.m_text = XtCreateManagedWidget("memoText", asciiTextWidgetClass, et, args, nargs);
XtOverrideTranslations(memo.m_popup,
XtParseTranslationTable("<Message>WM_PROTOCOLS: PopDownMemo()"));
XawTextSetInsertionPoint(memo.m_text, XawTextLastPosition(memo.m_text));
XtPopup(memo.m_popup, XtGrabNone);
XtSetKeyboardFocus(memo.m_popup, memo.m_text);
(void)XSetWMProtocols(XtDisplay(memo.m_popup), XtWindow(memo.m_popup),
&delWin, 1);
}
/*
* Adjust title line height
* possibly 4 objects
*/
static void
AdjustTitleHeight(quit, edit, help, label)
Widget quit;
Widget edit;
Widget help;
Widget label;
{
int hq, he, hh, hl;
int max;
hq = wHeight(quit);
he = edit ? wHeight(edit) : 0;
hh = help ? wHeight(help): 0;
hl = wHeight(label);
max = hq;
max = (he > max) ? he : max;
max = (hh > max) ? hh : max;
max = (hl > max) ? hl : max;
if (hq < max)
SetWidgetHeightMax(quit, hq, max);
if (he && he < max)
SetWidgetHeightMax(edit, he, max);
if (hh & hh < max)
SetWidgetHeightMax(help, hh, max);
if (hl < max)
SetWidgetHeightMax(label, hl, max);
}
/*
* This callback starts editing today
*/
static void
EditToday(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
StartEditing(w, &today, w);
}
/*
* Count newlines in a string
*/
static int
NewlineCount(str)
String str;
{
register int sum = 0;
while (*str)
if (*str++ == '\n')
sum++;
/* Add one line - assume last line does NOT have an nl */
sum++;
/* ignore a final newline */
if (str[-1] == '\n')
sum--;
if (sum <= 0)
sum = 1;
return (sum > appResources.maxDisplayLines ? appResources.maxDisplayLines : sum);
}
/*
* Entry point from outside when today's text changed
*/
void
UpdateMemo()
{
Arg args[1];
String str;
MonthEntry *me;
/*
* if the button widget is zero then we are displaying nothing
*/
if (memo.m_button == 0)
return;
me = GetMonthEntry(today.year, today.month);
str = me->me_have[today.day];
if (str == NULL)
str = "";
XtSetArg(args[0], XtNstring, str);
XtSetValues(memo.m_today, args, 1);
XtSetArg(args[0], XtNlabel, date_area);
XtSetValues(memo.m_display, args, 1);
if (memo.m_weeklytext)
XtFree(memo.m_weeklytext);
memo.m_weeklytext = str = GetWeeklyFile(today.wday);
if (str == NULL)
str = "";
XtSetArg(args[0], XtNstring, str);
XtSetValues(memo.m_weekly, args, 1);
}
/*
* Poll call from the alarm timeout
*/
void
MemoPoll()
{
int size;
Arg args[10];
int nargs;
if (memo.m_button == 0)
return;
if (memo.m_savesens == True)
return;
if (memoContents)
XtFree(memoContents);
memoContents = GetMemoFile();
if (memoContents) {
if (strcmp(memoContents, memo.m_data) == 0)
return;
size = strlen(memoContents) + 1;
if (size > memo.m_size) {
size += appResources.textbufsz;
XtFree(memo.m_data);
memo.m_data = XtMalloc(memo.m_size = size);
}
strcpy(memo.m_data, memoContents);
} else
*memo.m_data = '\0';
nargs = 0;
argLD(XtNstring, memo.m_data);
argLD(XtNlength, memo.m_size);
argLD(XtNuseStringInPlace, True);
XtSetValues(memo.m_text, args, nargs);
}
/*
* Call backs for various buttons
*/
/* ARGSUSED */
static void
MemoTextChanged(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
register MemoEdit *memo = (MemoEdit *) closure;
if (MyCalendar) {
memo->m_savesens = True;
XtSetSensitive(memo->m_save, True);
}
}
/*
* Callback routines
*/
/* ARGSUSED */
static void
SaveMemoEdits(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
MemoEdit *memo = (MemoEdit *) closure;
if (WriteMemoFile(memo) == False)
return;
if (memoContents) {
XtFree(memoContents);
memoContents = XtNewString(memo->m_data);
}
memo->m_savesens = False;
XtSetSensitive(memo->m_save, False);
}
/*
* Write the memo file out
*/
static Boolean
WriteMemoFile(memo)
MemoEdit *memo;
{
Cardinal len = strlen(memo->m_data);
String fname;
int fd;
if (len == 0) {
unlink(appResources.memoFile);
return (True);
}
/*
* First let's see if we have to create the toplevel directory
*/
if (!NeedTop())
return (False);
fname = appResources.memoFile;
if ((fd = open(fname, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0) {
XBell(XtDisplay(toplevel), 0);
fprintf(stderr, "xcalim: Could not open %s/%s for writing.\n", MapStem, fname);
perror("xcalim: open");
fflush(stderr);
return (False);
}
if (write(fd, memo->m_data, len) != len) {
XBell(XtDisplay(toplevel), 0);
fprintf(stderr, "xcalim: Write error %s/%s file.\n", MapStem, fname);
perror("xcalim: write");
fflush(stderr);
close(fd);
return (False);
}
close(fd);
return (True);
}
void
CloseMemo(w, event, params, numb)
Widget w;
XEvent *event;
String *params;
Cardinal *numb;
{
FinishMemoEditing(w, (void *) NULL, (void *) NULL);
}
static void
FinishMemoEditing(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
if (memo.m_savesens == True)
MemoCheckExit();
else
CleanMemo();
}
static void
CleanMemo()
{
static MemoEdit zerom;
Pixel bg, fg;
callbacks[0].callback = DoMemo;
callbacks[0].closure = NULL;
XtVaGetValues(memo.m_button, XtNforeground, &fg, XtNbackground, &bg, NULL);
XtVaSetValues(memo.m_button, XtNcallback, callbacks, XtNforeground, bg,
XtNbackground, fg, NULL);
XtSetSensitive(memo.m_button, True);
XtPopdown(memo.m_popup);
XtDestroyWidget(memo.m_popup);
XtFree(memo.m_data);
XtFree(memo.m_weeklytext);
memo = zerom;
}
static void
MemoCheckExit()
{
DialogPopup(memo.m_quit, MCheckDia, &memo, NULL);
}
static void
MCheckDia(pop, ed)
Widget pop;
MemoEdit *ed;
{
Widget dia;
XtSetSensitive(memo.m_button, False);
XtSetSensitive(memo.m_save, False);
/* Take "Save file?" from resources */
dia = XtCreateManagedWidget("memocheck", dialogWidgetClass, pop, NULL, 0);
XawDialogAddButton(dia, "yes", YesCheck, ed);
XawDialogAddButton(dia, "no", NoCheck, ed);
XawDialogAddButton(dia, "cancel", CancelCheck, ed);
XtOverrideTranslations(XtParent(dia),
XtParseTranslationTable("<Message>WM_PROTOCOLS: PressAButton(4)"));
XtOverrideTranslations(dia, XtParseTranslationTable(leaveTransl));
}
/* ARGSUSED */
static void
YesCheck(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
SaveMemoEdits(w, closure, call_data);
CleanMemo();
XtDestroyWidget(XtParent(XtParent(w)));
}
/* ARGSUSED */
static void
NoCheck(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
CleanMemo();
XtDestroyWidget(XtParent(XtParent(w)));
}
/* ARGSUSED */
static void
CancelCheck(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
MemoEdit *ed = (MemoEdit *) closure;
XtSetSensitive(ed->m_save, True);
XtSetSensitive(memo.m_button, True);
XtDestroyWidget(XtParent(XtParent(w)));
}