]> git.lyx.org Git - lyx.git/blob - src/lyx_gui.C
ascii export of paragraphs, fix small compile problem.
[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-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "lyx_gui.h"
18 #include FORMS_H_LOCATION
19 #include "tex-strings.h"
20 #include "lyx_main.h"
21 #include "debug.h"
22 #include "version.h"
23 #include "LyXView.h"
24 #include "buffer.h"
25 #include "BufferView.h"
26 #include "lyxserver.h"
27 #include "lyxrc.h"
28 #include "gettext.h"
29 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
30 #include "lyxlookup.h"
31 #endif
32 #include "bufferlist.h"
33 #include "ColorHandler.h"
34
35 #include "frontends/GUIRunTime.h"
36 #include "frontends/xforms/xforms_helpers.h" // for XformColor
37
38 #include "support/filetools.h"
39 #include "support/os.h"
40 #include "support/lyxlib.h"
41
42 #include <cstdlib>
43 #include <fcntl.h>
44
45 using std::endl;
46
47 extern LyXServer * lyxserver;
48 extern bool finished;   // flag, that we are quitting the program
49 extern BufferList bufferlist;
50 extern string user_lyxdir;
51
52 FL_CMD_OPT cmdopt[] =
53 {
54         {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
55 };
56
57 namespace {
58
59 int width  = 690;
60 int height = 510;
61 int xpos   = -1;
62 int ypos   = -1;
63 char geometry[40];
64
65 } // namespace anon
66
67
68 FL_resource res[] =
69 {
70         {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
71 };
72
73
74 extern "C" {
75         
76 static
77 int LyX_XErrHandler(Display * display, XErrorEvent * xeev)
78 {
79         // emergency cleanup
80         LyX::emergencyCleanup();
81
82         // Get the reason for the crash.
83         char etxt[513];
84         XGetErrorText(display, xeev->error_code, etxt, 512);
85         lyxerr << etxt << " id: " << xeev->resourceid << endl;
86         // By doing an abort we get a nice backtrace. (hopefully)
87         lyx::abort();
88         return 0; // Solaris CC wants us to return something
89 }
90         
91 }
92
93
94 LyXGUI::LyXGUI(LyX * owner, int * argc, char * argv[], bool GUI)
95         : _owner(owner), lyxViews(0)
96 {
97         gui = GUI;
98         if (!gui)
99                 return;
100
101         // 
102         setDefaults();
103         
104         static const int num_res = sizeof(res)/sizeof(FL_resource);
105         fl_initialize(argc, argv, "LyX", cmdopt, num_res);
106         // It appears that, in xforms >=0.89.5, fl_initialize()
107         // calls setlocale() and ruins our LC_NUMERIC setting.
108         locale_init();
109         fl_get_app_resources(res, num_res);
110
111         static const int geometryBitmask =
112                 XParseGeometry(geometry,
113                                 &xpos,
114                                 &ypos,
115                                 reinterpret_cast<unsigned int *>(&width),
116                                 reinterpret_cast<unsigned int *>(&height));
117
118         Display * display = fl_get_display();
119         if (!display) {
120                 lyxerr << "LyX: unable to access X display, exiting" << endl;
121                 os::warn("Unable to access X display, exiting");
122                 exit(1);
123         }
124         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
125         // X Error handler install goes here
126         XSetErrorHandler(LyX_XErrHandler);
127
128         // A width less than 590 pops up an awkward main window
129         // The minimal values of width/height (590/400) are defined in
130         // src/lyx.C  
131         if (width < 590) width = 590;
132         if (height < 400) height = 400;
133         
134         // If width is not set by geometry, check it against monitor width
135         if (!(geometryBitmask & 4)) {
136                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
137                 if (WidthOfScreen(scr) - 8 < width)
138                         width = WidthOfScreen(scr) - 8;
139         }
140
141         // If height is not set by geometry, check it against monitor height
142         if (!(geometryBitmask & 8)) {
143                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
144                 if (HeightOfScreen(scr) - 24 < height)
145                         height = HeightOfScreen(scr) - 24;
146         }
147
148         // Recalculate xpos if it's negative
149         if (geometryBitmask & 16)
150                 xpos += WidthOfScreen(ScreenOfDisplay(fl_get_display(),
151                                                       fl_screen)) - width;
152
153         // Recalculate ypos if it's negative
154         if (geometryBitmask & 32)
155                 ypos += HeightOfScreen(ScreenOfDisplay(fl_get_display(),
156                                                        fl_screen)) - height;
157
158         // Initialize the LyXColorHandler
159         lyxColorHandler.reset(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.popup_font_encoding.empty())
192                 lyxrc.popup_font_encoding = lyxrc.font_norm;
193         // Set the font name for popups and menus
194         string boldfontname = lyxrc.popup_bold_font
195                                + "-*-*-*-?-*-*-*-*-"  
196                                + lyxrc.popup_font_encoding;
197                 // "?" means "scale that font"
198         string fontname = lyxrc.popup_normal_font 
199                                + "-*-*-*-?-*-*-*-*-"  
200                                + lyxrc.popup_font_encoding;
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 #if FL_REVISION < 89 
239         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
240 #else
241         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
242 #endif
243
244         // all lyxrc settings has to be done here as lyxrc has not yet
245         // been read when the GUI is created (Jug)
246
247         // Update parameters.
248         lyxViews->redraw();
249
250         // Initialize the views.
251         lyxViews->init();
252
253         // in 0.12 the initialisation of the LyXServer must be done here
254         // 0.13 it should be moved again...
255         lyxserver = new LyXServer(lyxViews->getLyXFunc(), lyxrc.lyxpipes);
256 }
257
258
259 void LyXGUI::create_forms()
260 {
261         lyxerr[Debug::INIT] << "Initializing LyXView..." << endl;
262         lyxViews = GUIRunTime::createMainView(width, height);
263         lyxerr[Debug::INIT] << "Initializing LyXView...done" << endl;
264
265         // From here down should be done by somebody else. (Lgb)
266
267         // This is probably as good a time as any to map the xform colours,
268         // should a mapping exist.
269         string const filename = AddName(user_lyxdir, "preferences.xform");
270         XformsColor::read(filename);
271         
272         // Show the main & title form
273         int main_placement = FL_PLACE_CENTER | FL_FREE_SIZE;
274         // Did we get a valid position?
275         if (xpos >= 0 && ypos >= 0) {
276                 lyxViews->setPosition(xpos, ypos);
277                 main_placement = FL_PLACE_POSITION;
278         }
279
280         lyxViews->show(main_placement, FL_FULLBORDER, "LyX");
281 }
282
283
284 void LyXGUI::runTime()
285 {
286         if (!gui) return;
287
288         GUIRunTime::runTime();
289 }
290
291
292 void LyXGUI::regBuf(Buffer * b)
293 {
294         lyxViews->view()->buffer(b);
295 }
296
297
298 LyXView * LyXGUI::getLyXView() const
299 {
300         return lyxViews;
301 }