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