]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormTabular.C
Rewrote the maths panel so that ALL the popups now derive from FormBaseBD,
[lyx.git] / src / frontends / xforms / FormTabular.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 1995 Matthias Ettrich
8  *          Copyright 1995-2000 The LyX Team.
9  *
10  *======================================================*/
11 /* FormTabular.C
12  * FormTabular Interface Class Implementation
13  */
14
15 #include <config.h>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "FormTabular.h"
22 #include "form_tabular.h"
23 #include "LyXView.h"
24 #include "Dialogs.h"
25 #include "insets/insettabular.h"
26 #include "buffer.h"
27 #include "xforms_helpers.h"
28
29 using SigC::slot;
30
31 FormTabular::FormTabular(LyXView * lv, Dialogs * d)
32         : FormInset(lv, d, _("Tabular Layout")),
33           inset_(0), actCell_(-1)
34 {
35         // let the dialog be shown
36         // This is a permanent connection so we won't bother
37         // storing a copy because we won't be disconnecting.
38         d->showTabular.connect(slot(this, &FormTabular::showInset));
39         d->updateTabular.connect(slot(this, &FormTabular::updateInset));
40 }
41
42
43 void FormTabular::redraw()
44 {
45         if(form() && form()->visible)
46                 fl_redraw_form(form());
47         else
48                 return;
49
50         FL_FORM * outer_form = fl_get_active_folder(dialog_->tabFolder);
51         if (outer_form && outer_form->visible)
52                 fl_redraw_form(outer_form);
53 }
54
55
56 FL_FORM * FormTabular::form() const
57 {
58         if (dialog_.get())
59                 return dialog_->form;
60         return 0;
61 }
62
63
64 void FormTabular::disconnect()
65 {
66         inset_ = 0;
67         FormInset::disconnect();
68 }
69
70
71 void FormTabular::showInset(InsetTabular * inset)
72 {
73         if (inset == 0) return;
74
75         // If connected to another inset, disconnect from it.
76         if (inset_ != inset) {
77                 ih_.disconnect();
78                 ih_ = inset->hideDialog.connect(slot(this, &FormTabular::hide));
79                 inset_ = inset;
80         }
81
82         show();
83 }
84
85
86 void FormTabular::updateInset(InsetTabular * inset)
87 {
88         if (inset == 0 || inset_ == 0) return;
89
90         // If connected to another inset, disconnect from it.
91         if (inset_ != inset) {
92                 ih_.disconnect();
93                 ih_ = inset->hideDialog.connect(slot(this, &FormTabular::hide));
94                 inset_ = inset;
95         }
96
97         update();
98 }
99
100
101 void FormTabular::build()
102 {
103         dialog_.reset(build_tabular());
104         tabular_options_.reset(build_tabular_options());
105         column_options_.reset(build_column_options());
106         cell_options_.reset(build_cell_options());
107         longtable_options_.reset(build_longtable_options());
108
109         fl_set_input_return(column_options_->input_column_width,
110                             FL_RETURN_END);
111         fl_set_input_return(column_options_->input_special_alignment,
112                             FL_RETURN_END);
113         fl_set_input_return(cell_options_->input_mcolumn_width,
114                             FL_RETURN_END);
115         fl_set_input_return(cell_options_->input_special_multialign,
116                             FL_RETURN_END);
117
118         fl_addto_tabfolder(dialog_->tabFolder, _("Tabular"),
119                            tabular_options_->form);
120         fl_addto_tabfolder(dialog_->tabFolder, _("Column/Row"),
121                            column_options_->form);
122         fl_addto_tabfolder(dialog_->tabFolder, _("Cell"),
123                            cell_options_->form);
124         fl_addto_tabfolder(dialog_->tabFolder, _("LongTable"),
125                            longtable_options_->form);
126 }
127
128
129 void FormTabular::update()
130 {
131         if (!inset_ || !inset_->tabular)
132                 return;
133
134         LyXTabular * tabular = inset_->tabular;
135         int
136                 align,
137                 cell;
138         char
139                 buf[12];
140         string
141                 pwidth, special;
142
143         actCell_ = cell = inset_->GetActCell();
144         int column = tabular->column_of_cell(cell)+1;
145         fl_set_object_label(dialog_->text_warning,"");
146         fl_activate_object(column_options_->input_special_alignment);
147         fl_activate_object(cell_options_->input_special_multialign);
148         fl_activate_object(column_options_->input_column_width);
149         sprintf(buf,"%d",column);
150         fl_set_input(dialog_->input_tabular_column, buf);
151         fl_deactivate_object(dialog_->input_tabular_column);
152         int row = tabular->row_of_cell(cell)+1;
153         sprintf(buf,"%d",row);
154         fl_set_input(dialog_->input_tabular_row, buf);
155         fl_deactivate_object(dialog_->input_tabular_row);
156         if (tabular->IsMultiColumn(cell)) {
157                 fl_set_button(cell_options_->radio_multicolumn, 1);
158                 fl_set_button(cell_options_->radio_border_top,
159                               tabular->TopLine(cell)?1:0);
160                 setEnabled(cell_options_->radio_border_top, true);
161                 fl_set_button(cell_options_->radio_border_bottom,
162                               tabular->BottomLine(cell)?1:0);
163                 setEnabled(cell_options_->radio_border_bottom, true);
164                 fl_set_button(cell_options_->radio_border_left,
165                               tabular->LeftLine(cell)?1:0);
166                 setEnabled(cell_options_->radio_border_left, true);
167                 fl_set_button(cell_options_->radio_border_right,
168                               tabular->RightLine(cell)?1:0);
169                 setEnabled(cell_options_->radio_border_right, true);
170                 pwidth = tabular->GetMColumnPWidth(cell);
171                 align = tabular->GetAlignment(cell);
172                 if (!pwidth.empty() || (align == LYX_ALIGN_LEFT))
173                         fl_set_button(cell_options_->radio_align_left, 1);
174                 else if (align == LYX_ALIGN_RIGHT)
175                         fl_set_button(cell_options_->radio_align_right, 1);
176                 else
177                         fl_set_button(cell_options_->radio_align_center, 1);
178                 setEnabled(cell_options_->radio_align_left,   true);
179                 setEnabled(cell_options_->radio_align_right,  true);
180                 setEnabled(cell_options_->radio_align_center, true);
181                 align = tabular->GetVAlignment(cell);
182                 fl_set_button(cell_options_->radio_valign_top, 0);
183                 fl_set_button(cell_options_->radio_valign_bottom, 0);
184                 fl_set_button(cell_options_->radio_valign_center, 0);
185                 if (pwidth.empty() || (align == LyXTabular::LYX_VALIGN_CENTER))
186                         fl_set_button(cell_options_->radio_valign_center, 1);
187                 else if (align == LyXTabular::LYX_VALIGN_BOTTOM)
188                         fl_set_button(cell_options_->radio_valign_bottom, 1);
189                 else
190                         fl_set_button(cell_options_->radio_valign_top, 1);
191                 setEnabled(cell_options_->radio_valign_top,    true);
192                 setEnabled(cell_options_->radio_valign_bottom, true);
193                 setEnabled(cell_options_->radio_valign_center, true);
194                 special = tabular->GetAlignSpecial(cell,LyXTabular::SET_SPECIAL_MULTI);
195                 fl_set_input(cell_options_->input_special_multialign, special.c_str());
196                 fl_set_input(cell_options_->input_mcolumn_width,pwidth.c_str());
197                 if (!lv_->buffer()->isReadonly()) {
198                         setEnabled(cell_options_->input_special_multialign, true);
199                         setEnabled(cell_options_->input_mcolumn_width, true);
200                 }
201
202                 setEnabled(cell_options_->radio_valign_top,    !pwidth.empty());
203                 setEnabled(cell_options_->radio_valign_bottom, !pwidth.empty());
204                 setEnabled(cell_options_->radio_valign_center, !pwidth.empty());
205                 
206                 setEnabled(cell_options_->radio_align_left,   pwidth.empty());
207                 setEnabled(cell_options_->radio_align_right,  pwidth.empty());
208                 setEnabled(cell_options_->radio_align_center, pwidth.empty());
209         } else {
210                 fl_set_button(cell_options_->radio_multicolumn, 0);
211
212                 fl_set_button(cell_options_->radio_border_top, 0);
213                 setEnabled(cell_options_->radio_border_top, false);
214
215                 fl_set_button(cell_options_->radio_border_bottom, 0);
216                 setEnabled(cell_options_->radio_border_bottom, false);
217
218                 fl_set_button(cell_options_->radio_border_left, 0);
219                 setEnabled(cell_options_->radio_border_left, false);
220
221                 fl_set_button(cell_options_->radio_border_right, 0);
222                 setEnabled(cell_options_->radio_border_right, false);
223
224                 fl_set_button(cell_options_->radio_align_left, 0);
225                 setEnabled(cell_options_->radio_align_left, false);
226
227                 fl_set_button(cell_options_->radio_align_right, 0);
228                 setEnabled(cell_options_->radio_align_right, false);
229
230                 fl_set_button(cell_options_->radio_align_center, 0);
231                 setEnabled(cell_options_->radio_align_center, false);
232
233                 fl_set_button(cell_options_->radio_valign_top, 0);
234                 setEnabled(cell_options_->radio_valign_top, false);
235
236                 fl_set_button(cell_options_->radio_valign_bottom, 0);
237                 setEnabled(cell_options_->radio_valign_bottom, false);
238
239                 fl_set_button(cell_options_->radio_valign_center, 0);
240                 setEnabled(cell_options_->radio_valign_center, false);
241
242                 fl_set_input(cell_options_->input_special_multialign, "");
243                 setEnabled(cell_options_->input_special_multialign, false);
244
245                 fl_set_input(cell_options_->input_mcolumn_width, "");
246                 setEnabled(cell_options_->input_mcolumn_width, false);
247         }
248         if (tabular->GetRotateCell(cell))
249                 fl_set_button(cell_options_->radio_rotate_cell, 1);
250         else
251                 fl_set_button(cell_options_->radio_rotate_cell, 0);
252         if (tabular->TopLine(cell, true))
253                 fl_set_button(column_options_->radio_border_top, 1);
254         else
255                 fl_set_button(column_options_->radio_border_top, 0);
256         if (tabular->BottomLine(cell, true))
257                 fl_set_button(column_options_->radio_border_bottom, 1);
258         else
259                 fl_set_button(column_options_->radio_border_bottom, 0);
260         if (tabular->LeftLine(cell, true))
261                 fl_set_button(column_options_->radio_border_left, 1);
262         else
263                 fl_set_button(column_options_->radio_border_left, 0);
264         if (tabular->RightLine(cell, true))
265                 fl_set_button(column_options_->radio_border_right, 1);
266         else
267                 fl_set_button(column_options_->radio_border_right, 0);
268         special = tabular->GetAlignSpecial(cell,LyXTabular::SET_SPECIAL_COLUMN);
269         fl_set_input(column_options_->input_special_alignment, special.c_str());
270
271         bool const isReadonly = lv_->buffer()->isReadonly();
272         setEnabled(column_options_->input_special_alignment, !isReadonly);
273
274         pwidth = tabular->GetColumnPWidth(cell);
275         fl_set_input(column_options_->input_column_width,pwidth.c_str());
276         setEnabled(column_options_->input_column_width, !isReadonly);
277
278         setEnabled(cell_options_->radio_useminipage, !pwidth.empty());
279         if (!pwidth.empty()) {
280                 if (tabular->GetUsebox(cell) == 2)
281                         fl_set_button(cell_options_->radio_useminipage, 1);
282                 else
283                         fl_set_button(cell_options_->radio_useminipage, 0);
284         } else {
285                 fl_set_button(cell_options_->radio_useminipage,0);
286         }
287         align = tabular->GetAlignment(cell, true);
288         fl_set_button(column_options_->radio_align_left, 0);
289         fl_set_button(column_options_->radio_align_right, 0);
290         fl_set_button(column_options_->radio_align_center, 0);
291         if (!pwidth.empty() || (align == LYX_ALIGN_LEFT))
292                 fl_set_button(column_options_->radio_align_left, 1);
293         else if (align == LYX_ALIGN_RIGHT)
294                 fl_set_button(column_options_->radio_align_right, 1);
295         else
296                 fl_set_button(column_options_->radio_align_center, 1);
297         align = tabular->GetVAlignment(cell, true);
298         fl_set_button(column_options_->radio_valign_top, 0);
299         fl_set_button(column_options_->radio_valign_bottom, 0);
300         fl_set_button(column_options_->radio_valign_center, 0);
301         if (pwidth.empty() || (align == LyXTabular::LYX_VALIGN_CENTER))
302                 fl_set_button(column_options_->radio_valign_center, 1);
303         else if (align == LyXTabular::LYX_VALIGN_BOTTOM)
304                 fl_set_button(column_options_->radio_valign_bottom, 1);
305         else
306                 fl_set_button(column_options_->radio_valign_top, 1);
307
308         setEnabled(column_options_->radio_align_left,   pwidth.empty());
309         setEnabled(column_options_->radio_align_right,  pwidth.empty());
310         setEnabled(column_options_->radio_align_center, pwidth.empty());
311         
312         setEnabled(column_options_->radio_valign_top,    !pwidth.empty());
313         setEnabled(column_options_->radio_valign_bottom, !pwidth.empty());
314         setEnabled(column_options_->radio_valign_center, !pwidth.empty());
315
316         fl_set_button(tabular_options_->radio_longtable,
317                       tabular->IsLongTabular());
318
319         bool const enable = tabular->IsLongTabular();
320         setEnabled(longtable_options_->radio_lt_firsthead, enable);
321         setEnabled(longtable_options_->radio_lt_head,      enable);
322         setEnabled(longtable_options_->radio_lt_foot,      enable);
323         setEnabled(longtable_options_->radio_lt_lastfoot,  enable);
324         setEnabled(longtable_options_->radio_lt_newpage,   enable);
325
326         if (enable) {
327                 int dummy;
328                 fl_set_button(longtable_options_->radio_lt_firsthead,
329                               tabular->GetRowOfLTFirstHead(cell, dummy));
330                 fl_set_button(longtable_options_->radio_lt_head,
331                               tabular->GetRowOfLTHead(cell, dummy));
332                 fl_set_button(longtable_options_->radio_lt_foot,
333                               tabular->GetRowOfLTFoot(cell, dummy));
334                 fl_set_button(longtable_options_->radio_lt_lastfoot,
335                               tabular->GetRowOfLTLastFoot(cell, dummy));
336                 fl_set_button(longtable_options_->radio_lt_newpage,
337                               tabular->GetLTNewPage(cell));
338         } else {
339                 fl_set_button(longtable_options_->radio_lt_firsthead,0);
340                 fl_set_button(longtable_options_->radio_lt_head,0);
341                 fl_set_button(longtable_options_->radio_lt_foot,0);
342                 fl_set_button(longtable_options_->radio_lt_lastfoot,0);
343                 fl_set_button(longtable_options_->radio_lt_newpage,0);
344         }
345         fl_set_button(tabular_options_->radio_rotate_tabular,
346                       tabular->GetRotateTabular());
347 }
348
349 bool FormTabular::input(FL_OBJECT * ob, long)
350 {
351     if (!inset_)
352         return false;
353
354     LyXTabular * tabular = inset_->tabular;
355     int s;
356     LyXTabular::Feature num = LyXTabular::LAST_ACTION;
357     string special;;
358
359     int cell = inset_->GetActCell();
360     if (actCell_ != cell) {
361         update();
362         fl_set_object_label(dialog_->text_warning,
363                      _("Warning: Wrong Cursor position, updated window"));
364         fl_show_object(dialog_->text_warning);
365         return false;
366     }
367     // No point in processing directives that you can't do anything with
368     // anyhow, so exit now if the buffer is read-only.
369     if (lv_->buffer()->isReadonly()) {
370       update();
371       return false;
372     }
373     if (ob == column_options_->input_column_width) {
374         string str = fl_get_input(ob);
375         if (!str.empty() && !isValidLength(str)) {
376             fl_set_object_label(dialog_->text_warning,
377                  _("Warning: Invalid Length (valid example: 10mm)"));
378             fl_show_object(dialog_->text_warning);
379             return false;
380         }
381         inset_->TabularFeatures(lv_->view(), LyXTabular::SET_PWIDTH,str);
382         update(); // update for alignment
383         return true;
384     }
385     if (ob == cell_options_->input_mcolumn_width) {
386         string str = fl_get_input(ob);
387         if (!str.empty() && !isValidLength(str)) {
388             fl_set_object_label(dialog_->text_warning,
389                  _("Warning: Invalid Length (valid example: 10mm)"));
390             fl_show_object(dialog_->text_warning);
391             return false;
392         }
393         inset_->TabularFeatures(lv_->view(), LyXTabular::SET_MPWIDTH,str);
394         update(); // update for alignment
395         return true;
396     }
397     string str = fl_get_input(column_options_->input_column_width);
398     if (!str.empty() && !isValidLength(str)) {
399         fl_set_object_label(
400             dialog_->text_warning,
401             _("Warning: Invalid Length (valid example: 10mm)"));
402         fl_show_object(dialog_->text_warning);
403         return false;
404     }
405     if (ob == tabular_options_->button_append_row)
406         num = LyXTabular::APPEND_ROW;
407     else if (ob == tabular_options_->button_append_column)
408         num = LyXTabular::APPEND_COLUMN;
409     else if (ob == tabular_options_->button_delete_row)
410         num = LyXTabular::DELETE_ROW;
411     else if (ob == tabular_options_->button_delete_column)
412         num = LyXTabular::DELETE_COLUMN;
413     else if (ob == tabular_options_->button_set_borders)
414         num = LyXTabular::SET_ALL_LINES;
415     else if (ob == tabular_options_->button_unset_borders)
416         num = LyXTabular::UNSET_ALL_LINES;
417     else if (ob == column_options_->radio_border_top)
418         num = LyXTabular::TOGGLE_LINE_TOP;
419     else if (ob == column_options_->radio_border_bottom)
420         num = LyXTabular::TOGGLE_LINE_BOTTOM;
421     else if (ob == column_options_->radio_border_left)
422         num = LyXTabular::TOGGLE_LINE_LEFT;
423     else if (ob == column_options_->radio_border_right)
424         num = LyXTabular::TOGGLE_LINE_RIGHT;
425     else if (ob == column_options_->radio_align_left)
426         num = LyXTabular::ALIGN_LEFT;
427     else if (ob == column_options_->radio_align_right)
428         num = LyXTabular::ALIGN_RIGHT;
429     else if (ob == column_options_->radio_align_center)
430         num = LyXTabular::ALIGN_CENTER;
431     else if (ob == column_options_->radio_valign_top)
432         num = LyXTabular::VALIGN_TOP;
433     else if (ob == column_options_->radio_valign_bottom)
434         num = LyXTabular::VALIGN_BOTTOM;
435     else if (ob == column_options_->radio_valign_center)
436         num = LyXTabular::VALIGN_CENTER;
437     else if (ob == cell_options_->radio_multicolumn)
438         num = LyXTabular::MULTICOLUMN;
439     else if (ob == tabular_options_->radio_longtable) {
440             bool const enable =
441                     fl_get_button(tabular_options_->radio_longtable);
442             
443             setEnabled(longtable_options_->radio_lt_firsthead, enable);
444             setEnabled(longtable_options_->radio_lt_head,      enable);
445             setEnabled(longtable_options_->radio_lt_foot,      enable);
446             setEnabled(longtable_options_->radio_lt_lastfoot,  enable);
447             setEnabled(longtable_options_->radio_lt_newpage,   enable);
448
449             if (enable) {
450                     num = LyXTabular::SET_LONGTABULAR;
451                     int dummy;
452                     fl_set_button(longtable_options_->radio_lt_firsthead,
453                                   tabular->GetRowOfLTFirstHead(cell, dummy));
454                     fl_set_button(longtable_options_->radio_lt_head,
455                                   tabular->GetRowOfLTHead(cell, dummy));
456                     fl_set_button(longtable_options_->radio_lt_foot,
457                                   tabular->GetRowOfLTFoot(cell, dummy));
458                     fl_set_button(longtable_options_->radio_lt_lastfoot,
459                                   tabular->GetRowOfLTLastFoot(cell, dummy));
460                     fl_set_button(longtable_options_->radio_lt_firsthead,
461                                   tabular->GetLTNewPage(cell));
462             } else {
463                     num = LyXTabular::UNSET_LONGTABULAR;
464                     fl_set_button(longtable_options_->radio_lt_firsthead,0);
465                     fl_set_button(longtable_options_->radio_lt_head,0);
466                     fl_set_button(longtable_options_->radio_lt_foot,0);
467                     fl_set_button(longtable_options_->radio_lt_lastfoot,0);
468                     fl_set_button(longtable_options_->radio_lt_newpage,0);
469             }
470     } else if (ob == tabular_options_->radio_rotate_tabular) {
471         s = fl_get_button(tabular_options_->radio_rotate_tabular);
472         if (s)
473             num = LyXTabular::SET_ROTATE_TABULAR;
474         else
475             num = LyXTabular::UNSET_ROTATE_TABULAR;
476     } else if (ob == cell_options_->radio_rotate_cell) {
477         s = fl_get_button(cell_options_->radio_rotate_cell);
478         if (s)
479             num = LyXTabular::SET_ROTATE_CELL;
480         else
481             num = LyXTabular::UNSET_ROTATE_CELL;
482     } else if (ob == cell_options_->radio_useminipage) {
483         num = LyXTabular::SET_USEBOX;
484         special = "2";
485     } else if (ob == longtable_options_->radio_lt_firsthead) {
486         num = LyXTabular::SET_LTFIRSTHEAD;
487     } else if (ob == longtable_options_->radio_lt_head) {
488         num = LyXTabular::SET_LTHEAD;
489     } else if (ob == longtable_options_->radio_lt_foot) {
490         num = LyXTabular::SET_LTFOOT;
491     } else if (ob == longtable_options_->radio_lt_lastfoot) {
492         num = LyXTabular::SET_LTLASTFOOT;
493     } else if (ob == longtable_options_->radio_lt_newpage) {
494         num = LyXTabular::SET_LTNEWPAGE;
495     } else if (ob == column_options_->input_special_alignment) {
496         special = fl_get_input(column_options_->input_special_alignment);
497         num = LyXTabular::SET_SPECIAL_COLUMN;
498     } else if (ob == cell_options_->input_special_multialign) {
499         special = fl_get_input(cell_options_->input_special_multialign);
500         num = LyXTabular::SET_SPECIAL_MULTI;
501     } else if (ob == cell_options_->radio_border_top)
502         num = LyXTabular::M_TOGGLE_LINE_TOP;
503     else if (ob == cell_options_->radio_border_bottom)
504         num = LyXTabular::M_TOGGLE_LINE_BOTTOM;
505     else if (ob == cell_options_->radio_border_left)
506         num = LyXTabular::M_TOGGLE_LINE_LEFT;
507     else if (ob == cell_options_->radio_border_right)
508         num = LyXTabular::M_TOGGLE_LINE_RIGHT;
509     else if (ob == cell_options_->radio_align_left)
510         num = LyXTabular::M_ALIGN_LEFT;
511     else if (ob == cell_options_->radio_align_right)
512         num = LyXTabular::M_ALIGN_RIGHT;
513     else if (ob == cell_options_->radio_align_center)
514         num = LyXTabular::M_ALIGN_CENTER;
515     else if (ob == cell_options_->radio_valign_top)
516         num = LyXTabular::M_VALIGN_TOP;
517     else if (ob == cell_options_->radio_valign_bottom)
518         num = LyXTabular::M_VALIGN_BOTTOM;
519     else if (ob == cell_options_->radio_valign_center)
520         num = LyXTabular::M_VALIGN_CENTER;
521     else
522         return false;
523     
524     inset_->TabularFeatures(lv_->view(), num, special);
525     update();
526
527     return true;
528 }