1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 #include <Xm/Xm.h>
51 #include <Xm/Form.h>
52 #include <Xm/Text.h>
53 #include <Xm/Label.h>
54 #include <Xm/PushB.h>
55 #include <XmL/Grid.h>
56
57 Widget label, text, grid, gridText;
58
59 static int busy =
0;
60
61 void cellFocus();
62 void textModify();
63 void copy();
64 void paste();
65
66 main(argc, argv)
67 int argc;
68 char *argv[];
69 {
70 XtAppContext app;
71 Widget shell, form, copyButton, pasteButton;
72 XmString str;
73 char buf[
4];
74 int i;
75
76 shell = XtAppInitialize(&app,
"Grid3",
NULL,
0,
77 &argc, argv,
NULL,
NULL,
0);
78
79 form = XtVaCreateManagedWidget(
"form",
80 xmFormWidgetClass, shell,
81 XtVaTypedArg, XmNbackground, XmRString,
"#C0C0C0",
8,
82 XmNshadowThickness,
0,
83 NULL);
84
85 str = XmStringCreateSimple(
"(A 1)");
86 label = XtVaCreateManagedWidget(
"label",
87 xmLabelWidgetClass, form,
88 XtVaTypedArg, XmNbackground, XmRString,
"#C0C0C0",
8,
89 XtVaTypedArg, XmNforeground, XmRString,
"black",
6,
90 XmNtopAttachment, XmATTACH_FORM,
91 XmNmarginHeight,
4,
92 XmNleftAttachment, XmATTACH_FORM,
93 XmNlabelString, str,
94 NULL);
95 XmStringFree(str);
96
97 pasteButton = XtVaCreateManagedWidget(
"Paste To Focus",
98 xmPushButtonWidgetClass, form,
99 XtVaTypedArg, XmNbackground, XmRString,
"#C0C0C0",
8,
100 XtVaTypedArg, XmNforeground, XmRString,
"black",
6,
101 XmNrightAttachment, XmATTACH_FORM,
102 XmNmarginHeight,
0,
103 NULL);
104 XtAddCallback(pasteButton, XmNactivateCallback, paste,
NULL);
105
106 copyButton = XtVaCreateManagedWidget(
"Copy Selected",
107 xmPushButtonWidgetClass, form,
108 XtVaTypedArg, XmNbackground, XmRString,
"#C0C0C0",
8,
109 XtVaTypedArg, XmNforeground, XmRString,
"black",
6,
110 XmNrightAttachment, XmATTACH_WIDGET,
111 XmNrightWidget, pasteButton,
112 XmNmarginHeight,
0,
113 NULL);
114 XtAddCallback(copyButton, XmNactivateCallback, copy,
NULL);
115
116 text = XtVaCreateManagedWidget(
"text",
117 xmTextWidgetClass, form,
118 XtVaTypedArg, XmNbackground, XmRString,
"white",
6,
119 XtVaTypedArg, XmNforeground, XmRString,
"black",
6,
120 XmNtopAttachment, XmATTACH_FORM,
121 XmNleftAttachment, XmATTACH_WIDGET,
122 XmNleftWidget, label,
123 XmNrightAttachment, XmATTACH_WIDGET,
124 XmNrightWidget, copyButton,
125 XmNmarginHeight,
0,
126 NULL);
127
128
129 grid = XtVaCreateManagedWidget(
"grid",
130 xmlGridWidgetClass, form,
131 XtVaTypedArg, XmNbackground, XmRString,
"#C0C0C0",
8,
132 XtVaTypedArg, XmNforeground, XmRString,
"black",
6,
133 XmNheadingColumns,
1,
134 XmNcolumns,
26,
135 XmNvisibleColumns,
8,
136 XmNhsbDisplayPolicy, XmSTATIC,
137 XmNrows,
100,
138 XmNheadingRows,
1,
139 XmNvisibleRows,
12,
140 XmNvsbDisplayPolicy, XmSTATIC,
141 XmNallowDragSelected, True,
142 XmNallowDrop, True,
143 XmNallowRowResize, True,
144 XmNallowColumnResize, True,
145 XmNtopAttachment, XmATTACH_WIDGET,
146 XmNtopWidget, text,
147 XmNtopOffset,
2,
148 XmNbottomAttachment, XmATTACH_FORM,
149 XmNleftAttachment, XmATTACH_FORM,
150 XmNrightAttachment, XmATTACH_FORM,
151 XmNshadowThickness,
0,
152 XmNselectionPolicy, XmSELECT_CELL,
153 NULL);
154
155
156
157 XtVaSetValues(grid,
158 XmNcellEditable, True,
159 XtVaTypedArg, XmNcellBackground, XmRString,
"white",
6,
160 XmNcellAlignment, XmALIGNMENT_RIGHT,
161 XmNcellTopBorderType, XmBORDER_NONE,
162 XmNcellLeftBorderType, XmBORDER_NONE,
163 XtVaTypedArg, XmNcellRightBorderColor, XmRString,
"#606060",
8,
164 XtVaTypedArg, XmNcellBottomBorderColor, XmRString,
"#606060",
8,
165 NULL);
166
167
168
169 XtAddCallback(grid, XmNcellFocusCallback, cellFocus,
NULL);
170 XtAddCallback(text, XmNvalueChangedCallback, textModify,
NULL);
171 XtVaGetValues(grid,
172 XmNtextWidget, &gridText,
173 NULL);
174 XtAddCallback(gridText, XmNvalueChangedCallback, textModify,
NULL);
175
176 XtVaSetValues(gridText,
177 XtVaTypedArg, XmNbackground, XmRString,
"white",
6,
178 NULL);
179
180
181 for (i =
0; i <
26; i++)
182 {
183 sprintf(buf,
"%c",
'A' + i);
184 XmLGridSetStringsPos(grid, XmHEADING,
0, XmCONTENT, i, buf);
185 }
186 for (i =
0; i <
100; i++)
187 {
188 sprintf(buf,
"%d", i +
1);
189 XmLGridSetStringsPos(grid, XmCONTENT, i, XmHEADING,
0, buf);
190 }
191
192 XtRealizeWidget(shell);
193 XtAppMainLoop(app);
194 }
195
196
197 void cellFocus(w, clientData, callData)
198 Widget w;
199 XtPointer clientData;
200 XtPointer callData;
201 {
202 XmLGridCallbackStruct *cbs;
203 XmLGridRow row;
204 XmLGridColumn column;
205 Widget sharedText;
206 XmString str;
207 char buf[
10], *c;
208
209 cbs = (XmLGridCallbackStruct *)callData;
210 if (cbs->reason != XmCR_CELL_FOCUS_IN)
211 return;
212
213
214 sprintf(buf,
"(%c %d)",
'A' + cbs->column, cbs->row +
1);
215 str = XmStringCreateSimple(buf);
216 XtVaSetValues(label,
217 XmNlabelString, str,
218 NULL);
219 XmStringFree(str);
220
221
222
223
224
225
226 if (busy)
227 return;
228 busy =
1;
229 row = XmLGridGetRow(w, cbs->rowType, cbs->row);
230 column = XmLGridGetColumn(w, cbs->columnType, cbs->column);
231 XtVaGetValues(w,
232 XmNrowPtr, row,
233 XmNcolumnPtr, column,
234 XmNcellString, &str,
235 NULL);
236 c =
0;
237 if (str)
238 XmStringGetLtoR(str, XmSTRING_DEFAULT_CHARSET, &c);
239 if (c)
240 {
241 XmTextSetString(text, c);
242 XtFree(c);
243 XmTextSetSelection(text,
0, XmTextGetLastPosition(text),
244 CurrentTime);
245 }
246 else
247 XmTextSetString(text,
"");
248 if (str)
249 XmStringFree(str);
250 busy =
0;
251 }
252
253 void textModify(w, clientData, callData)
254 Widget w;
255 XtPointer clientData;
256 XtPointer callData;
257 {
258 int row, column;
259 Boolean focusIn;
260 XmString str;
261 char *c;
262
263
264
265
266
267
268
269 if (busy)
270 return;
271 busy =
1;
272 c = XmTextGetString(w);
273 if (w == gridText)
274 XmTextSetString(text, c);
275 else
276 {
277 XmLGridGetFocus(grid, &row, &column, &focusIn);
278 if (row != -
1 && column != -
1)
279 {
280 str = XmStringCreateSimple(c);
281 XtVaSetValues(grid,
282 XmNrow, row,
283 XmNcolumn, column,
284 XmNcellString, str,
285 NULL);
286 XmStringFree(str);
287 XmTextSetString(gridText, c);
288 }
289 }
290 XtFree(c);
291 busy =
0;
292 }
293
294 void copy(w, clientData, callData)
295 Widget w;
296 XtPointer clientData;
297 XtPointer callData;
298 {
299 XmLGridCopySelected(grid, CurrentTime);
300 }
301
302 void paste(w, clientData, callData)
303 Widget w;
304 XtPointer clientData;
305 XtPointer callData;
306 {
307
308
309
310
311 XmLGridPaste(grid);
312 }
313