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