]> git.lyx.org Git - lyx.git/blob - src/lyx_gui.C
8152476d8d6a8aaf3e3b4b1edec1ff476587e8db
[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 //#warning Please see if you can trigger this!
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 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
97         : _owner(owner), lyxViews(0)
98 {
99         gui = GUI;
100         if (!gui)
101                 return;
102
103         // 
104         setDefaults();
105         
106         static const int num_res = sizeof(res)/sizeof(FL_resource);
107         fl_initialize(argc, argv, "LyX", cmdopt, num_res);
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 = DefaultScreenOfDisplay(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 = DefaultScreenOfDisplay(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(DefaultScreenOfDisplay(fl_get_display())) - width;
149
150         // Recalculate ypos if it's negative
151         if (geometryBitmask & 32)
152                 ypos += HeightOfScreen(DefaultScreenOfDisplay(fl_get_display())) - height;
153
154         // Initialize the LyXColorHandler
155         lyxColorHandler = 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
168         CloseLyXLookup();
169 }
170
171
172 void LyXGUI::setDefaults()
173 {
174         GUIRunTime::setDefaults();
175 }
176
177
178 // This is called after we have parsed lyxrc
179 void LyXGUI::init()
180 {
181         if (!gui)
182                 return;
183
184         create_forms();
185
186         if (lyxrc.font_norm_menu.empty())
187                 lyxrc.font_norm_menu = lyxrc.font_norm;
188         // Set the font name for popups and menus
189         string menufontname = lyxrc.menu_font_name 
190                                + "-*-*-*-?-*-*-*-*-"  
191                                + lyxrc.font_norm_menu;
192                 // "?" means "scale that font"
193         string popupfontname = lyxrc.popup_font_name 
194                                + "-*-*-*-?-*-*-*-*-"  
195                                + lyxrc.font_norm_menu;
196
197         int bold = fl_set_font_name(FL_BOLD_STYLE, menufontname.c_str());
198         int normal = fl_set_font_name(FL_NORMAL_STYLE, popupfontname.c_str());
199         if (bold < 0)
200                 lyxerr << "Could not set menu font to "
201                        << menufontname << endl;
202
203         if (normal < 0)
204                 lyxerr << "Could not set popup font to "
205                        << popupfontname << endl;
206
207         if (bold < 0 && normal < 0) {
208                 lyxerr << "Using 'helvetica' font for menus" << endl;
209                 bold = fl_set_font_name(FL_BOLD_STYLE,
210                                         "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1");
211                 normal = fl_set_font_name(FL_NORMAL_STYLE,
212                                           "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1");
213                 if (bold < 0 && normal < 0) {
214                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
215                         normal = fl_set_font_name(FL_NORMAL_STYLE, "fixed");
216                 }
217         }
218
219         // put here (after fl_initialize) to avoid segfault. Cannot be done
220         // in setDefaults() (Matthias 140496)
221         // Moved from ::LyXGUI to ::init to allow popup font customization 
222         // (petr 120997).
223         fl_setpup_fontstyle(FL_NORMAL_STYLE);
224         fl_setpup_fontsize(FL_NORMAL_SIZE);
225         fl_setpup_color(FL_MCOL, FL_BLACK);
226         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
227
228         // all lyxrc settings has to be done here as lyxrc has not yet
229         // been read when the GUI is created (Jug)
230
231         // the sendto form
232         if (!lyxrc.custom_export_command.empty())
233                 fl_set_input(fd_form_sendto->input_cmd,
234                              lyxrc.custom_export_command.c_str());
235         if (lyxrc.custom_export_format == "lyx")
236                 fl_set_button(fd_form_sendto->radio_ftype_lyx, 1);
237         else if (lyxrc.custom_export_format == "tex")
238                 fl_set_button(fd_form_sendto->radio_ftype_latex, 1);
239         else if (lyxrc.custom_export_format == "dvi")
240                 fl_set_button(fd_form_sendto->radio_ftype_dvi, 1);
241         else if (lyxrc.custom_export_format == "ps")
242                 fl_set_button(fd_form_sendto->radio_ftype_ps, 1);
243         else if (lyxrc.custom_export_format == "ascii")
244                 fl_set_button(fd_form_sendto->radio_ftype_ascii, 1);
245
246         // Update parameters.
247         lyxViews->redraw();
248
249         // Initialize the views.
250         lyxViews->init();
251
252         // in 0.12 the initialisation of the LyXServer must be done here
253         // 0.13 it should be moved again...
254         lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
255 }
256
257
258 void LyXGUI::create_forms()
259 {
260         lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
261         lyxViews = new LyXView(width, height);
262         lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
263
264         // From here down should be done by somebody else. (Lgb)
265
266         //
267         // Create forms
268         //
269
270         // the title form
271         string banner_file = LibFileSearch("images", "banner", "xpm");
272         if (lyxrc.show_banner && !banner_file.empty()) {
273                 fd_form_title = create_form_form_title();
274                 fl_set_form_dblbuffer(fd_form_title->form_title, 1); // use dbl buffer
275                 fl_set_form_atclose(fd_form_title->form_title, CancelCloseBoxCB, 0);
276                 fl_addto_form(fd_form_title->form_title);
277                 FL_OBJECT *obj = fl_add_pixmapbutton(FL_NORMAL_BUTTON, 0, 0, 425, 290, "");
278                 fl_set_pixmapbutton_file(obj, banner_file.c_str());
279                 
280                 fl_set_pixmapbutton_focus_outline(obj, 3);
281                 fl_set_button_shortcut(obj, "^M ^[", 1);
282                 fl_set_object_boxtype(obj, FL_NO_BOX);
283                 fl_set_object_callback(obj, TimerCB, 0);
284                 
285                 obj = fl_add_text(FL_NORMAL_TEXT, 248, 265, 170, 16, LYX_VERSION);
286                 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
287                 fl_mapcolor(FL_FREE_COL2, 0x05, 0x2e, 0x4c);
288                 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
289                 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
290                 fl_set_object_lcol(obj, FL_FREE_COL3);
291                 fl_set_object_lalign(obj, FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
292                 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
293                 fl_end_form();
294         }
295
296         // the character form
297         fd_form_character = create_form_form_character();
298         fl_set_form_atclose(fd_form_character->form_character,
299                             CancelCloseBoxCB, 0);
300         fl_addto_choice(fd_form_character->choice_family, 
301                         _(" No change %l| Roman | Sans Serif | Typewriter %l| Reset "));
302         fl_addto_choice(fd_form_character->choice_series, 
303                         _(" No change %l| Medium | Bold %l| Reset "));
304         fl_addto_choice(fd_form_character->choice_shape,
305                         _(" No change %l| Upright | Italic | Slanted | Small Caps "
306                         "%l| Reset "));
307         fl_addto_choice(fd_form_character->choice_size, 
308                         _(" No change %l| Tiny | Smallest | Smaller | Small "
309                         "| Normal | Large | Larger | Largest | Huge | Huger "
310                         "%l| Increase | Decrease | Reset "));
311         fl_addto_choice(fd_form_character->choice_bar, 
312                         _(" No change %l| Emph | Underbar | Noun | LaTeX mode %l| Reset "));
313         fl_addto_choice(fd_form_character->choice_color, 
314                         _(" No change %l| No color | Black | White | Red | Green "
315                         "| Blue | Cyan | Magenta | Yellow %l| Reset "));
316         fl_set_form_minsize(fd_form_character->form_character,
317                             fd_form_character->form_character->w,
318                             fd_form_character->form_character->h);
319         lyxerr[Debug::INIT] << "Initializing form_character::combox..." << endl;
320         fl_addto_form(fd_form_character->form_character);
321         combo_language2 = new Combox(FL_COMBOX_DROPLIST);
322         FL_OBJECT * ob = fd_form_character->choice_language;
323         combo_language2->add(ob->x, ob->y, ob->w, ob->h, 250);
324         combo_language2->shortcut("#L", 1);
325         fl_end_form();
326         lyxerr[Debug::INIT] << "Initializing form_character...done" << endl;
327
328         // build up the combox entries
329         combo_language2->addto(_("No change"));
330         combo_language2->addto(_("Reset"));
331         for(Languages::const_iterator cit = languages.begin();
332             cit != languages.end(); ++cit) {
333 #ifdef DO_USE_DEFAULT_LANGUAGE
334             if ((*cit).second.lang() != "default")
335 #endif
336                 combo_language2->addto((*cit).second.lang().c_str());
337         }
338         combo_language2->select_text(_("No change"));
339
340         // the preamble form
341         fd_form_preamble = create_form_form_preamble();
342         fl_set_form_atclose(fd_form_preamble->form_preamble,
343                             CancelCloseBoxCB, 0);
344
345         // the sendto form
346         fd_form_sendto = create_form_form_sendto();
347         fl_set_form_atclose(fd_form_sendto->form_sendto, CancelCloseBoxCB, 0);
348
349         // the figure form
350         fd_form_figure = create_form_form_figure();
351         fl_set_form_atclose(fd_form_figure->form_figure,
352                             CancelCloseBoxCB, 0);
353         fl_set_button(fd_form_figure->radio_postscript, 1);
354
355         // the latex log form
356         fd_latex_log = create_form_LaTeXLog();
357         fl_set_form_atclose(fd_latex_log->LaTeXLog,
358                             CancelCloseBoxCB, 0);
359
360         // Show the main & title form
361         int main_placement = FL_PLACE_CENTER | FL_FREE_SIZE;
362         int title_placement = FL_PLACE_CENTER;
363         // Did we get a valid position?
364         if (xpos >= 0 && ypos >= 0) {
365                 lyxViews->setPosition(xpos, ypos);
366                 if (lyxrc.show_banner) {
367                         // show the title form in the middle of the main form
368                         fl_set_form_position(fd_form_title->form_title,
369                                              abs(xpos + (width/2) - (370 / 2)),
370                                              abs(ypos + (height/2) - (290 / 2)));
371                         title_placement = FL_PLACE_GEOMETRY;
372                         // The use of abs() above is a trick to ensure
373                         // valid positions
374                 }
375                         main_placement = FL_PLACE_POSITION;
376         }
377         lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
378         if (lyxrc.show_banner) {
379                 fl_show_form(fd_form_title->form_title, 
380                              title_placement, FL_NOBORDER, 
381                              _("LyX Banner"));
382                 fl_redraw_form(fd_form_title->form_title);
383                 fl_raise_form(fd_form_title->form_title);
384
385                 // Show the title form at most 7 secs (lowered from 10 secs)
386                 fl_set_timer(fd_form_title->timer_title, 7);
387         }
388 }
389
390
391 void LyXGUI::runTime()
392 {
393         if (!gui) return;
394
395         guiruntime.runTime();
396 }
397
398
399 void LyXGUI::regBuf(Buffer * b)
400 {
401         lyxViews->view()->buffer(b);
402 }
403
404
405 LyXView * LyXGUI::getLyXView() const
406 {
407         return lyxViews;
408 }