popup.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* $Xorg: popup.c,v 1.4 2001/02/09 02:06:01 xorgcvs Exp $ */
  2. /*
  3. Copyright 1994, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  12. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  15. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  16. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  17. OTHER DEALINGS IN THE SOFTWARE.
  18. Except as contained in this notice, the name of The Open Group shall
  19. not be used in advertising or otherwise to promote the sale, use or
  20. other dealings in this Software without prior written authorization
  21. from The Open Group.
  22. */
  23. /* $XFree86: xc/programs/xsm/popup.c,v 1.4 2001/01/17 23:46:30 dawes Exp $ */
  24. #include "xsm.h"
  25. #include <X11/Shell.h>
  26. #include "popup.h"
  27. void
  28. PopupPopup(Widget parent, Widget popup, Bool transient, Bool first_time,
  29. int offset_x, int offset_y, const _XtString delAction)
  30. {
  31. if (!transient && !first_time)
  32. {
  33. /*
  34. * For non-transient windows, if this isn't the first time
  35. * it's being popped up, just pop it up in the old position.
  36. */
  37. XtPopup (popup, XtGrabNone);
  38. return;
  39. }
  40. else
  41. {
  42. Position parent_x, parent_y;
  43. Position root_x, root_y;
  44. Position popup_x, popup_y;
  45. Dimension parent_width, parent_height, parent_border;
  46. Dimension popup_width, popup_height, popup_border;
  47. char geom[16];
  48. Bool repos = 0;
  49. /*
  50. * The window we pop up must be visible on the screen. We first
  51. * try to position it at the desired location (relative to the
  52. * parent widget). Once we are able to compute the popup's
  53. * geometry (after it's realized), we can determine if we need
  54. * to reposition it.
  55. */
  56. XtVaGetValues (parent,
  57. XtNx, &parent_x,
  58. XtNy, &parent_y,
  59. XtNwidth, &parent_width,
  60. XtNheight, &parent_height,
  61. XtNborderWidth, &parent_border,
  62. NULL);
  63. XtTranslateCoords (parent, parent_x, parent_y, &root_x, &root_y);
  64. popup_x = root_x + offset_x;
  65. popup_y = root_y + offset_y;
  66. if (transient)
  67. {
  68. XtVaSetValues (popup,
  69. XtNx, popup_x,
  70. XtNy, popup_y,
  71. NULL);
  72. }
  73. else
  74. {
  75. snprintf (geom, sizeof(geom), "+%d+%d", popup_x, popup_y);
  76. XtVaSetValues (popup,
  77. XtNgeometry, geom,
  78. NULL);
  79. }
  80. if (first_time)
  81. {
  82. /*
  83. * Realize it for the first time
  84. */
  85. XtRealizeWidget (popup);
  86. /*
  87. * Set support for WM_DELETE_WINDOW
  88. */
  89. (void) SetWM_DELETE_WINDOW (popup, delAction);
  90. }
  91. /*
  92. * Now make sure it's visible.
  93. */
  94. XtVaGetValues (popup,
  95. XtNwidth, &popup_width,
  96. XtNheight, &popup_height,
  97. XtNborderWidth, &popup_border,
  98. NULL);
  99. popup_border <<= 1;
  100. if ((int) (popup_x + popup_width + popup_border) >
  101. WidthOfScreen (XtScreen (topLevel)))
  102. {
  103. popup_x = WidthOfScreen (XtScreen (topLevel)) -
  104. popup_width - popup_border - parent_width - parent_border;
  105. repos = 1;
  106. }
  107. if ((int) (popup_y + popup_height + popup_border) >
  108. HeightOfScreen (XtScreen (topLevel)))
  109. {
  110. popup_y = HeightOfScreen (XtScreen (topLevel)) -
  111. popup_height - popup_border - parent_height - parent_border;
  112. repos = 1;
  113. }
  114. if (repos)
  115. {
  116. if (transient)
  117. {
  118. XtVaSetValues (popup,
  119. XtNx, popup_x,
  120. XtNy, popup_y,
  121. NULL);
  122. }
  123. else
  124. {
  125. /*
  126. * The only way we can reposition a non-transient
  127. * is by unrealizing it, setting the position, then
  128. * doing a realize.
  129. */
  130. XtUnrealizeWidget (popup);
  131. snprintf (geom, sizeof(geom), "+%d+%d", popup_x, popup_y);
  132. XtVaSetValues (popup,
  133. XtNgeometry, geom,
  134. NULL);
  135. XtRealizeWidget (popup);
  136. (void) SetWM_DELETE_WINDOW (popup, delAction);
  137. }
  138. }
  139. XtPopup (popup, XtGrabNone);
  140. }
  141. }