]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
take several functions out of BufferList, and split functionality
[lyx.git] / src / frontends / xforms / lyx_gui.C
1 /**
2  * \file xforms/lyx_gui.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #include "lyx_gui.h"
15
16 #include "support/lyxlib.h"
17 #include "support/os.h"
18 #include "support/filetools.h"
19
20 #include "debug.h"
21 #include "gettext.h"
22
23 #include "lyx_main.h"
24 #include "lyxrc.h"
25 #include "lyxfont.h"
26 #include "graphics/LoaderQueue.h"
27
28 // FIXME: move this stuff out again
29 #include "bufferlist.h"
30 #include "buffer_funcs.h"
31 #include "lyxfunc.h"
32 #include "lyxserver.h"
33 #include "BufferView.h"
34 #include "XFormsView.h"
35
36 #include "lyx_forms.h"
37 #include "ColorHandler.h"
38 #include "xforms_helpers.h"
39 #include "xfont_loader.h"
40 #include "xformsImage.h"
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 namespace lyx_gui {
133
134 bool use_gui = true;
135
136
137 void parse_init(int & argc, char * argv[])
138 {
139         setDefaults();
140
141         FL_CMD_OPT cmdopt[] = {
142                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
143         };
144
145         FL_resource res[] = {
146                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
147         };
148
149         const int num_res = sizeof(res)/sizeof(FL_resource);
150
151         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
152
153         // It appears that, in xforms >=0.89.5, fl_initialize()
154         // calls setlocale() and ruins our LC_NUMERIC setting.
155         locale_init();
156
157         fl_get_app_resources(res, num_res);
158
159         Display * display = fl_get_display();
160
161         if (!display) {
162                 lyxerr << "LyX: unable to access X display, exiting" << endl;
163                 os::warn("Unable to access X display, exiting");
164                 ::exit(1);
165         }
166
167         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
168
169         XSetErrorHandler(LyX_XErrHandler);
170
171         lyxColorHandler.reset(new LyXColorHandler());
172
173         using namespace grfx;
174
175         // connect the image loader based on the xforms library
176         Image::newImage = boost::bind(&xformsImage::newImage);
177         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
178
179         // must do this /before/ lyxrc gets read
180         lyxrc.dpi = getDPI();
181
182         LoaderQueue::setPriority(10,100);
183 }
184
185
186 void 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         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
234 }
235
236
237 void start(string const & batch, vector<string> const & files)
238 {
239         // initial geometry
240         int xpos = -1;
241         int ypos = -1;
242         unsigned int width = 690;
243         unsigned int height = 510;
244
245         int const geometryBitmask =
246                 XParseGeometry(geometry,
247                                &xpos, &ypos, &width, &height);
248
249         // if width is not set by geometry, check it against monitor width
250         if (!(geometryBitmask & WidthValue)) {
251                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
252                 if (WidthOfScreen(scr) - 8 < int(width))
253                         width = WidthOfScreen(scr) - 8;
254         }
255
256         // if height is not set by geometry, check it against monitor height
257         if (!(geometryBitmask & HeightValue)) {
258                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
259                 if (HeightOfScreen(scr) - 24 < int(height))
260                         height = HeightOfScreen(scr) - 24;
261         }
262
263         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
264
265         // recalculate xpos if it's not set
266         if (xpos == -1)
267                 xpos = (WidthOfScreen(s) - width) / 2;
268
269         // recalculate ypos if it's not set
270         if (ypos == -1)
271                 ypos = (HeightOfScreen(s) - height) / 2;
272
273         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
274                            << '+' << xpos << '+' << ypos << endl;
275
276         XFormsView view(width, height);
277         view.show(xpos, ypos, "LyX");
278         view.init();
279
280         Buffer * last = 0;
281
282         // FIXME: some code below needs moving
283
284         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
285
286         vector<string>::const_iterator cit = files.begin();
287         vector<string>::const_iterator end = files.end();
288         for (; cit != end; ++cit) {
289                 Buffer * b = bufferlist.newBuffer(*cit);
290                 if (loadLyXFile(b, *cit))
291                         last = b;
292         }
293
294         // switch to the last buffer successfully loaded
295         if (last) {
296                 view.view()->buffer(last);
297         }
298
299         // handle the batch commands the user asked for
300         if (!batch.empty()) {
301                 view.getLyXFunc().dispatch(batch);
302         }
303
304         // enter the event loop
305         while (!finished) {
306                 if (fl_check_forms() == FL_EVENT) {
307                         XEvent ev;
308                         fl_XNextEvent(&ev);
309                         lyxerr[Debug::GUI]
310                                 << "Received unhandled X11 event" << endl
311                                 << "Type: " << ev.xany.type
312                                 << " Target: 0x" << hex << ev.xany.window
313                                 << dec << endl;
314                 }
315         }
316
317         // FIXME: breaks emergencyCleanup
318         delete lyxserver;
319 }
320
321
322 void exit()
323 {
324         finished = true;
325 }
326
327
328 void sync_events()
329 {
330         // FIXME
331 }
332
333
334 FuncStatus getStatus(FuncRequest const & /*ev*/)
335 {
336         // Nothing interesting to do here
337         return FuncStatus();
338 }
339
340 string const hexname(LColor::color col)
341 {
342         unsigned int r, g, b;
343         bool const success = getRGBColor(col, r, g, b);
344         if (!success) {
345                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
346                        << '"' << endl;
347                 return string();
348         }
349
350         ostringstream os;
351
352         os << setbase(16) << setfill('0')
353            << setw(2) << r
354            << setw(2) << g
355            << setw(2) << b;
356
357         return STRCONV(os.str());
358 }
359
360
361 void update_color(LColor::color col)
362 {
363         lyxColorHandler->updateColor(col);
364 }
365
366
367 void update_fonts()
368 {
369         fontloader.update();
370 }
371
372
373 bool font_available(LyXFont const & font)
374 {
375         return fontloader.available(font);
376 }
377
378 namespace {
379
380 extern "C"
381 void C_read_callback(int, void * data)
382 {
383         LyXComm * comm = static_cast<LyXComm *>(data);
384         comm->read_ready();
385 }
386
387 }
388
389 void set_read_callback(int fd, LyXComm * comm)
390 {
391         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
392 }
393
394
395 void remove_read_callback(int fd)
396 {
397         fl_remove_io_callback(fd, FL_READ, C_read_callback);
398 }
399
400
401 string const roman_font_name()
402 {
403         return "times";
404 }
405
406
407 string const sans_font_name()
408 {
409         return "helvetica";
410 }
411
412
413 string const typewriter_font_name()
414 {
415         return "courier";
416 }
417
418 }; // namespace lyx_gui