]> git.lyx.org Git - lyx.git/blob - src/lyx_gui.C
Trivial fix for compiler warning.
[lyx.git] / src / lyx_gui.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "lyx_gui.h"
18 #include FORMS_H_LOCATION
19 #include "tex-strings.h"
20 #include "lyx_main.h"
21 #include "debug.h"
22 #include "version.h"
23 #include "frontends/LyXView.h"
24 #include "buffer.h"
25 #include "BufferView.h"
26 #include "lyxserver.h"
27 #include "lyxrc.h"
28 #include "gettext.h"
29 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
30 #include "frontends/xforms/lyxlookup.h"
31 #endif
32 #include "bufferlist.h"
33 #include "frontends/xforms/ColorHandler.h"
34
35 #include "frontends/GUIRunTime.h"
36 #include "frontends/xforms/xforms_helpers.h" // for XformColor
37
38 #include "support/filetools.h"
39 #include "support/os.h"
40 #include "support/lyxlib.h"
41
42 #include <cstdlib>
43 #include <fcntl.h>
44
45 #ifndef CXX_GLOBAL_CSTD
46 using std::exit;
47 #endif
48
49 using std::endl;
50
51 extern LyXServer * lyxserver;
52 extern bool finished;   // flag, that we are quitting the program
53 extern BufferList bufferlist;
54 extern string user_lyxdir;
55
56 FL_CMD_OPT cmdopt[] =
57 {
58         {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
59 };
60
61 namespace {
62
63 int width  = 690;
64 int height = 510;
65 int xpos   = -1;
66 int ypos   = -1;
67 char geometry[40];
68
69 } // namespace anon
70
71
72 FL_resource res[] =
73 {
74         {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
75 };
76
77
78 extern "C" {
79
80 static
81 int LyX_XErrHandler(Display * display, XErrorEvent * xeev)
82 {
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;
88                 return 0;
89         }
90
91         // emergency cleanup
92         LyX::emergencyCleanup();
93
94         // Get the reason for the crash.
95         char etxt[513];
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)
99         lyx::abort();
100         return 0;
101 }
102
103 }
104
105
106 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
107         : _owner(owner), lyxViews(0)
108 {
109         gui = GUI;
110         if (!gui)
111                 return;
112
113         //
114         setDefaults();
115
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.
120         locale_init();
121         fl_get_app_resources(res, num_res);
122
123         static const int geometryBitmask =
124                 XParseGeometry(geometry,
125                                 &xpos,
126                                 &ypos,
127                                 reinterpret_cast<unsigned int *>(&width),
128                                 reinterpret_cast<unsigned int *>(&height));
129
130         Display * display = fl_get_display();
131         if (!display) {
132                 lyxerr << "LyX: unable to access X display, exiting" << endl;
133                 os::warn("Unable to access X display, exiting");
134                 exit(1);
135         }
136         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
137         // X Error handler install goes here
138         XSetErrorHandler(LyX_XErrHandler);
139
140         // A width less than 590 pops up an awkward main window
141         // The minimal values of width/height (590/400) are defined in
142         // src/lyx.C
143         if (width < 590) width = 590;
144         if (height < 400) height = 400;
145
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;
151         }
152
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;
158         }
159
160         // Recalculate xpos if it's negative
161         if (geometryBitmask & 16)
162                 xpos += WidthOfScreen(ScreenOfDisplay(fl_get_display(),
163                                                       fl_screen)) - width;
164
165         // Recalculate ypos if it's negative
166         if (geometryBitmask & 32)
167                 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(),
168                                                        fl_screen)) - height;
169
170         // Initialize the LyXColorHandler
171         lyxColorHandler.reset(new LyXColorHandler);
172 }
173
174
175 // A destructor is always necessary  (asierra-970604)
176 LyXGUI::~LyXGUI()
177 {
178         // Lyxserver was created in this class so should be destroyed
179         // here.  asierra-970604
180         delete lyxserver;
181         lyxserver = 0;
182         delete lyxViews;
183 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
184         CloseLyXLookup();
185 #endif
186 }
187
188
189 void LyXGUI::setDefaults()
190 {
191         GUIRunTime::setDefaults();
192 }
193
194
195 // This is called after we have parsed lyxrc
196 void LyXGUI::init()
197 {
198         if (!gui)
199                 return;
200
201         create_forms();
202
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;
213
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());
216         if (bold < 0)
217                 lyxerr << "Could not set menu font to "
218                        << boldfontname << endl;
219
220         if (normal < 0)
221                 lyxerr << "Could not set popup font to "
222                        << fontname << endl;
223
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());
230
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");
234                         normal = bold = 0;
235                 }
236         }
237         if (bold < 0)
238                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
239         else if (normal < 0)
240                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
241
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
245         // (petr 120997).
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);
252 #else
253         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
254 #endif
255
256         // all lyxrc settings has to be done here as lyxrc has not yet
257         // been read when the GUI is created (Jug)
258
259         // Update parameters.
260         lyxViews->redraw();
261
262         // Initialize the views.
263         lyxViews->init();
264
265         // this should be moved ...
266         lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
267 }
268
269
270 void LyXGUI::create_forms()
271 {
272         lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
273         lyxViews = GUIRunTime::createMainView(width, height);
274         lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
275
276         // From here down should be done by somebody else. (Lgb)
277
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);
282
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;
289         }
290
291         lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
292 }
293
294
295 void LyXGUI::runTime()
296 {
297         if (!gui) return;
298
299         GUIRunTime::runTime();
300 }
301
302
303 void LyXGUI::regBuf(Buffer * b)
304 {
305         lyxViews->view()->buffer(b);
306 }
307
308
309 LyXView * LyXGUI::getLyXView() const
310 {
311         return lyxViews;
312 }