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