]> git.lyx.org Git - features.git/blob - src/frontends/gtk/lyx_gui.C
change "support/std_sstream.h" to <sstream>
[features.git] / src / frontends / gtk / lyx_gui.C
1 /**
2  * \file gtk/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 Bjnes
7  * \author John Levon
8  * \author Huang Ying
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14 #include <gtkmm.h>
15
16 #include "lyx_gui.h"
17
18 #include "support/lyxlib.h"
19 #include "support/os.h"
20 #include "support/filetools.h"
21 #include "support/path_defines.h"
22
23 #include "debug.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26
27 #include "LColor.h"
28 #include "LyXAction.h"
29 #include "lyx_main.h"
30 #include "lyxrc.h"
31 #include "lyxfont.h"
32 #include "graphics/LoaderQueue.h"
33
34 #include "io_callback.h"
35
36 // FIXME: move this stuff out again
37 #include "bufferlist.h"
38 #include "buffer_funcs.h"
39 #include "lyxfunc.h"
40 #include "lyxserver.h"
41 #include "lyxsocket.h"
42 #include "BufferView.h"
43
44 #include "GView.h"
45 #include "GtkmmX.h"
46
47 #include "xftFontLoader.h"
48 #include "GWorkArea.h"
49
50 #include <boost/bind.hpp>
51 #include <boost/function.hpp>
52 #include <boost/shared_ptr.hpp>
53
54 #include <fcntl.h>
55
56 #include <sstream>
57 #include <iomanip>
58
59 //just for xforms
60 #include "lyx_forms.h"
61 #include "xformsImage.h"
62 #include "xforms_helpers.h"
63
64 namespace os = lyx::support::os;
65
66 using std::ostringstream;
67 using std::string;
68
69 using lyx::frontend::colorCache;
70 using lyx::frontend::GView;
71 using lyx::frontend::XformsColor;
72
73
74 extern BufferList bufferlist;
75
76 // FIXME: wrong place !
77 LyXServer * lyxserver;
78 LyXServerSocket * lyxsocket;
79
80 bool lyx_gui::use_gui = true;
81
82 namespace {
83
84 /// quit lyx
85 bool finished = false;
86
87
88 /// estimate DPI from X server
89 float getDPI()
90 {
91         Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
92         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
93                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
94 }
95
96
97 /// set default GUI configuration
98 void setDefaults()
99 {
100         FL_IOPT cntl;
101         cntl.buttonFontSize = FL_NORMAL_SIZE;
102         cntl.browserFontSize = FL_NORMAL_SIZE;
103         cntl.labelFontSize = FL_NORMAL_SIZE;
104         cntl.choiceFontSize = FL_NORMAL_SIZE;
105         cntl.inputFontSize = FL_NORMAL_SIZE;
106         cntl.menuFontSize  = FL_NORMAL_SIZE;
107         cntl.borderWidth = -1;
108         cntl.vclass = FL_DefaultVisual;
109         fl_set_defaults(FL_PDVisual
110                         | FL_PDButtonFontSize
111                         | FL_PDBrowserFontSize
112                         | FL_PDLabelFontSize
113                         | FL_PDChoiceFontSize
114                         | FL_PDInputFontSize
115                         | FL_PDMenuFontSize
116                         | FL_PDBorderWidth, &cntl);
117 }
118
119
120 extern "C" {
121
122
123 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
124         // We don't abort on BadWindow
125         if (xeev->error_code == BadWindow) {
126                 lyxerr << "BadWindow received !" << std::endl;
127                 lyxerr << "If you're using xforms 1.0 or greater, "
128                        << " please report this to lyx-devel@lists.lyx.org"
129                        << std::endl;
130                 return 0;
131         }
132
133         // emergency cleanup
134         LyX::cref().emergencyCleanup();
135
136         // Get the reason for the crash.
137         char etxt[513];
138         XGetErrorText(display, xeev->error_code, etxt, 512);
139         lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
140         // By doing an abort we get a nice backtrace. (hopefully)
141         lyx::support::abort();
142         return 0;
143 }
144
145 }
146
147 /// read in geometry specification
148 char geometry[40];
149
150 } // namespace anon
151
152
153 void parse_init_xforms(int & argc, char * argv[])
154 {
155         setDefaults();
156
157         FL_CMD_OPT cmdopt[] = {
158                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
159         };
160
161         FL_resource res[] = {
162                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
163         };
164
165         const int num_res = sizeof(res)/sizeof(FL_resource);
166         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
167
168         // It appears that, in xforms >=0.89.5, fl_initialize()
169         // calls setlocale() and ruins our LC_NUMERIC setting.
170
171         fl_get_app_resources(res, num_res);
172
173         Display * display = fl_get_display();
174
175         if (!display) {
176                 lyxerr << "LyX: unable to access X display, exiting"
177                        << std::endl;
178                 lyx::support::os::warn("Unable to access X display, exiting");
179                 ::exit(1);
180         }
181
182         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
183
184         XSetErrorHandler(LyX_XErrHandler);
185
186         using namespace lyx::graphics;
187
188         // connect the image loader based on the xforms library
189         Image::newImage = boost::bind(&xformsImage::newImage);
190         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
191 }
192
193
194 void lyx_gui::parse_init(int & argc, char * argv[])
195 {
196         new Gtk::Main(argc, argv);
197
198         parse_init_xforms(argc, argv);
199
200         locale_init();
201
202         // must do this /before/ lyxrc gets read
203         lyxrc.dpi = getDPI();
204 }
205
206
207 void parse_lyxrc_xforms()
208 {
209         XformsColor::read(lyx::support::AddName(
210                                   lyx::support::user_lyxdir(), "preferences.xform"));
211
212         if (lyxrc.popup_font_encoding.empty())
213                 lyxrc.popup_font_encoding = lyxrc.font_norm;
214         // Set the font name for popups and menus
215         string boldfontname = lyxrc.popup_bold_font
216                                + "-*-*-*-?-*-*-*-*-"
217                                + lyxrc.popup_font_encoding;
218                 // "?" means "scale that font"
219         string fontname = lyxrc.popup_normal_font
220                                + "-*-*-*-?-*-*-*-*-"
221                                + lyxrc.popup_font_encoding;
222
223         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
224         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
225         if (bold < 0)
226                 lyxerr << "Could not set menu font to "
227                        << boldfontname << std::endl;
228
229         if (normal < 0)
230                 lyxerr << "Could not set popup font to "
231                        << fontname << std::endl;
232
233         if (bold < 0 && normal < 0) {
234                 lyxerr << "Using 'helvetica' font for menus" << std::endl;
235                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
236                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
237                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
238                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
239
240                 if (bold < 0 && normal < 0) {
241                         lyxerr << "Could not find helvetica font. Using 'fixed'."
242                                << std::endl;
243                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
244                         normal = bold = 0;
245                 }
246         }
247         if (bold < 0)
248                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
249         else if (normal < 0)
250                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
251
252         fl_setpup_fontstyle(FL_NORMAL_STYLE);
253         fl_setpup_fontsize(FL_NORMAL_SIZE);
254         fl_setpup_color(FL_MCOL, FL_BLACK);
255         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
256         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
257 }
258
259
260 void lyx_gui::parse_lyxrc()
261 {
262         parse_lyxrc_xforms();
263 }
264
265
266 void start_xforms()
267 {
268         // initial geometry
269         int xpos = -1;
270         int ypos = -1;
271         unsigned int width = 690;
272         unsigned int height = 510;
273
274         int const geometryBitmask =
275                 XParseGeometry(geometry,
276                                &xpos, &ypos, &width, &height);
277
278         // if width is not set by geometry, check it against monitor width
279         if (!(geometryBitmask & WidthValue)) {
280                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
281                 if (WidthOfScreen(scr) - 8 < int(width))
282                         width = WidthOfScreen(scr) - 8;
283         }
284
285         // if height is not set by geometry, check it against monitor height
286         if (!(geometryBitmask & HeightValue)) {
287                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
288                 if (HeightOfScreen(scr) - 24 < int(height))
289                         height = HeightOfScreen(scr) - 24;
290         }
291
292         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
293
294         // recalculate xpos if it's not set
295         if (xpos == -1)
296                 xpos = (WidthOfScreen(s) - width) / 2;
297
298         // recalculate ypos if it's not set
299         if (ypos == -1)
300                 ypos = (HeightOfScreen(s) - height) / 2;
301
302         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
303                            << '+' << xpos << '+' << ypos << std::endl;
304
305 //      XFormsView view(width, height);
306 //      view.show(xpos, ypos, "LyX");
307 //      view.init();
308 }
309
310
311 static void events_xforms()
312 {
313         if (fl_check_forms() == FL_EVENT) {
314                 XEvent ev;
315                 fl_XNextEvent(&ev);
316                 lyxerr[Debug::GUI]
317                         << "Received unhandled X11 event" << std::endl
318                         << "Type: " << ev.xany.type
319                         << " Target: 0x" << std::hex << ev.xany.window
320                         << std::dec << std::endl;
321         }
322 }
323
324
325 void lyx_gui::start(string const & batch, std::vector<string> const & files)
326 {
327         start_xforms();
328         // just for debug
329         XSynchronize(getDisplay(), true);
330
331         boost::shared_ptr<GView> view_ptr(new GView);
332         LyX::ref().addLyXView(view_ptr);
333
334         GView & view = *view_ptr.get();
335         view.show();
336         view.init();
337
338         // FIXME: some code below needs moving
339
340         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
341         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
342                           os::slashify_path(os::getTmpDir() + "/lyxsocket"));
343
344         std::vector<string>::const_iterator cit = files.begin();
345         std::vector<string>::const_iterator end = files.end();
346         for (; cit != end; ++cit)
347                 view.view()->loadLyXFile(*cit, true);
348
349         // handle the batch commands the user asked for
350         if (!batch.empty()) {
351                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
352         }
353
354         // enter the event loop
355         while (!finished) {
356                 while (Gtk::Main::events_pending())
357                         Gtk::Main::iteration(false);
358                 events_xforms();
359         }
360
361         // FIXME: breaks emergencyCleanup
362         delete lyxsocket;
363         delete lyxserver;
364 }
365
366
367 void lyx_gui::exit()
368 {
369         finished = true;
370 }
371
372
373 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
374 {
375         // Nothing interesting to do here
376         return FuncStatus();
377 }
378
379
380 string const lyx_gui::hexname(LColor_color col)
381 {
382         Gdk::Color gdkColor;
383         Gdk::Color * gclr = colorCache.getColor(col);
384         if (!gclr) {
385                 gclr = &gdkColor;
386                 gclr->parse(lcolor.getX11Name(col));
387         }
388
389         std::ostringstream os;
390
391         // Note that X stores the RGB values in the range 0 - 65535
392         // whilst we require them in the range 0 - 255.
393         os << std::setbase(16) << std::setfill('0')
394            << std::setw(2) << (gclr->get_red() / 256)
395            << std::setw(2) << (gclr->get_green() / 256)
396            << std::setw(2) << (gclr->get_blue() / 256);
397
398         return os.str();
399 }
400
401
402 void lyx_gui::update_color(LColor_color /*col*/)
403 {
404         colorCache.clear();
405 }
406
407
408 void lyx_gui::update_fonts()
409 {
410         fontLoader.update();
411 }
412
413
414 bool lyx_gui::font_available(LyXFont const & font)
415 {
416         return fontLoader.available(font);
417 }
418
419
420 namespace {
421
422 std::map<int, boost::shared_ptr<io_callback> > callbacks;
423
424 } // NS anon
425
426
427 void lyx_gui::register_socket_callback(int fd,
428                                        boost::function<void()> func)
429 {
430         callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
431 }
432
433
434 void lyx_gui::unregister_socket_callback(int fd)
435 {
436         callbacks.erase(fd);
437 }
438
439
440 string const lyx_gui::roman_font_name()
441 {
442         return "times";
443 }
444
445
446 string const lyx_gui::sans_font_name()
447 {
448         return "helvetica";
449 }
450
451
452 string const lyx_gui::typewriter_font_name()
453 {
454         return "courier";
455 }
456
457
458 void lyx_gui::sync_events()
459 {
460         // FIXME
461 }