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