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