-fpermissive to allow GCC to compile old C++
[oweals/cde.git] / cde / programs / dtmail / MotifApp / UIComponent.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: UIComponent.C /main/9 1998/07/23 17:57:36 mgreess $ */
24 /*
25  *+SNOTICE
26  *
27  *      RESTRICTED CONFIDENTIAL INFORMATION:
28  *      
29  *      The information in this document is subject to special
30  *      restrictions in a confidential disclosure agreement bertween
31  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
32  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
33  *      Sun's specific written approval.  This documment and all copies
34  *      and derivative works thereof must be returned or destroyed at
35  *      Sun's request.
36  *
37  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
38  *
39  *+ENOTICE
40  */
41 ///////////////////////////////////////////////////////////////////////////////
42 //////////////////////////////////////////////////////////////////////////////
43 //         This example code is from the book:
44 //
45 //           Object-Oriented Programming with C++ and OSF/Motif
46 //         by
47 //           Douglas Young
48 //           Prentice Hall, 1992
49 //           ISBN 0-13-630252-1 
50 //
51 //         Copyright 1991 by Prentice Hall
52 //         All Rights Reserved
53 //
54 //  Permission to use, copy, modify, and distribute this software for 
55 //  any purpose except publication and without fee is hereby granted, provided 
56 //  that the above copyright notice appear in all copies of the software.
57 ///////////////////////////////////////////////////////////////////////////////
58 //////////////////////////////////////////////////////////////////////////////
59
60
61 ///////////////////////////////////////////////////////////////
62 // UIComponent.C: Base class for all C++/Motif UI components
63 ///////////////////////////////////////////////////////////////
64 #include <Dt/Wsm.h>
65 #include "UIComponent.h"
66 #include <assert.h>
67 #include <stdio.h>
68
69 UIComponent::UIComponent ( const char *name ) : BasicComponent ( name )
70 {
71     // Empty
72     _numPendingTasks = 0;
73 }
74
75
76 void
77 UIComponent::widgetDestroyedCallback( Widget, 
78                                       XtPointer clientData, 
79                                       XtPointer )
80 {
81     UIComponent * obj = (UIComponent *) clientData;     
82     
83     obj->widgetDestroyed();
84 }
85
86 void
87 UIComponent::widgetDestroyed()
88 {
89     _w = NULL;
90 }
91
92 void
93 UIComponent::installDestroyHandler()
94 {
95     assert ( _w != NULL );
96     XtAddCallback ( _w, 
97                    XmNdestroyCallback,
98                    &UIComponent::widgetDestroyedCallback, 
99                    (XtPointer) this );
100 }
101
102 void
103 UIComponent::manage()
104 {
105     assert ( _w != NULL );
106     assert ( XtHasCallbacks ( _w, XmNdestroyCallback ) ==
107             XtCallbackHasSome );
108     XtManageChild ( _w );
109 }
110
111 void
112 UIComponent::displayInCurrentWorkspace()
113 {
114     Widget w = baseWidget();
115     while (w && !XtIsShell(w)) w = XtParent(w);
116     if (w && XtIsShell(w)) displayInCurrentWorkspace(w);
117     manage();
118 }
119
120 void
121 UIComponent::displayInCurrentWorkspace(Widget shell)
122 {
123     while (shell && !XtIsShell(shell)) shell = XtParent(shell);
124
125     // Make sure the shell is popped up and occupying the current workspace.
126     if (NULL != shell && XtIsShell(shell))
127     {
128         Atom     pCurrent;
129         Display *display = XtDisplay(shell);
130         Window  window = XtWindow(shell);
131
132         XtVaSetValues(shell, XmNiconic, False, NULL);
133         XRaiseWindow(display, window);
134
135         /* Get the current Workspace */
136         if (Success == DtWsmGetCurrentWorkspace(
137                                         display,
138                                         XRootWindowOfScreen(XtScreen(shell)),
139                                         &pCurrent))
140         {
141             Atom *ws = NULL;
142             unsigned long num = 0;
143             int k;
144
145             if (Success==DtWsmGetWorkspacesOccupied(display, window, &ws, &num))
146             {
147                 /* Already in this workspace? */
148                 for (k = 0; k < num; k++)
149                   if (ws[k] == pCurrent) break;
150
151                 /* Add to the workspace */
152                 if (k >= num)
153                 {
154                     size_t nbytes = sizeof(Atom) * (num+1);
155                     ws = (Atom*) XtRealloc((char*) ws, nbytes);
156                     ws[num] = pCurrent;
157                     DtWsmSetWorkspacesOccupied(display, window, ws, num + 1);
158                 }
159
160                 XFree((char *)ws);
161             }
162             else
163               /* Change the hints to reflect the current workspace */
164               DtWsmSetWorkspacesOccupied(display, window, &pCurrent, 1);
165         }
166     }
167 }
168
169 UIComponent::~UIComponent()
170 {
171     // Make sure the widget hasn't already been destroyed
172     
173     if ( _w ) 
174     {
175         // Remove destroy callback so Xt can't call the callback
176         // with a pointer to an object that has already been freed
177         
178         XtRemoveCallback ( _w, 
179                           XmNdestroyCallback,
180                           &UIComponent::widgetDestroyedCallback,
181                           (XtPointer) this );   
182     }
183 }
184
185 void
186 UIComponent::getResources( const XtResourceList resources, 
187                            const int numResources )
188 {
189     // Check for errors
190     
191     assert ( _w != NULL );
192     assert ( resources != NULL );
193     
194     // Retrieve the requested resources relative to the 
195     // parent of this object's base widget
196     // Added support for doing getResources on the Application
197     
198     if ( XtParent( _w ) ) 
199         XtGetSubresources ( XtParent( _w ), 
200                             (XtPointer) this, 
201                             _name,
202                             className(),
203                             resources, 
204                             numResources,
205                             NULL, 
206                             0 );
207     else 
208         XtGetSubresources ( _w , 
209                             (XtPointer) this, 
210                             _name,
211                             className(),
212                             resources, 
213                             numResources,
214                             NULL, 
215                             0 );
216 }
217
218
219 #ifdef DEAD_WOOD
220 void
221 UIComponent::setDefaultResources( const Widget w, 
222                                   const String *resourceSpec )
223 {
224     int         i;      
225     Display    *dpy = XtDisplay ( w );  // Retrieve the display pointer
226     XrmDatabase rdb = NULL;             // A resource data base
227     
228     // Create an empty resource database
229
230     rdb = XrmGetStringDatabase ( "" );
231
232     // Add the Component resources, prepending the name of the component
233
234     i = 0;
235     while ( resourceSpec[i] != NULL )
236         {
237             char *buf = new char[1000];
238             sprintf(buf, "*%s%s", _name, resourceSpec[i++]);
239             XrmPutLineResource( &rdb, buf );
240             delete [] buf;
241         }
242
243     // Merge them into the Xt database, with lowest precendence
244     
245     if ( rdb )
246         {
247             XrmDatabase db = XtDatabase(dpy);
248             XrmCombineDatabase(rdb, &db, FALSE);
249         }
250 }
251 #endif /* DEAD_WOOD */
252
253 #ifndef CAN_INLINE_VIRTUALS
254 const char *const
255 UIComponent::className(void)
256 {
257     return "UIComponent";
258 }
259 #endif /* ! CAN_INLINE_VIRTUALS */