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