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