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