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