]> git.lyx.org Git - lyx.git/blob - src/lyx_gui_misc.C
fixed two consecutive spaces handling
[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 "print_form.h"
27 #include "sp_form.h"
28 #include "LyXView.h"
29 #include "bufferview_funcs.h"
30 #include "support/filetools.h"
31 #include "lyxrc.h"
32 #include "lyxfunc.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 //
140 void WriteAlert(string const & s1, string const & s2, string const & s3)
141 {
142         LyXFunc * lfun = 0;
143         if (current_view && current_view->owner())
144                 lfun = current_view->owner()->getLyXFunc();
145         if (lfun) {
146                 /// Write to minibuffer
147                 ProhibitInput(current_view);
148                 string const msg = s1 + ' ' + s2 + ' ' + s3;
149                 lfun->Dispatch(LFUN_MESSAGE, msg);
150                 fl_set_resource("flAlert.dismiss.label", _("Dismiss"));
151                 fl_show_alert(s1.c_str(), s2.c_str(), s3.c_str(), 0);
152                 AllowInput(current_view);
153         } else {
154                 /// Write to lyxerr
155                 lyxerr << "----------------------------------------" << endl
156                        << s1 << endl << s2 << endl << s3 << endl
157                        << "----------------------------------------" << endl;
158         }
159 }
160
161
162 // Alarms user of something related to files
163 void WriteFSAlert(string const & s1, string const & s2)
164 {
165         WriteAlert(s1, s2, strerror(errno));
166 }
167
168
169 bool AskQuestion(string const & s1, string const & s2, string const & s3,
170                  bool default_value)
171 {
172         if (!lyxrc.use_gui) {
173                 lyxerr << "----------------------------------------" << endl
174                        << s1 << endl;
175                 if (!s2.empty())
176                         lyxerr << s2 << endl;
177                 if (!s3.empty())
178                         lyxerr << s3 << endl;
179                 lyxerr << "Assuming answer is "
180                        << (default_value ? "yes" : "no")
181                        << endl
182                        << "----------------------------------------" << endl;
183                 return default_value;
184         }
185
186         fl_set_resource("flQuestion.yes.label", idex(_("Yes|Yy#y")));
187         fl_set_resource("flQuestion.no.label", idex(_("No|Nn#n")));
188         return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
189 }
190
191
192 // Returns 1 for yes, 2 for no, 3 for cancel.
193 int AskConfirmation(string const & s1, string const & s2, string const & s3,
194                     int default_value)
195 {
196         if (!lyxrc.use_gui) {
197                 lyxerr << "----------------------------------------" << endl
198                        << s1 << endl;
199                 if (!s2.empty())
200                         lyxerr << s2 << endl;
201                 if (!s3.empty())
202                         lyxerr << s3 << endl;
203                 lyxerr << "Assuming answer is ";
204                 if (default_value == 1)
205                         lyxerr << "yes";
206                 else if (default_value == 2)
207                         lyxerr << "no";
208                 else
209                         lyxerr << "cancel";
210                 lyxerr << endl
211                        << "----------------------------------------" << endl;
212                 return default_value;
213         }
214
215         fl_set_choices_shortcut(scex(_("Yes|Yy#y")),
216                                 scex(_("No|Nn#n")),
217                                 scex(_("Cancel|^[")));
218         return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(), 
219                               3, idex(_("Yes|Yy#y")),
220                               idex(_("No|Nn#n")),
221                               idex(_("Cancel|^[")), 3);
222 }
223
224
225 // Asks for a text
226 pair<bool, string> const
227 askForText(string const & msg, string const & dflt)
228 {
229         if (!lyxrc.use_gui) {
230                 lyxerr << "----------------------------------------" << endl
231                        << msg << endl
232                        << "Assuming answer is " << dflt
233                        << "----------------------------------------" << endl;
234                 return make_pair<bool, string>(true, dflt);
235         }
236
237         fl_set_resource("flInput.cancel.label", idex(_("Cancel|^[")));
238         fl_set_resource("flInput.ok.label", idex(_("OK|#O")));
239         fl_set_resource("flInput.clear.label", idex(_("Clear|#e")));
240         char const * tmp = fl_show_input(msg.c_str(), dflt.c_str());
241         if (tmp != 0)
242           return make_pair<bool, string>(true, string(tmp));
243         else
244           return make_pair<bool, string>(false, string());
245 }
246
247
248 // Inform the user that the buffer is read-only, and that changes in the
249 // dialog box that is to appear will be ignored.
250 void WarnReadonly(string const & file)
251 {
252         WriteAlert(_("Any changes will be ignored"),
253                    _("The document is read-only:"),
254                    MakeDisplayPath(file));
255 }
256
257 /// Get the dpi setting of the current screen
258 float getScreenDPI()
259 {
260         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen); //DefaultScreen(fl_get_display());
261         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
262                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
263 }