Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtprintinfo / libUI / MotifUI / Menu.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 /* $XConsortium: Menu.C /main/3 1995/11/06 09:42:58 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 "Menu.h"
32 #include "MainWindow.h"
33
34 #include <Xm/RowColumn.h>
35 #include <Xm/Label.h>
36 #include <Xm/Separator.h>
37 #include <Xm/CascadeB.h>
38
39 Menu::Menu(MotifUI *parent, char *name, char *mnemonic, MenuType menu_type)
40         : MotifUI(parent, name, NULL)
41 {
42    CreateMenu(parent->InnerWidget(), name, NULL, mnemonic, menu_type);
43 }
44
45 Menu::Menu(MotifUI *parent, boolean /*has_title*/, char *title,
46            char *name, char *mnemonic, MenuType menu_type)
47         : MotifUI(parent, name, NULL)
48 {
49    CreateMenu(parent->InnerWidget(), name, NULL, mnemonic, menu_type);
50    CreateTitle(parent->InnerWidget(), NULL, title);
51 }
52
53 Menu::Menu(char *category, MotifUI *parent, char *name, char *mnemonic, 
54            MenuType menu_type)
55         : MotifUI(parent, name, category)
56 {
57    CreateMenu(parent->InnerWidget(), name, category, mnemonic, menu_type);
58 }
59
60 Menu::Menu(char *category, MotifUI *parent, boolean /*has_title*/,
61            char *title, char *name, char *mnemonic, MenuType menu_type)
62         : MotifUI(parent, name, category)
63 {
64    CreateMenu(parent->InnerWidget(), name, category, mnemonic, menu_type);
65    CreateTitle(parent->InnerWidget(), category, title);
66 }
67
68 void Menu::SetRadio(boolean flag)
69 {
70    XtVaSetValues(_w, XmNradioBehavior, flag, NULL);
71 }
72
73 boolean Menu::SetActivity(boolean flag)
74 {
75    XtSetSensitive(_cascadeButton, flag);
76    return true;
77 }
78
79 void Menu::CreateMenu(Widget parent, char *name, char * /*category*/,
80                       char *mnemonic, MenuType menu_type)
81 {
82    XmString xm_string = StringCreate(name);
83
84    _title = NULL;
85    _sep = NULL;
86    switch (_menu_type = menu_type)
87    {
88    case PULLDOWN_MENU:
89       _w = XmCreatePulldownMenu(parent, "pulldown_menu", NULL, 0);
90       break;
91    case OPTION_MENU:
92       _w = XmCreateOptionMenu(parent, "option_menu", NULL, 0);
93       break;
94    case POPUP_MENU:
95       _w = XmCreatePopupMenu(parent, "popup_menu", NULL, 0);
96       XtVaSetValues(_w, XmNwhichButton, MotifUI::bMenuButton, NULL);
97       break;
98    }
99    if (_menu_type != POPUP_MENU)
100     {
101       _cascadeButton = XtVaCreateManagedWidget("cascadeButton",
102                                                xmCascadeButtonWidgetClass,
103                                                parent,
104                                                XmNlabelString, xm_string,
105                                                XmNsubMenuId, _w, NULL);
106       if (depth == 1)
107        {
108          Pixel bg;
109          XtVaGetValues(_cascadeButton, XmNbackground, &bg, NULL);
110          if (bg == white)
111           {
112             XtVaSetValues(_w, XmNuserData, (XtPointer)this, NULL);
113             XtAddCallback(_w, XmNmapCallback, &(Menu::MapCB), NULL);
114             XtAddCallback(_w, XmNunmapCallback, &(Menu::MapCB), NULL);
115           }
116        }
117     }
118    StringFree(xm_string);
119    if (mnemonic)
120       XtVaSetValues(_cascadeButton,
121                     XmNmnemonic, XStringToKeysym(mnemonic), NULL);
122    InstallHelpCB();
123 }
124
125 void Menu::CreateTitle(Widget /*parent*/, char *title, char * /*category*/)
126 {
127    XmString xm_string = StringCreate(title);
128
129    if (_title)
130       XtVaSetValues(_title, XmNlabelString, xm_string, NULL);
131    else
132     {
133       _title = XtVaCreateManagedWidget("title", xmLabelWidgetClass, _w, 
134                                        XmNlabelString, xm_string, NULL);
135       _sep = XtVaCreateManagedWidget("separator", xmSeparatorWidgetClass, _w, 
136                                      XmNseparatorType, XmDOUBLE_LINE, NULL);
137     }
138    StringFree(xm_string);
139 }
140
141 boolean Menu::SetName(char *name)
142 {
143    CreateTitle(_w, name, (char *) Category());
144    return true;
145 }
146
147 void Menu::MapCB(Widget w, XtPointer /*client_data*/, XtPointer /*call_data*/)
148 {
149    Menu *obj;
150    XtVaGetValues(w, XmNuserData, &obj, NULL);
151    Pixel fg;
152    XtVaGetValues(obj->_cascadeButton, XmNforeground, &fg, NULL);
153    if (fg == white)
154       fg = black;
155    else
156       fg = white;
157    XtVaSetValues(obj->_cascadeButton, XmNforeground, fg, NULL);
158 }