1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
18 #include FORMS_H_LOCATION
19 #include "tex-strings.h"
23 #include "frontends/LyXView.h"
25 #include "BufferView.h"
26 #include "lyxserver.h"
29 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
30 #include "frontends/xforms/lyxlookup.h"
32 #include "bufferlist.h"
33 #include "frontends/xforms/ColorHandler.h"
35 #include "frontends/GUIRunTime.h"
36 #include "frontends/xforms/xforms_helpers.h" // for XformColor
38 #include "support/filetools.h"
39 #include "support/os.h"
40 #include "support/lyxlib.h"
47 extern LyXServer * lyxserver;
48 extern bool finished; // flag, that we are quitting the program
49 extern BufferList bufferlist;
50 extern string user_lyxdir;
54 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
70 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
77 int LyX_XErrHandler(Display * display, XErrorEvent * xeev)
79 if (xeev->error_code == BadWindow) {
80 // We don't abort on BadWindow
85 LyX::emergencyCleanup();
87 // Get the reason for the crash.
89 XGetErrorText(display, xeev->error_code, etxt, 512);
90 lyxerr << etxt << " id: " << xeev->resourceid << endl;
91 // By doing an abort we get a nice backtrace. (hopefully)
99 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
100 : _owner(owner), lyxViews(0)
109 static const int num_res = sizeof(res)/sizeof(FL_resource);
110 fl_initialize(argc, argv, "LyX", cmdopt, num_res);
111 // It appears that, in xforms >=0.89.5, fl_initialize()
112 // calls setlocale() and ruins our LC_NUMERIC setting.
114 fl_get_app_resources(res, num_res);
116 static const int geometryBitmask =
117 XParseGeometry(geometry,
120 reinterpret_cast<unsigned int *>(&width),
121 reinterpret_cast<unsigned int *>(&height));
123 Display * display = fl_get_display();
125 lyxerr << "LyX: unable to access X display, exiting" << endl;
126 os::warn("Unable to access X display, exiting");
129 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
130 // X Error handler install goes here
131 XSetErrorHandler(LyX_XErrHandler);
133 // A width less than 590 pops up an awkward main window
134 // The minimal values of width/height (590/400) are defined in
136 if (width < 590) width = 590;
137 if (height < 400) height = 400;
139 // If width is not set by geometry, check it against monitor width
140 if (!(geometryBitmask & 4)) {
141 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
142 if (WidthOfScreen(scr) - 8 < width)
143 width = WidthOfScreen(scr) - 8;
146 // If height is not set by geometry, check it against monitor height
147 if (!(geometryBitmask & 8)) {
148 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
149 if (HeightOfScreen(scr) - 24 < height)
150 height = HeightOfScreen(scr) - 24;
153 // Recalculate xpos if it's negative
154 if (geometryBitmask & 16)
155 xpos += WidthOfScreen(ScreenOfDisplay(fl_get_display(),
158 // Recalculate ypos if it's negative
159 if (geometryBitmask & 32)
160 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(),
161 fl_screen)) - height;
163 // Initialize the LyXColorHandler
164 lyxColorHandler.reset(new LyXColorHandler);
168 // A destructor is always necessary (asierra-970604)
171 // Lyxserver was created in this class so should be destroyed
172 // here. asierra-970604
176 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
182 void LyXGUI::setDefaults()
184 GUIRunTime::setDefaults();
188 // This is called after we have parsed lyxrc
196 if (lyxrc.popup_font_encoding.empty())
197 lyxrc.popup_font_encoding = lyxrc.font_norm;
198 // Set the font name for popups and menus
199 string boldfontname = lyxrc.popup_bold_font
200 + "-*-*-*-?-*-*-*-*-"
201 + lyxrc.popup_font_encoding;
202 // "?" means "scale that font"
203 string fontname = lyxrc.popup_normal_font
204 + "-*-*-*-?-*-*-*-*-"
205 + lyxrc.popup_font_encoding;
207 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
208 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
210 lyxerr << "Could not set menu font to "
211 << boldfontname << endl;
214 lyxerr << "Could not set popup font to "
217 if (bold < 0 && normal < 0) {
218 lyxerr << "Using 'helvetica' font for menus" << endl;
219 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
220 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
221 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
222 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
224 if (bold < 0 && normal < 0) {
225 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
226 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
231 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
233 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
235 // put here (after fl_initialize) to avoid segfault. Cannot be done
236 // in setDefaults() (Matthias 140496)
237 // Moved from ::LyXGUI to ::init to allow popup font customization
239 fl_setpup_fontstyle(FL_NORMAL_STYLE);
240 fl_setpup_fontsize(FL_NORMAL_SIZE);
241 fl_setpup_color(FL_MCOL, FL_BLACK);
242 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
244 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
246 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
249 // all lyxrc settings has to be done here as lyxrc has not yet
250 // been read when the GUI is created (Jug)
252 // Update parameters.
255 // Initialize the views.
258 // this should be moved ...
259 lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
263 void LyXGUI::create_forms()
265 lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
266 lyxViews = GUIRunTime::createMainView(width, height);
267 lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
269 // From here down should be done by somebody else. (Lgb)
271 // This is probably as good a time as any to map the xform colours,
272 // should a mapping exist.
273 string const filename = AddName(user_lyxdir, "preferences.xform");
274 XformsColor::read(filename);
276 // Show the main & title form
277 int main_placement = FL_PLACE_CENTER | FL_FREE_SIZE;
278 // Did we get a valid position?
279 if (xpos >= 0 && ypos >= 0) {
280 lyxViews->setPosition(xpos, ypos);
281 main_placement = FL_PLACE_POSITION;
284 lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
288 void LyXGUI::runTime()
292 GUIRunTime::runTime();
296 void LyXGUI::regBuf(Buffer * b)
298 lyxViews->view()->buffer(b);
302 LyXView * LyXGUI::getLyXView() const