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