This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xvdir.c
2944 lines (2337 loc) · 75.6 KB
/
xvdir.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
/*
* xvdir.c - Directory changin', file i/o dialog box
*
* callable functions:
*
* CreateDirW(geom,bwidth)- creates the dirW window. Doesn't map it.
* ResizeDirW() - change the size of the dirW window
* DirBox(vis) - random processing based on value of 'vis'
* maps/unmaps window, etc.
* ClickDirW() - handles mouse clicks in DirW
* DbouleClickDirW() - handles mouse double-clicks in DirW
* DragDirW() - handles mouse movement with mouse down in DirW
* LoadCurrentDirectory() - loads up current dir information for dirW
* GetDirPath() - returns path that 'dirW' is looking at
* DoSave() - calls appropriate save routines
* SetDirFName() - sets the 'load/save-as' filename and default
* GetDirFName() - gets the 'load/save-as' filename (no path)
* SetDirSaveMode() - sets default format/color settings
*
* InitPoll() - called whenever a file is first loaded
* CheckPoll(int) - checks to see whether we should reload
*/
#include "copyright.h"
#define NEEDSTIME /* for CheckPoll */
#define NEEDSDIR
#include "xv.h"
#include "bits/d_load"
#include "bits/d_save"
#ifndef VMS
#include <pwd.h> /* for getpwnam() prototype and passwd struct */
#endif
#define DEF_DIRWIDE 350 /* initial size of directory window */
#define DEF_DIRHIGH 400
#define MIN_DIRWIDE 330
#define MIN_DIRHIGH 300
#define DLIST_X 10 /* left of directory list box */
#define DLIST_Y 30 /* top of directory list box */
#define DNAM_X 80 /* left of filename box */
#define NLINES 15 /* initial # of lines in list control (keep odd) */
#define BUTTW 80 /* width of buttons */
#define BUTTH 24 /* height of buttons */
#define MBDIRPAD 10 /* space around directory name in menu button */
#define DNAMMORE 3 /* width of "more off screen" markers */
#define MAXDEEP 30 /* max num of directories in cwd path */
#define MAXFNLEN 256 /* max len of filename being entered */
#define FMTLABEL "Format:" /* label shown next to fmtMB */
#define COLLABEL "Colors:" /* label shown next to colMB */
#define FMTWIDE 150 /* width of fmtMB */
#define COLWIDE 150 /* width of colMB */
/* NOTE: make sure these match up with F_* definitions in xv.h */
static const char *saveColors[] = { "Full Color",
"Greyscale",
"B/W Dithered",
"Reduced Color" };
static const char *saveFormats[] = {
#ifdef HAVE_PNG
"PNG",
#endif
#ifdef HAVE_JPEG
"JPEG",
#endif
#ifdef HAVE_JP2K
"JPEG 2000",
"JP2",
#endif
"GIF",
#ifdef HAVE_WEBP
"WEBP",
#endif
#ifdef HAVE_TIFF
"TIFF",
#endif
"PostScript",
"PBM/PGM/PPM (raw)",
"PBM/PGM/PPM (ascii)",
"X11 Bitmap",
"XPM",
"BMP",
"Sun Rasterfile",
"IRIS RGB",
"Targa (24-bit)",
"FITS",
"PM",
"Spectrum SCREEN$", /* [JCE] */
"G3 fax", /* [JPD] and [DEG] */
"WBMP",
#ifdef HAVE_MAG
"MAG",
#endif
#ifdef HAVE_PIC
"PIC",
#endif
#ifdef HAVE_MAKI
"MAKI (640x400 only)",
#endif
#ifdef HAVE_PI
"PI",
#endif
#ifdef HAVE_PIC2
"PIC2",
#endif
#ifdef HAVE_MGCSFX
"MgcSfx",
#endif
MBSEP,
"Filename List" };
#ifdef HAVE_PIC2
extern int PIC2SaveParams PARM((char *, int));
#endif
static int DirHigh PARM((void));
static int DirWide PARM((void));
static int roomForLines PARM((int));
static int DNamWide PARM((void));
static int DNamY PARM((void));
static void arrangeElements PARM((int));
static int posOfCoordinate PARM((int));
static void moveInsertionPoint PARM((int));
static void unselect PARM((void));
static void removeSelectedRange PARM((void));
static void pasteIntoBox PARM((const char *text));
static void RedrawDList PARM((int, SCRL *));
static void changedDirMB PARM((int));
static int updatePrimarySelection PARM((void));
static int updateClipboardSelection PARM((void));
static int dnamcmp PARM((const void *, const void *));
static int FNameCdable PARM((void));
static void loadCWD PARM((void));
#ifdef FOO
static int cd_able PARM((char *));
#endif
static void scrollToFileName PARM((void));
static void setFName PARM((const char *));
static void showFName PARM((void));
static void changeSuffix PARM((void));
static int autoComplete PARM((void));
static byte *handleBWandReduced PARM((byte *, int,int,int, int, int *,
byte **, byte **, byte **));
static byte *handleNormSel PARM((int *, int *, int *, int *));
static int dir_h; /* current height of dir window */
static char *fnames[MAXNAMES];
static int numfnames = 0, ndirs = 0;
static char path[MAXPATHLEN+1]; /* '/' terminated */
static char loadpath[MAXPATHLEN+1]; /* '/' terminated */
static char savepath[MAXPATHLEN+1]; /* '/' terminated */
static char *dirs[MAXDEEP]; /* list of directory names */
static const char *dirMBlist[MAXDEEP]; /* list of dir names in right order */
static char *lastdir; /* name of the directory we're in */
static char filename[MAXFNLEN+100]; /* filename being entered */
static char deffname[MAXFNLEN+100]; /* default filename */
static int savemode; /* if 0 'load box', if 1 'save box' */
static int curPos; /* insertion point in textedit filename */
static int stPos, enPos; /* start and end of visible textedit filename */
static int selPos, selLen; /* start and length of selected region of textedit filename */
static MBUTT dirMB; /* popup path menu */
static MBUTT fmtMB; /* 'format' menu button (Save only) */
static MBUTT colMB; /* 'colors' menu button (Save only) */
static Pixmap d_loadPix, d_savePix;
static int haveoldinfo = 0;
static int oldformat, oldcolors;
static char oldfname[MAXFNLEN+100];
/* the name of the file actually opened. (the temp file if we are piping) */
static char outFName[256];
static int dopipe;
/***************************************************/
static int DirHigh()
{
return dir_h;
}
/***************************************************/
static int DirWide()
{
return dList.w + 113;
}
/***************************************************/
static int roomForLines(height)
int height;
{
int num;
num = (height - (dList.y + 66))/LINEHIGH;
if (num < 1)
num = 1;
if (num > MAXNAMES)
num = MAXNAMES;
return num;
}
/***************************************************/
void ResizeDirW(w, h)
int w, h;
{
int nlines;
dir_h = h;
nlines = roomForLines(h);
LSResize(&dList, w - 113, LINEHIGH*nlines, nlines);
arrangeElements(savemode);
showFName();
}
/***************************************************/
static int DNamWide()
{
return DirWide() - 100;
}
/***************************************************/
static int DNamY()
{
return DirHigh() - (10 + 2 + LINEHIGH+5);
}
/***************************************************/
void CreateDirW()
{
int nlines;
path[0] = '\0';
xv_getwd(loadpath, sizeof(loadpath));
xv_getwd(savepath, sizeof(savepath));
dir_h = DEF_DIRHIGH;
dirW = CreateFlexWindow("", "XVdir", NULL, DEF_DIRWIDE, DEF_DIRHIGH,
infofg, infobg, FALSE, FALSE, FALSE);
if (!dirW) FatalError("couldn't create 'directory' window!");
SetMinSizeWindow(dirW, MIN_DIRWIDE, MIN_DIRHIGH);
nlines = roomForLines(dir_h);
LSCreate(&dList, dirW, DLIST_X, 5 + 3*(6+LINEHIGH) + 6, DEF_DIRWIDE - 113,
LINEHIGH*nlines, nlines, fnames, numfnames, infofg, infobg,
hicol, locol, RedrawDList, TRUE, FALSE);
dnamW = XCreateSimpleWindow(theDisp, dirW, 0, 0, 1, 1,
1, infofg, infobg);
if (!dnamW) FatalError("can't create name window");
XSelectInput(theDisp, dnamW, ExposureMask);
/* create checkboxes */
CBCreate(&browseCB, dirW, 0, 0,
"Browse", infofg, infobg, hicol,locol);
CBCreate(&savenormCB, dirW, 0, 0,
"Normal Size", infofg, infobg, hicol,locol);
CBCreate(&saveselCB, dirW, 0, 0,
"Selected Area", infofg, infobg, hicol,locol);
/* y-coordinates get filled in when window is opened */
BTCreate(&dbut[S_BOK], dirW, 0, 0, BUTTW, BUTTH,
"Ok", infofg, infobg,hicol,locol);
BTCreate(&dbut[S_BCANC], dirW, 0, 0, BUTTW, BUTTH,
"Cancel", infofg,infobg,hicol,locol);
BTCreate(&dbut[S_BRESCAN], dirW, 0, 0, BUTTW, BUTTH,
"Rescan", infofg,infobg,hicol,locol);
BTCreate(&dbut[S_BOLDSET], dirW, 0, 0, BUTTW, BUTTH,
"Prev Set", infofg,infobg,hicol,locol);
BTCreate(&dbut[S_BOLDNAM], dirW, 0, 0, BUTTW, BUTTH,
"Prev Name", infofg,infobg,hicol,locol);
SetDirFName("");
XMapSubwindows(theDisp, dirW);
numfnames = 0;
/*
* create MBUTTs *after* calling XMapSubWindows() to keep popup unmapped
*/
MBCreate(&dirMB, dirW, 0, 0, 1, 1,
NULL, NULL, 0,
infofg,infobg,hicol,locol);
MBCreate(&fmtMB, dirW, 0, 0, 1, 1,
NULL, saveFormats, F_MAXFMTS,
infofg,infobg,hicol,locol);
fmtMB.hascheck = 1;
MBSelect(&fmtMB, 0);
MBCreate(&colMB, dirW, 0, 0, 1, 1,
NULL, saveColors, F_MAXCOLORS,
infofg,infobg,hicol,locol);
colMB.hascheck = 1;
MBSelect(&colMB, 0);
d_loadPix = XCreatePixmapFromBitmapData(theDisp, dirW,
(char *) d_load_bits, d_load_width, d_load_height,
infofg, infobg, dispDEEP);
d_savePix = XCreatePixmapFromBitmapData(theDisp, dirW,
(char *) d_save_bits, d_save_width, d_save_height,
infofg, infobg, dispDEEP);
}
/***************************************************/
void DirBox(mode)
int mode;
{
static int firstclose = 1;
if (mode == 0) {
if (savemode) strcpy(savepath, path);
else strcpy(loadpath, path);
if (firstclose) {
strcpy(loadpath, path);
strcpy(savepath, path);
firstclose = 0;
}
XUnmapWindow(theDisp, dirW); /* close */
}
else {
if (mode == BLOAD) {
savemode = FALSE;
strcpy(path, loadpath);
WaitCursor(); LoadCurrentDirectory(); SetCursors(-1);
XStoreName(theDisp, dirW, "xv load");
XSetIconName(theDisp, dirW, "xv load");
dbut[S_BLOADALL].str = "Load All";
BTSetActive(&dbut[S_BLOADALL], 1);
arrangeElements(savemode);
MBSetActive(&fmtMB, 0);
MBSetActive(&colMB, 0);
}
else if (mode == BSAVE) {
savemode = TRUE;
strcpy(path, savepath);
WaitCursor(); LoadCurrentDirectory(); SetCursors(-1);
XStoreName(theDisp, dirW, "xv save");
XSetIconName(theDisp, dirW, "xv save");
dbut[S_BOLDSET].str = "Prev Set";
arrangeElements(savemode);
BTSetActive(&dbut[S_BOLDSET], haveoldinfo);
BTSetActive(&dbut[S_BOLDNAM], haveoldinfo);
CBSetActive(&saveselCB, HaveSelection());
MBSetActive(&fmtMB, 1);
if (MBWhich(&fmtMB) == F_FILELIST) {
MBSetActive(&colMB, 0);
CBSetActive(&savenormCB, 0);
}
else {
MBSetActive(&colMB, 1);
CBSetActive(&savenormCB, 1);
}
}
CenterMapFlexWindow(dirW,
dbut[S_BOK].x + BUTTW/2, dbut[S_BOK].y + BUTTH/2,
DirWide(), DirHigh(), FALSE);
scrollToFileName();
}
dirUp = mode;
BTSetActive(&but[BLOAD], !dirUp);
BTSetActive(&but[BSAVE], !dirUp);
}
/***************************************************/
static void arrangeElements(savemode)
int savemode;
{
int i, nbts, ngaps, szdiff, top, gap;
/* buttons */
nbts = !savemode ? S_LOAD_NBUTTS : S_NBUTTS;
ngaps = nbts-1;
szdiff = dList.h - (nbts * BUTTH);
gap = szdiff / ngaps;
if (gap>16) {
gap = 16;
top = dList.y + (dList.h - (nbts*BUTTH) - (ngaps*gap))/2;
for (i=0; i<nbts; i++)
BTMove(&dbut[i],
dList.x + dList.w + 12,
top + i*(BUTTH+gap));
}
else {
for (i=0; i<nbts; i++)
BTMove(&dbut[i],
dList.x + dList.w + 12,
dList.y + ((dList.h-BUTTH)*i) / ngaps);
}
/* checkboxes */
CBMove(&browseCB, DirWide()/2, dList.y + (int) dList.h + 6);
CBMove(&savenormCB, 220, dList.y + (int) dList.h + 6);
CBMove(&saveselCB, 80, dList.y + (int) dList.h + 6);
/* filename box */
XMoveResizeWindow(theDisp, dnamW,
DNAM_X, DNamY(),
(u_int) DNamWide()+2*DNAMMORE, (u_int) LINEHIGH+5);
/* menu buttons */
MBChange(&dirMB,
dList.x + dList.w/2 - dirMB.w/2,
dList.y - (LINEHIGH+6),
(u_int) dirMB.w, (u_int) LINEHIGH);
MBChange(&fmtMB,
DirWide()-FMTWIDE-10, 5,
(u_int) FMTWIDE, (u_int) LINEHIGH);
MBChange(&colMB,
DirWide()-COLWIDE-10, 5+LINEHIGH+6,
(u_int) COLWIDE, (u_int) LINEHIGH);
}
/***************************************************/
void RedrawDirW(x, y, w, h)
int x, y, w, h;
{
int i, ypos, txtw;
char foo[30];
const char *str;
if (dList.nstr==1)
strcpy(foo, "1 file");
else
sprintf(foo, "%d files", dList.nstr);
ypos = dList.y + dList.h + 8 + ASCENT;
XSetForeground(theDisp, theGC, infobg);
XFillRectangle(theDisp, dirW, theGC, 10, ypos-ASCENT,
(u_int) DirWide(), (u_int) CHIGH);
XSetForeground(theDisp, theGC, infofg);
DrawString(dirW, 10, ypos, foo);
if (dirUp == BLOAD)
str = "Load file:";
else
str = "Save file:";
DrawString(dirW, 10, DNamY() + 1 + ASCENT + 3, str);
/* draw dividing line */
XSetForeground(theDisp, theGC, infofg);
XDrawLine(theDisp, dirW, theGC, 0, dirMB.y-6, DirWide(), dirMB.y-6);
if (ctrlColor) {
XSetForeground(theDisp, theGC, locol);
XDrawLine(theDisp, dirW, theGC, 0, dirMB.y-5, DirWide(), dirMB.y-5);
XSetForeground(theDisp, theGC, hicol);
}
XDrawLine(theDisp, dirW, theGC, 0, dirMB.y-4, DirWide(), dirMB.y-4);
for (i=0; i<(savemode ? S_NBUTTS : S_LOAD_NBUTTS); i++) BTRedraw(&dbut[i]);
MBRedraw(&dirMB);
MBRedraw(&fmtMB);
MBRedraw(&colMB);
XSetForeground(theDisp, theGC, infofg);
XSetBackground(theDisp, theGC, infobg);
txtw = StringWidth(FMTLABEL);
if (StringWidth(COLLABEL) > txtw) txtw = StringWidth(COLLABEL);
if (!savemode) {
XCopyArea(theDisp, d_loadPix, dirW, theGC, 0,0,d_load_width,d_load_height,
10, (dirMB.y-6)/2 - d_load_height/2);
XSetFillStyle(theDisp, theGC, FillStippled);
XSetStipple(theDisp, theGC, dimStip);
DrawString(dirW, fmtMB.x-6-txtw, 5+3+ASCENT, FMTLABEL);
DrawString(dirW, fmtMB.x-6-txtw, 5+3+ASCENT + (LINEHIGH+6), COLLABEL);
XSetFillStyle(theDisp,theGC,FillSolid);
CBRedraw(&browseCB);
}
else {
XCopyArea(theDisp, d_savePix, dirW, theGC, 0,0,d_save_width,d_save_height,
10, (dirMB.y-6)/2 - d_save_height/2);
XSetForeground(theDisp, theGC, infofg);
DrawString(dirW, fmtMB.x-6-txtw, 5+3+ASCENT, FMTLABEL);
DrawString(dirW, fmtMB.x-6-txtw, 5+3+ASCENT + (LINEHIGH+6), COLLABEL);
CBRedraw(&savenormCB);
CBRedraw(&saveselCB);
}
}
/***************************************************/
int ClickDirW(x, y, button)
int x, y;
int button;
{
BUTT *bp;
int bnum,i,maxbut,v;
char buf[1024];
switch (button) {
case 1:
if (savemode) { /* check format/colors MBUTTS */
i = v = 0;
if (MBClick(&fmtMB, x,y) && (v=MBTrack(&fmtMB))>=0) i=1;
else if (MBClick(&colMB, x,y) && (v=MBTrack(&colMB))>=0) i=2;
if (i) { /* changed one of them */
if (i==1) SetDirSaveMode(F_FORMAT, v);
else SetDirSaveMode(F_COLORS, v);
changeSuffix();
}
}
if (!savemode) { /* LOAD */
if (CBClick(&browseCB,x,y)) CBTrack(&browseCB);
}
else { /* SAVE */
if (CBClick(&savenormCB,x,y)) CBTrack(&savenormCB);
else if (CBClick(&saveselCB,x,y)) CBTrack(&saveselCB);
}
maxbut = (savemode) ? S_NBUTTS : S_LOAD_NBUTTS;
for (bnum=0; bnum<maxbut; bnum++) {
bp = &dbut[bnum];
if (PTINRECT(x, y, bp->x, bp->y, bp->w, bp->h)) break;
}
if (bnum<maxbut && BTTrack(bp)) { /* found one */
if (bnum<S_BOLDSET) return bnum; /* do Ok,Cancel,Rescan in xvevent.c */
if (bnum == S_BOLDSET && savemode && haveoldinfo) {
MBSelect(&fmtMB, oldformat);
MBSelect(&colMB, oldcolors);
changeSuffix();
}
else if (bnum == S_BOLDNAM && savemode && haveoldinfo) {
setFName(oldfname);
}
else if (bnum == S_BLOADALL && !savemode) {
int j, oldnumnames;
char *dname;
oldnumnames = numnames;
for (i=0; i<numfnames && numnames<MAXNAMES; i++) {
if (fnames[i][0] == C_REG || fnames[i][0] == C_EXE) {
sprintf(buf,"%s%s", path, fnames[i]+1);
/* check for dups. Don't add it if it is. */
for (j=0; j<numnames && strcmp(buf,namelist[j]); j++);
if (j==numnames) { /* add to list */
namelist[numnames] = (char *) malloc(strlen(buf)+1);
if (!namelist[numnames]) FatalError("out of memory!\n");
strcpy(namelist[numnames],buf);
dname = namelist[numnames];
/* figure out how much of name can be shown */
if (StringWidth(dname) > (nList.w-10-16)) { /* truncate */
char *tmp;
int prelen = 0;
tmp = dname;
while (1) {
tmp = (char *) index(tmp,'/'); /* find next '/' in buf */
if (!tmp) break;
tmp++; /* move to char following the '/' */
prelen = tmp - dname;
if (StringWidth(tmp) <= (nList.w-10-16)) break; /* cool now */
}
dispnames[numnames] = dname + prelen;
}
else dispnames[numnames] = dname;
numnames++;
}
}
}
if (oldnumnames != numnames) { /* added some */
if (numnames>0) BTSetActive(&but[BDELETE],1);
windowMB.dim[WMB_TEXTVIEW] = (numnames==0);
LSNewData(&nList, dispnames, numnames);
nList.selected = oldnumnames;
curname = oldnumnames - 1;
ActivePrevNext();
ScrollToCurrent(&nList);
DrawCtrlNumFiles();
if (!browseCB.val) DirBox(0);
}
}
}
if (MBClick(&dirMB, x, y)) {
i = MBTrack(&dirMB);
if (i >= 0) changedDirMB(i);
return -1;
}
/* handle clicks inside the filename box */
if (x > DNAM_X &&
x < DNAM_X + DNamWide()+2*DNAMMORE &&
y > DNamY() &&
y < DNamY() + LINEHIGH+5) {
Window dummy_root, dummy_child;
int dummy_rx, dummy_ry;
int wx, wy;
unsigned int mask;
int clkx_start, clkx_now;
int pos_start, pos_now, pos_prev;
/* make coordinates relative to dnamW */
/* left side plus the border plus the space for the "more stuff" sign */
clkx_start = x - (DNAM_X + 1 + DNAMMORE);
pos_start = posOfCoordinate(clkx_start);
clkx_now = clkx_start;
pos_prev = -1;
selPos=selLen = 0;
while (XQueryPointer(theDisp, dirW,
&dummy_root, &dummy_child, &dummy_rx, &dummy_ry,
&wx, &wy, &mask)) {
if (!(mask & Button1Mask)) break; /* button released */
clkx_now = wx - (DNAM_X + 1 + DNAMMORE);
pos_prev = pos_now;
pos_now = posOfCoordinate(clkx_now);
if (pos_prev != pos_now) {
if (pos_start == pos_now) {
selPos=selLen = 0;
curPos = pos_start;
} else if (pos_start < pos_now) {
selPos = pos_start;
selLen = pos_now - pos_start;
} else {
selPos = pos_now;
selLen = pos_start - pos_now;
}
showFName();
}
}
if (selLen > 0)
updatePrimarySelection();
else {
unselect();
moveInsertionPoint(clkx_start);
}
}
break;
case 2:
/* handle clicks inside the filename box */
if (x > DNAM_X &&
x < DNAM_X + DNamWide()+2*DNAMMORE &&
y > DNamY() &&
y < DNamY() + LINEHIGH+5) {
int clkx;
char *text;
/* make coordinates relative to dnamW */
clkx = x - (DNAM_X + 1 + DNAMMORE); /* left side plus the border plus the space for the "more stuff" sign */
moveInsertionPoint(clkx);
text = GetPrimaryText();
if (text != NULL) {
pasteIntoBox(text);
free(text);
}
}
break;
}
return -1;
}
/***************************************************/
int DoubleClickDirW(x, y, button)
int x, y;
int button;
{
/* handle double-clicks inside the filename box */
if (button == 1 &&
x > DNAM_X &&
x < DNAM_X + DNamWide()+2*DNAMMORE &&
y > DNamY() &&
y < DNamY() + LINEHIGH+5) {
SelectAllDirW();
return -1;
}
return ClickDirW(x, y, button);
}
static int posOfCoordinate(clkx)
int clkx;
{
int dx, pos;
for (pos=stPos; pos < enPos; pos++) {
if (XTextWidth(mfinfo, &filename[stPos], pos-stPos) + 1 > clkx)
break;
}
/* if we are more than halfway past this char, put the insertion point after it */
if (pos < enPos) {
dx = clkx - XTextWidth(mfinfo, &filename[stPos], pos-stPos);
if (dx > XTextWidth(mfinfo, &filename[pos], 1)/2)
pos++;
}
return pos;
}
/***************************************************/
static void moveInsertionPoint(clkx)
int clkx;
{
curPos = posOfCoordinate(clkx);
selPos=selLen = 0;
showFName();
}
/***************************************************/
static void unselect()
{
selPos=selLen = 0;
ReleaseSelection(XA_PRIMARY);
}
/***************************************************/
static void removeSelectedRange()
{
if (selLen > 0) {
int len;
len = strlen(filename);
xvbcopy(&filename[selPos + selLen], &filename[selPos], (size_t)(len - (selPos + selLen)));
filename[len - selLen] = '\0';
curPos = selPos;
}
unselect();
}
/***************************************************/
static void pasteIntoBox(text)
const char *text;
{
int len, cleanlen;
int tpos, cpos;
char ch;
char *clean;
removeSelectedRange();
len = strlen(filename);
clean = malloc(strlen(text)+1);
if (!clean) FatalError("out of memory!\n");
cpos = 0;
for (tpos=0; text[tpos]!='\0'; tpos++) {
ch = text[tpos];
/* skip non-printable characters */
if (ch<' ' || ch>='\177') continue;
/* note: only allow 'piped commands' in savemode... */
/* only allow vertbars in 'piped commands', not filenames */
if (ch=='|' && curPos+cpos>0 && !ISPIPE(filename[0])) continue;
/* stop if we fill up the filename array */
if (len + cpos >= MAXFNLEN-1) break;
clean[cpos] = ch;
cpos++;
}
clean[cpos] = '\0';
cleanlen = cpos;
xvbcopy(&filename[curPos], &filename[curPos+cleanlen], (size_t) (len+1-curPos));
xvbcopy(clean, &filename[curPos], cleanlen);
curPos += cleanlen;
free(clean);
scrollToFileName();
showFName();
}
/***************************************************/
void SelectDir(n)
int n;
{
/* called when entry #n in the dir list was selected/double-clicked */
/* if n<0, nothing was double-clicked, but perhaps the selection
has changed. Copy the selection to the filename if a) we're in
the 'load' box, and b) it's not a directory name */
if (n<0) {
if (dList.selected>=0)
setFName(dList.str[dList.selected]+1);
return;
}
/* can just pretend 'enter' was hit on a double click, as the original
click would've copied the string to filename */
if (!DirCheckCD()) FakeButtonPress(&dbut[S_BOK]);
}
/***************************************************/
static void changedDirMB(sel)
int sel;
{
if (sel != 0) { /* changed directories */
char tmppath[MAXPATHLEN+1], *trunc_point;
/* end 'path' by changing trailing '/' (of dir name) to a '\0' */
trunc_point = (dirs[(ndirs-1)-sel + 1] - 1);
*trunc_point = '\0';
if (path[0] == '\0') {
/* special case: if cd to '/', fix path (it's currently "") */
#ifdef apollo /*** Apollo DomainOS uses // as the network root ***/
strcpy(tmppath,"//");
#else
strcpy(tmppath,"/");
#endif
}
else strcpy(tmppath, path);
#ifdef VMS
/*
* The VMS chdir always needs 2 components (device and directory),
* so convert "/device" to "/device/000000" and convert
* "/" to "/XV_Root_Device/000000" (XV_Root_Device will need to be
* a special concealed device setup to provide a list of available
* disks).
*/
if ( ((ndirs-sel) == 2) && (strlen(tmppath) > 1) )
strcat ( tmppath, "/000000" ); /* add root dir for device */
else if ((ndirs-sel) == 1 ) {
strcpy ( tmppath, "/XV_Root_Device/000000" ); /* fake top level */
}
#endif
#ifdef AUTO_EXPAND
if (Chvdir(tmppath)) {
#else
if (chdir(tmppath)) {
#endif
char str[512];
sprintf(str,"Unable to cd to '%s'\n", tmppath);
*trunc_point = '/'; /* restore the path */
MBRedraw(&dirMB);
ErrPopUp(str, "\nWhatever");
}
else {
loadCWD();
}
}
}
/***************************************************/
static void RedrawDList(delta, sptr)
int delta;
SCRL *sptr;
{
LSRedraw(&dList,delta);
}
/***************************************************/
static void loadCWD()
{
/* loads up current-working-directory into load/save list */
xv_getwd(path, sizeof(path));
LoadCurrentDirectory();
}
/***************************************************/
void LoadCurrentDirectory()
{
/* rescans current load/save directory */
DIR *dirp;
int i, j, ftype, mode, changedDir;
struct stat st;
char *dbeg, *dend;
static char oldpath[MAXPATHLEN + 2] = { '\0' };
#ifdef NODIRENT
struct direct *dp;
#else
struct dirent *dp;
#endif
/* get rid of previous file names */
for (i=0; i<numfnames; i++) free(fnames[i]);
numfnames = 0;
/* get rid of old dirMBlist */
for (i=0; i<ndirs; i++) free((char *) dirMBlist[i]);
#ifndef VMS
if (strlen(path) == 0) xv_getwd(path, sizeof(path)); /* no dir, use cwd */
#else
xv_getwd(path, sizeof(path));
#endif
#ifdef AUTO_EXPAND
if (Chvdir(path)) {
#else
if (chdir(path)) {
#endif
ErrPopUp("Current load/save directory seems to have gone away!",
"\nYikes!");
#ifdef apollo
strcpy(path,"//");
#else
strcpy(path,"/");
#endif