]> git.lyx.org Git - lyx.git/blob - src/TabularLayout.C
some small changes
[lyx.git] / src / TabularLayout.C
1 #include <config.h>
2
3 #include <cstdlib>
4 #include FORMS_H_LOCATION
5
6 #include "layout_forms.h"
7 #include "lyx_main.h"
8 #include "lyxrc.h"
9 #include "LString.h"
10 #include "support/filetools.h"
11 #include "buffer.h"
12 #include "vspace.h"
13 #include "lyx_gui_misc.h"
14 #include "BufferView.h"
15 #include "gettext.h"
16 #include "layout.h"
17 #include "tabular.h"
18 #include "insets/insettabular.h"
19
20
21 // Prototypes
22 extern FD_form_table_options * fd_form_table_options;
23 extern FD_form_table_extra * fd_form_table_extra;
24 extern BufferView * current_view;
25
26 extern void OpenLayoutTableExtra();
27 extern bool UpdateLayoutTabular(bool, InsetTabular *);
28
29 static int Confirmed = false;
30 static int ActCell;
31
32 // hack to keep the cursor from jumping to the end of the text in the Extra
33 // form input fields during editing. The values in LyXTabular itself is
34 // changed in real-time, but we have no callbacks for the input fields,
35 // so I simply store and restore the cursor position for now.
36 // (too much of a hazzle to do it proper; we'll trash all this code
37 // in 1.1 anyway)
38 static int extra_col_cursor_x; // need no y's, one-line input fields
39 static int extra_multicol_cursor_x;
40 // Joacim
41
42 static InsetTabular * inset = 0;
43
44 void MenuLayoutTabular(bool flag, InsetTabular * ins)
45 {
46     inset = ins;
47     if (!inset)
48         return;
49     if (UpdateLayoutTabular(flag, ins)) {
50         if (fd_form_table_options->form_table_options->visible) {
51             fl_raise_form(fd_form_table_options->form_table_options);
52         }
53         else {
54             fl_show_form(fd_form_table_options->form_table_options,
55                          FL_PLACE_MOUSE, FL_FULLBORDER,
56                          _("Tabular Layout"));
57         }
58     }
59 }
60
61 bool UpdateLayoutTabular(bool flag, InsetTabular *ins)
62 {
63     if (!ins)
64         return false;
65
66     inset = ins;
67
68     bool update = true;
69     if (!current_view->available())
70         update = false;
71     
72     if (update) {
73         char buf[12];
74         string pwidth, special;
75    
76         LyXTabular * table = inset->tabular;
77
78         int cell = inset->GetActCell();
79         ActCell = cell;
80         int column = table->column_of_cell(cell)+1;
81         fl_set_object_label(fd_form_table_options->text_warning, "");
82         Confirmed = false;
83         fl_activate_object(fd_form_table_extra->input_special_alignment);
84         fl_activate_object(fd_form_table_extra->input_special_multialign);
85         fl_activate_object(fd_form_table_options->input_column_width);
86         sprintf(buf, "%d", column);
87         fl_set_input(fd_form_table_options->input_table_column, buf);
88         fl_deactivate_object(fd_form_table_options->input_table_column);
89         int row = table->row_of_cell(cell)+1;
90         sprintf(buf, "%d", row);
91         fl_set_input(fd_form_table_options->input_table_row, buf);
92         fl_deactivate_object(fd_form_table_options->input_table_row);
93         if (table->IsMultiColumn(cell))
94             fl_set_button(fd_form_table_options->radio_multicolumn, 1);
95         else
96             fl_set_button(fd_form_table_options->radio_multicolumn, 0);
97         if (table->GetRotateCell(cell))
98             fl_set_button(fd_form_table_options->radio_rotate_cell, 1);
99         else
100             fl_set_button(fd_form_table_options->radio_rotate_cell, 0);
101         if (table->TopLine(cell))
102             fl_set_button(fd_form_table_options->radio_border_top, 1);
103         else
104             fl_set_button(fd_form_table_options->radio_border_top, 0);
105         if (table->BottomLine(cell))
106             fl_set_button(fd_form_table_options->radio_border_bottom, 1);
107         else
108             fl_set_button(fd_form_table_options->radio_border_bottom, 0);
109         if (table->LeftLine(cell))
110             fl_set_button(fd_form_table_options->radio_border_left, 1);
111         else
112             fl_set_button(fd_form_table_options->radio_border_left, 0);
113         if (table->RightLine(cell))
114             fl_set_button(fd_form_table_options->radio_border_right, 1);
115         else
116             fl_set_button(fd_form_table_options->radio_border_right, 0);
117         int align = table->GetAlignment(cell);
118         fl_set_button(fd_form_table_options->radio_align_left, 0);
119         fl_set_button(fd_form_table_options->radio_align_right, 0);
120         fl_set_button(fd_form_table_options->radio_align_center, 0);
121         special = table->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
122         if (flag)
123         {
124             fl_set_input(fd_form_table_extra->input_special_alignment,
125                          special.c_str());
126             fl_set_input_cursorpos(fd_form_table_extra->input_special_alignment,
127                     extra_col_cursor_x, 0); // restore the cursor
128         }
129         if (current_view->buffer()->isReadonly()) 
130             fl_deactivate_object(fd_form_table_extra->input_special_alignment);
131         special = table->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
132         if (flag)
133         {
134             fl_set_input(fd_form_table_extra->input_special_multialign,
135                          special.c_str());
136             fl_set_input_cursorpos(fd_form_table_extra->input_special_multialign,
137                     extra_multicol_cursor_x, 0); // restore the cursor
138         }
139         if (current_view->buffer()->isReadonly()) 
140             fl_deactivate_object(fd_form_table_extra->input_special_multialign);
141         pwidth = table->GetPWidth(cell);
142         if (flag)
143             fl_set_input(fd_form_table_options->input_column_width, pwidth.c_str());
144         if (current_view->buffer()->isReadonly()) 
145             fl_deactivate_object(fd_form_table_options->input_column_width);
146         if (!pwidth.empty()) {
147             fl_activate_object(fd_form_table_options->radio_linebreak_cell);
148             fl_set_object_lcol(fd_form_table_options->radio_linebreak_cell,
149                                FL_BLACK);
150             fl_set_button(fd_form_table_options->radio_linebreak_cell,
151                           table->GetLinebreaks(cell));
152         } else {
153             fl_deactivate_object(fd_form_table_options->radio_linebreak_cell);
154             fl_set_object_lcol(fd_form_table_options->radio_linebreak_cell,
155                                FL_INACTIVE);
156             fl_set_button(fd_form_table_options->radio_linebreak_cell, 0);
157         }
158         if ((!pwidth.empty() && !table->IsMultiColumn(cell)) ||
159             (align == LYX_ALIGN_LEFT))
160             fl_set_button(fd_form_table_options->radio_align_left, 1);
161         else if (align == LYX_ALIGN_RIGHT)
162             fl_set_button(fd_form_table_options->radio_align_right, 1);
163         else
164             fl_set_button(fd_form_table_options->radio_align_center, 1);
165         if (!pwidth.empty() && !table->IsMultiColumn(cell)) {
166             fl_deactivate_object(fd_form_table_options->radio_align_left);
167             fl_deactivate_object(fd_form_table_options->radio_align_right);
168             fl_deactivate_object(fd_form_table_options->radio_align_center);
169             fl_set_object_lcol(fd_form_table_options->radio_align_left,
170                                FL_INACTIVE);
171             fl_set_object_lcol(fd_form_table_options->radio_align_right,
172                                FL_INACTIVE);
173             fl_set_object_lcol(fd_form_table_options->radio_align_center,
174                                FL_INACTIVE);
175         } else {
176             fl_activate_object(fd_form_table_options->radio_align_left);
177             fl_activate_object(fd_form_table_options->radio_align_right);
178             fl_activate_object(fd_form_table_options->radio_align_center);
179             fl_set_object_lcol(fd_form_table_options->radio_align_left,
180                                FL_BLACK);
181             fl_set_object_lcol(fd_form_table_options->radio_align_right,
182                                FL_BLACK);
183             fl_set_object_lcol(fd_form_table_options->radio_align_center,
184                                FL_BLACK);
185         }
186         fl_set_button(fd_form_table_options->radio_longtable, table->IsLongTabular());
187         if (table->IsLongTabular()) {
188             fl_activate_object(fd_form_table_options->radio_lt_firsthead);
189             fl_activate_object(fd_form_table_options->radio_lt_head);
190             fl_activate_object(fd_form_table_options->radio_lt_foot);
191             fl_activate_object(fd_form_table_options->radio_lt_lastfoot);
192             fl_activate_object(fd_form_table_options->radio_lt_newpage);
193             fl_set_object_lcol(fd_form_table_options->radio_lt_firsthead,
194                                FL_BLACK);
195             fl_set_object_lcol(fd_form_table_options->radio_lt_head,
196                                FL_BLACK);
197             fl_set_object_lcol(fd_form_table_options->radio_lt_foot,
198                                FL_BLACK);
199             fl_set_object_lcol(fd_form_table_options->radio_lt_lastfoot,
200                                FL_BLACK);
201             fl_set_object_lcol(fd_form_table_options->radio_lt_newpage,
202                                FL_BLACK);
203             fl_set_button(fd_form_table_options->radio_lt_firsthead,
204                           table->GetRowOfLTFirstHead(cell));
205             fl_set_button(fd_form_table_options->radio_lt_head,
206                           table->GetRowOfLTHead(cell));
207             fl_set_button(fd_form_table_options->radio_lt_foot,
208                           table->GetRowOfLTFoot(cell));
209             fl_set_button(fd_form_table_options->radio_lt_lastfoot,
210                           table->GetRowOfLTLastFoot(cell));
211             fl_set_button(fd_form_table_options->radio_lt_newpage,
212                           table->GetLTNewPage(cell));
213         } else {
214             fl_deactivate_object(fd_form_table_options->radio_lt_firsthead);
215             fl_deactivate_object(fd_form_table_options->radio_lt_head);
216             fl_deactivate_object(fd_form_table_options->radio_lt_foot);
217             fl_deactivate_object(fd_form_table_options->radio_lt_lastfoot);
218             fl_deactivate_object(fd_form_table_options->radio_lt_newpage);
219             fl_set_button(fd_form_table_options->radio_lt_firsthead, 0);
220             fl_set_button(fd_form_table_options->radio_lt_head, 0);
221             fl_set_button(fd_form_table_options->radio_lt_foot, 0);
222             fl_set_button(fd_form_table_options->radio_lt_lastfoot, 0);
223             fl_set_button(fd_form_table_options->radio_lt_newpage, 0);
224             fl_set_object_lcol(fd_form_table_options->radio_lt_firsthead,
225                                FL_INACTIVE);
226             fl_set_object_lcol(fd_form_table_options->radio_lt_head,
227                                FL_INACTIVE);
228             fl_set_object_lcol(fd_form_table_options->radio_lt_foot,
229                                FL_INACTIVE);
230             fl_set_object_lcol(fd_form_table_options->radio_lt_lastfoot,
231                                FL_INACTIVE);
232             fl_set_object_lcol(fd_form_table_options->radio_lt_newpage,
233                                FL_INACTIVE);
234         }
235         fl_set_button(fd_form_table_options->radio_rotate_table,
236                       table->GetRotateTabular());
237         fl_set_focus_object(fd_form_table_options->form_table_options,
238                             fd_form_table_options->button_table_delete);
239     } else if (fd_form_table_options->form_table_options->visible) {
240         fl_set_focus_object(fd_form_table_options->form_table_options,
241                             fd_form_table_options->button_table_delete);
242         fl_hide_form(fd_form_table_options->form_table_options);
243     }
244     return update;
245 }
246
247
248 void TabularOptionsCB(FL_OBJECT * ob, long)
249 {
250     if (!inset)
251         return;
252
253     int s, num = 0;
254     string special, str;
255
256     LyXTabular * table = inset->tabular;
257     
258     int cell = inset->GetActCell();
259     if (ActCell != cell) {
260         MenuLayoutTabular(false, inset);
261         fl_set_object_label(fd_form_table_options->text_warning,
262                           _("Warning: Wrong Cursor position, updated window"));
263         fl_show_object(fd_form_table_options->text_warning);
264         extra_col_cursor_x = 0; // would rather place it at the end, but...
265         extra_multicol_cursor_x = 0;
266         return;
267     }
268
269     // No point in processing directives that you can't do anything with
270     // anyhow, so exit now if the buffer is read-only.
271     if (current_view->buffer()->isReadonly()) {
272       MenuLayoutTabular(false, inset);
273       return;
274     }
275     
276     if (ob != fd_form_table_options->button_table_delete) {
277         fl_set_object_label(fd_form_table_options->text_warning, "");
278         Confirmed = false;
279     }
280     str = fl_get_input(fd_form_table_options->input_column_width);
281     if (!str.empty() && !isValidLength(str)) {
282         fl_set_object_label(fd_form_table_options->text_warning,
283                             _("Warning: Invalid Length (valid example: 10mm)"));
284         fl_show_object(fd_form_table_options->text_warning);
285         return;
286     }
287     if (((ob==fd_form_table_options->button_delete_row)&&(table->rows()<=1)) ||
288         ((ob==fd_form_table_options->button_delete_column)&&(table->columns()<=1)))
289         ob = fd_form_table_options->button_table_delete;
290     if (ob == fd_form_table_options->button_append_row)
291         num = LyXTabular::APPEND_ROW;
292     else if (ob == fd_form_table_options->button_append_column)
293         num = LyXTabular::APPEND_COLUMN;
294     else if (ob == fd_form_table_options->button_delete_row)
295         num = LyXTabular::DELETE_ROW;
296     else if (ob == fd_form_table_options->button_delete_column)
297         num = LyXTabular::DELETE_COLUMN;
298     else if (ob == fd_form_table_options->button_set_borders)
299         num = LyXTabular::SET_ALL_LINES;
300     else if (ob == fd_form_table_options->button_unset_borders)
301         num = LyXTabular::UNSET_ALL_LINES;
302     else if (ob == fd_form_table_options->radio_border_top)
303         num = LyXTabular::TOGGLE_LINE_TOP;
304     else if (ob == fd_form_table_options->radio_border_bottom)
305         num = LyXTabular::TOGGLE_LINE_BOTTOM;
306     else if (ob == fd_form_table_options->radio_border_left)
307         num = LyXTabular::TOGGLE_LINE_LEFT;
308     else if (ob == fd_form_table_options->radio_border_right)
309         num = LyXTabular::TOGGLE_LINE_RIGHT;
310     else if (ob == fd_form_table_options->radio_align_left)
311         num = LyXTabular::ALIGN_LEFT;
312     else if (ob == fd_form_table_options->radio_align_right)
313         num = LyXTabular::ALIGN_RIGHT;
314     else if (ob == fd_form_table_options->radio_align_center)
315         num = LyXTabular::ALIGN_CENTER;
316     else if ((ob == fd_form_table_options->button_table_delete) && !Confirmed) {
317         fl_set_object_label(fd_form_table_options->text_warning,
318                             _("Confirm: press Delete-Button again"));
319         Confirmed = true;
320         return;
321     } else if ((ob == fd_form_table_options->button_table_delete) 
322                && Confirmed) {
323         num = LyXTabular::DELETE_TABULAR;
324         Confirmed = false;
325     } else if (ob == fd_form_table_options->radio_multicolumn)
326         num = LyXTabular::MULTICOLUMN;
327     else if (ob == fd_form_table_options->radio_longtable) {
328         s = fl_get_button(fd_form_table_options->radio_longtable);
329         if (s) {
330             num = LyXTabular::SET_LONGTABULAR;
331             fl_activate_object(fd_form_table_options->radio_lt_firsthead);
332             fl_activate_object(fd_form_table_options->radio_lt_head);
333             fl_activate_object(fd_form_table_options->radio_lt_foot);
334             fl_activate_object(fd_form_table_options->radio_lt_lastfoot);
335             fl_activate_object(fd_form_table_options->radio_lt_newpage);
336             fl_set_button(fd_form_table_options->radio_lt_firsthead,
337                           table->GetRowOfLTFirstHead(cell));
338             fl_set_button(fd_form_table_options->radio_lt_head,
339                           table->GetRowOfLTHead(cell));
340             fl_set_button(fd_form_table_options->radio_lt_foot,
341                           table->GetRowOfLTFoot(cell));
342             fl_set_button(fd_form_table_options->radio_lt_lastfoot,
343                           table->GetRowOfLTLastFoot(cell));
344             fl_set_button(fd_form_table_options->radio_lt_firsthead,
345                           table->GetLTNewPage(cell));
346         } else {
347             num = LyXTabular::UNSET_LONGTABULAR;
348             fl_deactivate_object(fd_form_table_options->radio_lt_firsthead);
349             fl_deactivate_object(fd_form_table_options->radio_lt_head);
350             fl_deactivate_object(fd_form_table_options->radio_lt_foot);
351             fl_deactivate_object(fd_form_table_options->radio_lt_lastfoot);
352             fl_deactivate_object(fd_form_table_options->radio_lt_newpage);
353             fl_set_button(fd_form_table_options->radio_lt_firsthead, 0);
354             fl_set_button(fd_form_table_options->radio_lt_firsthead, 0);
355             fl_set_button(fd_form_table_options->radio_lt_firsthead, 0);
356             fl_set_button(fd_form_table_options->radio_lt_firsthead, 0);
357             fl_set_button(fd_form_table_options->radio_lt_firsthead, 0);
358             fl_set_object_lcol(fd_form_table_options->radio_lt_firsthead,
359                                FL_INACTIVE);
360             fl_set_object_lcol(fd_form_table_options->radio_lt_head,
361                                FL_INACTIVE);
362             fl_set_object_lcol(fd_form_table_options->radio_lt_foot,
363                                FL_INACTIVE);
364             fl_set_object_lcol(fd_form_table_options->radio_lt_lastfoot,
365                                FL_INACTIVE);
366             fl_set_object_lcol(fd_form_table_options->radio_lt_newpage,
367                                FL_INACTIVE);
368         }
369     } else if (ob == fd_form_table_options->radio_rotate_table) {
370         s = fl_get_button(fd_form_table_options->radio_rotate_table);
371         if (s)
372             num = LyXTabular::SET_ROTATE_TABULAR;
373         else
374             num = LyXTabular::UNSET_ROTATE_TABULAR;
375     } else if (ob == fd_form_table_options->radio_rotate_cell) {
376         s = fl_get_button(fd_form_table_options->radio_rotate_cell);
377         if (s)
378             num = LyXTabular::SET_ROTATE_CELL;
379         else
380             num = LyXTabular::UNSET_ROTATE_CELL;
381     } else if (ob == fd_form_table_options->radio_linebreak_cell) {
382         num = LyXTabular::SET_LINEBREAKS;
383     } else if (ob == fd_form_table_options->radio_lt_firsthead) {
384         num = LyXTabular::SET_LTFIRSTHEAD;
385     } else if (ob == fd_form_table_options->radio_lt_head) {
386         num = LyXTabular::SET_LTHEAD;
387     } else if (ob == fd_form_table_options->radio_lt_foot) {
388         num = LyXTabular::SET_LTFOOT;
389     } else if (ob == fd_form_table_options->radio_lt_lastfoot) {
390         num = LyXTabular::SET_LTLASTFOOT;
391     } else if (ob == fd_form_table_options->radio_lt_newpage) {
392         num = LyXTabular::SET_LTNEWPAGE;
393     } else if (ob == fd_form_table_options->button_table_extra) {
394         OpenLayoutTableExtra();
395         return;
396     } else if (ob == fd_form_table_extra->input_special_alignment) {
397         special = fl_get_input(fd_form_table_extra->input_special_alignment);
398         int dummy;
399         fl_get_input_cursorpos(ob, &extra_col_cursor_x, &dummy);
400         num = LyXTabular::SET_SPECIAL_COLUMN;
401     } else if (ob == fd_form_table_extra->input_special_multialign) {
402         special = fl_get_input(fd_form_table_extra->input_special_multialign);
403         int dummy;
404         fl_get_input_cursorpos(ob, &extra_multicol_cursor_x, &dummy);
405         num = LyXTabular::SET_SPECIAL_MULTI;
406     } else
407         return;
408     if (current_view->available()) {
409         current_view->hideCursor();
410         inset->TabularFeatures(current_view, num, special);
411     }
412     if (num == LyXTabular::DELETE_TABULAR) {
413         fl_set_focus_object(fd_form_table_options->form_table_options,
414                             fd_form_table_options->button_table_delete);
415         fl_hide_form(fd_form_table_options->form_table_options);
416     } else
417         UpdateLayoutTabular(true, inset);
418     return;
419 }
420
421 void SetPWidthTabularCB(FL_OBJECT * ob, long)
422 {
423     if (!inset) {
424         return;
425     }
426
427     fl_set_object_label(fd_form_table_options->text_warning, "");
428     Confirmed = false;
429     if (ob == fd_form_table_options->input_column_width) {
430         string str = fl_get_input(ob);
431         if (!str.empty() && !isValidLength(str)) {
432             fl_set_object_label(fd_form_table_options->text_warning,
433                            _("Warning: Invalid Length (valid example: 10mm)"));
434             fl_show_object(fd_form_table_options->text_warning);
435             return;
436         }
437         if (current_view->available()){
438             current_view->hideCursor();
439             inset->TabularFeatures(current_view, LyXTabular::SET_PWIDTH, str);
440         }
441         MenuLayoutTabular(false, inset); // update for alignment
442     }
443 }
444
445 void TabularOptClose()
446 {
447     inset = 0;
448 }
449