]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
remove a ColorHandler reference
[lyx.git] / src / frontends / xforms / lyx_gui.C
1 /**
2  * \file lyx_gui.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #include "lyx_gui.h"
13
14 #include "support/lyxlib.h"
15 #include "support/os.h"
16 #include "support/filetools.h"
17
18 #include "debug.h"
19 #include "gettext.h"
20
21 #include "lyx_main.h"
22 #include "lyxrc.h"
23
24 // FIXME: move this stuff out again
25 #include "bufferlist.h"
26 #include "lyxfunc.h"
27 #include "lyxserver.h"
28 #include "BufferView.h"
29 #include "XFormsView.h"
30
31 #include FORMS_H_LOCATION
32 #include "ColorHandler.h"
33 #include "xforms_helpers.h"
34 #ifdef USE_XFORMS_IMAGE_LOADER
35 #include "xformsImage.h"
36 #else
37 #include "graphics/GraphicsImageXPM.h"
38 #endif
39
40 #include "Lsstream.h"
41 #include <iomanip>
42 #include <fcntl.h>
43 #include <boost/bind.hpp>
44
45 #ifndef CXX_GLOBAL_CSTD
46 using std::exit;
47 #endif
48
49 using std::vector;
50 using std::hex;
51 using std::endl;
52 using std::setbase;
53 using std::setfill;
54 using std::setw;
55
56 extern bool finished;
57 extern BufferList bufferlist;
58
59 // FIXME: wrong place !
60 LyXServer * lyxserver;
61
62 namespace {
63
64 /// set default GUI configuration
65 void setDefaults()
66 {
67         FL_IOPT cntl;
68         cntl.buttonFontSize = FL_NORMAL_SIZE;
69         cntl.browserFontSize = FL_NORMAL_SIZE;
70         cntl.labelFontSize = FL_NORMAL_SIZE;
71         cntl.choiceFontSize = FL_NORMAL_SIZE;
72         cntl.inputFontSize = FL_NORMAL_SIZE;
73         cntl.menuFontSize  = FL_NORMAL_SIZE;
74         cntl.borderWidth = -1;
75         cntl.vclass = FL_DefaultVisual;
76         fl_set_defaults(FL_PDVisual
77                         | FL_PDButtonFontSize
78                         | FL_PDBrowserFontSize
79                         | FL_PDLabelFontSize
80                         | FL_PDChoiceFontSize
81                         | FL_PDInputFontSize
82                         | FL_PDMenuFontSize
83                         | FL_PDBorderWidth, &cntl);
84 }
85
86
87 extern "C" {
88
89 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
90         // We don't abort on BadWindow
91         if (xeev->error_code == BadWindow) {
92                 lyxerr << "BadWindow received !" << endl;
93                 lyxerr << "If you're using xforms 1.0 or greater, "
94                         << " please report this to lyx-devel@lists.lyx.org" << endl;
95                 return 0;
96         }
97
98         // emergency cleanup
99         LyX::emergencyCleanup();
100
101         // Get the reason for the crash.
102         char etxt[513];
103         XGetErrorText(display, xeev->error_code, etxt, 512);
104         lyxerr << etxt << " id: " << xeev->resourceid << endl;
105         // By doing an abort we get a nice backtrace. (hopefully)
106         lyx::abort();
107         return 0;
108 }
109
110 }
111
112 /// read in geometry specification
113 char geometry[40];
114
115 } // namespace anon
116
117
118 void lyx_gui::parse_init(int & argc, char * argv[])
119 {
120         setDefaults();
121
122         FL_CMD_OPT cmdopt[] = {
123                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
124         };
125
126         FL_resource res[] = {
127                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
128         };
129
130         const int num_res = sizeof(res)/sizeof(FL_resource);
131
132         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
133
134         // It appears that, in xforms >=0.89.5, fl_initialize()
135         // calls setlocale() and ruins our LC_NUMERIC setting.
136         locale_init();
137
138         fl_get_app_resources(res, num_res);
139
140         Display * display = fl_get_display();
141
142         if (!display) {
143                 lyxerr << "LyX: unable to access X display, exiting" << endl;
144                 os::warn("Unable to access X display, exiting");
145                 ::exit(1);
146         }
147
148         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
149
150         XSetErrorHandler(LyX_XErrHandler);
151
152         lyxColorHandler.reset(new LyXColorHandler());
153 }
154
155
156 void lyx_gui::parse_lyxrc()
157 {
158         // FIXME !!!!
159         lyxrc.dpi = 95;
160
161         XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
162
163         if (lyxrc.popup_font_encoding.empty())
164                 lyxrc.popup_font_encoding = lyxrc.font_norm;
165         // Set the font name for popups and menus
166         string boldfontname = lyxrc.popup_bold_font
167                                + "-*-*-*-?-*-*-*-*-"
168                                + lyxrc.popup_font_encoding;
169                 // "?" means "scale that font"
170         string fontname = lyxrc.popup_normal_font
171                                + "-*-*-*-?-*-*-*-*-"
172                                + lyxrc.popup_font_encoding;
173
174         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
175         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
176         if (bold < 0)
177                 lyxerr << "Could not set menu font to "
178                        << boldfontname << endl;
179
180         if (normal < 0)
181                 lyxerr << "Could not set popup font to "
182                        << fontname << endl;
183
184         if (bold < 0 && normal < 0) {
185                 lyxerr << "Using 'helvetica' font for menus" << endl;
186                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
187                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
188                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
189                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
190
191                 if (bold < 0 && normal < 0) {
192                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
193                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
194                         normal = bold = 0;
195                 }
196         }
197         if (bold < 0)
198                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
199         else if (normal < 0)
200                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
201
202         fl_setpup_fontstyle(FL_NORMAL_STYLE);
203         fl_setpup_fontsize(FL_NORMAL_SIZE);
204         fl_setpup_color(FL_MCOL, FL_BLACK);
205         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
206 #if FL_REVISION < 89
207         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
208 #else
209         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
210 #endif
211 }
212
213
214 void lyx_gui::start(string const & batch, vector<string> files)
215 {
216         // initial geometry
217         int xpos = -1;
218         int ypos = -1;
219         unsigned int width = 690;
220         unsigned int height = 510;
221
222         static const int geometryBitmask =
223                 XParseGeometry(geometry,
224                                 &xpos, &ypos, &width, &height);
225
226         // if width is not set by geometry, check it against monitor width
227         if (!(geometryBitmask & 4)) {
228                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
229                 if (WidthOfScreen(scr) - 8 < int(width))
230                         width = WidthOfScreen(scr) - 8;
231         }
232
233         // if height is not set by geometry, check it against monitor height
234         if (!(geometryBitmask & 8)) {
235                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
236                 if (HeightOfScreen(scr) - 24 < int(height))
237                         height = HeightOfScreen(scr) - 24;
238         }
239
240         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
241
242         // recalculate xpos if it's not set
243         if (xpos == -1)
244                 xpos = (WidthOfScreen(s) - width) / 2;
245
246         // recalculate ypos if it's not set
247         if (ypos == -1)
248                 ypos = (HeightOfScreen(s) - height) / 2;
249
250         lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
251                 << "+" << xpos << "+" << ypos << endl;
252
253         XFormsView view(width, height);
254         view.show(xpos, ypos, "LyX");
255         view.init();
256
257         Buffer * last = 0;
258
259         // FIXME: some code below needs moving
260
261         lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
262
263         vector<string>::const_iterator cit = files.begin();
264         vector<string>::const_iterator end = files.end();
265         for (; cit != end; ++cit) {
266                 Buffer * b = bufferlist.loadLyXFile(*cit);
267                 if (b) {
268                         last = b;
269                 }
270         }
271
272         // switch to the last buffer successfully loaded
273         if (last) {
274                 view.view()->buffer(last);
275         }
276
277         // handle the batch commands the user asked for
278         if (!batch.empty()) {
279                 view.getLyXFunc()->verboseDispatch(batch, false);
280         }
281
282         // enter the event loop
283         while (!finished) {
284                 if (fl_check_forms() == FL_EVENT) {
285                         XEvent ev;
286                         fl_XNextEvent(&ev);
287                         lyxerr << "Received unhandled X11 event" << endl;
288                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
289                                 " Target: 0x" << hex << ev.xany.window << endl;
290                 }
291         }
292
293         // FIXME
294         delete lyxserver;
295 }
296
297
298 // Called by the graphics cache to connect the appropriate frontend
299 // image loading routines to the LyX kernel.
300 void lyx_gui::init_graphics()
301 {
302         using namespace grfx;
303
304 #ifdef USE_XFORMS_IMAGE_LOADER
305         // connect the image loader based on the xforms library
306         Image::newImage = boost::bind(&xformsImage::newImage);
307         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
308 #else
309         // connect the image loader based on the XPM library
310         Image::newImage = boost::bind(&ImageXPM::newImage);
311         Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
312 #endif
313 }
314
315
316 string const lyx_gui::hexname(LColor::color col)
317 {
318         string const name = lcolor.getX11Name(col);
319         Display * const display = fl_get_display();
320         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
321         XColor xcol, ccol;
322
323         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
324                         lyxerr << "X can't find color \""
325                                << lcolor.getLyXName(col)
326                                << "\"" << endl;
327                         return string();
328         }
329
330         ostringstream os;
331
332         // Note that X stores the RGB values in the range 0 - 65535
333         // whilst we require them in the range 0 - 255.
334         os << setbase(16) << setfill('0')
335            << setw(2) << (xcol.red   / 256)
336            << setw(2) << (xcol.green / 256)
337            << setw(2) << (xcol.blue  / 256);
338
339         return os.str().c_str();
340 }
341
342
343 void lyx_gui::update_color(LColor::color col)
344 {
345         lyxColorHandler->updateColor(col);
346 }