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