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