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