]> git.lyx.org Git - lyx.git/blob - src/lyx_gui.C
*** empty log message ***
[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 #include <cstdlib>
13 #include <fcntl.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "lyx_gui.h"
20 #include FORMS_H_LOCATION
21 #include "support/filetools.h"
22 #include "support/os.h"
23 #include "support/lyxlib.h"
24 #include "figure_form.h"
25 #include "print_form.h"
26 #include "tex-strings.h"
27 #include "lyx_main.h"
28 #include "debug.h"
29 #include "version.h"
30 #include "LyXView.h"
31 #include "buffer.h"
32 #include "BufferView.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/xforms_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 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 save
84         if (!bufferlist.empty())
85                 bufferlist.emergencyWriteAll();
86
87         // Get the reason for the crash.
88         char etxt[513];
89         XGetErrorText(display, xeev->error_code, etxt, 512);
90         lyxerr << etxt << 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); //DefaultScreen(fl_get_display());
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); //DefaultScreen(fl_get_display());
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(), fl_screen)) - width; //DefaultScreen(fl_get_display())) - width;
156
157         // Recalculate ypos if it's negative
158         if (geometryBitmask & 32)
159                 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(), fl_screen)) - height; //DefaultScreen(fl_get_display())) - height;
160
161         // Initialize the LyXColorHandler
162         lyxColorHandler.reset(new LyXColorHandler);
163 }
164
165
166 // A destructor is always necessary  (asierra-970604)
167 LyXGUI::~LyXGUI()
168 {
169         // Lyxserver was created in this class so should be destroyed
170         // here.  asierra-970604
171         delete lyxserver;
172         lyxserver = 0;
173         delete lyxViews;
174 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
175         CloseLyXLookup();
176 #endif
177 }
178
179
180 void LyXGUI::setDefaults()
181 {
182         GUIRunTime::setDefaults();
183 }
184
185
186 // This is called after we have parsed lyxrc
187 void LyXGUI::init()
188 {
189         if (!gui)
190                 return;
191
192         create_forms();
193
194         if (lyxrc.font_norm_menu.empty())
195                 lyxrc.font_norm_menu = lyxrc.font_norm;
196         // Set the font name for popups and menus
197         string boldfontname = lyxrc.menu_font_name 
198                                + "-*-*-*-?-*-*-*-*-"  
199                                + lyxrc.font_norm_menu;
200                 // "?" means "scale that font"
201         string fontname = lyxrc.popup_font_name 
202                                + "-*-*-*-?-*-*-*-*-"  
203                                + lyxrc.font_norm_menu;
204
205         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
206         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
207         if (bold < 0)
208                 lyxerr << "Could not set menu font to "
209                        << boldfontname << endl;
210
211         if (normal < 0)
212                 lyxerr << "Could not set popup font to "
213                        << fontname << endl;
214
215         if (bold < 0 && normal < 0) {
216                 lyxerr << "Using 'helvetica' font for menus" << endl;
217                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
218                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
219                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
220                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
221
222                 if (bold < 0 && normal < 0) {
223                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
224                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
225                         normal = bold = 0;
226                 }
227         }
228         if (bold < 0)
229                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
230         else if (normal < 0)
231                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
232
233         // put here (after fl_initialize) to avoid segfault. Cannot be done
234         // in setDefaults() (Matthias 140496)
235         // Moved from ::LyXGUI to ::init to allow popup font customization 
236         // (petr 120997).
237         fl_setpup_fontstyle(FL_NORMAL_STYLE);
238         fl_setpup_fontsize(FL_NORMAL_SIZE);
239         fl_setpup_color(FL_MCOL, FL_BLACK);
240         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
241
242         // all lyxrc settings has to be done here as lyxrc has not yet
243         // been read when the GUI is created (Jug)
244
245         // the sendto form
246         if (!lyxrc.custom_export_command.empty())
247                 fl_set_input(fd_form_sendto->input_cmd,
248                              lyxrc.custom_export_command.c_str());
249         if (lyxrc.custom_export_format == "lyx")
250                 fl_set_button(fd_form_sendto->radio_ftype_lyx, 1);
251         else if (lyxrc.custom_export_format == "tex")
252                 fl_set_button(fd_form_sendto->radio_ftype_latex, 1);
253         else if (lyxrc.custom_export_format == "dvi")
254                 fl_set_button(fd_form_sendto->radio_ftype_dvi, 1);
255         else if (lyxrc.custom_export_format == "ps")
256                 fl_set_button(fd_form_sendto->radio_ftype_ps, 1);
257         else if (lyxrc.custom_export_format == "ascii")
258                 fl_set_button(fd_form_sendto->radio_ftype_ascii, 1);
259
260         // Update parameters.
261         lyxViews->redraw();
262
263         // Initialize the views.
264         lyxViews->init();
265
266         // in 0.12 the initialisation of the LyXServer must be done here
267         // 0.13 it should be moved again...
268         lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
269 }
270
271
272 void LyXGUI::create_forms()
273 {
274         lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
275         lyxViews = GUIRunTime::createMainView(width, height);
276         lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
277
278         // From here down should be done by somebody else. (Lgb)
279
280         //
281         // Create forms
282         //
283
284         // the sendto form
285         fd_form_sendto = create_form_form_sendto();
286         fl_set_form_atclose(fd_form_sendto->form_sendto, CancelCloseBoxCB, 0);
287
288         // the figure form
289         fd_form_figure = create_form_form_figure();
290         fl_set_form_atclose(fd_form_figure->form_figure,
291                             CancelCloseBoxCB, 0);
292         fl_set_button(fd_form_figure->radio_postscript, 1);
293
294         // This is probably as good a time as any to map the xform colours,
295         // should a mapping exist.
296         {
297                 string filename = AddName(user_lyxdir, "preferences.xform");
298                 XformsColor::read( filename );
299         }
300         
301         // Show the main & title form
302         int main_placement = FL_PLACE_CENTER | FL_FREE_SIZE;
303         // Did we get a valid position?
304         if (xpos >= 0 && ypos >= 0) {
305                 lyxViews->setPosition(xpos, ypos);
306                 main_placement = FL_PLACE_POSITION;
307         }
308
309         lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
310
311         if (lyxrc.show_banner)
312                 lyxViews->getDialogs()->showSplash();
313 }
314
315
316 void LyXGUI::runTime()
317 {
318         if (!gui) return;
319
320         GUIRunTime::runTime();
321 }
322
323
324 void LyXGUI::regBuf(Buffer * b)
325 {
326         lyxViews->view()->buffer(b);
327 }
328
329
330 LyXView * LyXGUI::getLyXView() const
331 {
332         return lyxViews;
333 }