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