]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
Move fontloader into lyx_gui stuff. Now Qt should compile out of the box
[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
157
158 void lyx_gui::parse_lyxrc()
159 {
160         // FIXME !!!!
161         lyxrc.dpi = 95;
162
163         XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
164
165         if (lyxrc.popup_font_encoding.empty())
166                 lyxrc.popup_font_encoding = lyxrc.font_norm;
167         // Set the font name for popups and menus
168         string boldfontname = lyxrc.popup_bold_font
169                                + "-*-*-*-?-*-*-*-*-"
170                                + lyxrc.popup_font_encoding;
171                 // "?" means "scale that font"
172         string fontname = lyxrc.popup_normal_font
173                                + "-*-*-*-?-*-*-*-*-"
174                                + lyxrc.popup_font_encoding;
175
176         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
177         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
178         if (bold < 0)
179                 lyxerr << "Could not set menu font to "
180                        << boldfontname << endl;
181
182         if (normal < 0)
183                 lyxerr << "Could not set popup font to "
184                        << fontname << endl;
185
186         if (bold < 0 && normal < 0) {
187                 lyxerr << "Using 'helvetica' font for menus" << endl;
188                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
189                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
190                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
191                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
192
193                 if (bold < 0 && normal < 0) {
194                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
195                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
196                         normal = bold = 0;
197                 }
198         }
199         if (bold < 0)
200                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
201         else if (normal < 0)
202                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
203
204         fl_setpup_fontstyle(FL_NORMAL_STYLE);
205         fl_setpup_fontsize(FL_NORMAL_SIZE);
206         fl_setpup_color(FL_MCOL, FL_BLACK);
207         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
208 #if FL_REVISION < 89
209         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
210 #else
211         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
212 #endif
213 }
214
215
216 void lyx_gui::start(string const & batch, vector<string> files)
217 {
218         // initial geometry
219         int xpos = -1;
220         int ypos = -1;
221         unsigned int width = 690;
222         unsigned int height = 510;
223
224         static const int geometryBitmask =
225                 XParseGeometry(geometry,
226                                 &xpos, &ypos, &width, &height);
227
228         // if width is not set by geometry, check it against monitor width
229         if (!(geometryBitmask & 4)) {
230                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
231                 if (WidthOfScreen(scr) - 8 < int(width))
232                         width = WidthOfScreen(scr) - 8;
233         }
234
235         // if height is not set by geometry, check it against monitor height
236         if (!(geometryBitmask & 8)) {
237                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
238                 if (HeightOfScreen(scr) - 24 < int(height))
239                         height = HeightOfScreen(scr) - 24;
240         }
241
242         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
243
244         // recalculate xpos if it's not set
245         if (xpos == -1)
246                 xpos = (WidthOfScreen(s) - width) / 2;
247
248         // recalculate ypos if it's not set
249         if (ypos == -1)
250                 ypos = (HeightOfScreen(s) - height) / 2;
251
252         lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
253                 << "+" << xpos << "+" << ypos << endl;
254
255         XFormsView view(width, height);
256         view.show(xpos, ypos, "LyX");
257         view.init();
258
259         Buffer * last = 0;
260
261         // FIXME: some code below needs moving
262
263         lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
264
265         vector<string>::const_iterator cit = files.begin();
266         vector<string>::const_iterator end = files.end();
267         for (; cit != end; ++cit) {
268                 Buffer * b = bufferlist.loadLyXFile(*cit);
269                 if (b) {
270                         last = b;
271                 }
272         }
273
274         // switch to the last buffer successfully loaded
275         if (last) {
276                 view.view()->buffer(last);
277         }
278
279         // handle the batch commands the user asked for
280         if (!batch.empty()) {
281                 view.getLyXFunc()->verboseDispatch(batch, false);
282         }
283
284         // enter the event loop
285         while (!finished) {
286                 if (fl_check_forms() == FL_EVENT) {
287                         XEvent ev;
288                         fl_XNextEvent(&ev);
289                         lyxerr << "Received unhandled X11 event" << endl;
290                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
291                                 " Target: 0x" << hex << ev.xany.window << endl;
292                 }
293         }
294
295         // FIXME
296         delete lyxserver;
297 }
298
299
300 // Called by the graphics cache to connect the appropriate frontend
301 // image loading routines to the LyX kernel.
302 void lyx_gui::init_graphics()
303 {
304         using namespace grfx;
305
306 #ifdef USE_XFORMS_IMAGE_LOADER
307         // connect the image loader based on the xforms library
308         Image::newImage = boost::bind(&xformsImage::newImage);
309         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
310 #else
311         // connect the image loader based on the XPM library
312         Image::newImage = boost::bind(&ImageXPM::newImage);
313         Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
314 #endif
315 }
316
317
318 string const lyx_gui::hexname(LColor::color col)
319 {
320         string const name = lcolor.getX11Name(col);
321         Display * const display = fl_get_display();
322         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
323         XColor xcol, ccol;
324
325         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
326                         lyxerr << "X can't find color \""
327                                << lcolor.getLyXName(col)
328                                << "\"" << endl;
329                         return string();
330         }
331
332         ostringstream os;
333
334         // Note that X stores the RGB values in the range 0 - 65535
335         // whilst we require them in the range 0 - 255.
336         os << setbase(16) << setfill('0')
337            << setw(2) << (xcol.red   / 256)
338            << setw(2) << (xcol.green / 256)
339            << setw(2) << (xcol.blue  / 256);
340
341         return os.str().c_str();
342 }
343
344
345 void lyx_gui::update_color(LColor::color col)
346 {
347         lyxColorHandler->updateColor(col);
348 }
349
350
351 void lyx_gui::update_fonts()
352 {
353         fontloader.update();
354 }
355
356
357 bool lyx_gui::font_available(LyXFont const & font)
358 {
359         return fontloader.available(font);
360 }