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