]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/xzwrite/Popup.c
Initial revision
[1ts-debian.git] / zephyr / clients / xzwrite / Popup.c
1 /*
2  * This code has gone back and forth between myself and Jon Kamens
3  * so many times that neither really knows who wrote it..
4  */
5
6 #include <X11/Intrinsic.h>
7 #include <X11/StringDefs.h>
8
9 static void _initPopup();
10 void Popup(), PopupSafe(), PopupAtPointer();
11
12 static int      display_height, display_width;
13
14 static void _initPopup(w)
15    Widget       w;
16 {
17      Display    *dpy;
18      int        screen;
19
20      dpy = XtDisplay(w);
21      screen = DefaultScreen(dpy);
22      display_height = DisplayHeight(dpy, screen);
23      display_width = DisplayWidth(dpy, screen);
24 }
25
26 /* ARGSUSED */
27 void Popup(shell, GrabType, pop_type)
28    Widget shell;
29    XtGrabKind GrabType;
30    int pop_type;
31 {
32      PopupAtPointer(shell, GrabType);
33 }
34
35 void PopupSafe(w, x, y, GrabType)
36    Widget w;
37    Dimension x, y;
38    XtGrabKind GrabType;
39 {
40      static int first_time = 1;
41      Dimension width, height, border;
42
43      if (first_time) {
44           _initPopup(w);
45           first_time = 0;
46      }
47
48      XtVaGetValues(w,
49                    XtNwidth, &width,
50                    XtNheight, &height,
51                    XtNborderWidth, &border,
52                    NULL);
53      
54      if (x + width + 2 * border > display_width)
55           x = display_width - width - 2 * border;
56      if (y + height + 2 * border > display_height)
57           y = display_height - height - 2 * border;
58      
59      XtVaSetValues(w,
60                    XtNx, x,
61                    XtNy, y,
62                    NULL);
63      
64      XtPopup(w, GrabType);
65 }
66
67 void PopupAtPointer(w, GrabType)
68    Widget       w;
69    XtGrabKind   GrabType;
70 {
71      Window garbage1, garbage2, window;
72      int root_x, root_y, x2, y2;
73      unsigned int mask;
74      Dimension width, height, border;
75      Display *dpy;
76
77      dpy = XtDisplay(w);
78      window = XtWindow(XtParent(w));
79
80      if (XQueryPointer(dpy, window, &garbage1, &garbage2,
81                        &root_x, &root_y, &x2, &y2, &mask)) {
82
83           XtVaGetValues(w,
84                         XtNwidth, &width,
85                         XtNheight, &height,
86                         XtNborderWidth, &border,
87                         NULL);
88           
89           if (root_x >= width / 2 + border)
90                root_x -= width / 2 + border;
91           else
92                root_x = 0;
93           if (root_y >= height / 2 + border)
94                root_y -= height / 2 + border;
95           else
96                root_y = 0;
97
98           PopupSafe(w, (Dimension) root_x, (Dimension) root_y, GrabType);
99      }
100 }