Even more spelling fixed
[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 libraries 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     _strings = NULL;
100     _data = NULL;
101
102     _nmenu_items = nmenu_items;
103     if (nmenu_items && strings != NULL)
104     {
105         _strings = (char **) XtMalloc(nmenu_items * sizeof(char*));
106         for (int i=0; i<nmenu_items; i++)
107           _strings[i] = strings[i];
108     }
109     if (nmenu_items && data != NULL)
110     {
111         _data = (void **) XtMalloc(nmenu_items * sizeof(void*));
112         for (int i=0; i<nmenu_items; i++)
113           _data[i] = data[i];
114     }
115
116     /*
117      * Create the pulldown menu for the option menus,
118      * and populate it with pushbuttons.
119      * Attach the original strings used to generate
120      * the button labels to the buttons as userData.
121      */
122     menu = XmCreatePulldownMenu(parent, "Menu", NULL, 0);
123     _buttons = (Widget *) XtMalloc( _nmenu_items * sizeof(Widget) );
124     for (int i=0; i<_nmenu_items; i++)
125     {
126         static char     button_label[32];
127
128         sprintf(button_label, "Button%d", i);
129         _buttons[i] = XmCreatePushButton(menu, button_label, NULL, 0);
130         xms = XmStringCreateLocalized(_strings[i]);
131         XtVaSetValues(
132                 _buttons[i],
133                 XmNuserData, i,
134                 XmNlabelString, xms,
135                 NULL);
136         XmStringFree(xms);
137         XtManageChild(_buttons[i]);
138     }
139
140     nargs=0;
141     XtSetArg(args[nargs], XmNsubMenuId, menu); nargs++;
142     _w = XmCreateOptionMenu(parent, "IndexedOptionMenu", args, nargs);
143     installDestroyHandler();
144
145     //XtRealizeWidget(_w);
146 }
147
148 IndexedOptionMenu::IndexedOptionMenu (
149                                 Widget  option_menu,
150                                 int     nmenu_items,
151                                 char    **strings,
152                                 void    **data,
153                                 Widget  *buttons
154                                 ) : UIComponent( "IndexedOptionMenu" )
155 {
156     _w = option_menu;
157     installDestroyHandler();
158     _nmenu_items = nmenu_items;
159     _strings = NULL;
160     _data = NULL;
161     _buttons = NULL;
162
163     if (nmenu_items && strings != NULL)
164     {
165         _strings = (char **) XtMalloc(nmenu_items * sizeof(char*));
166         for (int i=0; i<nmenu_items; i++)
167           _strings[i] = strings[i];
168     }
169     if (nmenu_items && data != NULL)
170     {
171         _data = (void **) XtMalloc(nmenu_items * sizeof(void*));
172         for (int i=0; i<nmenu_items; i++)
173           _data[i] = data[i];
174     }
175     if (nmenu_items && buttons != NULL)
176     {
177         _buttons = (Widget *) XtMalloc( _nmenu_items * sizeof(Widget) );
178         for (int i=0; i<nmenu_items; i++)
179           _buttons[i] = buttons[i];
180     }
181 }
182
183
184 IndexedOptionMenu::~IndexedOptionMenu (void)
185 {
186     if (_w != NULL)
187       XtDestroyWidget(_w);
188     if (_buttons)
189       XtFree((char *) _buttons);
190     if (_strings)
191       XtFree((char *) _strings);
192     if (_data)
193       XtFree((char *) _data);
194 }
195
196 void
197 IndexedOptionMenu::addMenuButtonCallback(
198                                         char* callback_name,
199                                         XtCallbackProc callback,
200                                         XtPointer client_data)
201 {
202     for (int i=0; i<_nmenu_items; i++)
203       XtAddCallback(_buttons[i], callback_name, callback, client_data);
204 }
205
206 void*
207 IndexedOptionMenu::getDataSpec (void)
208 {
209     int index = getIndexSpec();
210     return _data[index];
211 }
212
213 int
214 IndexedOptionMenu::getIndexSpec (void)
215 {
216     int         data = 0;
217
218     if (_w)
219     {
220         XtArgVal arg;
221         Widget  selected;
222         XtVaGetValues(_w, XmNmenuHistory, &arg, NULL);
223         selected = (Widget)arg;
224         XtVaGetValues(selected, XmNuserData, &arg, NULL);
225         data = (int)arg;
226     }
227     return data;
228 }
229
230 char*
231 IndexedOptionMenu::getStringSpec(void)
232 {
233     int index = getIndexSpec();
234     return _strings[index];
235 }
236
237 void
238 IndexedOptionMenu::setSpec (int spec)
239 {
240     XtVaSetValues(_w, XmNmenuHistory, _buttons[spec], NULL );
241 }
242
243 void
244 IndexedOptionMenu::setSpec (const char *string)
245 {
246     int spec = stringToIndex(string);
247     if (spec >= _nmenu_items) return;
248     setSpec(spec);
249 }
250
251 void
252 IndexedOptionMenu::setSpec (void *data)
253 {
254     int spec = dataToIndex(data);
255     if (spec >= _nmenu_items) return;
256     setSpec(spec);
257 }
258
259 int
260 IndexedOptionMenu::dataToIndex (void *data)
261 {
262     int index;
263
264     for (index=0; index<_nmenu_items; index++)
265         if (! strncmp((char*) _data[index], (char*) data, strlen((char*) data)))
266           return index;
267     
268     return index;
269 }
270
271 int
272 IndexedOptionMenu::stringToIndex (const char *string)
273 {
274     int index;
275
276     for (index=0; index<_nmenu_items; index++)
277         if (! strncmp(_strings[index], string, strlen(string)) )
278           return index;
279
280     return index;
281 }