]> git.lyx.org Git - features.git/blob - src/frontends/gnome/lyx_gui.C
change "support/std_sstream.h" to <sstream>
[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 <boost/bind.hpp>
42
43 #include <sstream>
44 #include <iomanip>
45 #include <fcntl.h>
46
47 #include "gnome_helpers.h"
48 #include <gtkmm/main.h>
49 #include <libglademm/xml.h>
50
51 #ifndef CXX_GLOBAL_CSTD
52 using std::exit;
53 #endif
54
55 using std::ostringstream;
56 using std::vector;
57 using std::hex;
58 using std::dec;
59 using std::endl;
60 using std::setbase;
61 using std::setfill;
62 using std::setw;
63
64 extern BufferList bufferlist;
65
66 // FIXME: wrong place !
67 LyXServer * lyxserver;
68
69 namespace {
70
71 /// quit lyx
72 bool finished = false;
73
74 /// estimate DPI from X server
75 float getDPI()
76 {
77         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
78         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
79                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
80 }
81
82
83 /// set default GUI configuration
84 void setDefaults()
85 {
86         FL_IOPT cntl;
87         cntl.buttonFontSize = FL_NORMAL_SIZE;
88         cntl.browserFontSize = FL_NORMAL_SIZE;
89         cntl.labelFontSize = FL_NORMAL_SIZE;
90         cntl.choiceFontSize = FL_NORMAL_SIZE;
91         cntl.inputFontSize = FL_NORMAL_SIZE;
92         cntl.menuFontSize  = FL_NORMAL_SIZE;
93         cntl.borderWidth = -1;
94         cntl.vclass = FL_DefaultVisual;
95         fl_set_defaults(FL_PDVisual
96                         | FL_PDButtonFontSize
97                         | FL_PDBrowserFontSize
98                         | FL_PDLabelFontSize
99                         | FL_PDChoiceFontSize
100                         | FL_PDInputFontSize
101                         | FL_PDMenuFontSize
102                         | FL_PDBorderWidth, &cntl);
103 }
104
105
106 extern "C" {
107
108 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
109         // We don't abort on BadWindow
110         if (xeev->error_code == BadWindow) {
111                 lyxerr << "BadWindow received !" << endl;
112                 lyxerr << "If you're using xforms 1.0 or greater, "
113                         << " please report this to lyx-devel@lists.lyx.org" << endl;
114                 return 0;
115         }
116
117         // emergency cleanup
118         LyX::emergencyCleanup();
119
120         // Get the reason for the crash.
121         char etxt[513];
122         XGetErrorText(display, xeev->error_code, etxt, 512);
123         lyxerr << etxt << " id: " << xeev->resourceid << endl;
124         // By doing an abort we get a nice backtrace. (hopefully)
125         lyx::abort();
126         return 0;
127 }
128
129 }
130
131 /// read in geometry specification
132 char geometry[40];
133
134 } // namespace anon
135
136
137 void lyx_gui::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 lyx::graphics;
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         new Gtk::Main (argc, argv);
183 }
184
185
186 void lyx_gui::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 void lyx_gui::start(string const & batch, vector<string> const & files)
237 {
238         // initial geometry
239         int xpos = -1;
240         int ypos = -1;
241         unsigned int width = 690;
242         unsigned int height = 510;
243
244         int const geometryBitmask =
245                 XParseGeometry(geometry,
246                                &xpos, &ypos, &width, &height);
247
248         // if width is not set by geometry, check it against monitor width
249         if (!(geometryBitmask & WidthValue)) {
250                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
251                 if (WidthOfScreen(scr) - 8 < int(width))
252                         width = WidthOfScreen(scr) - 8;
253         }
254
255         // if height is not set by geometry, check it against monitor height
256         if (!(geometryBitmask & HeightValue)) {
257                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
258                 if (HeightOfScreen(scr) - 24 < int(height))
259                         height = HeightOfScreen(scr) - 24;
260         }
261
262         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
263
264         // recalculate xpos if it's not set
265         if (xpos == -1)
266                 xpos = (WidthOfScreen(s) - width) / 2;
267
268         // recalculate ypos if it's not set
269         if (ypos == -1)
270                 ypos = (HeightOfScreen(s) - height) / 2;
271
272         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
273                            << '+' << xpos << '+' << ypos << endl;
274
275         XFormsView view(width, height);
276         view.show(xpos, ypos, "LyX");
277         view.init();
278
279         Buffer * last = 0;
280
281         // FIXME: some code below needs moving
282
283         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
284
285         vector<string>::const_iterator cit = files.begin();
286         vector<string>::const_iterator end = files.end();
287         for (; cit != end; ++cit) {
288                 Buffer * b = bufferlist.loadLyXFile(*cit);
289                 if (b) {
290                         last = b;
291                 }
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                 while (Gtk::Main::events_pending())
307                         Gtk::Main::iteration(false);
308                 if (fl_check_forms() == FL_EVENT) {
309                         XEvent ev;
310                         fl_XNextEvent(&ev);
311                         lyxerr[Debug::GUI]
312                                 << "Received unhandled X11 event" << endl
313                                 << "Type: " << ev.xany.type
314                                 << " Target: 0x" << hex << ev.xany.window
315                                 << dec << endl;
316                 }
317
318         }
319
320         // FIXME: breaks emergencyCleanup
321         delete lyxserver;
322 }
323
324 void lyx_gui::exit()
325 {
326         finished = true;
327 }
328
329
330 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
331 {
332         // Nothing interesting to do here
333         return FuncStatus();
334 }
335
336 string const lyx_gui::hexname(LColor::color col)
337 {
338         string const name = lcolor.getX11Name(col);
339         Display * const display = fl_get_display();
340         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
341         XColor xcol, ccol;
342
343         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
344                         lyxerr << "X can't find color \""
345                                << lcolor.getLyXName(col)
346                                << '"' << endl;
347                         return string();
348         }
349
350         ostringstream os;
351
352         // Note that X stores the RGB values in the range 0 - 65535
353         // whilst we require them in the range 0 - 255.
354         os << setbase(16) << setfill('0')
355            << setw(2) << (xcol.red   / 256)
356            << setw(2) << (xcol.green / 256)
357            << setw(2) << (xcol.blue  / 256);
358
359         return STRCONV(os.str());
360 }
361
362
363 void lyx_gui::update_color(LColor::color col)
364 {
365         lyxColorHandler->updateColor(col);
366 }
367
368
369 void lyx_gui::update_fonts()
370 {
371         fontloader.update();
372 }
373
374
375 bool lyx_gui::font_available(LyXFont const & font)
376 {
377         return fontloader.available(font);
378 }
379
380 namespace {
381
382 extern "C"
383 void C_read_callback(int, void * data)
384 {
385         LyXComm * comm = static_cast<LyXComm *>(data);
386         comm->read_ready();
387 }
388
389 }
390
391 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
392 {
393         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
394 }
395
396
397 void lyx_gui::remove_read_callback(int fd)
398 {
399         fl_remove_io_callback(fd, FL_READ, C_read_callback);
400 }