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