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