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