]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCharacter.C
Flatten the ButtonController tree by splitting it into a Controller and
[lyx.git] / src / frontends / xforms / FormCharacter.C
1 /**
2  * \file FormCharacter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <vector>
13
14
15 #include <config.h>
16
17 #include "xformsBC.h"
18 #include "ButtonController.h"
19 #include "ControlCharacter.h"
20 #include "FormCharacter.h"
21 #include "forms/form_character.h"
22 #include "gettext.h"
23 #include "combox.h"
24 #include "helper_funcs.h"
25 #include "xforms_helpers.h"
26 #include FORMS_H_LOCATION
27
28 #include "frnt_lang.h"
29
30 #include "support/lstrings.h"
31
32 using std::vector;
33 using std::find;
34
35 using namespace frnt;
36
37 typedef FormCB<ControlCharacter, FormDB<FD_character> > base_class;
38
39 FormCharacter::FormCharacter()
40         : base_class(_("Character Layout"), false)
41 {}
42
43
44 void FormCharacter::ComboInputCB(int, void * v, Combox *)
45 {
46         FormCharacter * pre = static_cast<FormCharacter*>(v);
47         // must use input() directly, to avoid treating the Combox
48         // as an FL_OBJECT, leading to a crash (bug 406)
49         pre->bc().input(pre->input(0, 0));
50 }
51
52 void FormCharacter::build()
53 {
54         dialog_.reset(build_character(this));
55
56         vector<FamilyPair>   const family = getFamilyData();
57         vector<SeriesPair>   const series = getSeriesData();
58         vector<ShapePair>    const shape  = getShapeData();
59         vector<SizePair>     const size   = getSizeData();
60         vector<BarPair>      const bar    = getBarData();
61         vector<ColorPair>    const color  = getColorData();
62         vector<LanguagePair> const langs  = getLanguageData(true);
63
64         // Store the identifiers for later
65         family_ = getSecond(family);
66         series_ = getSecond(series);
67         shape_  = getSecond(shape);
68         size_   = getSecond(size);
69         bar_    = getSecond(bar);
70         color_  = getSecond(color);
71         lang_   = getSecond(langs);
72
73         // create a string of entries " entry1 | entry2 | entry3 | entry4 "
74         // with which to initialise the xforms choice object.
75         string choice = ' ' + getStringFromVector(getFirst(family), " | ") + ' ';
76         fl_addto_choice(dialog_->choice_family, choice.c_str());
77
78         choice = ' ' + getStringFromVector(getFirst(series), " | ") + ' ';
79         fl_addto_choice(dialog_->choice_series, choice.c_str());
80
81         choice = ' ' + getStringFromVector(getFirst(shape), " | ") + ' ';
82         fl_addto_choice(dialog_->choice_shape, choice.c_str());
83
84         choice = ' ' + getStringFromVector(getFirst(size), " | ") + ' ';
85         fl_addto_choice(dialog_->choice_size, choice.c_str());
86
87         choice = ' ' + getStringFromVector(getFirst(bar), " | ") + ' ';
88         fl_addto_choice(dialog_->choice_bar, choice.c_str());
89
90         choice = ' ' + getStringFromVector(getFirst(color), " | ") + ' ';
91         fl_addto_choice(dialog_->choice_color, choice.c_str());
92
93         // xforms appears to need this to prevent a crash...
94         fl_addto_choice(dialog_->choice_language, "prevent crash");
95
96         // insert default language box manually
97         fl_addto_form(dialog_->form);
98         FL_OBJECT * ob = dialog_->choice_language;
99         fl_hide_object(dialog_->choice_language);
100
101         combo_language2_.reset(new Combox(FL_COMBOX_DROPLIST));
102         combo_language2_->add(ob->x, ob->y, ob->w, ob->h, 250);
103         combo_language2_->shortcut("#L", 1);
104         combo_language2_->setcallback(ComboInputCB, this);
105         fl_end_form();
106
107         // build up the combox entries
108         vector<LanguagePair>::const_iterator it  = langs.begin();
109         vector<LanguagePair>::const_iterator end = langs.end();
110         for (; it != end; ++it) {
111                 combo_language2_->addto(it->first);
112         }
113         combo_language2_->select(1);
114
115         // Manage the ok, apply and cancel/close buttons
116         bcview().setApply(dialog_->button_apply);
117         bcview().setCancel(dialog_->button_close);
118         bcview().addReadOnly(dialog_->check_toggle_all);
119 }
120
121
122 void FormCharacter::apply()
123 {
124         if (!form()) return;
125
126         int pos = fl_get_choice(dialog_->choice_family);
127         controller().setFamily(family_[pos - 1]);
128
129         pos = fl_get_choice(dialog_->choice_series);
130         controller().setSeries(series_[pos - 1]);
131
132         pos = fl_get_choice(dialog_->choice_shape);
133         controller().setShape(shape_[pos - 1]);
134
135         pos = fl_get_choice(dialog_->choice_size);
136         controller().setSize(size_[pos - 1]);
137
138         pos = fl_get_choice(dialog_->choice_bar);
139         controller().setBar(bar_[pos - 1]);
140
141         pos = fl_get_choice(dialog_->choice_color);
142         controller().setColor(color_[pos - 1]);
143
144         pos = combo_language2_->get();
145         controller().setLanguage(lang_[pos - 1]);
146
147         bool const toggleall = fl_get_button(dialog_->check_toggle_all);
148         controller().setToggleAll(toggleall);
149 }
150
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         pos = int(findPos(lang_, controller().getLanguage()));
173         combo_language2_->select(pos+1);
174
175         fl_set_button(dialog_->check_toggle_all, controller().getToggleAll());
176 }
177
178
179 ButtonPolicy::SMInput FormCharacter::input(FL_OBJECT *, long)
180 {
181         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
182
183         int pos = fl_get_choice(dialog_->choice_family);
184         if (family_[pos - 1] != LyXFont::IGNORE_FAMILY)
185                 activate = ButtonPolicy::SMI_VALID;
186
187         pos = fl_get_choice(dialog_->choice_series);
188         if (series_[pos - 1] != LyXFont::IGNORE_SERIES)
189                 activate = ButtonPolicy::SMI_VALID;
190
191         pos = fl_get_choice(dialog_->choice_shape);
192         if (shape_[pos - 1] != LyXFont::IGNORE_SHAPE)
193                 activate = ButtonPolicy::SMI_VALID;
194
195         pos = fl_get_choice(dialog_->choice_size);
196         if (size_[pos - 1] != LyXFont::IGNORE_SIZE)
197                 activate = ButtonPolicy::SMI_VALID;
198
199         pos = fl_get_choice(dialog_->choice_bar);
200         if (bar_[pos - 1] != frnt::IGNORE)
201                 activate = ButtonPolicy::SMI_VALID;
202
203         pos = fl_get_choice(dialog_->choice_color);
204         if (color_[pos - 1] != LColor::ignore)
205                 activate = ButtonPolicy::SMI_VALID;
206
207         pos = combo_language2_->get();
208         if (lang_[pos - 1] != "No change")
209                 activate = ButtonPolicy::SMI_VALID;
210
211         return activate;
212 }