UIComponent.C 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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: 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. // UIComponent.C: Base class for all C++/Motif UI components
  61. ///////////////////////////////////////////////////////////////
  62. #include <Dt/Wsm.h>
  63. #include "UIComponent.h"
  64. #include <assert.h>
  65. #include <stdio.h>
  66. UIComponent::UIComponent ( const char *name ) : BasicComponent ( name )
  67. {
  68. // Empty
  69. _numPendingTasks = 0;
  70. }
  71. void
  72. UIComponent::widgetDestroyedCallback( Widget,
  73. XtPointer clientData,
  74. XtPointer )
  75. {
  76. UIComponent * obj = (UIComponent *) clientData;
  77. obj->widgetDestroyed();
  78. }
  79. void
  80. UIComponent::widgetDestroyed()
  81. {
  82. _w = NULL;
  83. }
  84. void
  85. UIComponent::installDestroyHandler()
  86. {
  87. assert ( _w != NULL );
  88. XtAddCallback ( _w,
  89. XmNdestroyCallback,
  90. &UIComponent::widgetDestroyedCallback,
  91. (XtPointer) this );
  92. }
  93. void
  94. UIComponent::manage()
  95. {
  96. assert ( _w != NULL );
  97. assert ( XtHasCallbacks ( _w, XmNdestroyCallback ) ==
  98. XtCallbackHasSome );
  99. XtManageChild ( _w );
  100. }
  101. void
  102. UIComponent::displayInCurrentWorkspace()
  103. {
  104. Widget w = baseWidget();
  105. while (w && !XtIsShell(w)) w = XtParent(w);
  106. if (w && XtIsShell(w)) displayInCurrentWorkspace(w);
  107. manage();
  108. }
  109. void
  110. UIComponent::displayInCurrentWorkspace(Widget shell)
  111. {
  112. while (shell && !XtIsShell(shell)) shell = XtParent(shell);
  113. // Make sure the shell is popped up and occupying the current workspace.
  114. if (NULL != shell && XtIsShell(shell))
  115. {
  116. Atom pCurrent;
  117. Display *display = XtDisplay(shell);
  118. Window window = XtWindow(shell);
  119. XtVaSetValues(shell, XmNiconic, False, NULL);
  120. XRaiseWindow(display, window);
  121. /* Get the current Workspace */
  122. if (Success == DtWsmGetCurrentWorkspace(
  123. display,
  124. XRootWindowOfScreen(XtScreen(shell)),
  125. &pCurrent))
  126. {
  127. Atom *ws = NULL;
  128. unsigned long num = 0;
  129. int k;
  130. if (Success==DtWsmGetWorkspacesOccupied(display, window, &ws, &num))
  131. {
  132. /* Already in this workspace? */
  133. for (k = 0; k < num; k++)
  134. if (ws[k] == pCurrent) break;
  135. /* Add to the workspace */
  136. if (k >= num)
  137. {
  138. size_t nbytes = sizeof(Atom) * (num+1);
  139. ws = (Atom*) XtRealloc((char*) ws, nbytes);
  140. ws[num] = pCurrent;
  141. DtWsmSetWorkspacesOccupied(display, window, ws, num + 1);
  142. }
  143. XFree((char *)ws);
  144. }
  145. else
  146. /* Change the hints to reflect the current workspace */
  147. DtWsmSetWorkspacesOccupied(display, window, &pCurrent, 1);
  148. }
  149. }
  150. }
  151. UIComponent::~UIComponent()
  152. {
  153. // Make sure the widget hasn't already been destroyed
  154. if ( _w )
  155. {
  156. // Remove destroy callback so Xt can't call the callback
  157. // with a pointer to an object that has already been freed
  158. XtRemoveCallback ( _w,
  159. XmNdestroyCallback,
  160. &UIComponent::widgetDestroyedCallback,
  161. (XtPointer) this );
  162. }
  163. }
  164. void
  165. UIComponent::getResources( const XtResourceList resources,
  166. const int numResources )
  167. {
  168. // Check for errors
  169. assert ( _w != NULL );
  170. assert ( resources != NULL );
  171. // Retrieve the requested resources relative to the
  172. // parent of this object's base widget
  173. // Added support for doing getResources on the Application
  174. if ( XtParent( _w ) )
  175. XtGetSubresources ( XtParent( _w ),
  176. (XtPointer) this,
  177. _name,
  178. className(),
  179. resources,
  180. numResources,
  181. NULL,
  182. 0 );
  183. else
  184. XtGetSubresources ( _w ,
  185. (XtPointer) this,
  186. _name,
  187. className(),
  188. resources,
  189. numResources,
  190. NULL,
  191. 0 );
  192. }
  193. #ifdef DEAD_WOOD
  194. void
  195. UIComponent::setDefaultResources( const Widget w,
  196. const String *resourceSpec )
  197. {
  198. int i;
  199. Display *dpy = XtDisplay ( w ); // Retrieve the display pointer
  200. XrmDatabase rdb = NULL; // A resource data base
  201. // Create an empty resource database
  202. rdb = XrmGetStringDatabase ( "" );
  203. // Add the Component resources, prepending the name of the component
  204. i = 0;
  205. while ( resourceSpec[i] != NULL )
  206. {
  207. char *buf = new char[1000];
  208. sprintf(buf, "*%s%s", _name, resourceSpec[i++]);
  209. XrmPutLineResource( &rdb, buf );
  210. delete [] buf;
  211. }
  212. // Merge them into the Xt database, with lowest precendence
  213. if ( rdb )
  214. {
  215. XrmDatabase db = XtDatabase(dpy);
  216. XrmCombineDatabase(rdb, &db, FALSE);
  217. }
  218. }
  219. #endif /* DEAD_WOOD */
  220. #ifndef CAN_INLINE_VIRTUALS
  221. const char *const
  222. UIComponent::className(void)
  223. {
  224. return "UIComponent";
  225. }
  226. #endif /* ! CAN_INLINE_VIRTUALS */