home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / Microline3.0 / examples / grid4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.4 KB  |  286 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/PushB.h>
  30. #include <Xm/Form.h>
  31. #include <XmL/Grid.h>
  32.  
  33. void cellSelect();
  34. void showSelected();
  35.  
  36. #define HEADINGFONT "-*-helvetica-bold-o-*--*-100-*-*-*-*-iso8859-1"
  37. #define CONTENTFONT "-*-helvetica-medium-r-*--*-100-*-*-*-*-iso8859-1"
  38.  
  39. #define check_width 9
  40. #define check_height 9
  41. static unsigned char check_bits[] = {
  42.     0x00, 0x01, 0x80, 0x01, 0xc0, 0x00, 0x61, 0x00, 0x37, 0x00, 0x3e, 0x00,
  43.     0x1c, 0x00, 0x08, 0x00, 0x00, 0x00};
  44.  
  45. #define nocheck_width 9
  46. #define nocheck_height 9
  47. static char nocheck_bits[] = {
  48.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  50.  
  51. static char *data[20] = 
  52. {
  53.     "3|Bob Thompson|bobt@teledyne.com",
  54.     "12|Darl Simon|ds@atg.org",
  55.     "14|Jacq Frontier|jf@terrax.com",
  56.     "19|Patty Lee|patlee@isis.com",
  57.     "22|Michal Barnes|mickeyb@softorb.com",
  58.     "23|Dave Schultz|daves@timing.com",
  59.     "23|Eric Stanley|ericst@aldor.com",
  60.     "29|Tim Winters|timw@terra.com",
  61.     "31|Agin Tomitu|agt@umn.edu",
  62.     "33|Betty Tinner|bett@ost.edu",
  63.     "37|Tom Smith|tsmith@netwld.com",
  64.     "38|Rick Wild|raw@mlsoft.com",
  65.     "41|Al Joyce|aj@ulm.edu",
  66.     "41|Tim Burtan|timb@autoc.com",
  67.     "41|George Marlin|gjm@eyeln.com",
  68.     "41|Bill Boxer|billb@idesk.com",
  69.     "41|Maria Montez|marm@ohio.edu",
  70.     "41|Yin Fang|aj@utxs.edu",
  71.     "41|Suzy Saps|ss@umg.edu",
  72.     "41|Jerry Rodgers|jr@lyra.com",
  73. };
  74.  
  75. Pixmap nocheckPix, checkPix;
  76. Widget grid;
  77.  
  78. main(argc, argv)
  79. int argc;
  80. char *argv[];
  81. {
  82.     XtAppContext app;
  83.     Widget shell, form, button;
  84.     Pixel blackPixel, whitePixel;
  85.     Pixmap pix;
  86.     int i;
  87.  
  88.     shell =  XtAppInitialize(&app, "Grid4", NULL, 0,
  89.         &argc, argv, NULL, NULL, 0);
  90.  
  91.     /* Create the pixmaps used for checkmarks */
  92.     blackPixel = BlackPixelOfScreen(XtScreen(shell));
  93.     whitePixel = WhitePixelOfScreen(XtScreen(shell));
  94.     checkPix = XCreatePixmapFromBitmapData(XtDisplay(shell),
  95.         DefaultRootWindow(XtDisplay(shell)),
  96.         check_bits, check_width, check_height,
  97.         blackPixel, whitePixel, 
  98.         DefaultDepthOfScreen(XtScreen(shell)));
  99.     nocheckPix = XCreatePixmapFromBitmapData(XtDisplay(shell),
  100.         DefaultRootWindow(XtDisplay(shell)),
  101.         nocheck_bits, nocheck_width, nocheck_height,
  102.         blackPixel, whitePixel, 
  103.         DefaultDepthOfScreen(XtScreen(shell)));
  104.  
  105.     form = XtVaCreateManagedWidget("form",
  106.         xmFormWidgetClass, shell,
  107.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  108.         XmNshadowThickness, 0,
  109.         NULL);
  110.  
  111.     button = XtVaCreateManagedWidget("Show Selected",
  112.         xmPushButtonWidgetClass, form,
  113.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  114.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  115.         XmNbottomAttachment, XmATTACH_FORM,
  116.         XmNleftAttachment, XmATTACH_FORM,
  117.         NULL);
  118.     XtAddCallback(button, XmNactivateCallback, showSelected, NULL);
  119.  
  120.     /* Create a Grid with 4 columns.  We set the fontList in this */
  121.     /* function for the Grid to use when calculating the visible rows */
  122.     grid = XtVaCreateManagedWidget("grid",
  123.         xmlGridWidgetClass, form,
  124.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  125.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  126.         XtVaTypedArg, XmNfontList, XmRString, CONTENTFONT,
  127.         strlen(CONTENTFONT) + 1,
  128.         XmNcolumns, 4,
  129.         XmNsimpleWidths, "3c 4c 17c 20c",
  130.         XmNhorizontalSizePolicy, XmVARIABLE,
  131.         XmNvsbDisplayPolicy, XmSTATIC,
  132.         XmNvisibleRows, 13,
  133.         XmNselectionPolicy, XmSELECT_NONE,
  134.         XmNhighlightRowMode, True,
  135.         XmNshadowType, XmSHADOW_ETCHED_IN,
  136.         XmNtopAttachment, XmATTACH_FORM,
  137.         XmNleftAttachment, XmATTACH_FORM,
  138.         XmNrightAttachment, XmATTACH_FORM,
  139.         XmNbottomAttachment, XmATTACH_WIDGET,
  140.         XmNbottomWidget, button,
  141.         NULL);
  142.     XtAddCallback(grid, XmNselectCallback, cellSelect, NULL);
  143.  
  144.     /* Freeze the Grid's layout since we will be making changes which */
  145.     /* would cause the Grid to recompute its layout.  The Grid will */
  146.     /* recompute its layout when layoutFrozen is set back to False */
  147.     XtVaSetValues(grid,
  148.         XmNlayoutFrozen, True,
  149.         NULL);
  150.  
  151.     /* Set defaults for new cells and aligments for new cells in */
  152.     /* columns 1-3.  Then, add a heading row */
  153.     XtVaSetValues(grid, 
  154.         XmNcellDefaults, True,
  155.         XtVaTypedArg, XmNcellFontList, XmRString, HEADINGFONT,
  156.         strlen(HEADINGFONT) + 1,
  157.         XtVaTypedArg, XmNcellBackground, XmRString, "#000080", 8,
  158.         XtVaTypedArg, XmNcellForeground, XmRString, "white", 6,
  159.         XmNcellLeftBorderType, XmBORDER_NONE,
  160.         XmNcellRightBorderType, XmBORDER_NONE,
  161.         XmNcellTopBorderType, XmBORDER_NONE,
  162.         XmNcellBottomBorderType, XmBORDER_NONE,
  163.         NULL);
  164.     XtVaSetValues(grid,
  165.         XmNcellDefaults, True,
  166.         XmNcolumn, 1,
  167.         XmNcellAlignment, XmALIGNMENT_RIGHT,
  168.         NULL);
  169.     XtVaSetValues(grid,
  170.         XmNcellDefaults, True,
  171.         XmNcolumnRangeStart, 2,
  172.         XmNcolumnRangeEnd, 3,
  173.         XmNcellAlignment, XmALIGNMENT_LEFT,
  174.         NULL);
  175.     XmLGridAddRows(grid, XmHEADING, -1, 1);
  176.  
  177.     /* Set the headings */
  178.     XmLGridSetStrings(grid, "OD|Qty|Name|EMail Addr");
  179.  
  180.     /* Set defaults for new cells.  Also, set the default cell type */
  181.     /* for cells in column 0 to pixmap cell. Then add the content rows */
  182.     XtVaSetValues(grid,
  183.         XmNcellDefaults, True,
  184.         XtVaTypedArg, XmNcellFontList, XmRString, CONTENTFONT,
  185.         strlen(CONTENTFONT) + 1,
  186.         XtVaTypedArg, XmNcellBackground, XmRString, "white", 6,
  187.         XtVaTypedArg, XmNcellForeground, XmRString, "black", 6,
  188.         XmNcellBottomBorderType, XmBORDER_LINE,
  189.         XtVaTypedArg, XmNcellBottomBorderColor, XmRString, "black", 6,
  190.         NULL);
  191.     XtVaSetValues(grid,
  192.         XmNcellDefaults, True,
  193.         XmNcolumn, 0,
  194.         XmNcellType, XmPIXMAP_CELL,
  195.         NULL);
  196.     XmLGridAddRows(grid, XmCONTENT, -1, 20);
  197.  
  198.     /* Set the content rows, Rows 2, 4, 5, 8 and 13 will have checkmarks */
  199.     for (i = 0; i < 20; i++)
  200.     {
  201.         XmLGridSetStringsPos(grid, XmCONTENT, i, XmCONTENT, 1, data[i]);
  202.         if (i == 2 || i == 4 || i == 5 || i == 8 || i == 13)
  203.             pix = checkPix;
  204.         else
  205.             pix = nocheckPix;
  206.         XtVaSetValues(grid,
  207.             XmNcolumn, 0,
  208.             XmNrow, i,
  209.             XmNcellPixmap, pix,
  210.             NULL);
  211.     }
  212.  
  213.     XtVaSetValues(grid,
  214.         XmNlayoutFrozen, False,
  215.         NULL);
  216.  
  217.     XtRealizeWidget(shell);
  218.     XtAppMainLoop(app);
  219. }
  220.  
  221. void showSelected(w, clientData, callData)
  222. Widget w;
  223. XtPointer clientData;
  224. XtPointer callData;
  225. {
  226.     XmLGridRow row;
  227.     XmLGridColumn column;
  228.     Pixmap pix;
  229.     int i, n;
  230.  
  231.     /* Display the selected rows, these are the rows which have a */
  232.     /* checkPix Pixmap in the first cell in the row */
  233.     printf ("Selected Rows: ");
  234.     XtVaGetValues(grid,
  235.         XmNrows, &n,
  236.         NULL);
  237.     for (i = 0; i < n; i++)
  238.     {
  239.         row = XmLGridGetRow(grid, XmCONTENT, i);
  240.         column = XmLGridGetColumn(grid, XmCONTENT, 0);
  241.         XtVaGetValues(grid,
  242.             XmNrowPtr, row,
  243.             XmNcolumnPtr, column,
  244.             XmNcellPixmap, &pix,
  245.             NULL);
  246.         if (pix == checkPix)
  247.             printf ("%d ", i);
  248.     }
  249.     printf ("\n");
  250. }
  251.  
  252. void cellSelect(w, clientData, callData)
  253. Widget w;
  254. XtPointer clientData;
  255. XtPointer callData;
  256. {
  257.     XmLGridCallbackStruct *cbs;
  258.     XmLGridRow row;
  259.     XmLGridColumn column;
  260.     Pixmap pix;
  261.  
  262.     cbs = (XmLGridCallbackStruct *)callData;
  263.     if (cbs->reason != XmCR_SELECT_CELL)
  264.         return;
  265.     if (cbs->rowType != XmCONTENT)
  266.         return;
  267.  
  268.     /* Toggle the Pixmap in the first cell */
  269.     row = XmLGridGetRow(w, cbs->rowType, cbs->row);
  270.     column = XmLGridGetColumn(w, XmCONTENT, 0);
  271.     XtVaGetValues(w,
  272.         XmNrowPtr, row,
  273.         XmNcolumnPtr, column,
  274.         XmNcellPixmap, &pix,
  275.         NULL);
  276.     if (pix == nocheckPix)
  277.         pix = checkPix;
  278.     else
  279.         pix = nocheckPix;
  280.     XtVaSetValues(w,
  281.         XmNrow, cbs->row,
  282.         XmNcolumn, 0,
  283.         XmNcellPixmap, pix,
  284.         NULL);
  285. }
  286.