Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / examples / tt / broadcast.c
1 /*
2  * (c) Copyright 1995 Digital Equipment Corporation.
3  * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
4  * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
5  * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
6  * (c) Copyright 1993, 1994, 1995 Novell, Inc. 
7  * (c) Copyright 1995 FUJITSU LIMITED.
8  * (c) Copyright 1995 Hitachi.
9  *
10  * $XConsortium: broadcast.c /main/4 1996/08/12 18:34:24 barstow $
11  *
12  * broadcast - dynamic pattern and procedural notification example
13  *
14  */
15
16 #include <stdio.h>
17 #include <sys/param.h>
18 #include <sys/types.h>
19 #include <string.h>
20 #include <Xm/XmAll.h>
21 #include <Tt/tt_c.h>
22
23 Widget toplevel, base_frame, controls, slider, gauge, button;
24 char *my_procid;
25
26 void    broadcast_value();
27 void    receive_tt_message();
28 void    create_ui_components();
29
30
31 void
32 main(argc, argv)
33         int             argc;
34         char            **argv;
35 {
36         int ttfd;
37         Tt_pattern pat;
38         XtAppContext app;
39
40         /*
41          * Initialize Motif and create ui components
42          */
43         toplevel = XtVaAppInitialize(&app, "ttsample1", NULL, 0,
44                                         &argc, argv, NULL, NULL);
45         XtVaSetValues(toplevel, XmNtitle, "ToolTalk Sample 1", 0);
46         create_ui_components();
47
48         /*
49          * Initialize ToolTalk, using the initial default session, and
50          * obtain the file descriptor that will become active whenever
51          * ToolTalk has a message for this process.
52          */
53
54         my_procid = tt_open();
55         ttfd = tt_fd();
56
57         /*
58          * Arrange for Motif to call receive_tt_message when the ToolTalk
59          * file descriptor becomes active.
60          */
61
62         XtAppAddInput(app, ttfd, (XtPointer) XtInputReadMask,
63                       receive_tt_message, 0);
64
65         /*
66          * Create and register a pattern so ToolTalk knows we are interested
67          * in "ttsample1_value" messages within the session we join.
68          */
69
70         pat = tt_pattern_create();
71         tt_pattern_category_set(pat, TT_OBSERVE);
72         tt_pattern_scope_add(pat, TT_SESSION);
73         tt_pattern_op_add(pat, "ttsample1_value");
74         tt_pattern_register(pat);
75
76         /*
77          * Join the default session
78          */
79
80         tt_session_join(tt_default_session());
81
82         /*
83          * Turn control over to Motif.
84          */
85         XtRealizeWidget(toplevel);
86         XtAppMainLoop(app);
87         
88         /*
89          * Before leaving, allow ToolTalk to clean up.
90          */
91         tt_close();
92
93         exit(0);
94 }
95
96
97 /*
98  * When the button is pressed, broadcast the new slider value.
99  */
100 void
101 broadcast_value(widget, client_data, call_data)
102         Widget widget;
103         XtPointer client_data, call_data;
104 {
105         int slider_value;
106         Tt_message msg_out;
107
108         /*
109          * Create and send a ToolTalk notice message
110          * ttsample1_value(in int <new value)
111          */
112
113         XtVaGetValues(slider, XmNvalue, &slider_value, 0);
114         slider_value++;
115         XtVaSetValues(slider, XmNvalue, slider_value, 0);
116
117         msg_out = tt_pnotice_create(TT_SESSION, "ttsample1_value");
118         tt_message_arg_add(msg_out, TT_IN, "integer", NULL);
119         tt_message_arg_ival_set(msg_out, 0, slider_value);
120         tt_message_send(msg_out);
121
122         /*
123          * Since this message is a notice, we don't expect a reply, so
124          * there's no reason to keep a handle for the message.
125          */
126
127         tt_message_destroy(msg_out);
128 }
129
130 /*
131  * When a ToolTalk message is available, receive it; if it's a
132  * ttsample1_value message, update the gauge with the new value.
133  */
134 void
135 receive_tt_message(client_data, fid, id)
136 XtPointer client_data;
137 int *fid;
138 XtInputId *id;
139 {
140         Tt_message msg_in;
141         int mark;
142         int val_in;
143         char *op;
144         Tt_status err;
145
146         msg_in = tt_message_receive();
147
148         /*
149          * It's possible that the file descriptor would become active
150          * even though ToolTalk doesn't really have a message for us.
151          * The returned message handle is NULL in this case.
152          */
153
154         if (msg_in == NULL) return;
155
156
157         /*
158          * Get a storage mark so we can easily free all the data
159          * ToolTalk returns to us.
160          */
161
162         mark = tt_mark();
163
164         op = tt_message_op(msg_in);
165         err = tt_ptr_error(op);
166         if (err > TT_WRN_LAST) {
167                 printf( "tt_message_op(): %s\n", tt_status_message(err));
168         } else if (op != 0) {
169                 if (0==strcmp("ttsample1_value", tt_message_op(msg_in))) {
170                         tt_message_arg_ival(msg_in, 0, &val_in);
171                         XtVaSetValues(gauge, XmNvalue, val_in, 0);
172                 }
173         }
174
175         tt_message_destroy(msg_in);
176         tt_release(mark);
177         return;
178 }
179             
180 /*
181  * Straight Motif calls for creating the ui elements.  No
182  * ToolTalk-specific code here.
183  */
184 void
185 create_ui_components()
186 {
187         int decor;
188         Widget glabel, slabel;
189         XmString label;
190
191         base_frame = XtVaCreateManagedWidget("base_frame",
192                 xmMainWindowWidgetClass, toplevel,
193                 XmNwidth,                250,
194                 XmNheight,               175,
195                 0);
196         XtVaGetValues(base_frame, XmNmwmDecorations, &decor, 0);
197         decor &= ~MWM_DECOR_RESIZEH;
198         XtVaSetValues(base_frame, XmNmwmDecorations, &decor, 0);
199
200         controls = XtVaCreateManagedWidget("controls",
201                 xmFormWidgetClass, base_frame, NULL, 0, 0);
202
203         slabel = XtVaCreateManagedWidget("Send:",
204                 xmLabelWidgetClass, controls,
205                 XmNleftAttachment,  XmATTACH_WIDGET,
206                 XmNtopAttachment,   XmATTACH_WIDGET,
207                 XmNtopWidget,       controls,
208                 XmNtopOffset,           10,
209                 XmNleftOffset,          5,
210                 0);
211         slider = XtVaCreateManagedWidget("slider",
212                 xmScaleWidgetClass, controls,
213                 XmNleftAttachment,  XmATTACH_WIDGET,
214                 XmNleftWidget,      controls,
215                 XmNleftOffset,          10,
216                 XmNtopAttachment,   XmATTACH_WIDGET,
217                 XmNtopWidget,       slabel,
218                 XmNscaleWidth,      225,
219                 XmNscaleHeight,     18,
220                 XmNminimum,         0,
221                 XmNmaximum,         25,
222                 XmNorientation,     XmHORIZONTAL,
223                 XmNshowValue,       TRUE,
224                 0);
225
226         glabel = XtVaCreateManagedWidget("Received:",
227                 xmLabelWidgetClass, controls,
228                 XmNleftAttachment,  XmATTACH_WIDGET,
229                 XmNtopAttachment,   XmATTACH_WIDGET,
230                 XmNleftOffset,          5,
231                 XmNtopWidget,       slider,
232                 0);
233         gauge = XtVaCreateManagedWidget("gauge",
234                 xmScaleWidgetClass, controls,
235                 XmNleftAttachment,  XmATTACH_WIDGET,
236                 XmNleftWidget,      controls,
237                 XmNleftOffset,          10,
238                 XmNtopAttachment,   XmATTACH_WIDGET,
239                 XmNtopWidget,       glabel,
240                 XmNorientation,     XmHORIZONTAL,
241                 XmNscaleWidth,      225,
242                 XmNscaleHeight,     18,
243                 XmNminimum,         0,
244                 XmNmaximum,         25,
245                 XmNshowValue,       TRUE,
246                 0);
247
248         label = XmStringCreateSimple("Broadcast");
249         button = XtVaCreateManagedWidget("button",
250                 xmPushButtonWidgetClass, controls,
251                 XmNtopWidget,           gauge,
252                 XmNtopAttachment,       XmATTACH_WIDGET,
253                 XmNtopOffset,           5,
254                 XmNleftOffset,          75,
255                 XmNleftWidget,          controls,
256                 XmNleftAttachment,  XmATTACH_WIDGET,
257                 XmNbottomOffset,        5,
258                 XmNlabelString, label,
259                 0);
260         XmStringFree(label);
261         XtAddCallback(button, XmNactivateCallback, broadcast_value, 0);
262 }