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
|
/*-------------------------------------------------------------------------*/
/* filelist.c --- Xcircuit routines for the filelist widget */
/* Copyright (c) 1998 Tim Edwards, Johns Hopkins University */
/*-------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#ifdef linux
#include <dirent.h>
#include <linux/unistd.h>
#define direct dirent
#else
#ifdef SVR4_DIRENT
#include <dirent.h>
#include <unistd.h>
#define direct dirent
#else
#include <stdlib.h>
#include <sys/dir.h>
#endif
#endif
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include "Xw/Xw.h"
#include "Xw/WorkSpace.h"
/*-------------------------------------------------------------------------*/
/* Local includes */
/*-------------------------------------------------------------------------*/
#include "colordefs.h"
#include "xcircuit.h"
/*-------------------------------------------------------------------------*/
/* Local definitions */
/*-------------------------------------------------------------------------*/
#define INITDIRS 10
/*-------------------------------------------------------------------------*/
/* Global Variable definitions */
/*-------------------------------------------------------------------------*/
extern Display *dpy;
extern Window win;
extern Clientdata areastruct;
extern int *appcolors;
extern short popups; /* total number of popup windows */
extern char _STR2[250];
Pixmap flistpix = (Pixmap)NULL; /* For file-selection widget */
short flstart, flfiles, flcurrent;
int flcurwidth;
GC hgc = NULL, sgc = NULL;
char **filenames = NULL;
char *cwdname = NULL;
/*------------------------------------------------------------------------*/
/* Declarations of functions */
/*------------------------------------------------------------------------*/
void newfilelist();
XtCallbackProc showlscroll(), listfiles();
/*-------------------------------------------------------------------------*/
/* Compare two filenames (for use by qsort()) */
/*-------------------------------------------------------------------------*/
int fcompare(a, b)
const void *a, *b;
{
return(strcmp(*((char **)a), *((char **)b)));
}
/*-------------------------------------------------------------------------*/
/* Routines for drawing a box around the currently selected file */
/*-------------------------------------------------------------------------*/
XtEventHandler dragfilebox(w, calldata, event)
Widget w;
caddr_t calldata;
XMotionEvent *event;
{
short filenum;
int charheight = ROWHEIGHT - 6;
int twidth;
Window lwin = XtWindow(w);
filenum = (event->y + (charheight / 2) - 10) / charheight + flstart;
if (filenum < 0) filenum = 0;
else if (filenum >= flfiles) filenum = flfiles - 1;
if (filenum == flcurrent) return;
if (flcurrent >= 0) /* erase previous box */
XDrawRectangle(dpy, lwin, areastruct.gc, 5, -4 + charheight * (flcurrent
- flstart), flcurwidth + 10, charheight);
twidth = XTextWidth(areastruct.textstruct, filenames[filenum],
strlen(filenames[filenum]));
XDrawRectangle(dpy, lwin, areastruct.gc, 5, -4 + charheight * (filenum
- flstart), twidth + 10, charheight);
flcurrent = filenum;
flcurwidth = twidth;
}
/*-------------------------------------------------------------------------*/
/* Begin tracking the cursor position relative to the files in the list */
/*-------------------------------------------------------------------------*/
XtEventHandler startfiletrack(w, calldata, event)
Widget w;
caddr_t calldata;
XCrossingEvent *event;
{
XtAddEventHandler(w, PointerMotionMask, False, (XtEventHandler)dragfilebox, NULL);
XSetFunction(dpy, areastruct.gc, GXcopy);
XSetForeground(dpy, areastruct.gc, AUXCOLOR);
XSetLineAttributes(dpy, areastruct.gc, 1, LineSolid, CapRound, JoinMiter);
/* draw initial box */
flcurrent = -1;
dragfilebox(w, calldata, (XMotionEvent *)event);
XSetFunction(dpy, areastruct.gc, GXxor);
XSetForeground(dpy, areastruct.gc, AUXCOLOR ^ BACKGROUND);
}
/*-------------------------------------------------------------------------*/
/* Stop tracking the cursor and return to default state */
/*-------------------------------------------------------------------------*/
XtEventHandler endfiletrack(w, callstruct, event)
Widget w;
caddr_t callstruct;
XCrossingEvent *event;
{
Window lwin = XtWindow(w);
int charheight = ROWHEIGHT - 6;
XDrawRectangle(dpy, lwin, areastruct.gc, 5, -4 + charheight * (flcurrent
- flstart), flcurwidth + 10, charheight);
XtRemoveEventHandler(w, Button1MotionMask | Button2MotionMask,
False, (XtEventHandler)dragfilebox, NULL);
/* Restore graphics state values */
XSetForeground(dpy, areastruct.gc, areastruct.gccolor);
XSetFunction(dpy, areastruct.gc, areastruct.gctype);
}
/*-------------------------------------------------------------------------*/
/* Make a list of the files in the list widget window */
/*-------------------------------------------------------------------------*/
XtCallbackProc listfiles(w, callstruct, calldata)
Widget w;
caddr_t callstruct;
caddr_t calldata;
{
XGCValues values;
Arg wargs[2];
DIR *cwd;
Window lwin = XtWindow(w);
short allocd = INITDIRS;
short n = 0;
struct direct *dp;
struct stat statbuf;
int pixheight;
int charheight = ROWHEIGHT - 6;
Dimension textwidth, textheight;
XFontStruct *tmpstruct;
if (sgc == NULL) {
tmpstruct = XLoadQueryFont(dpy, "-*-helvetica-medium-r-normal--14-*");
values.foreground = FOREGROUND;
values.font = tmpstruct->fid;
values.function = GXcopy;
sgc = XCreateGC(dpy, lwin, GCForeground | GCFont | GCFunction, &values);
}
XtnSetArg(XtNwidth, &textwidth);
XtnSetArg(XtNheight, &textheight);
XtGetValues(w, wargs, n);
/* Generate a new flistpix pixmap if currently nonexistant */
if (!flistpix) {
/* get list of files in the current directory (cwd) */
if (filenames == NULL)
filenames = (char **) malloc (INITDIRS * sizeof(char *));
flfiles = 0;
if (cwdname == NULL) {
cwdname = (char *) malloc (sizeof(char));
cwdname[0] = '\0';
}
if (cwdname[0] == '\0')
cwd = opendir(".");
else
cwd = opendir(cwdname);
/* If current directory cannot be accessed for some reason, */
/* print "Invalid Directory" to the file list window. */
if (cwd == NULL) {
XSetForeground(dpy, sgc, BACKGROUND);
XFillRectangle(dpy, lwin, sgc, 0, 0, textwidth, textheight);
XSetForeground(dpy, sgc, AUXCOLOR);
XDrawString(dpy, lwin, sgc, 10, textheight / 2,
"(Invalid Directory)", 19);
return;
}
else {
/* write the contents of the current directory into the */
/* array "filenames[]" (except for current directory ".") */
while ((dp = readdir(cwd)) != NULL) {
/* don't put current directory in list */
if (!strcmp(dp->d_name, ".")) continue;
filenames[flfiles] = (char *) malloc ((strlen(dp->d_name) + 2) *
sizeof(char));
strcpy(filenames[flfiles++], dp->d_name);
if (flfiles == allocd) {
allocd += INITDIRS;
filenames = (char **) realloc (filenames, allocd * sizeof(char *));
}
}
}
closedir(cwd);
/* sort the filenames[] array into alphabetical order (like "ls") */
qsort((void *)filenames, (size_t)flfiles, sizeof(char *), fcompare);
pixheight = flfiles * charheight + 20;
flistpix = XCreatePixmap(dpy, win, textwidth, pixheight,
DefaultDepthOfScreen(XtScreen(w)));
/* Write the filenames onto the pixmap */
XSetForeground(dpy, sgc, BACKGROUND);
XFillRectangle(dpy, flistpix, sgc, 0, 0, textwidth, pixheight);
XSetForeground(dpy, sgc, FOREGROUND);
for (n = 0; n < flfiles; n++) {
sprintf(_STR2, "%s%s", cwdname, filenames[n]);
stat(_STR2, &statbuf);
if ((statbuf.st_mode & S_IFDIR) != 0) { /* is a directory */
XSetForeground(dpy, sgc, SELECTCOLOR);
strcat(filenames[n], "/");
}
XDrawString(dpy, flistpix, sgc, 10, 10 + n * charheight,
filenames[n], strlen(filenames[n]));
if ((statbuf.st_mode & S_IFDIR) != 0)
XSetForeground(dpy, sgc, FOREGROUND);
}
}
/* Copy the pixmap of filenames into the file list window */
XSetForeground(dpy, sgc, BACKGROUND);
XFillRectangle(dpy, lwin, sgc, 0, 0, textwidth, textheight);
XCopyArea(dpy, flistpix, lwin, sgc, 0, flstart * charheight,
textwidth, textheight, 0, 0);
}
/*-------------------------------------------------------------------------*/
/* Generate a new pixmap for writing the filelist and set the scrollbar */
/* size accordingly. */
/*-------------------------------------------------------------------------*/
void newfilelist(w, textw)
Widget w, textw;
{
short n;
for (n = 0; n < flfiles; n++)
free(filenames[n]);
free(filenames);
if (flistpix != (Pixmap)NULL) XFreePixmap(dpy, flistpix);
filenames = NULL;
flistpix = (Pixmap)NULL;
flstart = 0;
listfiles(w, NULL, NULL);
showlscroll(XtNameToWidget(XtParent(w), "LScroll"), NULL, NULL);
XwTextClearBuffer(textw);
XwTextInsert(textw, cwdname);
XwTextResize(textw);
}
/*-------------------------------------------------------------------------*/
/* Button press handler for file list window */
/*-------------------------------------------------------------------------*/
XtEventHandler fileselect(w, textw, event)
Widget w;
Widget textw;
XButtonEvent *event;
{
Arg wargs[2];
Window lwin = XtWindow(w);
int pixheight;
Dimension textwidth, textheight;
int charheight = ROWHEIGHT - 6;
short filenum, n = 0;
DIR *cwd;
char *tbuf;
flcurrent = -1;
XtnSetArg(XtNwidth, &textwidth);
XtnSetArg(XtNheight, &textheight);
XtGetValues(w, wargs, n);
/* third mouse button cancels selection and reverts buffer to cwd name */
if (event->button == Button3) {
newfilelist(w, textw);
return;
}
filenum = (event->y + (charheight / 2) - 10) / charheight + flstart;
if (filenum < 0) filenum = 0;
else if (filenum >= flfiles) filenum = flfiles - 1;
/* Attempt to enter invalid directory. . . treat like button 3 */
if (filenum < 0) {
newfilelist(w, textw);
return;
}
/* check if this file is a directory or not */
if (strchr(filenames[filenum], '/') == NULL) {
/* highlight the entry. . . */
XSetForeground(dpy, sgc, AUXCOLOR);
XDrawString(dpy, flistpix, sgc, 10, 10 + filenum * charheight,
filenames[filenum], strlen(filenames[filenum]));
XCopyArea(dpy, flistpix, lwin, sgc, 0, flstart * charheight,
textwidth, textheight, 0, 0);
/* . . .and append it to the text field */
tbuf = (char *)malloc((XwTextGetLastPos(textw)
+ strlen(filenames[filenum]) + 5) * sizeof(char));
strcpy(tbuf, (char *)XwTextCopyBuffer(textw));
/* add a comma if there is already text in the destination buffer */
if (tbuf[0] != '\0') {
if (tbuf[strlen(tbuf) - 1] != '/') strcat(tbuf, ",");
}
else {
if (cwdname != NULL) {
if (cwdname[0] != '\0') {
tbuf = (char *)realloc(tbuf, (strlen(cwdname) +
strlen(filenames[filenum]) + 5) * sizeof(char));
strcpy(tbuf, cwdname);
}
}
}
strcat(tbuf, filenames[filenum]);
XwTextClearBuffer(textw);
XwTextInsert(textw, tbuf);
XwTextResize(textw);
free(tbuf);
}
else { /* move to new directory */
if (!strcmp(filenames[filenum], "../")) {
char *cptr, *sptr = cwdname;
if (!strcmp(cwdname, "/")) return; /* no way up from root dir. */
while(strstr(sptr, "../") != NULL) sptr += 3;
if ((cptr = strrchr(sptr, '/')) != NULL) {
*cptr = '\0';
if ((cptr = strrchr(sptr, '/')) != NULL) *(cptr + 1) = '\0';
else *sptr = '\0';
}
else {
cwdname = (char *)realloc(cwdname, (strlen(cwdname) +
4) * sizeof(char));
strcat(cwdname, "../");
}
}
else {
cwdname = (char *)realloc(cwdname, (strlen(cwdname) +
strlen(filenames[filenum]) + 1) * sizeof(char));
strcat(cwdname, filenames[filenum]);
}
newfilelist(w, textw);
}
}
/*-------------------------------------------------------------------------*/
/* Scrollbar handler for file list widget */
/*-------------------------------------------------------------------------*/
XtCallbackProc showlscroll(w, callstruct, calldata)
Widget w;
caddr_t callstruct;
caddr_t calldata;
{
Arg wargs[2];
Window swin = XtWindow(w);
Dimension swidth, sheight;
int pstart, pheight, finscr;
short n = 0;
XtnSetArg(XtNwidth, &swidth);
XtnSetArg(XtNheight, &sheight);
XtGetValues(w, wargs, n);
XClearWindow(dpy, swin);
if (flfiles > 0) { /* no files, no bar */
finscr = LISTHEIGHT / ROWHEIGHT;
if (finscr > flfiles) finscr = flfiles;
pstart = (flstart * sheight) / flfiles;
pheight = (finscr * sheight) / flfiles;
XSetForeground(dpy, sgc, BARCOLOR);
XFillRectangle(dpy, swin, sgc, 0, pstart, swidth, pheight);
}
flcurrent = -1;
}
/*-------------------------------------------------------------------------*/
/* Button Motion handler for moving the scrollbar up and down */
/*-------------------------------------------------------------------------*/
XtEventHandler draglscroll(w, filew, event)
Widget w;
Widget filew;
XButtonEvent *event;
{
Arg wargs[2];
Window swin = XtWindow(w);
Dimension swidth, sheight;
int phheight, finscr, flsave = flstart;
short n = 0;
XtnSetArg(XtNwidth, &swidth);
XtnSetArg(XtNheight, &sheight);
XtGetValues(w, wargs, n);
finscr = LISTHEIGHT / ROWHEIGHT;
if (finscr > flfiles) finscr = flfiles;
/* center scrollbar on pointer vertical position */
phheight = (finscr * sheight) / (flfiles * 2);
flstart = (event->y > phheight) ? ((event->y - phheight) * flfiles) / sheight : 0;
if (flstart >= flfiles) flstart = flfiles - 1;
if (flstart != flsave) {
showlscroll(w, NULL, NULL);
listfiles(filew, NULL, NULL);
}
}
/*-------------------------------------------------------------------------*/
/* Generate the file list window */
/*-------------------------------------------------------------------------*/
void genfilelist(Widget parent, Widget entertext, Dimension width)
{
Arg wargs[8];
Widget listarea, lscroll;
short n = 0;
XtnSetArg(XtNx, 20);
XtnSetArg(XtNy, ROWHEIGHT - 10);
XtnSetArg(XtNwidth, width - SBARSIZE - 40);
XtnSetArg(XtNheight, LISTHEIGHT - ROWHEIGHT);
XtnSetArg(XtNfont, areastruct.textstruct);
listarea = XtCreateManagedWidget("Filelist", XwworkSpaceWidgetClass,
parent, wargs, n); n = 0;
XtAddCallback(listarea, XtNexpose, (XtCallbackProc)listfiles, NULL);
XtAddEventHandler(listarea, ButtonPressMask, False,
(XtEventHandler)fileselect, entertext);
XtAddEventHandler(listarea, EnterWindowMask, False,
(XtEventHandler)startfiletrack, NULL);
XtAddEventHandler(listarea, LeaveWindowMask, False,
(XtEventHandler)endfiletrack, NULL);
flstart = 0;
XtnSetArg(XtNx, width - SBARSIZE - 20);
XtnSetArg(XtNy, ROWHEIGHT - 10);
XtnSetArg(XtNwidth, SBARSIZE);
XtnSetArg(XtNheight, LISTHEIGHT - ROWHEIGHT);
XtnSetArg(XtNfont, areastruct.textstruct);
lscroll = XtCreateManagedWidget("LScroll", XwworkSpaceWidgetClass,
parent, wargs, n); n = 0;
XtAddCallback(lscroll, XtNexpose, (XtCallbackProc)showlscroll, NULL);
XtAddEventHandler(lscroll, Button1MotionMask | Button2MotionMask,
False, (XtEventHandler)draglscroll, listarea);
}
/*-------------------------------------------------------------------------*/
/* Look for a directory name in a string and update cwdname accordingly */
/*-------------------------------------------------------------------------*/
int lookdirectory(char *lstring)
{
int slen = strlen(_STR2);
if (lstring[slen - 1] == '/' || (opendir(lstring) != NULL)) {
short n;
if (lstring[slen - 1] != '/') strcat(lstring, "/");
cwdname = (char *)realloc(cwdname, (slen + 2) * sizeof(char));
strcpy(cwdname, lstring);
return(1);
}
return(0);
}
/*-------------------------------------------------------------------------*/
|