3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjnes
10 * Full author contact details are available in file CREDITS.
15 // Too hard to make concept checks work with this file
16 #ifdef _GLIBCXX_CONCEPT_CHECKS
17 #undef _GLIBCXX_CONCEPT_CHECKS
19 #ifdef _GLIBCPP_CONCEPT_CHECKS
20 #undef _GLIBCPP_CONCEPT_CHECKS
26 #include "funcrequest.h"
30 #include "LyXAction.h"
34 #include "graphics/LoaderQueue.h"
36 #include "io_callback.h"
38 // FIXME: move this stuff out again
39 #include "bufferlist.h"
41 #include "lyxserver.h"
42 #include "lyxsocket.h"
43 #include "BufferView.h"
48 #include "xftFontLoader.h"
49 #include "GWorkArea.h"
52 #include "lyx_forms.h"
53 #include "xforms_helpers.h"
55 #include "support/lyxlib.h"
56 #include "support/os.h"
57 #include "support/filetools.h"
58 #include "support/package.h"
62 #include "LyXGdkImage.h"
64 #include <boost/bind.hpp>
65 #include <boost/function.hpp>
66 #include <boost/shared_ptr.hpp>
73 namespace os = lyx::support::os;
75 using std::ostringstream;
78 using lyx::support::package;
80 using lyx::frontend::colorCache;
81 using lyx::frontend::GView;
82 using lyx::frontend::XformsColor;
85 extern BufferList bufferlist;
87 // FIXME: wrong place !
88 LyXServer * lyxserver;
89 LyXServerSocket * lyxsocket;
91 bool lyx_gui::use_gui = true;
96 bool finished = false;
99 /// estimate DPI from X server
102 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
103 return int(((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
104 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2);
108 /// set default GUI configuration
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
124 | FL_PDChoiceFontSize
127 | FL_PDBorderWidth, &cntl);
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"
145 LyX::cref().emergencyCleanup();
147 // Get the reason for the crash.
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();
158 /// read in geometry specification
164 void parse_init_xforms(int & argc, char * argv[])
168 FL_CMD_OPT cmdopt[] = {
169 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
172 FL_resource res[] = {
173 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
176 const int num_res = sizeof(res)/sizeof(FL_resource);
177 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
179 // It appears that, in xforms >=0.89.5, fl_initialize()
180 // calls setlocale() and ruins our LC_NUMERIC setting.
182 fl_get_app_resources(res, num_res);
184 Display * display = fl_get_display();
187 lyxerr << "LyX: unable to access X display, exiting"
192 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
194 XSetErrorHandler(LyX_XErrHandler);
199 void lyx_gui::parse_init(int & argc, char * argv[])
201 new Gtk::Main(argc, argv);
203 parse_init_xforms(argc, argv);
205 using namespace lyx::graphics;
206 Image::newImage = boost::bind(&LyXGdkImage::newImage);
207 Image::loadableFormats = boost::bind(&LyXGdkImage::loadableFormats);
211 // must do this /before/ lyxrc gets read
212 lyxrc.dpi = getDPI();
216 void parse_lyxrc_xforms()
218 XformsColor::read(lyx::support::AddName(package().user_support(),
219 "preferences.xform"));
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;
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());
235 lyxerr << "Could not set menu font to "
236 << boldfontname << std::endl;
239 lyxerr << "Could not set popup font to "
240 << fontname << std::endl;
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());
249 if (bold < 0 && normal < 0) {
250 lyxerr << "Could not find helvetica font. Using 'fixed'."
252 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
257 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
259 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
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);
269 void lyx_gui::parse_lyxrc()
271 parse_lyxrc_xforms();
280 unsigned int width = 690;
281 unsigned int height = 510;
283 int const geometryBitmask =
284 XParseGeometry(geometry,
285 &xpos, &ypos, &width, &height);
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;
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;
301 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
303 // recalculate xpos if it's not set
305 xpos = (WidthOfScreen(s) - width) / 2;
307 // recalculate ypos if it's not set
309 ypos = (HeightOfScreen(s) - height) / 2;
311 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
312 << '+' << xpos << '+' << ypos << std::endl;
314 // XFormsView view(width, height);
315 // view.show(xpos, ypos, "LyX");
320 static void events_xforms()
322 if (fl_check_forms() == FL_EVENT) {
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;
334 void lyx_gui::start(string const & batch, std::vector<string> const & files)
338 //XSynchronize(getDisplay(), true);
340 boost::shared_ptr<GView> view_ptr(new GView);
341 LyX::ref().addLyXView(view_ptr);
343 GView & view = *view_ptr.get();
347 // FIXME: server code below needs moving
349 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
350 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
351 os::internal_path(package().temp_dir() + "/lyxsocket"));
353 for_each(files.begin(), files.end(),
354 bind(&BufferView::loadLyXFile, view.view(), _1, true));
356 // handle the batch commands the user asked for
357 if (!batch.empty()) {
358 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
361 // enter the event loop
363 while (Gtk::Main::events_pending())
364 Gtk::Main::iteration(false);
368 // FIXME: breaks emergencyCleanup
380 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
384 // Add this back if the gtk doc prefs dialog includes preamble - jcs
385 /*case LFUN_DIALOG_SHOW:
386 if (ev.argument == "preamble")
389 case LFUN_TOOLTIPS_TOGGLE:
400 string const lyx_gui::hexname(LColor_color col)
403 Gdk::Color * gclr = colorCache.getColor(col);
406 gclr->parse(lcolor.getX11Name(col));
409 std::ostringstream os;
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);
422 void lyx_gui::update_color(LColor_color /*col*/)
428 void lyx_gui::update_fonts()
434 bool lyx_gui::font_available(LyXFont const & font)
436 return fontLoader.available(font);
442 std::map<int, boost::shared_ptr<io_callback> > callbacks;
447 void lyx_gui::register_socket_callback(int fd,
448 boost::function<void()> func)
450 callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
454 void lyx_gui::unregister_socket_callback(int fd)
460 string const lyx_gui::roman_font_name()
466 string const lyx_gui::sans_font_name()
472 string const lyx_gui::typewriter_font_name()
478 void lyx_gui::sync_events()