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