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