]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
Get rid of lyxstring, remove usage of STRCONV.
[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 #include "ColorHandler.h"
16 #include "xfont_loader.h"
17 #include "xforms_helpers.h"
18 #include "xformsImage.h"
19 #include "XFormsView.h"
20
21 #include "bufferlist.h"
22 #include "BufferView.h"
23 #include "debug.h"
24 #include "gettext.h"
25 #include "lyx_main.h"
26 #include "lyxfunc.h"
27 #include "lyxrc.h"
28 #include "lyxserver.h"
29
30 #include "graphics/LoaderQueue.h"
31
32 #include "support/lyxlib.h"
33 #include "support/os.h"
34 #include "support/filetools.h"
35 #include "support/path_defines.h"
36
37 #include "lyx_forms.h"
38
39 #include <boost/bind.hpp>
40
41 #include "support/std_sstream.h"
42 #include <iomanip>
43 #include <fcntl.h>
44
45 using lyx::support::AddName;
46 using lyx::support::os;
47 using lyx::support::user_lyxdir;
48
49 #ifndef CXX_GLOBAL_CSTD
50 using std::exit;
51 #endif
52
53 using std::dec;
54 using std::endl;
55 using std::hex;
56 using std::setbase;
57 using std::setfill;
58 using std::setw;
59 using std::ostringstream;
60 using std::vector;
61
62
63 extern BufferList bufferlist;
64
65 // FIXME: wrong place !
66 LyXServer * lyxserver;
67
68 namespace {
69
70 /// quit lyx
71 bool finished = false;
72
73 /// estimate DPI from X server
74 float getDPI()
75 {
76         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
77         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
78                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
79 }
80
81
82 /// set default GUI configuration
83 void setDefaults()
84 {
85         FL_IOPT cntl;
86         cntl.buttonFontSize = FL_NORMAL_SIZE;
87         cntl.browserFontSize = FL_NORMAL_SIZE;
88         cntl.labelFontSize = FL_NORMAL_SIZE;
89         cntl.choiceFontSize = FL_NORMAL_SIZE;
90         cntl.inputFontSize = FL_NORMAL_SIZE;
91         cntl.menuFontSize  = FL_NORMAL_SIZE;
92         cntl.borderWidth = -1;
93         cntl.vclass = FL_DefaultVisual;
94         fl_set_defaults(FL_PDVisual
95                         | FL_PDButtonFontSize
96                         | FL_PDBrowserFontSize
97                         | FL_PDLabelFontSize
98                         | FL_PDChoiceFontSize
99                         | FL_PDInputFontSize
100                         | FL_PDMenuFontSize
101                         | FL_PDBorderWidth, &cntl);
102 }
103
104
105 extern "C" {
106
107 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
108         // We don't abort on BadWindow
109         if (xeev->error_code == BadWindow) {
110                 lyxerr << "BadWindow received !" << endl;
111                 lyxerr << "If you're using xforms 1.0 or greater, "
112                         << " please report this to lyx-devel@lists.lyx.org" << endl;
113                 return 0;
114         }
115
116         // emergency cleanup
117         LyX::emergencyCleanup();
118
119         // Get the reason for the crash.
120         char etxt[513];
121         XGetErrorText(display, xeev->error_code, etxt, 512);
122         lyxerr << etxt << " id: " << xeev->resourceid << endl;
123         // By doing an abort we get a nice backtrace. (hopefully)
124         lyx::support::abort();
125         return 0;
126 }
127
128 }
129
130 /// read in geometry specification
131 char geometry[40];
132
133 } // namespace anon
134
135
136 namespace lyx_gui {
137
138 bool use_gui = true;
139
140
141 void parse_init(int & argc, char * argv[])
142 {
143         setDefaults();
144
145         FL_CMD_OPT cmdopt[] = {
146                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
147         };
148
149         FL_resource res[] = {
150                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
151         };
152
153         const int num_res = sizeof(res)/sizeof(FL_resource);
154
155         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
156
157         // It appears that, in xforms >=0.89.5, fl_initialize()
158         // calls setlocale() and ruins our LC_NUMERIC setting.
159         locale_init();
160
161         fl_get_app_resources(res, num_res);
162
163         Display * display = fl_get_display();
164
165         if (!display) {
166                 lyxerr << "LyX: unable to access X display, exiting" << endl;
167                 os::warn("Unable to access X display, exiting");
168                 ::exit(1);
169         }
170
171         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
172
173         XSetErrorHandler(LyX_XErrHandler);
174
175         lyxColorHandler.reset(new LyXColorHandler());
176
177         using namespace lyx::graphics;
178
179         // connect the image loader based on the xforms library
180         Image::newImage = boost::bind(&xformsImage::newImage);
181         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
182
183         // must do this /before/ lyxrc gets read
184         lyxrc.dpi = getDPI();
185
186         LoaderQueue::setPriority(10,100);
187 }
188
189
190 void parse_lyxrc()
191 {
192         XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
193
194         if (lyxrc.popup_font_encoding.empty())
195                 lyxrc.popup_font_encoding = lyxrc.font_norm;
196         // Set the font name for popups and menus
197         string boldfontname = lyxrc.popup_bold_font
198                                + "-*-*-*-?-*-*-*-*-"
199                                + lyxrc.popup_font_encoding;
200                 // "?" means "scale that font"
201         string fontname = lyxrc.popup_normal_font
202                                + "-*-*-*-?-*-*-*-*-"
203                                + lyxrc.popup_font_encoding;
204
205         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
206         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
207         if (bold < 0)
208                 lyxerr << "Could not set menu font to "
209                        << boldfontname << endl;
210
211         if (normal < 0)
212                 lyxerr << "Could not set popup font to "
213                        << fontname << endl;
214
215         if (bold < 0 && normal < 0) {
216                 lyxerr << "Using 'helvetica' font for menus" << endl;
217                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
218                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
219                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
220                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
221
222                 if (bold < 0 && normal < 0) {
223                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
224                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
225                         normal = bold = 0;
226                 }
227         }
228         if (bold < 0)
229                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
230         else if (normal < 0)
231                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
232
233         fl_setpup_fontstyle(FL_NORMAL_STYLE);
234         fl_setpup_fontsize(FL_NORMAL_SIZE);
235         fl_setpup_color(FL_MCOL, FL_BLACK);
236         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
237         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
238 }
239
240
241 void start(string const & batch, vector<string> const & 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         int const 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 & WidthValue)) {
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 & HeightValue)) {
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         // FIXME: some code below needs moving
285
286         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
287
288         vector<string>::const_iterator cit = files.begin();
289         vector<string>::const_iterator end = files.end();
290         for (; cit != end; ++cit)
291                 view.view()->loadLyXFile(*cit, true);
292
293         // handle the batch commands the user asked for
294         if (!batch.empty())
295                 view.getLyXFunc().dispatch(batch);
296
297         // enter the event loop
298         while (!finished) {
299                 if (fl_check_forms() == FL_EVENT) {
300                         XEvent ev;
301                         fl_XNextEvent(&ev);
302                         lyxerr[Debug::GUI]
303                                 << "Received unhandled X11 event" << endl
304                                 << "Type: " << ev.xany.type
305                                 << " Target: 0x" << hex << ev.xany.window
306                                 << dec << endl;
307                 }
308         }
309
310         // FIXME: breaks emergencyCleanup
311         delete lyxserver;
312 }
313
314
315 void exit()
316 {
317         finished = true;
318 }
319
320
321 void sync_events()
322 {
323         // FIXME
324 }
325
326
327 FuncStatus getStatus(FuncRequest const & /*ev*/)
328 {
329         // Nothing interesting to do here
330         return FuncStatus();
331 }
332
333 string const hexname(LColor::color col)
334 {
335         unsigned int r, g, b;
336         bool const success = getRGBColor(col, r, g, b);
337         if (!success) {
338                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
339                        << '"' << endl;
340                 return string();
341         }
342
343         ostringstream os;
344
345         os << setbase(16) << setfill('0')
346            << setw(2) << r
347            << setw(2) << g
348            << setw(2) << b;
349
350         return os.str();
351 }
352
353
354 void update_color(LColor::color col)
355 {
356         lyxColorHandler->updateColor(col);
357 }
358
359
360 void update_fonts()
361 {
362         fontloader.update();
363 }
364
365
366 bool font_available(LyXFont const & font)
367 {
368         return fontloader.available(font);
369 }
370
371 namespace {
372
373 extern "C"
374 void C_read_callback(int, void * data)
375 {
376         LyXComm * comm = static_cast<LyXComm *>(data);
377         comm->read_ready();
378 }
379
380 }
381
382 void set_read_callback(int fd, LyXComm * comm)
383 {
384         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
385 }
386
387
388 void remove_read_callback(int fd)
389 {
390         fl_remove_io_callback(fd, FL_READ, C_read_callback);
391 }
392
393
394 string const roman_font_name()
395 {
396         return "times";
397 }
398
399
400 string const sans_font_name()
401 {
402         return "helvetica";
403 }
404
405
406 string const typewriter_font_name()
407 {
408         return "courier";
409 }
410
411 }; // namespace lyx_gui