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