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