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_VERSION < 1 && (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"
45 #ifndef CXX_GLOBAL_CSTD
51 extern LyXServer * lyxserver;
52 extern bool finished; // flag, that we are quitting the program
53 extern BufferList bufferlist;
54 extern string user_lyxdir;
58 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
74 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
81 int LyX_XErrHandler(Display * display, XErrorEvent * xeev)
83 // We don't abort on BadWindow
84 if (xeev->error_code == BadWindow) {
85 lyxerr << "BadWindow received !" << endl;
86 lyxerr << "If you're using xforms 1.0 or greater, "
87 << " please report this to lyx-devel@lists.lyx.org" << endl;
92 LyX::emergencyCleanup();
94 // Get the reason for the crash.
96 XGetErrorText(display, xeev->error_code, etxt, 512);
97 lyxerr << etxt << " id: " << xeev->resourceid << endl;
98 // By doing an abort we get a nice backtrace. (hopefully)
106 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
107 : _owner(owner), lyxViews(0)
116 static const int num_res = sizeof(res)/sizeof(FL_resource);
117 fl_initialize(argc, argv, "LyX", cmdopt, num_res);
118 // It appears that, in xforms >=0.89.5, fl_initialize()
119 // calls setlocale() and ruins our LC_NUMERIC setting.
121 fl_get_app_resources(res, num_res);
123 static const int geometryBitmask =
124 XParseGeometry(geometry,
127 reinterpret_cast<unsigned int *>(&width),
128 reinterpret_cast<unsigned int *>(&height));
130 Display * display = fl_get_display();
132 lyxerr << "LyX: unable to access X display, exiting" << endl;
133 os::warn("Unable to access X display, exiting");
136 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
137 // X Error handler install goes here
138 XSetErrorHandler(LyX_XErrHandler);
140 // A width less than 590 pops up an awkward main window
141 // The minimal values of width/height (590/400) are defined in
143 if (width < 590) width = 590;
144 if (height < 400) height = 400;
146 // If width is not set by geometry, check it against monitor width
147 if (!(geometryBitmask & 4)) {
148 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
149 if (WidthOfScreen(scr) - 8 < width)
150 width = WidthOfScreen(scr) - 8;
153 // If height is not set by geometry, check it against monitor height
154 if (!(geometryBitmask & 8)) {
155 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
156 if (HeightOfScreen(scr) - 24 < height)
157 height = HeightOfScreen(scr) - 24;
160 // Recalculate xpos if it's negative
161 if (geometryBitmask & 16)
162 xpos += WidthOfScreen(ScreenOfDisplay(fl_get_display(),
165 // Recalculate ypos if it's negative
166 if (geometryBitmask & 32)
167 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(),
168 fl_screen)) - height;
170 // Initialize the LyXColorHandler
171 lyxColorHandler.reset(new LyXColorHandler);
175 // A destructor is always necessary (asierra-970604)
178 // Lyxserver was created in this class so should be destroyed
179 // here. asierra-970604
183 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
189 void LyXGUI::setDefaults()
191 GUIRunTime::setDefaults();
195 // This is called after we have parsed lyxrc
203 if (lyxrc.popup_font_encoding.empty())
204 lyxrc.popup_font_encoding = lyxrc.font_norm;
205 // Set the font name for popups and menus
206 string boldfontname = lyxrc.popup_bold_font
207 + "-*-*-*-?-*-*-*-*-"
208 + lyxrc.popup_font_encoding;
209 // "?" means "scale that font"
210 string fontname = lyxrc.popup_normal_font
211 + "-*-*-*-?-*-*-*-*-"
212 + lyxrc.popup_font_encoding;
214 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
215 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
217 lyxerr << "Could not set menu font to "
218 << boldfontname << endl;
221 lyxerr << "Could not set popup font to "
224 if (bold < 0 && normal < 0) {
225 lyxerr << "Using 'helvetica' font for menus" << endl;
226 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
227 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
228 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
229 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
231 if (bold < 0 && normal < 0) {
232 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
233 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
238 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
240 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
242 // put here (after fl_initialize) to avoid segfault. Cannot be done
243 // in setDefaults() (Matthias 140496)
244 // Moved from ::LyXGUI to ::init to allow popup font customization
246 fl_setpup_fontstyle(FL_NORMAL_STYLE);
247 fl_setpup_fontsize(FL_NORMAL_SIZE);
248 fl_setpup_color(FL_MCOL, FL_BLACK);
249 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
250 #if FL_VERSION < 1 && FL_REVISION < 89
251 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
253 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
256 // all lyxrc settings has to be done here as lyxrc has not yet
257 // been read when the GUI is created (Jug)
259 // Update parameters.
262 // Initialize the views.
265 // this should be moved ...
266 lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
270 void LyXGUI::create_forms()
272 lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
273 lyxViews = GUIRunTime::createMainView(width, height);
274 lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
276 // From here down should be done by somebody else. (Lgb)
278 // This is probably as good a time as any to map the xform colours,
279 // should a mapping exist.
280 string const filename = AddName(user_lyxdir, "preferences.xform");
281 XformsColor::read(filename);
283 // Show the main & title form
284 int main_placement = FL_PLACE_CENTER | FL_FREE_SIZE;
285 // Did we get a valid position?
286 if (xpos >= 0 && ypos >= 0) {
287 lyxViews->setPosition(xpos, ypos);
288 main_placement = FL_PLACE_POSITION;
291 lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
295 void LyXGUI::runTime()
299 GUIRunTime::runTime();
303 void LyXGUI::regBuf(Buffer * b)
305 lyxViews->view()->buffer(b);
309 LyXView * LyXGUI::getLyXView() const