-fpermissive to allow GCC to compile old C++
[oweals/cde.git] / cde / programs / dtmail / dtmail / IndexedOptionMenu.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 /* $TOG: IndexedOptionMenu.C /main/3 1997/11/21 18:42:21 mgreess $ */
24
25 /*
26  *+SNOTICE
27  *
28  *      $:$
29  *
30  *      RESTRICTED CONFIDENTIAL INFORMATION:
31  *      
32  *      The information in this document is subject to special
33  *      restrictions in a confidential disclosure agreement between
34  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
35  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
36  *      Sun's specific written approval.  This document and all copies
37  *      and derivative works thereof must be returned or destroyed at
38  *      Sun's request.
39  *
40  *      Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
41  *
42  *+ENOTICE
43  */
44 /*
45  *                   Common Desktop Environment
46  *
47  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
48  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
49  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
50  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
51  *   (c) Copyright 1995 Digital Equipment Corp.
52  *   (c) Copyright 1995 Fujitsu Limited
53  *   (c) Copyright 1995 Hitachi, Ltd.
54  *                                                                   
55  *
56  *                     RESTRICTED RIGHTS LEGEND                              
57  *
58  *Use, duplication, or disclosure by the U.S. Government is subject to
59  *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
60  *Technical Data and Computer Software clause in DFARS 252.227-7013.  Rights
61  *for non-DOD U.S. Government Departments and Agencies are as set forth in
62  *FAR 52.227-19(c)(1,2).
63
64  *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
65  *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A. 
66  *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
67  *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
68  *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
69  *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
70  *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
71  */
72
73 #include <stdio.h>
74 #include <X11/Intrinsic.h>
75 #include <Xm/Xm.h>
76 #include <Xm/DialogS.h>
77 #include <Xm/Form.h>
78 #include <Xm/PushB.h>
79 #include <Xm/RowColumn.h>
80 #include <Xm/ToggleB.h>
81
82 #include "Dmx.h"
83 #include "IndexedOptionMenu.h"
84 #include "MailMsg.h"
85 #include "dtmailopts.h"
86
87 IndexedOptionMenu::IndexedOptionMenu (
88                                 Widget  parent,
89                                 int     nmenu_items,
90                                 char    **strings,
91                                 void    **data
92                                 ) : UIComponent( "IndexedOptionMenu" )
93 {
94     int         nargs;
95     Arg         args[8];
96     Widget      menu;
97     XmString    xms;
98
99     _nmenu_items = nmenu_items;
100     if (nmenu_items && strings != NULL)
101     {
102         _strings = (char **) XtMalloc(nmenu_items * sizeof(char*));
103         for (int i=0; i<nmenu_items; i++)
104           _strings[i] = strings[i];
105     }
106     if (nmenu_items && data != NULL)
107     {
108         _data = (void **) XtMalloc(nmenu_items * sizeof(void*));
109         for (int i=0; i<nmenu_items; i++)
110           _data[i] = data[i];
111     }
112
113     /*
114      * Create the pulldown menu for the option menus,
115      * and populate it with pushbuttons.
116      * Attach the original strings used to generate
117      * the button labels to the buttons as userData.
118      */
119     menu = XmCreatePulldownMenu(parent, "Menu", NULL, 0);
120     _buttons = (Widget *) XtMalloc( _nmenu_items * sizeof(Widget) );
121     for (int i=0; i<_nmenu_items; i++)
122     {
123         static char     button_label[32];
124
125         sprintf(button_label, "Button%d", i);
126         _buttons[i] = XmCreatePushButton(menu, button_label, NULL, 0);
127         xms = XmStringCreateLocalized(_strings[i]);
128         XtVaSetValues(
129                 _buttons[i],
130                 XmNuserData, i,
131                 XmNlabelString, xms,
132                 NULL);
133         XmStringFree(xms);
134         XtManageChild(_buttons[i]);
135     }
136
137     nargs=0;
138     XtSetArg(args[nargs], XmNsubMenuId, menu); nargs++;
139     _w = XmCreateOptionMenu(parent, "IndexedOptionMenu", args, nargs);
140     installDestroyHandler();
141
142     //XtRealizeWidget(_w);
143 }
144
145 IndexedOptionMenu::IndexedOptionMenu (
146                                 Widget  option_menu,
147                                 int     nmenu_items,
148                                 char    **strings,
149                                 void    **data,
150                                 Widget  *buttons
151                                 ) : UIComponent( "IndexedOptionMenu" )
152 {
153     _w = option_menu;
154     installDestroyHandler();
155     _nmenu_items = nmenu_items;
156     _strings = NULL;
157     _data = NULL;
158     _buttons = NULL;
159
160     if (nmenu_items && strings != NULL)
161     {
162         _strings = (char **) XtMalloc(nmenu_items * sizeof(char*));
163         for (int i=0; i<nmenu_items; i++)
164           _strings[i] = strings[i];
165     }
166     if (nmenu_items && data != NULL)
167     {
168         _data = (void **) XtMalloc(nmenu_items * sizeof(void*));
169         for (int i=0; i<nmenu_items; i++)
170           _data[i] = data[i];
171     }
172     if (nmenu_items && buttons != NULL)
173     {
174         _buttons = (Widget *) XtMalloc( _nmenu_items * sizeof(Widget) );
175         for (int i=0; i<nmenu_items; i++)
176           _buttons[i] = buttons[i];
177     }
178 }
179
180
181 IndexedOptionMenu::~IndexedOptionMenu (void)
182 {
183     if (_w != NULL)
184       XtDestroyWidget(_w);
185     if (_buttons)
186       XtFree((char *) _buttons);
187     if (_strings)
188       XtFree((char *) _strings);
189     if (_data)
190       XtFree((char *) _data);
191 }
192
193 void
194 IndexedOptionMenu::addMenuButtonCallback(
195                                         char* callback_name,
196                                         XtCallbackProc callback,
197                                         XtPointer client_data)
198 {
199     for (int i=0; i<_nmenu_items; i++)
200       XtAddCallback(_buttons[i], callback_name, callback, client_data);
201 }
202
203 void*
204 IndexedOptionMenu::getDataSpec (void)
205 {
206     int index = getIndexSpec();
207     return _data[index];
208 }
209
210 int
211 IndexedOptionMenu::getIndexSpec (void)
212 {
213     int         data = 0;
214
215     if (_w)
216     {
217         Widget  selected;
218         XtVaGetValues(_w, XmNmenuHistory, &selected, NULL);
219         XtVaGetValues(selected, XmNuserData, &data, NULL);
220     }
221     return data;
222 }
223
224 char*
225 IndexedOptionMenu::getStringSpec(void)
226 {
227     int index = getIndexSpec();
228     return _strings[index];
229 }
230
231 void
232 IndexedOptionMenu::setSpec (int spec)
233 {
234     XtVaSetValues(_w, XmNmenuHistory, _buttons[spec], NULL );
235 }
236
237 void
238 IndexedOptionMenu::setSpec (const char *string)
239 {
240     int spec = stringToIndex(string);
241     if (spec >= _nmenu_items) return;
242     setSpec(spec);
243 }
244
245 void
246 IndexedOptionMenu::setSpec (void *data)
247 {
248     int spec = dataToIndex(data);
249     if (spec >= _nmenu_items) return;
250     setSpec(spec);
251 }
252
253 int
254 IndexedOptionMenu::dataToIndex (void *data)
255 {
256     int index;
257
258     for (index=0; index<_nmenu_items; index++)
259         if (! strncmp((char*) _data[index], (char*) data, strlen((char*) data)))
260           return index;
261     
262     return index;
263 }
264
265 int
266 IndexedOptionMenu::stringToIndex (const char *string)
267 {
268     int index;
269
270     for (index=0; index<_nmenu_items; index++)
271         if (! strncmp(_strings[index], string, strlen(string)) )
272           return index;
273
274     return index;
275 }