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