File: colordemo.c

package info (click to toggle)
motif 2.3.8-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 36,432 kB
  • sloc: ansic: 452,643; sh: 4,613; makefile: 2,030; yacc: 1,604; lex: 352; cpp: 348
file content (418 lines) | stat: -rw-r--r-- 12,644 bytes parent folder | download | duplicates (7)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <Xm/Xm.h>
#include <Xm/MessageB.h>
#include <Xm/PushB.h>
#include <Xm/Label.h>
#include <Xm/Frame.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/ScrolledW.h>
#include <Xm/ColorS.h>
#include <Xm/Paned.h>
#include <Xm/ButtonBox.h>


/*
 *
 * The following defines help in clarifying the data being passed.
 *
 */

#define EXPLAIN_MODE 1
#define EXPLAIN_NAME 2
#define EXPLAIN_MAR 3
#define EXPLAIN_RGB 4

void Exit(Widget w, XtPointer client, XtPointer call);
void Explain(Widget w, XtPointer client, XtPointer call);
void GetColor(Widget w, XtPointer client, XtPointer call);

/*
 *
 * Structure to pass data back and forth using a callbacks' client_data
 *
 */

typedef struct _Holder{
    Widget colorBtn;
    Widget colorWidget;
    XColor the_color;
    Colormap the_map;
    } holderStruct;
    
/*
 *
 * These are fallbacks that are designed to ensure the
 * program works properly in case the resource file is
 * not picked up.
 *
 */

static String fallbacks[] = {
    /*
     * General fallback resources.
     */ 
    "*background: grey", 
    "*colorSelector*background: white",
    "*fontList: -*-helvetica-medium-r-*-*-*-140-*-*-*-*-*-*",
    "*infolabel*fontList: -*-helvetica-bold-r-*-*-*-140-*-*-*-*-*-*",
    "*infolabel.marginWidth: 10",
    "*infolabel.marginHeight: 10",
    "*buttonBox1*XmPushButton.labelString: Explain...",
    "*rightpane*colModeLabel.labelString: XmNcolorMode",
    "*rightpane*colNameLabel.labelString: XmNcolorName",
    "*rightpane*marLabel.labelString: XmNmarginWidth, XmNmarginHeight",
    "*rightpane*rgbFileLabel.labelString: XmNrgbFile",
    "*quitPB.labelString: Quit",
    "*colorPB.labelString: Color",
    "*buttonBox1*XmLabel.alignment: XmALIGNMENT_BEGINNING",
    NULL,
};

void Exit(Widget w, XtPointer client, XtPointer call)
{
    exit(0);
}

/*
 *
 * Stuff needed for the demo program to output info
 *
 */

void Explain(Widget w, XtPointer client, XtPointer call)
{
    Arg args[5];
    Cardinal argcnt;
    XmString xmstring=NULL;
    static Widget info = NULL;
    int explain = (int) client;

    if (info == NULL) {
        Widget temp;

        argcnt = 0;
        XtSetArg(args[argcnt], XmNtitle, "Explanation"); argcnt++;
        info = XmCreateInformationDialog(w, "explain", args, argcnt);

        temp = XmMessageBoxGetChild(info, XmDIALOG_CANCEL_BUTTON);
        XtUnmanageChild(temp);
        temp = XmMessageBoxGetChild(info, XmDIALOG_HELP_BUTTON);
        XtUnmanageChild(temp);
    }

    switch (explain) {
    case EXPLAIN_MODE:
        xmstring = XmStringCreateLtoR(
"This resouce may be either XmListMode and XmScaleMode. This\n\
determines what mode the Color Selector should use when it is\n\
created.\n\
\n\
List Mode presents a listing of colors built up from the\n\
XmNrgbFile resource. Scale Mode displays three sliders that\n\
adjust Red, Green, and Blue color values.", 
                                      XmSTRING_DEFAULT_CHARSET);
        break;
    case EXPLAIN_NAME:
        xmstring = XmStringCreateLtoR(
"This resource controls the name of the color that is displayed\n\
to the user. Using XtGetValues on this resource returns the name\n\
of the current color. Using XtSetValues on this resource sets the\n\
current color.\n\
\n\
This resource is of type String.",
                                      XmSTRING_DEFAULT_CHARSET);
        break;
    case EXPLAIN_MAR:
        xmstring = XmStringCreateLtoR(
"The Color Selector is made up of many children. This resource\n\
adjusts the remaining space between a) each of the children in the\n\
Color Selector, and b) the outside children and the edge of the\n\
Color Selector.",

                                      XmSTRING_DEFAULT_CHARSET);
        break;
    case EXPLAIN_RGB:
        xmstring = XmStringCreateLtoR(
"The name of a file that contains valid color names and their\n\
appropriate values.\n\
\n\
All duplicates are removed, and the list is sorted, before it is\n\
shown to the user.",

                                      XmSTRING_DEFAULT_CHARSET);
        break;
    default:
        printf("explaining NOTHING\n");
        break;
    }

    argcnt = 0;
    XtSetArg(args[argcnt], XmNmessageString, xmstring); argcnt++;
    XtSetValues(info, args, argcnt);

    XmStringFree(xmstring);

    XtManageChild(info);
}

static XmString
CreateHelpArea()
{
  XmString xmstr = 
    XmStringCreateLtoR(
"The Motif Color Selector is an interface to the colors \n\
available on a display.\n\
\n\
In Scale Mode, the position of the sliders \n\
determines RGB values of the current color, and displays the \n\
pixel value in the colored area at the bottom.\n\
In List Mode, a list of all the named colors in the current\n\
rgb.txt file are shown. Selecting from this list updates\n\
the color display area with the associated pixel value.\n\
The ColorSelector will attempt to show the closest approximation\n\
to the color selected in one mode when switching to another\n\
if it cannot find an exact match .\n\
\n\
A developer may get the current color value programmatically\n\
through XtGetValues. An example of how this is used is shown\n\
by the selecting the Color Button at the bottom of the right \n\
half of the demo. The value obtained from the ColorSelector\n\
was used to set the backgroundColor of the Color Button.\n\
\n\
Resources defined by the Color Selector are shown below. For more\n\
information on a resource, select the Explain button to \n\
its right.", XmSTRING_DEFAULT_CHARSET);

  return( xmstr );
}



/*
 *
 * This function demonstrates the use of the new widget.  
 * The color generated by the Color Selector Widget is used
 * to change the background of the push button calling this 
 * routine.
 *
 */

void GetColor(Widget w, XtPointer client, XtPointer call)
{
    Arg args[5];
    Cardinal argcnt;
    String color_name;

    holderStruct holder = *(holderStruct *)client;

    argcnt = 0;
    XtSetArg(args[argcnt], XmNcolorName, &color_name); argcnt++;
    XtGetValues(holder.colorWidget, args, argcnt);

    XParseColor(XtDisplay(w), holder.the_map, color_name, &(holder.the_color));
    XAllocColor(XtDisplay(w), holder.the_map, &(holder.the_color));

    argcnt = 0;
    XtSetArg(args[argcnt], XmNbackground, holder.the_color.pixel); argcnt++;
    XtSetValues(holder.colorBtn, args, argcnt);

}
int
main(int argc, char **argv)
{
    Arg args[5];
    Cardinal argcnt;
    Widget top, colorframe, colorSelector, infoframe, infolabel,
    buttonBox, rbuttonBox, lbuttonBox, colModeLabel, pushButton,
    big_pane, right_pane, left_pane, explain_scrolled, 
    colNameLabel, marLabel, rgbFileLabel, quitPB, colorPB;
    XtAppContext app;

    XmString      xmstring;
    Colormap      default_map;
    XColor        the_color;
    unsigned long mask;
    
    holderStruct holder;
    
    XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL); 

    argcnt = 0;
    XtSetArg(args[argcnt], XmNtitle, "Color Selector Demo"); argcnt++;
    XtSetArg(args[argcnt], XmNallowShellResize, True); argcnt++;
    top = XtOpenApplication(
            &app, 
            "ColorSelector", 
            NULL, 
            0,
            &argc, 
            argv, 
            fallbacks,  
            sessionShellWidgetClass, 
            args, argcnt);

    default_map = DefaultColormapOfScreen(XtScreen(top));

    /* big pane */
    argcnt = 0;
    XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
    big_pane = XtCreateManagedWidget("bull",
		xmPanedWidgetClass, top, args, argcnt);

    colorframe = XtVaCreateManagedWidget("colorframe", xmFrameWidgetClass,
					 big_pane, 
					 XmNshowSash, False,
					 NULL);

    colorSelector = XtVaCreateManagedWidget("colorSelector",
					    xmColorSelectorWidgetClass, 
					    colorframe, NULL);

    /* right pane */
    argcnt = 0;
    XtSetArg(args[argcnt], XmNshowSash, False); argcnt++;
    XtSetArg(args[argcnt], XmNskipAdjust, False ); argcnt++;    
    XtSetArg(args[argcnt], XmNorientation, XmVERTICAL); argcnt++;
    right_pane = XtCreateManagedWidget("rightpane", xmPanedWidgetClass,
				   big_pane, args,argcnt);

    infoframe = XtVaCreateManagedWidget("infoframe", xmFrameWidgetClass, 
					right_pane, 
					XmNshowSash, False,
					XmNresizeToPreferred, True,
					XmNpreferredPaneSize, 300,
					XmNallowResize, True, NULL);

    argcnt = 0;
    XtSetArg(args[argcnt], XmNscrollingPolicy, XmAUTOMATIC ); argcnt++;
    XtSetArg(args[argcnt], XmNscrollBarDisplayPolicy, XmAS_NEEDED ); 
    argcnt++;
    explain_scrolled = XtCreateManagedWidget( "scrollExplain", 
					     xmScrolledWindowWidgetClass,
					     infoframe, args, argcnt );
    
    xmstring = CreateHelpArea();

    infolabel = XtVaCreateManagedWidget("infolabel", xmLabelWidgetClass,
					explain_scrolled,
					XmNalignment, XmALIGNMENT_BEGINNING,
					XmNlabelString, xmstring,
					NULL);
    
    /* Resources and their associated Explain buttons */

				       
    argcnt = 0;
    XtSetArg(args[argcnt], XmNequalSize, True); argcnt++;
    XtSetArg(args[argcnt], XmNfillOption, XmFillAll); argcnt++;
    XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
    XtSetArg(args[argcnt], XmNshowSash, False); argcnt++;
    XtSetArg(args[argcnt], XmNskipAdjust, True); argcnt++;
    buttonBox = XtCreateManagedWidget("buttonBox1",xmButtonBoxWidgetClass,
				      right_pane, args, argcnt);
    argcnt = 0;
    XtSetArg(args[argcnt], XmNequalSize, True); argcnt++;
    XtSetArg(args[argcnt], XmNfillOption, XmFillMinor); argcnt++;
    XtSetArg(args[argcnt], XmNorientation, XmVERTICAL); argcnt++;
    lbuttonBox = XtCreateManagedWidget("buttonBoxl", xmButtonBoxWidgetClass,
				       buttonBox, args, argcnt);

    argcnt = 0;
    XtSetArg(args[argcnt], XmNequalSize, True); argcnt++;
    XtSetArg(args[argcnt], XmNfillOption, XmFillMinor); argcnt++;
    XtSetArg(args[argcnt], XmNorientation, XmVERTICAL); argcnt++;
    rbuttonBox = XtCreateManagedWidget("buttonBoxr",
				xmButtonBoxWidgetClass,
				buttonBox, args, argcnt);

    colModeLabel = XtVaCreateManagedWidget("colModeLabel",
					   xmLabelWidgetClass,
					   lbuttonBox, NULL);

    pushButton = XtVaCreateManagedWidget("pushButton",
					 xmPushButtonWidgetClass,
					 rbuttonBox, NULL);
    XtAddCallback(pushButton, XmNactivateCallback, Explain, 
		  (XtPointer)EXPLAIN_MODE);


    colNameLabel = XtVaCreateManagedWidget("colNameLabel",
					   xmLabelWidgetClass,
					   lbuttonBox, NULL);

    pushButton = XtVaCreateManagedWidget("pushButton1",
					  xmPushButtonWidgetClass, 
					  rbuttonBox, NULL);
    XtAddCallback(pushButton, XmNactivateCallback, Explain, 
		  (XtPointer)EXPLAIN_NAME);

    marLabel = XtVaCreateManagedWidget("marLabel", xmLabelWidgetClass, 
					   lbuttonBox, NULL);

    pushButton = XtVaCreateManagedWidget("pushButton2",
					  xmPushButtonWidgetClass, 
					  rbuttonBox, NULL);
    XtAddCallback(pushButton, XmNactivateCallback, Explain, 
		  (XtPointer)EXPLAIN_MAR);

    rgbFileLabel = XtVaCreateManagedWidget("rgbFileLabel",
					   xmLabelWidgetClass,
					   lbuttonBox, NULL);

    pushButton = XtVaCreateManagedWidget("pushButton3",
					  xmPushButtonWidgetClass, 
					  rbuttonBox, NULL);

    XtAddCallback(pushButton, XmNactivateCallback, Explain, 
		  (XtPointer)EXPLAIN_RGB);

                           
/*
 *  Take Note:
 *
 * The following push button demostrates the point in using
 * the colorSelector widget. 
 *
 */

    argcnt = 0;
    XtSetArg(args[argcnt], XmNequalSize, True); argcnt++;
    XtSetArg(args[argcnt], XmNfillOption, XmFillNone); argcnt++;
    XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
    XtSetArg(args[argcnt], XmNshowSash, False); argcnt++;
    XtSetArg(args[argcnt], XmNskipAdjust, True); argcnt++;
    buttonBox = XtCreateManagedWidget("buttonBox2",
				xmButtonBoxWidgetClass,
				right_pane, args, argcnt);

    colorPB = XtVaCreateManagedWidget("Color", xmPushButtonWidgetClass,
				      buttonBox, NULL );

    quitPB = XtVaCreateManagedWidget("quitPB", xmPushButtonWidgetClass, 
				      buttonBox, NULL );


    /* finished all the interface pieces, now do all the rest */

    XAllocColorCells(XtDisplay(top), default_map, False, 
                               &mask, 0, &the_color.pixel, 1);
    holder.colorBtn = colorPB;
    holder.colorWidget = colorSelector;
    holder.the_color = the_color;
    holder.the_map = default_map;

    XtAddCallback(colorPB, XmNactivateCallback, GetColor, &holder);

    XtAddCallback(quitPB, XmNactivateCallback, Exit, (XtPointer)0);

    XtRealizeWidget(top);

    XtAppMainLoop(app);
    return(0);
}