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