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