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