]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
Compilation fix: give C_read_callback extern "C" linkage.
[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 bool finished;
59 extern BufferList bufferlist;
60
61 // FIXME: wrong place !
62 LyXServer * lyxserver;
63
64 namespace {
65
66 /// set default GUI configuration
67 void setDefaults()
68 {
69         FL_IOPT cntl;
70         cntl.buttonFontSize = FL_NORMAL_SIZE;
71         cntl.browserFontSize = FL_NORMAL_SIZE;
72         cntl.labelFontSize = FL_NORMAL_SIZE;
73         cntl.choiceFontSize = FL_NORMAL_SIZE;
74         cntl.inputFontSize = FL_NORMAL_SIZE;
75         cntl.menuFontSize  = FL_NORMAL_SIZE;
76         cntl.borderWidth = -1;
77         cntl.vclass = FL_DefaultVisual;
78         fl_set_defaults(FL_PDVisual
79                         | FL_PDButtonFontSize
80                         | FL_PDBrowserFontSize
81                         | FL_PDLabelFontSize
82                         | FL_PDChoiceFontSize
83                         | FL_PDInputFontSize
84                         | FL_PDMenuFontSize
85                         | FL_PDBorderWidth, &cntl);
86 }
87
88
89 extern "C" {
90
91 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
92         // We don't abort on BadWindow
93         if (xeev->error_code == BadWindow) {
94                 lyxerr << "BadWindow received !" << endl;
95                 lyxerr << "If you're using xforms 1.0 or greater, "
96                         << " please report this to lyx-devel@lists.lyx.org" << endl;
97                 return 0;
98         }
99
100         // emergency cleanup
101         LyX::emergencyCleanup();
102
103         // Get the reason for the crash.
104         char etxt[513];
105         XGetErrorText(display, xeev->error_code, etxt, 512);
106         lyxerr << etxt << " id: " << xeev->resourceid << endl;
107         // By doing an abort we get a nice backtrace. (hopefully)
108         lyx::abort();
109         return 0;
110 }
111
112 }
113
114 /// read in geometry specification
115 char geometry[40];
116
117 } // namespace anon
118
119
120 void lyx_gui::parse_init(int & argc, char * argv[])
121 {
122         setDefaults();
123
124         FL_CMD_OPT cmdopt[] = {
125                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
126         };
127
128         FL_resource res[] = {
129                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
130         };
131
132         const int num_res = sizeof(res)/sizeof(FL_resource);
133
134         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
135
136         // It appears that, in xforms >=0.89.5, fl_initialize()
137         // calls setlocale() and ruins our LC_NUMERIC setting.
138         locale_init();
139
140         fl_get_app_resources(res, num_res);
141
142         Display * display = fl_get_display();
143
144         if (!display) {
145                 lyxerr << "LyX: unable to access X display, exiting" << endl;
146                 os::warn("Unable to access X display, exiting");
147                 ::exit(1);
148         }
149
150         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
151
152         XSetErrorHandler(LyX_XErrHandler);
153
154         lyxColorHandler.reset(new LyXColorHandler());
155  
156         using namespace grfx;
157
158 #ifdef USE_XFORMS_IMAGE_LOADER
159         // connect the image loader based on the xforms library
160         Image::newImage = boost::bind(&xformsImage::newImage);
161         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
162 #else
163         // connect the image loader based on the XPM library
164         Image::newImage = boost::bind(&ImageXPM::newImage);
165         Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
166 #endif
167 }
168
169
170 void lyx_gui::parse_lyxrc()
171 {
172         // FIXME !!!!
173         lyxrc.dpi = 95;
174
175         XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
176
177         if (lyxrc.popup_font_encoding.empty())
178                 lyxrc.popup_font_encoding = lyxrc.font_norm;
179         // Set the font name for popups and menus
180         string boldfontname = lyxrc.popup_bold_font
181                                + "-*-*-*-?-*-*-*-*-"
182                                + lyxrc.popup_font_encoding;
183                 // "?" means "scale that font"
184         string fontname = lyxrc.popup_normal_font
185                                + "-*-*-*-?-*-*-*-*-"
186                                + lyxrc.popup_font_encoding;
187
188         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
189         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
190         if (bold < 0)
191                 lyxerr << "Could not set menu font to "
192                        << boldfontname << endl;
193
194         if (normal < 0)
195                 lyxerr << "Could not set popup font to "
196                        << fontname << endl;
197
198         if (bold < 0 && normal < 0) {
199                 lyxerr << "Using 'helvetica' font for menus" << endl;
200                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
201                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
202                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
203                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
204
205                 if (bold < 0 && normal < 0) {
206                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
207                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
208                         normal = bold = 0;
209                 }
210         }
211         if (bold < 0)
212                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
213         else if (normal < 0)
214                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
215
216         fl_setpup_fontstyle(FL_NORMAL_STYLE);
217         fl_setpup_fontsize(FL_NORMAL_SIZE);
218         fl_setpup_color(FL_MCOL, FL_BLACK);
219         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
220 #if FL_REVISION < 89
221         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
222 #else
223         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
224 #endif
225 }
226
227
228 void lyx_gui::start(string const & batch, vector<string> 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         static const int 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 & 4)) {
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 & 8)) {
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()->verboseDispatch(batch, false);
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 << "Received unhandled X11 event" << endl;
302                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
303                                 " Target: 0x" << hex << ev.xany.window << endl;
304                 }
305         }
306
307         // FIXME
308         delete lyxserver;
309 }
310
311
312 string const lyx_gui::hexname(LColor::color col)
313 {
314         string const name = lcolor.getX11Name(col);
315         Display * const display = fl_get_display();
316         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
317         XColor xcol, ccol;
318
319         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
320                         lyxerr << "X can't find color \""
321                                << lcolor.getLyXName(col)
322                                << "\"" << endl;
323                         return string();
324         }
325
326         ostringstream os;
327
328         // Note that X stores the RGB values in the range 0 - 65535
329         // whilst we require them in the range 0 - 255.
330         os << setbase(16) << setfill('0')
331            << setw(2) << (xcol.red   / 256)
332            << setw(2) << (xcol.green / 256)
333            << setw(2) << (xcol.blue  / 256);
334
335         return os.str().c_str();
336 }
337
338
339 void lyx_gui::update_color(LColor::color col)
340 {
341         lyxColorHandler->updateColor(col);
342 }
343
344
345 void lyx_gui::update_fonts()
346 {
347         fontloader.update();
348 }
349
350
351 bool lyx_gui::font_available(LyXFont const & font)
352 {
353         return fontloader.available(font);
354 }
355
356 namespace {
357
358 extern "C"
359 void C_read_callback(int, void * data)
360 {
361         LyXComm * comm = static_cast<LyXComm *>(data); 
362         comm->read_ready();
363 }
364
365 }
366
367 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
368 {
369         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
370 }