]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/lyx_gui.C
Minipage is no more (long live the box inset)
[lyx.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 // FIXME: move this stuff out again
35 #include "bufferlist.h"
36 #include "buffer_funcs.h"
37 #include "lyxfunc.h"
38 #include "lyxserver.h"
39 #include "lyxsocket.h"
40 #include "BufferView.h"
41
42 #include "GView.h"
43 #include "GtkmmX.h"
44
45 #include "xftFontLoader.h"
46 #include "GWorkArea.h"
47
48 #include "support/std_sstream.h"
49 #include <iomanip>
50 #include <fcntl.h>
51 #include <boost/bind.hpp>
52
53 //just for xforms
54 #include "lyx_forms.h"
55 #include "xformsImage.h"
56 #include "xforms_helpers.h"
57
58 namespace os = lyx::support::os;
59
60 using std::ostringstream;
61 using std::string;
62
63
64 extern BufferList bufferlist;
65
66 // FIXME: wrong place !
67 LyXServer * lyxserver;
68 LyXServerSocket * lyxsocket;
69
70 bool lyx_gui::use_gui = true;
71
72 namespace {
73
74 /// quit lyx
75 bool finished = false;
76
77
78 /// estimate DPI from X server
79 float getDPI()
80 {
81         Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
82         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
83                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
84 }
85
86
87 /// set default GUI configuration
88 void setDefaults()
89 {
90         FL_IOPT cntl;
91         cntl.buttonFontSize = FL_NORMAL_SIZE;
92         cntl.browserFontSize = FL_NORMAL_SIZE;
93         cntl.labelFontSize = FL_NORMAL_SIZE;
94         cntl.choiceFontSize = FL_NORMAL_SIZE;
95         cntl.inputFontSize = FL_NORMAL_SIZE;
96         cntl.menuFontSize  = FL_NORMAL_SIZE;
97         cntl.borderWidth = -1;
98         cntl.vclass = FL_DefaultVisual;
99         fl_set_defaults(FL_PDVisual
100                         | FL_PDButtonFontSize
101                         | FL_PDBrowserFontSize
102                         | FL_PDLabelFontSize
103                         | FL_PDChoiceFontSize
104                         | FL_PDInputFontSize
105                         | FL_PDMenuFontSize
106                         | FL_PDBorderWidth, &cntl);
107 }
108
109
110 extern "C" {
111
112
113 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
114         // We don't abort on BadWindow
115         if (xeev->error_code == BadWindow) {
116                 lyxerr << "BadWindow received !" << std::endl;
117                 lyxerr << "If you're using xforms 1.0 or greater, "
118                        << " please report this to lyx-devel@lists.lyx.org"
119                        << std::endl;
120                 return 0;
121         }
122
123         // emergency cleanup
124         LyX::cref().emergencyCleanup();
125
126         // Get the reason for the crash.
127         char etxt[513];
128         XGetErrorText(display, xeev->error_code, etxt, 512);
129         lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
130         // By doing an abort we get a nice backtrace. (hopefully)
131         lyx::support::abort();
132         return 0;
133 }
134
135 }
136
137 /// read in geometry specification
138 char geometry[40];
139
140 } // namespace anon
141
142
143 void parse_init_xforms(int & argc, char * argv[])
144 {
145         setDefaults();
146
147         FL_CMD_OPT cmdopt[] = {
148                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
149         };
150
151         FL_resource res[] = {
152                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
153         };
154
155         const int num_res = sizeof(res)/sizeof(FL_resource);
156         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
157
158         // It appears that, in xforms >=0.89.5, fl_initialize()
159         // calls setlocale() and ruins our LC_NUMERIC setting.
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"
167                        << std::endl;
168                 lyx::support::os::warn("Unable to access X display, exiting");
169                 ::exit(1);
170         }
171
172         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
173
174         XSetErrorHandler(LyX_XErrHandler);
175
176         using namespace lyx::graphics;
177
178         // connect the image loader based on the xforms library
179         Image::newImage = boost::bind(&xformsImage::newImage);
180         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
181 }
182
183
184 void lyx_gui::parse_init(int & argc, char * argv[])
185 {
186         new Gtk::Main(argc, argv);
187
188         parse_init_xforms(argc, argv);
189
190         locale_init();
191
192         // must do this /before/ lyxrc gets read
193         lyxrc.dpi = getDPI();
194 }
195
196
197 void parse_lyxrc_xforms()
198 {
199         XformsColor::read(lyx::support::AddName(
200                                   lyx::support::user_lyxdir(), "preferences.xform"));
201
202         if (lyxrc.popup_font_encoding.empty())
203                 lyxrc.popup_font_encoding = lyxrc.font_norm;
204         // Set the font name for popups and menus
205         string boldfontname = lyxrc.popup_bold_font
206                                + "-*-*-*-?-*-*-*-*-"
207                                + lyxrc.popup_font_encoding;
208                 // "?" means "scale that font"
209         string fontname = lyxrc.popup_normal_font
210                                + "-*-*-*-?-*-*-*-*-"
211                                + lyxrc.popup_font_encoding;
212
213         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
214         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
215         if (bold < 0)
216                 lyxerr << "Could not set menu font to "
217                        << boldfontname << std::endl;
218
219         if (normal < 0)
220                 lyxerr << "Could not set popup font to "
221                        << fontname << std::endl;
222
223         if (bold < 0 && normal < 0) {
224                 lyxerr << "Using 'helvetica' font for menus" << std::endl;
225                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
226                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
227                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
228                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
229
230                 if (bold < 0 && normal < 0) {
231                         lyxerr << "Could not find helvetica font. Using 'fixed'."
232                                << std::endl;
233                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
234                         normal = bold = 0;
235                 }
236         }
237         if (bold < 0)
238                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
239         else if (normal < 0)
240                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
241
242         fl_setpup_fontstyle(FL_NORMAL_STYLE);
243         fl_setpup_fontsize(FL_NORMAL_SIZE);
244         fl_setpup_color(FL_MCOL, FL_BLACK);
245         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
246         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
247 }
248
249
250 void lyx_gui::parse_lyxrc()
251 {
252         parse_lyxrc_xforms();
253 }
254
255
256 void start_xforms()
257 {
258         // initial geometry
259         int xpos = -1;
260         int ypos = -1;
261         unsigned int width = 690;
262         unsigned int height = 510;
263
264         int const geometryBitmask =
265                 XParseGeometry(geometry,
266                                &xpos, &ypos, &width, &height);
267
268         // if width is not set by geometry, check it against monitor width
269         if (!(geometryBitmask & WidthValue)) {
270                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
271                 if (WidthOfScreen(scr) - 8 < int(width))
272                         width = WidthOfScreen(scr) - 8;
273         }
274
275         // if height is not set by geometry, check it against monitor height
276         if (!(geometryBitmask & HeightValue)) {
277                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
278                 if (HeightOfScreen(scr) - 24 < int(height))
279                         height = HeightOfScreen(scr) - 24;
280         }
281
282         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
283
284         // recalculate xpos if it's not set
285         if (xpos == -1)
286                 xpos = (WidthOfScreen(s) - width) / 2;
287
288         // recalculate ypos if it's not set
289         if (ypos == -1)
290                 ypos = (HeightOfScreen(s) - height) / 2;
291
292         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
293                            << '+' << xpos << '+' << ypos << std::endl;
294
295 //      XFormsView view(width, height);
296 //      view.show(xpos, ypos, "LyX");
297 //      view.init();
298 }
299
300
301 static void events_xforms()
302 {
303         if (fl_check_forms() == FL_EVENT) {
304                 XEvent ev;
305                 fl_XNextEvent(&ev);
306                 lyxerr[Debug::GUI]
307                         << "Received unhandled X11 event" << std::endl
308                         << "Type: " << ev.xany.type
309                         << " Target: 0x" << std::hex << ev.xany.window
310                         << std::dec << std::endl;
311         }
312 }
313
314
315 void lyx_gui::start(string const & batch, std::vector<string> const & files)
316 {
317         start_xforms();
318         // just for debug
319         XSynchronize(getDisplay(), true);
320
321         boost::shared_ptr<GView> view_ptr(new GView);
322         LyX::ref().addLyXView(view_ptr);
323
324         GView & view = *view_ptr.get();
325         view.show();
326         view.init();
327
328         Buffer * last = 0;
329
330         // FIXME: some code below needs moving
331
332         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
333         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
334                           os::slashify_path(os::getTmpDir() + "/lyxsocket"));
335
336         std::vector<string>::const_iterator cit = files.begin();
337         std::vector<string>::const_iterator end = files.end();
338         for (; cit != end; ++cit) {
339                 Buffer * b = bufferlist.newBuffer(*cit);
340                 if (loadLyXFile(b, *cit))
341                         last = b;
342         }
343
344         // switch to the last buffer successfully loaded
345         if (last) {
346                 view.view()->buffer(last);
347         }
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
423 bool readCallback(Glib::IOCondition /*condition*/, LyXComm * comm)
424 {
425         comm->read_ready();
426         return true;
427 }
428
429
430 std::map<int, SigC::Connection> gReadCallbackMap;
431
432 }
433
434
435 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
436 {
437         gReadCallbackMap[fd] = Glib::signal_io().connect(
438                 SigC::bind(SigC::slot(readCallback), comm),
439                 fd,
440                 Glib::IO_IN);
441 }
442
443
444 void lyx_gui::remove_read_callback(int fd)
445 {
446         gReadCallbackMap[fd].disconnect();
447         gReadCallbackMap.erase(fd);
448 }
449
450
451 void lyx_gui::set_datasocket_callback(LyXDataSocket * /* p */)
452 {}
453
454
455 void lyx_gui::remove_datasocket_callback(LyXDataSocket * /* p */)
456 {}
457
458
459 void lyx_gui::set_serversocket_callback(LyXServerSocket * /* p */)
460 {}
461
462
463 void lyx_gui::remove_serversocket_callback(LyXServerSocket * /* p */)
464 {}
465
466
467 string const lyx_gui::roman_font_name()
468 {
469         return "times";
470 }
471
472
473 string const lyx_gui::sans_font_name()
474 {
475         return "helvetica";
476 }
477
478
479 string const lyx_gui::typewriter_font_name()
480 {
481         return "courier";
482 }
483
484
485 void lyx_gui::sync_events()
486 {
487         // FIXME
488 }