3 * Combox: A combination of two objects (a button and a browser) is
4 * encapsulated to get a combobox-like object. All XForms
5 * functions are hidden.
7 * GNU Copyleft (C) 1996 Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
10 * Dependencies: Only XForms, but created to be used with LyX.
16 * 2/06/1996, Alejandro Aguilar Sierra
19 * 4/06/1996, Alejandro Aguilar Sierra
20 * Added droplist mode (a button with a black down arrow at right)
21 * and support for middle and right buttons, as XForms choice object.
23 * 6/06/1996, Lars Gullik Bjønnes
24 * Added a combox with an input object. and a pre and a post handle.
26 * 22/07/96, Alejandro Aguilar Sierra
27 * Assigned to the browser its own popup window. No more need of
28 * external pre and post handlers to simulate the wanted behaviour.
35 #pragma implementation
43 // These are C wrappers around static members of Combox, used as
44 // callbacks for xforms.
45 extern "C" void C_Combox_input_cb(FL_OBJECT *ob, long);
46 extern "C" void C_Combox_combo_cb(FL_OBJECT *ob, long data) ;
47 extern "C" int C_Combox_peek_event(FL_FORM * form, void *xev);
49 Combox::Combox(combox_type t): type(t)
70 if (browser) fl_clear_browser(browser);
72 if (type == FL_COMBOX_INPUT)
73 fl_set_input(label, "");
75 fl_set_object_label(label, "");
82 lyxerr.debug() << "Button: " << button << endl;
84 fl_delete_object(button);
85 fl_free_object(button);
88 lyxerr.debug() << "Label: " << label << endl;
89 if (label && label!=button) {
90 fl_delete_object(label);
91 fl_free_object(label);
94 lyxerr.debug() << "Form: " << form << endl;
95 lyxerr.debug() << "Browser: " << browser << endl;
96 if (form && browser) {
97 fl_delete_object(browser);
98 fl_free_object(browser);
110 void Combox::addline(char const* text)
112 if (!browser) return;
113 fl_add_browser_line(browser, text);
115 // By default the first item is selected
118 if (type == FL_COMBOX_INPUT)
119 fl_set_input(label, text);
121 fl_set_object_label(label, text);
127 bool Combox::select_text(char const* t)
129 if (!browser || !t) return false;
130 for (int i=1; i<=fl_get_browser_maxline(browser); i++) {
131 if (!strcmp(t, fl_get_browser_line(browser, i))) {
136 return false; // t does not exist in browser
140 void Combox::select(int i)
142 if (!browser || !button) return;
143 if (i>0 && i<=fl_get_browser_maxline(browser)) sel = i;
144 fl_deactivate_object(button);
146 if (type == FL_COMBOX_INPUT)
147 fl_set_input(label, fl_get_browser_line(browser, sel));
149 fl_set_object_label(label, fl_get_browser_line(browser, sel));
150 fl_activate_object(button);
154 void Combox::add(int x, int y, int w, int hmin, int hmax)
159 case FL_COMBOX_DROPLIST:
161 button = obj = fl_add_button(FL_NORMAL_BUTTON,
162 x+w-22,y,22,hmin,"@2->");
163 fl_set_object_color(obj,FL_MCOL, FL_MCOL);
164 fl_set_object_dblbuffer(obj, 1);
165 fl_set_object_callback(obj,C_Combox_combo_cb,0);
166 label = obj = fl_add_button(FL_NORMAL_TEXT,x,y,w-22,hmin,"");
167 fl_set_object_boxtype(obj,FL_DOWN_BOX);
168 fl_set_object_color(obj,FL_MCOL,FL_BLACK);
169 fl_set_object_lalign(obj,FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
170 fl_set_object_dblbuffer(obj, 1);
171 fl_set_object_callback(obj,C_Combox_combo_cb,0);
174 case FL_COMBOX_NORMAL:
176 button = obj = fl_add_button(FL_NORMAL_BUTTON,x,y,w,hmin,"");
177 fl_set_object_color(obj,FL_MCOL, FL_MCOL);
178 fl_set_object_boxtype(obj,FL_DOWN_BOX);
179 fl_set_object_callback(obj,C_Combox_combo_cb,0);
180 fl_set_object_color(obj,FL_MCOL,FL_BLACK);
184 case FL_COMBOX_INPUT:
186 button = obj = fl_add_button(FL_NORMAL_BUTTON,
187 x+w-22,y,22,hmin,"@2->");
188 fl_set_object_color(obj,FL_MCOL, FL_MCOL);
189 fl_set_object_callback(obj,C_Combox_combo_cb,0);
190 label = obj = fl_add_input(FL_NORMAL_INPUT,x,y,w-22,hmin,"");
191 fl_set_object_boxtype(obj,FL_DOWN_BOX);
192 fl_set_object_return(obj, FL_RETURN_END_CHANGED);
193 fl_set_object_callback(obj,C_Combox_input_cb,0);
194 //fl_set_object_color(obj,FL_MCOL,FL_BLACK);
195 //fl_set_object_lalign(obj,FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
200 label->u_vdata = (void*)this;
201 button->u_vdata = (void*)this;
203 // Hmm, it seems fl_create_browser is broken in xforms 0.86.
204 // We have to work around that by creating the dropped browser form
205 // at this point already. However, this means that we have
206 // to do a little hacking: (Asger)
207 FL_FORM * current_form = fl_current_form;
210 bw = w+20; bh = hmax-hmin-12;
212 form = fl_bgn_form(FL_NO_BOX, bw, bh);
213 browser = obj = fl_add_browser(FL_HOLD_BROWSER, 0, 0, bw,bh,"");
214 fl_set_object_boxtype(obj,FL_UP_BOX);
215 fl_set_object_color(obj,FL_MCOL, FL_YELLOW);
216 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
217 fl_set_object_callback(obj,C_Combox_combo_cb,2);
219 browser->u_vdata = (void*)this;
220 form->u_vdata = browser;
221 fl_register_raw_callback(form,
222 ButtonPressMask|KeyPressMask,
223 C_Combox_peek_event);
225 // And revert to adding to the old form (Asger)
226 fl_addto_form(current_form);
230 static Window save_window;
233 void Combox::Redraw()
235 if (browser) fl_redraw_object(browser);
236 if (button) fl_redraw_object(button);
237 if (label) fl_redraw_object(label);
245 XGetInputFocus(fl_display, &save_window, &tmp); //BUG-Fix Dietmar
247 if (button && type != FL_COMBOX_NORMAL) {
248 fl_set_object_label(button, "@2<-");
249 fl_redraw_object(button);
251 int x = label->form->x + label->x, y = label->form->y + label->y;
252 fl_set_form_position(form, x, y + label->h);
253 fl_show_form(form, FL_PLACE_POSITION, FL_NOBORDER, "");
255 fl_set_browser_topline(browser, sel);
256 fl_select_browser_line(browser, sel);
258 XGrabPointer(fl_display, form->window, false,
259 ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
260 GrabModeAsync, GrabModeAsync,
265 void Combox::Hide(int who)
267 if (!who && browser && label) {
268 sel = fl_get_browser(browser);
270 if (type == FL_COMBOX_INPUT)
271 fl_set_input(label, fl_get_browser_line(browser, sel));
273 fl_set_object_label(label,
274 fl_get_browser_line(browser, sel));
275 if (callback) callback(sel,cb_arg);
277 XUngrabPointer(fl_display, 0);
281 XSetInputFocus(fl_display, save_window,
282 RevertToParent, CurrentTime); // BUG-FIX-Dietmar
286 if (type != FL_COMBOX_NORMAL){
287 fl_set_object_label(button, "@2->");
288 fl_redraw_object(button);
295 void Combox::activate()
297 if (browser) fl_activate_object(browser);
298 if (button) fl_activate_object(button);
299 if (label) fl_activate_object(label);
303 void Combox::deactivate()
305 if (browser) fl_deactivate_object(browser);
306 if (button) fl_deactivate_object(button);
307 if (label) fl_deactivate_object(label);
310 void Combox::input_cb(FL_OBJECT *ob, long)
312 Combox *combo = (Combox*)ob->u_vdata;
314 char const *text = fl_get_input(ob);
317 combo->is_empty = false;
320 extern "C" void C_Combox_input_cb(FL_OBJECT *ob, long data)
322 Combox::input_cb(ob, data);
326 void Combox::combo_cb(FL_OBJECT *ob, long data)
328 Combox *combo = (Combox*)ob->u_vdata;
332 int i = combo->get();
333 switch (fl_get_button_numb(ob)) {
338 combo->callback(combo->sel,
346 combo->callback(combo->sel,
350 default: combo->Show(); break;
360 extern "C" void C_Combox_combo_cb(FL_OBJECT *ob, long data)
362 Combox::combo_cb(ob,data);
366 int Combox::peek_event(FL_FORM * form, void *xev)
368 FL_OBJECT *ob = (FL_OBJECT *)form->u_vdata;
369 Combox *combo = (Combox*)ob->u_vdata;
372 if(((XEvent *)xev)->type==ButtonPress && !ob->belowmouse)
375 // I don't know why belowmouse does not work, but it doesn't. (Asger)
376 if (((XEvent *) xev)->type == ButtonPress && (
377 ((XEvent *)xev)->xbutton.x - ob->x < 0 ||
378 ((XEvent *)xev)->xbutton.x - ob->x > ob->w ||
379 ((XEvent *)xev)->xbutton.y - ob->y < 0 ||
380 ((XEvent *)xev)->xbutton.y - ob->y > ob->h))
387 if (((XEvent*)xev)->type != KeyPress) return 0;
390 static int num_bytes;
391 KeySym keysym_return;
392 num_bytes = XLookupString(&((XEvent*)xev)->xkey, s_r, 10,
395 switch (keysym_return) {
397 if (fl_get_browser(combo->browser) <
398 fl_get_browser_maxline(combo->browser))
399 fl_select_browser_line(combo->browser,
400 fl_get_browser(combo->browser)+1);
401 if (fl_get_browser(combo->browser)>=
402 fl_get_browser_topline(combo->browser) +
403 fl_get_browser_screenlines(combo->browser))
404 fl_set_browser_topline(combo->browser,
405 fl_get_browser(combo->browser)
406 - fl_get_browser_screenlines(combo->browser)+1);
407 if (fl_get_browser(combo->browser)<
408 fl_get_browser_topline(combo->browser))
409 fl_set_browser_topline(combo->browser,
410 fl_get_browser(combo->browser));
413 if (fl_get_browser(combo->browser) > 1)
414 fl_select_browser_line(combo->browser,
415 fl_get_browser(combo->browser)-1);
416 if (fl_get_browser(combo->browser)>=
417 fl_get_browser_topline(combo->browser) +
418 fl_get_browser_screenlines(combo->browser))
419 fl_set_browser_topline(combo->browser,
420 fl_get_browser(combo->browser)
421 - fl_get_browser_screenlines(combo->browser)+1);
422 if (fl_get_browser(combo->browser) <
423 fl_get_browser_topline(combo->browser))
424 fl_set_browser_topline(combo->browser,
425 fl_get_browser(combo->browser));
437 extern "C" int C_Combox_peek_event(FL_FORM * form, void *xev)
439 return Combox::peek_event(form, xev);
451 //Combox combo(FL_COMBOX_DROPLIST);
452 Combox combo(FL_COMBOX_INPUT);
456 FD_test *create_form_test(void)
459 FD_test *fdui = (FD_test *) fl_calloc(1, sizeof(*fdui));
461 fdui->test = fl_bgn_form(FL_NO_BOX, 320, 190);
462 obj = fl_add_box(FL_UP_BOX,0,0,320,190,"");
463 obj = fl_add_box(FL_DOWN_BOX,10,50,300,110,"");
464 obj = fl_add_button(FL_NORMAL_BUTTON,250,10,50,30,_("Done"));
465 combo.add(10,15,120,25,135);
473 fprintf(stderr, "selected %d:%s\n", i, combo.getline());
476 int main(int argc, char *argv[])
480 // Same defaults as in lyx
482 cntl.buttonFontSize = FL_NORMAL_SIZE;
483 cntl.browserFontSize = FL_NORMAL_SIZE;
484 cntl.labelFontSize = FL_NORMAL_SIZE;
485 cntl.choiceFontSize = FL_NORMAL_SIZE;
486 cntl.inputFontSize = FL_NORMAL_SIZE;
487 cntl.borderWidth = -2;
488 fl_set_defaults(FL_PDButtonFontSize, &cntl);
489 fl_set_defaults(FL_PDBrowserFontSize, &cntl);
490 fl_set_defaults(FL_PDLabelFontSize, &cntl);
491 fl_set_defaults(FL_PDChoiceFontSize, &cntl);
492 fl_set_defaults(FL_PDInputFontSize, &cntl);
493 fl_set_defaults(FL_PDBorderWidth, &cntl);
494 fl_initialize(&argc, argv, 0, 0, 0);
496 fd_test = create_form_test();
498 /* fill-in form initialization code */
499 combo.addline("Title");
500 combo.addline("Author");
501 combo.addline("Date");
502 combo.addline("Abstract");
503 combo.addline("Chapter");
504 combo.addline("Section");
505 combo.addline("Subsection");
506 combo.addline("List");
507 combo.addline("Description");
508 combo.addline("Verse");
509 combo.addline("Verbatim");
510 combo.setcallback(combo_cb);
513 /* show the first form */
514 fl_show_form(fd_test->test,FL_PLACE_CENTER,FL_FULLBORDER,"test");