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