]> git.lyx.org Git - lyx.git/blob - src/lyx_gui_misc.C
32eeb878f96b147813af7ab121683870270272a6
[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_paragraph_extra * fd_form_paragraph_extra;
57 extern FD_LaTeXLog * fd_latex_log;
58 extern FD_form_spell_check * fd_form_spell_check;
59 extern FD_form_bullet * fd_form_bullet;
60 extern FD_panel  * fd_panel;
61 extern FD_delim  * fd_delim;
62 extern FD_deco   * fd_deco;
63 extern FD_space  * fd_space;
64 extern FD_matrix * fd_matrix;
65 extern FD_bibitem_form * bibitem_form;
66 extern FD_include * form;
67
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 CloseAllBufferRelatedDialogs()
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 #ifdef USE_OLD_DOCUMENT_LAYOUT
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_paper->form_paper->visible) {
111                 fl_hide_form(fd_form_paper->form_paper);
112         }
113         if (fd_form_bullet) {
114                 if (fd_form_bullet->form_bullet->visible) {
115                         fl_hide_form(fd_form_bullet->form_bullet);
116                 }
117         }
118 #endif
119         if (fd_form_preamble->form_preamble->visible) {
120                 fl_hide_form(fd_form_preamble->form_preamble);
121         }
122         if (fd_form_figure->form_figure->visible) {
123                 fl_hide_form(fd_form_figure->form_figure);
124         }
125 #ifndef NEW_TABULAR
126         if (fd_form_table->form_table->visible) {
127                 fl_hide_form(fd_form_table->form_table);
128         }
129         if (fd_form_table_options->form_table_options->visible) {
130                 fl_hide_form(fd_form_table_options->form_table_options);
131         }
132 #endif
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_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 #ifdef USE_OLD_DOCUMENT_LAYOUT
187         if (fd_form_document->form_document->visible) {
188                 UpdateLayoutDocument();
189         }
190         if (fd_form_quotes->form_quotes->visible) {
191                 UpdateLayoutQuotes();
192         }
193         if (fd_form_paper->form_paper->visible) {
194                 UpdateLayoutPaper();
195         }
196 #endif
197         if (fd_form_preamble->form_preamble->visible) {
198                 UpdateLayoutPreamble();
199         }
200         if (fd_form_paragraph->form_paragraph->visible) {
201                 UpdateLayoutParagraph();
202         }
203         if (fd_form_paragraph_extra->form_paragraph_extra->visible) {
204                 UpdateParagraphExtra();
205         }
206 #ifndef NEW_TABULAR
207         if (fd_form_table_options->form_table_options->visible) {
208                 UpdateLayoutTable(1); // just like a right mouse click
209         }
210 #endif
211         if (fd_form_bullet) {
212                 if (fd_form_bullet->form_bullet->visible) {
213                         updateBulletForm();
214                 }
215         }
216         if (fd_latex_log->LaTeXLog->visible) {
217                 LatexLogUpdate(0,0);
218         }
219         if (current_view->buffer()->isReadonly()) {
220                 // a little crude perhaps but it works. ARRae
221                 if (fd_form_character->form_character->visible) {
222                         fl_hide_form(fd_form_character->form_character);
223                 }
224                 if (fd_form_table->form_table->visible) {
225                         fl_hide_form(fd_form_table->form_table);
226                 }
227
228 #ifndef ALWAYS_CLOSE_MATH_PANELS
229                 // The math popups should be closed only if we switch
230                 // to a readonly buffer
231                 if (fd_panel) {
232                         if (fd_panel->panel->visible) {
233                                 fl_hide_form(fd_panel->panel);
234                         }
235                 }
236                 if (fd_delim) {
237                         if (fd_delim->delim->visible) {
238                                 fl_hide_form(fd_delim->delim);
239                         }
240                 }
241                 if (fd_deco) {
242                         if (fd_deco->deco->visible) {
243                                 fl_hide_form(fd_deco->deco);
244                         }
245                 }
246                 if (fd_space) {
247                         if (fd_space->space->visible) {
248                                 fl_hide_form(fd_space->space);
249                         }
250                 }
251                 if (fd_matrix) {
252                         if (fd_matrix->matrix->visible) {
253                                 fl_hide_form(fd_matrix->matrix);
254                         }
255                 }
256 #endif
257         }
258
259         // We have either changed buffers or changed the readonly status
260         // so the safest thing to do is hide all inset popups that
261         // are editting insets from the previous buffer or aren't
262         // allowed in readonly docs.
263 #ifdef ALWAYS_CLOSE_MATH_PANELS
264         if (fd_panel) {
265                 if (fd_panel->panel->visible) {
266                         fl_hide_form(fd_panel->panel);
267                 }
268         }
269         if (fd_delim) {
270                 if (fd_delim->delim->visible) {
271                         fl_hide_form(fd_delim->delim);
272                 }
273         }
274         if (fd_deco) {
275                 if (fd_deco->deco->visible) {
276                         fl_hide_form(fd_deco->deco);
277                 }
278         }
279         if (fd_space) {
280                 if (fd_space->space->visible) {
281                         fl_hide_form(fd_space->space);
282                 }
283         }
284         if (fd_matrix) {
285                 if (fd_matrix->matrix->visible) {
286                         fl_hide_form(fd_matrix->matrix);
287                 }
288         }
289 #endif
290         if (bibitem_form) {
291                 if (bibitem_form->bibitem_form->visible) {
292                         fl_hide_form(bibitem_form->bibitem_form);
293                 }
294         }
295         if (form) {
296                 if (form->include->visible) {
297                         fl_hide_form(form->include);
298                 }
299         }
300         HideFiguresPopups();
301 }
302
303 // Extract shortcut from <ident>|<shortcut> string
304 char const * flyx_shortcut_extract(char const * sc)
305 {
306         // Find '|' in the sc and return the string after that.
307         register char const * sd = sc;
308         while(sd[0]!= 0 && sd[0] != '|') ++sd;
309
310         if (sd[0] == '|') {
311                 ++sd;
312                 //lyxerr << sd << endl;
313                 return sd;
314         }
315         return "";
316 }
317
318
319 // Extract identifier from <ident>|<shortcut> string
320 char const * flyx_ident_extract(char const * sc)
321 {
322         register char const * se = sc;
323         while(se[0]!= 0 && se[0] != '|') ++se;
324
325         if (se[0] == 0) return sc;
326         
327         char * sb = new char[se - sc + 1];
328         int index = 0;
329         register char const * sd = sc;
330         while (sd != se) {
331                 sb[index] = sd[0];
332                 ++index; ++sd;
333         }
334         sb[index] = 0;
335         return sb;
336 }
337
338
339 //
340 void WriteAlert(string const & s1, string const & s2, string const & s3)
341 {
342         MiniBuffer * minibuffer = 0;
343         if (current_view && current_view->owner())
344                 minibuffer = current_view->owner()->getMiniBuffer();
345         if (minibuffer) {
346                 ProhibitInput(current_view);
347                 minibuffer->Set(s1, s2, s3);
348         }
349
350         fl_set_resource("flAlert.dismiss.label", _("Dismiss"));
351         fl_show_alert(s1.c_str(), s2.c_str(), s3.c_str(), 0);
352         if (minibuffer) {
353                 AllowInput(current_view);
354         }
355 }
356
357
358 // Alarms user of something related to files
359 void WriteFSAlert(string const & s1, string const & s2)
360 {
361         WriteAlert (s1, s2, strerror(errno));
362 }
363
364
365 bool AskQuestion(string const & s1, string const & s2, string const & s3)
366 {
367         fl_set_resource("flQuestion.yes.label", idex(_("Yes|Yy#y")));
368         fl_set_resource("flQuestion.no.label", idex(_("No|Nn#n")));
369         return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
370 }
371
372
373 // Returns 1 for yes, 2 for no, 3 for cancel.
374 int AskConfirmation(string const & s1, string const & s2, string const & s3)
375 {
376         fl_set_choices_shortcut(scex(_("Yes|Yy#y")),
377                                 scex(_("No|Nn#n")),
378                                 scex(_("Cancel|^[")));
379         return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(), 
380                               3, idex(_("Yes|Yy#y")),
381                               idex(_("No|Nn#n")),
382                               idex(_("Cancel|^[")), 3);
383 }
384
385
386 // Asks for a text
387 pair<bool, string> const
388 askForText(string const & msg, string const & dflt)
389 {
390         fl_set_resource("flInput.cancel.label", idex(_("Cancel|^[")));
391         fl_set_resource("flInput.ok.label", idex(_("OK|#O")));
392         fl_set_resource("flInput.clear.label", idex(_("Clear|#e")));
393         char const * tmp = fl_show_input(msg.c_str(), dflt.c_str());
394         if (tmp != 0)
395           return make_pair<bool, string>(true, string(tmp));
396         else
397           return make_pair<bool, string>(false, string());
398 }
399
400
401 // Inform the user that the buffer is read-only, and that changes in the
402 // dialog box that is to appear will be ignored.
403 void WarnReadonly(string const & file)
404 {
405         WriteAlert(_("Any changes will be ignored"),
406                    _("The document is read-only:"),
407                    MakeDisplayPath(file));
408 }
409
410 /// Get the dpi setting of the current screen
411 float getScreenDPI()
412 {
413         Screen * scr = DefaultScreenOfDisplay(fl_get_display());
414         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
415                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
416 }