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