]> git.lyx.org Git - features.git/blob - src/frontends/gnome/lyx_gui.C
Replace LString.h with support/std_string.h,
[features.git] / src / frontends / gnome / lyx_gui.C
1 /**
2  * \file gnome/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 unknown
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 #include "support/path_defines.h"
20
21 #include "debug.h"
22 #include "gettext.h"
23
24 #include "lyx_main.h"
25 #include "lyxrc.h"
26 #include "lyxfont.h"
27
28 // FIXME: move this stuff out again
29 #include "bufferlist.h"
30 #include "lyxfunc.h"
31 #include "lyxserver.h"
32 #include "BufferView.h"
33 #include "XFormsView.h"
34
35 #include FORMS_H_LOCATION
36 #include "ColorHandler.h"
37 #include "xforms_helpers.h"
38 #include "xfont_loader.h"
39 #include "xformsImage.h"
40
41 #include "support/std_sstream.h"
42 #include <iomanip>
43 #include <fcntl.h>
44 #include <boost/bind.hpp>
45
46 #include "gnome_helpers.h"
47 #include <gtkmm/main.h>
48 #include <libglademm/xml.h>
49
50 #ifndef CXX_GLOBAL_CSTD
51 using std::exit;
52 #endif
53
54 using std::vector;
55 using std::hex;
56 using std::dec;
57 using std::endl;
58 using std::setbase;
59 using std::setfill;
60 using std::setw;
61
62 extern BufferList bufferlist;
63
64 // FIXME: wrong place !
65 LyXServer * lyxserver;
66
67 namespace {
68
69 /// quit lyx
70 bool finished = false;
71
72 /// estimate DPI from X server
73 float getDPI()
74 {
75         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
76         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
77                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
78 }
79
80
81 /// set default GUI configuration
82 void setDefaults()
83 {
84         FL_IOPT cntl;
85         cntl.buttonFontSize = FL_NORMAL_SIZE;
86         cntl.browserFontSize = FL_NORMAL_SIZE;
87         cntl.labelFontSize = FL_NORMAL_SIZE;
88         cntl.choiceFontSize = FL_NORMAL_SIZE;
89         cntl.inputFontSize = FL_NORMAL_SIZE;
90         cntl.menuFontSize  = FL_NORMAL_SIZE;
91         cntl.borderWidth = -1;
92         cntl.vclass = FL_DefaultVisual;
93         fl_set_defaults(FL_PDVisual
94                         | FL_PDButtonFontSize
95                         | FL_PDBrowserFontSize
96                         | FL_PDLabelFontSize
97                         | FL_PDChoiceFontSize
98                         | FL_PDInputFontSize
99                         | FL_PDMenuFontSize
100                         | FL_PDBorderWidth, &cntl);
101 }
102
103
104 extern "C" {
105
106 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
107         // We don't abort on BadWindow
108         if (xeev->error_code == BadWindow) {
109                 lyxerr << "BadWindow received !" << endl;
110                 lyxerr << "If you're using xforms 1.0 or greater, "
111                         << " please report this to lyx-devel@lists.lyx.org" << endl;
112                 return 0;
113         }
114
115         // emergency cleanup
116         LyX::emergencyCleanup();
117
118         // Get the reason for the crash.
119         char etxt[513];
120         XGetErrorText(display, xeev->error_code, etxt, 512);
121         lyxerr << etxt << " id: " << xeev->resourceid << endl;
122         // By doing an abort we get a nice backtrace. (hopefully)
123         lyx::abort();
124         return 0;
125 }
126
127 }
128
129 /// read in geometry specification
130 char geometry[40];
131
132 } // namespace anon
133
134
135 void lyx_gui::parse_init(int & argc, char * argv[])
136 {
137         setDefaults();
138
139         FL_CMD_OPT cmdopt[] = {
140                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
141         };
142
143         FL_resource res[] = {
144                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
145         };
146
147         const int num_res = sizeof(res)/sizeof(FL_resource);
148
149         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
150
151         // It appears that, in xforms >=0.89.5, fl_initialize()
152         // calls setlocale() and ruins our LC_NUMERIC setting.
153         locale_init();
154
155         fl_get_app_resources(res, num_res);
156
157         Display * display = fl_get_display();
158
159         if (!display) {
160                 lyxerr << "LyX: unable to access X display, exiting" << endl;
161                 os::warn("Unable to access X display, exiting");
162                 ::exit(1);
163         }
164
165         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
166
167         XSetErrorHandler(LyX_XErrHandler);
168
169         lyxColorHandler.reset(new LyXColorHandler());
170
171         using namespace lyx::graphics;
172
173         // connect the image loader based on the xforms library
174         Image::newImage = boost::bind(&xformsImage::newImage);
175         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
176
177         // must do this /before/ lyxrc gets read
178         lyxrc.dpi = getDPI();
179
180         new Gtk::Main (argc, argv);
181 }
182
183
184 void lyx_gui::parse_lyxrc()
185 {
186         XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
187
188         if (lyxrc.popup_font_encoding.empty())
189                 lyxrc.popup_font_encoding = lyxrc.font_norm;
190         // Set the font name for popups and menus
191         string boldfontname = lyxrc.popup_bold_font
192                                + "-*-*-*-?-*-*-*-*-"
193                                + lyxrc.popup_font_encoding;
194                 // "?" means "scale that font"
195         string fontname = lyxrc.popup_normal_font
196                                + "-*-*-*-?-*-*-*-*-"
197                                + lyxrc.popup_font_encoding;
198
199         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
200         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
201         if (bold < 0)
202                 lyxerr << "Could not set menu font to "
203                        << boldfontname << endl;
204
205         if (normal < 0)
206                 lyxerr << "Could not set popup font to "
207                        << fontname << endl;
208
209         if (bold < 0 && normal < 0) {
210                 lyxerr << "Using 'helvetica' font for menus" << endl;
211                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
212                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
213                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
214                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
215
216                 if (bold < 0 && normal < 0) {
217                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
218                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
219                         normal = bold = 0;
220                 }
221         }
222         if (bold < 0)
223                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
224         else if (normal < 0)
225                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
226
227         fl_setpup_fontstyle(FL_NORMAL_STYLE);
228         fl_setpup_fontsize(FL_NORMAL_SIZE);
229         fl_setpup_color(FL_MCOL, FL_BLACK);
230         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
231         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
232 }
233
234 void lyx_gui::start(string const & batch, vector<string> const & files)
235 {
236         // initial geometry
237         int xpos = -1;
238         int ypos = -1;
239         unsigned int width = 690;
240         unsigned int height = 510;
241
242         int const geometryBitmask =
243                 XParseGeometry(geometry,
244                                &xpos, &ypos, &width, &height);
245
246         // if width is not set by geometry, check it against monitor width
247         if (!(geometryBitmask & WidthValue)) {
248                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
249                 if (WidthOfScreen(scr) - 8 < int(width))
250                         width = WidthOfScreen(scr) - 8;
251         }
252
253         // if height is not set by geometry, check it against monitor height
254         if (!(geometryBitmask & HeightValue)) {
255                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
256                 if (HeightOfScreen(scr) - 24 < int(height))
257                         height = HeightOfScreen(scr) - 24;
258         }
259
260         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
261
262         // recalculate xpos if it's not set
263         if (xpos == -1)
264                 xpos = (WidthOfScreen(s) - width) / 2;
265
266         // recalculate ypos if it's not set
267         if (ypos == -1)
268                 ypos = (HeightOfScreen(s) - height) / 2;
269
270         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
271                            << '+' << xpos << '+' << ypos << endl;
272
273         XFormsView view(width, height);
274         view.show(xpos, ypos, "LyX");
275         view.init();
276
277         Buffer * last = 0;
278
279         // FIXME: some code below needs moving
280
281         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
282
283         vector<string>::const_iterator cit = files.begin();
284         vector<string>::const_iterator end = files.end();
285         for (; cit != end; ++cit) {
286                 Buffer * b = bufferlist.loadLyXFile(*cit);
287                 if (b) {
288                         last = b;
289                 }
290         }
291
292         // switch to the last buffer successfully loaded
293         if (last) {
294                 view.view()->buffer(last);
295         }
296
297         // handle the batch commands the user asked for
298         if (!batch.empty()) {
299                 view.getLyXFunc().dispatch(batch);
300         }
301
302         // enter the event loop
303         while (!finished) {
304                 while (Gtk::Main::events_pending())
305                         Gtk::Main::iteration(false);
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
318         // FIXME: breaks emergencyCleanup
319         delete lyxserver;
320 }
321
322 void lyx_gui::exit()
323 {
324         finished = true;
325 }
326
327
328 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
329 {
330         // Nothing interesting to do here
331         return FuncStatus();
332 }
333
334 string const lyx_gui::hexname(LColor::color col)
335 {
336         string const name = lcolor.getX11Name(col);
337         Display * const display = fl_get_display();
338         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
339         XColor xcol, ccol;
340
341         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
342                         lyxerr << "X can't find color \""
343                                << lcolor.getLyXName(col)
344                                << '"' << endl;
345                         return string();
346         }
347
348         ostringstream os;
349
350         // Note that X stores the RGB values in the range 0 - 65535
351         // whilst we require them in the range 0 - 255.
352         os << setbase(16) << setfill('0')
353            << setw(2) << (xcol.red   / 256)
354            << setw(2) << (xcol.green / 256)
355            << setw(2) << (xcol.blue  / 256);
356
357         return STRCONV(os.str());
358 }
359
360
361 void lyx_gui::update_color(LColor::color col)
362 {
363         lyxColorHandler->updateColor(col);
364 }
365
366
367 void lyx_gui::update_fonts()
368 {
369         fontloader.update();
370 }
371
372
373 bool lyx_gui::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 lyx_gui::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 lyx_gui::remove_read_callback(int fd)
396 {
397         fl_remove_io_callback(fd, FL_READ, C_read_callback);
398 }