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