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