]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/freebrowser.c
shared-4.diff. Gui independant config.h
[lyx.git] / src / frontends / xforms / freebrowser.c
1 /**
2  * \file freebrowser.c
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  *
11  * Full author contact details are available in file CREDITS
12  *
13  * This is a rewrite of Alejandro's C++ Combox class, originally written
14  * for LyX in 1996. The rewrite turns it into a native xforms widget.
15  */
16 #include <config.h>
17
18 #include "lyx_forms.h"
19 #include "freebrowser.h"
20 #include <ctype.h> /* isprint */
21
22 extern void fl_hide_tooltip(void);
23
24 static void browser_cb(FL_OBJECT * ob, long data);
25 static int peek_event(FL_FORM * form, void * xev);
26
27
28 FL_FREEBROWSER *
29 fl_create_freebrowser(void * parent)
30 {
31     FL_FORM * current_form = fl_current_form;
32     FL_FREEBROWSER * fb = fl_calloc(1, sizeof(FL_FREEBROWSER));
33     FL_OBJECT * ob;
34
35     /* Default size */
36     FL_Coord const w = 100;
37     FL_Coord const h = 100;
38
39     fb->parent = parent;
40     fb->callback = 0;
41     fb->want_printable = 0;
42     fb->last_printable = 0;
43
44     if (current_form) fl_end_form();
45     fb->form = fl_bgn_form(FL_NO_BOX, w, h);
46
47     fb->form->u_vdata = fb;
48     fl_register_raw_callback(fb->form, ButtonPressMask|KeyPressMask,
49                              peek_event);
50
51     ob = fb->browser = fl_add_browser(FL_HOLD_BROWSER, 0, 0, w, h, "");
52     ob->u_vdata = fb;
53     fl_set_object_boxtype(ob, FL_UP_BOX);
54     fl_set_object_color(ob, FL_MCOL, FL_YELLOW);
55     fl_set_object_gravity(ob, NorthWestGravity, SouthEastGravity);
56     fl_set_object_callback(ob, browser_cb, 0);
57     fl_end_form();
58
59     if (current_form)
60         fl_addto_form(current_form);
61
62     return fb;
63 }
64
65
66 void
67 fl_free_freebrowser(FL_FREEBROWSER * fb)
68 {
69     if (!fb)
70         return;
71
72     if (fb->form)
73         fl_free_form(fb->form);
74
75     fl_free(fb);
76     fb = 0;
77 }
78
79
80 void
81 fl_show_freebrowser(FL_FREEBROWSER * fb,
82                     FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
83 {
84     int tmp;
85     int const pos = fl_get_browser(fb->browser);
86
87     fl_set_form_geometry(fb->form, x, y, w, h);
88
89     XGetInputFocus(fl_get_display(), &fb->save_window, &tmp);
90     XFlush(fl_get_display());
91
92     fl_show_form(fb->form, FL_PLACE_POSITION, FL_NOBORDER, "");
93
94     fl_select_browser_line(fb->browser, pos);
95     fl_set_browser_topline(fb->browser, pos);
96
97     XGrabPointer(fl_get_display(), fb->form->window, 0,
98                  ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
99                  GrabModeAsync, GrabModeAsync,
100                  0, 0, 0);
101     XFlush(fl_get_display());
102
103     fl_hide_tooltip();
104 }
105
106
107 void
108 fl_hide_freebrowser(FL_FREEBROWSER * fb)
109 {
110     XUngrabPointer(fl_get_display(), 0);
111     XFlush(fl_get_display());
112
113     if (fb->form->visible)
114         fl_hide_form(fb->form);
115
116     XSetInputFocus(fl_get_display(), fb->save_window,
117                    RevertToParent, CurrentTime);
118     XFlush(fl_get_display());
119
120     if (fb->callback)
121         fb->callback(fb, 0);
122 }
123
124
125 static void
126 browser_cb(FL_OBJECT * ob, long data)
127 {
128     FL_FREEBROWSER * fb = ob->u_vdata;
129     fl_hide_freebrowser(fb);
130     if (fb->callback)
131         fb->callback(fb, 1);
132 }
133
134
135 static int
136 eventWithinObj(XEvent * xev, FL_OBJECT * ob)
137 {
138     int result = 0;
139     if (xev && ob) {
140         int const xev_x = (int)(xev->xbutton.x);
141         int const xev_y = (int)(xev->xbutton.y);
142         int const ob_x  = (int)(ob->x);
143         int const ob_y  = (int)(ob->y);
144         int const ob_w  = (int)(ob->w);
145         int const ob_h  = (int)(ob->h);
146
147         int const dx = xev_x - ob_x;
148         int const dy = xev_y - ob_y;
149
150         /* result is true if the event occurred within the ob */
151         result = !(dx < 0 || dx > ob_w || dy < 0 || dy > ob_h);
152     }
153     return result;
154 }
155
156
157 static int
158 peek_event(FL_FORM * form, void * ev)
159 {
160     XEvent * xev = ev;
161     FL_FREEBROWSER * fb = form->u_vdata;
162     FL_OBJECT * browser = fb->browser;
163
164     fb->last_printable = 0;
165     fl_hide_tooltip();
166
167     if (xev->type == ButtonPress && !eventWithinObj(xev, browser)) {
168         fl_hide_freebrowser(fb);
169         return 1;
170     }
171
172     if (xev->type == KeyPress) {
173         KeySym keysym_return;
174         char s_r[10];
175         s_r[9] = '\0';
176         XLookupString(&xev->xkey, s_r, 10, &keysym_return, 0);
177
178         XFlush(fl_get_display());
179
180         switch (keysym_return) {
181         case XK_Down:
182 #ifdef XK_KP_Down
183         case XK_KP_Down:
184 #endif
185         {
186             int sel = fl_get_browser(browser);
187             int const top = fl_get_browser_topline(browser);
188             int const screenlines = fl_get_browser_screenlines(browser);
189
190             if (sel < 1 || sel >= fl_get_browser_maxline(browser))
191                 return 0;
192
193             ++sel;
194             fl_select_browser_line(browser, sel);
195
196             if (sel >= top + screenlines)
197                 fl_set_browser_topline(browser, sel - screenlines + 1);
198
199             if (sel < top)
200                 fl_set_browser_topline(browser, sel);
201
202             return 1;
203         }
204
205         case XK_Up:
206 #ifdef XK_KP_Up
207         case XK_KP_Up:
208 #endif
209         {
210             int sel = fl_get_browser(browser);
211             int const top = fl_get_browser_topline(browser);
212             int const screenlines = fl_get_browser_screenlines(browser);
213
214             if (sel <= 1 || sel > fl_get_browser_maxline(browser))
215                 return 0;
216
217             --sel;
218             fl_select_browser_line(browser, sel);
219
220             if (sel >= top + screenlines)
221                 fl_set_browser_topline(browser, sel - screenlines + 1);
222
223             if (sel < top)
224                 fl_set_browser_topline(browser, sel);
225
226             return 1;
227         }
228
229         case XK_Return:
230 #ifdef XK_KP_Enter
231         case XK_KP_Enter:
232 #endif
233             fl_hide_freebrowser(fb);
234             if (fb->callback)
235                 fb->callback(fb, 1);
236             return 1;
237
238         case XK_Escape:
239             fl_hide_freebrowser(fb);
240             return 1;
241         default:
242             if (fb->want_printable && s_r[0] && isprint(s_r[0])) {
243                 fb->last_printable = s_r[0];
244                 fl_hide_freebrowser(fb);
245                 if (fb->callback)
246                     fb->callback(fb, 1);
247                 return 1;
248             }
249         }
250     }
251     return 0;
252 }