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