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