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
|
/*
* simple file browser
* (c) 2001 Gerd Knorr <kraxel@bytesex.org>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/extensions/XShm.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include <Xm/CascadeB.h>
#include <Xm/ScrolledW.h>
#include <Xm/SelectioB.h>
#include "RegEdit.h"
#include "ida.h"
#include "loader.h"
#include "viewer.h"
#include "browser.h"
#include "filter.h"
#include "x11.h"
#include "dither.h"
#include "selections.h"
static int icon_width = 64;
static int icon_height = 48;
/*----------------------------------------------------------------------*/
struct browser_file {
struct dirent dirent;
char *filename;
Widget rowcol,icon,name;
int mode,unknown;
};
struct browser_handle {
char *dirname;
char *filter;
Widget shell,viewport,rowcol,status;
struct browser_file *list;
int files;
int dirs,sfiles,afiles;
XtWorkProcId wproc;
struct ida_loader *loader;
void *wdata;
struct ida_image wimg;
struct ida_image simg;
int i,y,state;
};
/*----------------------------------------------------------------------*/
static void browser_cd(Widget widget, XtPointer clientdata,
XtPointer call_data);
static void browser_file(Widget widget, XtPointer clientdata,
XtPointer call_data);
static void browser_drag(Widget widget, XtPointer clientdata,
XtPointer call_data);
/*----------------------------------------------------------------------*/
struct browser_cache {
struct browser_cache *next;
char *path;
Pixmap pix;
};
static struct browser_cache *bcache = NULL;;
static void browser_cache_add(char *path, Pixmap pix)
{
struct browser_cache *item;
item = malloc(sizeof(*item));
memset(item,0,sizeof(*item));
item->next = bcache;
item->path = strdup(path);
item->pix = pix;
bcache = item;
}
static Pixmap browser_cache_get(char *path)
{
struct browser_cache *item;
for (item = bcache; item != NULL; item = item->next)
if (0 == strcmp(path,item->path))
return item->pix;
return 0;
}
/*----------------------------------------------------------------------*/
static int browser_cmpfile(const void *a, const void *b)
{
const struct browser_file *aa = a;
const struct browser_file *bb = b;
if (S_ISDIR(aa->mode) != S_ISDIR(bb->mode))
return S_ISDIR(aa->mode) ? -1 : 1;
return strcmp(aa->dirent.d_name,bb->dirent.d_name);
}
static int browser_addfile(struct browser_handle *h,
struct browser_file *l)
{
XmString str;
str = XmStringGenerate(l->dirent.d_name, NULL, XmMULTIBYTE_TEXT, NULL);
l->rowcol = XtVaCreateWidget(l->dirent.d_name,
xmRowColumnWidgetClass,h->rowcol,
NULL);
l->icon = XtVaCreateManagedWidget("icon",xmPushButtonWidgetClass,l->rowcol,
XtNwidth,icon_width+4,
XtNheight,icon_height+4,
XmNrecomputeSize,False,
NULL);
l->name = XtVaCreateManagedWidget("name",xmLabelWidgetClass,l->rowcol,
XmNlabelString,str,
NULL);
XmStringFree(str);
return 0;
}
static Boolean
browser_readfiles(XtPointer clientdata)
{
struct browser_handle *h = clientdata;
struct op_resize_parm resize;
struct ida_rect rect;
struct ARGS save;
XmString str;
char blk[512],line[512];
FILE *fp;
Pixmap pix;
int l;
float xs,ys,scale;
if (h->i == h->files) {
/* all done */
h->wproc = 0;
if (h->wimg.data)
free(h->wimg.data);
h->wimg.data = NULL;
if (h->simg.data)
free(h->simg.data);
h->simg.data = NULL;
if (h->filter) {
sprintf(line,"%d dirs, %d/%d files [%s]",
h->dirs,h->sfiles,h->afiles,h->filter);
} else {
sprintf(line,"%d dirs, %d files",
h->dirs,h->afiles);
}
str = XmStringGenerate(line, NULL, XmMULTIBYTE_TEXT, NULL);
XtVaSetValues(h->status,XmNlabelString,str,NULL);
XmStringFree(str);
#if 0
/* unmap unknown regular files */
{
WidgetList list;
int i,n;
list = malloc(sizeof(Widget*)*h->files);
for (i = 0, n = 0; i < h->files; i++)
if (h->list[i].unknown)
list[n++] = h->list[i].rowcol;
XtUnmanageChildren(list,n);
free(list);
}
#endif
return TRUE;
}
switch (h->state) {
case 0:
if ((h->list[h->i].mode & S_IFMT) != S_IFREG)
goto next;
/* check cache */
pix = browser_cache_get(h->list[h->i].filename);
if (0 != pix) {
XtVaSetValues(h->list[h->i].icon,
XmNlabelPixmap,pix,
XmNlabelType,XmPIXMAP,
NULL);
goto next;
}
/* open file */
if (NULL == (fp = fopen(h->list[h->i].filename, "r"))) {
if (debug)
fprintf(stderr,"open %s: %s\n",h->list[h->i].filename,
strerror(errno));
goto unknown;
}
if (debug)
fprintf(stderr,"OPENED: %s\n",h->list[h->i].filename);
memset(blk,0,sizeof(blk));
fread(blk,1,sizeof(blk),fp);
rewind(fp);
/* pick loader */
for (l = 0;; l++) {
h->loader = loaders[l];
if (NULL == h->loader) {
if (debug)
fprintf(stderr,"%s: unknown format\n",
h->list[h->i].filename);
fclose(fp);
goto unknown;
}
if (NULL == h->loader->magic)
continue;
if (0 == memcmp(blk+h->loader->moff,h->loader->magic,
h->loader->mlen))
break;
}
/* load image */
save = args;
args.pcd_res = 1;
pcd_res = args.pcd_res;
h->wdata = h->loader->init(fp,h->list[h->i].filename,&h->wimg.i);
args = save;
pcd_res = args.pcd_res;
if (NULL == h->wdata) {
if (debug)
fprintf(stderr,"loading %s [%s] FAILED\n",
h->list[h->i].filename,h->loader->name);
goto unknown;
}
if (h->wimg.data)
free(h->wimg.data);
h->wimg.data = malloc(h->wimg.i.width * h->wimg.i.height * 3);
sprintf(line,"reading %.450s [%dx%d] ...",
strrchr(h->list[h->i].filename,'/')+1,
h->wimg.i.width, h->wimg.i.height);
str = XmStringGenerate(line, NULL, XmMULTIBYTE_TEXT, NULL);
XtVaSetValues(h->status,XmNlabelString,str,NULL);
XmStringFree(str);
h->state = 1;
h->y = 0;
return FALSE;
case 1:
if (h->y < h->wimg.i.height) {
h->loader->read(h->wimg.data + 3*h->y*h->wimg.i.width,
h->y, h->wdata);
h->y++;
return FALSE;
}
h->loader->done(h->wdata);
if (debug)
fprintf(stderr,"LOADED: %s [%dx%d]\n",
h->list[h->i].filename,h->wimg.i.width,h->wimg.i.height);
/* resize image */
xs = (float)icon_width / h->wimg.i.width;
ys = (float)icon_height / h->wimg.i.height;
scale = (xs < ys) ? xs : ys;
resize.width = h->wimg.i.width * scale;
resize.height = h->wimg.i.height * scale;
if (0 == resize.width)
resize.width = 1;
if (0 == resize.height)
resize.height = 1;
rect.x1 = 0;
rect.x2 = h->wimg.i.width;
rect.y1 = 0;
rect.y2 = h->wimg.i.height;
h->wdata = desc_resize.init(&h->wimg,&rect,&h->simg.i,&resize);
if (h->simg.data)
free(h->simg.data);
h->simg.data = malloc(h->simg.i.width * h->simg.i.height * 3);
h->state = 2;
h->y = 0;
return FALSE;
case 2:
if (h->y < h->simg.i.height) {
desc_resize.work(&h->wimg,&rect,h->simg.data+3*h->simg.i.width*h->y,
h->y, h->wdata);
h->y++;
return FALSE;
}
desc_resize.done(h->wdata);
if (debug)
fprintf(stderr,"SCALED: %s [%dx%d]\n",
h->list[h->i].filename,h->simg.i.width,h->simg.i.height);
/* build, cache + install pixmap */
pix = image_to_pixmap(&h->simg);
browser_cache_add(h->list[h->i].filename,pix);
XtVaSetValues(h->list[h->i].icon,
XmNlabelPixmap,pix,
XmNlabelType,XmPIXMAP,
NULL);
h->state = 0;
goto next;
default:
/* shouldn't happen */
fprintf(stderr,"Oops: %s:%d\n",__FILE__,__LINE__);
exit(1);
}
unknown:
/* generic file icon */
h->list[h->i].unknown = 1;
pix = XmGetPixmap(XtScreen(h->list[h->i].icon),"unknown",0,0);
XtVaSetValues(h->list[h->i].icon,
XmNlabelPixmap,pix,
XmNlabelType,XmPIXMAP,
NULL);
next:
h->i++;
return FALSE;
}
static Boolean
browser_statfiles(XtPointer clientdata)
{
struct browser_handle *h = clientdata;
Pixmap pix;
char *type;
if (h->i == h->files) {
/* done => read thumbnails now */
h->wproc = 0;
h->i = 0;
h->wproc = XtAppAddWorkProc(app_context,browser_readfiles,h);
return TRUE;
}
/* handle file */
switch (h->list[h->i].mode & S_IFMT) {
case S_IFDIR:
XtAddCallback(h->list[h->i].icon,XmNactivateCallback,browser_cd,h);
type = "dir";
break;
case S_IFREG:
XtAddCallback(h->list[h->i].icon,XmNactivateCallback,browser_file,
h->list[h->i].filename);
XtAddCallback(h->list[h->i].icon,XmNconvertCallback,browser_drag,
h->list[h->i].filename);
type = "file";
break;
default:
type = NULL;
}
if (type) {
pix = XmGetPixmap(XtScreen(h->list[h->i].icon),type,0,0);
XtVaSetValues(h->list[h->i].icon,
XmNlabelPixmap,pix,
XmNlabelType,XmPIXMAP,
NULL);
}
h->i++;
return FALSE;
}
static int browser_readdir(struct browser_handle *h)
{
struct browser_file *tmp;
struct dirent *dirent;
WidgetList children;
Cardinal nchildren;
XmString str,elem;
struct stat st;
DIR *dir;
int len,i;
/* status line */
str = XmStringGenerate("scanning ", NULL, XmMULTIBYTE_TEXT, NULL);
elem = XmStringGenerate(h->dirname, NULL, XmMULTIBYTE_TEXT, NULL);
str = XmStringConcatAndFree(str,elem);
elem = XmStringGenerate(" ...", NULL, XmMULTIBYTE_TEXT, NULL);
str = XmStringConcatAndFree(str,elem);
XtVaSetValues(h->status,XmNlabelString,str,NULL);
XmStringFree(str);
/* read + sort dir */
dir = opendir(h->dirname);
if (NULL == dir) {
fprintf(stderr,"opendir %s: %s\n",h->dirname,strerror(errno));
return -1;
}
h->dirs = 0;
h->sfiles = 0;
h->afiles = 0;
for (h->files = 0; NULL != (dirent = readdir(dir));) {
/* get memory */
if (0 == (h->files % 16)) {
tmp = realloc(h->list, sizeof(struct browser_file)*
(h->files + 16));
if (NULL == tmp)
break;
h->list = tmp;
memset(h->list+h->files, 0, sizeof(struct browser_file)*16);
}
/* skip dotfiles */
if (dirent->d_name[0] == '.' && 0 != strcmp(dirent->d_name,".."))
continue;
/* get file type */
h->list[h->files].dirent = *dirent;
len = strlen(h->dirname)+strlen(h->list[h->files].dirent.d_name)+4;
h->list[h->files].filename = malloc(len);
sprintf(h->list[h->files].filename,"%s/%s",h->dirname,
h->list[h->files].dirent.d_name);
if (h->list[h->files].dirent.d_type != DT_UNKNOWN) {
h->list[h->files].mode = DTTOIF(h->list[h->files].dirent.d_type);
} else {
if (-1 == lstat(h->list[h->files].filename,&st)) {
fprintf(stderr,"lstat %s: %s\n",
h->list[h->files].filename,strerror(errno));
} else
h->list[h->files].mode = st.st_mode;
}
/* user-specified filter */
if (S_ISDIR(h->list[h->files].mode)) {
h->dirs++;
} else {
h->afiles++;
if (h->filter && 0 != fnmatch(h->filter,dirent->d_name,0))
continue;
else
h->sfiles++;
}
h->files++;
}
closedir(dir);
qsort(h->list,h->files,sizeof(struct browser_file),browser_cmpfile);
/* create widgets */
for (i = 0; i < h->files; i++)
browser_addfile(h,h->list+i);
XtVaGetValues(h->rowcol,XtNchildren,&children,
XtNnumChildren,&nchildren,NULL);
XtManageChildren(children,nchildren);
XtVaSetValues(h->shell,XtNtitle,h->dirname,NULL);
/* start bg processing */
h->i = 0;
h->wproc = XtAppAddWorkProc(app_context,browser_statfiles,h);
return 0;
}
static void browser_bgcancel(struct browser_handle *h)
{
if (h->wproc)
XtRemoveWorkProc(h->wproc);
h->wproc = 0;
if (h->state == 1 && h->wdata)
h->loader->done(h->wdata);
if (h->state == 2 && h->wdata)
desc_resize.done(h->wdata);
if (h->wimg.data)
free(h->wimg.data);
h->wimg.data = NULL;
if (h->simg.data)
free(h->simg.data);
h->simg.data = NULL;
}
static void browser_delwidgets(struct browser_handle *h)
{
WidgetList children,list;
Cardinal nchildren;
int i;
/* delete widgets */
XtVaGetValues(h->rowcol,XtNchildren,&children,
XtNnumChildren,&nchildren,NULL);
list = malloc(sizeof(Widget*)*nchildren);
memcpy(list,children,sizeof(Widget*)*nchildren);
XtUnmanageChildren(list,nchildren);
for (i = 0; i < nchildren; i++)
XtDestroyWidget(list[i]);
free(list);
/* reset state */
if (h->list) {
for (i = 0; i < h->files; i++)
if (h->list[i].filename)
free(h->list[i].filename);
free(h->list);
}
h->list = NULL;
h->files = 0;
h->state = 0;
}
/*----------------------------------------------------------------------*/
static void
browser_resize(Widget widget, XtPointer clientdata, XEvent *event, Boolean *d)
{
struct browser_handle *h = clientdata;
Widget clip;
Dimension width;
XtVaGetValues(h->viewport,XmNclipWindow,&clip,NULL);
XtVaGetValues(clip,XtNwidth,&width,NULL);
XtVaSetValues(h->rowcol,XtNwidth,width,NULL);
}
static void
browser_cd(Widget widget, XtPointer clientdata, XtPointer call_data)
{
struct browser_handle *h = clientdata;
char *dir, *newpath, *tmp;
/* build new dir path */
dir = XtName(XtParent(widget));
if (0 == strcmp(dir,"..")) {
tmp = strrchr(h->dirname,'/');
if (tmp == h->dirname)
tmp++;
*tmp = 0;
} else {
newpath = malloc(strlen(h->dirname)+strlen(dir)+4);
if (0 == strcmp(h->dirname,"/")) {
sprintf(newpath,"/%s",dir);
} else {
sprintf(newpath,"%s/%s",h->dirname,dir);
}
free(h->dirname);
h->dirname = newpath;
}
/* cleanup old stuff + read dir */
browser_bgcancel(h);
browser_delwidgets(h);
browser_readdir(h);
}
static void
browser_filter_done(Widget widget, XtPointer clientdata, XtPointer call_data)
{
struct browser_handle *h = clientdata;
XmSelectionBoxCallbackStruct *cb = call_data;
char *filter;
if (cb->reason == XmCR_OK) {
filter = XmStringUnparse(cb->value,NULL,
XmMULTIBYTE_TEXT,XmMULTIBYTE_TEXT,
NULL,0,0);
if (h->filter)
free(h->filter);
h->filter = NULL;
if (strlen(filter) > 0)
h->filter = strdup(filter);
XtFree(filter);
if (debug)
fprintf(stderr,"filter: %s\n", h->filter ? h->filter : "[none]");
browser_bgcancel(h);
browser_delwidgets(h);
browser_readdir(h);
}
XtDestroyWidget(widget);
}
static void
browser_filter(Widget widget, XtPointer clientdata, XtPointer call_data)
{
struct browser_handle *h = clientdata;
Widget shell;
shell = XmCreatePromptDialog(h->shell,"filter",NULL,0);
XtUnmanageChild(XmSelectionBoxGetChild(shell,XmDIALOG_HELP_BUTTON));
XtAddCallback(shell,XmNokCallback,browser_filter_done,h);
XtAddCallback(shell,XmNcancelCallback,browser_filter_done,h);
XtManageChild(shell);
}
static void
browser_nofilter(Widget widget, XtPointer clientdata, XtPointer call_data)
{
struct browser_handle *h = clientdata;
if (!h->filter)
return;
if (debug)
fprintf(stderr,"filter: reset\n");
free(h->filter);
h->filter = NULL;
browser_bgcancel(h);
browser_delwidgets(h);
browser_readdir(h);
}
static void
browser_file(Widget widget, XtPointer clientdata, XtPointer call_data)
{
new_file(clientdata,1);
}
static void
browser_drag(Widget widget, XtPointer clientdata, XtPointer call_data)
{
XmConvertCallbackStruct *ccs = call_data;
char *name,*file = NULL;
Atom *targs;
int n;
if ((ccs->target == TARGETS) ||
(ccs->target == _MOTIF_CLIPBOARD_TARGETS) ||
(ccs->target == _MOTIF_EXPORT_TARGETS)) {
targs = (Atom*)XtMalloc(sizeof(Atom)*8);
n = 0;
targs[n++] = TARGETS;
targs[n++] = XA_FILE_NAME;
targs[n++] = XA_FILE;
targs[n++] = _NETSCAPE_URL;
ccs->value = targs;
ccs->length = n;
ccs->type = XA_ATOM;
ccs->format = 32;
ccs->status = XmCONVERT_MERGE;
}
if (ccs->target == XA_FILE_NAME ||
ccs->target == XA_FILE ||
ccs->target == XA_STRING) {
file = XtMalloc(strlen(clientdata)+1);
strcpy(file,clientdata);
ccs->value = file;
ccs->length = strlen(file);
ccs->type = XA_STRING;
ccs->format = 8;
ccs->status = XmCONVERT_DONE;
}
if (ccs->target == _NETSCAPE_URL) {
file = XtMalloc(strlen(clientdata)+8);
sprintf(file,"file:%s",(char*)clientdata);
ccs->value = file;
ccs->length = strlen(file);
ccs->type = _NETSCAPE_URL;
ccs->format = 8;
ccs->status = XmCONVERT_DONE;
}
if (debug) {
name = XGetAtomName(dpy,ccs->target);
fprintf(stderr,"drag: %s [%s]\n",name,file ? file : "-");
XFree(name);
}
}
static void
browser_destroy(Widget widget, XtPointer clientdata, XtPointer call_data)
{
struct browser_handle *h = clientdata;
int i;
browser_bgcancel(h);
ptr_unregister(h->shell);
if (h->list) {
for (i = 0; i < h->files; i++)
if (h->list[i].filename)
free(h->list[i].filename);
free(h->list);
}
free(h);
}
/*----------------------------------------------------------------------*/
static char*
browser_fixpath(char *dir)
{
char path[1024];
char *s,*d;
memset(path,0,sizeof(path));
if (dir[0] == '/') {
/* absolute */
strncpy(path,dir,sizeof(path)-1);
} else {
/* relative */
getcwd(path,sizeof(path)-1);
if (strlen(path)+strlen(dir)+4 < sizeof(path)) {
strcat(path,"/");
strcat(path,dir);
}
}
for (s = d = path; *s != 0;) {
if (0 == strncmp(s,"//",2)) {
s++;
continue;
}
if (0 == strncmp(s,"/./",3)) {
s+=2;
continue;
}
if (0 == strcmp(s,"/"))
s++;
if (0 == strcmp(s,"/."))
s+=2;
*d = *s;
s++, d++;
}
return strdup(path);
}
void
browser_window(char *dirname)
{
Widget form,clip,menubar,menu,push;
struct browser_handle *h;
h = malloc(sizeof(*h));
if (NULL == h) {
fprintf(stderr,"out of memory");
return;
}
memset(h,0,sizeof(*h));
h->dirname = browser_fixpath(dirname);
h->shell = XtVaAppCreateShell("browser","Iv",
topLevelShellWidgetClass,
dpy,
XtNclientLeader,app_shell,
XmNdeleteResponse,XmDESTROY,
NULL);
XmdRegisterEditres(h->shell);
XtAddCallback(h->shell,XtNdestroyCallback,browser_destroy,h);
/* widgets */
form = XtVaCreateManagedWidget("form", xmFormWidgetClass, h->shell,
NULL);
menubar = XmCreateMenuBar(form,"bar",NULL,0);
XtManageChild(menubar);
h->status = XtVaCreateManagedWidget("status",xmLabelWidgetClass, form,
NULL);
h->viewport = XmCreateScrolledWindow(form,"view",NULL,0);
h->rowcol = XtVaCreateManagedWidget("box", xmRowColumnWidgetClass,
h->viewport, NULL);
XtManageChild(h->viewport);
XtVaGetValues(h->viewport,XmNclipWindow,&clip,NULL);
if (NULL == clip)
fprintf(stderr,"FIXME: clip is NULL\n");
else
XtAddEventHandler(clip,StructureNotifyMask,True,
browser_resize,h);
/* menu - file */
menu = XmCreatePulldownMenu(menubar,"fileM",NULL,0);
XtVaCreateManagedWidget("file",xmCascadeButtonWidgetClass,menubar,
XmNsubMenuId,menu,NULL);
push = XtVaCreateManagedWidget("close",xmPushButtonWidgetClass,menu,NULL);
XtAddCallback(push,XmNactivateCallback,destroy_cb,h->shell);
/* menu - view */
menu = XmCreatePulldownMenu(menubar,"viewM",NULL,0);
XtVaCreateManagedWidget("view",xmCascadeButtonWidgetClass,menubar,
XmNsubMenuId,menu,NULL);
push = XtVaCreateManagedWidget("filter",xmPushButtonWidgetClass,menu,NULL);
XtAddCallback(push,XmNactivateCallback,browser_filter,h);
push = XtVaCreateManagedWidget("freset",xmPushButtonWidgetClass,menu,NULL);
XtAddCallback(push,XmNactivateCallback,browser_nofilter,h);
/* read dir and show window */
browser_readdir(h);
XtPopup(h->shell,XtGrabNone);
ptr_register(h->shell);
}
void
browser_ac(Widget widget, XEvent *event,
String *params, Cardinal *num_params)
{
if (*num_params > 0)
browser_window(params[0]);
else
browser_window(".");
}
|