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