]> git.lyx.org Git - lyx.git/blob - src/lyx_gui_misc.C
dc3a66d7334b30c1945f1f96611a078d604cf7b9
[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
35 using std::pair;
36 using std::make_pair;
37
38 extern BufferView * current_view;
39
40 extern FD_form_paragraph * fd_form_paragraph;
41 extern FD_form_paragraph_extra * fd_form_paragraph_extra;
42 extern FD_form_character * fd_form_character;
43 extern FD_form_document * fd_form_document;
44 extern FD_form_paper * fd_form_paper;
45 extern FD_form_table_options * fd_form_table_options;
46 extern FD_form_quotes * fd_form_quotes;
47 extern FD_form_preamble * fd_form_preamble;
48 extern FD_form_table * fd_form_table;
49 extern FD_form_print * fd_form_print;
50 extern FD_form_sendto * fd_form_sendto;
51 extern FD_form_figure * fd_form_figure;
52 extern FD_form_toc * fd_form_toc;
53 extern FD_form_ref * fd_form_ref;
54 extern FD_form_paragraph_extra * fd_form_paragraph_extra;
55 extern FD_LaTeXLog * fd_latex_log;
56 extern FD_form_spell_check * fd_form_spell_check;
57 extern FD_form_bullet * fd_form_bullet;
58 extern FD_panel  * fd_panel;
59 extern FD_delim  * fd_delim;
60 extern FD_deco   * fd_deco;
61 extern FD_space  * fd_space;
62 extern FD_matrix * fd_matrix;
63 extern FD_citation_form * citation_form;
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 CloseAllBufferRelatedPopups()
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_print->form_print->visible) {
117                 fl_hide_form(fd_form_print->form_print);
118         }
119         if (fd_form_figure->form_figure->visible) {
120                 fl_hide_form(fd_form_figure->form_figure);
121         }
122         if (fd_form_toc->form_toc->visible) {
123                 fl_hide_form(fd_form_toc->form_toc);
124         }
125         if (fd_form_ref->form_ref->visible) {
126                 fl_hide_form(fd_form_ref->form_ref);
127         }
128         if (fd_form_paper->form_paper->visible) {
129                 fl_hide_form(fd_form_paper->form_paper);
130         }
131         if (fd_form_table_options->form_table_options->visible) {
132                 fl_hide_form(fd_form_table_options->form_table_options);
133         }
134         if (fd_form_sendto->form_sendto->visible) {
135                 fl_hide_form(fd_form_sendto->form_sendto);
136         }
137         if (fd_latex_log->LaTeXLog->visible) {
138                 fl_hide_form(fd_latex_log->LaTeXLog);
139         }
140         if (fd_form_spell_check) {
141                 if (fd_form_spell_check->form_spell_check->visible) {
142                         fl_trigger_object(fd_form_spell_check->done);
143                 }
144         }
145         if (fd_form_bullet) {
146                 if (fd_form_bullet->form_bullet->visible) {
147                         fl_hide_form(fd_form_bullet->form_bullet);
148                 }
149         }
150         if (fd_panel) {
151                 if (fd_panel->panel->visible) {
152                         fl_hide_form(fd_panel->panel);
153                 }
154         }
155         if (fd_delim) {
156                 if (fd_delim->delim->visible) {
157                         fl_hide_form(fd_delim->delim);
158                 }
159         }
160         if (fd_deco) {
161                 if (fd_deco->deco->visible) {
162                         fl_hide_form(fd_deco->deco);
163                 }
164         }
165         if (fd_space) {
166                 if (fd_space->space->visible) {
167                         fl_hide_form(fd_space->space);
168                 }
169         }
170         if (fd_matrix) {
171                 if (fd_matrix->matrix->visible) {
172                         fl_hide_form(fd_matrix->matrix);
173                 }
174         }
175         if (citation_form) {
176                 if (citation_form->citation_form->visible) {
177                         fl_hide_form(citation_form->citation_form);
178                 }
179         }
180         if (bibitem_form) {
181                 if (bibitem_form->bibitem_form->visible) {
182                         fl_hide_form(bibitem_form->bibitem_form);
183                 }
184         }
185         if (form) {
186                 if (form->include->visible) {
187                         fl_hide_form(form->include);
188                 }
189         }
190         if (index_form) {
191                 if (index_form->index_form->visible) {
192                         fl_hide_form(index_form->index_form);
193                 }
194         }
195         HideFiguresPopups();
196 }
197
198 // This is another function we really don't want.
199 // Again the Signal/Slot mechanism is tailor made for this task.
200 void updateAllVisibleBufferRelatedPopups()
201 {
202         if (fd_form_document->form_document->visible) {
203                 UpdateLayoutDocument();
204         }
205         if (fd_form_preamble->form_preamble->visible) {
206                 UpdateLayoutPreamble();
207         }
208         if (fd_form_quotes->form_quotes->visible) {
209                 UpdateLayoutQuotes();
210         }
211         if (fd_form_paragraph->form_paragraph->visible) {
212                 UpdateLayoutParagraph();
213         }
214         if (fd_form_paragraph_extra->form_paragraph_extra->visible) {
215                 UpdateParagraphExtra();
216         }
217         if (fd_form_paper->form_paper->visible) {
218                 UpdateLayoutPaper();
219         }
220         if (fd_form_table_options->form_table_options->visible) {
221                 UpdateLayoutTable(1); // just like a right mouse click
222         }
223         if (fd_form_bullet) {
224                 if (fd_form_bullet->form_bullet->visible) {
225                         updateBulletForm();
226                 }
227         }
228         if (fd_form_toc->browser_toc->visible) {
229                 TocUpdateCB(0, 0);
230         }
231 #ifdef ALWAYS_UPDATE_REF
232         // Ideally, the RefUpdateCB should be modified so that if the
233         // current document is a subdocument the reference list should come
234         // from master document (or even be a merger of references from all
235         // its sibling documents).  For now this has effectively been
236         // commented out to ensure that users of multipart documents can
237         // access the reference list of a different document.  This is
238         // of course a compromise and an annoyance for users switching
239         // between separate documents.  ARRae 241198
240         if (fd_form_ref->form_ref->visible) {
241                 RefUpdateCB(0, 0);
242         }
243 #endif
244         if (current_view->buffer()->isReadonly()) {
245                 // a little crude perhaps but it works. ARRae
246                 if (fd_form_character->form_character->visible) {
247                         fl_hide_form(fd_form_character->form_character);
248                 }
249                 if (fd_form_table->form_table->visible) {
250                         fl_hide_form(fd_form_table->form_table);
251                 }
252         }
253
254         // We have either changed buffers or changed the readonly status
255         // so the safest thing to do is hide all inset popups that
256         // are editting insets from the previous buffer or aren't
257         // allowed in readonly docs.
258         if (fd_panel) {
259                 if (fd_panel->panel->visible) {
260                         fl_hide_form(fd_panel->panel);
261                 }
262         }
263         if (fd_delim) {
264                 if (fd_delim->delim->visible) {
265                         fl_hide_form(fd_delim->delim);
266                 }
267         }
268         if (fd_deco) {
269                 if (fd_deco->deco->visible) {
270                         fl_hide_form(fd_deco->deco);
271                 }
272         }
273         if (fd_space) {
274                 if (fd_space->space->visible) {
275                         fl_hide_form(fd_space->space);
276                 }
277         }
278         if (fd_matrix) {
279                 if (fd_matrix->matrix->visible) {
280                         fl_hide_form(fd_matrix->matrix);
281                 }
282         }
283         if (citation_form) {
284                 if (citation_form->citation_form->visible) {
285                         fl_hide_form(citation_form->citation_form);
286                 }
287         }
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         if (index_form) {
299                 if (index_form->index_form->visible) {
300                         fl_hide_form(index_form->index_form);
301                 }
302         }
303         HideFiguresPopups();
304 }
305
306 // Extract shortcut from <ident>|<shortcut> string
307 char const * flyx_shortcut_extract(char const * sc)
308 {
309         // Find '|' in the sc and return the string after that.
310         register char const * sd = sc;
311         while(sd[0]!= 0 && sd[0] != '|') ++sd;
312
313         if (sd[0] == '|') {
314                 ++sd;
315                 //lyxerr << sd << endl;
316                 return sd;
317         }
318         return "";
319 }
320
321
322 // Extract identifier from <ident>|<shortcut> string
323 char const * flyx_ident_extract(char const * sc)
324 {
325         register char const * se = sc;
326         while(se[0]!= 0 && se[0] != '|') ++se;
327
328         if (se[0] == 0) return sc;
329         
330         char * sb = new char[se - sc + 1];
331         int index = 0;
332         register char const * sd = sc;
333         while (sd != se) {
334                 sb[index] = sd[0];
335                 ++index; ++sd;
336         }
337         sb[index] = 0;
338         return sb;
339 }
340
341
342 //
343 void WriteAlert(string const & s1, string const & s2, string const & s3)
344 {
345         MiniBuffer * minibuffer = 0;
346         if (current_view && current_view->owner())
347                 minibuffer = current_view->owner()->getMiniBuffer();
348         if (minibuffer) {
349                 ProhibitInput();
350                 minibuffer->Set(s1, s2, s3);
351         }
352
353         fl_set_resource("flAlert.dismiss.label", _("Dismiss"));
354         fl_show_alert(s1.c_str(), s2.c_str(), s3.c_str(), 0);
355         if (minibuffer) {
356                 AllowInput();
357         }
358 }
359
360
361 // Alarms user of something related to files
362 void WriteFSAlert(string const & s1, string const & s2)
363 {
364         WriteAlert (s1, s2, strerror(errno));
365 }
366
367
368 bool AskQuestion(string const & s1, string const & s2, string const & s3)
369 {
370         fl_set_resource("flQuestion.yes.label", idex(_("Yes|Yy#y")));
371         fl_set_resource("flQuestion.no.label", idex(_("No|Nn#n")));
372         return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
373 }
374
375
376 // Returns 1 for yes, 2 for no, 3 for cancel.
377 int AskConfirmation(string const & s1, string const & s2, string const & s3)
378 {
379         fl_set_choices_shortcut(scex(_("Yes|Yy#y")),
380                                 scex(_("No|Nn#n")),
381                                 scex(_("Cancel|^[")));
382         return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(), 
383                               3, idex(_("Yes|Yy#y")),
384                               idex(_("No|Nn#n")),
385                               idex(_("Cancel|^[")), 3);
386 }
387
388
389 // Asks for a text
390 pair<bool, string> askForText(string const & msg, string const & dflt)
391 {
392         char const * tmp;
393         fl_set_resource("flInput.cancel.label", idex(_("Cancel|^[")));
394         fl_set_resource("flInput.ok.label", idex(_("OK|#O")));
395         fl_set_resource("flInput.clear.label", idex(_("Clear|#e")));
396         tmp = fl_show_input(msg.c_str(), dflt.c_str());
397         if (tmp != 0)
398           return make_pair<bool, string>(true, tmp);
399         else
400           return make_pair<bool, string>(false, string());
401 }
402
403
404 // Inform the user that the buffer is read-only, and that changes in the
405 // dialog box that is to appear will be ignored.
406 void WarnReadonly(string const & file)
407 {
408         WriteAlert(_("Any changes will be ignored"),
409                    _("The document is read-only:"),
410                    file);
411 }
412
413 /// Get the dpi setting of the current screen
414 float getScreenDPI()
415 {
416         Screen * scr = DefaultScreenOfDisplay(fl_get_display());
417         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
418                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
419 }