]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
fix dpi
[lyx.git] / src / frontends / xforms / lyx_gui.C
1 /**
2  * \file lyx_gui.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #include "lyx_gui.h"
13
14 #include "support/lyxlib.h"
15 #include "support/os.h"
16 #include "support/filetools.h"
17
18 #include "debug.h"
19 #include "gettext.h"
20
21 #include "lyx_main.h"
22 #include "lyxrc.h"
23 #include "lyxfont.h"
24
25 // FIXME: move this stuff out again
26 #include "bufferlist.h"
27 #include "lyxfunc.h"
28 #include "lyxserver.h"
29 #include "BufferView.h"
30 #include "XFormsView.h"
31
32 #include FORMS_H_LOCATION
33 #include "ColorHandler.h"
34 #include "xforms_helpers.h"
35 #include "xfont_loader.h"
36 #ifdef USE_XFORMS_IMAGE_LOADER
37 #include "xformsImage.h"
38 #else
39 #include "graphics/GraphicsImageXPM.h"
40 #endif
41
42 #include "Lsstream.h"
43 #include <iomanip>
44 #include <fcntl.h>
45 #include <boost/bind.hpp>
46
47 #ifndef CXX_GLOBAL_CSTD
48 using std::exit;
49 #endif
50
51 using std::vector;
52 using std::hex;
53 using std::dec;
54 using std::endl;
55 using std::setbase;
56 using std::setfill;
57 using std::setw;
58
59 extern BufferList bufferlist;
60
61 // FIXME: wrong place !
62 LyXServer * lyxserver;
63
64 namespace {
65
66 /// quit lyx
67 bool finished = false;
68
69 /// estimate DPI from X server
70 float getDPI()
71 {
72         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
73         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
74                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2; 
75 }
76
77  
78 /// set default GUI configuration
79 void setDefaults()
80 {
81         FL_IOPT cntl;
82         cntl.buttonFontSize = FL_NORMAL_SIZE;
83         cntl.browserFontSize = FL_NORMAL_SIZE;
84         cntl.labelFontSize = FL_NORMAL_SIZE;
85         cntl.choiceFontSize = FL_NORMAL_SIZE;
86         cntl.inputFontSize = FL_NORMAL_SIZE;
87         cntl.menuFontSize  = FL_NORMAL_SIZE;
88         cntl.borderWidth = -1;
89         cntl.vclass = FL_DefaultVisual;
90         fl_set_defaults(FL_PDVisual
91                         | FL_PDButtonFontSize
92                         | FL_PDBrowserFontSize
93                         | FL_PDLabelFontSize
94                         | FL_PDChoiceFontSize
95                         | FL_PDInputFontSize
96                         | FL_PDMenuFontSize
97                         | FL_PDBorderWidth, &cntl);
98 }
99
100
101 extern "C" {
102
103 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
104         // We don't abort on BadWindow
105         if (xeev->error_code == BadWindow) {
106                 lyxerr << "BadWindow received !" << endl;
107                 lyxerr << "If you're using xforms 1.0 or greater, "
108                         << " please report this to lyx-devel@lists.lyx.org" << endl;
109                 return 0;
110         }
111
112         // emergency cleanup
113         LyX::emergencyCleanup();
114
115         // Get the reason for the crash.
116         char etxt[513];
117         XGetErrorText(display, xeev->error_code, etxt, 512);
118         lyxerr << etxt << " id: " << xeev->resourceid << endl;
119         // By doing an abort we get a nice backtrace. (hopefully)
120         lyx::abort();
121         return 0;
122 }
123
124 }
125
126 /// read in geometry specification
127 char geometry[40];
128
129 } // namespace anon
130
131
132 void lyx_gui::parse_init(int & argc, char * argv[])
133 {
134         setDefaults();
135
136         FL_CMD_OPT cmdopt[] = {
137                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
138         };
139
140         FL_resource res[] = {
141                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
142         };
143
144         const int num_res = sizeof(res)/sizeof(FL_resource);
145
146         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
147
148         // It appears that, in xforms >=0.89.5, fl_initialize()
149         // calls setlocale() and ruins our LC_NUMERIC setting.
150         locale_init();
151
152         fl_get_app_resources(res, num_res);
153
154         Display * display = fl_get_display();
155
156         if (!display) {
157                 lyxerr << "LyX: unable to access X display, exiting" << endl;
158                 os::warn("Unable to access X display, exiting");
159                 ::exit(1);
160         }
161
162         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
163
164         XSetErrorHandler(LyX_XErrHandler);
165
166         lyxColorHandler.reset(new LyXColorHandler());
167
168         using namespace grfx;
169
170 #ifdef USE_XFORMS_IMAGE_LOADER
171         // connect the image loader based on the xforms library
172         Image::newImage = boost::bind(&xformsImage::newImage);
173         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
174 #else
175         // connect the image loader based on the XPM library
176         Image::newImage = boost::bind(&ImageXPM::newImage);
177         Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
178 #endif
179
180         // must do this /before/ lyxrc gets read 
181         lyxrc.dpi = getDPI();
182 }
183
184
185 void lyx_gui::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 #if FL_REVISION < 89
233         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
234 #else
235         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
236 #endif
237 }
238
239
240 void lyx_gui::start(string const & batch, vector<string> files)
241 {
242         // initial geometry
243         int xpos = -1;
244         int ypos = -1;
245         unsigned int width = 690;
246         unsigned int height = 510;
247
248         static const int geometryBitmask =
249                 XParseGeometry(geometry,
250                                 &xpos, &ypos, &width, &height);
251
252         // if width is not set by geometry, check it against monitor width
253         if (!(geometryBitmask & 4)) {
254                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
255                 if (WidthOfScreen(scr) - 8 < int(width))
256                         width = WidthOfScreen(scr) - 8;
257         }
258
259         // if height is not set by geometry, check it against monitor height
260         if (!(geometryBitmask & 8)) {
261                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
262                 if (HeightOfScreen(scr) - 24 < int(height))
263                         height = HeightOfScreen(scr) - 24;
264         }
265
266         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
267
268         // recalculate xpos if it's not set
269         if (xpos == -1)
270                 xpos = (WidthOfScreen(s) - width) / 2;
271
272         // recalculate ypos if it's not set
273         if (ypos == -1)
274                 ypos = (HeightOfScreen(s) - height) / 2;
275
276         lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
277                 << "+" << xpos << "+" << ypos << endl;
278
279         XFormsView view(width, height);
280         view.show(xpos, ypos, "LyX");
281         view.init();
282
283         Buffer * last = 0;
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                 Buffer * b = bufferlist.loadLyXFile(*cit);
293                 if (b) {
294                         last = b;
295                 }
296         }
297
298         // switch to the last buffer successfully loaded
299         if (last) {
300                 view.view()->buffer(last);
301         }
302
303         // handle the batch commands the user asked for
304         if (!batch.empty()) {
305                 view.getLyXFunc().dispatch(batch);
306         }
307
308         // enter the event loop
309         while (!finished) {
310                 if (fl_check_forms() == FL_EVENT) {
311                         XEvent ev;
312                         fl_XNextEvent(&ev);
313                         lyxerr << "Received unhandled X11 event" << endl;
314                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
315                                 " Target: 0x" << hex << ev.xany.window << dec << endl;
316                 }
317         }
318
319         // FIXME: breaks emergencyCleanup
320         delete lyxserver;
321 }
322
323
324 void lyx_gui::exit()
325 {
326         finished = true;
327 }
328
329
330 string const lyx_gui::hexname(LColor::color col)
331 {
332         string const name = lcolor.getX11Name(col);
333         Display * const display = fl_get_display();
334         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
335         XColor xcol, ccol;
336
337         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
338                         lyxerr << "X can't find color \""
339                                << lcolor.getLyXName(col)
340                                << "\"" << endl;
341                         return string();
342         }
343
344         ostringstream os;
345
346         // Note that X stores the RGB values in the range 0 - 65535
347         // whilst we require them in the range 0 - 255.
348         os << setbase(16) << setfill('0')
349            << setw(2) << (xcol.red   / 256)
350            << setw(2) << (xcol.green / 256)
351            << setw(2) << (xcol.blue  / 256);
352
353         return os.str().c_str();
354 }
355
356
357 void lyx_gui::update_color(LColor::color col)
358 {
359         lyxColorHandler->updateColor(col);
360 }
361
362
363 void lyx_gui::update_fonts()
364 {
365         fontloader.update();
366 }
367
368
369 bool lyx_gui::font_available(LyXFont const & font)
370 {
371         return fontloader.available(font);
372 }
373
374 namespace {
375
376 extern "C"
377 void C_read_callback(int, void * data)
378 {
379         LyXComm * comm = static_cast<LyXComm *>(data);
380         comm->read_ready();
381 }
382
383 }
384
385 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
386 {
387         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
388 }
389
390
391 void lyx_gui::remove_read_callback(int fd)
392 {
393         fl_remove_io_callback(fd, FL_READ, C_read_callback);
394 }