]> git.lyx.org Git - lyx.git/blob - src/lyx_gui.C
Fix export of graphics file with relative path name.
[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 "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_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
30 #include "lyxlookup.h"
31 #endif
32 #include "bufferlist.h"
33 #include "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 using std::endl;
46
47 extern LyXServer * lyxserver;
48 extern bool finished;   // flag, that we are quitting the program
49 extern BufferList bufferlist;
50 extern string user_lyxdir;
51
52 FL_CMD_OPT cmdopt[] =
53 {
54         {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
55 };
56
57 namespace {
58
59 int width  = 690;
60 int height = 510;
61 int xpos   = -1;
62 int ypos   = -1;
63 char geometry[40];
64
65 } // namespace anon
66
67
68 FL_resource res[] =
69 {
70         {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
71 };
72
73
74 extern "C" {
75
76 static
77 int LyX_XErrHandler(Display * display, XErrorEvent * xeev)
78 {
79         if (xeev->error_code == BadWindow) {
80                 // We don't abort on BadWindow
81                 return 0;
82         }
83
84         // emergency cleanup
85         LyX::emergencyCleanup();
86
87         // Get the reason for the crash.
88         char etxt[513];
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)
92         lyx::abort();
93         return 0; // Solaris CC wants us to return something
94 }
95
96 }
97
98
99 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
100         : _owner(owner), lyxViews(0)
101 {
102         gui = GUI;
103         if (!gui)
104                 return;
105
106         //
107         setDefaults();
108
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.
113         locale_init();
114         fl_get_app_resources(res, num_res);
115
116         static const int geometryBitmask =
117                 XParseGeometry(geometry,
118                                 &xpos,
119                                 &ypos,
120                                 reinterpret_cast<unsigned int *>(&width),
121                                 reinterpret_cast<unsigned int *>(&height));
122
123         Display * display = fl_get_display();
124         if (!display) {
125                 lyxerr << "LyX: unable to access X display, exiting" << endl;
126                 os::warn("Unable to access X display, exiting");
127                 exit(1);
128         }
129         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
130         // X Error handler install goes here
131         XSetErrorHandler(LyX_XErrHandler);
132
133         // A width less than 590 pops up an awkward main window
134         // The minimal values of width/height (590/400) are defined in
135         // src/lyx.C
136         if (width < 590) width = 590;
137         if (height < 400) height = 400;
138
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;
144         }
145
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;
151         }
152
153         // Recalculate xpos if it's negative
154         if (geometryBitmask & 16)
155                 xpos += WidthOfScreen(ScreenOfDisplay(fl_get_display(),
156                                                       fl_screen)) - width;
157
158         // Recalculate ypos if it's negative
159         if (geometryBitmask & 32)
160                 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(),
161                                                        fl_screen)) - height;
162
163         // Initialize the LyXColorHandler
164         lyxColorHandler.reset(new LyXColorHandler);
165 }
166
167
168 // A destructor is always necessary  (asierra-970604)
169 LyXGUI::~LyXGUI()
170 {
171         // Lyxserver was created in this class so should be destroyed
172         // here.  asierra-970604
173         delete lyxserver;
174         lyxserver = 0;
175         delete lyxViews;
176 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
177         CloseLyXLookup();
178 #endif
179 }
180
181
182 void LyXGUI::setDefaults()
183 {
184         GUIRunTime::setDefaults();
185 }
186
187
188 // This is called after we have parsed lyxrc
189 void LyXGUI::init()
190 {
191         if (!gui)
192                 return;
193
194         create_forms();
195
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;
206
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());
209         if (bold < 0)
210                 lyxerr << "Could not set menu font to "
211                        << boldfontname << endl;
212
213         if (normal < 0)
214                 lyxerr << "Could not set popup font to "
215                        << fontname << endl;
216
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());
223
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");
227                         normal = bold = 0;
228                 }
229         }
230         if (bold < 0)
231                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
232         else if (normal < 0)
233                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
234
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
238         // (petr 120997).
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);
243 #if FL_REVISION < 89
244         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
245 #else
246         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
247 #endif
248
249         // all lyxrc settings has to be done here as lyxrc has not yet
250         // been read when the GUI is created (Jug)
251
252         // Update parameters.
253         lyxViews->redraw();
254
255         // Initialize the views.
256         lyxViews->init();
257
258         // this should be moved ...
259         lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
260 }
261
262
263 void LyXGUI::create_forms()
264 {
265         lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
266         lyxViews = GUIRunTime::createMainView(width, height);
267         lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
268
269         // From here down should be done by somebody else. (Lgb)
270
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);
275
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;
282         }
283
284         lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
285 }
286
287
288 void LyXGUI::runTime()
289 {
290         if (!gui) return;
291
292         GUIRunTime::runTime();
293 }
294
295
296 void LyXGUI::regBuf(Buffer * b)
297 {
298         lyxViews->view()->buffer(b);
299 }
300
301
302 LyXView * LyXGUI::getLyXView() const
303 {
304         return lyxViews;
305 }