-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcalim_edit.c
1239 lines (1109 loc) · 32.6 KB
/
xcalim_edit.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*** Deal with editable days ***/
#include <stdio.h>
#include <ctype.h>
#include <pwd.h>
#include <X11/Intrinsic.h>
#include <X11/Xos.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"
#include <sys/stat.h>
#include <dirent.h>
#define argLD(N,V) { XtSetArg(args[nargs], N, V); nargs++; }
static String editTransl =
"Ctrl <Key>s : PressAButton(0)\n\
Ctrl Shift <Key>d : PressAButton(1)\n\
<Key>F1: PressAButton(11)";
static String confDialTransl =
"<Key>n: PressAButton(3)\n\
<Key>y: PressAButton(4)\n\
<Key>c: PressAButton(9)";
typedef struct editline {
struct editline *ed_next; /* Pointer to next */
struct meWrap *ed_meWrap; /* Pointer to head of the chain */
Cardinal ed_day; /* What day we are */
Widget ed_popup; /* widget of editor popup */
Widget ed_quit; /* widget of quit button */
Widget ed_save; /* widget of save button */
Widget ed_delete; /* widget of delete button */
Widget ed_text; /* the text area */
Cardinal ed_size; /* size of the buffer */
char *ed_data; /* pointer to malloc'ed data buffer */
} EditLine;
typedef struct meWrap {
struct meWrap *mw_next;
String mw_dir; /* name of the directory */
Boolean mw_useTopDir; /* have found some data in the top dir */
MonthEntry mw_me; /* what the external world sees */
Instance *mw_list; /* list of toplevel widget for the */
/* current set of displayed strips */
EditLine *mw_ed; /* data being edited */
} MeWrap;
#define mw_year mw_me.me_year
#define mw_month mw_me.me_month
#define mw_have mw_me.me_have
#define StripType(mw) (mw->mw_me.me_type)
static MeWrap *WrapBase; /* base of the list */
static MeWrap *WrapEnd; /* the last one in the list */
char *MapStem; /* pointer to the string which is */
/* where the map data is stored */
Boolean FoundCalendarDir; /* whether the Calendar directory exists */
static XtCallbackRec callbacks[] = {
{NULL, NULL},
{NULL, NULL}
};
#define ClearCallbacks() memset((void *)callbacks, '\0', sizeof (callbacks))
/*
* Routine specs
*/
static void MakeMapStem();
static MeWrap *NewMeWrap();
static MeWrap *MeWrapSearch();
static void DeRegisterMonth();
static Boolean WriteCalendarFile();
static Boolean WriteWeeklyFile();
static void DeleteCalendarFile();
static int EditCheck();
static void SetAllButtons();
static void TextChanged();
static void UpdateDayDetails();
static void FinishEditing();
static void CleanEditPanel();
static void CheckExit();
static void CheckDia();
static void YesCheck();
static void NoCheck();
static void CancelCheck();
static int DayMatch();
static void SaveEdits();
static void DeleteEditFile();
void StartDayEditor();
void Fatal();
/*
* Fire up the month entry environment called once
*/
void
InitMonthEntries()
{
if (MapStem == NULL)
MakeMapStem();
if (access(MapStem, F_OK) < 0)
FoundCalendarDir = False;
else {
FoundCalendarDir = True;
/* * If we can see the directory, then lurch into it */
if (chdir(MapStem) < 0)
Fatal("Cannot change into %s", MapStem);
}
}
/*
* Find the base of the Calendar structure
*/
static void
MakeMapStem()
{
char buf[BUFSIZ];
struct passwd *pw;
if (appResources.otheruser) {
/* someone else */
pw = getpwnam(appResources.otheruser);
if (pw == NULL)
Fatal("Cannot get password details for %s\n",
appResources.otheruser);
} else {
pw = getpwuid(getuid());
if (pw == NULL)
Fatal("Cannot get password details for YOU!\n");
}
(void) sprintf(buf, "%s/%s", pw->pw_dir, appResources.directory);
MapStem = XtNewString(buf);
}
/*
* Get the entry for a specific month
*
* xcalendar files are all
* xc<d><Mon><Year>
* or
* xc<dd><Mon><Year>
*
* where d or dd is the day (%02d would have been better)
* <Mon> is a capitalised first three letters of the
* English month name. If you need compatibility
* don't redefine the short names in the resources
* for this program.
* <Year> is the full numeric year.
*
* We will follow this BUT we will also make this program
* create subdirectories for new years
* xy<Year>
* to speed up file access
*/
MonthEntry *
GetMonthEntry(yr, mo)
Cardinal yr;
Cardinal mo;
{
MeWrap *mw;
char *dir;
DIR *dirp;
struct dirent *dp;
int da;
Boolean inSubDir;
char yearbuf[5];
char monthbuf[4];
if ((mw = MeWrapSearch(yr, mo)) == NULL)
mw = NewMeWrap(yr, mo);
mw->mw_me.me_type = ME_MONTHLY;
if (!FoundCalendarDir)
return (&mw->mw_me);
/* * need this for string match */
(void) sprintf(yearbuf, "%d", yr);
(void) sprintf(monthbuf, "%s", appResources.smon[mo]);
/* we are in the directory */
/* so let's look here for the any files of interest */
dir = ".";
inSubDir = False;
if (mw->mw_dir) {
dir = mw->mw_dir;
inSubDir = True;
}
if ((dirp = opendir(dir)) == NULL)
Fatal("Cannot open directory: %s", MapStem);
for (da = 1; da < 32; da++)
if (mw->mw_have[da]) {
XtFree(mw->mw_have[da]);
mw->mw_have[da] = NULL;
}
while ((dp = readdir(dirp)) != NULL) {
switch (strlen(dp->d_name)) {
case 6: /* xy<Year> ?? */
if (dp->d_name[0] == 'x' &&
dp->d_name[1] == 'y' &&
dp->d_name[2] == yearbuf[0] &&
dp->d_name[3] == yearbuf[1] &&
dp->d_name[4] == yearbuf[2] &&
dp->d_name[5] == yearbuf[3] &&
appResources.calCompat == False) {
/*
* well - we're wasting
* our time at the top
* level - rejig things
* to work in the
* subdirectory
*/
inSubDir = True;
mw->mw_useTopDir = False;
mw->mw_dir = XtNewString(dp->d_name);
closedir(dirp);
if ((dirp = opendir(mw->mw_dir)) == NULL)
Fatal("Cannot open directory %s/%s", MapStem, mw->mw_dir);
}
break;
case 10: /* xc<d><Mon><Year> ?? */
if (dp->d_name[0] == 'x' &&
dp->d_name[1] == 'c' &&
isdigit(dp->d_name[2]) &&
dp->d_name[3] == monthbuf[0] &&
dp->d_name[4] == monthbuf[1] &&
dp->d_name[5] == monthbuf[2] &&
dp->d_name[6] == yearbuf[0] &&
dp->d_name[7] == yearbuf[1] &&
dp->d_name[8] == yearbuf[2] &&
dp->d_name[9] == yearbuf[3]) {
da = dp->d_name[2] - '0';
mw->mw_have[da] = ReadCalendarFile(mw->mw_dir, dp->d_name);
if (inSubDir == False)
mw->mw_useTopDir = True;
}
break;
case 11: /* xc<dd><Mon><Year> ?? */
if (dp->d_name[0] == 'x' &&
dp->d_name[1] == 'c' &&
isdigit(dp->d_name[2]) &&
isdigit(dp->d_name[3]) &&
dp->d_name[4] == monthbuf[0] &&
dp->d_name[5] == monthbuf[1] &&
dp->d_name[6] == monthbuf[2] &&
dp->d_name[7] == yearbuf[0] &&
dp->d_name[8] == yearbuf[1] &&
dp->d_name[9] == yearbuf[2] &&
dp->d_name[10] == yearbuf[3]) {
da = (dp->d_name[2] - '0') * 10 + (dp->d_name[3] - '0');
mw->mw_have[da] = ReadCalendarFile(mw->mw_dir, dp->d_name);
if (inSubDir == False)
mw->mw_useTopDir = True;
}
break;
}
}
closedir(dirp);
return (&mw->mw_me);
}
/*
* Get the entry for the weekly strip
* Files are xw<Day> in the Calendar directory
*/
MonthEntry *
GetWeeklyEntry()
{
MeWrap *mw;
int da;
DIR *dirp;
struct dirent *dp;
if ((mw = MeWrapSearch(0, 0)) == NULL)
mw = NewMeWrap(0, 0);
mw->mw_me.me_type = ME_WEEKLY;
if (!FoundCalendarDir)
return (&mw->mw_me);
if ((dirp = opendir(".")) == NULL)
Fatal("Cannot open directory: %s", MapStem);
for (da = 0; da < 7; da++)
if (mw->mw_have[da]) {
XtFree(mw->mw_have[da]);
mw->mw_have[da] = NULL;
}
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_name[0] == 'x' &&
dp->d_name[1] == 'w' &&
((da = DayMatch(&dp->d_name[2])) != -1)) {
mw->mw_have[da] = ReadCalendarFile(NULL, dp->d_name);
}
}
closedir(dirp);
return (&mw->mw_me);
}
/*
* Look for a short name match with a day
*/
static int
DayMatch(day)
String day;
{
register int i;
for (i = 0; i < 7; i++)
if (strcmp(day, appResources.sday[i]) == 0)
return (i);
return (-1);
}
/*
* create a new MapWrap area
*/
static MeWrap *
NewMeWrap(yr, mo)
Cardinal yr;
Cardinal mo;
{
register MeWrap *mw;
static MeWrap zerow;
mw = (MeWrap *) XtMalloc(sizeof(MeWrap));
*mw = zerow;
if (WrapEnd)
WrapEnd->mw_next = mw;
WrapEnd = mw;
if (WrapBase == NULL)
WrapBase = mw;
mw->mw_year = yr;
mw->mw_month = mo;
mw->mw_useTopDir = False;
return (mw);
}
/*
* Search the MapWrap list for a year
*/
static MeWrap *
MeWrapSearch(yr, mo)
Cardinal yr;
Cardinal mo;
{
register MeWrap *mw;
if (WrapBase)
for (mw = WrapBase; mw; mw = mw->mw_next)
if (yr == mw->mw_year && mo == mw->mw_month)
return (mw);
return (NULL);
}
/*
* Register an instance of a month Return a pointer to an instance structure
* so it can be filled in by the caller
*/
Instance *
RegisterMonth(yr, mo, w)
Cardinal yr;
Cardinal mo;
Widget w;
{
register MeWrap *mw;
register Instance *ins;
if ((mw = MeWrapSearch(yr, mo)) == NULL)
mw = NewMeWrap(yr, mo);
ins = (Instance *) XtMalloc(sizeof(Instance));
ins->i_next = mw->mw_list;
mw->mw_list = ins;
ins->i_w = w;
callbacks[0].callback = DeRegisterMonth;
#ifdef LONG_IS_32_BITS
callbacks[0].closure = (void *) DatePack(0, 0, mo, yr);
#else
callbacks[0].closure = (void *) DatePack(mo, yr);
#endif
XtAddCallbacks(w, XtNdestroyCallback, callbacks);
return (ins);
}
/*
* Return the head of an instance list - given a date
*/
Instance *
FindInstanceList(da)
Date *da;
{
register MeWrap *mw;
if ((mw = MeWrapSearch(da->year, da->month)) == NULL)
return (NULL);
return (mw->mw_list);
}
/*
* Delete an instance
*/
/* ARGSUSED */
static void
DeRegisterMonth(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
Cardinal yr, mo;
register Instance *ins, *inlast;
register MeWrap *mw;
yr = YrUnpack((Cardinal) closure);
mo = MoUnpack((Cardinal) closure);
if ((mw = MeWrapSearch(yr, mo)) == NULL)
return;
for (ins = mw->mw_list, inlast = NULL;
ins;
inlast = ins, ins = ins->i_next) {
if (ins->i_w == w) {
if (inlast)
inlast->i_next = ins->i_next;
else
mw->mw_list = ins->i_next;
XtFree((char *) ins);
return;
}
inlast = ins;
}
}
/*
* Read a calendar file into memory into a string
* if the file is zero length then unlink and return NULL
*/
String
ReadCalendarFile(dir, file)
String dir;
String file;
{
char fname[256];
int fd;
String destb;
struct stat fsb;
if (dir) {
(void) sprintf(fname, "%s/%s", dir, file);
file = fname;
}
if ((fd = open(file, 0)) < 0) {
if (MyCalendar)
Fatal("Cannot open: %s for reading", file);
else
return(XtNewString(appResources.private));
}
if (fstat(fd, &fsb) < 0)
Fatal("Cannot fstat %s", file);
if (fsb.st_size == 0) {
if (MyCalendar)
(void) unlink(file);
close(fd);
return (NULL);
}
destb = (String) XtMalloc(fsb.st_size + 1);
if (read(fd, (String) destb, fsb.st_size) != fsb.st_size)
Fatal("Read error on %s", file);
close(fd);
destb[fsb.st_size] = '\0';
return (destb);
}
/*
* Check to see if we should create the top directory
*/
Boolean
NeedTop()
{
if (!FoundCalendarDir) {
if (mkdir(MapStem, 0777) == -1) {
XBell(XtDisplay(toplevel), 0);
fprintf(stderr, "xcalim: Could not create: %s directory.\n",
MapStem);
perror("xcalim: mkdir");
fflush(stderr);
return (False);
}
if (chdir(MapStem) < 0) {
XBell(XtDisplay(toplevel), 0);
fprintf(stderr, "xcalim: Could not chdir into %s.\n", MapStem);
perror("xcalim: chdir");
fflush(stderr);
return (False);
}
FoundCalendarDir = True;
}
return (True);
}
/*
* Write a calendar file creating any directories which are needed
* Return True if OK
*/
static Boolean
WriteCalendarFile(mw, day, contents)
register MeWrap *mw;
Cardinal day;
char *contents;
{
int fd;
Cardinal len;
char fname[256];
char cname[16];
len = strlen(contents);
if (len == 0) {
DeleteCalendarFile(mw, day);
return (True);
}
if (!NeedTop())
return (False);
/*
* So that looks OK
* We can now create the output file. However, we
* would like to put any new data into subdirectories named for the
* year unless we are compatible with xcalendar
*/
fname[0] = '\0';
if (appResources.calCompat == False && mw->mw_useTopDir == False) {
/*
* we have no data in the top directory
* so let's create the directory name
*/
(void) sprintf(fname, "xy%d", mw->mw_year);
if (access(fname, F_OK) < 0) {
if (mkdir(fname, 0777) < 0) {
XBell(XtDisplay(toplevel), 0);
fprintf(stderr, "xcalim: Could not create: %s/%s directory.\n",
MapStem, fname);
perror("xcalim: mkdir ");
fflush(stderr);
return (False);
}
}
strcat(fname, "/");
}
/*
* Whew - it looks as if we can now write the file
*/
(void) sprintf(cname, "xc%d%s%d", day,
appResources.smon[mw->mw_month],
mw->mw_year);
strcat(fname, cname);
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, contents, 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);
/*
* tickle the alarm system if we have altered `today'
*/
if (today.day == day && today.month == mw->mw_month &&
today.year == mw->mw_year) AlarmFilePoll(NULL);
return (True);
}
static void
DeleteCalendarFile(mw, day)
register MeWrap *mw;
Cardinal day;
{
char fname[256];
char cname[16];
fname[0] = '\0';
if (mw->mw_me.me_type == ME_WEEKLY) {
(void) sprintf(cname, "xw%s", appResources.sday[day]);
} else {
if (mw->mw_useTopDir == False) {
/* we have no data in the top directory
so let's create the directory name */
(void) sprintf(fname, "xy%d", mw->mw_year);
if (access(fname, F_OK) < 0) return;
strcat(fname, "/");
}
(void) sprintf(cname, "xc%d%s%d", day,
appResources.smon[mw->mw_month],
mw->mw_year);
}
strcat(fname, cname);
unlink(fname);
/* * tickle the alarm system if we have altered `today' */
if (today.day == day && today.month == mw->mw_month &&
today.year == mw->mw_year) AlarmFilePoll(NULL);
}
/*
* Write daily file out
*/
static Boolean
WriteWeeklyFile(mw, day, contents)
register MeWrap *mw;
Cardinal day;
char *contents;
{
int fd;
Cardinal len;
char *fname;
fname = MakeWeeklyName(day);
len = strlen(contents);
if (len == 0)
(void) unlink(fname);
else {
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, contents, 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);
}
/* * tickle the alarm system if we have altered `today' */
if (today.wday == day)
AlarmFilePoll(NULL);
return (True);
}
/*
* Create a standard weekly file name
*/
String
MakeWeeklyName(day)
Cardinal day;
{
static char fname[16];
(void) sprintf(fname, "xw%s", appResources.sday[day]);
return (fname);
}
/*
* Get the contents of the current Weekly file if any
*/
String
GetWeeklyFile(day)
Cardinal day;
{
char *fname;
if (FoundCalendarDir == False)
return (NULL);
fname = MakeWeeklyName(day);
if (access(fname, F_OK) < 0)
return (NULL);
return (ReadCalendarFile(NULL, fname));
}
/*
* Start up an editor window from the callback
* Pass the calling widget so we can change its contents after the edit
*/
/* ARGSUSED */
void
StartEditing(w, da, but)
Widget w;
Date *da;
Widget but;
{
register MeWrap *mw;
register EditLine *ed;
static EditLine zeroe;
if ((mw = MeWrapSearch(da->year, da->month)) == NULL)
mw = NewMeWrap(da->year, da->month); /* shouldn't happen */
/*
* see if we are editing this day
*/
if (EditCheck(w, mw, da))
return;
/*
* Things are looking OK
* Create a new editing record
*/
ed = (EditLine *) XtMalloc(sizeof(EditLine));
*ed = zeroe;
ed->ed_day = da->day;
ed->ed_meWrap = mw; /* help for unlinking */
/*
* Do we have a string now
*/
if (mw->mw_have[da->day]) {
ed->ed_size = appResources.textbufsz + strlen(mw->mw_have[da->day]) + 1;
ed->ed_data = XtMalloc(ed->ed_size);
strcpy(ed->ed_data, mw->mw_have[da->day]);
} else {
ed->ed_data = XtMalloc(ed->ed_size = appResources.textbufsz);
*ed->ed_data = '\0';
}
/*
* put the record into the list
*/
ed->ed_next = mw->mw_ed;
mw->mw_ed = ed;
/*
* We fiddle with the source widget too
* Desensitise visible source widgets
* If the user starts up another strip for this month then
* the NoEditIsPossible() code above copes
*/
SetAllButtons(mw->mw_list, da->day, False);
/*
* Now we should start up the edit window for this month
*/
StartDayEditor(mw, da, but);
}
/*
* See if we are editing a day already
* complain and return 1 if we are
* return 0 if not
*/
static int
EditCheck(w, mw, da)
Widget w;
MeWrap *mw;
Date *da;
{
register EditLine *ed;
/* * see if we are already editing this day */
for (ed = mw->mw_ed; ed; ed = ed->ed_next) {
if (ed->ed_day == da->day) { /* we are! */
/* Complain via a popup */
switch (StripType(mw)) {
case ME_MONTHLY:
NoEditIsPossible(w, da);
break;
case ME_WEEKLY:
NoDayEditIsPossible(w, da);
break;
}
return 1;
}
}
return 0;
}
/* * Set all the relevant buttons in a widget list to off or on */
static void
SetAllButtons(ins, day, val)
Instance *ins;
Cardinal day;
Boolean val;
{
for (; ins; ins = ins->i_next) XtSetSensitive(ins->i_day_info[day], val);
}
/* * Start up a day editor Modelled on xcalendar.c */
void
StartDayEditor(mw, da, but)
register MeWrap *mw;
register Date *da;
Widget but;
{
register EditLine *ed = mw->mw_ed; /* top of the list is ours */
Widget lw, et;
Widget frame;
char buf[BUFSIZ];
void EditHelp();
ed->ed_popup = XtVaCreatePopupShell("edit",
topLevelShellWidgetClass, toplevel, NULL);
if (but && XtIsSubclass(but, commandWidgetClass))
ButtonOff(but, ed->ed_popup);
/* Create the title line - which is a form containing buttons and
a date label */
et = XtVaCreateManagedWidget("panel", panedWidgetClass,
ed->ed_popup, NULL);
frame = XtVaCreateManagedWidget("title", formWidgetClass, et,
XtNshowGrip, False,
XtNskipAdjust, True,
XtNdefaultDistance, 1,
NULL);
/* * Take label "quit" from resources */
callbacks[0].callback = FinishEditing;
callbacks[0].closure = (void *) ed;
ed->ed_quit = XtVaCreateWidget("quit", commandWidgetClass, frame,
XtNcallback, callbacks,
NULL);
/* * Take label "save" from resources */
if (MyCalendar) {
callbacks[0].callback = SaveEdits;
callbacks[0].closure = (void *) ed;
lw = ed->ed_save = XtVaCreateManagedWidget("save",
commandWidgetClass, frame,
XtNcallback, callbacks,
XtNfromHoriz, NULL,
XtNleft, XtChainLeft,
XtNright, XtChainLeft,
XtNsensitive, False,
NULL);
callbacks[0].callback = DeleteEditFile;
callbacks[0].closure = (void *) ed;
lw = ed->ed_delete = XtVaCreateManagedWidget("delete",
commandWidgetClass, frame,
XtNcallback, callbacks,
XtNfromHoriz, ed->ed_save,
XtNleft, XtChainLeft,
XtNright, XtChainLeft,
XtNsensitive, True,
NULL);
}
if (appResources.giveHelp) {
/* * Take label "help" from resources */
callbacks[0].callback = EditHelp;
callbacks[0].closure = (void *) 0;
lw = XtVaCreateManagedWidget("help",
commandWidgetClass, frame,
XtNcallback, callbacks,
XtNfromHoriz, lw,
XtNleft, XtChainLeft,
XtNright, XtChainLeft,
NULL);
}
switch (StripType(mw)) {
case ME_MONTHLY:
FmtDate(da, buf, sizeof buf, appResources.editfmt);
break;
case ME_WEEKLY:
(void) strcpy(buf, appResources.day[da->day]);
break;
}
lw = XtVaCreateManagedWidget("date", labelWidgetClass, frame,
XtNlabel, buf,
XtNborderWidth, 0,
XtNfromHoriz, lw,
XtNfromVert, NULL,
XtNvertDistance, 2,
XtNleft, XtChainLeft,
XtNright, XtChainRight,
NULL);
/*
* The text widget is in the pane below
* The Scroll Attributes are controlled from the application
* defaults file
*/
callbacks[0].callback = TextChanged;
callbacks[0].closure = (void *) ed;
ed->ed_text = XtVaCreateManagedWidget("text",
asciiTextWidgetClass, et,
XtNshowGrip, False,
XtNstring, ed->ed_data,
XtNeditType, XawtextEdit,
XtNlength, ed->ed_size,
XtNuseStringInPlace, True,
XtNcallback, callbacks,
NULL);
XtOverrideTranslations(ed->ed_text, XtParseTranslationTable(editTransl));
XawTextSetInsertionPoint(ed->ed_text, XawTextLastPosition(ed->ed_text));
XtOverrideTranslations(ed->ed_popup,
XtParseTranslationTable("<Message>WM_PROTOCOLS: PressAButton(2)"));
XtPopup(ed->ed_popup, XtGrabNone);
XtSetKeyboardFocus(ed->ed_popup, ed->ed_text);
(void) XSetWMProtocols(XtDisplay(ed->ed_popup), XtWindow(ed->ed_popup),
&delWin, 1);
}
/* Callback for text widget. This gets called before the string is updated. */
/* ARGSUSED */
static void
TextChanged(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
register EditLine *ed = (EditLine *) closure;
if (MyCalendar)
XtSetSensitive(ed->ed_save, True);
}
/* ARGSUSED */
static void
SaveEdits(w, closure, call_data)
Widget w;
void *closure;
void *call_data;
{
register EditLine *ed = (EditLine *) closure;
register MeWrap *mw;
register Cardinal day;
mw = ed->ed_meWrap;
day = ed->ed_day;
switch (StripType(mw)) {
case ME_MONTHLY:
if (WriteCalendarFile(mw, day, ed->ed_data) == False)
return;
break;
case ME_WEEKLY:
if (WriteWeeklyFile(mw, day, ed->ed_data) == False)
return;
break;
}
/* * Otherwise change the displayed string */
if (mw->mw_have[day])
XtFree(mw->mw_have[day]);
mw->mw_have[day] = XtMalloc(strlen(ed->ed_data) + 1);
strcpy(mw->mw_have[day], ed->ed_data);
/* * Update the visual image */
UpdateDayDetails(mw, day);
XtSetSensitive(ed->ed_save, False);
}
static void
DeleteEditFile(w, closure, call_data)
Widget w;
void * closure;
void * call_data;
{
register EditLine *ed = (EditLine *) closure;
register MeWrap *mw;
register Cardinal day;
mw = ed->ed_meWrap;
day = ed->ed_day;
DeleteCalendarFile(mw, day);
if (mw->mw_have[day]) XtFree(mw->mw_have[day]);
mw->mw_have[day] = XtMalloc(1);
strcpy(mw->mw_have[day], "\0");
UpdateDayDetails(mw, day);