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