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