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