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