]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormTabular.C
5eda1408ad5a36be9c6b33b76963804eefa1d6fb
[lyx.git] / src / frontends / xforms / FormTabular.C
1 /* FormTabular.C
2  * FormTabular Interface Class Implementation
3  */
4
5 #include <config.h>
6 #include "lyx_gui_misc.h"
7 #include "gettext.h"
8 #include FORMS_H_LOCATION
9
10 #include "FormTabular.h"
11 #include "xform_macros.h"
12 #include "input_validators.h"
13 #include "LyXView.h"
14 #include "lyxfunc.h"
15 #include "Dialogs.h"
16 #include "lyxrc.h"
17 #include "debug.h"
18 #include "insets/insettabular.h"
19 #include "buffer.h"
20
21
22 #ifdef SIGC_CXX_NAMESPACES
23 using SigC::slot;
24 #endif
25
26 C_RETURNCB(FormTabular,  WMHideCB)
27 C_GENERICCB(FormTabular, CloseCB)
28 C_GENERICCB(FormTabular, InputCB)
29 C_GENERICCB(FormTabular, OKCB)
30 C_GENERICCB(FormTabular, ApplyCB)
31 C_GENERICCB(FormTabular, CancelCB)
32
33
34 FormTabular::FormTabular(LyXView * lv, Dialogs * d)
35         : dialog_(0), tabular_options_(0), column_options_(0),
36           cell_options_(0), longtable_options_(0), create_tabular_(0),
37           lv_(lv), d_(d), u_(0), h_(0), inset_(0), actCell_(-1) 
38 {
39     // let the dialog be shown
40     // This is a permanent connection so we won't bother
41     // storing a copy because we won't be disconnecting.
42     d->showTabular.connect(slot(this, &FormTabular::showInset));
43     d->updateTabular.connect(slot(this, &FormTabular::updateInset));
44     d->hideTabular.connect(slot(this, &FormTabular::hideInset));
45     d->showTabularCreate.connect(slot(this, &FormTabular::show_create));
46 }
47
48
49 FormTabular::~FormTabular()
50 {
51     free();
52 }
53
54
55 void FormTabular::build()
56 {
57     dialog_ = build_tabular();
58     tabular_options_ = build_tabular_options();
59     column_options_ = build_column_options();
60     cell_options_ = build_cell_options();
61     longtable_options_ = build_longtable_options();
62     create_tabular_ = build_create_tabular();
63
64     fl_set_input_return(column_options_->input_column_width,
65                         FL_RETURN_CHANGED);
66     fl_set_input_return(column_options_->input_special_alignment,
67                         FL_RETURN_CHANGED);
68     fl_set_input_return(cell_options_->input_mcolumn_width,
69                         FL_RETURN_CHANGED);
70     fl_set_input_return(cell_options_->input_special_multialign,
71                         FL_RETURN_CHANGED);
72
73     fl_addto_tabfolder(dialog_->tabFolder, _("Tabular"),
74                        tabular_options_->form_tabular_options);
75     fl_addto_tabfolder(dialog_->tabFolder, _("Column/Row"),
76                        column_options_->form_column_options);
77     fl_addto_tabfolder(dialog_->tabFolder, _("Cell"),
78                        cell_options_->form_cell_options);
79     fl_addto_tabfolder(dialog_->tabFolder, _("LongTable"),
80                        longtable_options_->form_longtable_options);
81     
82     fl_set_form_atclose(dialog_->form_tabular,
83                         C_FormTabularWMHideCB, 0);
84
85     fl_set_slider_bounds(create_tabular_->slider_rows, 1, 50);
86     fl_set_slider_bounds(create_tabular_->slider_columns, 1, 50);
87     fl_set_slider_value(create_tabular_->slider_rows, 5);
88     fl_set_slider_value(create_tabular_->slider_columns, 5);
89     fl_set_slider_precision(create_tabular_->slider_rows, 0);
90     fl_set_slider_precision(create_tabular_->slider_columns, 0);
91     fl_set_form_atclose(create_tabular_->form_create_tabular, 
92                         C_FormTabularWMHideCB, 0);
93 }
94
95
96 void FormTabular::show()
97 {
98     if (!dialog_) {
99         build();
100     }
101     update();  // make sure its up-to-date
102
103     if (dialog_->form_tabular->visible) {
104         fl_raise_form(dialog_->form_tabular);
105     } else {
106         fl_show_form(dialog_->form_tabular,
107                      FL_PLACE_MOUSE | FL_FREE_SIZE,
108                      FL_TRANSIENT,
109                      _("Tabular Layout"));
110 //      u_ = d_->updateBufferDependent.connect(slot(this,
111 //                                                  &FormTabular::update));
112         h_ = d_->hideBufferDependent.connect(slot(this,
113                                                   &FormTabular::hide));
114     }
115 }
116
117 void FormTabular::showInset(InsetTabular * ti)
118 {
119     inset_ = ti;
120     if (ti) {
121         show();
122     }
123 }
124
125 void FormTabular::hide()
126 {
127     if (dialog_ && dialog_->form_tabular && dialog_->form_tabular->visible) {
128         fl_hide_form(dialog_->form_tabular);
129         u_.disconnect();
130         h_.disconnect();
131         inset_ = 0;
132     }
133 }
134
135
136 void FormTabular::hideInset(InsetTabular * ti)
137 {
138     if (inset_ == ti) {
139         inset_ = 0;
140         hide();
141     }
142 }
143
144
145 void FormTabular::apply()
146 {
147 }
148
149
150 void FormTabular::update()
151 {
152     if (dialog_) {
153         local_update(true);
154     }
155 }
156
157 void FormTabular::updateInset(InsetTabular * ti)
158 {
159     inset_ = ti;
160     if (ti && dialog_ && dialog_->form_tabular->visible) {
161         update();
162     }
163 }
164
165 void FormTabular::input()
166 {
167     bool activate = true;
168     //
169     // whatever checks you need
170     //
171     if (activate) {
172     } else {
173     }
174 }
175
176
177 void FormTabular::free()
178 {
179     // we don't need to delete u and h here because
180     // hide() does that after disconnecting.
181     if (dialog_) {
182         if (dialog_->form_tabular
183             && dialog_->form_tabular->visible) {
184             hide();
185         }
186         fl_free_form(dialog_->form_tabular);
187         delete dialog_;
188         dialog_ = 0;
189     }
190 }
191
192
193 int FormTabular::WMHideCB(FL_FORM * form, void *)
194 {
195     // Ensure that the signals (u and h) are disconnected even if the
196     // window manager is used to close the dialog.
197     FormTabular * pre = static_cast<FormTabular*>(form->u_vdata);
198     pre->hide();
199     return FL_CANCEL;
200 }
201
202
203 void FormTabular::CloseCB(FL_OBJECT * ob, long)
204 {
205     FormTabular * pre = static_cast<FormTabular*>(ob->form->u_vdata);
206     pre->hide();
207 }
208
209
210 void FormTabular::InputCB(FL_OBJECT * ob, long)
211 {
212     FormTabular * pre = static_cast<FormTabular*>(ob->form->u_vdata);
213     pre->input();
214 }
215
216
217 bool FormTabular::local_update(bool flag)
218 {
219     if (!inset_ || !inset_->tabular)
220         return false;
221
222     LyXTabular
223         * tabular = inset_->tabular;
224     int
225         align,
226         cell,
227         column,row;
228     char
229         buf[12];
230     string
231         pwidth, special;
232
233     actCell_ = cell = inset_->GetActCell();
234     column = tabular->column_of_cell(cell)+1;
235     fl_set_object_label(dialog_->text_warning,"");
236     confirmed_ = false;
237     fl_activate_object(column_options_->input_special_alignment);
238     fl_activate_object(cell_options_->input_special_multialign);
239     fl_activate_object(column_options_->input_column_width);
240     sprintf(buf,"%d",column);
241     fl_set_input(dialog_->input_tabular_column, buf);
242     fl_deactivate_object(dialog_->input_tabular_column);
243     row = tabular->row_of_cell(cell)+1;
244     sprintf(buf,"%d",row);
245     fl_set_input(dialog_->input_tabular_row, buf);
246     fl_deactivate_object(dialog_->input_tabular_row);
247     if (tabular->IsMultiColumn(cell))
248         fl_set_button(cell_options_->radio_multicolumn, 1);
249     else
250         fl_set_button(cell_options_->radio_multicolumn, 0);
251     if (tabular->GetRotateCell(cell))
252         fl_set_button(cell_options_->radio_rotate_cell, 1);
253     else
254         fl_set_button(cell_options_->radio_rotate_cell, 0);
255     if (tabular->TopLine(cell))
256         fl_set_button(column_options_->radio_border_top, 1);
257     else
258         fl_set_button(column_options_->radio_border_top, 0);
259     if (tabular->BottomLine(cell))
260         fl_set_button(column_options_->radio_border_bottom, 1);
261     else
262         fl_set_button(column_options_->radio_border_bottom, 0);
263     if (tabular->LeftLine(cell))
264         fl_set_button(column_options_->radio_border_left, 1);
265     else
266         fl_set_button(column_options_->radio_border_left, 0);
267     if (tabular->RightLine(cell))
268         fl_set_button(column_options_->radio_border_right, 1);
269     else
270         fl_set_button(column_options_->radio_border_right, 0);
271     align = tabular->GetAlignment(cell);
272     fl_set_button(column_options_->radio_align_left, 0);
273     fl_set_button(column_options_->radio_align_right, 0);
274     fl_set_button(column_options_->radio_align_center, 0);
275     special = tabular->GetAlignSpecial(cell,LyXTabular::SET_SPECIAL_COLUMN);
276     if (flag)
277         fl_set_input(column_options_->input_special_alignment,
278                      special.c_str());
279     if (lv_->buffer()->isReadonly()) 
280         fl_deactivate_object(column_options_->input_special_alignment);
281     special = tabular->GetAlignSpecial(cell,LyXTabular::SET_SPECIAL_MULTI);
282     if (flag)
283         fl_set_input(cell_options_->input_special_multialign, special.c_str());
284     if (lv_->buffer()->isReadonly()) 
285         fl_deactivate_object(cell_options_->input_special_multialign);
286     pwidth = tabular->GetPWidth(cell);
287     if (flag)
288         fl_set_input(column_options_->input_column_width,pwidth.c_str());
289     if (lv_->buffer()->isReadonly()) {
290         fl_deactivate_object(column_options_->input_column_width);
291     } else {
292         fl_activate_object(column_options_->input_column_width);
293     }
294     if (!pwidth.empty()) {
295         fl_activate_object(cell_options_->radio_linebreak_cell);
296         fl_set_object_lcol(cell_options_->radio_linebreak_cell, FL_BLACK);
297         fl_set_button(cell_options_->radio_linebreak_cell,
298                       tabular->GetLinebreaks(cell));
299     } else {
300         fl_deactivate_object(cell_options_->radio_linebreak_cell);
301         fl_set_object_lcol(cell_options_->radio_linebreak_cell, FL_INACTIVE);
302         fl_set_button(cell_options_->radio_linebreak_cell,0);
303     }
304     if ((!pwidth.empty() && !tabular->IsMultiColumn(cell)) ||
305         (align == LYX_ALIGN_LEFT))
306         fl_set_button(column_options_->radio_align_left, 1);
307     else if (align == LYX_ALIGN_RIGHT)
308         fl_set_button(column_options_->radio_align_right, 1);
309     else
310         fl_set_button(column_options_->radio_align_center, 1);
311     if (!pwidth.empty() && !tabular->IsMultiColumn(cell)) {
312         fl_deactivate_object(column_options_->radio_align_left);
313         fl_deactivate_object(column_options_->radio_align_right);
314         fl_deactivate_object(column_options_->radio_align_center);
315         fl_set_object_lcol(column_options_->radio_align_left, FL_INACTIVE);
316         fl_set_object_lcol(column_options_->radio_align_right, FL_INACTIVE);
317         fl_set_object_lcol(column_options_->radio_align_center, FL_INACTIVE);
318     } else {
319         fl_activate_object(column_options_->radio_align_left);
320         fl_activate_object(column_options_->radio_align_right);
321         fl_activate_object(column_options_->radio_align_center);
322         fl_set_object_lcol(column_options_->radio_align_left, FL_BLACK);
323         fl_set_object_lcol(column_options_->radio_align_right, FL_BLACK);
324         fl_set_object_lcol(column_options_->radio_align_center, FL_BLACK);
325     }
326     fl_set_button(tabular_options_->radio_longtable,
327                   tabular->IsLongTabular());
328     if (tabular->IsLongTabular()) {
329         fl_activate_object(longtable_options_->radio_lt_firsthead);
330         fl_activate_object(longtable_options_->radio_lt_head);
331         fl_activate_object(longtable_options_->radio_lt_foot);
332         fl_activate_object(longtable_options_->radio_lt_lastfoot);
333         fl_activate_object(longtable_options_->radio_lt_newpage);
334         fl_set_object_lcol(longtable_options_->radio_lt_firsthead, FL_BLACK);
335         fl_set_object_lcol(longtable_options_->radio_lt_head, FL_BLACK);
336         fl_set_object_lcol(longtable_options_->radio_lt_foot, FL_BLACK);
337         fl_set_object_lcol(longtable_options_->radio_lt_lastfoot, FL_BLACK);
338         fl_set_object_lcol(longtable_options_->radio_lt_newpage, FL_BLACK);
339         fl_set_button(longtable_options_->radio_lt_firsthead,
340                       tabular->GetRowOfLTFirstHead(cell));
341         fl_set_button(longtable_options_->radio_lt_head,
342                       tabular->GetRowOfLTHead(cell));
343         fl_set_button(longtable_options_->radio_lt_foot,
344                       tabular->GetRowOfLTFoot(cell));
345         fl_set_button(longtable_options_->radio_lt_lastfoot,
346                       tabular->GetRowOfLTLastFoot(cell));
347         fl_set_button(longtable_options_->radio_lt_newpage,
348                       tabular->GetLTNewPage(cell));
349     } else {
350         fl_deactivate_object(longtable_options_->radio_lt_firsthead);
351         fl_deactivate_object(longtable_options_->radio_lt_head);
352         fl_deactivate_object(longtable_options_->radio_lt_foot);
353         fl_deactivate_object(longtable_options_->radio_lt_lastfoot);
354         fl_deactivate_object(longtable_options_->radio_lt_newpage);
355         fl_set_button(longtable_options_->radio_lt_firsthead,0);
356         fl_set_button(longtable_options_->radio_lt_head,0);
357         fl_set_button(longtable_options_->radio_lt_foot,0);
358         fl_set_button(longtable_options_->radio_lt_lastfoot,0);
359         fl_set_button(longtable_options_->radio_lt_newpage,0);
360         fl_set_object_lcol(longtable_options_->radio_lt_firsthead,FL_INACTIVE);
361         fl_set_object_lcol(longtable_options_->radio_lt_head, FL_INACTIVE);
362         fl_set_object_lcol(longtable_options_->radio_lt_foot, FL_INACTIVE);
363         fl_set_object_lcol(longtable_options_->radio_lt_lastfoot, FL_INACTIVE);
364         fl_set_object_lcol(longtable_options_->radio_lt_newpage, FL_INACTIVE);
365     }
366     fl_set_button(tabular_options_->radio_rotate_tabular,
367                   tabular->GetRotateTabular());
368     return true;
369 }
370
371 // +-----------------------------------------------------------------------+
372 // |          Functions/Dialogs for creating tabular insets                |
373 // +-----------------------------------------------------------------------+
374
375 void FormTabular::show_create()
376 {
377     if (!dialog_) {
378         build();
379     }
380     if (create_tabular_->form_create_tabular->visible) {
381         fl_raise_form(create_tabular_->form_create_tabular);
382     } else {
383         fl_show_form(create_tabular_->form_create_tabular,
384                      FL_PLACE_MOUSE | FL_FREE_SIZE,
385                      FL_FULLBORDER, _("Insert Tabular"));
386     }
387 }
388
389
390 void FormTabular::hide_create()
391 {
392     if (create_tabular_->form_create_tabular)
393         fl_hide_form(create_tabular_->form_create_tabular);
394 }
395
396
397 void FormTabular::apply_create()
398 {
399     int
400         xsize,ysize;
401
402 //    comm->setMinibuffer(_("Inserting tabular inset..."));
403     ysize = int(fl_get_slider_value(create_tabular_->slider_columns) + 0.5);
404     xsize = int(fl_get_slider_value(create_tabular_->slider_rows) + 0.5);
405     InsetTabular * in = new InsetTabular(lv_->buffer(),xsize,ysize);
406     if (!lv_->view()->open_new_inset(in)) {
407         delete in;
408     }
409 //    comm->setMinibuffer(_("Tabular mode"));
410 }
411
412
413 void FormTabular::OKCB(FL_OBJECT * ob, long)
414 {
415     FormTabular * pre = (FormTabular*)ob->form->u_vdata;
416     pre->apply_create();
417     pre->hide_create();
418 }
419
420
421 void FormTabular::ApplyCB(FL_OBJECT * ob, long)
422 {
423     FormTabular * pre = (FormTabular*)ob->form->u_vdata;
424     pre->apply_create();
425 }
426
427
428 void FormTabular::CancelCB(FL_OBJECT * ob, long)
429 {
430     FormTabular * pre = (FormTabular*)ob->form->u_vdata;
431     pre->hide_create();
432 }
433
434