]> git.lyx.org Git - lyx.git/blob - src/TableLayout.C
encoding + etc patch from dekel
[lyx.git] / src / TableLayout.C
1 #include <config.h>
2
3 #include <cstdlib>
4 #include FORMS_H_LOCATION
5
6 #include "layout_forms.h"
7 #include "lyx_main.h"
8 #include "lyxrc.h"
9 #include "LString.h"
10 #include "support/filetools.h"
11 #include "buffer.h"
12 #include "vspace.h"
13 #include "lyx_gui_misc.h"
14 #include "BufferView.h"
15 #include "gettext.h"
16 #include "lyxtext.h"
17 #include "layout.h"
18 #include "insets/lyxinset.h"
19
20
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->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 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(BufferView::SELECT|BufferView::FITCUR);
426         }
427         if ((num == LyXTable::SET_SPECIAL_COLUMN) ||
428             (num == LyXTable::SET_SPECIAL_MULTI))
429             current_view->text->TableFeatures(current_view, num, special);
430         else
431             current_view->text->TableFeatures(current_view, num);
432         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
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(BufferView::SELECT|BufferView::FITCUR);
477             }
478             current_view->text->TableFeatures(current_view, LyXTable::SET_PWIDTH, str);
479             current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
480         }
481         MenuLayoutTable(0); // update for alignment
482     }
483 }