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