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