]> git.lyx.org Git - lyx.git/blob - src/lyx_gui.C
Herbert's upgrade to the recognized graphics formats.
[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 "print_form.h"
20 #include "tex-strings.h"
21 #include "lyx_main.h"
22 #include "debug.h"
23 #include "version.h"
24 #include "LyXView.h"
25 #include "buffer.h"
26 #include "BufferView.h"
27 #include "lyxserver.h"
28 #include "lyxrc.h"
29 #include "gettext.h"
30 #include "lyx_gui_misc.h"
31 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
32 #include "lyxlookup.h"
33 #endif
34 #include "bufferlist.h"
35 #include "ColorHandler.h"
36
37 #include "frontends/GUIRunTime.h"
38 #include "frontends/xforms/xforms_helpers.h" // for XformColor
39
40 #include "support/filetools.h"
41 #include "support/os.h"
42 #include "support/lyxlib.h"
43
44 #include <cstdlib>
45 #include <fcntl.h>
46
47 using std::endl;
48
49 FD_form_sendto * fd_form_sendto;
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         // emergency cleanup
84         LyX::emergencyCleanup();
85
86         // Get the reason for the crash.
87         char etxt[513];
88         XGetErrorText(display, xeev->error_code, etxt, 512);
89         lyxerr << etxt << " id: " << xeev->resourceid << endl;
90         // By doing an abort we get a nice backtrace. (hopefully)
91         lyx::abort();
92         return 0; // Solaris CC wants us to return something
93 }
94         
95 }
96
97
98 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
99         : _owner(owner), lyxViews(0)
100 {
101         gui = GUI;
102         if (!gui)
103                 return;
104
105         // 
106         setDefaults();
107         
108         static const int num_res = sizeof(res)/sizeof(FL_resource);
109         fl_initialize(argc, argv, "LyX", cmdopt, num_res);
110         // It appears that, in xforms >=0.89.5, fl_initialize()
111         // calls setlocale() and ruins our LC_NUMERIC setting.
112         locale_init();
113         fl_get_app_resources(res, num_res);
114
115         static const int geometryBitmask =
116                 XParseGeometry(geometry,
117                                 &xpos,
118                                 &ypos,
119                                 reinterpret_cast<unsigned int *>(&width),
120                                 reinterpret_cast<unsigned int *>(&height));
121
122         Display * display = fl_get_display();
123         if (!display) {
124                 lyxerr << "LyX: unable to access X display, exiting" << endl;
125                 os::warn("Unable to access X display, exiting");
126                 exit(1);
127         }
128         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
129         // X Error handler install goes here
130         XSetErrorHandler(LyX_XErrHandler);
131
132         // A width less than 590 pops up an awkward main window
133         // The minimal values of width/height (590/400) are defined in
134         // src/lyx.C  
135         if (width < 590) width = 590;
136         if (height < 400) height = 400;
137         
138         // If width is not set by geometry, check it against monitor width
139         if (!(geometryBitmask & 4)) {
140                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
141                 if (WidthOfScreen(scr) - 8 < width)
142                         width = WidthOfScreen(scr) - 8;
143         }
144
145         // If height is not set by geometry, check it against monitor height
146         if (!(geometryBitmask & 8)) {
147                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
148                 if (HeightOfScreen(scr) - 24 < height)
149                         height = HeightOfScreen(scr) - 24;
150         }
151
152         // Recalculate xpos if it's negative
153         if (geometryBitmask & 16)
154                 xpos += WidthOfScreen(ScreenOfDisplay(fl_get_display(),
155                                                       fl_screen)) - width;
156
157         // Recalculate ypos if it's negative
158         if (geometryBitmask & 32)
159                 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(),
160                                                        fl_screen)) - height;
161
162         // Initialize the LyXColorHandler
163         lyxColorHandler.reset(new LyXColorHandler);
164 }
165
166
167 // A destructor is always necessary  (asierra-970604)
168 LyXGUI::~LyXGUI()
169 {
170         // Lyxserver was created in this class so should be destroyed
171         // here.  asierra-970604
172         delete lyxserver;
173         lyxserver = 0;
174         delete lyxViews;
175 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
176         CloseLyXLookup();
177 #endif
178 }
179
180
181 void LyXGUI::setDefaults()
182 {
183         GUIRunTime::setDefaults();
184 }
185
186
187 // This is called after we have parsed lyxrc
188 void LyXGUI::init()
189 {
190         if (!gui)
191                 return;
192
193         create_forms();
194
195         if (lyxrc.popup_font_encoding.empty())
196                 lyxrc.popup_font_encoding = lyxrc.font_norm;
197         // Set the font name for popups and menus
198         string boldfontname = lyxrc.popup_bold_font
199                                + "-*-*-*-?-*-*-*-*-"  
200                                + lyxrc.popup_font_encoding;
201                 // "?" means "scale that font"
202         string fontname = lyxrc.popup_normal_font 
203                                + "-*-*-*-?-*-*-*-*-"  
204                                + lyxrc.popup_font_encoding;
205
206         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
207         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
208         if (bold < 0)
209                 lyxerr << "Could not set menu font to "
210                        << boldfontname << endl;
211
212         if (normal < 0)
213                 lyxerr << "Could not set popup font to "
214                        << fontname << endl;
215
216         if (bold < 0 && normal < 0) {
217                 lyxerr << "Using 'helvetica' font for menus" << endl;
218                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
219                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
220                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
221                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
222
223                 if (bold < 0 && normal < 0) {
224                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
225                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
226                         normal = bold = 0;
227                 }
228         }
229         if (bold < 0)
230                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
231         else if (normal < 0)
232                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
233
234         // put here (after fl_initialize) to avoid segfault. Cannot be done
235         // in setDefaults() (Matthias 140496)
236         // Moved from ::LyXGUI to ::init to allow popup font customization 
237         // (petr 120997).
238         fl_setpup_fontstyle(FL_NORMAL_STYLE);
239         fl_setpup_fontsize(FL_NORMAL_SIZE);
240         fl_setpup_color(FL_MCOL, FL_BLACK);
241         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
242 #if FL_REVISION < 89 
243         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
244 #else
245         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
246 #endif
247
248         // all lyxrc settings has to be done here as lyxrc has not yet
249         // been read when the GUI is created (Jug)
250
251         // the sendto form
252         if (!lyxrc.custom_export_command.empty())
253                 fl_set_input(fd_form_sendto->input_cmd,
254                              lyxrc.custom_export_command.c_str());
255         if (lyxrc.custom_export_format == "lyx")
256                 fl_set_button(fd_form_sendto->radio_ftype_lyx, 1);
257         else if (lyxrc.custom_export_format == "tex")
258                 fl_set_button(fd_form_sendto->radio_ftype_latex, 1);
259         else if (lyxrc.custom_export_format == "dvi")
260                 fl_set_button(fd_form_sendto->radio_ftype_dvi, 1);
261         else if (lyxrc.custom_export_format == "ps")
262                 fl_set_button(fd_form_sendto->radio_ftype_ps, 1);
263         else if (lyxrc.custom_export_format == "ascii")
264                 fl_set_button(fd_form_sendto->radio_ftype_ascii, 1);
265
266         // Update parameters.
267         lyxViews->redraw();
268
269         // Initialize the views.
270         lyxViews->init();
271
272         // in 0.12 the initialisation of the LyXServer must be done here
273         // 0.13 it should be moved again...
274         lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
275 }
276
277
278 void LyXGUI::create_forms()
279 {
280         lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
281         lyxViews = GUIRunTime::createMainView(width, height);
282         lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
283
284         // From here down should be done by somebody else. (Lgb)
285
286         //
287         // Create forms
288         //
289
290         // the sendto form
291         fd_form_sendto = create_form_form_sendto();
292         fl_set_form_atclose(fd_form_sendto->form_sendto, CancelCloseBoxCB, 0);
293
294         // This is probably as good a time as any to map the xform colours,
295         // should a mapping exist.
296         string const filename = AddName(user_lyxdir, "preferences.xform");
297         XformsColor::read(filename);
298         
299         // Show the main & title form
300         int main_placement = FL_PLACE_CENTER | FL_FREE_SIZE;
301         // Did we get a valid position?
302         if (xpos >= 0 && ypos >= 0) {
303                 lyxViews->setPosition(xpos, ypos);
304                 main_placement = FL_PLACE_POSITION;
305         }
306
307         lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
308 }
309
310
311 void LyXGUI::runTime()
312 {
313         if (!gui) return;
314
315         GUIRunTime::runTime();
316 }
317
318
319 void LyXGUI::regBuf(Buffer * b)
320 {
321         lyxViews->view()->buffer(b);
322 }
323
324
325 LyXView * LyXGUI::getLyXView() const
326 {
327         return lyxViews;
328 }