]> git.lyx.org Git - features.git/blob - src/frontends/gtk/GBox.C
disable conecpt checks for gtk dir, fix concept checks for qt
[features.git] / src / frontends / gtk / GBox.C
1 /**
2  * \file GBox.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Spray
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCPP_CONCEPT_CHECKS
15 #undef _GLIBCPP_CONCEPT_CHECKS
16 #endif
17
18 #include "GBox.h"
19
20 #include "ghelpers.h"
21
22 #include "controllers/ControlBox.h"
23
24 #include "insets/insetbox.h"
25 #include "lengthcommon.h"
26 #include "lyxrc.h" // to set the default length values
27
28 using std::string;
29 using std::vector;
30
31 namespace lyx {
32 namespace frontend {
33
34 namespace {
35 string defaultUnit("cm");
36 } // namespace anon
37
38 GBox::GBox(Dialog & parent)
39         : GViewCB<ControlBox, GViewGladeB>(parent, _("Box Settings"), false)
40 {}
41
42
43 void GBox::doBuild()
44 {
45         string const gladeName = findGladeFile("box");
46         xml_ = Gnome::Glade::Xml::create(gladeName);
47
48         Gtk::Button * closebutton;
49         xml_->get_widget("Close", closebutton);
50         setCancel(closebutton);
51
52         xml_->get_widget("Type", typecombo_);
53         bcview().addReadOnly(typecombo_);
54         xml_->get_widget("InnerBox", innerboxcombo_);
55         bcview().addReadOnly(innerboxcombo_);
56         xml_->get_widget("WidthUnits", widthunitscombo_);
57         bcview().addReadOnly(widthunitscombo_);
58         xml_->get_widget("HeightUnits", heightunitscombo_);
59         bcview().addReadOnly(heightunitscombo_);
60         xml_->get_widget("BoxVertical", boxvertcombo_);
61         bcview().addReadOnly(boxvertcombo_);
62         xml_->get_widget("ContentVertical", contentvertcombo_);
63         bcview().addReadOnly(contentvertcombo_);
64         xml_->get_widget("ContentHorizontal", contenthorzcombo_);
65         bcview().addReadOnly(contenthorzcombo_);
66         xml_->get_widget("Width", widthspin_);
67         bcview().addReadOnly(widthspin_);
68         xml_->get_widget("Height", heightspin_);
69         bcview().addReadOnly(heightspin_);
70
71         cols_.add(stringcol_);
72
73         // fill the box type choice
74         box_gui_tokens(ids_, gui_names_);
75         PopulateComboBox(typecombo_, gui_names_);
76         typecombo_->signal_changed().connect(
77                 sigc::mem_fun(*this, &GBox::onTypeComboChanged));
78
79         // set up innerbox (populated in setInnerType)
80         innerboxstore_ = Gtk::ListStore::create(cols_);
81         innerboxcombo_->set_model(innerboxstore_);
82         Gtk::CellRendererText * cell = Gtk::manage(new Gtk::CellRendererText);
83         innerboxcombo_->pack_start(*cell, true);
84         innerboxcombo_->add_attribute(*cell, "text", 0);
85
86         innerboxcombo_->signal_changed().connect(
87                 sigc::mem_fun(*this, &GBox::onInnerBoxComboChanged));
88
89         boxvertcombo_->signal_changed().connect(
90                 sigc::mem_fun(*this, &GBox::onAlignChanged));
91         contenthorzcombo_->signal_changed().connect(
92                 sigc::mem_fun(*this, &GBox::onAlignChanged));
93         contentvertcombo_->signal_changed().connect(
94                 sigc::mem_fun(*this, &GBox::onAlignChanged));
95
96         heightunitscombo_->signal_changed().connect(
97                 sigc::mem_fun(*this, &GBox::onHeightChanged));
98         widthunitscombo_->signal_changed().connect(
99                 sigc::mem_fun(*this, &GBox::onWidthChanged));
100
101         heightspin_->signal_value_changed().connect(
102                 sigc::mem_fun(*this, &GBox::onHeightChanged));
103         widthspin_->signal_value_changed().connect(
104                 sigc::mem_fun(*this, &GBox::onWidthChanged));
105
106
107         widthunitsstore_ = Gtk::ListStore::create(cols_);
108         widthunitscombo_->set_model(widthunitsstore_);
109         cell = Gtk::manage(new Gtk::CellRendererText);
110         widthunitscombo_->pack_start(*cell, true);
111         widthunitscombo_->add_attribute(*cell, "text", 0);
112         //widthunitscombo_ is populated in setSpecial
113
114         box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
115         vector<string> heightunits = buildLengthUnitList();
116         // Append special entries, skipping the first item "None"
117         heightunits.insert(heightunits.end(),
118                 ++gui_names_spec_.begin(), gui_names_spec_.end());
119
120         PopulateComboBox(heightunitscombo_, heightunits);
121 }
122
123
124 void GBox::PopulateComboBox(Gtk::ComboBox * combo,
125                                   vector<string> const & strings
126                                   )
127 {
128         Glib::RefPtr<Gtk::ListStore> model = Gtk::ListStore::create(cols_);
129         vector<string>::const_iterator it = strings.begin();
130         vector<string>::const_iterator end = strings.end();
131         for(; it != end; ++it)
132                 (*model->append())[stringcol_] = *it;
133
134         combo->set_model(model);
135         Gtk::CellRendererText * cell = Gtk::manage(new Gtk::CellRendererText);
136         combo->pack_start(*cell, true);
137         combo->add_attribute(*cell, "text", 0);
138 }
139
140
141 void GBox::update()
142 {
143         applylock_ = true;
144
145         defaultUnit = getDefaultUnit();
146
147         char c = controller().params().pos;
148         boxvertcombo_->set_active(string("tcb").find(c, 0));
149         c = controller().params().inner_pos;
150         contentvertcombo_->set_active(string("tcbs").find(c, 0));
151         c = controller().params().hor_pos;
152         contenthorzcombo_->set_active(string("lcrs").find(c, 0));
153
154         string type(controller().params().type);
155         for (unsigned int i = 0; i < gui_names_.size(); ++i) {
156                 if (type == ids_[i])
157                         typecombo_->set_active(i);
158         }
159
160         applylock_ = false;
161         updateInnerBoxCombo();
162         applylock_ = true;
163
164         bool ibox = controller().params().inner_box;
165         boxvertcombo_->set_sensitive(ibox);
166         contentvertcombo_->set_sensitive(ibox);
167         contenthorzcombo_->set_sensitive(!ibox);
168         setSpecial(ibox);
169
170         widthspin_->get_adjustment()->set_value(controller().params().width.value());
171         unitsComboFromLength(widthunitscombo_, stringcol_,
172                              controller().params().width, defaultUnit);
173
174         string const special(controller().params().special);
175         if (!special.empty() && special != "none") {
176                 string spc;
177                 for (unsigned int i = 0; i < gui_names_spec_.size(); ++i) {
178                         if (special == ids_spec_[i])
179                                 spc = gui_names_spec_[i];
180                 }
181                 for (int j = 0; j < widthunitsstore_->children().size(); ++j) {
182                         if (widthunitsstore_->children()[j][stringcol_] == spc)
183                                 widthunitscombo_->set_active(j);
184                 }
185         }
186
187         heightspin_->get_adjustment()->set_value(controller().params().height.value());
188         unitsComboFromLength(heightunitscombo_, stringcol_,
189                              controller().params().height, defaultUnit);
190
191         string const height_special(controller().params().height_special);
192         if (!height_special.empty() && height_special != "none") {
193                 string hspc;
194                 for (unsigned int i = 0; i < gui_names_spec_.size(); ++i) {
195                         if (height_special == ids_spec_[i]) {
196                                 hspc = gui_names_spec_[i];
197                         }
198                 }
199                 for (int j = 0; j < heightunitscombo_->get_model()->children().size(); ++j) {
200                         if (heightunitscombo_->get_model()->children()[j][stringcol_] == hspc) {
201                                 heightunitscombo_->set_active(j);
202                         }
203                 }
204         }
205
206         heightspin_->set_sensitive(ibox);
207         heightunitscombo_->set_sensitive(ibox);
208         applylock_ = false;
209 }
210
211
212 void GBox::setSpecial(bool ibox)
213 {
214         bool const oldlock = applylock_;
215         applylock_ = true;
216
217         unsigned int const initselection = widthunitscombo_->get_active_row_number();
218         widthunitsstore_->clear();
219         vector<string> normalunits = buildLengthUnitList();
220         if (ibox) {
221                 vector<string>::const_iterator it = normalunits.begin();
222                 vector<string>::const_iterator end = normalunits.end();
223                 for(; it != end; ++it)
224                         (*widthunitsstore_->append())[stringcol_] = *it;
225         } else {
226                 vector<string>::const_iterator it = normalunits.begin();
227                 vector<string>::const_iterator end = normalunits.end();
228                 for(; it != end; ++it)
229                         (*widthunitsstore_->append())[stringcol_] = *it;
230                 // Skip the first item "None"
231                 it = ++gui_names_spec_.begin();
232                 end = gui_names_spec_.end();
233                 for(; it != end; ++it)
234                         (*widthunitsstore_->append())[stringcol_] = *it;
235         }
236
237         int const store_size = widthunitsstore_->children().size();
238         if (initselection >= store_size) {
239                 widthunitscombo_->set_active(0);
240                 onWidthChanged();
241         } else {
242                 widthunitscombo_->set_active(initselection);
243         }
244         applylock_ = oldlock;
245 }
246
247
248 void GBox::updateInnerBoxCombo()
249 {
250         bool const oldlock = applylock_;
251         applylock_ = true;
252         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
253         // we have to remove "none" then and adjust the combo
254
255         // default: minipage
256         unsigned int i = 2;
257         if (!controller().params().inner_box)
258                 // none
259                 i = 0;
260         if (controller().params().use_parbox)
261                 // parbox
262                 i = 1;
263         bool frameless = (controller().params().type == "Frameless");
264
265         int const oldsize = innerboxstore_->children().size();
266         // Store the initial selection in 0,1,2 format
267         int oldselection = -1;
268         if (oldsize == 2)
269                 oldselection = innerboxcombo_->get_active_row_number() + 1;
270         else if (oldsize == 3)
271                 oldselection = innerboxcombo_->get_active_row_number();
272
273         if (frameless && oldsize != 2) {
274                 innerboxstore_->clear();
275                 (*innerboxstore_->append())[stringcol_] = _("Parbox");
276                 (*innerboxstore_->append())[stringcol_] = _("Minipage");
277                 // Cope when the backend asks for no inner box in
278                 // a frameless box
279                 if (i == 0) {
280                         applylock_ = false;
281                         innerboxcombo_->set_active(i);
282                         applylock_ = true;
283                 } else
284                         innerboxcombo_->set_active(i - 1);
285         } else if (!frameless && oldsize != 3) {
286                 innerboxstore_->clear();
287                 (*innerboxstore_->append())[stringcol_] = _("None");
288                 (*innerboxstore_->append())[stringcol_] = _("Parbox");
289                 (*innerboxstore_->append())[stringcol_] = _("Minipage");
290                 innerboxcombo_->set_active(i);
291         } else {
292                 // we're not changing the liststore, just selecting i
293                 if (frameless)
294                         innerboxcombo_->set_active(i - 1);
295                 else
296                         innerboxcombo_->set_active(i);
297         }
298
299         // Update the width units list if we've changed inner box type
300         if (i != oldselection)
301                 setSpecial(i != 0);
302
303         applylock_ = oldlock;
304 }
305
306
307 void GBox::onInnerBoxComboChanged()
308 {
309         if (applylock_)
310                 return;
311
312         controller().params().use_parbox =
313                 (*innerboxcombo_->get_active())[stringcol_] ==  _("Parbox");
314
315         bool const ibox = (*innerboxcombo_->get_active())[stringcol_] != _("None");
316         controller().params().inner_box = ibox;
317         setSpecial(ibox);
318
319         boxvertcombo_->set_sensitive(ibox);
320         contentvertcombo_->set_sensitive(ibox);
321         contenthorzcombo_->set_sensitive(!ibox);
322         heightspin_->set_sensitive(ibox);
323         heightunitscombo_->set_sensitive(ibox);
324         // wtf? form_->set_sensitive(ibox);
325
326         controller().dispatchParams();
327 }
328
329
330 void GBox::onTypeComboChanged()
331 {
332         int const index = typecombo_->get_active_row_number();
333         controller().params().type = ids_[index];
334
335         bool frameless = (index == 0);
336         if (frameless) {
337                 boxvertcombo_->set_sensitive(true);
338                 contentvertcombo_->set_sensitive(true);
339                 contenthorzcombo_->set_sensitive(false);
340                 heightspin_->set_sensitive(true);
341                 heightunitscombo_->set_sensitive(true);
342                 //wtf? form_->setSpecial(true);
343         }
344         //int itype = innerboxcombo_->get_active_row_number();
345         controller().dispatchParams();
346
347         updateInnerBoxCombo();
348 }
349
350
351 void GBox::onHeightChanged()
352 {
353         if (applylock_)
354                 return;
355
356         // "None"
357         int i = 0;
358         bool spec = false;
359         Glib::ustring special = (*heightunitscombo_->get_active())[stringcol_];
360         for (int j = 1; j < gui_names_spec_.size() ; ++j) {
361                 if (gui_names_spec_[j] == special) {
362                         i=j;
363                         spec = true;
364                 }
365         }
366         controller().params().height_special = ids_spec_[i];
367
368         string height;
369         if (spec) {
370                 height = heightspin_->get_text();
371                 // beware: bogosity! the unit is simply ignored in this case
372                 height += "in";
373         } else {
374                 Glib::ustring const heightunit =
375                         (*heightunitscombo_->get_active())[stringcol_];
376                 height = heightspin_->get_text() + heightunit;
377         }
378
379         controller().params().height = LyXLength(height);
380         controller().dispatchParams();
381 }
382
383
384 void GBox::onWidthChanged()
385 {
386         if (applylock_)
387                 return;
388
389         int i = 0;
390         bool spec = false;
391         Glib::ustring special = (*widthunitscombo_->get_active())[stringcol_];
392         for (int j = 1; j < gui_names_spec_.size() ; ++j) {
393                 if (gui_names_spec_[j] == special) {
394                         i=j;
395                         spec = true;
396                 }
397         }
398         controller().params().special = ids_spec_[i];
399
400         string width;
401         if (spec) {
402                 width = widthspin_->get_text();
403                 // beware: bogosity! the unit is simply ignored in this case
404                 width += "in";
405         } else {
406                 Glib::ustring const widthunit =
407                         (*widthunitscombo_->get_active())[stringcol_];
408                 width = widthspin_->get_text() + widthunit;
409         }
410
411         controller().params().width = LyXLength(width);
412         controller().dispatchParams();
413 }
414
415
416 void GBox::onAlignChanged()
417 {
418         if (applylock_)
419                 return;
420
421         controller().params().pos =
422                 "tcb"[boxvertcombo_->get_active_row_number()];
423         controller().params().inner_pos =
424                 "tcbs"[contenthorzcombo_->get_active_row_number()];
425         controller().params().hor_pos =
426                 "lcrs"[contentvertcombo_->get_active_row_number()];
427
428         controller().dispatchParams();
429 }
430
431 } // namespace frontend
432 } // namespace lyx