]> git.lyx.org Git - lyx.git/blob - src/lyx_gui_misc.C
Applied Angus patch to compile on DEC C++ and to avoid name clashes
[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 "minibuffer.h"
28 #include "print_form.h"
29 #include "sp_form.h"
30 #include "LyXView.h"
31 #include "bufferview_funcs.h"
32 #include "support/filetools.h"
33 #include "lyxrc.h"
34
35 using std::pair;
36 using std::make_pair;
37 using std::endl;
38
39 extern BufferView * current_view;
40
41 extern FD_form_figure * fd_form_figure;
42 extern FD_form_sendto * fd_form_sendto;
43 extern FD_form_spell_check * fd_form_spell_check;
44 extern FD_form_spell_options * fd_form_spell_options;
45
46 extern void HideFiguresPopups();
47
48 // Prevents LyX from being killed when the close box is pressed in a popup.
49 extern "C" int CancelCloseBoxCB(FL_FORM *, void *)
50 {
51         return FL_CANCEL;
52 }
53 // Redraw the form (on receipt of a Signal indicating, for example,
54 // that the xform colors have been re-mapped).
55 void RedrawAllBufferRelatedDialogs()
56 {
57         if (fd_form_figure->form_figure->visible) {
58                 fl_redraw_form(fd_form_figure->form_figure);
59         }
60         if (fd_form_sendto->form_sendto->visible) {
61                 fl_redraw_form(fd_form_sendto->form_sendto);
62         }
63         if (fd_form_spell_check &&
64             fd_form_spell_check->form_spell_check->visible) {
65                 fl_redraw_form(fd_form_spell_check->form_spell_check);
66         }
67         if (fd_form_spell_options &&
68             fd_form_spell_options->form_spell_options->visible) {
69                 fl_redraw_form(fd_form_spell_options->form_spell_options);
70         }
71 }
72
73 // Prevents LyX from crashing when no buffers available
74 // This is also one of the functions that we _really_ dont want
75 // we should try to finds way to help us with that.
76 // The signal/slot mechanism can probably help. 
77 void CloseAllBufferRelatedDialogs()
78 {
79         // don't forget to check that dynamically created forms
80         // have been created otherwise hiding one could cause a crash
81         // need the visible check otherwise XForms prints a warning
82         // if hiding an invisible form
83         if (fd_form_figure->form_figure->visible) {
84                 fl_hide_form(fd_form_figure->form_figure);
85         }
86         if (fd_form_sendto->form_sendto->visible) {
87                 fl_hide_form(fd_form_sendto->form_sendto);
88         }
89         if (fd_form_spell_check) {
90                 if (fd_form_spell_check->form_spell_check->visible) {
91                         fl_trigger_object(fd_form_spell_check->done);
92                 }
93         }
94         HideFiguresPopups();
95 }
96
97 // This is another function we really don't want.
98 // Again the Signal/Slot mechanism is tailor made for this task.
99 void updateAllVisibleBufferRelatedDialogs(bool)
100 {
101         HideFiguresPopups();
102 }
103
104 // Extract shortcut from <ident>|<shortcut> string
105 char const * flyx_shortcut_extract(char const * sc)
106 {
107         // Find '|' in the sc and return the string after that.
108         register char const * sd = sc;
109         while(sd[0]!= 0 && sd[0] != '|') ++sd;
110
111         if (sd[0] == '|') {
112                 ++sd;
113                 //lyxerr << sd << endl;
114                 return sd;
115         }
116         return "";
117 }
118
119
120 // Extract identifier from <ident>|<shortcut> string
121 char const * flyx_ident_extract(char const * sc)
122 {
123         register char const * se = sc;
124         while(se[0]!= 0 && se[0] != '|') ++se;
125
126         if (se[0] == 0) return sc;
127         
128         char * sb = new char[se - sc + 1];
129         int index = 0;
130         register char const * sd = sc;
131         while (sd != se) {
132                 sb[index] = sd[0];
133                 ++index; ++sd;
134         }
135         sb[index] = 0;
136         return sb;
137 }
138
139
140 void WriteStatus(MiniBuffer * minib, string const & s)
141 {
142         if (minib) {
143                 minib->Set(s);
144                 minib->Store();
145         } else
146                 lyxerr << s << endl;
147 }
148
149
150 //
151 void WriteAlert(string const & s1, string const & s2, string const & s3)
152 {
153         MiniBuffer * minibuffer = 0;
154         if (current_view && current_view->owner())
155                 minibuffer = current_view->owner()->getMiniBuffer();
156         if (minibuffer) {
157                 /// Write to minibuffer
158                 ProhibitInput(current_view);
159                 minibuffer->Set(s1, s2, s3);
160                 fl_set_resource("flAlert.dismiss.label", _("Dismiss"));
161                 fl_show_alert(s1.c_str(), s2.c_str(), s3.c_str(), 0);
162                 AllowInput(current_view);
163         } else {
164                 /// Write to lyxerr
165                 lyxerr << "----------------------------------------" << endl
166                        << s1 << endl << s2 << endl << s3 << endl
167                        << "----------------------------------------" << endl;
168         }
169 }
170
171
172 // Alarms user of something related to files
173 void WriteFSAlert(string const & s1, string const & s2)
174 {
175         WriteAlert(s1, s2, strerror(errno));
176 }
177
178
179 bool AskQuestion(string const & s1, string const & s2, string const & s3,
180                  bool default_value)
181 {
182         if (!lyxrc.use_gui) {
183                 lyxerr << "----------------------------------------" << endl
184                        << s1 << endl;
185                 if (!s2.empty())
186                         lyxerr << s2 << endl;
187                 if (!s3.empty())
188                         lyxerr << s3 << endl;
189                 lyxerr << "Assuming answer is "
190                        << (default_value ? "yes" : "no")
191                        << endl
192                        << "----------------------------------------" << endl;
193                 return default_value;
194         }
195
196         fl_set_resource("flQuestion.yes.label", idex(_("Yes|Yy#y")));
197         fl_set_resource("flQuestion.no.label", idex(_("No|Nn#n")));
198         return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
199 }
200
201
202 // Returns 1 for yes, 2 for no, 3 for cancel.
203 int AskConfirmation(string const & s1, string const & s2, string const & s3,
204                     int default_value)
205 {
206         if (!lyxrc.use_gui) {
207                 lyxerr << "----------------------------------------" << endl
208                        << s1 << endl;
209                 if (!s2.empty())
210                         lyxerr << s2 << endl;
211                 if (!s3.empty())
212                         lyxerr << s3 << endl;
213                 lyxerr << "Assuming answer is ";
214                 if (default_value == 1)
215                         lyxerr << "yes";
216                 else if (default_value == 2)
217                         lyxerr << "no";
218                 else
219                         lyxerr << "cancel";
220                 lyxerr << endl
221                        << "----------------------------------------" << endl;
222                 return default_value;
223         }
224
225         fl_set_choices_shortcut(scex(_("Yes|Yy#y")),
226                                 scex(_("No|Nn#n")),
227                                 scex(_("Cancel|^[")));
228         return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(), 
229                               3, idex(_("Yes|Yy#y")),
230                               idex(_("No|Nn#n")),
231                               idex(_("Cancel|^[")), 3);
232 }
233
234
235 // Asks for a text
236 pair<bool, string> const
237 askForText(string const & msg, string const & dflt)
238 {
239         if (!lyxrc.use_gui) {
240                 lyxerr << "----------------------------------------" << endl
241                        << msg << endl
242                        << "Assuming answer is " << dflt
243                        << "----------------------------------------" << endl;
244                 return make_pair<bool, string>(true, dflt);
245         }
246
247         fl_set_resource("flInput.cancel.label", idex(_("Cancel|^[")));
248         fl_set_resource("flInput.ok.label", idex(_("OK|#O")));
249         fl_set_resource("flInput.clear.label", idex(_("Clear|#e")));
250         char const * tmp = fl_show_input(msg.c_str(), dflt.c_str());
251         if (tmp != 0)
252           return make_pair<bool, string>(true, string(tmp));
253         else
254           return make_pair<bool, string>(false, string());
255 }
256
257
258 // Inform the user that the buffer is read-only, and that changes in the
259 // dialog box that is to appear will be ignored.
260 void WarnReadonly(string const & file)
261 {
262         WriteAlert(_("Any changes will be ignored"),
263                    _("The document is read-only:"),
264                    MakeDisplayPath(file));
265 }
266
267 /// Get the dpi setting of the current screen
268 float getScreenDPI()
269 {
270         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen); //DefaultScreen(fl_get_display());
271         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
272                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
273 }