]> git.lyx.org Git - lyx.git/blob - src/lyx_gui.C
5e1635b159a915f9799eff69693ef5b69b6883cb
[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 "layout_forms.h"
26 #include "print_form.h"
27 #include "tex-strings.h"
28 #include "lyx_main.h"
29 #include "log_form.h"
30 #include "debug.h"
31 #include "version.h"
32 #include "LyXView.h"
33 #include "buffer.h"
34 #include "lyxserver.h"
35 #include "lyxrc.h"
36 #include "gettext.h"
37 #include "lyx_gui_misc.h"
38 #include "lyxlookup.h"
39 #include "bufferlist.h"
40 #include "language.h"
41 #include "ColorHandler.h"
42 #include "frontends/GUIRunTime.h"
43
44 using std::endl;
45
46 FD_form_title * fd_form_title;
47 FD_form_character * fd_form_character;
48 FD_form_preamble * fd_form_preamble;
49 FD_form_sendto * fd_form_sendto;
50 FD_form_figure * fd_form_figure;
51 FD_LaTeXLog * fd_latex_log; // from log_form.h
52 Combox * combo_language;
53 Combox * combo_language2;
54
55 extern LyXServer * lyxserver;
56 extern bool finished;   // flag, that we are quitting the program
57 extern BufferList bufferlist;
58 extern GUIRunTime guiruntime;
59
60 FL_CMD_OPT cmdopt[] =
61 {
62         {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
63 };
64
65 static int width  = 690;
66 static int height = 510;
67 static int xpos   = -1;
68 static int ypos   = -1;
69 static char geometry[40];
70
71
72 FL_resource res[] =
73 {
74         {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
75 };
76
77
78 extern "C"
79 int LyX_XErrHandler(Display * display, XErrorEvent * xeev)
80 {
81         // emergency save
82         if (!bufferlist.empty())
83                 bufferlist.emergencyWriteAll();
84
85         // Get the reason for the crash.
86         char etxt[513];
87         XGetErrorText(display, xeev->error_code, etxt, 512);
88         lyxerr << etxt << endl;
89         // By doing an abort we get a nice backtrace. (hopefully)
90         lyx::abort();
91         return 0; // Solaris CC wants us to return something
92 }
93
94
95 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
96         : _owner(owner), lyxViews(0)
97 {
98         gui = GUI;
99         if (!gui)
100                 return;
101
102         // 
103         setDefaults();
104         
105         static const int num_res = sizeof(res)/sizeof(FL_resource);
106         fl_initialize(argc, argv, "LyX", cmdopt, num_res);
107         fl_get_app_resources(res, num_res);
108
109         static const int geometryBitmask =
110                 XParseGeometry( geometry,
111                                 &xpos,
112                                 &ypos,
113                                 reinterpret_cast<unsigned int *>(&width),
114                                 reinterpret_cast<unsigned int *>(&height));
115
116         Display * display = fl_get_display();
117         if (!display) {
118                 lyxerr << "LyX: unable to access X display, exiting" << endl;
119                 exit(1);
120         }
121         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
122         // X Error handler install goes here
123         XSetErrorHandler(LyX_XErrHandler);
124
125         // A width less than 590 pops up an awkward main window
126         // The minimal values of width/height (590/400) are defined in
127         // src/lyx.C  
128         if (width < 590) width = 590;
129         if (height < 400) height = 400;
130         
131         // If width is not set by geometry, check it against monitor width
132         if ( !(geometryBitmask & 4) ) {
133                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen); //DefaultScreen(fl_get_display());
134                 if (WidthOfScreen(scr) - 8 < width)
135                         width = WidthOfScreen(scr) - 8;
136         }
137
138         // If height is not set by geometry, check it against monitor height
139         if ( !(geometryBitmask & 8) ) {
140                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen); //DefaultScreen(fl_get_display());
141                 if (HeightOfScreen(scr) - 24 < height)
142                         height = HeightOfScreen(scr) - 24;
143         }
144
145         // Recalculate xpos if it's negative
146         if (geometryBitmask & 16)
147                 xpos += WidthOfScreen(ScreenOfDisplay(fl_get_display(), fl_screen)) - width; //DefaultScreen(fl_get_display())) - width;
148
149         // Recalculate ypos if it's negative
150         if (geometryBitmask & 32)
151                 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(), fl_screen)) - height; //DefaultScreen(fl_get_display())) - height;
152
153         // Initialize the LyXColorHandler
154         lyxColorHandler = new LyXColorHandler;
155 }
156
157
158 // A destructor is always necessary  (asierra-970604)
159 LyXGUI::~LyXGUI()
160 {
161         // Lyxserver was created in this class so should be destroyed
162         // here.  asierra-970604
163         delete lyxserver;
164         lyxserver = 0;
165         delete lyxViews;
166
167         CloseLyXLookup();
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 menufontname = lyxrc.menu_font_name 
189                                + "-*-*-*-?-*-*-*-*-"  
190                                + lyxrc.font_norm_menu;
191                 // "?" means "scale that font"
192         string popupfontname = lyxrc.popup_font_name 
193                                + "-*-*-*-?-*-*-*-*-"  
194                                + lyxrc.font_norm_menu;
195
196         int bold = fl_set_font_name(FL_BOLD_STYLE, menufontname.c_str());
197         int normal = fl_set_font_name(FL_NORMAL_STYLE, popupfontname.c_str());
198         if (bold < 0)
199                 lyxerr << "Could not set menu font to "
200                        << menufontname << endl;
201
202         if (normal < 0)
203                 lyxerr << "Could not set popup font to "
204                        << popupfontname << endl;
205
206         if (bold < 0 && normal < 0) {
207                 lyxerr << "Using 'helvetica' font for menus" << endl;
208                 bold = fl_set_font_name(FL_BOLD_STYLE,
209                                         "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1");
210                 normal = fl_set_font_name(FL_NORMAL_STYLE,
211                                           "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1");
212                 if (bold < 0 && normal < 0) {
213                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
214                         normal = fl_set_font_name(FL_NORMAL_STYLE, "fixed");
215                 }
216         }
217
218         // put here (after fl_initialize) to avoid segfault. Cannot be done
219         // in setDefaults() (Matthias 140496)
220         // Moved from ::LyXGUI to ::init to allow popup font customization 
221         // (petr 120997).
222         fl_setpup_fontstyle(FL_NORMAL_STYLE);
223         fl_setpup_fontsize(FL_NORMAL_SIZE);
224         fl_setpup_color(FL_MCOL, FL_BLACK);
225         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
226
227         // all lyxrc settings has to be done here as lyxrc has not yet
228         // been read when the GUI is created (Jug)
229
230         // the sendto form
231         if (!lyxrc.custom_export_command.empty())
232                 fl_set_input(fd_form_sendto->input_cmd,
233                              lyxrc.custom_export_command.c_str());
234         if (lyxrc.custom_export_format == "lyx")
235                 fl_set_button(fd_form_sendto->radio_ftype_lyx, 1);
236         else if (lyxrc.custom_export_format == "tex")
237                 fl_set_button(fd_form_sendto->radio_ftype_latex, 1);
238         else if (lyxrc.custom_export_format == "dvi")
239                 fl_set_button(fd_form_sendto->radio_ftype_dvi, 1);
240         else if (lyxrc.custom_export_format == "ps")
241                 fl_set_button(fd_form_sendto->radio_ftype_ps, 1);
242         else if (lyxrc.custom_export_format == "ascii")
243                 fl_set_button(fd_form_sendto->radio_ftype_ascii, 1);
244
245         // Update parameters.
246         lyxViews->redraw();
247
248         // Initialize the views.
249         lyxViews->init();
250
251         // in 0.12 the initialisation of the LyXServer must be done here
252         // 0.13 it should be moved again...
253         lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
254 }
255
256
257 void LyXGUI::create_forms()
258 {
259         lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
260         lyxViews = new LyXView(width, height);
261         lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
262
263         // From here down should be done by somebody else. (Lgb)
264
265         //
266         // Create forms
267         //
268
269         // the title form
270         string banner_file = LibFileSearch("images", "banner", "xpm");
271         if (lyxrc.show_banner && !banner_file.empty()) {
272                 fd_form_title = create_form_form_title();
273                 fl_set_form_dblbuffer(fd_form_title->form_title, 1); // use dbl buffer
274                 fl_set_form_atclose(fd_form_title->form_title, CancelCloseBoxCB, 0);
275                 fl_addto_form(fd_form_title->form_title);
276                 FL_OBJECT *obj = fl_add_pixmapbutton(FL_NORMAL_BUTTON, 0, 0, 425, 290, "");
277                 fl_set_pixmapbutton_file(obj, banner_file.c_str());
278                 
279                 fl_set_pixmapbutton_focus_outline(obj, 3);
280                 fl_set_button_shortcut(obj, "^M ^[", 1);
281                 fl_set_object_boxtype(obj, FL_NO_BOX);
282                 fl_set_object_callback(obj, TimerCB, 0);
283                 
284                 obj = fl_add_text(FL_NORMAL_TEXT, 248, 265, 170, 16, LYX_VERSION);
285                 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
286                 fl_mapcolor(FL_FREE_COL2, 0x05, 0x2e, 0x4c);
287                 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
288                 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
289                 fl_set_object_lcol(obj, FL_FREE_COL3);
290                 fl_set_object_lalign(obj, FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
291                 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
292                 fl_end_form();
293         }
294
295         // the character form
296         fd_form_character = create_form_form_character();
297         fl_set_form_atclose(fd_form_character->form_character,
298                             CancelCloseBoxCB, 0);
299         fl_addto_choice(fd_form_character->choice_family, 
300                         _(" No change %l| Roman | Sans Serif | Typewriter %l| Reset "));
301         fl_addto_choice(fd_form_character->choice_series, 
302                         _(" No change %l| Medium | Bold %l| Reset "));
303         fl_addto_choice(fd_form_character->choice_shape,
304                         _(" No change %l| Upright | Italic | Slanted | Small Caps "
305                         "%l| Reset "));
306         fl_addto_choice(fd_form_character->choice_size, 
307                         _(" No change %l| Tiny | Smallest | Smaller | Small "
308                         "| Normal | Large | Larger | Largest | Huge | Huger "
309                         "%l| Increase | Decrease | Reset "));
310         fl_addto_choice(fd_form_character->choice_bar, 
311                         _(" No change %l| Emph | Underbar | Noun | LaTeX mode %l| Reset "));
312         fl_addto_choice(fd_form_character->choice_color, 
313                         _(" No change %l| No color | Black | White | Red | Green "
314                         "| Blue | Cyan | Magenta | Yellow %l| Reset "));
315         fl_set_form_minsize(fd_form_character->form_character,
316                             fd_form_character->form_character->w,
317                             fd_form_character->form_character->h);
318         lyxerr[Debug::INIT] << "Initializing form_character::combox..." << endl;
319         fl_addto_form(fd_form_character->form_character);
320         combo_language2 = new Combox(FL_COMBOX_DROPLIST);
321         FL_OBJECT * ob = fd_form_character->choice_language;
322         combo_language2->add(ob->x, ob->y, ob->w, ob->h, 250);
323         combo_language2->shortcut("#L", 1);
324         fl_end_form();
325         lyxerr[Debug::INIT] << "Initializing form_character...done" << endl;
326
327         // build up the combox entries
328         combo_language2->addto(_("No change"));
329         combo_language2->addto(_("Reset"));
330         for(Languages::const_iterator cit = languages.begin();
331             cit != languages.end(); ++cit) {
332 #ifdef DO_USE_DEFAULT_LANGUAGE
333             if ((*cit).second.lang() != "default")
334 #endif
335                 combo_language2->addto((*cit).second.lang());
336         }
337         combo_language2->select_text(_("No change"));
338
339         // the preamble form
340         fd_form_preamble = create_form_form_preamble();
341         fl_set_form_atclose(fd_form_preamble->form_preamble,
342                             CancelCloseBoxCB, 0);
343
344         // the sendto form
345         fd_form_sendto = create_form_form_sendto();
346         fl_set_form_atclose(fd_form_sendto->form_sendto, CancelCloseBoxCB, 0);
347
348         // the figure form
349         fd_form_figure = create_form_form_figure();
350         fl_set_form_atclose(fd_form_figure->form_figure,
351                             CancelCloseBoxCB, 0);
352         fl_set_button(fd_form_figure->radio_postscript, 1);
353
354         // the latex log form
355         fd_latex_log = create_form_LaTeXLog();
356         fl_set_form_atclose(fd_latex_log->LaTeXLog,
357                             CancelCloseBoxCB, 0);
358
359         // Show the main & title form
360         int main_placement = FL_PLACE_CENTER | FL_FREE_SIZE;
361         int title_placement = FL_PLACE_CENTER;
362         // Did we get a valid position?
363         if (xpos >= 0 && ypos >= 0) {
364                 lyxViews->setPosition(xpos, ypos);
365                 if (lyxrc.show_banner) {
366                         // show the title form in the middle of the main form
367                         fl_set_form_position(fd_form_title->form_title,
368                                              abs(xpos + (width/2) - (370 / 2)),
369                                              abs(ypos + (height/2) - (290 / 2)));
370                         title_placement = FL_PLACE_GEOMETRY;
371                         // The use of abs() above is a trick to ensure
372                         // valid positions
373                 }
374                         main_placement = FL_PLACE_POSITION;
375         }
376         lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
377         if (lyxrc.show_banner) {
378                 fl_show_form(fd_form_title->form_title, 
379                              title_placement, FL_NOBORDER, 
380                              _("LyX Banner"));
381                 fl_redraw_form(fd_form_title->form_title);
382                 fl_raise_form(fd_form_title->form_title);
383
384                 // Show the title form at most 7 secs (lowered from 10 secs)
385                 fl_set_timer(fd_form_title->timer_title, 7);
386         }
387 }
388
389
390 void LyXGUI::runTime()
391 {
392         if (!gui) return;
393
394         guiruntime.runTime();
395 }
396
397
398 void LyXGUI::regBuf(Buffer * b)
399 {
400         lyxViews->view()->buffer(b);
401 }
402
403
404 LyXView * LyXGUI::getLyXView() const
405 {
406         return lyxViews;
407 }