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