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