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