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