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