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