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