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 if (xeev->error_code == BadWindow) {
84 // We don't abort on BadWindow
89 LyX::emergencyCleanup();
91 // Get the reason for the crash.
93 XGetErrorText(display, xeev->error_code, etxt, 512);
94 lyxerr << etxt << " id: " << xeev->resourceid << endl;
95 // By doing an abort we get a nice backtrace. (hopefully)
103 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
104 : _owner(owner), lyxViews(0)
113 static const int num_res = sizeof(res)/sizeof(FL_resource);
114 fl_initialize(argc, argv, "LyX", cmdopt, num_res);
115 // It appears that, in xforms >=0.89.5, fl_initialize()
116 // calls setlocale() and ruins our LC_NUMERIC setting.
118 fl_get_app_resources(res, num_res);
120 static const int geometryBitmask =
121 XParseGeometry(geometry,
124 reinterpret_cast<unsigned int *>(&width),
125 reinterpret_cast<unsigned int *>(&height));
127 Display * display = fl_get_display();
129 lyxerr << "LyX: unable to access X display, exiting" << endl;
130 os::warn("Unable to access X display, exiting");
133 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
134 // X Error handler install goes here
135 XSetErrorHandler(LyX_XErrHandler);
137 // A width less than 590 pops up an awkward main window
138 // The minimal values of width/height (590/400) are defined in
140 if (width < 590) width = 590;
141 if (height < 400) height = 400;
143 // If width is not set by geometry, check it against monitor width
144 if (!(geometryBitmask & 4)) {
145 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
146 if (WidthOfScreen(scr) - 8 < width)
147 width = WidthOfScreen(scr) - 8;
150 // If height is not set by geometry, check it against monitor height
151 if (!(geometryBitmask & 8)) {
152 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
153 if (HeightOfScreen(scr) - 24 < height)
154 height = HeightOfScreen(scr) - 24;
157 // Recalculate xpos if it's negative
158 if (geometryBitmask & 16)
159 xpos += WidthOfScreen(ScreenOfDisplay(fl_get_display(),
162 // Recalculate ypos if it's negative
163 if (geometryBitmask & 32)
164 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(),
165 fl_screen)) - height;
167 // Initialize the LyXColorHandler
168 lyxColorHandler.reset(new LyXColorHandler);
172 // A destructor is always necessary (asierra-970604)
175 // Lyxserver was created in this class so should be destroyed
176 // here. asierra-970604
180 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
186 void LyXGUI::setDefaults()
188 GUIRunTime::setDefaults();
192 // This is called after we have parsed lyxrc
200 if (lyxrc.popup_font_encoding.empty())
201 lyxrc.popup_font_encoding = lyxrc.font_norm;
202 // Set the font name for popups and menus
203 string boldfontname = lyxrc.popup_bold_font
204 + "-*-*-*-?-*-*-*-*-"
205 + lyxrc.popup_font_encoding;
206 // "?" means "scale that font"
207 string fontname = lyxrc.popup_normal_font
208 + "-*-*-*-?-*-*-*-*-"
209 + lyxrc.popup_font_encoding;
211 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
212 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
214 lyxerr << "Could not set menu font to "
215 << boldfontname << endl;
218 lyxerr << "Could not set popup font to "
221 if (bold < 0 && normal < 0) {
222 lyxerr << "Using 'helvetica' font for menus" << endl;
223 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
224 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
225 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
226 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
228 if (bold < 0 && normal < 0) {
229 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
230 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
235 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
237 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
239 // put here (after fl_initialize) to avoid segfault. Cannot be done
240 // in setDefaults() (Matthias 140496)
241 // Moved from ::LyXGUI to ::init to allow popup font customization
243 fl_setpup_fontstyle(FL_NORMAL_STYLE);
244 fl_setpup_fontsize(FL_NORMAL_SIZE);
245 fl_setpup_color(FL_MCOL, FL_BLACK);
246 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
247 #if FL_VERSION < 1 && FL_REVISION < 89
248 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
250 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
253 // all lyxrc settings has to be done here as lyxrc has not yet
254 // been read when the GUI is created (Jug)
256 // Update parameters.
259 // Initialize the views.
262 // this should be moved ...
263 lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
267 void LyXGUI::create_forms()
269 lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
270 lyxViews = GUIRunTime::createMainView(width, height);
271 lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
273 // From here down should be done by somebody else. (Lgb)
275 // This is probably as good a time as any to map the xform colours,
276 // should a mapping exist.
277 string const filename = AddName(user_lyxdir, "preferences.xform");
278 XformsColor::read(filename);
280 // Show the main & title form
281 int main_placement = FL_PLACE_CENTER | FL_FREE_SIZE;
282 // Did we get a valid position?
283 if (xpos >= 0 && ypos >= 0) {
284 lyxViews->setPosition(xpos, ypos);
285 main_placement = FL_PLACE_POSITION;
288 lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
292 void LyXGUI::runTime()
296 GUIRunTime::runTime();
300 void LyXGUI::regBuf(Buffer * b)
302 lyxViews->view()->buffer(b);
306 LyXView * LyXGUI::getLyXView() const