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