]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiTabular.cpp
Cosmetics.
[lyx.git] / src / frontends / qt4 / GuiTabular.cpp
1 /**
2  * \file GuiTabular.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  * \author Herbert Voß
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiTabular.h"
16
17 #include "GuiSetBorder.h"
18 #include "GuiView.h"
19 #include "LengthCombo.h"
20 #include "qt_helpers.h"
21 #include "Validator.h"
22
23 #include "BufferView.h"
24 #include "Cursor.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "LyXFunc.h"
28
29 #include "insets/InsetTabular.h"
30
31 #include <QCheckBox>
32 #include <QPushButton>
33 #include <QRadioButton>
34 #include <QLineEdit>
35
36 using namespace std;
37
38 namespace lyx {
39 namespace frontend {
40
41 GuiTabular::GuiTabular(GuiView & lv)
42         : GuiDialog(lv, "tabular", qt_("Table Settings")),
43         // tabular_ is initialised at dialog construction in initialiseParams()
44         tabular_(*lv.buffer(), 0, 0)
45 {
46         active_cell_ = Tabular::npos;
47
48         setupUi(this);
49
50         widthED->setValidator(unsignedLengthValidator(widthED));
51         topspaceED->setValidator(new LengthValidator(topspaceED));
52         bottomspaceED->setValidator(new LengthValidator(bottomspaceED));
53         interlinespaceED->setValidator(new LengthValidator(interlinespaceED));
54
55         connect(topspaceED, SIGNAL(returnPressed()),
56                 this, SLOT(topspace_changed()));
57         connect(topspaceUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
58                 this, SLOT(topspace_changed()));
59         connect(topspaceCO, SIGNAL(activated(int)), this, SLOT(topspace_changed()));
60         connect(bottomspaceED, SIGNAL(returnPressed()),
61                 this, SLOT(bottomspace_changed()));
62         connect(bottomspaceUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
63                 this, SLOT(bottomspace_changed()));
64         connect(bottomspaceCO, SIGNAL(activated(int)), this, SLOT(bottomspace_changed()));
65         connect(interlinespaceED, SIGNAL(returnPressed()),
66                 this, SLOT(interlinespace_changed()));
67         connect(interlinespaceUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
68                 this, SLOT(interlinespace_changed()));
69         connect(interlinespaceCO, SIGNAL(activated(int)), this, SLOT(interlinespace_changed()));
70         connect(booktabsRB, SIGNAL(clicked(bool)), this, SLOT(booktabsChanged(bool)));
71         connect(borderDefaultRB, SIGNAL(clicked(bool)), this, SLOT(booktabsChanged(bool)));
72         connect(borderSetPB, SIGNAL(clicked()), this, SLOT(borderSet_clicked()));
73         connect(borderUnsetPB, SIGNAL(clicked()), this, SLOT(borderUnset_clicked()));
74         connect(longTabularCB, SIGNAL(toggled(bool)), longtableGB, SLOT(setEnabled(bool)));
75         connect(longTabularCB, SIGNAL(toggled(bool)), newpageCB, SLOT(setEnabled(bool)));
76         connect(hAlignCB, SIGNAL(activated(int)), this, SLOT(hAlign_changed(int)));
77         connect(vAlignCB, SIGNAL(activated(int)), this, SLOT(vAlign_changed(int)));
78         connect(multicolumnCB, SIGNAL(clicked()), this, SLOT(multicolumn_clicked()));
79         connect(newpageCB, SIGNAL(clicked()), this, SLOT(ltNewpage_clicked()));
80         connect(headerStatusCB, SIGNAL(clicked()), this, SLOT(ltHeaderStatus_clicked()));
81         connect(headerBorderAboveCB, SIGNAL(clicked()), this, SLOT(ltHeaderBorderAbove_clicked()));
82         connect(headerBorderBelowCB, SIGNAL(clicked()), this, SLOT(ltHeaderBorderBelow_clicked()));
83         connect(firstheaderStatusCB, SIGNAL(clicked()), this, SLOT(ltFirstHeaderStatus_clicked()));
84         connect(firstheaderBorderAboveCB, SIGNAL(clicked()), this, SLOT(ltFirstHeaderBorderAbove_clicked()));
85         connect(firstheaderBorderBelowCB, SIGNAL(clicked()), this, SLOT(ltFirstHeaderBorderBelow_clicked()));
86         connect(firstheaderNoContentsCB, SIGNAL(clicked()), this, SLOT(ltFirstHeaderEmpty_clicked()));
87         connect(footerStatusCB, SIGNAL(clicked()), this, SLOT(ltFooterStatus_clicked()));
88         connect(footerBorderAboveCB, SIGNAL(clicked()), this, SLOT(ltFooterBorderAbove_clicked()));
89         connect(footerBorderBelowCB, SIGNAL(clicked()), this, SLOT(ltFooterBorderBelow_clicked()));
90         connect(lastfooterStatusCB, SIGNAL(clicked()), this, SLOT(ltLastFooterStatus_clicked()));
91         connect(lastfooterBorderAboveCB, SIGNAL(clicked()), this, SLOT(ltLastFooterBorderAbove_clicked()));
92         connect(lastfooterBorderBelowCB, SIGNAL(clicked()), this, SLOT(ltLastFooterBorderBelow_clicked()));
93         connect(lastfooterNoContentsCB, SIGNAL(clicked()), this, SLOT(ltLastFooterEmpty_clicked()));
94         connect(specialAlignmentED, SIGNAL(returnPressed()), this, SLOT(specialAlignment_changed()));
95         connect(widthED, SIGNAL(editingFinished()), this, SLOT(width_changed()));
96         connect(widthUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)), this, SLOT(width_changed()));
97         connect(closePB, SIGNAL(clicked()), this, SLOT(close_clicked()));
98         connect(borders, SIGNAL(topSet(bool)), this, SLOT(topBorder_changed()));
99         connect(borders, SIGNAL(bottomSet(bool)), this, SLOT(bottomBorder_changed()));
100         connect(borders, SIGNAL(rightSet(bool)), this, SLOT(rightBorder_changed()));
101         connect(borders, SIGNAL(leftSet(bool)), this, SLOT(leftBorder_changed()));
102         connect(rotateTabularCB, SIGNAL(clicked()), this, SLOT(rotateTabular()));
103         connect(rotateCellCB, SIGNAL(clicked()), this, SLOT(rotateCell()));
104         connect(longTabularCB, SIGNAL(clicked()), this, SLOT(longTabular()));
105
106         bc().setPolicy(ButtonPolicy::IgnorantPolicy);
107         
108         bc().addReadOnly(topspaceED);
109         bc().addReadOnly(topspaceUnit);
110         bc().addReadOnly(topspaceCO);
111         bc().addReadOnly(bottomspaceED);
112         bc().addReadOnly(bottomspaceUnit);
113         bc().addReadOnly(bottomspaceCO);
114         bc().addReadOnly(interlinespaceED);
115         bc().addReadOnly(interlinespaceUnit);
116         bc().addReadOnly(interlinespaceCO);
117         bc().addReadOnly(borderDefaultRB);
118         bc().addReadOnly(booktabsRB);
119
120         bc().addReadOnly(multicolumnCB);
121         bc().addReadOnly(rotateCellCB);
122         bc().addReadOnly(rotateTabularCB);
123         bc().addReadOnly(specialAlignmentED);
124         bc().addReadOnly(widthED);
125         bc().addReadOnly(widthUnit);
126         bc().addReadOnly(hAlignCB);
127         bc().addReadOnly(vAlignCB);
128         bc().addReadOnly(borderSetPB);
129         bc().addReadOnly(borderUnsetPB);
130         bc().addReadOnly(borders);
131         bc().addReadOnly(longTabularCB);
132         bc().addReadOnly(headerStatusCB);
133         bc().addReadOnly(headerBorderAboveCB);
134         bc().addReadOnly(headerBorderBelowCB);
135         bc().addReadOnly(firstheaderStatusCB);
136         bc().addReadOnly(firstheaderBorderAboveCB);
137         bc().addReadOnly(firstheaderBorderBelowCB);
138         bc().addReadOnly(firstheaderNoContentsCB);
139         bc().addReadOnly(footerStatusCB);
140         bc().addReadOnly(footerBorderAboveCB);
141         bc().addReadOnly(footerBorderBelowCB);
142         bc().addReadOnly(lastfooterStatusCB);
143         bc().addReadOnly(lastfooterBorderAboveCB);
144         bc().addReadOnly(lastfooterBorderBelowCB);
145         bc().addReadOnly(lastfooterNoContentsCB);
146         bc().addReadOnly(newpageCB);
147
148         // initialize the length validator
149         bc().addCheckedLineEdit(widthED, fixedWidthColLA);
150         bc().addCheckedLineEdit(topspaceED, topspaceLA);
151         bc().addCheckedLineEdit(bottomspaceED, bottomspaceLA);
152         bc().addCheckedLineEdit(interlinespaceED, interlinespaceLA);
153 }
154
155
156 GuiTabular::~GuiTabular()
157 {
158 }
159
160
161 void GuiTabular::change_adaptor()
162 {
163         changed();
164 }
165
166
167 void GuiTabular::booktabsChanged(bool)
168 {
169         changed();
170         booktabs(booktabsRB->isChecked());
171         update_borders();
172 }
173
174
175 void GuiTabular::topspace_changed()
176 {
177         switch (topspaceCO->currentIndex()) {
178                 case 0: {
179                         set(Tabular::SET_TOP_SPACE, "");
180                         topspaceED->setEnabled(false);
181                         topspaceUnit->setEnabled(false);
182                         break;
183                 }
184                 case 1: {
185                         set(Tabular::SET_TOP_SPACE, "default");
186                         topspaceED->setEnabled(false);
187                         topspaceUnit->setEnabled(false);
188                         break;
189                 }
190                 case 2: {
191                         if (!topspaceED->text().isEmpty())
192                                 set(Tabular::SET_TOP_SPACE,
193                                         widgetsToLength(topspaceED, topspaceUnit));
194                         if (!bc().policy().isReadOnly()) {
195                                 topspaceED->setEnabled(true);
196                                 topspaceUnit->setEnabled(true);
197                         }
198                         break;
199                 }
200         }
201         changed();
202 }
203
204
205 void GuiTabular::bottomspace_changed()
206 {
207         switch (bottomspaceCO->currentIndex()) {
208                 case 0: {
209                         set(Tabular::SET_BOTTOM_SPACE, "");
210                                 bottomspaceED->setEnabled(false);
211                                 bottomspaceUnit->setEnabled(false);
212                         break;
213                 }
214                 case 1: {
215                         set(Tabular::SET_BOTTOM_SPACE, "default");
216                         bottomspaceED->setEnabled(false);
217                         bottomspaceUnit->setEnabled(false);
218                         break;
219                 }
220                 case 2: {
221                         if (!bottomspaceED->text().isEmpty())
222                                 set(Tabular::SET_BOTTOM_SPACE,
223                                         widgetsToLength(bottomspaceED, bottomspaceUnit));
224                         if (!bc().policy().isReadOnly()) {
225                                 bottomspaceED->setEnabled(true);
226                                 bottomspaceUnit->setEnabled(true);
227                         }
228                         break;
229                 }
230         }
231         changed();
232 }
233
234
235 void GuiTabular::interlinespace_changed()
236 {
237         switch (interlinespaceCO->currentIndex()) {
238                 case 0: {
239                         set(Tabular::SET_INTERLINE_SPACE, "");
240                                 interlinespaceED->setEnabled(false);
241                                 interlinespaceUnit->setEnabled(false);
242                         break;
243                 }
244                 case 1: {
245                         set(Tabular::SET_INTERLINE_SPACE, "default");
246                         interlinespaceED->setEnabled(false);
247                         interlinespaceUnit->setEnabled(false);
248                         break;
249                 }
250                 case 2: {
251                         if (!interlinespaceED->text().isEmpty())
252                                 set(Tabular::SET_INTERLINE_SPACE,
253                                         widgetsToLength(interlinespaceED, interlinespaceUnit));
254                         if (!bc().policy().isReadOnly()) {
255                                 interlinespaceED->setEnabled(true);
256                                 interlinespaceUnit->setEnabled(true);
257                         }
258                         break;
259                 }
260         }
261         changed();
262 }
263
264
265 void GuiTabular::close_clicked()
266 {
267         closeGUI();
268         slotClose();
269 }
270
271
272 void GuiTabular::borderSet_clicked()
273 {
274         set(Tabular::SET_ALL_LINES);
275         update_borders();
276         changed();
277 }
278
279
280 void GuiTabular::borderUnset_clicked()
281 {
282         set(Tabular::UNSET_ALL_LINES);
283         update_borders();
284         changed();
285 }
286
287
288 void GuiTabular::leftBorder_changed()
289 {
290         set(Tabular::TOGGLE_LINE_LEFT);
291         changed();
292 }
293
294
295 void GuiTabular::rightBorder_changed()
296 {
297         set(Tabular::TOGGLE_LINE_RIGHT);
298         changed();
299 }
300
301
302 void GuiTabular::topBorder_changed()
303 {
304         set(Tabular::TOGGLE_LINE_TOP);
305         changed();
306 }
307
308
309 void GuiTabular::bottomBorder_changed()
310 {
311         set(Tabular::TOGGLE_LINE_BOTTOM);
312         changed();
313 }
314
315
316 void GuiTabular::specialAlignment_changed()
317 {
318         string special = fromqstr(specialAlignmentED->text());
319         setSpecial(special);
320         changed();
321 }
322
323
324 void GuiTabular::width_changed()
325 {
326         changed();
327         string const width = widgetsToLength(widthED, widthUnit);
328         setWidth(width);
329 }
330
331
332 void GuiTabular::multicolumn_clicked()
333 {
334         toggleMultiColumn();
335         changed();
336 }
337
338
339 void GuiTabular::rotateTabular()
340 {
341         rotateTabular(rotateTabularCB->isChecked());
342         changed();
343 }
344
345
346 void GuiTabular::rotateCell()
347 {
348         rotateCell(rotateCellCB->isChecked());
349         changed();
350 }
351
352
353 void GuiTabular::hAlign_changed(int align)
354 {
355         GuiTabular::HALIGN h = GuiTabular::LEFT;
356
357         switch (align) {
358                 case 0: h = GuiTabular::LEFT; break;
359                 case 1: h = GuiTabular::CENTER; break;
360                 case 2: h = GuiTabular::RIGHT; break;
361                 case 3: h = GuiTabular::BLOCK; break;
362         }
363
364         halign(h);
365 }
366
367
368 void GuiTabular::vAlign_changed(int align)
369 {
370         GuiTabular::VALIGN v = GuiTabular::TOP;
371
372         switch (align) {
373                 case 0: v = GuiTabular::TOP; break;
374                 case 1: v = GuiTabular::MIDDLE; break;
375                 case 2: v = GuiTabular::BOTTOM; break;
376         }
377
378         valign(v);
379 }
380
381
382 void GuiTabular::longTabular()
383 {
384         longTabular(longTabularCB->isChecked());
385         changed();
386 }
387
388
389 void GuiTabular::ltNewpage_clicked()
390 {
391         set(Tabular::SET_LTNEWPAGE);
392         changed();
393 }
394
395
396 void GuiTabular::on_captionStatusCB_toggled()
397 {
398         set(Tabular::TOGGLE_LTCAPTION);
399         changed();
400 }
401
402
403 void GuiTabular::ltHeaderStatus_clicked()
404 {
405         bool enable = headerStatusCB->isChecked();
406         if (enable)
407                 set(Tabular::SET_LTHEAD, "");
408         else
409                 set(Tabular::UNSET_LTHEAD, "");
410         headerBorderAboveCB->setEnabled(enable);
411         headerBorderBelowCB->setEnabled(enable);
412         firstheaderNoContentsCB->setEnabled(enable);
413         changed();
414 }
415
416
417 void GuiTabular::ltHeaderBorderAbove_clicked()
418 {
419         if (headerBorderAboveCB->isChecked())
420                 set(Tabular::SET_LTHEAD, "dl_above");
421         else
422                 set(Tabular::UNSET_LTHEAD, "dl_above");
423         changed();
424 }
425
426
427 void GuiTabular::ltHeaderBorderBelow_clicked()
428 {
429         if (headerBorderBelowCB->isChecked())
430                 set(Tabular::SET_LTHEAD, "dl_below");
431         else
432                 set(Tabular::UNSET_LTHEAD, "dl_below");
433         changed();
434 }
435
436
437 void GuiTabular::ltFirstHeaderBorderAbove_clicked()
438 {
439         if (firstheaderBorderAboveCB->isChecked())
440                 set(Tabular::SET_LTFIRSTHEAD, "dl_above");
441         else
442                 set(Tabular::UNSET_LTFIRSTHEAD, "dl_above");
443         changed();
444 }
445
446
447 void GuiTabular::ltFirstHeaderBorderBelow_clicked()
448 {
449         if (firstheaderBorderBelowCB->isChecked())
450                 set(Tabular::SET_LTFIRSTHEAD, "dl_below");
451         else
452                 set(Tabular::UNSET_LTFIRSTHEAD, "dl_below");
453         changed();
454 }
455
456
457 void GuiTabular::ltFirstHeaderStatus_clicked()
458 {
459         bool enable = firstheaderStatusCB->isChecked();
460         if (enable)
461                 set(Tabular::SET_LTFIRSTHEAD, "");
462         else
463                 set(Tabular::UNSET_LTFIRSTHEAD, "");
464         firstheaderBorderAboveCB->setEnabled(enable);
465         firstheaderBorderBelowCB->setEnabled(enable);
466         changed();
467 }
468
469
470 void GuiTabular::ltFirstHeaderEmpty_clicked()
471 {
472         bool enable = firstheaderNoContentsCB->isChecked();
473         if (enable)
474                 set(Tabular::SET_LTFIRSTHEAD, "empty");
475         else
476                 set(Tabular::UNSET_LTFIRSTHEAD, "empty");
477         firstheaderStatusCB->setEnabled(!enable);
478         firstheaderBorderAboveCB->setEnabled(!enable);
479         firstheaderBorderBelowCB->setEnabled(!enable);
480         changed();
481 }
482
483
484 void GuiTabular::ltFooterStatus_clicked()
485 {
486         bool enable = footerStatusCB->isChecked();
487         if (enable)
488                 set(Tabular::SET_LTFOOT, "");
489         else
490                 set(Tabular::UNSET_LTFOOT, "");
491         footerBorderAboveCB->setEnabled(enable);
492         footerBorderBelowCB->setEnabled(enable);
493         lastfooterNoContentsCB->setEnabled(enable);
494         changed();
495 }
496
497
498 void GuiTabular::ltFooterBorderAbove_clicked()
499 {
500         if (footerBorderAboveCB->isChecked())
501                 set(Tabular::SET_LTFOOT, "dl_above");
502         else
503                 set(Tabular::UNSET_LTFOOT, "dl_above");
504         changed();
505 }
506
507
508 void GuiTabular::ltFooterBorderBelow_clicked()
509 {
510         if (footerBorderBelowCB->isChecked())
511                 set(Tabular::SET_LTFOOT, "dl_below");
512         else
513                 set(Tabular::UNSET_LTFOOT, "dl_below");
514         changed();
515 }
516
517
518 void GuiTabular::ltLastFooterStatus_clicked()
519 {
520         bool enable = lastfooterStatusCB->isChecked();
521         if (enable)
522                 set(Tabular::SET_LTLASTFOOT, "");
523         else
524                 set(Tabular::UNSET_LTLASTFOOT, "");
525         lastfooterBorderAboveCB->setEnabled(enable);
526         lastfooterBorderBelowCB->setEnabled(enable);
527         changed();
528 }
529
530
531 void GuiTabular::ltLastFooterBorderAbove_clicked()
532 {
533         if (lastfooterBorderAboveCB->isChecked())
534                 set(Tabular::SET_LTLASTFOOT, "dl_above");
535         else
536                 set(Tabular::UNSET_LTLASTFOOT, "dl_above");
537         changed();
538 }
539
540
541 void GuiTabular::ltLastFooterBorderBelow_clicked()
542 {
543         if (lastfooterBorderBelowCB->isChecked())
544                 set(Tabular::SET_LTLASTFOOT, "dl_below");
545         else
546                 set(Tabular::UNSET_LTLASTFOOT, "dl_below");
547         changed();
548 }
549
550
551 void GuiTabular::ltLastFooterEmpty_clicked()
552 {
553         bool enable = lastfooterNoContentsCB->isChecked();
554         if (enable)
555                 set(Tabular::SET_LTLASTFOOT, "empty");
556         else
557                 set(Tabular::UNSET_LTLASTFOOT, "empty");
558         lastfooterStatusCB->setEnabled(!enable);
559         lastfooterBorderAboveCB->setEnabled(!enable);
560         lastfooterBorderBelowCB->setEnabled(!enable);
561         changed();
562 }
563
564
565 void GuiTabular::update_borders()
566 {
567         Tabular::idx_type const cell = getActiveCell();
568         borders->setTop(tabular_.topLine(cell));
569         borders->setBottom(tabular_.bottomLine(cell));
570         borders->setLeft(tabular_.leftLine(cell));
571         borders->setRight(tabular_.rightLine(cell));
572         // repaint the setborder widget
573         borders->update();
574 }
575
576
577 namespace {
578
579 Length getColumnPWidth(Tabular const & t, size_t cell)
580 {
581         return t.column_info[t.cellColumn(cell)].p_width;
582 }
583
584
585 Length getMColumnPWidth(Tabular const & t, size_t cell)
586 {
587         if (t.isMultiColumn(cell))
588                 return t.cellInfo(cell).p_width;
589         return Length();
590 }
591
592
593 docstring getAlignSpecial(Tabular const & t, size_t cell, int what)
594 {
595         if (what == Tabular::SET_SPECIAL_MULTI)
596                 return t.cellInfo(cell).align_special;
597         return t.column_info[t.cellColumn(cell)].align_special;
598 }
599
600 }
601
602
603
604 void GuiTabular::updateContents()
605 {
606         initialiseParams(string());
607
608         size_t const cell = getActiveCell();
609
610         Tabular::row_type const row = tabular_.cellRow(cell);
611         Tabular::col_type const col = tabular_.cellColumn(cell);
612
613         tabularRowED->setText(QString::number(row + 1));
614         tabularColumnED->setText(QString::number(col + 1));
615
616         bool const multicol(tabular_.isMultiColumn(cell));
617
618         multicolumnCB->setChecked(multicol);
619
620         rotateCellCB->setChecked(tabular_.getRotateCell(cell));
621         rotateTabularCB->setChecked(tabular_.rotate);
622
623         longTabularCB->setChecked(tabular_.is_long_tabular);
624
625         update_borders();
626
627         Length pwidth;
628         docstring special;
629
630         if (multicol) {
631                 special = getAlignSpecial(tabular_, cell, Tabular::SET_SPECIAL_MULTI);
632                 pwidth = getMColumnPWidth(tabular_, cell);
633         } else {
634                 special = getAlignSpecial(tabular_, cell, Tabular::SET_SPECIAL_COLUMN);
635                 pwidth = getColumnPWidth(tabular_, cell);
636         }
637
638         specialAlignmentED->setText(toqstr(special));
639
640         bool const isReadonly = bc().policy().isReadOnly();
641         specialAlignmentED->setEnabled(!isReadonly);
642
643         Length::UNIT const default_unit = Length::defaultUnit();
644
645         borderDefaultRB->setChecked(!tabular_.use_booktabs);
646         booktabsRB->setChecked(tabular_.use_booktabs);
647
648         if (tabular_.row_info[row].top_space.empty()
649             && !tabular_.row_info[row].top_space_default) {
650                 topspaceCO->setCurrentIndex(0);
651         } else if (tabular_.row_info[row].top_space_default) {
652                 topspaceCO->setCurrentIndex(1);
653         } else {
654                 topspaceCO->setCurrentIndex(2);
655                 lengthToWidgets(topspaceED,
656                                 topspaceUnit,
657                                 tabular_.row_info[row].top_space.asString(),
658                                 default_unit);
659         }
660         topspaceED->setEnabled(!isReadonly
661                 && (topspaceCO->currentIndex() == 2));
662         topspaceUnit->setEnabled(!isReadonly
663                 && (topspaceCO->currentIndex() == 2));
664         topspaceCO->setEnabled(!isReadonly);
665
666         if (tabular_.row_info[row].bottom_space.empty()
667             && !tabular_.row_info[row].bottom_space_default) {
668                 bottomspaceCO->setCurrentIndex(0);
669         } else if (tabular_.row_info[row].bottom_space_default) {
670                 bottomspaceCO->setCurrentIndex(1);
671         } else {
672                 bottomspaceCO->setCurrentIndex(2);
673                 lengthToWidgets(bottomspaceED,
674                                 bottomspaceUnit,
675                                 tabular_.row_info[row].bottom_space.asString(),
676                                 default_unit);
677         }
678         bottomspaceED->setEnabled(!isReadonly
679                 && (bottomspaceCO->currentIndex() == 2));
680         bottomspaceUnit->setEnabled(!isReadonly
681                 && (bottomspaceCO->currentIndex() == 2));
682         bottomspaceCO->setEnabled(!isReadonly);
683
684         if (tabular_.row_info[row].interline_space.empty()
685             && !tabular_.row_info[row].interline_space_default) {
686                 interlinespaceCO->setCurrentIndex(0);
687         } else if (tabular_.row_info[row].interline_space_default) {
688                 interlinespaceCO->setCurrentIndex(1);
689         } else {
690                 interlinespaceCO->setCurrentIndex(2);
691                 lengthToWidgets(interlinespaceED,
692                                 interlinespaceUnit,
693                                 tabular_.row_info[row].interline_space.asString(),
694                                 default_unit);
695         }
696         interlinespaceED->setEnabled(!isReadonly
697                 && (interlinespaceCO->currentIndex() == 2));
698         interlinespaceUnit->setEnabled(!isReadonly
699                 && (interlinespaceCO->currentIndex() == 2));
700         interlinespaceCO->setEnabled(!isReadonly);
701
702         string colwidth;
703         if (!pwidth.zero())
704                 colwidth = pwidth.asString();
705         lengthToWidgets(widthED, widthUnit,
706                 colwidth, default_unit);
707
708         widthED->setEnabled(!isReadonly);
709         widthUnit->setEnabled(!isReadonly);
710
711         hAlignCB->clear();
712         hAlignCB->addItem(qt_("Left"));
713         hAlignCB->addItem(qt_("Center"));
714         hAlignCB->addItem(qt_("Right"));
715         if (!multicol && !pwidth.zero())
716                 hAlignCB->addItem(qt_("Justified"));
717
718         int align = 0;
719         switch (tabular_.getAlignment(cell)) {
720         case LYX_ALIGN_LEFT:
721                 align = 0;
722                 break;
723         case LYX_ALIGN_CENTER:
724                 align = 1;
725                 break;
726         case LYX_ALIGN_RIGHT:
727                 align = 2;
728                 break;
729         case LYX_ALIGN_BLOCK:
730         {
731                 if (!multicol && !pwidth.zero())
732                         align = 3;
733                 break;
734         }
735         default:
736                 align = 0;
737                 break;
738         }
739         hAlignCB->setCurrentIndex(align);
740
741         int valign = 0;
742         switch (tabular_.getVAlignment(cell)) {
743         case Tabular::LYX_VALIGN_TOP:
744                 valign = 0;
745                 break;
746         case Tabular::LYX_VALIGN_MIDDLE:
747                 valign = 1;
748                 break;
749         case Tabular::LYX_VALIGN_BOTTOM:
750                 valign = 2;
751                 break;
752         default:
753                 valign = 0;
754                 break;
755         }
756         if (pwidth.zero())
757                 valign = 0;
758         vAlignCB->setCurrentIndex(valign);
759
760         hAlignCB->setEnabled(true);
761         vAlignCB->setEnabled(!pwidth.zero());
762
763         if (!tabular_.is_long_tabular) {
764                 headerStatusCB->setChecked(false);
765                 headerBorderAboveCB->setChecked(false);
766                 headerBorderBelowCB->setChecked(false);
767                 firstheaderStatusCB->setChecked(false);
768                 firstheaderBorderAboveCB->setChecked(false);
769                 firstheaderBorderBelowCB->setChecked(false);
770                 firstheaderNoContentsCB->setChecked(false);
771                 footerStatusCB->setChecked(false);
772                 footerBorderAboveCB->setChecked(false);
773                 footerBorderBelowCB->setChecked(false);
774                 lastfooterStatusCB->setChecked(false);
775                 lastfooterBorderAboveCB->setChecked(false);
776                 lastfooterBorderBelowCB->setChecked(false);
777                 lastfooterNoContentsCB->setChecked(false);
778                 newpageCB->setChecked(false);
779                 newpageCB->setEnabled(false);
780                 captionStatusCB->blockSignals(true);
781                 captionStatusCB->setChecked(false);
782                 captionStatusCB->blockSignals(false);
783                 return;
784         }
785         captionStatusCB->blockSignals(true);
786         captionStatusCB->setChecked(tabular_.ltCaption(row));
787         captionStatusCB->blockSignals(false);
788
789         // FIXME: shouldn't this be handled by GuiDialog?
790         // FIXME: Some of them should be handled directly in TabularUI.ui
791         firstheaderBorderAboveCB->setEnabled(funcEnabled(Tabular::SET_LTFIRSTHEAD));
792         firstheaderBorderBelowCB->setEnabled(funcEnabled(Tabular::SET_LTFIRSTHEAD));
793         // first header can only be suppressed when there is a header
794         firstheaderNoContentsCB->setEnabled(tabular_.haveLTHead()
795                 && !tabular_.haveLTFirstHead());
796         // check if setting a first header is allowed
797         // additionally check firstheaderStatusCB because when this is the case
798         // a first header makes no sense
799         firstheaderStatusCB->setEnabled(funcEnabled(Tabular::SET_LTFIRSTHEAD)
800                 && !firstheaderNoContentsCB->isChecked());
801         //firstheaderStatusCB->setEnabled(!firstheaderNoContentsCB->isChecked());
802         headerBorderAboveCB->setEnabled(funcEnabled(Tabular::SET_LTHEAD));
803         headerBorderBelowCB->setEnabled(funcEnabled(Tabular::SET_LTHEAD));
804         headerStatusCB->setEnabled(funcEnabled(Tabular::SET_LTHEAD));
805         footerBorderAboveCB->setEnabled(funcEnabled(Tabular::SET_LTFOOT));
806         footerBorderBelowCB->setEnabled(funcEnabled(Tabular::SET_LTFOOT));
807         footerStatusCB->setEnabled(funcEnabled(Tabular::SET_LTFOOT));
808         lastfooterBorderAboveCB->setEnabled(funcEnabled(Tabular::SET_LTLASTFOOT));
809         lastfooterBorderBelowCB->setEnabled(funcEnabled(Tabular::SET_LTLASTFOOT));
810         // last footer can only be suppressed when there is a footer
811         lastfooterNoContentsCB->setEnabled(tabular_.haveLTFoot()
812                 && !tabular_.haveLTLastFoot());
813         // check if setting a last footer is allowed
814         // additionally check lastfooterNoContentsCB because when this is the case
815         // a last footer makes no sense
816         lastfooterStatusCB->setEnabled(funcEnabled(Tabular::SET_LTLASTFOOT)
817                 && !lastfooterNoContentsCB->isChecked());
818         captionStatusCB->setEnabled(funcEnabled(Tabular::TOGGLE_LTCAPTION));
819         // When a row is set as longtable caption, it must not be allowed to unset
820         // that this row is a multicolumn.
821         multicolumnCB->setEnabled(funcEnabled(Tabular::MULTICOLUMN));
822
823         Tabular::ltType ltt;
824         bool use_empty;
825         bool row_set = tabular_.getRowOfLTHead(row, ltt);
826         headerStatusCB->setChecked(row_set);
827         if (ltt.set) {
828                 headerBorderAboveCB->setChecked(ltt.topDL);
829                 headerBorderBelowCB->setChecked(ltt.bottomDL);
830                 use_empty = true;
831         } else {
832                 headerBorderAboveCB->setChecked(false);
833                 headerBorderBelowCB->setChecked(false);
834                 headerBorderAboveCB->setEnabled(false);
835                 headerBorderBelowCB->setEnabled(false);
836                 firstheaderNoContentsCB->setChecked(false);
837                 firstheaderNoContentsCB->setEnabled(false);
838                 use_empty = false;
839         }
840
841         row_set = tabular_.getRowOfLTFirstHead(row, ltt);
842         firstheaderStatusCB->setChecked(row_set);
843         if (ltt.set && (!ltt.empty || !use_empty)) {
844                 firstheaderBorderAboveCB->setChecked(ltt.topDL);
845                 firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
846         } else {
847                 firstheaderBorderAboveCB->setEnabled(false);
848                 firstheaderBorderBelowCB->setEnabled(false);
849                 firstheaderBorderAboveCB->setChecked(false);
850                 firstheaderBorderBelowCB->setChecked(false);
851                 if (use_empty) {
852                         if (ltt.empty)
853                                 firstheaderStatusCB->setEnabled(false);
854                 }
855         }
856
857         row_set = tabular_.getRowOfLTFoot(row, ltt);
858         footerStatusCB->setChecked(row_set);
859         if (ltt.set) {
860                 footerBorderAboveCB->setChecked(ltt.topDL);
861                 footerBorderBelowCB->setChecked(ltt.bottomDL);
862                 use_empty = true;
863         } else {
864                 footerBorderAboveCB->setChecked(false);
865                 footerBorderBelowCB->setChecked(false);
866                 footerBorderAboveCB->setEnabled(false);
867                 footerBorderBelowCB->setEnabled(false);
868                 lastfooterNoContentsCB->setChecked(false);
869                 lastfooterNoContentsCB->setEnabled(false);
870                 use_empty = false;
871         }
872
873         row_set = tabular_.getRowOfLTLastFoot(row, ltt);
874         lastfooterStatusCB->setChecked(row_set);
875         if (ltt.set && (!ltt.empty || !use_empty)) {
876                 lastfooterBorderAboveCB->setChecked(ltt.topDL);
877                 lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
878         } else {
879                 lastfooterBorderAboveCB->setEnabled(false);
880                 lastfooterBorderBelowCB->setEnabled(false);
881                 lastfooterBorderAboveCB->setChecked(false);
882                 lastfooterBorderBelowCB->setChecked(false);
883                 if (use_empty) {
884                         if (ltt.empty)
885                                 lastfooterStatusCB->setEnabled(false);
886                 }
887         }
888         newpageCB->setChecked(tabular_.getLTNewPage(row));
889 }
890
891
892 void GuiTabular::closeGUI()
893 {
894         // ugly hack to auto-apply the stuff that hasn't been
895         // yet. don't let this continue to exist ...
896
897         // Subtle here, we must /not/ apply any changes and
898         // then refer to tabular, as it will have been freed
899         // since the changes update the actual tabular_
900         //
901         // apply the fixed width values
902         size_t const cell = getActiveCell();
903         bool const multicol = tabular_.isMultiColumn(cell);
904         string width = widgetsToLength(widthED, widthUnit);
905         string width2;
906
907         Length llen = getColumnPWidth(tabular_, cell);
908         Length llenMulti = getMColumnPWidth(tabular_, cell);
909
910         if (multicol && !llenMulti.zero())
911                 width2 = llenMulti.asString();
912         else if (!multicol && !llen.zero())
913                 width2 = llen.asString();
914
915         // apply the special alignment
916         docstring const sa1 = qstring_to_ucs4(specialAlignmentED->text());
917         docstring sa2;
918
919         if (multicol)
920                 sa2 = getAlignSpecial(tabular_, cell, Tabular::SET_SPECIAL_MULTI);
921         else
922                 sa2 = getAlignSpecial(tabular_, cell, Tabular::SET_SPECIAL_COLUMN);
923
924         if (sa1 != sa2) {
925                 if (multicol)
926                         set(Tabular::SET_SPECIAL_MULTI, to_utf8(sa1));
927                 else
928                         set(Tabular::SET_SPECIAL_COLUMN, to_utf8(sa1));
929         }
930
931         if (width != width2) {
932                 if (multicol)
933                         set(Tabular::SET_MPWIDTH, width);
934                 else
935                         set(Tabular::SET_PWIDTH, width);
936         }
937
938         /* DO WE NEED THIS?
939         switch (topspaceCO->currentIndex()) {
940                 case 0:
941                         set(Tabular::SET_TOP_SPACE, "");
942                         break;
943                 case 1:
944                         set(Tabular::SET_TOP_SPACE, "default");
945                         break;
946                 case 2:
947                         set(Tabular::SET_TOP_SPACE,
948                                 widgetsToLength(topspaceED, topspaceUnit));
949                         break;
950         }
951
952         switch (bottomspaceCO->currentIndex()) {
953                 case 0:
954                         set(Tabular::SET_BOTTOM_SPACE, "");
955                         break;
956                 case 1:
957                         set(Tabular::SET_BOTTOM_SPACE, "default");
958                         break;
959                 case 2:
960                         set(Tabular::SET_BOTTOM_SPACE,
961                                 widgetsToLength(bottomspaceED, bottomspaceUnit));
962                         break;
963         }
964
965         switch (interlinespaceCO->currentIndex()) {
966                 case 0:
967                         set(Tabular::SET_INTERLINE_SPACE, "");
968                         break;
969                 case 1:
970                         set(Tabular::SET_INTERLINE_SPACE, "default");
971                         break;
972                 case 2:
973                         set(Tabular::SET_INTERLINE_SPACE,
974                                 widgetsToLength(interlinespaceED, interlinespaceUnit));
975                         break;
976         }
977 */
978 }
979
980
981 bool GuiTabular::initialiseParams(string const & data)
982 {
983         // try to get the current cell
984         BufferView const * const bv = bufferview();
985         InsetTabular const * current_inset = 0;
986         if (bv) {
987                 Cursor const & cur = bv->cursor();
988                 // get the innermost tabular inset;
989                 // assume that it is "ours"
990                 for (int i = cur.depth() - 1; i >= 0; --i)
991                         if (cur[i].inset().lyxCode() == TABULAR_CODE) {
992                                 current_inset = static_cast<InsetTabular const *>(&cur[i].inset());
993                                 active_cell_ = cur[i].idx();
994                                 break;
995                         }
996         }
997
998         if (current_inset && data.empty()) {
999                 tabular_ = Tabular(current_inset->tabular);
1000                 return true;
1001         }
1002
1003         InsetTabular tmp(const_cast<Buffer &>(buffer()));
1004         InsetTabular::string2params(data, tmp);
1005         tabular_ = Tabular(tmp.tabular);
1006         return true;
1007 }
1008
1009
1010 void GuiTabular::clearParams()
1011 {
1012         // This function is also called when LyX is closing and the dialog is
1013         // still open. At that time, the buffer might not be available anymore.
1014         if (isBufferAvailable()) {
1015                 InsetTabular tmp(const_cast<Buffer &>(buffer()));
1016                 tabular_ = tmp.tabular;
1017         }
1018         active_cell_ = Tabular::npos;
1019 }
1020
1021
1022 Tabular::idx_type GuiTabular::getActiveCell() const
1023 {
1024         return active_cell_;
1025 }
1026
1027
1028 void GuiTabular::set(Tabular::Feature f, string const & arg)
1029 {
1030         string const data = featureAsString(f) + ' ' + arg;
1031         dispatch(FuncRequest(getLfun(), data));
1032 }
1033
1034
1035 void GuiTabular::setSpecial(string const & special)
1036 {
1037         if (tabular_.isMultiColumn(getActiveCell()))
1038                 set(Tabular::SET_SPECIAL_MULTI, special);
1039         else
1040                 set(Tabular::SET_SPECIAL_COLUMN, special);
1041 }
1042
1043
1044 void GuiTabular::setWidth(string const & width)
1045 {
1046         if (tabular_.isMultiColumn(getActiveCell()))
1047                 set(Tabular::SET_MPWIDTH, width);
1048         else
1049                 set(Tabular::SET_PWIDTH, width);
1050
1051         updateView();
1052 }
1053
1054
1055 void GuiTabular::toggleMultiColumn()
1056 {
1057         set(Tabular::MULTICOLUMN);
1058         updateView();
1059 }
1060
1061
1062 void GuiTabular::rotateTabular(bool yes)
1063 {
1064         if (yes)
1065                 set(Tabular::SET_ROTATE_TABULAR);
1066         else
1067                 set(Tabular::UNSET_ROTATE_TABULAR);
1068 }
1069
1070
1071 void GuiTabular::rotateCell(bool yes)
1072 {
1073         if (yes)
1074                 set(Tabular::SET_ROTATE_CELL);
1075         else
1076                 set(Tabular::UNSET_ROTATE_CELL);
1077 }
1078
1079
1080 void GuiTabular::halign(GuiTabular::HALIGN h)
1081 {
1082         Tabular::Feature num = Tabular::ALIGN_LEFT;
1083         Tabular::Feature multi_num = Tabular::M_ALIGN_LEFT;
1084
1085         switch (h) {
1086                 case LEFT:
1087                         num = Tabular::ALIGN_LEFT;
1088                         multi_num = Tabular::M_ALIGN_LEFT;
1089                         break;
1090                 case CENTER:
1091                         num = Tabular::ALIGN_CENTER;
1092                         multi_num = Tabular::M_ALIGN_CENTER;
1093                         break;
1094                 case RIGHT:
1095                         num = Tabular::ALIGN_RIGHT;
1096                         multi_num = Tabular::M_ALIGN_RIGHT;
1097                         break;
1098                 case BLOCK:
1099                         num = Tabular::ALIGN_BLOCK;
1100                         //multi_num: no equivalent
1101                         break;
1102         }
1103
1104         if (tabular_.isMultiColumn(getActiveCell()))
1105                 set(multi_num);
1106         else
1107                 set(num);
1108 }
1109
1110
1111 void GuiTabular::valign(GuiTabular::VALIGN v)
1112 {
1113         Tabular::Feature num = Tabular::VALIGN_MIDDLE;
1114         Tabular::Feature multi_num = Tabular::M_VALIGN_MIDDLE;
1115
1116         switch (v) {
1117                 case TOP:
1118                         num = Tabular::VALIGN_TOP;
1119                         multi_num = Tabular::M_VALIGN_TOP;
1120                         break;
1121                 case MIDDLE:
1122                         num = Tabular::VALIGN_MIDDLE;
1123                         multi_num = Tabular::M_VALIGN_MIDDLE;
1124                         break;
1125                 case BOTTOM:
1126                         num = Tabular::VALIGN_BOTTOM;
1127                         multi_num = Tabular::M_VALIGN_BOTTOM;
1128                         break;
1129         }
1130
1131         if (tabular_.isMultiColumn(getActiveCell()))
1132                 set(multi_num);
1133         else
1134                 set(num);
1135 }
1136
1137
1138 void GuiTabular::booktabs(bool yes)
1139 {
1140         if (yes)
1141                 set(Tabular::SET_BOOKTABS);
1142         else
1143                 set(Tabular::UNSET_BOOKTABS);
1144 }
1145
1146
1147 void GuiTabular::longTabular(bool yes)
1148 {
1149         if (yes)
1150                 set(Tabular::SET_LONGTABULAR);
1151         else
1152                 set(Tabular::UNSET_LONGTABULAR);
1153 }
1154
1155
1156 // to get the status of the longtable row settings
1157 bool GuiTabular::funcEnabled(Tabular::Feature f) const
1158 {
1159         return getStatus(FuncRequest(getLfun(), featureAsString(f))).enabled();
1160 }
1161
1162
1163 Dialog * createGuiTabular(GuiView & lv) { return new GuiTabular(lv); }
1164
1165
1166 } // namespace frontend
1167 } // namespace lyx
1168
1169 #include "moc_GuiTabular.cpp"