]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GTabular.C
Extracted from r14281
[lyx.git] / src / frontends / gtk / GTabular.C
1 /**
2  * \file GTabular.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 _GLIBCXX_CONCEPT_CHECKS
15 #undef _GLIBCXX_CONCEPT_CHECKS
16 #endif
17 #ifdef _GLIBCPP_CONCEPT_CHECKS
18 #undef _GLIBCPP_CONCEPT_CHECKS
19 #endif
20
21 #include "GTabular.h"
22 #include "ControlTabular.h"
23
24 #include "ghelpers.h"
25
26 #include "controllers/helper_funcs.h"
27
28 #include "debug.h"
29
30 using std::string;
31 using std::vector;
32
33
34 #ifdef WITH_WARNINGS
35 #warning Implement booktabs settings
36 #endif
37
38 namespace lyx {
39 namespace frontend {
40
41 GTabular::GTabular(Dialog & parent)
42         : GViewCB<ControlTabular, GViewGladeB>(parent, _("Table Settings"), false)
43 {}
44
45
46 void GTabular::doBuild()
47 {
48         string const gladeName = findGladeFile("tabular");
49         xml_ = Gnome::Glade::Xml::create(gladeName);
50
51         Gtk::Button *button;
52         xml_->get_widget("Close", button);
53         setCancel(button);
54
55         // **** Current cell coords ****
56         xml_->get_widget("CurrentRow", cellrowspin_);
57         xml_->get_widget("CurrentColumn", cellcolspin_);
58
59         // **** Table tab ****
60         Gtk::HBox *box;
61         xml_->get_widget("HorizontalAlignment", box);
62         horzaligncombo_ = Gtk::manage (new Gtk::ComboBoxText);
63         box->add(*horzaligncombo_);
64         box->show_all();
65         xml_->get_widget("VerticalAlignment", vertaligncombo_);
66         xml_->get_widget("SpecifyWidth", specifywidthcheck_);
67         specifywidthcheck_->signal_toggled().connect(
68                 sigc::mem_fun(*this, &GTabular::updateSensitivity));
69
70         xml_->get_widget_derived ("Width", widthlength_);
71         xml_->get_widget("LaTeXArgument", latexargsentry_);
72         xml_->get_widget("RotateTable", rotatetablecheck_);
73
74         // **** This Cell tab ****
75         xml_->get_widget("UpperBorder", upperbordercheck_);
76         xml_->get_widget("LowerBorder", lowerbordercheck_);
77         xml_->get_widget("LeftBorder", leftbordercheck_);
78         xml_->get_widget("RightBorder", rightbordercheck_);
79         xml_->get_widget("RotateCell", rotatecellcheck_);
80         xml_->get_widget("MultiColumn", multicolcheck_);
81         multicolcheck_->signal_toggled().connect(
82                 sigc::mem_fun(*this, &GTabular::updateSensitivity));
83         xml_->get_widget("NoBorders", button);
84         button->signal_clicked().connect(
85                 sigc::bind(sigc::mem_fun(*this, &GTabular::allBorders), false));
86         xml_->get_widget("AllBorders", button);
87         button->signal_clicked().connect(
88                 sigc::bind(sigc::mem_fun(*this, &GTabular::allBorders), true));
89
90         // **** Longtable tab ****
91         xml_->get_widget("LongTable", longtablecheck_);
92
93         longtablecheck_->signal_toggled().connect(
94                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
95
96         xml_->get_widget("PageBreak", pagebreakcheck_);
97
98         xml_->get_widget("SpecialRows", specialrowstable_);
99
100         xml_->get_widget("HeaderOn",headeroncheck_);
101         xml_->get_widget("FooterOn",footeroncheck_);
102         xml_->get_widget("FirstHeaderOn",firstheaderoncheck_);
103         xml_->get_widget("LastFooterOn",lastfooteroncheck_);
104         xml_->get_widget("FirstHeaderEmpty",firstheaderemptycheck_);
105         xml_->get_widget("LastFooterEmpty",lastfooteremptycheck_);
106         xml_->get_widget("HeaderUpperBorderDouble",headerupperborderdoublecheck_);
107         xml_->get_widget("FooterUpperBorderDouble",footerupperborderdoublecheck_);
108         xml_->get_widget("FirstHeaderUpperBorderDouble",firstheaderupperborderdoublecheck_);
109         xml_->get_widget("LastFooterUpperBorderDouble",lastfooterupperborderdoublecheck_);
110         xml_->get_widget("HeaderLowerBorderDouble",headerlowerborderdoublecheck_);
111         xml_->get_widget("FooterLowerBorderDouble",footerlowerborderdoublecheck_);
112         xml_->get_widget("FirstHeaderLowerBorderDouble",firstheaderlowerborderdoublecheck_);
113         xml_->get_widget("LastFooterLowerBorderDouble",lastfooterlowerborderdoublecheck_);
114
115         headeroncheck_->signal_toggled().connect(
116                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
117         footeroncheck_->signal_toggled().connect(
118                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
119         firstheaderoncheck_->signal_toggled().connect(
120                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
121         lastfooteroncheck_->signal_toggled().connect(
122                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
123         firstheaderemptycheck_->signal_toggled().connect(
124                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
125         lastfooteremptycheck_->signal_toggled().connect(
126                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
127         headerupperborderdoublecheck_->signal_toggled().connect(
128                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
129         footerupperborderdoublecheck_->signal_toggled().connect(
130                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
131         firstheaderupperborderdoublecheck_->signal_toggled().connect(
132                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
133         lastfooterupperborderdoublecheck_->signal_toggled().connect(
134                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
135         headerlowerborderdoublecheck_->signal_toggled().connect(
136                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
137         footerlowerborderdoublecheck_->signal_toggled().connect(
138                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
139         firstheaderlowerborderdoublecheck_->signal_toggled().connect(
140                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
141         lastfooterlowerborderdoublecheck_->signal_toggled().connect(
142                 sigc::mem_fun(*this, &GTabular::updateSpecialRowsSensitivity));
143
144 /*      headeroncheck_;
145         firstheaderoncheck_;
146         lastfooteroncheck_;
147         firstheaderemptycheck_;
148         lastfooteremptycheck_;
149         headerupperborderdoublecheck_;
150         footerupperborderdoublecheck_;
151         firstheaderupperborderdoublecheck_;
152         lastfooterupperborderdoublecheck_;
153         headerlowerborderdoublecheck_;
154         footerlowerborderdoublecheck_;
155         firstheaderlowerborderdoublecheck_;
156         lastfooterlowerborderdoublecheck_;*/
157
158         // Disable for read-only documents.
159 //      bcview().addReadOnly(browsebutton_);
160
161 }
162
163
164 void GTabular::apply()
165 {
166
167 }
168
169
170 void GTabular::update()
171 {
172         updating_ = true;
173
174         LyXTabular const & tabular = controller().tabular();
175         LyXTabular::idx_type const cell = controller().getActiveCell();
176         bool const multicol = tabular.isMultiColumn(cell);
177
178         cellcolspin_->get_adjustment()->set_value(tabular.column_of_cell (cell)+1);
179         cellrowspin_->get_adjustment()->set_value(tabular.row_of_cell (cell));
180
181         // **** Table tab ****
182
183         LyXLength pwidth;
184         string special;
185
186         if (multicol) {
187                 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
188                 pwidth = tabular.getMColumnPWidth(cell);
189         } else {
190                 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
191                 pwidth = tabular.getColumnPWidth(cell);
192         }
193
194         updateHorzAlignCombo(!multicol && !pwidth.zero());
195
196         int halign = tabular.getAlignment(cell);
197         if (halign == LYX_ALIGN_RIGHT)
198                 horzaligncombo_->set_active(2);
199         else if (halign == LYX_ALIGN_CENTER)
200                 horzaligncombo_->set_active(1);
201         else if (halign == LYX_ALIGN_BLOCK)
202                 horzaligncombo_->set_active(3);
203         else
204                 horzaligncombo_->set_active(0);
205
206         int valign = tabular.getVAlignment(cell);
207         if (valign == LyXTabular::LYX_VALIGN_BOTTOM)
208                 vertaligncombo_->set_active(2);
209         else if (valign == LyXTabular::LYX_VALIGN_MIDDLE)
210                 vertaligncombo_->set_active(1);
211         else
212                 vertaligncombo_->set_active(0);
213
214         latexargsentry_->set_text(special);
215         specifywidthcheck_->set_active(!pwidth.zero());
216         widthlength_->set_length(pwidth);
217         rotatetablecheck_->set_active(tabular.getRotateTabular());
218
219         // **** This Cell tab ****
220         multicolcheck_->set_active(multicol);
221         rotatecellcheck_->set_active(tabular.getRotateCell(cell));
222
223         upperbordercheck_->set_active(tabular.topLine(cell));
224         lowerbordercheck_->set_active(tabular.bottomLine(cell));
225         leftbordercheck_->set_active(tabular.leftLine(cell));
226         rightbordercheck_->set_active(tabular.rightLine(cell));
227
228         // **** Long-table tab ****
229         LyXTabular::row_type const row(tabular.row_of_cell(cell));
230         bool const longtable = tabular.isLongTabular();
231         longtablecheck_->set_active(longtable);
232         pagebreakcheck_->set_active(tabular.getLTNewPage(row));
233
234         if (longtable) {
235                 LyXTabular::ltType ltt;
236                 bool use_empty;
237                 bool row_set = tabular.getRowOfLTHead(row, ltt);
238
239                 headeroncheck_->set_active(row_set);
240                 lyxerr << "row_set for header = " << row_set << "\n";
241                 if (ltt.set) {
242                         headerupperborderdoublecheck_->set_active(ltt.topDL);
243                         headerlowerborderdoublecheck_->set_active(ltt.bottomDL);
244                         use_empty = true;
245                 } else {
246                         headerupperborderdoublecheck_->set_active(false);
247                         headerlowerborderdoublecheck_->set_active(false);
248                         /*headerupperborderdoublecheck_->set_sensitive(false);
249                         headerlowerborderdoublecheck_->set_sensitive(false);*/
250                         firstheaderemptycheck_->set_active(false);
251                         /*firstheaderemptycheck_->set_sensitive(false);*/
252                         use_empty = false;
253                 }
254
255                 row_set = tabular.getRowOfLTFirstHead(row, ltt);
256                 firstheaderoncheck_->set_active(row_set);
257                 if (ltt.set && (!ltt.empty || !use_empty)) {
258                         firstheaderupperborderdoublecheck_->set_active(ltt.topDL);
259                         firstheaderlowerborderdoublecheck_->set_active(ltt.bottomDL);
260                 } else {
261                         firstheaderupperborderdoublecheck_->set_active(false);
262                         firstheaderlowerborderdoublecheck_->set_active(false);
263                         /*firstheaderupperborderdoublecheck_->set_sensitive(false);
264                         firstheaderlowerborderdoublecheck_->set_sensitive(false);*/
265                         if (use_empty) {
266                                 firstheaderemptycheck_->set_active(ltt.empty);
267                                 //if (ltt.empty)
268                                         //firstheaderoncheck_->set_sensitive(false);
269                         }
270                 }
271
272                 row_set = tabular.getRowOfLTFoot(row, ltt);
273                 footeroncheck_->set_active(row_set);
274                 if (ltt.set) {
275                         footerupperborderdoublecheck_->set_active(ltt.topDL);
276                         footerlowerborderdoublecheck_->set_active(ltt.bottomDL);
277                         use_empty = true;
278                 } else {
279                         footerupperborderdoublecheck_->set_active(false);
280                         footerlowerborderdoublecheck_->set_active(false);
281                         //footerupperborderdoublecheck_->set_sensitive(false);
282                         //footerlowerborderdoublecheck_->set_sensitive(false);
283                         lastfooteremptycheck_->set_active(false);
284                         //lastfooteremptycheck_->set_sensitive(false);
285                         use_empty = false;
286                 }
287
288                 row_set = tabular.getRowOfLTLastFoot(row, ltt);
289                 lastfooteroncheck_->set_active(row_set);
290                 if (ltt.set && (!ltt.empty || !use_empty)) {
291                         lastfooterupperborderdoublecheck_->set_active(ltt.topDL);
292                         lastfooterlowerborderdoublecheck_->set_active(ltt.bottomDL);
293                 } else {
294                         lastfooterupperborderdoublecheck_->set_active(false);
295                         lastfooterlowerborderdoublecheck_->set_active(false);
296                         //lastfooterupperborderdoublecheck_->set_sensitive(false);
297                         //lastfooterlowerborderdoublecheck_->set_sensitive(false);
298                         if (use_empty) {
299                                 lastfooteremptycheck_->set_active(ltt.empty);
300                                 //if (ltt.empty)
301                                         //lastfooteroncheck_->set_sensitive(false);
302                         }
303                 }
304
305         }
306
307         updating_ = false;
308
309         updateSensitivity();
310         updateSpecialRowsSensitivity();
311 }
312
313
314 void GTabular::onInput()
315 {
316         updateSensitivity();
317 }
318
319
320 void GTabular::allBorders(bool borders)
321 {
322         if (upperbordercheck_->is_sensitive())
323                 upperbordercheck_->set_active(borders);
324         if (lowerbordercheck_->is_sensitive())
325                 lowerbordercheck_->set_active(borders);
326         if (leftbordercheck_->is_sensitive())
327                 leftbordercheck_->set_active(borders);
328         if (rightbordercheck_->is_sensitive())
329                 rightbordercheck_->set_active(borders);
330 }
331
332
333 void GTabular::updateSensitivity()
334 {
335         if (updating_)
336                 return;
337
338         bool const specifywidth = specifywidthcheck_->get_active();
339         bool const multicol = multicolcheck_->get_active();
340
341         widthlength_->set_sensitive(specifywidth);
342         vertaligncombo_->set_sensitive(specifywidth);
343
344         updateHorzAlignCombo(!multicol && specifywidth);
345 }
346
347
348 void GTabular::updateHorzAlignCombo(bool extraoption)
349 {
350         if (updating_)
351                 return;
352
353         //FIXME: check if we really need to completely rewrite combo data
354         // Deprecated in favor of clear_items in gtkmm >= 2.8
355         unsigned int const oldselection = horzaligncombo_->get_active_row_number();
356         horzaligncombo_->clear();
357         horzaligncombo_->append_text(_("Left"));
358         horzaligncombo_->append_text(_("Center"));
359         horzaligncombo_->append_text(_("Right"));
360         if (extraoption)
361                 horzaligncombo_->append_text(_("Block"));
362         if (oldselection < horzaligncombo_->get_model()->children().size())
363                 horzaligncombo_->set_active(oldselection);
364         else
365                 horzaligncombo_->set_active(0);
366 }
367
368
369 void GTabular::updateSpecialRowsSensitivity()
370 {
371         if (updating_)
372                 return;
373
374         bool const header = headeroncheck_->get_active();
375         bool const footer = footeroncheck_->get_active();
376         bool const firstheader = firstheaderoncheck_->get_active();
377         bool const firstheaderempty = firstheaderemptycheck_->get_active();
378         bool const lastfooter = lastfooteroncheck_->get_active();
379         bool const lastfooterempty = lastfooteremptycheck_->get_active();
380         firstheaderemptycheck_->set_sensitive(!firstheader);
381         lastfooteremptycheck_->set_sensitive(!lastfooter);
382         headerupperborderdoublecheck_->set_sensitive(header);
383         headerlowerborderdoublecheck_->set_sensitive(header);
384         footerupperborderdoublecheck_->set_sensitive(footer);
385         footerlowerborderdoublecheck_->set_sensitive(footer);
386
387         firstheaderoncheck_->set_sensitive(!firstheaderempty);
388         firstheaderupperborderdoublecheck_->set_sensitive(firstheaderempty || firstheader);
389         firstheaderlowerborderdoublecheck_->set_sensitive(firstheaderempty || firstheader);
390         lastfooteroncheck_->set_sensitive(!lastfooterempty);
391         lastfooterupperborderdoublecheck_->set_sensitive(lastfooterempty || lastfooter);
392         lastfooterlowerborderdoublecheck_->set_sensitive(lastfooterempty || lastfooter);
393         bool const longtable = longtablecheck_->get_active();
394         specialrowstable_->set_sensitive(longtable);
395         pagebreakcheck_->set_sensitive(longtable);
396 }
397
398 } // namespace frontend
399 } // namespace lyx