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