#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Text.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <XmL/Grid.h>
Widget label, text, grid, gridText;
static int busy = 0;
void cellFocus();
void textModify();
void copy();
void paste();
main(argc, argv)
int argc;
char *argv[];
{
XtAppContext app;
Widget shell, form, copyButton, pasteButton;
XmString str;
char buf[4];
int i;
shell = XtAppInitialize(&app, "Grid3", NULL, 0,
&argc, argv, NULL, NULL, 0);
form = XtVaCreateManagedWidget("form",
xmFormWidgetClass, shell,
XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
XmNshadowThickness, 0,
NULL);
str = XmStringCreateSimple("(A 1)");
label = XtVaCreateManagedWidget("label",
xmLabelWidgetClass, form,
XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
XtVaTypedArg, XmNforeground, XmRString, "black", 6,
XmNtopAttachment, XmATTACH_FORM,
XmNmarginHeight, 4,
XmNleftAttachment, XmATTACH_FORM,
XmNlabelString, str,
NULL);
XmStringFree(str);
pasteButton = XtVaCreateManagedWidget("Paste To Focus",
xmPushButtonWidgetClass, form,
XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
XtVaTypedArg, XmNforeground, XmRString, "black", 6,
XmNrightAttachment, XmATTACH_FORM,
XmNmarginHeight, 0,
NULL);
XtAddCallback(pasteButton, XmNactivateCallback, paste, NULL);
copyButton = XtVaCreateManagedWidget("Copy Selected",
xmPushButtonWidgetClass, form,
XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
XtVaTypedArg, XmNforeground, XmRString, "black", 6,
XmNrightAttachment, XmATTACH_WIDGET,
XmNrightWidget, pasteButton,
XmNmarginHeight, 0,
NULL);
XtAddCallback(copyButton, XmNactivateCallback, copy, NULL);
text = XtVaCreateManagedWidget("text",
xmTextWidgetClass, form,
XtVaTypedArg, XmNbackground, XmRString, "white", 6,
XtVaTypedArg, XmNforeground, XmRString, "black", 6,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, label,
XmNrightAttachment, XmATTACH_WIDGET,
XmNrightWidget, copyButton,
XmNmarginHeight, 0,
NULL);
grid = XtVaCreateManagedWidget("grid",
xmlGridWidgetClass, form,
XtVaTypedArg, XmNbackground, XmRString, "#C0C0C0", 8,
XtVaTypedArg, XmNforeground, XmRString, "black", 6,
XmNheadingColumns, 1,
XmNcolumns, 26,
XmNvisibleColumns, 8,
XmNhsbDisplayPolicy, XmSTATIC,
XmNrows, 100,
XmNheadingRows, 1,
XmNvisibleRows, 12,
XmNvsbDisplayPolicy, XmSTATIC,
XmNallowDragSelected, True,
XmNallowDrop, True,
XmNallowRowResize, True,
XmNallowColumnResize, True,
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, text,
XmNtopOffset, 2,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNshadowThickness, 0,
XmNselectionPolicy, XmSELECT_CELL,
NULL);
XtVaSetValues(grid,
XmNcellEditable, True,
XtVaTypedArg, XmNcellBackground, XmRString, "white", 6,
XmNcellAlignment, XmALIGNMENT_RIGHT,
XmNcellTopBorderType, XmBORDER_NONE,
XmNcellLeftBorderType, XmBORDER_NONE,
XtVaTypedArg, XmNcellRightBorderColor, XmRString, "#606060", 8,
XtVaTypedArg, XmNcellBottomBorderColor, XmRString, "#606060", 8,
NULL);
XtAddCallback(grid, XmNcellFocusCallback, cellFocus, NULL);
XtAddCallback(text, XmNvalueChangedCallback, textModify, NULL);
XtVaGetValues(grid,
XmNtextWidget, &gridText,
NULL);
XtAddCallback(gridText, XmNvalueChangedCallback, textModify, NULL);
XtVaSetValues(gridText,
XtVaTypedArg, XmNbackground, XmRString, "white", 6,
NULL);
for (i = 0; i < 26; i++)
{
sprintf(buf, "%c", 'A' + i);
XmLGridSetStringsPos(grid, XmHEADING, 0, XmCONTENT, i, buf);
}
for (i = 0; i < 100; i++)
{
sprintf(buf, "%d", i + 1);
XmLGridSetStringsPos(grid, XmCONTENT, i, XmHEADING, 0, buf);
}
XtRealizeWidget(shell);
XtAppMainLoop(app);
}
void cellFocus(w, clientData, callData)
Widget w;
XtPointer clientData;
XtPointer callData;
{
XmLGridCallbackStruct *cbs;
XmLGridRow row;
XmLGridColumn column;
Widget sharedText;
XmString str;
char buf[10], *c;
cbs = (XmLGridCallbackStruct *)callData;
if (cbs->reason != XmCR_CELL_FOCUS_IN)
return;
sprintf(buf, "(%c %d)", 'A' + cbs->column, cbs->row + 1);
str = XmStringCreateSimple(buf);
XtVaSetValues(label,
XmNlabelString, str,
NULL);
XmStringFree(str);
if (busy)
return;
busy = 1;
row = XmLGridGetRow(w, cbs->rowType, cbs->row);
column = XmLGridGetColumn(w, cbs->columnType, cbs->column);
XtVaGetValues(w,
XmNrowPtr, row,
XmNcolumnPtr, column,
XmNcellString, &str,
NULL);
c = 0;
if (str)
XmStringGetLtoR(str, XmSTRING_DEFAULT_CHARSET, &c);
if (c)
{
XmTextSetString(text, c);
XtFree(c);
XmTextSetSelection(text, 0, XmTextGetLastPosition(text),
CurrentTime);
}
else
XmTextSetString(text, "");
if (str)
XmStringFree(str);
busy = 0;
}
void textModify(w, clientData, callData)
Widget w;
XtPointer clientData;
XtPointer callData;
{
int row, column;
Boolean focusIn;
XmString str;
char *c;
if (busy)
return;
busy = 1;
c = XmTextGetString(w);
if (w == gridText)
XmTextSetString(text, c);
else
{
XmLGridGetFocus(grid, &row, &column, &focusIn);
if (row != -1 && column != -1)
{
str = XmStringCreateSimple(c);
XtVaSetValues(grid,
XmNrow, row,
XmNcolumn, column,
XmNcellString, str,
NULL);
XmStringFree(str);
XmTextSetString(gridText, c);
}
}
XtFree(c);
busy = 0;
}
void copy(w, clientData, callData)
Widget w;
XtPointer clientData;
XtPointer callData;
{
XmLGridCopySelected(grid, CurrentTime);
}
void paste(w, clientData, callData)
Widget w;
XtPointer clientData;
XtPointer callData;
{
XmLGridPaste(grid);
}