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