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