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