]> git.lyx.org Git - lyx.git/blob - src/lyx_gui_misc.C
fix a couple of hard crashes, constify local variables, whitespace changes, some...
[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 "form1.h"
24 #include "gettext.h"
25 #include "include_form.h"
26 #include "log_form.h"
27 #include "layout_forms.h"
28 #include "lyx.h"
29 #include "lyx_cb.h"
30 #include "lyx_main.h"
31 #include "mathed/math_forms.h"
32 #include "minibuffer.h"
33 #include "print_form.h"
34 #include "sp_form.h"
35 #include "LyXView.h"
36 #include "bufferview_funcs.h"
37 #include "support/filetools.h"
38 #include "lyxrc.h"
39
40 using std::pair;
41 using std::make_pair;
42 using std::endl;
43
44 extern BufferView * current_view;
45
46 extern FD_form_character * fd_form_character;
47 extern FD_form_preamble * fd_form_preamble;
48 extern FD_form_sendto * fd_form_sendto;
49 extern FD_form_figure * fd_form_figure;
50 extern FD_LaTeXLog * fd_latex_log;
51 extern FD_form_spell_check * fd_form_spell_check;
52 extern FD_panel  * fd_panel;
53 extern FD_delim  * fd_delim;
54 extern FD_deco   * fd_deco;
55 extern FD_space  * fd_space;
56 extern FD_matrix * fd_matrix;
57 extern FD_bibitem_form * bibitem_form;
58 extern FD_include * form;
59
60 extern void HideFiguresPopups();
61
62 // Prevents LyX from being killed when the close box is pressed in a popup.
63 extern "C" int CancelCloseBoxCB(FL_FORM *, void *)
64 {
65         return FL_CANCEL;
66 }
67
68
69 // Prevents LyX from crashing when no buffers available
70 // This is also one of the functions that we _really_ dont want
71 // we should try to finds way to help us with that.
72 // The signal/slot mechanism can probably help. 
73 void CloseAllBufferRelatedDialogs()
74 {
75         // don't forget to check that dynamically created forms
76         // have been created otherwise hiding one could cause a crash
77         // need the visible check otherwise XForms prints a warning
78         // if hiding an invisible form
79         if (fd_form_character->form_character->visible) {
80                 fl_hide_form(fd_form_character->form_character);
81         }
82         if (fd_form_preamble->form_preamble->visible) {
83                 fl_hide_form(fd_form_preamble->form_preamble);
84         }
85         if (fd_form_figure->form_figure->visible) {
86                 fl_hide_form(fd_form_figure->form_figure);
87         }
88         if (fd_form_sendto->form_sendto->visible) {
89                 fl_hide_form(fd_form_sendto->form_sendto);
90         }
91         if (fd_latex_log->LaTeXLog->visible) {
92                 fl_hide_form(fd_latex_log->LaTeXLog);
93         }
94         if (fd_form_spell_check) {
95                 if (fd_form_spell_check->form_spell_check->visible) {
96                         fl_trigger_object(fd_form_spell_check->done);
97                 }
98         }
99         if (fd_panel) {
100                 if (fd_panel->panel->visible) {
101                         fl_hide_form(fd_panel->panel);
102                 }
103         }
104         if (fd_delim) {
105                 if (fd_delim->delim->visible) {
106                         fl_hide_form(fd_delim->delim);
107                 }
108         }
109         if (fd_deco) {
110                 if (fd_deco->deco->visible) {
111                         fl_hide_form(fd_deco->deco);
112                 }
113         }
114         if (fd_space) {
115                 if (fd_space->space->visible) {
116                         fl_hide_form(fd_space->space);
117                 }
118         }
119         if (fd_matrix) {
120                 if (fd_matrix->matrix->visible) {
121                         fl_hide_form(fd_matrix->matrix);
122                 }
123         }
124         if (bibitem_form) {
125                 if (bibitem_form->bibitem_form->visible) {
126                         fl_hide_form(bibitem_form->bibitem_form);
127                 }
128         }
129         if (form) {
130                 if (form->include->visible) {
131                         fl_hide_form(form->include);
132                 }
133         }
134         HideFiguresPopups();
135 }
136
137 // This is another function we really don't want.
138 // Again the Signal/Slot mechanism is tailor made for this task.
139 void updateAllVisibleBufferRelatedDialogs(bool)
140 {
141         if (fd_form_preamble->form_preamble->visible) {
142                 UpdateLayoutPreamble(current_view);
143         }
144         if (fd_latex_log->LaTeXLog->visible) {
145                 LatexLogUpdate(0,0);
146         }
147         if (current_view->buffer() &&  current_view->buffer()->isReadonly()) {
148                 // a little crude perhaps but it works. ARRae
149                 if (fd_form_character->form_character->visible) {
150                         fl_hide_form(fd_form_character->form_character);
151                 }
152 #ifndef ALWAYS_CLOSE_MATH_PANELS
153                 // The math popups should be closed only if we switch
154                 // to a readonly buffer
155                 if (fd_panel) {
156                         if (fd_panel->panel->visible) {
157                                 fl_hide_form(fd_panel->panel);
158                         }
159                 }
160                 if (fd_delim) {
161                         if (fd_delim->delim->visible) {
162                                 fl_hide_form(fd_delim->delim);
163                         }
164                 }
165                 if (fd_deco) {
166                         if (fd_deco->deco->visible) {
167                                 fl_hide_form(fd_deco->deco);
168                         }
169                 }
170                 if (fd_space) {
171                         if (fd_space->space->visible) {
172                                 fl_hide_form(fd_space->space);
173                         }
174                 }
175                 if (fd_matrix) {
176                         if (fd_matrix->matrix->visible) {
177                                 fl_hide_form(fd_matrix->matrix);
178                         }
179                 }
180 #endif
181         }
182
183         // We have either changed buffers or changed the readonly status
184         // so the safest thing to do is hide all inset popups that
185         // are editting insets from the previous buffer or aren't
186         // allowed in readonly docs.
187 #ifdef ALWAYS_CLOSE_MATH_PANELS
188         if (fd_panel) {
189                 if (fd_panel->panel->visible) {
190                         fl_hide_form(fd_panel->panel);
191                 }
192         }
193         if (fd_delim) {
194                 if (fd_delim->delim->visible) {
195                         fl_hide_form(fd_delim->delim);
196                 }
197         }
198         if (fd_deco) {
199                 if (fd_deco->deco->visible) {
200                         fl_hide_form(fd_deco->deco);
201                 }
202         }
203         if (fd_space) {
204                 if (fd_space->space->visible) {
205                         fl_hide_form(fd_space->space);
206                 }
207         }
208         if (fd_matrix) {
209                 if (fd_matrix->matrix->visible) {
210                         fl_hide_form(fd_matrix->matrix);
211                 }
212         }
213 #endif
214         if (bibitem_form) {
215                 if (bibitem_form->bibitem_form->visible) {
216                         fl_hide_form(bibitem_form->bibitem_form);
217                 }
218         }
219         if (form) {
220                 if (form->include->visible) {
221                         fl_hide_form(form->include);
222                 }
223         }
224         HideFiguresPopups();
225 }
226
227 // Extract shortcut from <ident>|<shortcut> string
228 char const * flyx_shortcut_extract(char const * sc)
229 {
230         // Find '|' in the sc and return the string after that.
231         register char const * sd = sc;
232         while(sd[0]!= 0 && sd[0] != '|') ++sd;
233
234         if (sd[0] == '|') {
235                 ++sd;
236                 //lyxerr << sd << endl;
237                 return sd;
238         }
239         return "";
240 }
241
242
243 // Extract identifier from <ident>|<shortcut> string
244 char const * flyx_ident_extract(char const * sc)
245 {
246         register char const * se = sc;
247         while(se[0]!= 0 && se[0] != '|') ++se;
248
249         if (se[0] == 0) return sc;
250         
251         char * sb = new char[se - sc + 1];
252         int index = 0;
253         register char const * sd = sc;
254         while (sd != se) {
255                 sb[index] = sd[0];
256                 ++index; ++sd;
257         }
258         sb[index] = 0;
259         return sb;
260 }
261
262
263 void WriteStatus(MiniBuffer * minib, string const & s)
264 {
265         if (minib) {
266                 minib->Set(s);
267                 minib->Store();
268         } else
269                 lyxerr << s << endl;
270 }
271
272
273 //
274 void WriteAlert(string const & s1, string const & s2, string const & s3)
275 {
276         MiniBuffer * minibuffer = 0;
277         if (current_view && current_view->owner())
278                 minibuffer = current_view->owner()->getMiniBuffer();
279         if (minibuffer) {
280                 /// Write to minibuffer
281                 ProhibitInput(current_view);
282                 minibuffer->Set(s1, s2, s3);
283                 fl_set_resource("flAlert.dismiss.label", _("Dismiss"));
284                 fl_show_alert(s1.c_str(), s2.c_str(), s3.c_str(), 0);
285                 AllowInput(current_view);
286         } else {
287                 /// Write to lyxerr
288                 lyxerr << "----------------------------------------" << endl
289                        << s1 << endl << s2 << endl << s3 << endl
290                        << "----------------------------------------" << endl;
291         }
292 }
293
294
295 // Alarms user of something related to files
296 void WriteFSAlert(string const & s1, string const & s2)
297 {
298         WriteAlert(s1, s2, strerror(errno));
299 }
300
301
302 bool AskQuestion(string const & s1, string const & s2, string const & s3,
303                  bool default_value)
304 {
305         if (!lyxrc.use_gui) {
306                 lyxerr << "----------------------------------------" << endl
307                        << s1 << endl;
308                 if (!s2.empty())
309                         lyxerr << s2 << endl;
310                 if (!s3.empty())
311                         lyxerr << s3 << endl;
312                 lyxerr << "Assuming answer is "
313                        << (default_value ? "yes" : "no")
314                        << endl
315                        << "----------------------------------------" << endl;
316                 return default_value;
317         }
318
319         fl_set_resource("flQuestion.yes.label", idex(_("Yes|Yy#y")));
320         fl_set_resource("flQuestion.no.label", idex(_("No|Nn#n")));
321         return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
322 }
323
324
325 // Returns 1 for yes, 2 for no, 3 for cancel.
326 int AskConfirmation(string const & s1, string const & s2, string const & s3,
327                     int default_value)
328 {
329         if (!lyxrc.use_gui) {
330                 lyxerr << "----------------------------------------" << endl
331                        << s1 << endl;
332                 if (!s2.empty())
333                         lyxerr << s2 << endl;
334                 if (!s3.empty())
335                         lyxerr << s3 << endl;
336                 lyxerr << "Assuming answer is ";
337                 if (default_value == 1)
338                         lyxerr << "yes";
339                 else if (default_value == 2)
340                         lyxerr << "no";
341                 else
342                         lyxerr << "cancel";
343                 lyxerr << endl
344                        << "----------------------------------------" << endl;
345                 return default_value;
346         }
347
348         fl_set_choices_shortcut(scex(_("Yes|Yy#y")),
349                                 scex(_("No|Nn#n")),
350                                 scex(_("Cancel|^[")));
351         return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(), 
352                               3, idex(_("Yes|Yy#y")),
353                               idex(_("No|Nn#n")),
354                               idex(_("Cancel|^[")), 3);
355 }
356
357
358 // Asks for a text
359 pair<bool, string> const
360 askForText(string const & msg, string const & dflt)
361 {
362         if (!lyxrc.use_gui) {
363                 lyxerr << "----------------------------------------" << endl
364                        << msg << endl
365                        << "Assuming answer is " << dflt
366                        << "----------------------------------------" << endl;
367                 return make_pair<bool, string>(true, dflt);
368         }
369
370         fl_set_resource("flInput.cancel.label", idex(_("Cancel|^[")));
371         fl_set_resource("flInput.ok.label", idex(_("OK|#O")));
372         fl_set_resource("flInput.clear.label", idex(_("Clear|#e")));
373         char const * tmp = fl_show_input(msg.c_str(), dflt.c_str());
374         if (tmp != 0)
375           return make_pair<bool, string>(true, string(tmp));
376         else
377           return make_pair<bool, string>(false, string());
378 }
379
380
381 // Inform the user that the buffer is read-only, and that changes in the
382 // dialog box that is to appear will be ignored.
383 void WarnReadonly(string const & file)
384 {
385         WriteAlert(_("Any changes will be ignored"),
386                    _("The document is read-only:"),
387                    MakeDisplayPath(file));
388 }
389
390 /// Get the dpi setting of the current screen
391 float getScreenDPI()
392 {
393         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen); //DefaultScreen(fl_get_display());
394         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
395                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
396 }