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