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