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