]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCharacter.C
Bugfixes: checkboxes to radiobuttons (from J�rgen S) and remove a little
[lyx.git] / src / frontends / xforms / FormCharacter.C
1 /**
2  * \file FormCharacter.C
3  * Copyright 2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Edwin Leuven,  leuven@fee.uva.nl
7  * \author Angus Leeming, a.leeming@ic.ac.uk
8  */
9
10 #include <vector>
11
12 #ifdef __GNUG_
13 #pragma implementation
14 #endif
15
16 #include <config.h>
17
18 #include "xformsBC.h"
19 #include "ControlCharacter.h"
20 #include "FormCharacter.h"
21 #include "form_character.h"
22 #include "gettext.h"
23 #include "combox.h"
24 #include "helper_funcs.h"
25
26 using std::vector;
27 using namespace character;
28
29 typedef FormCB<ControlCharacter, FormDB<FD_form_character> > base_class;
30
31 FormCharacter::FormCharacter(ControlCharacter & c)
32         : base_class(c, _("Character Layout"), false)
33 {}
34
35
36 void FormCharacter::ComboInputCB(int, void * v, Combox * combox)
37 {
38         FormCharacter * pre = static_cast<FormCharacter*>(v);
39         pre->InputCB(reinterpret_cast<FL_OBJECT *>(combox), 0);
40 }
41
42 void FormCharacter::build()
43 {
44         dialog_.reset(build_character());
45
46         vector<FamilyPair> const family = getFamilyData();
47         vector<SeriesPair> const series = getSeriesData();
48         vector<ShapePair>  const shape  = getShapeData();
49         vector<SizePair>   const size   = getSizeData();
50         vector<BarPair>    const bar    = getBarData();
51         vector<ColorPair>  const color  = getColorData();
52         vector<string> const language = getLanguageData();
53
54         // Store the enums for later
55         family_ = getSecond(family);
56         series_ = getSecond(series);
57         shape_ = getSecond(shape);
58         size_ = getSecond(size);
59         bar_ = getSecond(bar);
60         color_ = getSecond(color);
61
62         // create a string of entries " entry1 | entry2 | entry3 | entry4 "
63         // with which to initialise the xforms choice object.
64         string choice = " " + getStringFromVector(getFirst(family), " | ") +" ";
65         fl_addto_choice(dialog_->choice_family, choice.c_str());
66
67         choice = " " + getStringFromVector(getFirst(series), " | ") + " ";
68         fl_addto_choice(dialog_->choice_series, choice.c_str());
69
70         choice = " " + getStringFromVector(getFirst(shape), " | ") + " ";
71         fl_addto_choice(dialog_->choice_shape, choice.c_str());
72
73         choice = " " + getStringFromVector(getFirst(size), " | ") + " ";
74         fl_addto_choice(dialog_->choice_size, choice.c_str());
75
76         choice = " " + getStringFromVector(getFirst(bar), " | ") + " ";
77         fl_addto_choice(dialog_->choice_bar, choice.c_str());
78
79         choice = " " + getStringFromVector(getFirst(color), " | ") + " ";
80         fl_addto_choice(dialog_->choice_color, choice.c_str());
81
82         // xforms appears to need this to prevent a crash...
83         fl_addto_choice(dialog_->choice_language, "prevent crash");
84
85         // insert default language box manually
86         fl_addto_form(dialog_->form);
87                 FL_OBJECT * ob = dialog_->choice_language;
88                 fl_hide_object(dialog_->choice_language);
89
90                 combo_language2_.reset(new Combox(FL_COMBOX_DROPLIST));
91                 combo_language2_->add(ob->x, ob->y, ob->w, ob->h, 250);
92                 combo_language2_->shortcut("#L", 1);
93                 combo_language2_->setcallback(ComboInputCB, this);
94         fl_end_form();
95
96         // build up the combox entries
97         for (vector<string>::const_iterator cit = language.begin(); 
98              cit != language.end(); ++cit) {
99                 combo_language2_->addto(*cit);
100         }
101         combo_language2_->select(*language.begin());
102
103         // Manage the ok, apply and cancel/close buttons
104         bc().setApply(dialog_->button_apply);
105         bc().setCancel(dialog_->button_close);
106         bc().addReadOnly(dialog_->check_toggle_all);
107 }
108
109
110 void FormCharacter::apply()
111 {
112         if (!form()) return;
113
114         int pos = fl_get_choice(dialog_->choice_family);
115         controller().setFamily(family_[pos-1]);
116
117         pos = fl_get_choice(dialog_->choice_series);
118         controller().setSeries(series_[pos-1]);
119    
120         pos = fl_get_choice(dialog_->choice_shape);
121         controller().setShape(shape_[pos-1]);
122
123         pos = fl_get_choice(dialog_->choice_size);
124         controller().setSize(size_[pos-1]);
125    
126         pos = fl_get_choice(dialog_->choice_bar);
127         controller().setBar(bar_[pos-1]);
128    
129         pos = fl_get_choice(dialog_->choice_color);
130         controller().setColor(color_[pos-1]);
131
132         controller().setLanguage(combo_language2_->getline());
133
134         bool const toggleall = fl_get_button(dialog_->check_toggle_all);
135         controller().setToggleAll(toggleall);
136 }
137
138 namespace {
139
140 template<class A>
141 typename vector<A>::size_type findPos(vector<A> const & vec, A const & val)
142 {
143         vector<A>::const_iterator it =
144                 std::find(vec.begin(), vec.end(), val);
145         if (it == vec.end())
146                 return 0;
147         return it - vec.begin();
148 }
149  
150 } // namespace anon
151
152 void FormCharacter::update()
153 {
154         int pos = int(findPos(family_, controller().getFamily()));
155         fl_set_choice(dialog_->choice_family, pos+1);
156         
157         pos = int(findPos(series_, controller().getSeries()));
158         fl_set_choice(dialog_->choice_series, pos+1);
159
160         pos = int(findPos(shape_, controller().getShape()));
161         fl_set_choice(dialog_->choice_shape, pos+1);
162
163         pos = int(findPos(size_, controller().getSize()));
164         fl_set_choice(dialog_->choice_size, pos+1);
165
166         pos = int(findPos(bar_, controller().getBar()));
167         fl_set_choice(dialog_->choice_bar, pos+1);
168
169         pos = int(findPos(color_, controller().getColor()));
170         fl_set_choice(dialog_->choice_color, pos+1);
171
172         combo_language2_->select(controller().getLanguage());
173
174         fl_set_button(dialog_->check_toggle_all, controller().getToggleAll());
175 }
176
177
178 ButtonPolicy::SMInput FormCharacter::input(FL_OBJECT *, long)
179 {
180         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
181
182         int pos = fl_get_choice(dialog_->choice_family);
183         if (family_[pos-1] != LyXFont::IGNORE_FAMILY)
184                 activate = ButtonPolicy::SMI_VALID;
185         
186         pos = fl_get_choice(dialog_->choice_series);
187         if (series_[pos-1] != LyXFont::IGNORE_SERIES)
188                 activate = ButtonPolicy::SMI_VALID;
189
190         pos = fl_get_choice(dialog_->choice_shape);
191         if (shape_[pos-1] != LyXFont::IGNORE_SHAPE)
192                 activate = ButtonPolicy::SMI_VALID;
193
194         pos = fl_get_choice(dialog_->choice_size);
195         if (size_[pos-1] != LyXFont::IGNORE_SIZE)
196                 activate = ButtonPolicy::SMI_VALID;
197
198         pos = fl_get_choice(dialog_->choice_bar);
199         if (bar_[pos-1] != character::IGNORE)
200                 activate = ButtonPolicy::SMI_VALID;
201
202         pos = fl_get_choice(dialog_->choice_color);
203         if (color_[pos-1] != LColor::ignore)
204                 activate = ButtonPolicy::SMI_VALID;
205
206         string const language = combo_language2_->getline();
207         if (language != _("No change"))
208                 activate = ButtonPolicy::SMI_VALID;
209
210         return activate;
211 }