Remove UXPDS support
[oweals/cde.git] / cde / programs / dtprintinfo / libUI / MotifUI / ComboBoxObj.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 /* $XConsortium: ComboBoxObj.C /main/4 1995/11/06 09:39:21 rswiston $ */
24 /*                                                                      *
25  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
26  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
27  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
28  * (c) Copyright 1993, 1994 Novell, Inc.                                *
29  */
30
31 #include "ComboBoxObj.h"
32
33 #include <Xm/Label.h>
34 #include <Xm/Form.h>
35 #include <Xm/List.h>
36 #include <Xm/ComboBox.h>
37
38 ComboBoxObj::ComboBoxObj(MotifUI *parent,
39 #if defined(USL)
40                          void (*callback)(ComboBoxObj *, char *, int),
41 #else
42                          ComboBoxCallback callback, 
43 #endif
44                          char *name, 
45                          char **items, 
46                          int n_items)
47         : MotifUI(parent, name, NULL)
48 {
49    XmString *items_list;
50    int itemCount;
51    if (items && n_items)
52     {
53       items_list = new XmString [n_items];
54       int i;
55       if (n_items > 8)
56          itemCount = 8;
57       else
58          itemCount = n_items;
59       for (i = 0; i < n_items; i++)
60          items_list[i] = StringCreate(items[i]);
61     }
62    else
63       items_list = NULL;
64    _callback = callback;
65    if (name)
66     {
67       XmString xm_string = StringCreate(name);
68       _w = XtVaCreateManagedWidget("form", xmFormWidgetClass, 
69                                    parent->InnerWidget(), NULL);
70       _label = XtVaCreateManagedWidget("form", xmLabelWidgetClass, 
71                                        _w, XmNlabelString, xm_string,
72                                        XmNtopAttachment, XmATTACH_FORM, 
73                                        XmNbottomAttachment, XmATTACH_FORM, 
74                                        XmNleftAttachment, XmATTACH_FORM, NULL); 
75       _combo_box = XtVaCreateManagedWidget("combo_box", xmComboBoxWidgetClass, 
76                                            _w, XmNleftWidget, _label,
77                                            XmNleftAttachment, XmATTACH_WIDGET,
78                                            XmNrightAttachment, XmATTACH_FORM,
79                                            XmNtopAttachment, XmATTACH_FORM, 
80                                            XmNbottomAttachment, XmATTACH_FORM, 
81                                            XmNcomboBoxType, XmDROP_DOWN_LIST,
82                                            (items_list ? XmNitems : NULL),
83                                               items_list,
84                                            XmNitemCount, n_items,
85                                            XmNvisibleItemCount, itemCount, 
86                                            NULL); 
87       StringFree(xm_string);
88     }
89    else
90     {
91       _w = XtVaCreateManagedWidget("combo_box", xmComboBoxWidgetClass, 
92                                    parent->InnerWidget(),
93                                    XmNcomboBoxType, XmDROP_DOWN_LIST,
94                                    (items_list ? XmNitems : NULL), items_list,
95                                    XmNitemCount, n_items,
96                                    XmNvisibleItemCount, itemCount, NULL); 
97       _label = NULL;
98       _combo_box = _w;
99     }
100    _list = XtNameToWidget(_combo_box, "*List");
101    XtAddCallback(_combo_box, XmNselectionCallback, &ComboBoxObj::SelectCB, 
102                  (XtPointer)this);
103    if (items_list)
104     {
105       int i;
106       for (i = 0; i < n_items; i++)
107          StringFree(items_list[i]);
108       delete items_list;
109     }
110 }
111
112 char * ComboBoxObj::Item(int item)
113 {
114    XmString *items;
115    int n_items;
116    XtVaGetValues(_list, XmNitems, &items, XmNitemCount, &n_items, NULL);
117    if (item < 0 || item > n_items - 1)
118       return NULL;
119    else
120       return StringExtract(items[item + 1]);
121 }
122
123 char * ComboBoxObj::Item(char *item)
124 {
125    XmString value = StringCreate(item);
126    int n = XmListItemExists(_list, value);
127    StringFree(value);
128    if (n)
129       return item;
130    else
131       return NULL;
132 }
133
134 void ComboBoxObj::Add(char *item)
135 {
136    XmString value = StringCreate(item);
137    XmComboBoxAddItem(_combo_box, value, 0, True);
138    StringFree(value);
139 }
140
141 void ComboBoxObj::Delete(char *item)
142 {
143    XmString value = StringCreate(item);
144    int n = XmListItemPos(_list, value);
145    if (n)
146       XmComboBoxDeletePos(_combo_box, n);
147    StringFree(value);
148 }
149
150 void ComboBoxObj::SelectCB(Widget, XtPointer client_data, XtPointer call_data)
151 {
152    ComboBoxObj *obj = (ComboBoxObj *) client_data;
153    XmComboBoxCallbackStruct * cb = (XmComboBoxCallbackStruct *)call_data;
154    if (obj->_callback)
155     {
156       char *item = obj->StringExtract(cb->item_or_text);
157       (*obj->_callback)(obj, item, cb->item_position);
158       XtFree(item);
159     }
160 }