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