UNIXworkcode

1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * 3 * ***** BEGIN LICENSE BLOCK ***** 4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 5 * 6 * The contents of this file are subject to the Mozilla Public License Version 7 * 1.1 (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * http://www.mozilla.org/MPL/ 10 * 11 * Software distributed under the License is distributed on an "AS IS" basis, 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 * for the specific language governing rights and limitations under the 14 * License. 15 * 16 * The Original Code is the Microline Widget Library, originally made available under the NPL by Neuron Data <http://www.neurondata.com>. 17 * 18 * The Initial Developer of the Original Code is 19 * Netscape Communications Corporation. 20 * Portions created by the Initial Developer are Copyright (C) 1998 21 * the Initial Developer. All Rights Reserved. 22 * 23 * Contributor(s): 24 * 25 * Alternatively, the contents of this file may be used under the terms of 26 * either the GNU General Public License Version 2 or later (the "GPL"), or 27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 28 * in which case the provisions of the GPL or the LGPL are applicable instead 29 * of those above. If you wish to allow use of your version of this file only 30 * under the terms of either the GPL or the LGPL, and not to allow others to 31 * use your version of this file under the terms of the MPL, indicate your 32 * decision by deleting the provisions above and replace them with the notice 33 * and other provisions required by the GPL or the LGPL. If you do not delete 34 * the provisions above, a recipient may use your version of this file under 35 * the terms of any one of the MPL, the GPL or the LGPL. 36 * 37 * In addition, as a special exception to the GNU GPL, the copyright holders 38 * give permission to link the code of this program with the Motif and Open 39 * Motif libraries (or with modified versions of these that use the same 40 * license), and distribute linked combinations including the two. You 41 * must obey the GNU General Public License in all respects for all of 42 * the code used other than linking with Motif/Open Motif. If you modify 43 * this file, you may extend this exception to your version of the file, 44 * but you are not obligated to do so. If you do not wish to do so, 45 * delete this exception statement from your version. 46 * 47 * ***** END LICENSE BLOCK ***** */ 48 49 50 #include <Xm/Xm.h> 51 #include <Xm/PushB.h> 52 #include <Xm/Form.h> 53 #include <XmL/Grid.h> 54 55 void cellSelect(); 56 void showSelected(); 57 58 #define HEADINGFONT "-*-helvetica-bold-o-*--*-100-*-*-*-*-iso8859-1" 59 #define CONTENTFONT "-*-helvetica-medium-r-*--*-100-*-*-*-*-iso8859-1" 60 61 #define check_width 9 62 #define check_height 9 63 static unsigned char check_bits[] = { 64 0x00, 0x01, 0x80, 0x01, 0xc0, 0x00, 0x61, 0x00, 0x37, 0x00, 0x3e, 0x00, 65 0x1c, 0x00, 0x08, 0x00, 0x00, 0x00}; 66 67 #define nocheck_width 9 68 #define nocheck_height 9 69 static char nocheck_bits[] = { 70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 72 73 static char *data[20] = 74 { 75 "3|Bob Thompson|bobt@teledyne.com", 76 "12|Darl Simon|ds@atg.org", 77 "14|Jacq Frontier|jf@terrax.com", 78 "19|Patty Lee|patlee@isis.com", 79 "22|Michal Barnes|mickeyb@softorb.com", 80 "23|Dave Schultz|daves@timing.com", 81 "23|Eric Stanley|ericst@aldor.com", 82 "29|Tim Winters|timw@terra.com", 83 "31|Agin Tomitu|agt@umn.edu", 84 "33|Betty Tinner|bett@ost.edu", 85 "37|Tom Smith|tsmith@netwld.com", 86 "38|Rick Wild|raw@mlsoft.com", 87 "41|Al Joyce|aj@ulm.edu", 88 "41|Tim Burtan|timb@autoc.com", 89 "41|George Marlin|gjm@eyeln.com", 90 "41|Bill Boxer|billb@idesk.com", 91 "41|Maria Montez|marm@ohio.edu", 92 "41|Yin Fang|aj@utxs.edu", 93 "41|Suzy Saps|ss@umg.edu", 94 "41|Jerry Rodgers|jr@lyra.com", 95 }; 96 97 Pixmap nocheckPix, checkPix; 98 Widget grid; 99 100 main(argc, argv) 101 int argc; 102 char *argv[]; 103 { 104 XtAppContext app; 105 Widget shell, form, button; 106 Pixel blackPixel, whitePixel; 107 Pixmap pix; 108 int i; 109 110 shell = XtAppInitialize(&app, "Grid4", NULL, 0, 111 &argc, argv, NULL, NULL, 0); 112 113 /* Create the pixmaps used for checkmarks */ 114 blackPixel = BlackPixelOfScreen(XtScreen(shell)); 115 whitePixel = WhitePixelOfScreen(XtScreen(shell)); 116 checkPix = XCreatePixmapFromBitmapData(XtDisplay(shell), 117 DefaultRootWindow(XtDisplay(shell)), 118 check_bits, check_width, check_height, 119 blackPixel, whitePixel, 120 DefaultDepthOfScreen(XtScreen(shell))); 121 nocheckPix = XCreatePixmapFromBitmapData(XtDisplay(shell), 122 DefaultRootWindow(XtDisplay(shell)), 123 nocheck_bits, nocheck_width, nocheck_height, 124 blackPixel, whitePixel, 125 DefaultDepthOfScreen(XtScreen(shell))); 126 127 form = XtVaCreateManagedWidget("form", 128 xmFormWidgetClass, shell, 129 XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8, 130 XmNshadowThickness, 0, 131 NULL); 132 133 button = XtVaCreateManagedWidget("Show Selected", 134 xmPushButtonWidgetClass, form, 135 XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8, 136 XtVaTypedArg, XmNforeground, XmRString, "black", 6, 137 XmNbottomAttachment, XmATTACH_FORM, 138 XmNleftAttachment, XmATTACH_FORM, 139 NULL); 140 XtAddCallback(button, XmNactivateCallback, showSelected, NULL); 141 142 /* Create a Grid with 4 columns. We set the fontList in this */ 143 /* function for the Grid to use when calculating the visible rows */ 144 grid = XtVaCreateManagedWidget("grid", 145 xmlGridWidgetClass, form, 146 XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8, 147 XtVaTypedArg, XmNforeground, XmRString, "black", 6, 148 XtVaTypedArg, XmNfontList, XmRString, CONTENTFONT, 149 strlen(CONTENTFONT) + 1, 150 XmNcolumns, 4, 151 XmNsimpleWidths, "3c 4c 17c 20c", 152 XmNhorizontalSizePolicy, XmVARIABLE, 153 XmNvsbDisplayPolicy, XmSTATIC, 154 XmNvisibleRows, 13, 155 XmNselectionPolicy, XmSELECT_NONE, 156 XmNhighlightRowMode, True, 157 XmNshadowType, XmSHADOW_ETCHED_IN, 158 XmNtopAttachment, XmATTACH_FORM, 159 XmNleftAttachment, XmATTACH_FORM, 160 XmNrightAttachment, XmATTACH_FORM, 161 XmNbottomAttachment, XmATTACH_WIDGET, 162 XmNbottomWidget, button, 163 NULL); 164 XtAddCallback(grid, XmNselectCallback, cellSelect, NULL); 165 166 /* Freeze the Grid's layout since we will be making changes which */ 167 /* would cause the Grid to recompute its layout. The Grid will */ 168 /* recompute its layout when layoutFrozen is set back to False */ 169 XtVaSetValues(grid, 170 XmNlayoutFrozen, True, 171 NULL); 172 173 /* Set defaults for new cells and aligments for new cells in */ 174 /* columns 1-3. Then, add a heading row */ 175 XtVaSetValues(grid, 176 XmNcellDefaults, True, 177 XtVaTypedArg, XmNcellFontList, XmRString, HEADINGFONT, 178 strlen(HEADINGFONT) + 1, 179 XtVaTypedArg, XmNcellBackground, XmRString, "#000080", 8, 180 XtVaTypedArg, XmNcellForeground, XmRString, "white", 6, 181 XmNcellLeftBorderType, XmBORDER_NONE, 182 XmNcellRightBorderType, XmBORDER_NONE, 183 XmNcellTopBorderType, XmBORDER_NONE, 184 XmNcellBottomBorderType, XmBORDER_NONE, 185 NULL); 186 XtVaSetValues(grid, 187 XmNcellDefaults, True, 188 XmNcolumn, 1, 189 XmNcellAlignment, XmALIGNMENT_RIGHT, 190 NULL); 191 XtVaSetValues(grid, 192 XmNcellDefaults, True, 193 XmNcolumnRangeStart, 2, 194 XmNcolumnRangeEnd, 3, 195 XmNcellAlignment, XmALIGNMENT_LEFT, 196 NULL); 197 XmLGridAddRows(grid, XmHEADING, -1, 1); 198 199 /* Set the headings */ 200 XmLGridSetStrings(grid, "OD|Qty|Name|EMail Addr"); 201 202 /* Set defaults for new cells. Also, set the default cell type */ 203 /* for cells in column 0 to pixmap cell. Then add the content rows */ 204 XtVaSetValues(grid, 205 XmNcellDefaults, True, 206 XtVaTypedArg, XmNcellFontList, XmRString, CONTENTFONT, 207 strlen(CONTENTFONT) + 1, 208 XtVaTypedArg, XmNcellBackground, XmRString, "white", 6, 209 XtVaTypedArg, XmNcellForeground, XmRString, "black", 6, 210 XmNcellBottomBorderType, XmBORDER_LINE, 211 XtVaTypedArg, XmNcellBottomBorderColor, XmRString, "black", 6, 212 NULL); 213 XtVaSetValues(grid, 214 XmNcellDefaults, True, 215 XmNcolumn, 0, 216 XmNcellType, XmPIXMAP_CELL, 217 NULL); 218 XmLGridAddRows(grid, XmCONTENT, -1, 20); 219 220 /* Set the content rows, Rows 2, 4, 5, 8 and 13 will have checkmarks */ 221 for (i = 0; i < 20; i++) 222 { 223 XmLGridSetStringsPos(grid, XmCONTENT, i, XmCONTENT, 1, data[i]); 224 if (i == 2 || i == 4 || i == 5 || i == 8 || i == 13) 225 pix = checkPix; 226 else 227 pix = nocheckPix; 228 XtVaSetValues(grid, 229 XmNcolumn, 0, 230 XmNrow, i, 231 XmNcellPixmap, pix, 232 NULL); 233 } 234 235 XtVaSetValues(grid, 236 XmNlayoutFrozen, False, 237 NULL); 238 239 XtRealizeWidget(shell); 240 XtAppMainLoop(app); 241 } 242 243 void showSelected(w, clientData, callData) 244 Widget w; 245 XtPointer clientData; 246 XtPointer callData; 247 { 248 XmLGridRow row; 249 XmLGridColumn column; 250 Pixmap pix; 251 int i, n; 252 253 /* Display the selected rows, these are the rows which have a */ 254 /* checkPix Pixmap in the first cell in the row */ 255 printf ("Selected Rows: "); 256 XtVaGetValues(grid, 257 XmNrows, &n, 258 NULL); 259 for (i = 0; i < n; i++) 260 { 261 row = XmLGridGetRow(grid, XmCONTENT, i); 262 column = XmLGridGetColumn(grid, XmCONTENT, 0); 263 XtVaGetValues(grid, 264 XmNrowPtr, row, 265 XmNcolumnPtr, column, 266 XmNcellPixmap, &pix, 267 NULL); 268 if (pix == checkPix) 269 printf ("%d ", i); 270 } 271 printf ("\n"); 272 } 273 274 void cellSelect(w, clientData, callData) 275 Widget w; 276 XtPointer clientData; 277 XtPointer callData; 278 { 279 XmLGridCallbackStruct *cbs; 280 XmLGridRow row; 281 XmLGridColumn column; 282 Pixmap pix; 283 284 cbs = (XmLGridCallbackStruct *)callData; 285 if (cbs->reason != XmCR_SELECT_CELL) 286 return; 287 if (cbs->rowType != XmCONTENT) 288 return; 289 290 /* Toggle the Pixmap in the first cell */ 291 row = XmLGridGetRow(w, cbs->rowType, cbs->row); 292 column = XmLGridGetColumn(w, XmCONTENT, 0); 293 XtVaGetValues(w, 294 XmNrowPtr, row, 295 XmNcolumnPtr, column, 296 XmNcellPixmap, &pix, 297 NULL); 298 if (pix == nocheckPix) 299 pix = checkPix; 300 else 301 pix = nocheckPix; 302 XtVaSetValues(w, 303 XmNrow, cbs->row, 304 XmNcolumn, 0, 305 XmNcellPixmap, pix, 306 NULL); 307 } 308