]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
remove defaults stuff, let Qt handle no toolbar
[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 unknown
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 "lyxfunc.h"
31 #include "lyxserver.h"
32 #include "BufferView.h"
33 #include "XFormsView.h"
34
35 #include FORMS_H_LOCATION
36 #include "ColorHandler.h"
37 #include "xforms_helpers.h"
38 #include "xfont_loader.h"
39 #include "xformsImage.h"
40
41 #include "Lsstream.h"
42 #include <iomanip>
43 #include <fcntl.h>
44 #include <boost/bind.hpp>
45
46 #ifndef CXX_GLOBAL_CSTD
47 using std::exit;
48 #endif
49
50 using std::vector;
51 using std::hex;
52 using std::dec;
53 using std::endl;
54 using std::setbase;
55 using std::setfill;
56 using std::setw;
57
58 extern BufferList bufferlist;
59
60 // FIXME: wrong place !
61 LyXServer * lyxserver;
62
63 namespace {
64
65 /// quit lyx
66 bool finished = false;
67
68 /// estimate DPI from X server
69 float getDPI()
70 {
71         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
72         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
73                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
74 }
75
76
77 /// set default GUI configuration
78 void setDefaults()
79 {
80         FL_IOPT cntl;
81         cntl.buttonFontSize = FL_NORMAL_SIZE;
82         cntl.browserFontSize = FL_NORMAL_SIZE;
83         cntl.labelFontSize = FL_NORMAL_SIZE;
84         cntl.choiceFontSize = FL_NORMAL_SIZE;
85         cntl.inputFontSize = FL_NORMAL_SIZE;
86         cntl.menuFontSize  = FL_NORMAL_SIZE;
87         cntl.borderWidth = -1;
88         cntl.vclass = FL_DefaultVisual;
89         fl_set_defaults(FL_PDVisual
90                         | FL_PDButtonFontSize
91                         | FL_PDBrowserFontSize
92                         | FL_PDLabelFontSize
93                         | FL_PDChoiceFontSize
94                         | FL_PDInputFontSize
95                         | FL_PDMenuFontSize
96                         | FL_PDBorderWidth, &cntl);
97 }
98
99
100 extern "C" {
101
102 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
103         // We don't abort on BadWindow
104         if (xeev->error_code == BadWindow) {
105                 lyxerr << "BadWindow received !" << endl;
106                 lyxerr << "If you're using xforms 1.0 or greater, "
107                         << " please report this to lyx-devel@lists.lyx.org" << endl;
108                 return 0;
109         }
110
111         // emergency cleanup
112         LyX::emergencyCleanup();
113
114         // Get the reason for the crash.
115         char etxt[513];
116         XGetErrorText(display, xeev->error_code, etxt, 512);
117         lyxerr << etxt << " id: " << xeev->resourceid << endl;
118         // By doing an abort we get a nice backtrace. (hopefully)
119         lyx::abort();
120         return 0;
121 }
122
123 }
124
125 /// read in geometry specification
126 char geometry[40];
127
128 } // namespace anon
129
130
131 namespace lyx_gui {
132
133 bool use_gui = true;
134
135
136 void parse_init(int & argc, char * argv[])
137 {
138         setDefaults();
139
140         FL_CMD_OPT cmdopt[] = {
141                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
142         };
143
144         FL_resource res[] = {
145                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
146         };
147
148         const int num_res = sizeof(res)/sizeof(FL_resource);
149
150         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
151
152         // It appears that, in xforms >=0.89.5, fl_initialize()
153         // calls setlocale() and ruins our LC_NUMERIC setting.
154         locale_init();
155
156         fl_get_app_resources(res, num_res);
157
158         Display * display = fl_get_display();
159
160         if (!display) {
161                 lyxerr << "LyX: unable to access X display, exiting" << endl;
162                 os::warn("Unable to access X display, exiting");
163                 ::exit(1);
164         }
165
166         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
167
168         XSetErrorHandler(LyX_XErrHandler);
169
170         lyxColorHandler.reset(new LyXColorHandler());
171
172         using namespace grfx;
173
174         // connect the image loader based on the xforms library
175         Image::newImage = boost::bind(&xformsImage::newImage);
176         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
177
178         // must do this /before/ lyxrc gets read
179         lyxrc.dpi = getDPI();
180
181         LoaderQueue::setPriority(10,100);
182 }
183
184
185 void parse_lyxrc()
186 {
187         XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
188
189         if (lyxrc.popup_font_encoding.empty())
190                 lyxrc.popup_font_encoding = lyxrc.font_norm;
191         // Set the font name for popups and menus
192         string boldfontname = lyxrc.popup_bold_font
193                                + "-*-*-*-?-*-*-*-*-"
194                                + lyxrc.popup_font_encoding;
195                 // "?" means "scale that font"
196         string fontname = lyxrc.popup_normal_font
197                                + "-*-*-*-?-*-*-*-*-"
198                                + lyxrc.popup_font_encoding;
199
200         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
201         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
202         if (bold < 0)
203                 lyxerr << "Could not set menu font to "
204                        << boldfontname << endl;
205
206         if (normal < 0)
207                 lyxerr << "Could not set popup font to "
208                        << fontname << endl;
209
210         if (bold < 0 && normal < 0) {
211                 lyxerr << "Using 'helvetica' font for menus" << endl;
212                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
213                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
214                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
215                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
216
217                 if (bold < 0 && normal < 0) {
218                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
219                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
220                         normal = bold = 0;
221                 }
222         }
223         if (bold < 0)
224                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
225         else if (normal < 0)
226                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
227
228         fl_setpup_fontstyle(FL_NORMAL_STYLE);
229         fl_setpup_fontsize(FL_NORMAL_SIZE);
230         fl_setpup_color(FL_MCOL, FL_BLACK);
231         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
232         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
233 }
234
235
236 void start(string const & batch, vector<string> const & files)
237 {
238         // initial geometry
239         int xpos = -1;
240         int ypos = -1;
241         unsigned int width = 690;
242         unsigned int height = 510;
243
244         int const geometryBitmask =
245                 XParseGeometry(geometry,
246                                &xpos, &ypos, &width, &height);
247
248         // if width is not set by geometry, check it against monitor width
249         if (!(geometryBitmask & WidthValue)) {
250                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
251                 if (WidthOfScreen(scr) - 8 < int(width))
252                         width = WidthOfScreen(scr) - 8;
253         }
254
255         // if height is not set by geometry, check it against monitor height
256         if (!(geometryBitmask & HeightValue)) {
257                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
258                 if (HeightOfScreen(scr) - 24 < int(height))
259                         height = HeightOfScreen(scr) - 24;
260         }
261
262         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
263
264         // recalculate xpos if it's not set
265         if (xpos == -1)
266                 xpos = (WidthOfScreen(s) - width) / 2;
267
268         // recalculate ypos if it's not set
269         if (ypos == -1)
270                 ypos = (HeightOfScreen(s) - height) / 2;
271
272         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
273                            << '+' << xpos << '+' << ypos << endl;
274
275         XFormsView view(width, height);
276         view.show(xpos, ypos, "LyX");
277         view.init();
278
279         Buffer * last = 0;
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                 Buffer * b = bufferlist.loadLyXFile(*cit);
289                 if (b) {
290                         last = b;
291                 }
292         }
293
294         // switch to the last buffer successfully loaded
295         if (last) {
296                 view.view()->buffer(last);
297         }
298
299         // handle the batch commands the user asked for
300         if (!batch.empty()) {
301                 view.getLyXFunc().dispatch(batch);
302         }
303
304         // enter the event loop
305         while (!finished) {
306                 if (fl_check_forms() == FL_EVENT) {
307                         XEvent ev;
308                         fl_XNextEvent(&ev);
309                         lyxerr[Debug::GUI]
310                                 << "Received unhandled X11 event" << endl
311                                 << "Type: " << ev.xany.type
312                                 << " Target: 0x" << hex << ev.xany.window
313                                 << dec << endl;
314                 }
315         }
316
317         // FIXME: breaks emergencyCleanup
318         delete lyxserver;
319 }
320
321
322 void exit()
323 {
324         finished = true;
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(LColor::color 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 STRCONV(os.str());
352 }
353
354
355 void update_color(LColor::color 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