home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / Microline3.0 / examples / grid3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.2 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 <Xm/Text.h>
  31. #include <Xm/Label.h>
  32. #include <Xm/PushB.h>
  33. #include <XmL/Grid.h>
  34.  
  35. Widget label, text, grid, gridText;
  36.  
  37. static int busy = 0;
  38.  
  39. void cellFocus();
  40. void textModify();
  41. void copy();
  42. void paste();
  43.  
  44. main(argc, argv)
  45. int argc;
  46. char *argv[];
  47. {
  48.     XtAppContext app;
  49.     Widget shell, form, copyButton, pasteButton;
  50.     XmString str;
  51.     char buf[4];
  52.     int i;
  53.  
  54.     shell =  XtAppInitialize(&app, "Grid3", NULL, 0,
  55.         &argc, argv, NULL, NULL, 0);
  56.  
  57.     form = XtVaCreateManagedWidget("form",
  58.         xmFormWidgetClass, shell,
  59.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  60.         XmNshadowThickness, 0,
  61.         NULL);
  62.  
  63.     str = XmStringCreateSimple("(A 1)");
  64.     label = XtVaCreateManagedWidget("label",
  65.         xmLabelWidgetClass, form,
  66.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  67.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  68.         XmNtopAttachment, XmATTACH_FORM,
  69.         XmNmarginHeight, 4,
  70.         XmNleftAttachment, XmATTACH_FORM,
  71.         XmNlabelString, str,
  72.         NULL);
  73.     XmStringFree(str);
  74.  
  75.     pasteButton = XtVaCreateManagedWidget("Paste To Focus",
  76.         xmPushButtonWidgetClass, form,
  77.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  78.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  79.         XmNrightAttachment, XmATTACH_FORM,
  80.         XmNmarginHeight, 0,
  81.         NULL);
  82.     XtAddCallback(pasteButton, XmNactivateCallback, paste, NULL);
  83.  
  84.     copyButton = XtVaCreateManagedWidget("Copy Selected",
  85.         xmPushButtonWidgetClass, form,
  86.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  87.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  88.         XmNrightAttachment, XmATTACH_WIDGET,
  89.         XmNrightWidget, pasteButton,
  90.         XmNmarginHeight, 0,
  91.         NULL);
  92.     XtAddCallback(copyButton, XmNactivateCallback, copy, NULL);
  93.  
  94.     text = XtVaCreateManagedWidget("text",
  95.         xmTextWidgetClass, form,
  96.         XtVaTypedArg, XmNbackground, XmRString, "white", 6,
  97.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  98.         XmNtopAttachment, XmATTACH_FORM,
  99.         XmNleftAttachment, XmATTACH_WIDGET,
  100.         XmNleftWidget, label,
  101.         XmNrightAttachment, XmATTACH_WIDGET,
  102.         XmNrightWidget, copyButton,
  103.         XmNmarginHeight, 0,
  104.         NULL);
  105.  
  106.     /* Create a Grid with 1 heading column and 1 heading row */
  107.     grid = XtVaCreateManagedWidget("grid",
  108.         xmlGridWidgetClass, form,
  109.         XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
  110.         XtVaTypedArg, XmNforeground, XmRString, "black", 6,
  111.         XmNheadingColumns, 1,
  112.         XmNcolumns, 26,
  113.         XmNvisibleColumns, 8,
  114.         XmNhsbDisplayPolicy, XmSTATIC,
  115.         XmNrows, 100,
  116.         XmNheadingRows, 1,
  117.         XmNvisibleRows, 12,
  118.         XmNvsbDisplayPolicy, XmSTATIC,
  119.         XmNallowDragSelected, True,
  120.         XmNallowDrop, True,
  121.         XmNallowRowResize, True,
  122.         XmNallowColumnResize, True,
  123.         XmNtopAttachment, XmATTACH_WIDGET,
  124.         XmNtopWidget, text,
  125.         XmNtopOffset, 2,
  126.         XmNbottomAttachment, XmATTACH_FORM,
  127.         XmNleftAttachment, XmATTACH_FORM,
  128.         XmNrightAttachment, XmATTACH_FORM,
  129.         XmNshadowThickness, 0,
  130.         XmNselectionPolicy, XmSELECT_CELL,
  131.         NULL);
  132.  
  133.     /* Make all cells in the content rows and content columns */
  134.     /* (the data cells) editable and set their borders and color */
  135.     XtVaSetValues(grid,
  136.         XmNcellEditable, True,
  137.         XtVaTypedArg, XmNcellBackground, XmRString, "white", 6,
  138.         XmNcellAlignment, XmALIGNMENT_RIGHT,
  139.         XmNcellTopBorderType, XmBORDER_NONE,
  140.         XmNcellLeftBorderType, XmBORDER_NONE,
  141.         XtVaTypedArg, XmNcellRightBorderColor, XmRString, "#606060", 8,
  142.         XtVaTypedArg, XmNcellBottomBorderColor, XmRString, "#606060", 8,
  143.         NULL);
  144.  
  145.     /* Add callbacks which update the Label and to synchronize */
  146.     /* the Text widget outside the Grid with the one inside the Grid */
  147.     XtAddCallback(grid, XmNcellFocusCallback, cellFocus, NULL);
  148.     XtAddCallback(text, XmNvalueChangedCallback, textModify, NULL);
  149.     XtVaGetValues(grid,
  150.         XmNtextWidget, &gridText,
  151.         NULL);
  152.     XtAddCallback(gridText, XmNvalueChangedCallback, textModify, NULL);
  153.  
  154.     XtVaSetValues(gridText,
  155.         XtVaTypedArg, XmNbackground, XmRString, "white", 6,
  156.         NULL);
  157.  
  158.     /* Set the labels on the heading rows and columns */
  159.     for (i = 0; i < 26; i++)
  160.     {
  161.         sprintf(buf, "%c", 'A' + i);
  162.         XmLGridSetStringsPos(grid, XmHEADING, 0, XmCONTENT, i, buf);
  163.     }
  164.     for (i = 0; i < 100; i++)
  165.     {
  166.         sprintf(buf, "%d", i + 1);
  167.         XmLGridSetStringsPos(grid, XmCONTENT, i, XmHEADING, 0, buf);
  168.     }
  169.  
  170.     XtRealizeWidget(shell);
  171.     XtAppMainLoop(app);
  172. }
  173.  
  174.  
  175. void cellFocus(w, clientData, callData)
  176. Widget w;
  177. XtPointer clientData;
  178. XtPointer callData;
  179. {
  180.     XmLGridCallbackStruct *cbs;
  181.     XmLGridRow row;
  182.     XmLGridColumn column;
  183.     Widget sharedText;
  184.     XmString str;
  185.     char buf[10], *c;
  186.  
  187.     cbs = (XmLGridCallbackStruct *)callData;
  188.     if (cbs->reason != XmCR_CELL_FOCUS_IN)
  189.         return;
  190.  
  191.     /* Update the Label to reflect the new focus position */
  192.     sprintf(buf, "(%c %d)", 'A' + cbs->column, cbs->row + 1);
  193.     str = XmStringCreateSimple(buf);
  194.     XtVaSetValues(label,
  195.         XmNlabelString, str,
  196.         NULL);
  197.     XmStringFree(str);
  198.  
  199.     /* Set the Text widget outside the Grid to the string contained */
  200.     /* in the new focus cell.  We set busy here because this change will */
  201.     /* generate a valueChanged callback and we want to notify our */
  202.     /* valueChanged callback not to do any processing because of */
  203.     /* this change */
  204.     if (busy)
  205.         return;
  206.     busy = 1;
  207.     row = XmLGridGetRow(w, cbs->rowType, cbs->row);
  208.     column = XmLGridGetColumn(w, cbs->columnType, cbs->column);
  209.     XtVaGetValues(w,
  210.         XmNrowPtr, row,
  211.         XmNcolumnPtr, column,
  212.         XmNcellString, &str,
  213.         NULL);
  214.     c = 0;
  215.     if (str)
  216.         XmStringGetLtoR(str, XmSTRING_DEFAULT_CHARSET, &c);
  217.     if (c)
  218.     {
  219.         XmTextSetString(text, c);
  220.         XtFree(c);
  221.         XmTextSetSelection(text, 0, XmTextGetLastPosition(text),
  222.             CurrentTime);
  223.     }
  224.     else
  225.         XmTextSetString(text, "");
  226.     if (str)
  227.         XmStringFree(str);
  228.     busy = 0;
  229. }
  230.  
  231. void textModify(w, clientData, callData)
  232. Widget w;
  233. XtPointer clientData;
  234. XtPointer callData;
  235. {
  236.     int row, column;
  237.     Boolean focusIn;
  238.     XmString str;
  239.     char *c;
  240.  
  241.     /* If either Text widget changes (the Grid's Text widget or */
  242.     /* the Text widget outside of the Grid), update the other one */
  243.     /* to reflect to change and update the Grid itself if the */
  244.     /* Text widget outside the Grid changes.  We set busy in this */
  245.     /* function to keep the XmTextSetString() from causing this */
  246.     /* callback to be called while inside the callback. */
  247.     if (busy)
  248.         return;
  249.     busy = 1;
  250.     c = XmTextGetString(w);
  251.     if (w == gridText)
  252.         XmTextSetString(text, c);
  253.     else
  254.     {
  255.         XmLGridGetFocus(grid, &row, &column, &focusIn);
  256.         if (row != -1 && column != -1)
  257.         {
  258.             str = XmStringCreateSimple(c);
  259.             XtVaSetValues(grid,
  260.                 XmNrow, row,
  261.                 XmNcolumn, column,
  262.                 XmNcellString, str,
  263.                 NULL);
  264.             XmStringFree(str);
  265.             XmTextSetString(gridText, c);
  266.         }
  267.     }
  268.     XtFree(c);
  269.     busy = 0;
  270. }
  271.  
  272. void copy(w, clientData, callData)
  273. Widget w;
  274. XtPointer clientData;
  275. XtPointer callData;
  276. {
  277.     XmLGridCopySelected(grid, CurrentTime);
  278. }
  279.  
  280. void paste(w, clientData, callData)
  281. Widget w;
  282. XtPointer clientData;
  283. XtPointer callData;
  284. {
  285.     /* This pastes starting at the current focus cell, an alternative */
  286.     /* method of pasting would be to have the user select an area */
  287.     /* to paste into. To perform this, we could get the selected area */
  288.     /* and use XmLGridPastePos() to paste into that area */
  289.     XmLGridPaste(grid);
  290. }
  291.