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