]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/combox.h
8810d841adb84fe0c9ee8bed906783e254dd05ec
[lyx.git] / src / frontends / xforms / combox.h
1 // -*- C++ -*-
2 /*
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.         
6  * 
7  *  GNU Copyleft 1996 Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
8  *                        and the LyX Team.
9  * 
10  *  Dependencies:  Only XForms, but created to be used with LyX.
11  * 
12  */ 
13
14 /* Change log:
15  *  
16  *  2/06/1996,   Alejandro Aguilar Sierra 
17  *    Created and tested.
18  *  
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.
22  */ 
23
24 #ifndef COMBOX_H
25 #define COMBOX_H
26
27 #ifdef __GNUG__
28 #pragma interface
29 #endif
30
31 #include FORMS_H_LOCATION
32 #include <cstdlib>
33 #include "LString.h"
34
35 ///
36 enum combox_type {
37         ///
38         FL_COMBOX_NORMAL,
39         ///
40         FL_COMBOX_DROPLIST,
41         ///
42         FL_COMBOX_INPUT
43 };
44
45 class Combox;
46
47 /// callback prototype
48 typedef void (*FL_COMBO_CB) (int, void *, Combox *);
49 /// pre post prototype
50 typedef void (*FL_COMBO_PRE_POST) ();
51
52
53 ///
54 class Combox {
55 public:
56         ///
57         explicit Combox(combox_type t = FL_COMBOX_NORMAL);
58         ///
59         ~Combox();
60
61         /** To add this object to a form. Note that there are two heights
62             for normal (button) and expanded (browser) mode each.
63             The optional tabfolder arguments are needed to overcome an
64             xforms bug when repositioning a combox in a tab folder.
65             tabfolder1_ is the folder holding the combox.
66             If using nested tabfolders, tabfolder2_ is the "base" folder
67             holding tabfolder1_.
68         */
69         void add(int x, int y, int w, int hmin, int hmax,
70                  FL_OBJECT * tabfolder1_ = 0, FL_OBJECT * tabfolder2_ = 0);
71         
72         /// Add lines. Same as for fl_browser object
73         void addline(string const &);
74         /// Add lines. Same as for fl_browser object
75         void addto(string const &);
76         
77         /// Returns the selected item
78         int get() const;
79    
80         /// Returns a pointer to the selected line of text
81         string const getline() const;
82    
83         ///  Select an arbitrary item
84         void select(int);
85         ///
86         bool select_text(string const &);
87    
88         ///  Clear all the list
89         void clear();
90
91         /// Is the combox cleared (empty)
92         bool empty() const { return is_empty; }
93         
94         /// Remove the objects from the form they are in. 
95         void remove();
96
97         /** 
98          * Assign a callback to this object. The callback should be a void
99          * function with a int, a void pointer, and a Combox pointer as 
100          * parameters.
101         */
102         void setcallback(FL_COMBO_CB, void *);
103    
104         ///  Pre handler
105         void setpre(FL_COMBO_PRE_POST);
106         /// Post handler
107         void setpost(FL_COMBO_PRE_POST);
108         
109         ///  XForms attributes
110         void resize(unsigned);
111         ///
112         void gravity(unsigned, unsigned);
113         ///
114         void activate();
115         ///
116         void deactivate();
117         ///
118         void shortcut(string const &, int);
119         ///
120         void Redraw();
121         ///
122         void Show();
123         ///
124         static void combo_cb(FL_OBJECT *, long);
125         ///
126         static void input_cb(FL_OBJECT *, long);
127         ///
128         static int  peek_event(FL_FORM *, void *);
129  protected:
130         /// At least Hide should not be public
131         void Hide(int who = 0);
132         ///
133         FL_OBJECT * browser;
134  private:
135         ///
136         combox_type type;
137         ///
138         int bw;
139         ///
140         int bh;
141         ///
142         int sel;
143         ///
144         bool is_empty;
145         ///
146         FL_COMBO_CB callback;
147         ///
148         void * cb_arg;
149         ///
150         FL_COMBO_PRE_POST _pre;
151         ///
152         FL_COMBO_PRE_POST _post;
153         ///
154         FL_OBJECT * button;
155         ///
156         FL_OBJECT * label;
157         ///
158         FL_FORM* form;
159         ///
160         FL_OBJECT * tabfolder1;
161         ///
162         FL_OBJECT * tabfolder2;
163 };
164
165
166
167 //-----------------  Inline methods  --------------------------- 
168
169 inline
170 void Combox::addto(string const & text)
171 {
172         if (browser) {
173                 fl_addto_browser(browser, text.c_str());
174                 is_empty = false;
175         }
176 }
177
178
179 inline
180 void Combox::resize(unsigned r)
181 {
182    fl_set_object_resize(button, r);
183    if (label!= button) fl_set_object_resize(label, r); 
184 }
185
186
187 inline
188 void Combox::gravity(unsigned g1, unsigned g2)
189 {
190    fl_set_object_gravity(button, g1, g2);
191    if (label!= button) fl_set_object_gravity(label, g1, g2); 
192 }
193
194
195 inline
196 void Combox::shortcut(string const & s, int i)
197 {
198    if (button)
199       fl_set_object_shortcut(button, s.c_str(), i);
200 }
201
202
203 inline
204 void Combox::setcallback(FL_COMBO_CB cb, void * a = 0)
205 {
206    callback = cb;
207    cb_arg = a;
208 }
209
210
211 inline
212 void Combox::setpre(FL_COMBO_PRE_POST cb)
213 {
214         _pre = cb;
215 }
216
217
218 inline
219 void Combox::setpost(FL_COMBO_PRE_POST cb)
220 {
221         _post = cb;
222 }
223
224
225 inline
226 int Combox::get() const
227 {
228    return sel;
229 }
230
231
232 inline
233 string const Combox::getline() const
234 {
235     if (type == FL_COMBOX_INPUT) 
236       return fl_get_input(label);
237     else
238       return (browser && sel > 0) ?
239               string(fl_get_browser_line(browser, sel)) : string();
240 }
241
242 #endif