]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
Really dull and boring header shit
[lyx.git] / src / frontends / xforms / lyx_gui.C
1 /**
2  * \file lyx_gui.C
3  * Read the file COPYING
4  *
5  * \author unknown
6  * \author John Levon 
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "lyx_gui.h"
14
15 #include "support/lyxlib.h"
16 #include "support/os.h"
17 #include "support/filetools.h"
18
19 #include "debug.h"
20 #include "gettext.h"
21
22 #include "lyx_main.h"
23 #include "lyxrc.h"
24 #include "lyxfont.h"
25
26 // FIXME: move this stuff out again
27 #include "bufferlist.h"
28 #include "lyxfunc.h"
29 #include "lyxserver.h"
30 #include "BufferView.h"
31 #include "XFormsView.h"
32
33 #include FORMS_H_LOCATION
34 #include "ColorHandler.h"
35 #include "xforms_helpers.h"
36 #include "xfont_loader.h"
37 #ifdef USE_XFORMS_IMAGE_LOADER
38 #include "xformsImage.h"
39 #else
40 #include "graphics/GraphicsImageXPM.h"
41 #endif
42
43 #include "Lsstream.h"
44 #include <iomanip>
45 #include <fcntl.h>
46 #include <boost/bind.hpp>
47
48 #ifndef CXX_GLOBAL_CSTD
49 using std::exit;
50 #endif
51
52 using std::vector;
53 using std::hex;
54 using std::dec;
55 using std::endl;
56 using std::setbase;
57 using std::setfill;
58 using std::setw;
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::abort();
122         return 0;
123 }
124
125 }
126
127 /// read in geometry specification
128 char geometry[40];
129
130 } // namespace anon
131
132
133 void lyx_gui::parse_init(int & argc, char * argv[])
134 {
135         setDefaults();
136
137         FL_CMD_OPT cmdopt[] = {
138                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
139         };
140
141         FL_resource res[] = {
142                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
143         };
144
145         const int num_res = sizeof(res)/sizeof(FL_resource);
146
147         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
148
149         // It appears that, in xforms >=0.89.5, fl_initialize()
150         // calls setlocale() and ruins our LC_NUMERIC setting.
151         locale_init();
152
153         fl_get_app_resources(res, num_res);
154
155         Display * display = fl_get_display();
156
157         if (!display) {
158                 lyxerr << "LyX: unable to access X display, exiting" << endl;
159                 os::warn("Unable to access X display, exiting");
160                 ::exit(1);
161         }
162
163         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
164
165         XSetErrorHandler(LyX_XErrHandler);
166
167         lyxColorHandler.reset(new LyXColorHandler());
168
169         using namespace grfx;
170
171 #ifdef USE_XFORMS_IMAGE_LOADER
172         // connect the image loader based on the xforms library
173         Image::newImage = boost::bind(&xformsImage::newImage);
174         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
175 #else
176         // connect the image loader based on the XPM library
177         Image::newImage = boost::bind(&ImageXPM::newImage);
178         Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
179 #endif
180
181         // must do this /before/ lyxrc gets read 
182         lyxrc.dpi = getDPI();
183 }
184
185
186 void lyx_gui::parse_lyxrc()
187 {
188         XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
189
190         if (lyxrc.popup_font_encoding.empty())
191                 lyxrc.popup_font_encoding = lyxrc.font_norm;
192         // Set the font name for popups and menus
193         string boldfontname = lyxrc.popup_bold_font
194                                + "-*-*-*-?-*-*-*-*-"
195                                + lyxrc.popup_font_encoding;
196                 // "?" means "scale that font"
197         string fontname = lyxrc.popup_normal_font
198                                + "-*-*-*-?-*-*-*-*-"
199                                + lyxrc.popup_font_encoding;
200
201         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
202         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
203         if (bold < 0)
204                 lyxerr << "Could not set menu font to "
205                        << boldfontname << endl;
206
207         if (normal < 0)
208                 lyxerr << "Could not set popup font to "
209                        << fontname << endl;
210
211         if (bold < 0 && normal < 0) {
212                 lyxerr << "Using 'helvetica' font for menus" << endl;
213                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
214                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
215                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
216                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
217
218                 if (bold < 0 && normal < 0) {
219                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
220                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
221                         normal = bold = 0;
222                 }
223         }
224         if (bold < 0)
225                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
226         else if (normal < 0)
227                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
228
229         fl_setpup_fontstyle(FL_NORMAL_STYLE);
230         fl_setpup_fontsize(FL_NORMAL_SIZE);
231         fl_setpup_color(FL_MCOL, FL_BLACK);
232         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
233 #if FL_REVISION < 89
234         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
235 #else
236         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
237 #endif
238 }
239
240
241 void lyx_gui::start(string const & batch, vector<string> files)
242 {
243         // initial geometry
244         int xpos = -1;
245         int ypos = -1;
246         unsigned int width = 690;
247         unsigned int height = 510;
248
249         static const int geometryBitmask =
250                 XParseGeometry(geometry,
251                                 &xpos, &ypos, &width, &height);
252
253         // if width is not set by geometry, check it against monitor width
254         if (!(geometryBitmask & 4)) {
255                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
256                 if (WidthOfScreen(scr) - 8 < int(width))
257                         width = WidthOfScreen(scr) - 8;
258         }
259
260         // if height is not set by geometry, check it against monitor height
261         if (!(geometryBitmask & 8)) {
262                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
263                 if (HeightOfScreen(scr) - 24 < int(height))
264                         height = HeightOfScreen(scr) - 24;
265         }
266
267         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
268
269         // recalculate xpos if it's not set
270         if (xpos == -1)
271                 xpos = (WidthOfScreen(s) - width) / 2;
272
273         // recalculate ypos if it's not set
274         if (ypos == -1)
275                 ypos = (HeightOfScreen(s) - height) / 2;
276
277         lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
278                 << "+" << xpos << "+" << ypos << endl;
279
280         XFormsView view(width, height);
281         view.show(xpos, ypos, "LyX");
282         view.init();
283
284         Buffer * last = 0;
285
286         // FIXME: some code below needs moving
287
288         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
289
290         vector<string>::const_iterator cit = files.begin();
291         vector<string>::const_iterator end = files.end();
292         for (; cit != end; ++cit) {
293                 Buffer * b = bufferlist.loadLyXFile(*cit);
294                 if (b) {
295                         last = b;
296                 }
297         }
298
299         // switch to the last buffer successfully loaded
300         if (last) {
301                 view.view()->buffer(last);
302         }
303
304         // handle the batch commands the user asked for
305         if (!batch.empty()) {
306                 view.getLyXFunc().dispatch(batch);
307         }
308
309         // enter the event loop
310         while (!finished) {
311                 if (fl_check_forms() == FL_EVENT) {
312                         XEvent ev;
313                         fl_XNextEvent(&ev);
314                         lyxerr << "Received unhandled X11 event" << endl;
315                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
316                                 " Target: 0x" << hex << ev.xany.window << dec << endl;
317                 }
318         }
319
320         // FIXME: breaks emergencyCleanup
321         delete lyxserver;
322 }
323
324
325 void lyx_gui::exit()
326 {
327         finished = true;
328 }
329
330
331 string const lyx_gui::hexname(LColor::color col)
332 {
333         string const name = lcolor.getX11Name(col);
334         Display * const display = fl_get_display();
335         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
336         XColor xcol, ccol;
337
338         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
339                         lyxerr << "X can't find color \""
340                                << lcolor.getLyXName(col)
341                                << "\"" << endl;
342                         return string();
343         }
344
345         ostringstream os;
346
347         // Note that X stores the RGB values in the range 0 - 65535
348         // whilst we require them in the range 0 - 255.
349         os << setbase(16) << setfill('0')
350            << setw(2) << (xcol.red   / 256)
351            << setw(2) << (xcol.green / 256)
352            << setw(2) << (xcol.blue  / 256);
353
354         return os.str().c_str();
355 }
356
357
358 void lyx_gui::update_color(LColor::color col)
359 {
360         lyxColorHandler->updateColor(col);
361 }
362
363
364 void lyx_gui::update_fonts()
365 {
366         fontloader.update();
367 }
368
369
370 bool lyx_gui::font_available(LyXFont const & font)
371 {
372         return fontloader.available(font);
373 }
374
375 namespace {
376
377 extern "C"
378 void C_read_callback(int, void * data)
379 {
380         LyXComm * comm = static_cast<LyXComm *>(data);
381         comm->read_ready();
382 }
383
384 }
385
386 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
387 {
388         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
389 }
390
391
392 void lyx_gui::remove_read_callback(int fd)
393 {
394         fl_remove_io_callback(fd, FL_READ, C_read_callback);
395 }