]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
add 'dec' to switch back to decimal output
[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::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 /// set default GUI configuration
69 void setDefaults()
70 {
71         FL_IOPT cntl;
72         cntl.buttonFontSize = FL_NORMAL_SIZE;
73         cntl.browserFontSize = FL_NORMAL_SIZE;
74         cntl.labelFontSize = FL_NORMAL_SIZE;
75         cntl.choiceFontSize = FL_NORMAL_SIZE;
76         cntl.inputFontSize = FL_NORMAL_SIZE;
77         cntl.menuFontSize  = FL_NORMAL_SIZE;
78         cntl.borderWidth = -1;
79         cntl.vclass = FL_DefaultVisual;
80         fl_set_defaults(FL_PDVisual
81                         | FL_PDButtonFontSize
82                         | FL_PDBrowserFontSize
83                         | FL_PDLabelFontSize
84                         | FL_PDChoiceFontSize
85                         | FL_PDInputFontSize
86                         | FL_PDMenuFontSize
87                         | FL_PDBorderWidth, &cntl);
88 }
89
90
91 extern "C" {
92
93 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
94         // We don't abort on BadWindow
95         if (xeev->error_code == BadWindow) {
96                 lyxerr << "BadWindow received !" << endl;
97                 lyxerr << "If you're using xforms 1.0 or greater, "
98                         << " please report this to lyx-devel@lists.lyx.org" << endl;
99                 return 0;
100         }
101
102         // emergency cleanup
103         LyX::emergencyCleanup();
104
105         // Get the reason for the crash.
106         char etxt[513];
107         XGetErrorText(display, xeev->error_code, etxt, 512);
108         lyxerr << etxt << " id: " << xeev->resourceid << endl;
109         // By doing an abort we get a nice backtrace. (hopefully)
110         lyx::abort();
111         return 0;
112 }
113
114 }
115
116 /// read in geometry specification
117 char geometry[40];
118
119 } // namespace anon
120
121
122 void lyx_gui::parse_init(int & argc, char * argv[])
123 {
124         setDefaults();
125
126         FL_CMD_OPT cmdopt[] = {
127                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
128         };
129
130         FL_resource res[] = {
131                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
132         };
133
134         const int num_res = sizeof(res)/sizeof(FL_resource);
135
136         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
137
138         // It appears that, in xforms >=0.89.5, fl_initialize()
139         // calls setlocale() and ruins our LC_NUMERIC setting.
140         locale_init();
141
142         fl_get_app_resources(res, num_res);
143
144         Display * display = fl_get_display();
145
146         if (!display) {
147                 lyxerr << "LyX: unable to access X display, exiting" << endl;
148                 os::warn("Unable to access X display, exiting");
149                 ::exit(1);
150         }
151
152         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
153
154         XSetErrorHandler(LyX_XErrHandler);
155
156         lyxColorHandler.reset(new LyXColorHandler());
157  
158         using namespace grfx;
159
160 #ifdef USE_XFORMS_IMAGE_LOADER
161         // connect the image loader based on the xforms library
162         Image::newImage = boost::bind(&xformsImage::newImage);
163         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
164 #else
165         // connect the image loader based on the XPM library
166         Image::newImage = boost::bind(&ImageXPM::newImage);
167         Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
168 #endif
169 }
170
171
172 void lyx_gui::parse_lyxrc()
173 {
174         // FIXME !!!!
175         lyxrc.dpi = 95;
176
177         XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
178
179         if (lyxrc.popup_font_encoding.empty())
180                 lyxrc.popup_font_encoding = lyxrc.font_norm;
181         // Set the font name for popups and menus
182         string boldfontname = lyxrc.popup_bold_font
183                                + "-*-*-*-?-*-*-*-*-"
184                                + lyxrc.popup_font_encoding;
185                 // "?" means "scale that font"
186         string fontname = lyxrc.popup_normal_font
187                                + "-*-*-*-?-*-*-*-*-"
188                                + lyxrc.popup_font_encoding;
189
190         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
191         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
192         if (bold < 0)
193                 lyxerr << "Could not set menu font to "
194                        << boldfontname << endl;
195
196         if (normal < 0)
197                 lyxerr << "Could not set popup font to "
198                        << fontname << endl;
199
200         if (bold < 0 && normal < 0) {
201                 lyxerr << "Using 'helvetica' font for menus" << endl;
202                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
203                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
204                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
205                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
206
207                 if (bold < 0 && normal < 0) {
208                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
209                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
210                         normal = bold = 0;
211                 }
212         }
213         if (bold < 0)
214                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
215         else if (normal < 0)
216                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
217
218         fl_setpup_fontstyle(FL_NORMAL_STYLE);
219         fl_setpup_fontsize(FL_NORMAL_SIZE);
220         fl_setpup_color(FL_MCOL, FL_BLACK);
221         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
222 #if FL_REVISION < 89
223         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
224 #else
225         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
226 #endif
227 }
228
229
230 void lyx_gui::start(string const & batch, vector<string> files)
231 {
232         // initial geometry
233         int xpos = -1;
234         int ypos = -1;
235         unsigned int width = 690;
236         unsigned int height = 510;
237
238         static const int geometryBitmask =
239                 XParseGeometry(geometry,
240                                 &xpos, &ypos, &width, &height);
241
242         // if width is not set by geometry, check it against monitor width
243         if (!(geometryBitmask & 4)) {
244                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
245                 if (WidthOfScreen(scr) - 8 < int(width))
246                         width = WidthOfScreen(scr) - 8;
247         }
248
249         // if height is not set by geometry, check it against monitor height
250         if (!(geometryBitmask & 8)) {
251                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
252                 if (HeightOfScreen(scr) - 24 < int(height))
253                         height = HeightOfScreen(scr) - 24;
254         }
255
256         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
257
258         // recalculate xpos if it's not set
259         if (xpos == -1)
260                 xpos = (WidthOfScreen(s) - width) / 2;
261
262         // recalculate ypos if it's not set
263         if (ypos == -1)
264                 ypos = (HeightOfScreen(s) - height) / 2;
265
266         lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
267                 << "+" << xpos << "+" << ypos << endl;
268
269         XFormsView view(width, height);
270         view.show(xpos, ypos, "LyX");
271         view.init();
272
273         Buffer * last = 0;
274
275         // FIXME: some code below needs moving
276
277         lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
278
279         vector<string>::const_iterator cit = files.begin();
280         vector<string>::const_iterator end = files.end();
281         for (; cit != end; ++cit) {
282                 Buffer * b = bufferlist.loadLyXFile(*cit);
283                 if (b) {
284                         last = b;
285                 }
286         }
287
288         // switch to the last buffer successfully loaded
289         if (last) {
290                 view.view()->buffer(last);
291         }
292
293         // handle the batch commands the user asked for
294         if (!batch.empty()) {
295                 view.getLyXFunc()->dispatch(batch);
296         }
297
298         // enter the event loop
299         while (!finished) {
300                 if (fl_check_forms() == FL_EVENT) {
301                         XEvent ev;
302                         fl_XNextEvent(&ev);
303                         lyxerr << "Received unhandled X11 event" << endl;
304                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
305                                 " Target: 0x" << hex << ev.xany.window << 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 string const lyx_gui::hexname(LColor::color col)
321 {
322         string const name = lcolor.getX11Name(col);
323         Display * const display = fl_get_display();
324         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
325         XColor xcol, ccol;
326
327         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
328                         lyxerr << "X can't find color \""
329                                << lcolor.getLyXName(col)
330                                << "\"" << endl;
331                         return string();
332         }
333
334         ostringstream os;
335
336         // Note that X stores the RGB values in the range 0 - 65535
337         // whilst we require them in the range 0 - 255.
338         os << setbase(16) << setfill('0')
339            << setw(2) << (xcol.red   / 256)
340            << setw(2) << (xcol.green / 256)
341            << setw(2) << (xcol.blue  / 256);
342
343         return os.str().c_str();
344 }
345
346
347 void lyx_gui::update_color(LColor::color col)
348 {
349         lyxColorHandler->updateColor(col);
350 }
351
352
353 void lyx_gui::update_fonts()
354 {
355         fontloader.update();
356 }
357
358
359 bool lyx_gui::font_available(LyXFont const & font)
360 {
361         return fontloader.available(font);
362 }
363
364 namespace {
365
366 extern "C"
367 void C_read_callback(int, void * data)
368 {
369         LyXComm * comm = static_cast<LyXComm *>(data); 
370         comm->read_ready();
371 }
372
373 }
374
375 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
376 {
377         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
378 }