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