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