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