]> git.lyx.org Git - lyx.git/blob - src/TableLayout.C
update libtool, try to make it work for std::streams (be VERY careful I have experien...
[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 "lyxscreen.h"
16 #include "gettext.h"
17 #include "lyxtext.h"
18 #include "layout.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 static int Confirmed = false;
27 static int ActCell;
28
29 // hack to keep the cursor from jumping to the end of the text in the Extra
30 // form input fields during editing. The values in LyXTable itself is
31 // changed in real-time, but we have no callbacks for the input fields,
32 // so I simply store and restore the cursor position for now.
33 // (too much of a hazzle to do it proper; we'll trash all this code
34 // in 1.1 anyway)
35 static int extra_col_cursor_x; // need no y's, one-line input fields
36 static int extra_multicol_cursor_x;
37 // Joacim
38
39
40 bool UpdateLayoutTable(int flag)
41 {
42     bool update = true;
43     if (!current_view->getScreen() || !current_view->available())
44         update = false;
45     
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 if (fd_form_table_options->form_table_options->visible) {
216         fl_set_focus_object(fd_form_table_options->form_table_options,
217                             fd_form_table_options->button_table_delete);
218         fl_hide_form(fd_form_table_options->form_table_options);
219     }
220     return update;
221 }
222
223
224 void OpenLayoutTableExtra()
225 {
226
227         if (fd_form_table_extra->form_table_extra->visible) {
228                 fl_raise_form(fd_form_table_extra->form_table_extra);
229         } else {
230                 static int ow = -1, oh;
231                 fl_show_form(fd_form_table_extra->form_table_extra,
232                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
233                              _("Table Extra Form"));
234                 if (ow < 0) {
235                         ow = fd_form_table_extra->form_table_extra->w;
236                         oh = fd_form_table_extra->form_table_extra->h;
237                 }
238                 fl_set_form_minsize(fd_form_table_extra->form_table_extra,
239                                     ow, oh);
240         }
241 }
242
243
244 void MenuLayoutTable(int flag)
245 {
246     if (UpdateLayoutTable(flag)) {
247         if (fd_form_table_options->form_table_options->visible) {
248             fl_raise_form(fd_form_table_options->form_table_options);
249         }
250         else {
251             fl_show_form(fd_form_table_options->form_table_options,
252                          FL_PLACE_MOUSE, FL_FULLBORDER,
253                          _("Table Layout"));
254         }
255     }
256 }
257
258
259 void TableOptionsCB(FL_OBJECT * ob, long)
260 {
261     LyXTable * table = 0;
262     int s, num = 0;
263     string special, str;
264
265     if (!current_view->available()
266         || !(table = current_view->text->cursor.par->table)) {
267         MenuLayoutTable(0);
268         return;
269       }
270     int cell = current_view->text->
271         NumberOfCell(current_view->text->cursor.par, 
272                      current_view->text->cursor.pos);
273     if (ActCell != cell) {
274         MenuLayoutTable(0);
275         fl_set_object_label(fd_form_table_options->text_warning,
276                             _("Warning: Wrong Cursor position, updated window"));
277         fl_show_object(fd_form_table_options->text_warning);
278         extra_col_cursor_x = 0; // would rather place it at the end, but...
279         extra_multicol_cursor_x = 0;
280         return;
281     }
282
283     // No point in processing directives that you can't do anything with
284     // anyhow, so exit now if the buffer is read-only.
285     if (current_view->buffer()->isReadonly()) {
286       MenuLayoutTable(0);
287       return;
288     }
289     
290     if (ob != fd_form_table_options->button_table_delete) {
291         fl_set_object_label(fd_form_table_options->text_warning, "");
292         Confirmed = false;
293     }
294     str = fl_get_input(fd_form_table_options->input_column_width);
295     if (!str.empty() && !isValidLength(str)) {
296         fl_set_object_label(fd_form_table_options->text_warning,
297                             _("Warning: Invalid Length (valid example: 10mm)"));
298         fl_show_object(fd_form_table_options->text_warning);
299         return;
300     }
301     if (((ob == fd_form_table_options->button_delete_row) && (table->rows<= 1)) ||
302         ((ob == fd_form_table_options->button_delete_column) && (table->columns<= 1)))
303         ob = fd_form_table_options->button_table_delete;
304     if (ob == fd_form_table_options->button_append_row)
305         num = LyXTable::APPEND_ROW;
306     else if (ob == fd_form_table_options->button_append_column)
307         num = LyXTable::APPEND_COLUMN;
308     else if (ob == fd_form_table_options->button_delete_row)
309         num = LyXTable::DELETE_ROW;
310     else if (ob == fd_form_table_options->button_delete_column)
311         num = LyXTable::DELETE_COLUMN;
312     else if (ob == fd_form_table_options->button_set_borders)
313         num = LyXTable::SET_ALL_LINES;
314     else if (ob == fd_form_table_options->button_unset_borders)
315         num = LyXTable::UNSET_ALL_LINES;
316     else if (ob == fd_form_table_options->radio_border_top)
317         num = LyXTable::TOGGLE_LINE_TOP;
318     else if (ob == fd_form_table_options->radio_border_bottom)
319         num = LyXTable::TOGGLE_LINE_BOTTOM;
320     else if (ob == fd_form_table_options->radio_border_left)
321         num = LyXTable::TOGGLE_LINE_LEFT;
322     else if (ob == fd_form_table_options->radio_border_right)
323         num = LyXTable::TOGGLE_LINE_RIGHT;
324     else if (ob == fd_form_table_options->radio_align_left)
325         num = LyXTable::ALIGN_LEFT;
326     else if (ob == fd_form_table_options->radio_align_right)
327         num = LyXTable::ALIGN_RIGHT;
328     else if (ob == fd_form_table_options->radio_align_center)
329         num = LyXTable::ALIGN_CENTER;
330     else if ((ob == fd_form_table_options->button_table_delete) && !Confirmed) {
331         fl_set_object_label(fd_form_table_options->text_warning,
332                             _("Confirm: press Delete-Button again"));
333         Confirmed = true;
334         return;
335     } else if ((ob == fd_form_table_options->button_table_delete) 
336                && Confirmed) {
337         num = LyXTable::DELETE_TABLE;
338         Confirmed = false;
339     } else if (ob == fd_form_table_options->radio_multicolumn)
340         num = LyXTable::MULTICOLUMN;
341     else if (ob == fd_form_table_options->radio_longtable) {
342         s = fl_get_button(fd_form_table_options->radio_longtable);
343         if (s) {
344             num = LyXTable::SET_LONGTABLE;
345             fl_activate_object(fd_form_table_options->radio_lt_firsthead);
346             fl_activate_object(fd_form_table_options->radio_lt_head);
347             fl_activate_object(fd_form_table_options->radio_lt_foot);
348             fl_activate_object(fd_form_table_options->radio_lt_lastfoot);
349             fl_activate_object(fd_form_table_options->radio_lt_newpage);
350             fl_set_button(fd_form_table_options->radio_lt_firsthead,
351                           table->RowOfLTFirstHead(cell));
352             fl_set_button(fd_form_table_options->radio_lt_head,
353                           table->RowOfLTHead(cell));
354             fl_set_button(fd_form_table_options->radio_lt_foot,
355                           table->RowOfLTFoot(cell));
356             fl_set_button(fd_form_table_options->radio_lt_lastfoot,
357                           table->RowOfLTLastFoot(cell));
358             fl_set_button(fd_form_table_options->radio_lt_firsthead,
359                           table->LTNewPage(cell));
360         } else {
361             num = LyXTable::UNSET_LONGTABLE;
362             fl_deactivate_object(fd_form_table_options->radio_lt_firsthead);
363             fl_deactivate_object(fd_form_table_options->radio_lt_head);
364             fl_deactivate_object(fd_form_table_options->radio_lt_foot);
365             fl_deactivate_object(fd_form_table_options->radio_lt_lastfoot);
366             fl_deactivate_object(fd_form_table_options->radio_lt_newpage);
367             fl_set_button(fd_form_table_options->radio_lt_firsthead, 0);
368             fl_set_button(fd_form_table_options->radio_lt_firsthead, 0);
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_object_lcol(fd_form_table_options->radio_lt_firsthead,
373                                FL_INACTIVE);
374             fl_set_object_lcol(fd_form_table_options->radio_lt_head,
375                                FL_INACTIVE);
376             fl_set_object_lcol(fd_form_table_options->radio_lt_foot,
377                                FL_INACTIVE);
378             fl_set_object_lcol(fd_form_table_options->radio_lt_lastfoot,
379                                FL_INACTIVE);
380             fl_set_object_lcol(fd_form_table_options->radio_lt_newpage,
381                                FL_INACTIVE);
382         }
383     } else if (ob == fd_form_table_options->radio_rotate_table) {
384         s = fl_get_button(fd_form_table_options->radio_rotate_table);
385         if (s)
386             num = LyXTable::SET_ROTATE_TABLE;
387         else
388             num = LyXTable::UNSET_ROTATE_TABLE;
389     } else if (ob == fd_form_table_options->radio_rotate_cell) {
390         s = fl_get_button(fd_form_table_options->radio_rotate_cell);
391         if (s)
392             num = LyXTable::SET_ROTATE_CELL;
393         else
394             num = LyXTable::UNSET_ROTATE_CELL;
395     } else if (ob == fd_form_table_options->radio_linebreak_cell) {
396         num = LyXTable::SET_LINEBREAKS;
397     } else if (ob == fd_form_table_options->radio_lt_firsthead) {
398         num = LyXTable::SET_LTFIRSTHEAD;
399     } else if (ob == fd_form_table_options->radio_lt_head) {
400         num = LyXTable::SET_LTHEAD;
401     } else if (ob == fd_form_table_options->radio_lt_foot) {
402         num = LyXTable::SET_LTFOOT;
403     } else if (ob == fd_form_table_options->radio_lt_lastfoot) {
404         num = LyXTable::SET_LTLASTFOOT;
405     } else if (ob == fd_form_table_options->radio_lt_newpage) {
406         num = LyXTable::SET_LTNEWPAGE;
407     } else if (ob == fd_form_table_options->button_table_extra) {
408         OpenLayoutTableExtra();
409         return;
410     } else if (ob == fd_form_table_extra->input_special_alignment) {
411         special = fl_get_input(fd_form_table_extra->input_special_alignment);
412         int dummy;
413         fl_get_input_cursorpos(ob, &extra_col_cursor_x, &dummy);
414         num = LyXTable::SET_SPECIAL_COLUMN;
415     } else if (ob == fd_form_table_extra->input_special_multialign) {
416         special = fl_get_input(fd_form_table_extra->input_special_multialign);
417         int dummy;
418         fl_get_input_cursorpos(ob, &extra_multicol_cursor_x, &dummy);
419         num = LyXTable::SET_SPECIAL_MULTI;
420     } else
421         return;
422     if (current_view->available()) {
423         current_view->getScreen()->HideCursor();
424         if (!current_view->text->selection){
425             current_view->beforeChange();
426             current_view->update(-2);
427         }
428         if ((num == LyXTable::SET_SPECIAL_COLUMN) ||
429             (num == LyXTable::SET_SPECIAL_MULTI))
430             current_view->text->TableFeatures(num, special);
431         else
432             current_view->text->TableFeatures(num);
433         current_view->update(1);
434     }
435     if (num == LyXTable::DELETE_TABLE) {
436         fl_set_focus_object(fd_form_table_options->form_table_options,
437                             fd_form_table_options->button_table_delete);
438         fl_hide_form(fd_form_table_options->form_table_options);
439     } else
440         UpdateLayoutTable(true);
441     return;
442 }
443
444
445 void TableOptCloseCB(FL_OBJECT *, long)
446 {
447     fl_set_focus_object(fd_form_table_options->form_table_options,
448                         fd_form_table_options->button_table_delete);
449     fl_hide_form(fd_form_table_options->form_table_options);
450     return;
451 }
452
453 void TableSpeCloseCB(FL_OBJECT *, long)
454 {
455     fl_set_focus_object(fd_form_table_options->form_table_options,
456                         fd_form_table_options->button_table_delete);
457     fl_hide_form(fd_form_table_extra->form_table_extra);
458     return;
459 }
460
461 void SetPWidthCB(FL_OBJECT * ob, long)
462 {
463     fl_set_object_label(fd_form_table_options->text_warning, "");
464     Confirmed = false;
465     if (ob == fd_form_table_options->input_column_width) {
466         string str = fl_get_input(ob);
467         if (!str.empty() && !isValidLength(str)) {
468             fl_set_object_label(fd_form_table_options->text_warning,
469                            _("Warning: Invalid Length (valid example: 10mm)"));
470             fl_show_object(fd_form_table_options->text_warning);
471             return;
472         }
473         if (current_view->available()){
474             current_view->getScreen()->HideCursor();
475             if (!current_view->text->selection) {
476                 current_view->beforeChange(); 
477                 current_view->update(-2);
478             }
479             current_view->text->TableFeatures(LyXTable::SET_PWIDTH, str);
480             current_view->update(1);
481         }
482         MenuLayoutTable(0); // update for alignment
483     }
484 }