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