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