]> git.decadent.org.uk Git - ion3.git/blob - mod_query/query.c
[svn-inject] Installing original source of ion3
[ion3.git] / mod_query / query.c
1 /*
2  * ion/mod_query/query.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2006. 
5  *
6  * Ion is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <libextl/extl.h>
13
14 #include <ioncore/common.h>
15 #include <ioncore/global.h>
16 #include <ioncore/binding.h>
17 #include <ioncore/regbind.h>
18 #include <ioncore/key.h>
19 #include "query.h"
20 #include "wedln.h"
21
22
23 static void create_cycle_binding(WEdln *wedln, XKeyEvent *ev, ExtlFn cycle)
24 {
25     WBindmap *bindmap=create_bindmap();
26     WBinding b;
27     
28     if(bindmap==NULL)
29         return;
30         
31     b.ksb=XKeycodeToKeysym(ioncore_g.dpy, ev->keycode, 0);
32     b.kcb=ev->keycode;
33     b.state=ev->state;
34     b.act=BINDING_KEYPRESS;
35     b.area=0;
36     b.wait=FALSE;
37     b.submap=NULL;
38     b.func=extl_ref_fn(cycle);
39     
40     if(!bindmap_add_binding(bindmap, &b)){
41         extl_unref_fn(b.func);
42         bindmap_destroy(bindmap);
43         return;
44     }
45     
46     if(!region_add_bindmap((WRegion*)wedln, bindmap)){
47         bindmap_destroy(bindmap);
48         return;
49     }
50     
51     wedln->cycle_bindmap=bindmap;
52 }
53     
54
55 /*--lowlevel routine not to be called by the user--EXTL_DOC
56  * Show a query window in \var{mplex} with prompt \var{prompt}, initial
57  * contents \var{dflt}. The function \var{handler} is called with
58  * the entered string as the sole argument when \fnref{WEdln.finish}
59  * is called. The function \var{completor} is called with the created
60  * \type{WEdln} is first argument and the string to complete is the
61  * second argument when \fnref{WEdln.complete} is called.
62  */
63 EXTL_EXPORT
64 WEdln *mod_query_do_query(WMPlex *mplex, const char *prompt, const char *dflt,
65                           ExtlFn handler, ExtlFn completor, ExtlFn cycle)
66 {
67     WRectangle geom;
68     WEdlnCreateParams fnp;
69     WMPlexAttachParams par;
70     XKeyEvent *ev=ioncore_current_key_event();
71     WEdln *wedln;
72
73     fnp.prompt=prompt;
74     fnp.dflt=dflt;
75     fnp.handler=handler;
76     fnp.completor=completor;
77     
78     par.flags=(MPLEX_ATTACH_SWITCHTO|
79                MPLEX_ATTACH_MODAL|
80                MPLEX_ATTACH_UNNUMBERED|
81                MPLEX_ATTACH_SIZEPOLICY);
82     par.szplcy=SIZEPOLICY_FULL_BOUNDS;
83
84     wedln=(WEdln*)mplex_do_attach_new(mplex, &par,
85                                       (WRegionCreateFn*)create_wedln,
86                                       (void*)&fnp); 
87                                       
88     if(wedln!=NULL && ev!=NULL && cycle!=extl_fn_none())
89         create_cycle_binding(wedln, ev, cycle);
90         
91     
92     return wedln;
93 }
94