]> git.lyx.org Git - lyx.git/blob - src/lyx_gui_misc.C
Lots of changes for update of TextInsets, still some problems!
[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 #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 #include "bufferview_funcs.h"
35 #include "support/filetools.h"
36
37 using std::pair;
38 using std::make_pair;
39
40 extern BufferView * current_view;
41
42 extern FD_form_paragraph * fd_form_paragraph;
43 extern FD_form_paragraph_extra * fd_form_paragraph_extra;
44 extern FD_form_character * fd_form_character;
45 extern FD_form_document * fd_form_document;
46 extern FD_form_paper * fd_form_paper;
47 extern FD_form_table_options * fd_form_table_options;
48 extern FD_form_quotes * fd_form_quotes;
49 extern FD_form_preamble * fd_form_preamble;
50 extern FD_form_table * fd_form_table;
51 extern FD_form_sendto * fd_form_sendto;
52 extern FD_form_figure * fd_form_figure;
53 extern FD_form_toc * fd_form_toc;
54 extern FD_form_ref * fd_form_ref;
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 CloseAllBufferRelatedDialogs()
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_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_figure->form_figure->visible) {
118                 fl_hide_form(fd_form_figure->form_figure);
119         }
120         if (fd_form_toc->form_toc->visible) {
121                 fl_hide_form(fd_form_toc->form_toc);
122         }
123         if (fd_form_ref->form_ref->visible) {
124                 fl_hide_form(fd_form_ref->form_ref);
125         }
126         if (fd_form_paper->form_paper->visible) {
127                 fl_hide_form(fd_form_paper->form_paper);
128         }
129         if (fd_form_table_options->form_table_options->visible) {
130                 fl_hide_form(fd_form_table_options->form_table_options);
131         }
132         if (fd_form_sendto->form_sendto->visible) {
133                 fl_hide_form(fd_form_sendto->form_sendto);
134         }
135         if (fd_latex_log->LaTeXLog->visible) {
136                 fl_hide_form(fd_latex_log->LaTeXLog);
137         }
138         if (fd_form_spell_check) {
139                 if (fd_form_spell_check->form_spell_check->visible) {
140                         fl_trigger_object(fd_form_spell_check->done);
141                 }
142         }
143         if (fd_form_bullet) {
144                 if (fd_form_bullet->form_bullet->visible) {
145                         fl_hide_form(fd_form_bullet->form_bullet);
146                 }
147         }
148         if (fd_panel) {
149                 if (fd_panel->panel->visible) {
150                         fl_hide_form(fd_panel->panel);
151                 }
152         }
153         if (fd_delim) {
154                 if (fd_delim->delim->visible) {
155                         fl_hide_form(fd_delim->delim);
156                 }
157         }
158         if (fd_deco) {
159                 if (fd_deco->deco->visible) {
160                         fl_hide_form(fd_deco->deco);
161                 }
162         }
163         if (fd_space) {
164                 if (fd_space->space->visible) {
165                         fl_hide_form(fd_space->space);
166                 }
167         }
168         if (fd_matrix) {
169                 if (fd_matrix->matrix->visible) {
170                         fl_hide_form(fd_matrix->matrix);
171                 }
172         }
173         if (citation_form) {
174                 if (citation_form->form->visible) {
175                         fl_hide_form(citation_form->form);
176                 }
177         }
178         if (bibitem_form) {
179                 if (bibitem_form->bibitem_form->visible) {
180                         fl_hide_form(bibitem_form->bibitem_form);
181                 }
182         }
183         if (form) {
184                 if (form->include->visible) {
185                         fl_hide_form(form->include);
186                 }
187         }
188         if (index_form) {
189                 if (index_form->index_form->visible) {
190                         fl_hide_form(index_form->index_form);
191                 }
192         }
193         HideFiguresPopups();
194 }
195
196 // This is another function we really don't want.
197 // Again the Signal/Slot mechanism is tailor made for this task.
198 void updateAllVisibleBufferRelatedDialogs()
199 {
200         if (fd_form_document->form_document->visible) {
201                 UpdateLayoutDocument();
202         }
203         if (fd_form_preamble->form_preamble->visible) {
204                 UpdateLayoutPreamble();
205         }
206         if (fd_form_quotes->form_quotes->visible) {
207                 UpdateLayoutQuotes();
208         }
209         if (fd_form_paragraph->form_paragraph->visible) {
210                 UpdateLayoutParagraph();
211         }
212         if (fd_form_paragraph_extra->form_paragraph_extra->visible) {
213                 UpdateParagraphExtra();
214         }
215         if (fd_form_paper->form_paper->visible) {
216                 UpdateLayoutPaper();
217         }
218         if (fd_form_table_options->form_table_options->visible) {
219                 UpdateLayoutTable(1); // just like a right mouse click
220         }
221         if (fd_form_bullet) {
222                 if (fd_form_bullet->form_bullet->visible) {
223                         updateBulletForm();
224                 }
225         }
226         if (fd_form_toc->browser_toc->visible) {
227                 TocUpdateCB(0, 0);
228         }
229         if (fd_latex_log->LaTeXLog->visible) {
230                 LatexLogUpdate(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 #ifndef ALWAYS_UPDATE_REF
255                 // We must update the popup in order to make the
256                 // insert ref buttons insactive
257                 if (fd_form_ref->form_ref->visible) {
258                         RefUpdateCB(0, 0);
259                 }
260 #endif
261
262 #ifndef ALWAYS_CLOSE_MATH_PANELS
263                 // The math popups should be closed only if we switch
264                 // to a readonly buffer
265                 if (fd_panel) {
266                         if (fd_panel->panel->visible) {
267                                 fl_hide_form(fd_panel->panel);
268                         }
269                 }
270                 if (fd_delim) {
271                         if (fd_delim->delim->visible) {
272                                 fl_hide_form(fd_delim->delim);
273                         }
274                 }
275                 if (fd_deco) {
276                         if (fd_deco->deco->visible) {
277                                 fl_hide_form(fd_deco->deco);
278                         }
279                 }
280                 if (fd_space) {
281                         if (fd_space->space->visible) {
282                                 fl_hide_form(fd_space->space);
283                         }
284                 }
285                 if (fd_matrix) {
286                         if (fd_matrix->matrix->visible) {
287                                 fl_hide_form(fd_matrix->matrix);
288                         }
289                 }
290 #endif
291         }
292
293         // We have either changed buffers or changed the readonly status
294         // so the safest thing to do is hide all inset popups that
295         // are editting insets from the previous buffer or aren't
296         // allowed in readonly docs.
297 #ifdef ALWAYS_CLOSE_MATH_PANELS
298         if (fd_panel) {
299                 if (fd_panel->panel->visible) {
300                         fl_hide_form(fd_panel->panel);
301                 }
302         }
303         if (fd_delim) {
304                 if (fd_delim->delim->visible) {
305                         fl_hide_form(fd_delim->delim);
306                 }
307         }
308         if (fd_deco) {
309                 if (fd_deco->deco->visible) {
310                         fl_hide_form(fd_deco->deco);
311                 }
312         }
313         if (fd_space) {
314                 if (fd_space->space->visible) {
315                         fl_hide_form(fd_space->space);
316                 }
317         }
318         if (fd_matrix) {
319                 if (fd_matrix->matrix->visible) {
320                         fl_hide_form(fd_matrix->matrix);
321                 }
322         }
323 #endif
324         if (citation_form) {
325                 if (citation_form->form->visible) {
326                         fl_hide_form(citation_form->form);
327                 }
328         }
329         if (bibitem_form) {
330                 if (bibitem_form->bibitem_form->visible) {
331                         fl_hide_form(bibitem_form->bibitem_form);
332                 }
333         }
334         if (form) {
335                 if (form->include->visible) {
336                         fl_hide_form(form->include);
337                 }
338         }
339         if (index_form) {
340                 if (index_form->index_form->visible) {
341                         fl_hide_form(index_form->index_form);
342                 }
343         }
344         HideFiguresPopups();
345 }
346
347 // Extract shortcut from <ident>|<shortcut> string
348 char const * flyx_shortcut_extract(char const * sc)
349 {
350         // Find '|' in the sc and return the string after that.
351         register char const * sd = sc;
352         while(sd[0]!= 0 && sd[0] != '|') ++sd;
353
354         if (sd[0] == '|') {
355                 ++sd;
356                 //lyxerr << sd << endl;
357                 return sd;
358         }
359         return "";
360 }
361
362
363 // Extract identifier from <ident>|<shortcut> string
364 char const * flyx_ident_extract(char const * sc)
365 {
366         register char const * se = sc;
367         while(se[0]!= 0 && se[0] != '|') ++se;
368
369         if (se[0] == 0) return sc;
370         
371         char * sb = new char[se - sc + 1];
372         int index = 0;
373         register char const * sd = sc;
374         while (sd != se) {
375                 sb[index] = sd[0];
376                 ++index; ++sd;
377         }
378         sb[index] = 0;
379         return sb;
380 }
381
382
383 //
384 void WriteAlert(string const & s1, string const & s2, string const & s3)
385 {
386         MiniBuffer * minibuffer = 0;
387         if (current_view && current_view->owner())
388                 minibuffer = current_view->owner()->getMiniBuffer();
389         if (minibuffer) {
390                 ProhibitInput(current_view);
391                 minibuffer->Set(s1, s2, s3);
392         }
393
394         fl_set_resource("flAlert.dismiss.label", _("Dismiss"));
395         fl_show_alert(s1.c_str(), s2.c_str(), s3.c_str(), 0);
396         if (minibuffer) {
397                 AllowInput(current_view);
398         }
399 }
400
401
402 // Alarms user of something related to files
403 void WriteFSAlert(string const & s1, string const & s2)
404 {
405         WriteAlert (s1, s2, strerror(errno));
406 }
407
408
409 bool AskQuestion(string const & s1, string const & s2, string const & s3)
410 {
411         fl_set_resource("flQuestion.yes.label", idex(_("Yes|Yy#y")));
412         fl_set_resource("flQuestion.no.label", idex(_("No|Nn#n")));
413         return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
414 }
415
416
417 // Returns 1 for yes, 2 for no, 3 for cancel.
418 int AskConfirmation(string const & s1, string const & s2, string const & s3)
419 {
420         fl_set_choices_shortcut(scex(_("Yes|Yy#y")),
421                                 scex(_("No|Nn#n")),
422                                 scex(_("Cancel|^[")));
423         return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(), 
424                               3, idex(_("Yes|Yy#y")),
425                               idex(_("No|Nn#n")),
426                               idex(_("Cancel|^[")), 3);
427 }
428
429
430 // Asks for a text
431 pair<bool, string> askForText(string const & msg, string const & dflt)
432 {
433         fl_set_resource("flInput.cancel.label", idex(_("Cancel|^[")));
434         fl_set_resource("flInput.ok.label", idex(_("OK|#O")));
435         fl_set_resource("flInput.clear.label", idex(_("Clear|#e")));
436         char const * tmp = fl_show_input(msg.c_str(), dflt.c_str());
437         if (tmp != 0)
438           return make_pair<bool, string>(true, string(tmp));
439         else
440           return make_pair<bool, string>(false, string());
441 }
442
443
444 // Inform the user that the buffer is read-only, and that changes in the
445 // dialog box that is to appear will be ignored.
446 void WarnReadonly(string const & file)
447 {
448         WriteAlert(_("Any changes will be ignored"),
449                    _("The document is read-only:"),
450                    MakeDisplayPath(file));
451 }
452
453 /// Get the dpi setting of the current screen
454 float getScreenDPI()
455 {
456         Screen * scr = DefaultScreenOfDisplay(fl_get_display());
457         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
458                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
459 }