]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
The std::string mammoth path.
[lyx.git] / src / frontends / xforms / lyx_gui.C
1 /**
2  * \file xforms/lyx_gui.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "lyx_gui.h"
15 #include "ColorHandler.h"
16 #include "xfont_loader.h"
17 #include "xforms_helpers.h"
18 #include "xformsImage.h"
19 #include "XFormsView.h"
20
21 #include "bufferlist.h"
22 #include "BufferView.h"
23 #include "debug.h"
24 #include "gettext.h"
25 #include "LColor.h"
26 #include "lyx_main.h"
27 #include "lyxfunc.h"
28 #include "lyxrc.h"
29 #include "lyxserver.h"
30
31 #include "graphics/LoaderQueue.h"
32
33 #include "support/lyxlib.h"
34 #include "support/os.h"
35 #include "support/filetools.h"
36 #include "support/path_defines.h"
37
38 #include "lyx_forms.h"
39
40 #include <boost/bind.hpp>
41
42 #include "support/std_sstream.h"
43 #include <iomanip>
44 #include <fcntl.h>
45
46 using lyx::support::AddName;
47 using lyx::support::user_lyxdir;
48
49 namespace os = lyx::support::os;
50
51 #ifndef CXX_GLOBAL_CSTD
52 using std::exit;
53 #endif
54
55 using std::dec;
56 using std::endl;
57 using std::hex;
58 using std::setbase;
59 using std::setfill;
60 using std::setw;
61 using std::ostringstream;
62 using std::vector;
63 using std::string;
64
65
66 extern BufferList bufferlist;
67
68 // FIXME: wrong place !
69 LyXServer * lyxserver;
70
71 namespace {
72
73 /// quit lyx
74 bool finished = false;
75
76 /// estimate DPI from X server
77 float getDPI()
78 {
79         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
80         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
81                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
82 }
83
84
85 /// set default GUI configuration
86 void setDefaults()
87 {
88         FL_IOPT cntl;
89         cntl.buttonFontSize = FL_NORMAL_SIZE;
90         cntl.browserFontSize = FL_NORMAL_SIZE;
91         cntl.labelFontSize = FL_NORMAL_SIZE;
92         cntl.choiceFontSize = FL_NORMAL_SIZE;
93         cntl.inputFontSize = FL_NORMAL_SIZE;
94         cntl.menuFontSize  = FL_NORMAL_SIZE;
95         cntl.borderWidth = -1;
96         cntl.vclass = FL_DefaultVisual;
97         fl_set_defaults(FL_PDVisual
98                         | FL_PDButtonFontSize
99                         | FL_PDBrowserFontSize
100                         | FL_PDLabelFontSize
101                         | FL_PDChoiceFontSize
102                         | FL_PDInputFontSize
103                         | FL_PDMenuFontSize
104                         | FL_PDBorderWidth, &cntl);
105 }
106
107
108 extern "C" {
109
110 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
111         // We don't abort on BadWindow
112         if (xeev->error_code == BadWindow) {
113                 lyxerr << "BadWindow received !" << endl;
114                 lyxerr << "If you're using xforms 1.0 or greater, "
115                         << " please report this to lyx-devel@lists.lyx.org" << endl;
116                 return 0;
117         }
118
119         // emergency cleanup
120         LyX::emergencyCleanup();
121
122         // Get the reason for the crash.
123         char etxt[513];
124         XGetErrorText(display, xeev->error_code, etxt, 512);
125         lyxerr << etxt << " id: " << xeev->resourceid << endl;
126         // By doing an abort we get a nice backtrace. (hopefully)
127         lyx::support::abort();
128         return 0;
129 }
130
131 }
132
133 /// read in geometry specification
134 char geometry[40];
135
136 } // namespace anon
137
138
139 namespace lyx_gui {
140
141 bool use_gui = true;
142
143
144 void parse_init(int & argc, char * argv[])
145 {
146         setDefaults();
147
148         FL_CMD_OPT cmdopt[] = {
149                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
150         };
151
152         FL_resource res[] = {
153                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
154         };
155
156         const int num_res = sizeof(res)/sizeof(FL_resource);
157
158         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
159
160         // It appears that, in xforms >=0.89.5, fl_initialize()
161         // calls setlocale() and ruins our LC_NUMERIC setting.
162         locale_init();
163
164         fl_get_app_resources(res, num_res);
165
166         Display * display = fl_get_display();
167
168         if (!display) {
169                 lyxerr << "LyX: unable to access X display, exiting" << endl;
170                 os::warn("Unable to access X display, exiting");
171                 ::exit(1);
172         }
173
174         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
175
176         XSetErrorHandler(LyX_XErrHandler);
177
178         lyxColorHandler.reset(new LyXColorHandler);
179
180         using namespace lyx::graphics;
181
182         // connect the image loader based on the xforms library
183         Image::newImage = boost::bind(&xformsImage::newImage);
184         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
185
186         // must do this /before/ lyxrc gets read
187         lyxrc.dpi = getDPI();
188
189         LoaderQueue::setPriority(10,100);
190 }
191
192
193 void parse_lyxrc()
194 {
195         XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
196
197         if (lyxrc.popup_font_encoding.empty())
198                 lyxrc.popup_font_encoding = lyxrc.font_norm;
199         // Set the font name for popups and menus
200         string boldfontname = lyxrc.popup_bold_font
201                                + "-*-*-*-?-*-*-*-*-"
202                                + lyxrc.popup_font_encoding;
203                 // "?" means "scale that font"
204         string fontname = lyxrc.popup_normal_font
205                                + "-*-*-*-?-*-*-*-*-"
206                                + lyxrc.popup_font_encoding;
207
208         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
209         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
210         if (bold < 0)
211                 lyxerr << "Could not set menu font to "
212                        << boldfontname << endl;
213
214         if (normal < 0)
215                 lyxerr << "Could not set popup font to "
216                        << fontname << endl;
217
218         if (bold < 0 && normal < 0) {
219                 lyxerr << "Using 'helvetica' font for menus" << endl;
220                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
221                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
222                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
223                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
224
225                 if (bold < 0 && normal < 0) {
226                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
227                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
228                         normal = bold = 0;
229                 }
230         }
231         if (bold < 0)
232                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
233         else if (normal < 0)
234                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
235
236         fl_setpup_fontstyle(FL_NORMAL_STYLE);
237         fl_setpup_fontsize(FL_NORMAL_SIZE);
238         fl_setpup_color(FL_MCOL, FL_BLACK);
239         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
240         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
241 }
242
243
244 void start(string const & batch, vector<string> const & files)
245 {
246         // initial geometry
247         int xpos = -1;
248         int ypos = -1;
249         unsigned int width = 690;
250         unsigned int height = 510;
251
252         int const geometryBitmask =
253                 XParseGeometry(geometry,
254                                &xpos, &ypos, &width, &height);
255
256         // if width is not set by geometry, check it against monitor width
257         if (!(geometryBitmask & WidthValue)) {
258                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
259                 if (WidthOfScreen(scr) - 8 < int(width))
260                         width = WidthOfScreen(scr) - 8;
261         }
262
263         // if height is not set by geometry, check it against monitor height
264         if (!(geometryBitmask & HeightValue)) {
265                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
266                 if (HeightOfScreen(scr) - 24 < int(height))
267                         height = HeightOfScreen(scr) - 24;
268         }
269
270         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
271
272         // recalculate xpos if it's not set
273         if (xpos == -1)
274                 xpos = (WidthOfScreen(s) - width) / 2;
275
276         // recalculate ypos if it's not set
277         if (ypos == -1)
278                 ypos = (HeightOfScreen(s) - height) / 2;
279
280         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
281                            << '+' << xpos << '+' << ypos << endl;
282
283         XFormsView view(width, height);
284         view.show(xpos, ypos, "LyX");
285         view.init();
286
287         // FIXME: some code below needs moving
288
289         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
290
291         vector<string>::const_iterator cit = files.begin();
292         vector<string>::const_iterator end = files.end();
293         for (; cit != end; ++cit)
294                 view.view()->loadLyXFile(*cit, true);
295
296         // handle the batch commands the user asked for
297         if (!batch.empty())
298                 view.getLyXFunc().dispatch(batch);
299
300         // enter the event loop
301         while (!finished) {
302                 if (fl_check_forms() == FL_EVENT) {
303                         XEvent ev;
304                         fl_XNextEvent(&ev);
305                         lyxerr[Debug::GUI]
306                                 << "Received unhandled X11 event" << endl
307                                 << "Type: " << ev.xany.type
308                                 << " Target: 0x" << hex << ev.xany.window
309                                 << dec << endl;
310                 }
311         }
312
313         // FIXME: breaks emergencyCleanup
314         delete lyxserver;
315 }
316
317
318 void exit()
319 {
320         finished = true;
321 }
322
323
324 void sync_events()
325 {
326         // FIXME
327 }
328
329
330 FuncStatus getStatus(FuncRequest const & /*ev*/)
331 {
332         // Nothing interesting to do here
333         return FuncStatus();
334 }
335
336 string const hexname(LColor_color col)
337 {
338         unsigned int r, g, b;
339         bool const success = getRGBColor(col, r, g, b);
340         if (!success) {
341                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
342                        << '"' << endl;
343                 return string();
344         }
345
346         ostringstream os;
347
348         os << setbase(16) << setfill('0')
349            << setw(2) << r
350            << setw(2) << g
351            << setw(2) << b;
352
353         return os.str();
354 }
355
356
357 void update_color(LColor_color col)
358 {
359         lyxColorHandler->updateColor(col);
360 }
361
362
363 void update_fonts()
364 {
365         fontloader.update();
366 }
367
368
369 bool font_available(LyXFont const & font)
370 {
371         return fontloader.available(font);
372 }
373
374 namespace {
375
376 extern "C"
377 void C_read_callback(int, void * data)
378 {
379         LyXComm * comm = static_cast<LyXComm *>(data);
380         comm->read_ready();
381 }
382
383 }
384
385 void set_read_callback(int fd, LyXComm * comm)
386 {
387         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
388 }
389
390
391 void remove_read_callback(int fd)
392 {
393         fl_remove_io_callback(fd, FL_READ, C_read_callback);
394 }
395
396
397 string const roman_font_name()
398 {
399         return "times";
400 }
401
402
403 string const sans_font_name()
404 {
405         return "helvetica";
406 }
407
408
409 string const typewriter_font_name()
410 {
411         return "courier";
412 }
413
414 }; // namespace lyx_gui