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