]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormTabular.C
Compile fixes for DEC cxx, John's maths and keymap patches.
[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         // Workaround dumb xforms sizing bug
110         minw_ = form()->w;
111         minh_ = form()->h;
112
113         fl_set_input_return(column_options_->input_column_width,
114                             FL_RETURN_END);
115         fl_set_input_return(column_options_->input_special_alignment,
116                             FL_RETURN_END);
117         fl_set_input_return(cell_options_->input_mcolumn_width,
118                             FL_RETURN_END);
119         fl_set_input_return(cell_options_->input_special_multialign,
120                             FL_RETURN_END);
121
122         fl_addto_tabfolder(dialog_->tabFolder, _("Tabular"),
123                            tabular_options_->form);
124         fl_addto_tabfolder(dialog_->tabFolder, _("Column/Row"),
125                            column_options_->form);
126         fl_addto_tabfolder(dialog_->tabFolder, _("Cell"),
127                            cell_options_->form);
128         fl_addto_tabfolder(dialog_->tabFolder, _("LongTable"),
129                            longtable_options_->form);
130 }
131
132
133 void FormTabular::update()
134 {
135         if (!inset_ || !inset_->tabular)
136                 return;
137
138         LyXTabular * tabular = inset_->tabular;
139         int
140                 align,
141                 cell;
142         char
143                 buf[12];
144         string
145                 pwidth, special;
146
147         actCell_ = cell = inset_->GetActCell();
148         int column = tabular->column_of_cell(cell)+1;
149         fl_set_object_label(dialog_->text_warning,"");
150         fl_activate_object(column_options_->input_special_alignment);
151         fl_activate_object(cell_options_->input_special_multialign);
152         fl_activate_object(column_options_->input_column_width);
153         sprintf(buf,"%d",column);
154         fl_set_input(dialog_->input_tabular_column, buf);
155         fl_deactivate_object(dialog_->input_tabular_column);
156         int row = tabular->row_of_cell(cell)+1;
157         sprintf(buf,"%d",row);
158         fl_set_input(dialog_->input_tabular_row, buf);
159         fl_deactivate_object(dialog_->input_tabular_row);
160         if (tabular->IsMultiColumn(cell)) {
161                 fl_set_button(cell_options_->radio_multicolumn, 1);
162                 fl_set_button(cell_options_->radio_border_top,
163                               tabular->TopLine(cell)?1:0);
164                 setEnabled(cell_options_->radio_border_top, true);
165                 fl_set_button(cell_options_->radio_border_bottom,
166                               tabular->BottomLine(cell)?1:0);
167                 setEnabled(cell_options_->radio_border_bottom, true);
168                 fl_set_button(cell_options_->radio_border_left,
169                               tabular->LeftLine(cell)?1:0);
170                 setEnabled(cell_options_->radio_border_left, true);
171                 fl_set_button(cell_options_->radio_border_right,
172                               tabular->RightLine(cell)?1:0);
173                 setEnabled(cell_options_->radio_border_right, true);
174                 pwidth = tabular->GetMColumnPWidth(cell);
175                 align = tabular->GetAlignment(cell);
176                 if (!pwidth.empty() || (align == LYX_ALIGN_LEFT))
177                         fl_set_button(cell_options_->radio_align_left, 1);
178                 else if (align == LYX_ALIGN_RIGHT)
179                         fl_set_button(cell_options_->radio_align_right, 1);
180                 else
181                         fl_set_button(cell_options_->radio_align_center, 1);
182                 setEnabled(cell_options_->radio_align_left,   true);
183                 setEnabled(cell_options_->radio_align_right,  true);
184                 setEnabled(cell_options_->radio_align_center, true);
185                 align = tabular->GetVAlignment(cell);
186                 fl_set_button(cell_options_->radio_valign_top, 0);
187                 fl_set_button(cell_options_->radio_valign_bottom, 0);
188                 fl_set_button(cell_options_->radio_valign_center, 0);
189                 if (pwidth.empty() || (align == LyXTabular::LYX_VALIGN_CENTER))
190                         fl_set_button(cell_options_->radio_valign_center, 1);
191                 else if (align == LyXTabular::LYX_VALIGN_BOTTOM)
192                         fl_set_button(cell_options_->radio_valign_bottom, 1);
193                 else
194                         fl_set_button(cell_options_->radio_valign_top, 1);
195                 setEnabled(cell_options_->radio_valign_top,    true);
196                 setEnabled(cell_options_->radio_valign_bottom, true);
197                 setEnabled(cell_options_->radio_valign_center, true);
198                 special = tabular->GetAlignSpecial(cell,LyXTabular::SET_SPECIAL_MULTI);
199                 fl_set_input(cell_options_->input_special_multialign, special.c_str());
200                 fl_set_input(cell_options_->input_mcolumn_width,pwidth.c_str());
201                 if (!lv_->buffer()->isReadonly()) {
202                         setEnabled(cell_options_->input_special_multialign, true);
203                         setEnabled(cell_options_->input_mcolumn_width, true);
204                 }
205
206                 setEnabled(cell_options_->radio_valign_top,    !pwidth.empty());
207                 setEnabled(cell_options_->radio_valign_bottom, !pwidth.empty());
208                 setEnabled(cell_options_->radio_valign_center, !pwidth.empty());
209                 
210                 setEnabled(cell_options_->radio_align_left,   pwidth.empty());
211                 setEnabled(cell_options_->radio_align_right,  pwidth.empty());
212                 setEnabled(cell_options_->radio_align_center, pwidth.empty());
213         } else {
214                 fl_set_button(cell_options_->radio_multicolumn, 0);
215
216                 fl_set_button(cell_options_->radio_border_top, 0);
217                 setEnabled(cell_options_->radio_border_top, false);
218
219                 fl_set_button(cell_options_->radio_border_bottom, 0);
220                 setEnabled(cell_options_->radio_border_bottom, false);
221
222                 fl_set_button(cell_options_->radio_border_left, 0);
223                 setEnabled(cell_options_->radio_border_left, false);
224
225                 fl_set_button(cell_options_->radio_border_right, 0);
226                 setEnabled(cell_options_->radio_border_right, false);
227
228                 fl_set_button(cell_options_->radio_align_left, 0);
229                 setEnabled(cell_options_->radio_align_left, false);
230
231                 fl_set_button(cell_options_->radio_align_right, 0);
232                 setEnabled(cell_options_->radio_align_right, false);
233
234                 fl_set_button(cell_options_->radio_align_center, 0);
235                 setEnabled(cell_options_->radio_align_center, false);
236
237                 fl_set_button(cell_options_->radio_valign_top, 0);
238                 setEnabled(cell_options_->radio_valign_top, false);
239
240                 fl_set_button(cell_options_->radio_valign_bottom, 0);
241                 setEnabled(cell_options_->radio_valign_bottom, false);
242
243                 fl_set_button(cell_options_->radio_valign_center, 0);
244                 setEnabled(cell_options_->radio_valign_center, false);
245
246                 fl_set_input(cell_options_->input_special_multialign, "");
247                 setEnabled(cell_options_->input_special_multialign, false);
248
249                 fl_set_input(cell_options_->input_mcolumn_width, "");
250                 setEnabled(cell_options_->input_mcolumn_width, false);
251         }
252         if (tabular->GetRotateCell(cell))
253                 fl_set_button(cell_options_->radio_rotate_cell, 1);
254         else
255                 fl_set_button(cell_options_->radio_rotate_cell, 0);
256         if (tabular->TopLine(cell, true))
257                 fl_set_button(column_options_->radio_border_top, 1);
258         else
259                 fl_set_button(column_options_->radio_border_top, 0);
260         if (tabular->BottomLine(cell, true))
261                 fl_set_button(column_options_->radio_border_bottom, 1);
262         else
263                 fl_set_button(column_options_->radio_border_bottom, 0);
264         if (tabular->LeftLine(cell, true))
265                 fl_set_button(column_options_->radio_border_left, 1);
266         else
267                 fl_set_button(column_options_->radio_border_left, 0);
268         if (tabular->RightLine(cell, true))
269                 fl_set_button(column_options_->radio_border_right, 1);
270         else
271                 fl_set_button(column_options_->radio_border_right, 0);
272         special = tabular->GetAlignSpecial(cell,LyXTabular::SET_SPECIAL_COLUMN);
273         fl_set_input(column_options_->input_special_alignment, special.c_str());
274
275         bool const isReadonly = lv_->buffer()->isReadonly();
276         setEnabled(column_options_->input_special_alignment, !isReadonly);
277
278         pwidth = tabular->GetColumnPWidth(cell);
279         fl_set_input(column_options_->input_column_width,pwidth.c_str());
280         setEnabled(column_options_->input_column_width, !isReadonly);
281
282         setEnabled(cell_options_->radio_useminipage, !pwidth.empty());
283         if (!pwidth.empty()) {
284                 if (tabular->GetUsebox(cell) == 2)
285                         fl_set_button(cell_options_->radio_useminipage, 1);
286                 else
287                         fl_set_button(cell_options_->radio_useminipage, 0);
288         } else {
289                 fl_set_button(cell_options_->radio_useminipage,0);
290         }
291         align = tabular->GetAlignment(cell, true);
292         fl_set_button(column_options_->radio_align_left, 0);
293         fl_set_button(column_options_->radio_align_right, 0);
294         fl_set_button(column_options_->radio_align_center, 0);
295         if (!pwidth.empty() || (align == LYX_ALIGN_LEFT))
296                 fl_set_button(column_options_->radio_align_left, 1);
297         else if (align == LYX_ALIGN_RIGHT)
298                 fl_set_button(column_options_->radio_align_right, 1);
299         else
300                 fl_set_button(column_options_->radio_align_center, 1);
301         align = tabular->GetVAlignment(cell, true);
302         fl_set_button(column_options_->radio_valign_top, 0);
303         fl_set_button(column_options_->radio_valign_bottom, 0);
304         fl_set_button(column_options_->radio_valign_center, 0);
305         if (pwidth.empty() || (align == LyXTabular::LYX_VALIGN_CENTER))
306                 fl_set_button(column_options_->radio_valign_center, 1);
307         else if (align == LyXTabular::LYX_VALIGN_BOTTOM)
308                 fl_set_button(column_options_->radio_valign_bottom, 1);
309         else
310                 fl_set_button(column_options_->radio_valign_top, 1);
311
312         setEnabled(column_options_->radio_align_left,   pwidth.empty());
313         setEnabled(column_options_->radio_align_right,  pwidth.empty());
314         setEnabled(column_options_->radio_align_center, pwidth.empty());
315         
316         setEnabled(column_options_->radio_valign_top,    !pwidth.empty());
317         setEnabled(column_options_->radio_valign_bottom, !pwidth.empty());
318         setEnabled(column_options_->radio_valign_center, !pwidth.empty());
319
320         fl_set_button(tabular_options_->radio_longtable,
321                       tabular->IsLongTabular());
322
323         bool const enable = tabular->IsLongTabular();
324         setEnabled(longtable_options_->radio_lt_firsthead, enable);
325         setEnabled(longtable_options_->radio_lt_head,      enable);
326         setEnabled(longtable_options_->radio_lt_foot,      enable);
327         setEnabled(longtable_options_->radio_lt_lastfoot,  enable);
328         setEnabled(longtable_options_->radio_lt_newpage,   enable);
329
330         if (enable) {
331                 int dummy;
332                 fl_set_button(longtable_options_->radio_lt_firsthead,
333                               tabular->GetRowOfLTFirstHead(cell, dummy));
334                 fl_set_button(longtable_options_->radio_lt_head,
335                               tabular->GetRowOfLTHead(cell, dummy));
336                 fl_set_button(longtable_options_->radio_lt_foot,
337                               tabular->GetRowOfLTFoot(cell, dummy));
338                 fl_set_button(longtable_options_->radio_lt_lastfoot,
339                               tabular->GetRowOfLTLastFoot(cell, dummy));
340                 fl_set_button(longtable_options_->radio_lt_newpage,
341                               tabular->GetLTNewPage(cell));
342         } else {
343                 fl_set_button(longtable_options_->radio_lt_firsthead,0);
344                 fl_set_button(longtable_options_->radio_lt_head,0);
345                 fl_set_button(longtable_options_->radio_lt_foot,0);
346                 fl_set_button(longtable_options_->radio_lt_lastfoot,0);
347                 fl_set_button(longtable_options_->radio_lt_newpage,0);
348         }
349         fl_set_button(tabular_options_->radio_rotate_tabular,
350                       tabular->GetRotateTabular());
351 }
352
353 bool FormTabular::input(FL_OBJECT * ob, long)
354 {
355     if (!inset_)
356         return false;
357
358     LyXTabular * tabular = inset_->tabular;
359     int s;
360     LyXTabular::Feature num = LyXTabular::LAST_ACTION;
361     string special;;
362
363     int cell = inset_->GetActCell();
364     if (actCell_ != cell) {
365         update();
366         fl_set_object_label(dialog_->text_warning,
367                      _("Warning: Wrong Cursor position, updated window"));
368         fl_show_object(dialog_->text_warning);
369         return false;
370     }
371     // No point in processing directives that you can't do anything with
372     // anyhow, so exit now if the buffer is read-only.
373     if (lv_->buffer()->isReadonly()) {
374       update();
375       return false;
376     }
377     if (ob == column_options_->input_column_width) {
378         string str = fl_get_input(ob);
379         if (!str.empty() && !isValidLength(str)) {
380             fl_set_object_label(dialog_->text_warning,
381                  _("Warning: Invalid Length (valid example: 10mm)"));
382             fl_show_object(dialog_->text_warning);
383             return false;
384         }
385         inset_->TabularFeatures(lv_->view(), LyXTabular::SET_PWIDTH,str);
386         update(); // update for alignment
387         return true;
388     }
389     if (ob == cell_options_->input_mcolumn_width) {
390         string str = fl_get_input(ob);
391         if (!str.empty() && !isValidLength(str)) {
392             fl_set_object_label(dialog_->text_warning,
393                  _("Warning: Invalid Length (valid example: 10mm)"));
394             fl_show_object(dialog_->text_warning);
395             return false;
396         }
397         inset_->TabularFeatures(lv_->view(), LyXTabular::SET_MPWIDTH,str);
398         update(); // update for alignment
399         return true;
400     }
401     string str = fl_get_input(column_options_->input_column_width);
402     if (!str.empty() && !isValidLength(str)) {
403         fl_set_object_label(
404             dialog_->text_warning,
405             _("Warning: Invalid Length (valid example: 10mm)"));
406         fl_show_object(dialog_->text_warning);
407         return false;
408     }
409     if (ob == tabular_options_->button_append_row)
410         num = LyXTabular::APPEND_ROW;
411     else if (ob == tabular_options_->button_append_column)
412         num = LyXTabular::APPEND_COLUMN;
413     else if (ob == tabular_options_->button_delete_row)
414         num = LyXTabular::DELETE_ROW;
415     else if (ob == tabular_options_->button_delete_column)
416         num = LyXTabular::DELETE_COLUMN;
417     else if (ob == tabular_options_->button_set_borders)
418         num = LyXTabular::SET_ALL_LINES;
419     else if (ob == tabular_options_->button_unset_borders)
420         num = LyXTabular::UNSET_ALL_LINES;
421     else if (ob == column_options_->radio_border_top)
422         num = LyXTabular::TOGGLE_LINE_TOP;
423     else if (ob == column_options_->radio_border_bottom)
424         num = LyXTabular::TOGGLE_LINE_BOTTOM;
425     else if (ob == column_options_->radio_border_left)
426         num = LyXTabular::TOGGLE_LINE_LEFT;
427     else if (ob == column_options_->radio_border_right)
428         num = LyXTabular::TOGGLE_LINE_RIGHT;
429     else if (ob == column_options_->radio_align_left)
430         num = LyXTabular::ALIGN_LEFT;
431     else if (ob == column_options_->radio_align_right)
432         num = LyXTabular::ALIGN_RIGHT;
433     else if (ob == column_options_->radio_align_center)
434         num = LyXTabular::ALIGN_CENTER;
435     else if (ob == column_options_->radio_valign_top)
436         num = LyXTabular::VALIGN_TOP;
437     else if (ob == column_options_->radio_valign_bottom)
438         num = LyXTabular::VALIGN_BOTTOM;
439     else if (ob == column_options_->radio_valign_center)
440         num = LyXTabular::VALIGN_CENTER;
441     else if (ob == cell_options_->radio_multicolumn)
442         num = LyXTabular::MULTICOLUMN;
443     else if (ob == tabular_options_->radio_longtable) {
444             bool const enable =
445                     fl_get_button(tabular_options_->radio_longtable);
446             
447             setEnabled(longtable_options_->radio_lt_firsthead, enable);
448             setEnabled(longtable_options_->radio_lt_head,      enable);
449             setEnabled(longtable_options_->radio_lt_foot,      enable);
450             setEnabled(longtable_options_->radio_lt_lastfoot,  enable);
451             setEnabled(longtable_options_->radio_lt_newpage,   enable);
452
453             if (enable) {
454                     num = LyXTabular::SET_LONGTABULAR;
455                     int dummy;
456                     fl_set_button(longtable_options_->radio_lt_firsthead,
457                                   tabular->GetRowOfLTFirstHead(cell, dummy));
458                     fl_set_button(longtable_options_->radio_lt_head,
459                                   tabular->GetRowOfLTHead(cell, dummy));
460                     fl_set_button(longtable_options_->radio_lt_foot,
461                                   tabular->GetRowOfLTFoot(cell, dummy));
462                     fl_set_button(longtable_options_->radio_lt_lastfoot,
463                                   tabular->GetRowOfLTLastFoot(cell, dummy));
464                     fl_set_button(longtable_options_->radio_lt_firsthead,
465                                   tabular->GetLTNewPage(cell));
466             } else {
467                     num = LyXTabular::UNSET_LONGTABULAR;
468                     fl_set_button(longtable_options_->radio_lt_firsthead,0);
469                     fl_set_button(longtable_options_->radio_lt_head,0);
470                     fl_set_button(longtable_options_->radio_lt_foot,0);
471                     fl_set_button(longtable_options_->radio_lt_lastfoot,0);
472                     fl_set_button(longtable_options_->radio_lt_newpage,0);
473             }
474     } else if (ob == tabular_options_->radio_rotate_tabular) {
475         s = fl_get_button(tabular_options_->radio_rotate_tabular);
476         if (s)
477             num = LyXTabular::SET_ROTATE_TABULAR;
478         else
479             num = LyXTabular::UNSET_ROTATE_TABULAR;
480     } else if (ob == cell_options_->radio_rotate_cell) {
481         s = fl_get_button(cell_options_->radio_rotate_cell);
482         if (s)
483             num = LyXTabular::SET_ROTATE_CELL;
484         else
485             num = LyXTabular::UNSET_ROTATE_CELL;
486     } else if (ob == cell_options_->radio_useminipage) {
487         num = LyXTabular::SET_USEBOX;
488         special = "2";
489     } else if (ob == longtable_options_->radio_lt_firsthead) {
490         num = LyXTabular::SET_LTFIRSTHEAD;
491     } else if (ob == longtable_options_->radio_lt_head) {
492         num = LyXTabular::SET_LTHEAD;
493     } else if (ob == longtable_options_->radio_lt_foot) {
494         num = LyXTabular::SET_LTFOOT;
495     } else if (ob == longtable_options_->radio_lt_lastfoot) {
496         num = LyXTabular::SET_LTLASTFOOT;
497     } else if (ob == longtable_options_->radio_lt_newpage) {
498         num = LyXTabular::SET_LTNEWPAGE;
499     } else if (ob == column_options_->input_special_alignment) {
500         special = fl_get_input(column_options_->input_special_alignment);
501         num = LyXTabular::SET_SPECIAL_COLUMN;
502     } else if (ob == cell_options_->input_special_multialign) {
503         special = fl_get_input(cell_options_->input_special_multialign);
504         num = LyXTabular::SET_SPECIAL_MULTI;
505     } else if (ob == cell_options_->radio_border_top)
506         num = LyXTabular::M_TOGGLE_LINE_TOP;
507     else if (ob == cell_options_->radio_border_bottom)
508         num = LyXTabular::M_TOGGLE_LINE_BOTTOM;
509     else if (ob == cell_options_->radio_border_left)
510         num = LyXTabular::M_TOGGLE_LINE_LEFT;
511     else if (ob == cell_options_->radio_border_right)
512         num = LyXTabular::M_TOGGLE_LINE_RIGHT;
513     else if (ob == cell_options_->radio_align_left)
514         num = LyXTabular::M_ALIGN_LEFT;
515     else if (ob == cell_options_->radio_align_right)
516         num = LyXTabular::M_ALIGN_RIGHT;
517     else if (ob == cell_options_->radio_align_center)
518         num = LyXTabular::M_ALIGN_CENTER;
519     else if (ob == cell_options_->radio_valign_top)
520         num = LyXTabular::M_VALIGN_TOP;
521     else if (ob == cell_options_->radio_valign_bottom)
522         num = LyXTabular::M_VALIGN_BOTTOM;
523     else if (ob == cell_options_->radio_valign_center)
524         num = LyXTabular::M_VALIGN_CENTER;
525     else
526         return false;
527     
528     inset_->TabularFeatures(lv_->view(), num, special);
529     update();
530
531     return true;
532 }