home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / Microline3.0 / examples / tree4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.9 KB  |  291 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * The following source code is part of the Microline Widget Library.
  21.  * The Microline widget library is made available to Mozilla developers
  22.  * under the Netscape Public License (NPL) by Neuron Data.  To learn
  23.  * more about Neuron Data, please visit the Neuron Data Home Page at
  24.  * http://www.neurondata.com.
  25.  */
  26.  
  27.  
  28. #include <Xm/Xm.h>
  29. #include <Xm/Form.h>
  30. #include <XmL/Grid.h>
  31. #include <XmL/Tree.h>
  32.  
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <dirent.h>
  36.  
  37. void rowExpand();
  38. void rowCollapse();
  39. void cellSelect();
  40.  
  41. Widget grid;
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.     XtAppContext app;
  48.     Widget shell, form, tree;
  49.     XmString str;
  50.  
  51.     shell = XtAppInitialize(&app, "Tree4", NULL, 0,
  52.         &argc, argv, NULL, NULL, 0);
  53.  
  54.     form = XtVaCreateManagedWidget("form",
  55.         xmFormWidgetClass, shell,
  56.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  57.         XmNshadowThickness, 0,
  58.         NULL);
  59.  
  60.     /* Add Tree to left of Form */
  61.     tree = XtVaCreateManagedWidget("tree",
  62.         xmlTreeWidgetClass, form,
  63.         XmNhorizontalSizePolicy, XmCONSTANT,
  64.         XmNautoSelect, False,
  65.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  66.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  67.         XtVaTypedArg, XmNblankBackground, XmRString, "white", 6,
  68.         XtVaTypedArg, XmNselectBackground, XmRString, "#000080", 8,
  69.         XtVaTypedArg, XmNselectForeground, XmRString, "white", 6,
  70.         XtVaTypedArg, XmNconnectingLineColor, XmRString, "#808080", 8,
  71.         XmNleftAttachment, XmATTACH_FORM,
  72.         XmNtopAttachment, XmATTACH_FORM,
  73.         XmNbottomAttachment, XmATTACH_FORM,
  74.         XmNrightAttachment, XmATTACH_POSITION,
  75.         XmNrightPosition, 45,
  76.         NULL);
  77.     XtVaSetValues(tree,
  78.         XmNcellDefaults, True,
  79.         XtVaTypedArg, XmNcellBackground, XmRString, "white", 6,
  80.         NULL);
  81.  
  82.     /* Add a single row containing the root path to the Tree */
  83.     str = XmStringCreateSimple("/");
  84.     XmLTreeAddRow(tree, 1, True, False, 0,
  85.         XmUNSPECIFIED_PIXMAP, XmUNSPECIFIED_PIXMAP, str);
  86.     XmStringFree(str);
  87.     XtVaSetValues(tree,
  88.         XmNrow, 0,
  89.         XmNrowUserData, strdup("/"),
  90.         NULL);
  91.  
  92.     XtAddCallback(tree, XmNexpandCallback, rowExpand, NULL);
  93.     XtAddCallback(tree, XmNcollapseCallback, rowCollapse, NULL);
  94.     XtAddCallback(tree, XmNselectCallback, cellSelect, NULL);
  95.  
  96.     /* Add a Grid to the right of the Form and set cell defaults */
  97.     grid = XtVaCreateManagedWidget("grid",
  98.         xmlGridWidgetClass, form,
  99.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  100.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  101.         XtVaTypedArg, XmNblankBackground, XmRString, "white", 6,
  102.         XtVaTypedArg, XmNselectBackground, XmRString, "#000080", 8,
  103.         XtVaTypedArg, XmNselectForeground, XmRString, "white", 6,
  104.         XmNcolumns, 3,
  105.         XmNsimpleWidths, "24c 12c 10c",
  106.         XmNsimpleHeadings, "Name|Type|Size",
  107.         XmNvisibleColumns, 6,
  108.         XmNallowColumnResize, True,
  109.         XmNheadingRows, 1,
  110.         XmNvisibleRows, 16,
  111.         XmNhighlightRowMode, True,
  112.         XmNleftAttachment, XmATTACH_POSITION,
  113.         XmNleftPosition, 46,
  114.         XmNtopAttachment, XmATTACH_FORM,
  115.         XmNbottomAttachment, XmATTACH_FORM,
  116.         XmNrightAttachment, XmATTACH_FORM,
  117.         NULL);
  118.     XtVaSetValues(grid,
  119.         XmNcellDefaults, True,
  120.         XtVaTypedArg, XmNcellBackground, XmRString, "white", 6,
  121.         XmNcellTopBorderType, XmBORDER_NONE,
  122.         XmNcellBottomBorderType, XmBORDER_NONE,
  123.         XmNcellRightBorderType, XmBORDER_NONE,
  124.         XmNcellLeftBorderType, XmBORDER_NONE,
  125.         XmNcellAlignment, XmALIGNMENT_LEFT,
  126.         NULL);
  127.     XtVaSetValues(grid,
  128.         XmNcellDefaults, True,
  129.         XmNcolumn, 2,
  130.         XmNcellAlignment, XmALIGNMENT_RIGHT,
  131.         NULL);
  132.  
  133.     /* Invoke the select callback for the first row in the Tree */
  134.     /* to fill the Grid with the data for the root path */
  135.     XmLGridSelectRow(tree, 0, True);
  136.  
  137.     XtRealizeWidget(shell);
  138.     XtAppMainLoop(app);
  139. }
  140.  
  141. void rowExpand(w, clientData, callData)
  142. Widget w;
  143. XtPointer clientData, callData;
  144. {
  145.     XmLGridCallbackStruct *cbs;
  146.     XmLGridRow row;
  147.     int level, pos;
  148.     DIR *dir;
  149.     struct dirent *d;
  150.     struct stat s;
  151.     char *path, fullpath[1024];
  152.     XmString str;
  153.  
  154.     /* Retrieve the path of the directory expanded.  This is kept */
  155.     /* in the row's rowUserData */
  156.     cbs = (XmLGridCallbackStruct *)callData;
  157.     row = XmLGridGetRow(w, XmCONTENT, cbs->row);
  158.     XtVaGetValues(w,
  159.         XmNrowPtr, row,
  160.         XmNrowUserData, &path,
  161.         XmNrowLevel, &level,
  162.         NULL);
  163.     pos = cbs->row + 1;
  164.     dir = opendir(path);
  165.     if (!dir)
  166.         return;
  167.  
  168.     /* Add the subdirectories of the directory expanded to the Tree */
  169.     XtVaSetValues(w,
  170.         XmNlayoutFrozen, True,
  171.         NULL);
  172.     while (d = readdir(dir))
  173.     {
  174.         if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  175.             continue;
  176.         sprintf(fullpath, "%s/%s", path, d->d_name);
  177.         if (lstat(fullpath, &s) == -1)
  178.             continue;
  179.         if (!S_ISDIR(s.st_mode))
  180.             continue;
  181.         str = XmStringCreateSimple(d->d_name);
  182.         XmLTreeAddRow(w, level + 1, True, False, pos,
  183.             XmUNSPECIFIED_PIXMAP, XmUNSPECIFIED_PIXMAP, str);
  184.         XmStringFree(str);
  185.         XtVaSetValues(w,
  186.             XmNrow, pos,
  187.             XmNrowUserData, strdup(fullpath),
  188.             NULL);
  189.         pos++;
  190.     }
  191.     closedir(dir);
  192.     XtVaSetValues(w,
  193.         XmNlayoutFrozen, False,
  194.         NULL);
  195. }
  196.  
  197. void rowCollapse(w, clientData, callData)
  198. Widget w;
  199. XtPointer clientData, callData;
  200. {
  201.     XmLGridCallbackStruct *cbs;
  202.     XmLGridRow row;
  203.     char *path;
  204.     int i, j, level, rows;
  205.  
  206.     /* Collapse the row by deleting the rows in the tree which */
  207.     /* are children of the collapsed row.  We also need to free */
  208.     /* the path contained in the rowUserData of the rows deleted */
  209.     cbs = (XmLGridCallbackStruct *)callData;
  210.     row = XmLGridGetRow(w, XmCONTENT, cbs->row);
  211.     XtVaGetValues(w,
  212.         XmNrowPtr, row,
  213.         XmNrowLevel, &level,
  214.         NULL);
  215.     XtVaGetValues(w,
  216.         XmNrows, &rows,
  217.         NULL);
  218.     i = cbs->row + 1;
  219.     while (i < rows)
  220.     {
  221.         row = XmLGridGetRow(w, XmCONTENT, i);
  222.         XtVaGetValues(w,
  223.             XmNrowPtr, row,
  224.             XmNrowLevel, &j,
  225.             XmNrowUserData, &path,
  226.             NULL);
  227.         if (j <= level)
  228.             break;
  229.         free(path);
  230.         i++;
  231.     }
  232.     j = i - cbs->row - 1;
  233.     XmLGridDeleteRows(w, XmCONTENT, cbs->row + 1, j);
  234. }
  235.  
  236. void cellSelect(w, clientData, callData)
  237. Widget w;
  238. XtPointer clientData, callData;
  239. {
  240.     XmLGridCallbackStruct *cbs;
  241.     XmLGridRow row;
  242.     DIR *dir;
  243.     struct stat s;
  244.     struct dirent *d;
  245.     char *path, fullpath[1024], buf[256];
  246.     int pos;
  247.  
  248.     /* Retrieve the directory selected */
  249.     cbs = (XmLGridCallbackStruct *)callData;
  250.     if (cbs->rowType != XmCONTENT)
  251.         return;
  252.     row = XmLGridGetRow(w, XmCONTENT, cbs->row);
  253.     XtVaGetValues(w,
  254.         XmNrowPtr, row,
  255.         XmNrowUserData, &path,
  256.         NULL);
  257.     dir = opendir(path);
  258.     if (!dir)
  259.         return;
  260.  
  261.     /* Add a row for each file in the directory to the Grid */
  262.     pos = 0;
  263.     XtVaSetValues(grid,
  264.         XmNlayoutFrozen, True,
  265.         NULL);
  266.     XmLGridDeleteAllRows(grid, XmCONTENT);
  267.     while (d = readdir(dir))
  268.     {
  269.         sprintf(fullpath, "%s/%s", path, d->d_name);
  270.         if (lstat(fullpath, &s) == -1)
  271.             continue;
  272.         XmLGridAddRows(grid, XmCONTENT, pos, 1);
  273.         XmLGridSetStringsPos(grid, XmCONTENT, pos, XmCONTENT, 0, d->d_name);
  274.         if (S_ISDIR(s.st_mode))
  275.             sprintf(buf, "Directory");
  276.         else if (S_ISLNK(s.st_mode))
  277.             sprintf(buf, "Link");
  278.         else
  279.             sprintf(buf, "File");
  280.         XmLGridSetStringsPos(grid, XmCONTENT, pos, XmCONTENT, 1, buf);
  281.         sprintf(buf, "%d", (int)s.st_size);
  282.         XmLGridSetStringsPos(grid, XmCONTENT, pos, XmCONTENT, 2, buf);
  283.         pos++;
  284.     }
  285.     closedir(dir);
  286.     XtVaSetValues(grid,
  287.         XmNlayoutFrozen, False,
  288.         NULL);
  289. }
  290.  
  291.