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