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