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