]> git.lyx.org Git - lyx.git/blob - src/lyx_gui_misc.C
make NEW_TABULAR default, some GUIRunTime changes
[lyx.git] / src / lyx_gui_misc.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include <cerrno>
19 #include "lyx_gui_misc.h"
20 #include "BufferView.h"
21 #include "bibforms.h"
22 #include "buffer.h"
23 #include "bullet_forms.h"
24 #include "form1.h"
25 #include "gettext.h"
26 #include "include_form.h"
27 #include "log_form.h"
28 #include "layout_forms.h"
29 #include "lyx.h"
30 #include "lyx_cb.h"
31 #include "lyx_main.h"
32 #include "mathed/math_forms.h"
33 #include "minibuffer.h"
34 #include "print_form.h"
35 #include "sp_form.h"
36 #include "LyXView.h"
37 #include "bufferview_funcs.h"
38 #include "support/filetools.h"
39
40 using std::pair;
41 using std::make_pair;
42
43 extern BufferView * current_view;
44
45 extern FD_form_paragraph * fd_form_paragraph;
46 extern FD_form_paragraph_extra * fd_form_paragraph_extra;
47 extern FD_form_character * fd_form_character;
48 extern FD_form_document * fd_form_document;
49 extern FD_form_paper * fd_form_paper;
50 extern FD_form_table_options * fd_form_table_options;
51 extern FD_form_quotes * fd_form_quotes;
52 extern FD_form_preamble * fd_form_preamble;
53 extern FD_form_table * fd_form_table;
54 extern FD_form_sendto * fd_form_sendto;
55 extern FD_form_figure * fd_form_figure;
56 extern FD_form_paragraph_extra * fd_form_paragraph_extra;
57 extern FD_LaTeXLog * fd_latex_log;
58 extern FD_form_spell_check * fd_form_spell_check;
59 extern FD_form_bullet * fd_form_bullet;
60 extern FD_panel  * fd_panel;
61 extern FD_delim  * fd_delim;
62 extern FD_deco   * fd_deco;
63 extern FD_space  * fd_space;
64 extern FD_matrix * fd_matrix;
65 extern FD_bibitem_form * bibitem_form;
66 extern FD_include * form;
67
68 extern void HideFiguresPopups();
69
70 // Prevents LyX from being killed when the close box is pressed in a popup.
71 extern "C" int CancelCloseBoxCB(FL_FORM *, void *)
72 {
73         return FL_CANCEL;
74 }
75
76
77 // Prevents LyX from being killed when the close box is pressed in a popup.
78 extern "C" int IgnoreCloseBoxCB(FL_FORM *, void *)
79 {
80         return FL_IGNORE;
81 }
82
83
84 // Prevents LyX from crashing when no buffers available
85 // This is also one of the functions that we _really_ dont want
86 // we should try to finds way to help us with that.
87 // The signal/slot mechanism can probably help. 
88 void CloseAllBufferRelatedDialogs()
89 {
90         // don't forget to check that dynamically created forms
91         // have been created otherwise hiding one could cause a crash
92         // need the visible check otherwise XForms prints a warning
93         // if hiding an invisible form
94         if (fd_form_paragraph->form_paragraph->visible) {
95                 fl_hide_form(fd_form_paragraph->form_paragraph);
96         }
97         if (fd_form_paragraph_extra->form_paragraph_extra->visible) {
98                 fl_hide_form(fd_form_paragraph_extra->form_paragraph_extra);
99         }
100         if (fd_form_character->form_character->visible) {
101                 fl_hide_form(fd_form_character->form_character);
102         }
103 #ifdef USE_OLD_DOCUMENT_LAYOUT
104         if (fd_form_document->form_document->visible) {
105                 fl_hide_form(fd_form_document->form_document);
106         }
107         if (fd_form_quotes->form_quotes->visible) {
108                 fl_hide_form(fd_form_quotes->form_quotes);
109         }
110         if (fd_form_paper->form_paper->visible) {
111                 fl_hide_form(fd_form_paper->form_paper);
112         }
113 #endif
114         if (fd_form_preamble->form_preamble->visible) {
115                 fl_hide_form(fd_form_preamble->form_preamble);
116         }
117         if (fd_form_table->form_table->visible) {
118                 fl_hide_form(fd_form_table->form_table);
119         }
120         if (fd_form_figure->form_figure->visible) {
121                 fl_hide_form(fd_form_figure->form_figure);
122         }
123         if (fd_form_table_options->form_table_options->visible) {
124                 fl_hide_form(fd_form_table_options->form_table_options);
125         }
126         if (fd_form_sendto->form_sendto->visible) {
127                 fl_hide_form(fd_form_sendto->form_sendto);
128         }
129         if (fd_latex_log->LaTeXLog->visible) {
130                 fl_hide_form(fd_latex_log->LaTeXLog);
131         }
132         if (fd_form_spell_check) {
133                 if (fd_form_spell_check->form_spell_check->visible) {
134                         fl_trigger_object(fd_form_spell_check->done);
135                 }
136         }
137         if (fd_form_bullet) {
138                 if (fd_form_bullet->form_bullet->visible) {
139                         fl_hide_form(fd_form_bullet->form_bullet);
140                 }
141         }
142         if (fd_panel) {
143                 if (fd_panel->panel->visible) {
144                         fl_hide_form(fd_panel->panel);
145                 }
146         }
147         if (fd_delim) {
148                 if (fd_delim->delim->visible) {
149                         fl_hide_form(fd_delim->delim);
150                 }
151         }
152         if (fd_deco) {
153                 if (fd_deco->deco->visible) {
154                         fl_hide_form(fd_deco->deco);
155                 }
156         }
157         if (fd_space) {
158                 if (fd_space->space->visible) {
159                         fl_hide_form(fd_space->space);
160                 }
161         }
162         if (fd_matrix) {
163                 if (fd_matrix->matrix->visible) {
164                         fl_hide_form(fd_matrix->matrix);
165                 }
166         }
167         if (bibitem_form) {
168                 if (bibitem_form->bibitem_form->visible) {
169                         fl_hide_form(bibitem_form->bibitem_form);
170                 }
171         }
172         if (form) {
173                 if (form->include->visible) {
174                         fl_hide_form(form->include);
175                 }
176         }
177         HideFiguresPopups();
178 }
179
180 // This is another function we really don't want.
181 // Again the Signal/Slot mechanism is tailor made for this task.
182 void updateAllVisibleBufferRelatedDialogs()
183 {
184 #ifdef USE_OLD_DOCUMENT_LAYOUT
185         if (fd_form_document->form_document->visible) {
186                 UpdateLayoutDocument();
187         }
188         if (fd_form_quotes->form_quotes->visible) {
189                 UpdateLayoutQuotes();
190         }
191         if (fd_form_paper->form_paper->visible) {
192                 UpdateLayoutPaper();
193         }
194 #endif
195         if (fd_form_preamble->form_preamble->visible) {
196                 UpdateLayoutPreamble();
197         }
198         if (fd_form_paragraph->form_paragraph->visible) {
199                 UpdateLayoutParagraph();
200         }
201         if (fd_form_paragraph_extra->form_paragraph_extra->visible) {
202                 UpdateParagraphExtra();
203         }
204 #ifndef NEW_TABULAR
205         if (fd_form_table_options->form_table_options->visible) {
206                 UpdateLayoutTable(1); // just like a right mouse click
207         }
208 #endif
209         if (fd_form_bullet) {
210                 if (fd_form_bullet->form_bullet->visible) {
211                         updateBulletForm();
212                 }
213         }
214         if (fd_latex_log->LaTeXLog->visible) {
215                 LatexLogUpdate(0,0);
216         }
217         if (current_view->buffer()->isReadonly()) {
218                 // a little crude perhaps but it works. ARRae
219                 if (fd_form_character->form_character->visible) {
220                         fl_hide_form(fd_form_character->form_character);
221                 }
222                 if (fd_form_table->form_table->visible) {
223                         fl_hide_form(fd_form_table->form_table);
224                 }
225
226 #ifndef ALWAYS_CLOSE_MATH_PANELS
227                 // The math popups should be closed only if we switch
228                 // to a readonly buffer
229                 if (fd_panel) {
230                         if (fd_panel->panel->visible) {
231                                 fl_hide_form(fd_panel->panel);
232                         }
233                 }
234                 if (fd_delim) {
235                         if (fd_delim->delim->visible) {
236                                 fl_hide_form(fd_delim->delim);
237                         }
238                 }
239                 if (fd_deco) {
240                         if (fd_deco->deco->visible) {
241                                 fl_hide_form(fd_deco->deco);
242                         }
243                 }
244                 if (fd_space) {
245                         if (fd_space->space->visible) {
246                                 fl_hide_form(fd_space->space);
247                         }
248                 }
249                 if (fd_matrix) {
250                         if (fd_matrix->matrix->visible) {
251                                 fl_hide_form(fd_matrix->matrix);
252                         }
253                 }
254 #endif
255         }
256
257         // We have either changed buffers or changed the readonly status
258         // so the safest thing to do is hide all inset popups that
259         // are editting insets from the previous buffer or aren't
260         // allowed in readonly docs.
261 #ifdef ALWAYS_CLOSE_MATH_PANELS
262         if (fd_panel) {
263                 if (fd_panel->panel->visible) {
264                         fl_hide_form(fd_panel->panel);
265                 }
266         }
267         if (fd_delim) {
268                 if (fd_delim->delim->visible) {
269                         fl_hide_form(fd_delim->delim);
270                 }
271         }
272         if (fd_deco) {
273                 if (fd_deco->deco->visible) {
274                         fl_hide_form(fd_deco->deco);
275                 }
276         }
277         if (fd_space) {
278                 if (fd_space->space->visible) {
279                         fl_hide_form(fd_space->space);
280                 }
281         }
282         if (fd_matrix) {
283                 if (fd_matrix->matrix->visible) {
284                         fl_hide_form(fd_matrix->matrix);
285                 }
286         }
287 #endif
288         if (bibitem_form) {
289                 if (bibitem_form->bibitem_form->visible) {
290                         fl_hide_form(bibitem_form->bibitem_form);
291                 }
292         }
293         if (form) {
294                 if (form->include->visible) {
295                         fl_hide_form(form->include);
296                 }
297         }
298         HideFiguresPopups();
299 }
300
301 // Extract shortcut from <ident>|<shortcut> string
302 char const * flyx_shortcut_extract(char const * sc)
303 {
304         // Find '|' in the sc and return the string after that.
305         register char const * sd = sc;
306         while(sd[0]!= 0 && sd[0] != '|') ++sd;
307
308         if (sd[0] == '|') {
309                 ++sd;
310                 //lyxerr << sd << endl;
311                 return sd;
312         }
313         return "";
314 }
315
316
317 // Extract identifier from <ident>|<shortcut> string
318 char const * flyx_ident_extract(char const * sc)
319 {
320         register char const * se = sc;
321         while(se[0]!= 0 && se[0] != '|') ++se;
322
323         if (se[0] == 0) return sc;
324         
325         char * sb = new char[se - sc + 1];
326         int index = 0;
327         register char const * sd = sc;
328         while (sd != se) {
329                 sb[index] = sd[0];
330                 ++index; ++sd;
331         }
332         sb[index] = 0;
333         return sb;
334 }
335
336
337 //
338 void WriteAlert(string const & s1, string const & s2, string const & s3)
339 {
340         MiniBuffer * minibuffer = 0;
341         if (current_view && current_view->owner())
342                 minibuffer = current_view->owner()->getMiniBuffer();
343         if (minibuffer) {
344                 ProhibitInput(current_view);
345                 minibuffer->Set(s1, s2, s3);
346         }
347
348         fl_set_resource("flAlert.dismiss.label", _("Dismiss"));
349         fl_show_alert(s1.c_str(), s2.c_str(), s3.c_str(), 0);
350         if (minibuffer) {
351                 AllowInput(current_view);
352         }
353 }
354
355
356 // Alarms user of something related to files
357 void WriteFSAlert(string const & s1, string const & s2)
358 {
359         WriteAlert (s1, s2, strerror(errno));
360 }
361
362
363 bool AskQuestion(string const & s1, string const & s2, string const & s3)
364 {
365         fl_set_resource("flQuestion.yes.label", idex(_("Yes|Yy#y")));
366         fl_set_resource("flQuestion.no.label", idex(_("No|Nn#n")));
367         return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
368 }
369
370
371 // Returns 1 for yes, 2 for no, 3 for cancel.
372 int AskConfirmation(string const & s1, string const & s2, string const & s3)
373 {
374         fl_set_choices_shortcut(scex(_("Yes|Yy#y")),
375                                 scex(_("No|Nn#n")),
376                                 scex(_("Cancel|^[")));
377         return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(), 
378                               3, idex(_("Yes|Yy#y")),
379                               idex(_("No|Nn#n")),
380                               idex(_("Cancel|^[")), 3);
381 }
382
383
384 // Asks for a text
385 pair<bool, string> const
386 askForText(string const & msg, string const & dflt)
387 {
388         fl_set_resource("flInput.cancel.label", idex(_("Cancel|^[")));
389         fl_set_resource("flInput.ok.label", idex(_("OK|#O")));
390         fl_set_resource("flInput.clear.label", idex(_("Clear|#e")));
391         char const * tmp = fl_show_input(msg.c_str(), dflt.c_str());
392         if (tmp != 0)
393           return make_pair<bool, string>(true, string(tmp));
394         else
395           return make_pair<bool, string>(false, string());
396 }
397
398
399 // Inform the user that the buffer is read-only, and that changes in the
400 // dialog box that is to appear will be ignored.
401 void WarnReadonly(string const & file)
402 {
403         WriteAlert(_("Any changes will be ignored"),
404                    _("The document is read-only:"),
405                    MakeDisplayPath(file));
406 }
407
408 /// Get the dpi setting of the current screen
409 float getScreenDPI()
410 {
411         Screen * scr = DefaultScreenOfDisplay(fl_get_display());
412         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
413                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
414 }