]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiTabular.cpp
next one
[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 "BufferView.h"
18 #include "Cursor.h"
19 #include "FuncRequest.h"
20 #include "GuiSetBorder.h"
21 #include "LengthCombo.h"
22 #include "LyXRC.h"
23 #include "qt_helpers.h"
24 #include "Validator.h"
25
26 #include "insets/InsetTabular.h"
27
28 #include <QCloseEvent>
29 #include <QCheckBox>
30 #include <QPushButton>
31 #include <QRadioButton>
32 #include <QLineEdit>
33
34 using std::string;
35
36
37 namespace lyx {
38 namespace frontend {
39
40 GuiTabular::GuiTabular(LyXView & lv)
41         : GuiDialog(lv, "tabular"), Controller(this)
42 {
43         active_cell_ = Tabular::npos;
44
45         setupUi(this);
46         setViewTitle(_("Table Settings"));
47         setController(this, false);
48
49         widthED->setValidator(unsignedLengthValidator(widthED));
50         topspaceED->setValidator(new LengthValidator(topspaceED));
51         bottomspaceED->setValidator(new LengthValidator(bottomspaceED));
52         interlinespaceED->setValidator(new LengthValidator(interlinespaceED));
53
54         connect(topspaceED, SIGNAL(returnPressed()),
55                 this, SLOT(topspace_changed()));
56         connect(topspaceUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
57                 this, SLOT(topspace_changed()));
58         connect(topspaceCO, SIGNAL(activated(int)), this, SLOT(topspace_changed()));
59         connect(bottomspaceED, SIGNAL(returnPressed()),
60                 this, SLOT(bottomspace_changed()));
61         connect(bottomspaceUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
62                 this, SLOT(bottomspace_changed()));
63         connect(bottomspaceCO, SIGNAL(activated(int)), this, SLOT(bottomspace_changed()));
64         connect(interlinespaceED, SIGNAL(returnPressed()),
65                 this, SLOT(interlinespace_changed()));
66         connect(interlinespaceUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)),
67                 this, SLOT(interlinespace_changed()));
68         connect(interlinespaceCO, SIGNAL(activated(int)), this, SLOT(interlinespace_changed()));
69         connect(booktabsRB, SIGNAL(clicked(bool)), this, SLOT(booktabsChanged(bool)));
70         connect(borderDefaultRB, SIGNAL(clicked(bool)), this, SLOT(booktabsChanged(bool)));
71         connect(borderSetPB, SIGNAL(clicked()), this, SLOT(borderSet_clicked()));
72         connect(borderUnsetPB, SIGNAL(clicked()), this, SLOT(borderUnset_clicked()));
73         connect(longTabularCB, SIGNAL(toggled(bool)), longtableGB, SLOT(setEnabled(bool)));
74         connect(longTabularCB, SIGNAL(toggled(bool)), newpageCB, SLOT(setEnabled(bool)));
75         connect(hAlignCB, SIGNAL(activated(int)), this, SLOT(hAlign_changed(int)));
76         connect(vAlignCB, SIGNAL(activated(int)), this, SLOT(vAlign_changed(int)));
77         connect(multicolumnCB, SIGNAL(clicked()), this, SLOT(multicolumn_clicked()));
78         connect(newpageCB, SIGNAL(clicked()), this, SLOT(ltNewpage_clicked()));
79         connect(headerStatusCB, SIGNAL(clicked()), this, SLOT(ltHeaderStatus_clicked()));
80         connect(headerBorderAboveCB, SIGNAL(clicked()), this, SLOT(ltHeaderBorderAbove_clicked()));
81         connect(headerBorderBelowCB, SIGNAL(clicked()), this, SLOT(ltHeaderBorderBelow_clicked()));
82         connect(firstheaderStatusCB, SIGNAL(clicked()), this, SLOT(ltFirstHeaderStatus_clicked()));
83         connect(firstheaderBorderAboveCB, SIGNAL(clicked()), this, SLOT(ltFirstHeaderBorderAbove_clicked()));
84         connect(firstheaderBorderBelowCB, SIGNAL(clicked()), this, SLOT(ltFirstHeaderBorderBelow_clicked()));
85         connect(firstheaderNoContentsCB, SIGNAL(clicked()), this, SLOT(ltFirstHeaderEmpty_clicked()));
86         connect(footerStatusCB, SIGNAL(clicked()), this, SLOT(ltFooterStatus_clicked()));
87         connect(footerBorderAboveCB, SIGNAL(clicked()), this, SLOT(ltFooterBorderAbove_clicked()));
88         connect(footerBorderBelowCB, SIGNAL(clicked()), this, SLOT(ltFooterBorderBelow_clicked()));
89         connect(lastfooterStatusCB, SIGNAL(clicked()), this, SLOT(ltLastFooterStatus_clicked()));
90         connect(lastfooterBorderAboveCB, SIGNAL(clicked()), this, SLOT(ltLastFooterBorderAbove_clicked()));
91         connect(lastfooterBorderBelowCB, SIGNAL(clicked()), this, SLOT(ltLastFooterBorderBelow_clicked()));
92         connect(lastfooterNoContentsCB, SIGNAL(clicked()), this, SLOT(ltLastFooterEmpty_clicked()));
93         connect(specialAlignmentED, SIGNAL(returnPressed()), this, SLOT(specialAlignment_changed()));
94         connect(widthED, SIGNAL(returnPressed()), this, SLOT(width_changed()));
95         connect(widthUnit, SIGNAL(selectionChanged(lyx::Length::UNIT)), this, SLOT(width_changed()));
96         connect(closePB, SIGNAL(clicked()), this, SLOT(close_clicked()));
97         connect(borders, SIGNAL(topSet(bool)), this, SLOT(topBorder_changed()));
98         connect(borders, SIGNAL(bottomSet(bool)), this, SLOT(bottomBorder_changed()));
99         connect(borders, SIGNAL(rightSet(bool)), this, SLOT(rightBorder_changed()));
100         connect(borders, SIGNAL(leftSet(bool)), this, SLOT(leftBorder_changed()));
101         connect(rotateTabularCB, SIGNAL(clicked()), this, SLOT(rotateTabular()));
102         connect(rotateCellCB, SIGNAL(clicked()), this, SLOT(rotateCell()));
103         connect(longTabularCB, SIGNAL(clicked()), this, SLOT(longTabular()));
104
105         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
106         bc().setCancel(closePB);
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::closeEvent(QCloseEvent * e)
168 {
169         slotClose();
170         GuiDialog::closeEvent(e);
171 }
172
173
174 void GuiTabular::booktabsChanged(bool)
175 {
176         changed();
177         booktabs(booktabsRB->isChecked());
178         update_borders();
179 }
180
181
182 void GuiTabular::topspace_changed()
183 {
184         switch (topspaceCO->currentIndex()) {
185                 case 0: {
186                         set(Tabular::SET_TOP_SPACE, "");
187                         topspaceED->setEnabled(false);
188                         topspaceUnit->setEnabled(false);
189                         break;
190                 }
191                 case 1: {
192                         set(Tabular::SET_TOP_SPACE, "default");
193                         topspaceED->setEnabled(false);
194                         topspaceUnit->setEnabled(false);
195                         break;
196                 }
197                 case 2: {
198                         if (!topspaceED->text().isEmpty())
199                                 set(Tabular::SET_TOP_SPACE,
200                                         widgetsToLength(topspaceED, topspaceUnit));
201                         if (!bc().policy().isReadOnly()) {
202                                 topspaceED->setEnabled(true);
203                                 topspaceUnit->setEnabled(true);
204                         }
205                         break;
206                 }
207         }
208         changed();
209 }
210
211
212 void GuiTabular::bottomspace_changed()
213 {
214         switch (bottomspaceCO->currentIndex()) {
215                 case 0: {
216                         set(Tabular::SET_BOTTOM_SPACE, "");
217                                 bottomspaceED->setEnabled(false);
218                                 bottomspaceUnit->setEnabled(false);
219                         break;
220                 }
221                 case 1: {
222                         set(Tabular::SET_BOTTOM_SPACE, "default");
223                         bottomspaceED->setEnabled(false);
224                         bottomspaceUnit->setEnabled(false);
225                         break;
226                 }
227                 case 2: {
228                         if (!bottomspaceED->text().isEmpty())
229                                 set(Tabular::SET_BOTTOM_SPACE,
230                                         widgetsToLength(bottomspaceED, bottomspaceUnit));
231                         if (!bc().policy().isReadOnly()) {
232                                 bottomspaceED->setEnabled(true);
233                                 bottomspaceUnit->setEnabled(true);
234                         }
235                         break;
236                 }
237         }
238         changed();
239 }
240
241
242 void GuiTabular::interlinespace_changed()
243 {
244         switch (interlinespaceCO->currentIndex()) {
245                 case 0: {
246                         set(Tabular::SET_INTERLINE_SPACE, "");
247                                 interlinespaceED->setEnabled(false);
248                                 interlinespaceUnit->setEnabled(false);
249                         break;
250                 }
251                 case 1: {
252                         set(Tabular::SET_INTERLINE_SPACE, "default");
253                         interlinespaceED->setEnabled(false);
254                         interlinespaceUnit->setEnabled(false);
255                         break;
256                 }
257                 case 2: {
258                         if (!interlinespaceED->text().isEmpty())
259                                 set(Tabular::SET_INTERLINE_SPACE,
260                                         widgetsToLength(interlinespaceED, interlinespaceUnit));
261                         if (!bc().policy().isReadOnly()) {
262                                 interlinespaceED->setEnabled(true);
263                                 interlinespaceUnit->setEnabled(true);
264                         }
265                         break;
266                 }
267         }
268         changed();
269 }
270
271
272 void GuiTabular::close_clicked()
273 {
274         closeGUI();
275         slotClose();
276 }
277
278
279 void GuiTabular::borderSet_clicked()
280 {
281         set(Tabular::SET_ALL_LINES);
282         update_borders();
283         changed();
284 }
285
286
287 void GuiTabular::borderUnset_clicked()
288 {
289         set(Tabular::UNSET_ALL_LINES);
290         update_borders();
291         changed();
292 }
293
294
295 void GuiTabular::leftBorder_changed()
296 {
297         toggleLeftLine();
298         changed();
299 }
300
301
302 void GuiTabular::rightBorder_changed()
303 {
304         toggleRightLine();
305         changed();
306 }
307
308
309 void GuiTabular::topBorder_changed()
310 {
311         toggleTopLine();
312         changed();
313 }
314
315
316 void GuiTabular::bottomBorder_changed()
317 {
318         toggleBottomLine();
319         changed();
320 }
321
322
323 void GuiTabular::specialAlignment_changed()
324 {
325         string special = fromqstr(specialAlignmentED->text());
326         setSpecial(special);
327         changed();
328 }
329
330
331 void GuiTabular::width_changed()
332 {
333         changed();
334         string const width = widgetsToLength(widthED, widthUnit);
335         setWidth(width);
336 }
337
338
339 void GuiTabular::multicolumn_clicked()
340 {
341         toggleMultiColumn();
342         changed();
343 }
344
345
346 void GuiTabular::rotateTabular()
347 {
348         rotateTabular(rotateTabularCB->isChecked());
349         changed();
350 }
351
352
353 void GuiTabular::rotateCell()
354 {
355         rotateCell(rotateCellCB->isChecked());
356         changed();
357 }
358
359
360 void GuiTabular::hAlign_changed(int align)
361 {
362         GuiTabular::HALIGN h = GuiTabular::LEFT;
363
364         switch (align) {
365                 case 0: h = GuiTabular::LEFT; break;
366                 case 1: h = GuiTabular::CENTER; break;
367                 case 2: h = GuiTabular::RIGHT; break;
368                 case 3: h = GuiTabular::BLOCK; break;
369         }
370
371         halign(h);
372 }
373
374
375 void GuiTabular::vAlign_changed(int align)
376 {
377         GuiTabular::VALIGN v = GuiTabular::TOP;
378
379         switch (align) {
380                 case 0: v = GuiTabular::TOP; break;
381                 case 1: v = GuiTabular::MIDDLE; break;
382                 case 2: v = GuiTabular::BOTTOM; break;
383         }
384
385         valign(v);
386 }
387
388
389 void GuiTabular::longTabular()
390 {
391         longTabular(longTabularCB->isChecked());
392         changed();
393 }
394
395
396 void GuiTabular::ltNewpage_clicked()
397 {
398         set(Tabular::SET_LTNEWPAGE);
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         bool const isMulticolumnCell = tabular_.isMultiColumn(cell);
569
570         if (!isMulticolumnCell) {
571                 borders->setLeftEnabled(true);
572                 borders->setRightEnabled(true);
573                 borders->setTop(tabular_.topLine(cell, true));
574                 borders->setBottom(tabular_.bottomLine(cell, true));
575                 borders->setLeft(tabular_.leftLine(cell, true));
576                 borders->setRight(tabular_.rightLine(cell, true));
577                 // repaint the setborder widget
578                 borders->update();
579                 return;
580         }
581
582         borders->setTop(tabular_.topLine(cell));
583         borders->setBottom(tabular_.bottomLine(cell));
584         // pay attention to left/right lines: they are only allowed
585         // to set if we are in first/last cell of row or if the left/right
586         // cell is also a multicolumn.
587         if (tabular_.isFirstCellInRow(cell) || tabular_.isMultiColumn(cell - 1)) {
588                 borders->setLeftEnabled(true);
589                 borders->setLeft(tabular_.leftLine(cell));
590         } else {
591                 borders->setLeft(false);
592                 borders->setLeftEnabled(false);
593         }
594         if (tabular_.isLastCellInRow(cell) || tabular_.isMultiColumn(cell + 1)) {
595                 borders->setRightEnabled(true);
596                 borders->setRight(tabular_.rightLine(cell));
597         } else {
598                 borders->setRight(false);
599                 borders->setRightEnabled(false);
600         }
601         // repaint the setborder widget
602         borders->update();
603 }
604
605
606 void GuiTabular::updateContents()
607 {
608         initialiseParams(string());
609
610         Tabular::idx_type const cell = getActiveCell();
611
612         Tabular::row_type const row = tabular_.cellRow(cell);
613         Tabular::col_type const col = tabular_.cellColumn(cell);
614
615         tabularRowED->setText(QString::number(row + 1));
616         tabularColumnED->setText(QString::number(col + 1));
617
618         bool const multicol(tabular_.isMultiColumn(cell));
619
620         multicolumnCB->setChecked(multicol);
621
622         rotateCellCB->setChecked(tabular_.getRotateCell(cell));
623         rotateTabularCB->setChecked(tabular_.getRotateTabular());
624
625         longTabularCB->setChecked(tabular_.isLongTabular());
626
627         update_borders();
628
629         Length pwidth;
630         docstring special;
631
632         if (multicol) {
633                 special = tabular_.getAlignSpecial(cell, Tabular::SET_SPECIAL_MULTI);
634                 pwidth = tabular_.getMColumnPWidth(cell);
635         } else {
636                 special = tabular_.getAlignSpecial(cell, Tabular::SET_SPECIAL_COLUMN);
637                 pwidth = tabular_.getColumnPWidth(cell);
638         }
639
640         specialAlignmentED->setText(toqstr(special));
641
642         bool const isReadonly = bc().policy().isReadOnly();
643         specialAlignmentED->setEnabled(!isReadonly);
644
645         Length::UNIT default_unit =
646                 useMetricUnits() ? Length::CM : Length::IN;
647
648         borderDefaultRB->setChecked(!tabular_.useBookTabs());
649         booktabsRB->setChecked(tabular_.useBookTabs());
650
651         if (tabular_.row_info[row].top_space.empty()
652             && !tabular_.row_info[row].top_space_default) {
653                 topspaceCO->setCurrentIndex(0);
654         } else if (tabular_.row_info[row].top_space_default) {
655                 topspaceCO->setCurrentIndex(1);
656         } else {
657                 topspaceCO->setCurrentIndex(2);
658                 lengthToWidgets(topspaceED,
659                                 topspaceUnit,
660                                 tabular_.row_info[row].top_space.asString(),
661                                 default_unit);
662         }
663         topspaceED->setEnabled(!isReadonly
664                 && (topspaceCO->currentIndex() == 2));
665         topspaceUnit->setEnabled(!isReadonly
666                 && (topspaceCO->currentIndex() == 2));
667         topspaceCO->setEnabled(!isReadonly);
668
669         if (tabular_.row_info[row].bottom_space.empty()
670             && !tabular_.row_info[row].bottom_space_default) {
671                 bottomspaceCO->setCurrentIndex(0);
672         } else if (tabular_.row_info[row].bottom_space_default) {
673                 bottomspaceCO->setCurrentIndex(1);
674         } else {
675                 bottomspaceCO->setCurrentIndex(2);
676                 lengthToWidgets(bottomspaceED,
677                                 bottomspaceUnit,
678                                 tabular_.row_info[row].bottom_space.asString(),
679                                 default_unit);
680         }
681         bottomspaceED->setEnabled(!isReadonly
682                 && (bottomspaceCO->currentIndex() == 2));
683         bottomspaceUnit->setEnabled(!isReadonly
684                 && (bottomspaceCO->currentIndex() == 2));
685         bottomspaceCO->setEnabled(!isReadonly);
686
687         if (tabular_.row_info[row].interline_space.empty()
688             && !tabular_.row_info[row].interline_space_default) {
689                 interlinespaceCO->setCurrentIndex(0);
690         } else if (tabular_.row_info[row].interline_space_default) {
691                 interlinespaceCO->setCurrentIndex(1);
692         } else {
693                 interlinespaceCO->setCurrentIndex(2);
694                 lengthToWidgets(interlinespaceED,
695                                 interlinespaceUnit,
696                                 tabular_.row_info[row].interline_space.asString(),
697                                 default_unit);
698         }
699         interlinespaceED->setEnabled(!isReadonly
700                 && (interlinespaceCO->currentIndex() == 2));
701         interlinespaceUnit->setEnabled(!isReadonly
702                 && (interlinespaceCO->currentIndex() == 2));
703         interlinespaceCO->setEnabled(!isReadonly);
704
705         string colwidth;
706         if (!pwidth.zero())
707                 colwidth = pwidth.asString();
708         lengthToWidgets(widthED, widthUnit,
709                 colwidth, default_unit);
710
711         widthED->setEnabled(!isReadonly);
712         widthUnit->setEnabled(!isReadonly);
713
714         hAlignCB->clear();
715         hAlignCB->addItem(qt_("Left"));
716         hAlignCB->addItem(qt_("Center"));
717         hAlignCB->addItem(qt_("Right"));
718         if (!multicol && !pwidth.zero())
719                 hAlignCB->addItem(qt_("Justified"));
720
721         int align = 0;
722         switch (tabular_.getAlignment(cell)) {
723         case LYX_ALIGN_LEFT:
724                 align = 0;
725                 break;
726         case LYX_ALIGN_CENTER:
727                 align = 1;
728                 break;
729         case LYX_ALIGN_RIGHT:
730                 align = 2;
731                 break;
732         case LYX_ALIGN_BLOCK:
733         {
734                 if (!multicol && !pwidth.zero())
735                         align = 3;
736                 break;
737         }
738         default:
739                 align = 0;
740                 break;
741         }
742         hAlignCB->setCurrentIndex(align);
743
744         int valign = 0;
745         switch (tabular_.getVAlignment(cell)) {
746         case Tabular::LYX_VALIGN_TOP:
747                 valign = 0;
748                 break;
749         case Tabular::LYX_VALIGN_MIDDLE:
750                 valign = 1;
751                 break;
752         case Tabular::LYX_VALIGN_BOTTOM:
753                 valign = 2;
754                 break;
755         default:
756                 valign = 1;
757                 break;
758         }
759         if (pwidth.zero())
760                 valign = 1;
761         vAlignCB->setCurrentIndex(valign);
762
763         hAlignCB->setEnabled(true);
764         vAlignCB->setEnabled(!pwidth.zero());
765
766         if (!tabular_.isLongTabular()) {
767                 headerStatusCB->setChecked(false);
768                 headerBorderAboveCB->setChecked(false);
769                 headerBorderBelowCB->setChecked(false);
770                 firstheaderStatusCB->setChecked(false);
771                 firstheaderBorderAboveCB->setChecked(false);
772                 firstheaderBorderBelowCB->setChecked(false);
773                 firstheaderNoContentsCB->setChecked(false);
774                 footerStatusCB->setChecked(false);
775                 footerBorderAboveCB->setChecked(false);
776                 footerBorderBelowCB->setChecked(false);
777                 lastfooterStatusCB->setChecked(false);
778                 lastfooterBorderAboveCB->setChecked(false);
779                 lastfooterBorderBelowCB->setChecked(false);
780                 lastfooterNoContentsCB->setChecked(false);
781                 newpageCB->setChecked(false);
782                 newpageCB->setEnabled(false);
783                 return;
784         }
785
786         Tabular::ltType ltt;
787         bool use_empty;
788         bool row_set = tabular_.getRowOfLTHead(row, ltt);
789         headerStatusCB->setChecked(row_set);
790         if (ltt.set) {
791                 headerBorderAboveCB->setChecked(ltt.topDL);
792                 headerBorderBelowCB->setChecked(ltt.bottomDL);
793                 use_empty = true;
794         } else {
795                 headerBorderAboveCB->setChecked(false);
796                 headerBorderBelowCB->setChecked(false);
797                 headerBorderAboveCB->setEnabled(false);
798                 headerBorderBelowCB->setEnabled(false);
799                 firstheaderNoContentsCB->setChecked(false);
800                 firstheaderNoContentsCB->setEnabled(false);
801                 use_empty = false;
802         }
803
804         row_set = tabular_.getRowOfLTFirstHead(row, ltt);
805         firstheaderStatusCB->setChecked(row_set);
806         if (ltt.set && (!ltt.empty || !use_empty)) {
807                 firstheaderBorderAboveCB->setChecked(ltt.topDL);
808                 firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
809         } else {
810                 firstheaderBorderAboveCB->setEnabled(false);
811                 firstheaderBorderBelowCB->setEnabled(false);
812                 firstheaderBorderAboveCB->setChecked(false);
813                 firstheaderBorderBelowCB->setChecked(false);
814                 if (use_empty) {
815                         firstheaderNoContentsCB->setChecked(ltt.empty);
816                         if (ltt.empty)
817                                 firstheaderStatusCB->setEnabled(false);
818                 }
819         }
820
821         row_set = tabular_.getRowOfLTFoot(row, ltt);
822         footerStatusCB->setChecked(row_set);
823         if (ltt.set) {
824                 footerBorderAboveCB->setChecked(ltt.topDL);
825                 footerBorderBelowCB->setChecked(ltt.bottomDL);
826                 use_empty = true;
827         } else {
828                 footerBorderAboveCB->setChecked(false);
829                 footerBorderBelowCB->setChecked(false);
830                 footerBorderAboveCB->setEnabled(false);
831                 footerBorderBelowCB->setEnabled(false);
832                 lastfooterNoContentsCB->setChecked(false);
833                 lastfooterNoContentsCB->setEnabled(false);
834                 use_empty = false;
835         }
836
837         row_set = tabular_.getRowOfLTLastFoot(row, ltt);
838                 lastfooterStatusCB->setChecked(row_set);
839         if (ltt.set && (!ltt.empty || !use_empty)) {
840                 lastfooterBorderAboveCB->setChecked(ltt.topDL);
841                 lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
842         } else {
843                 lastfooterBorderAboveCB->setEnabled(false);
844                 lastfooterBorderBelowCB->setEnabled(false);
845                 lastfooterBorderAboveCB->setChecked(false);
846                 lastfooterBorderBelowCB->setChecked(false);
847                 if (use_empty) {
848                         lastfooterNoContentsCB->setChecked(ltt.empty);
849                         if (ltt.empty)
850                                 lastfooterStatusCB->setEnabled(false);
851                 }
852         }
853         newpageCB->setChecked(tabular_.getLTNewPage(row));
854 }
855
856
857 void GuiTabular::closeGUI()
858 {
859         // ugly hack to auto-apply the stuff that hasn't been
860         // yet. don't let this continue to exist ...
861
862         // Subtle here, we must /not/ apply any changes and
863         // then refer to tabular, as it will have been freed
864         // since the changes update the actual tabular_
865         //
866         // apply the fixed width values
867         Tabular::idx_type const cell = getActiveCell();
868         bool const multicol = tabular_.isMultiColumn(cell);
869         string width = widgetsToLength(widthED, widthUnit);
870         string width2;
871
872         Length llen = tabular_.getColumnPWidth(cell);
873         Length llenMulti = tabular_.getMColumnPWidth(cell);
874
875         if (multicol && !llenMulti.zero())
876                 width2 = llenMulti.asString();
877         else if (!multicol && !llen.zero())
878                 width2 = llen.asString();
879
880         // apply the special alignment
881         docstring const sa1 = qstring_to_ucs4(specialAlignmentED->text());
882         docstring sa2;
883
884         if (multicol)
885                 sa2 = tabular_.getAlignSpecial(cell, Tabular::SET_SPECIAL_MULTI);
886         else
887                 sa2 = tabular_.getAlignSpecial(cell, Tabular::SET_SPECIAL_COLUMN);
888
889         if (sa1 != sa2) {
890                 if (multicol)
891                         set(Tabular::SET_SPECIAL_MULTI, to_utf8(sa1));
892                 else
893                         set(Tabular::SET_SPECIAL_COLUMN, to_utf8(sa1));
894         }
895
896         if (width != width2) {
897                 if (multicol)
898                         set(Tabular::SET_MPWIDTH, width);
899                 else
900                         set(Tabular::SET_PWIDTH, width);
901         }
902
903         /* DO WE NEED THIS?
904         switch (topspaceCO->currentIndex()) {
905                 case 0:
906                         set(Tabular::SET_TOP_SPACE, "");
907                         break;
908                 case 1:
909                         set(Tabular::SET_TOP_SPACE, "default");
910                         break;
911                 case 2:
912                         set(Tabular::SET_TOP_SPACE,
913                                 widgetsToLength(topspaceED, topspaceUnit));
914                         break;
915         }
916
917         switch (bottomspaceCO->currentIndex()) {
918                 case 0:
919                         set(Tabular::SET_BOTTOM_SPACE, "");
920                         break;
921                 case 1:
922                         set(Tabular::SET_BOTTOM_SPACE, "default");
923                         break;
924                 case 2:
925                         set(Tabular::SET_BOTTOM_SPACE,
926                                 widgetsToLength(bottomspaceED, bottomspaceUnit));
927                         break;
928         }
929
930         switch (interlinespaceCO->currentIndex()) {
931                 case 0:
932                         set(Tabular::SET_INTERLINE_SPACE, "");
933                         break;
934                 case 1:
935                         set(Tabular::SET_INTERLINE_SPACE, "default");
936                         break;
937                 case 2:
938                         set(Tabular::SET_INTERLINE_SPACE,
939                                 widgetsToLength(interlinespaceED, interlinespaceUnit));
940                         break;
941         }
942 */
943 }
944
945 bool GuiTabular::initialiseParams(string const & data)
946 {
947         // try to get the current cell
948         BufferView const * const bv = bufferview();
949         InsetTabular const * current_inset = 0;
950         if (bv) {
951                 Cursor const & cur = bv->cursor();
952                 // get the innermost tabular inset;
953                 // assume that it is "ours"
954                 for (int i = cur.depth() - 1; i >= 0; --i)
955                         if (cur[i].inset().lyxCode() == Inset::TABULAR_CODE) {
956                                 current_inset = static_cast<InsetTabular const *>(&cur[i].inset());
957                                 active_cell_ = cur[i].idx();
958                                 break;
959                         }
960         }
961
962         if (current_inset && data.empty()) {
963                 tabular_ = Tabular(current_inset->tabular);
964                 return true;
965         }
966
967         InsetTabular tmp(buffer());
968         InsetTabularMailer::string2params(data, tmp);
969         tabular_ = Tabular(tmp.tabular);
970         return true;
971 }
972
973
974 void GuiTabular::clearParams()
975 {
976         InsetTabular tmp(buffer());
977         tabular_ = tmp.tabular;
978         active_cell_ = Tabular::npos;
979 }
980
981
982 Tabular::idx_type GuiTabular::getActiveCell() const
983 {
984         return active_cell_;
985 }
986
987
988 void GuiTabular::set(Tabular::Feature f, string const & arg)
989 {
990         string const data = featureAsString(f) + ' ' + arg;
991         dispatch(FuncRequest(getLfun(), data));
992 }
993
994
995 bool GuiTabular::useMetricUnits() const
996 {
997         return lyxrc.default_papersize > PAPER_USEXECUTIVE;
998 }
999
1000
1001 void GuiTabular::toggleTopLine()
1002 {
1003         if (tabular_.isMultiColumn(getActiveCell()))
1004                 set(Tabular::M_TOGGLE_LINE_TOP);
1005         else
1006                 set(Tabular::TOGGLE_LINE_TOP);
1007 }
1008
1009
1010 void GuiTabular::toggleBottomLine()
1011 {
1012         if (tabular_.isMultiColumn(getActiveCell()))
1013                 set(Tabular::M_TOGGLE_LINE_BOTTOM);
1014         else
1015                 set(Tabular::TOGGLE_LINE_BOTTOM);
1016 }
1017
1018
1019 void GuiTabular::toggleLeftLine()
1020 {
1021         if (tabular_.isMultiColumn(getActiveCell()))
1022                 set(Tabular::M_TOGGLE_LINE_LEFT);
1023         else
1024                 set(Tabular::TOGGLE_LINE_LEFT);
1025 }
1026
1027
1028 void GuiTabular::toggleRightLine()
1029 {
1030         if (tabular_.isMultiColumn(getActiveCell()))
1031                 set(Tabular::M_TOGGLE_LINE_RIGHT);
1032         else
1033                 set(Tabular::TOGGLE_LINE_RIGHT);
1034 }
1035
1036
1037 void GuiTabular::setSpecial(string const & special)
1038 {
1039         if (tabular_.isMultiColumn(getActiveCell()))
1040                 set(Tabular::SET_SPECIAL_MULTI, special);
1041         else
1042                 set(Tabular::SET_SPECIAL_COLUMN, special);
1043 }
1044
1045
1046 void GuiTabular::setWidth(string const & width)
1047 {
1048         if (tabular_.isMultiColumn(getActiveCell()))
1049                 set(Tabular::SET_MPWIDTH, width);
1050         else
1051                 set(Tabular::SET_PWIDTH, width);
1052
1053         dialog().updateView();
1054 }
1055
1056
1057 void GuiTabular::toggleMultiColumn()
1058 {
1059         set(Tabular::MULTICOLUMN);
1060         dialog().updateView();
1061 }
1062
1063
1064 void GuiTabular::rotateTabular(bool yes)
1065 {
1066         if (yes)
1067                 set(Tabular::SET_ROTATE_TABULAR);
1068         else
1069                 set(Tabular::UNSET_ROTATE_TABULAR);
1070 }
1071
1072
1073 void GuiTabular::rotateCell(bool yes)
1074 {
1075         if (yes)
1076                 set(Tabular::SET_ROTATE_CELL);
1077         else
1078                 set(Tabular::UNSET_ROTATE_CELL);
1079 }
1080
1081
1082 void GuiTabular::halign(GuiTabular::HALIGN h)
1083 {
1084         Tabular::Feature num = Tabular::ALIGN_LEFT;
1085         Tabular::Feature multi_num = Tabular::M_ALIGN_LEFT;
1086
1087         switch (h) {
1088                 case LEFT:
1089                         num = Tabular::ALIGN_LEFT;
1090                         multi_num = Tabular::M_ALIGN_LEFT;
1091                         break;
1092                 case CENTER:
1093                         num = Tabular::ALIGN_CENTER;
1094                         multi_num = Tabular::M_ALIGN_CENTER;
1095                         break;
1096                 case RIGHT:
1097                         num = Tabular::ALIGN_RIGHT;
1098                         multi_num = Tabular::M_ALIGN_RIGHT;
1099                         break;
1100                 case BLOCK:
1101                         num = Tabular::ALIGN_BLOCK;
1102                         //multi_num: no equivalent
1103                         break;
1104         }
1105
1106         if (tabular_.isMultiColumn(getActiveCell()))
1107                 set(multi_num);
1108         else
1109                 set(num);
1110 }
1111
1112
1113 void GuiTabular::valign(GuiTabular::VALIGN v)
1114 {
1115         Tabular::Feature num = Tabular::VALIGN_MIDDLE;
1116         Tabular::Feature multi_num = Tabular::M_VALIGN_MIDDLE;
1117
1118         switch (v) {
1119                 case TOP:
1120                         num = Tabular::VALIGN_TOP;
1121                         multi_num = Tabular::M_VALIGN_TOP;
1122                         break;
1123                 case MIDDLE:
1124                         num = Tabular::VALIGN_MIDDLE;
1125                         multi_num = Tabular::M_VALIGN_MIDDLE;
1126                         break;
1127                 case BOTTOM:
1128                         num = Tabular::VALIGN_BOTTOM;
1129                         multi_num = Tabular::M_VALIGN_BOTTOM;
1130                         break;
1131         }
1132
1133         if (tabular_.isMultiColumn(getActiveCell()))
1134                 set(multi_num);
1135         else
1136                 set(num);
1137 }
1138
1139
1140 void GuiTabular::booktabs(bool yes)
1141 {
1142         if (yes)
1143                 set(Tabular::SET_BOOKTABS);
1144         else
1145                 set(Tabular::UNSET_BOOKTABS);
1146 }
1147
1148
1149 void GuiTabular::longTabular(bool yes)
1150 {
1151         if (yes)
1152                 set(Tabular::SET_LONGTABULAR);
1153         else
1154                 set(Tabular::UNSET_LONGTABULAR);
1155 }
1156
1157
1158 Dialog * createGuiTabular(LyXView & lv) { return new GuiTabular(lv); }
1159
1160
1161 } // namespace frontend
1162 } // namespace lyx
1163
1164 #include "GuiTabular_moc.cpp"