]> git.lyx.org Git - lyx.git/blob - src/lyx_gui_misc.C
mathed47.diff
[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                 // The math popups should be closed only if we switch
155                 // to a readonly buffer
156                 if (fd_panel) {
157                         if (fd_panel->panel->visible) {
158                                 fl_hide_form(fd_panel->panel);
159                         }
160                 }
161                 if (fd_delim) {
162                         if (fd_delim->delim->visible) {
163                                 fl_hide_form(fd_delim->delim);
164                         }
165                 }
166                 if (fd_deco) {
167                         if (fd_deco->deco->visible) {
168                                 fl_hide_form(fd_deco->deco);
169                         }
170                 }
171                 if (fd_space) {
172                         if (fd_space->space->visible) {
173                                 fl_hide_form(fd_space->space);
174                         }
175                 }
176                 if (fd_matrix) {
177                         if (fd_matrix->matrix->visible) {
178                                 fl_hide_form(fd_matrix->matrix);
179                         }
180                 }
181         }
182         HideFiguresPopups();
183 }
184
185 // Extract shortcut from <ident>|<shortcut> string
186 char const * flyx_shortcut_extract(char const * sc)
187 {
188         // Find '|' in the sc and return the string after that.
189         register char const * sd = sc;
190         while(sd[0]!= 0 && sd[0] != '|') ++sd;
191
192         if (sd[0] == '|') {
193                 ++sd;
194                 //lyxerr << sd << endl;
195                 return sd;
196         }
197         return "";
198 }
199
200
201 // Extract identifier from <ident>|<shortcut> string
202 char const * flyx_ident_extract(char const * sc)
203 {
204         register char const * se = sc;
205         while(se[0]!= 0 && se[0] != '|') ++se;
206
207         if (se[0] == 0) return sc;
208         
209         char * sb = new char[se - sc + 1];
210         int index = 0;
211         register char const * sd = sc;
212         while (sd != se) {
213                 sb[index] = sd[0];
214                 ++index; ++sd;
215         }
216         sb[index] = 0;
217         return sb;
218 }
219
220
221 void WriteStatus(MiniBuffer * minib, string const & s)
222 {
223         if (minib) {
224                 minib->Set(s);
225                 minib->Store();
226         } else
227                 lyxerr << s << endl;
228 }
229
230
231 //
232 void WriteAlert(string const & s1, string const & s2, string const & s3)
233 {
234         MiniBuffer * minibuffer = 0;
235         if (current_view && current_view->owner())
236                 minibuffer = current_view->owner()->getMiniBuffer();
237         if (minibuffer) {
238                 /// Write to minibuffer
239                 ProhibitInput(current_view);
240                 minibuffer->Set(s1, s2, s3);
241                 fl_set_resource("flAlert.dismiss.label", _("Dismiss"));
242                 fl_show_alert(s1.c_str(), s2.c_str(), s3.c_str(), 0);
243                 AllowInput(current_view);
244         } else {
245                 /// Write to lyxerr
246                 lyxerr << "----------------------------------------" << endl
247                        << s1 << endl << s2 << endl << s3 << endl
248                        << "----------------------------------------" << endl;
249         }
250 }
251
252
253 // Alarms user of something related to files
254 void WriteFSAlert(string const & s1, string const & s2)
255 {
256         WriteAlert(s1, s2, strerror(errno));
257 }
258
259
260 bool AskQuestion(string const & s1, string const & s2, string const & s3,
261                  bool default_value)
262 {
263         if (!lyxrc.use_gui) {
264                 lyxerr << "----------------------------------------" << endl
265                        << s1 << endl;
266                 if (!s2.empty())
267                         lyxerr << s2 << endl;
268                 if (!s3.empty())
269                         lyxerr << s3 << endl;
270                 lyxerr << "Assuming answer is "
271                        << (default_value ? "yes" : "no")
272                        << endl
273                        << "----------------------------------------" << endl;
274                 return default_value;
275         }
276
277         fl_set_resource("flQuestion.yes.label", idex(_("Yes|Yy#y")));
278         fl_set_resource("flQuestion.no.label", idex(_("No|Nn#n")));
279         return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
280 }
281
282
283 // Returns 1 for yes, 2 for no, 3 for cancel.
284 int AskConfirmation(string const & s1, string const & s2, string const & s3,
285                     int default_value)
286 {
287         if (!lyxrc.use_gui) {
288                 lyxerr << "----------------------------------------" << endl
289                        << s1 << endl;
290                 if (!s2.empty())
291                         lyxerr << s2 << endl;
292                 if (!s3.empty())
293                         lyxerr << s3 << endl;
294                 lyxerr << "Assuming answer is ";
295                 if (default_value == 1)
296                         lyxerr << "yes";
297                 else if (default_value == 2)
298                         lyxerr << "no";
299                 else
300                         lyxerr << "cancel";
301                 lyxerr << endl
302                        << "----------------------------------------" << endl;
303                 return default_value;
304         }
305
306         fl_set_choices_shortcut(scex(_("Yes|Yy#y")),
307                                 scex(_("No|Nn#n")),
308                                 scex(_("Cancel|^[")));
309         return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(), 
310                               3, idex(_("Yes|Yy#y")),
311                               idex(_("No|Nn#n")),
312                               idex(_("Cancel|^[")), 3);
313 }
314
315
316 // Asks for a text
317 pair<bool, string> const
318 askForText(string const & msg, string const & dflt)
319 {
320         if (!lyxrc.use_gui) {
321                 lyxerr << "----------------------------------------" << endl
322                        << msg << endl
323                        << "Assuming answer is " << dflt
324                        << "----------------------------------------" << endl;
325                 return make_pair<bool, string>(true, dflt);
326         }
327
328         fl_set_resource("flInput.cancel.label", idex(_("Cancel|^[")));
329         fl_set_resource("flInput.ok.label", idex(_("OK|#O")));
330         fl_set_resource("flInput.clear.label", idex(_("Clear|#e")));
331         char const * tmp = fl_show_input(msg.c_str(), dflt.c_str());
332         if (tmp != 0)
333           return make_pair<bool, string>(true, string(tmp));
334         else
335           return make_pair<bool, string>(false, string());
336 }
337
338
339 // Inform the user that the buffer is read-only, and that changes in the
340 // dialog box that is to appear will be ignored.
341 void WarnReadonly(string const & file)
342 {
343         WriteAlert(_("Any changes will be ignored"),
344                    _("The document is read-only:"),
345                    MakeDisplayPath(file));
346 }
347
348 /// Get the dpi setting of the current screen
349 float getScreenDPI()
350 {
351         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen); //DefaultScreen(fl_get_display());
352         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
353                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
354 }