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