]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/lyx_gui.C
* src/frontends/*/lyx_gui.C:
[lyx.git] / src / frontends / gtk / lyx_gui.C
1 /**
2  * \file gtk/lyx_gui.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjnes
7  * \author John Levon
8  * \author Huang Ying
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 // Too hard to make concept checks work with this file
16 #ifdef _GLIBCXX_CONCEPT_CHECKS
17 #undef _GLIBCXX_CONCEPT_CHECKS
18 #endif
19 #ifdef _GLIBCPP_CONCEPT_CHECKS
20 #undef _GLIBCPP_CONCEPT_CHECKS
21 #endif
22
23 #include "lyx_gui.h"
24
25 #include "debug.h"
26 #include "funcrequest.h"
27 #include "gettext.h"
28
29 #include "Color.h"
30 #include "LColor.h"
31 #include "LyXAction.h"
32 #include "lyx_main.h"
33 #include "lyxrc.h"
34 #include "lyxfont.h"
35 #include "graphics/LoaderQueue.h"
36
37 #include "io_callback.h"
38
39 // FIXME: move this stuff out again
40 #include "bufferlist.h"
41 #include "lyxfunc.h"
42 #include "lyxserver.h"
43 #include "lyxsocket.h"
44 #include "BufferView.h"
45
46 #include "GView.h"
47 #include "GtkmmX.h"
48
49 #include "xftFontLoader.h"
50 #include "GWorkArea.h"
51
52 #include "support/lyxlib.h"
53 #include "support/os.h"
54 #include "support/filetools.h"
55 #include "support/package.h"
56
57 #include <gtkmm.h>
58
59 #include "LyXGdkImage.h"
60
61 #include <boost/bind.hpp>
62 #include <boost/function.hpp>
63 #include <boost/shared_ptr.hpp>
64
65 #include <fcntl.h>
66
67 #include <sstream>
68 #include <iomanip>
69
70 namespace os = lyx::support::os;
71
72 using std::ostringstream;
73 using std::string;
74
75 using lyx::support::package;
76
77 using lyx::frontend::colorCache;
78 using lyx::frontend::GView;
79
80
81 extern BufferList bufferlist;
82
83 // FIXME: wrong place !
84 LyXServer * lyxserver;
85 LyXServerSocket * lyxsocket;
86
87 bool lyx_gui::use_gui = true;
88
89 namespace {
90
91 /// estimate DPI from X server
92 int getDPI()
93 {
94         //TODO use GDK instead
95         Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
96         return int(((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
97                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2);
98 }
99
100 } // namespace anon
101
102
103 void lyx_gui::parse_init(int & argc, char * argv[])
104 {
105         new Gtk::Main(argc, argv);
106
107         using namespace lyx::graphics;
108         Image::newImage = boost::bind(&LyXGdkImage::newImage);
109         Image::loadableFormats = boost::bind(&LyXGdkImage::loadableFormats);
110
111         locale_init();
112
113         // must do this /before/ lyxrc gets read
114         lyxrc.dpi = getDPI();
115 }
116
117
118 void lyx_gui::parse_lyxrc()
119 {
120 }
121
122
123 void lyx_gui::start(string const & batch, std::vector<string> const & files,
124                     unsigned int width, unsigned int height, int posx, int posy)
125 {
126         boost::shared_ptr<GView> view_ptr(new GView);
127         LyX::ref().addLyXView(view_ptr);
128
129         GView & view = *view_ptr.get();
130         view.show();
131         view.init();
132
133         // FIXME: server code below needs moving
134
135         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
136         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
137                           os::internal_path(package().temp_dir() + "/lyxsocket"));
138
139         for_each(files.begin(), files.end(),
140                  bind(&BufferView::loadLyXFile, view.view(), _1, true));
141
142         // handle the batch commands the user asked for
143         if (!batch.empty()) {
144                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
145         }
146
147         Gtk::Main::run();
148
149         // FIXME: breaks emergencyCleanup
150         delete lyxsocket;
151         delete lyxserver;
152 }
153
154
155 void lyx_gui::exit()
156 {
157         Gtk::Main::quit();
158 }
159
160
161 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
162 {
163         FuncStatus flag;
164         switch (ev.action) {
165         case LFUN_DIALOG_SHOW:
166                 if (ev.argument == "preamble")
167                         flag.unknown(true);
168                 break;
169         case LFUN_TOOLTIPS_TOGGLE:
170                 flag.unknown(true);
171                 break;
172         default:
173                 break;
174         }
175
176         return flag;
177 }
178
179
180 bool lyx_gui::getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
181 {
182         Gdk::Color gdkColor;
183         Gdk::Color * gclr = colorCache.getColor(col);
184         if (!gclr) {
185                 gclr = &gdkColor;
186                 if(!gclr->parse(lcolor.getX11Name(col))) {
187                         rgbcol.r = 0;
188                         rgbcol.g = 0;
189                         rgbcol.b = 0;
190                         return false;
191                 }
192         }
193
194         // Note that X stores the RGB values in the range 0 - 65535
195         // whilst we require them in the range 0 - 255.
196         rgbcol.r = gclr->get_red() / 256;
197         rgbcol.g = gclr->get_green() / 256;
198         rgbcol.b = gclr->get_blue() / 256;
199         return true;
200 }
201
202
203 string const lyx_gui::hexname(LColor_color col)
204 {
205         lyx::RGBColor rgbcol;
206         if (!getRGBColor(col, rgbcol)) {
207                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
208                        << '"' << std::endl;
209                 return string();
210         }
211
212         std::ostringstream os;
213
214         os << std::setbase(16) << std::setfill('0')
215            << std::setw(2) << rgbcol.r
216            << std::setw(2) << rgbcol.g
217            << std::setw(2) << rgbcol.b;
218
219         return os.str();
220 }
221
222
223 void lyx_gui::update_color(LColor_color /*col*/)
224 {
225         colorCache.clear();
226 }
227
228
229 void lyx_gui::update_fonts()
230 {
231         fontLoader.update();
232 }
233
234
235 bool lyx_gui::font_available(LyXFont const & font)
236 {
237         return fontLoader.available(font);
238 }
239
240
241 namespace {
242
243 std::map<int, boost::shared_ptr<io_callback> > callbacks;
244
245 } // NS anon
246
247
248 void lyx_gui::register_socket_callback(int fd,
249                                        boost::function<void()> func)
250 {
251         callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
252 }
253
254
255 void lyx_gui::unregister_socket_callback(int fd)
256 {
257         callbacks.erase(fd);
258 }
259
260
261 string const lyx_gui::roman_font_name()
262 {
263         return "times";
264 }
265
266
267 string const lyx_gui::sans_font_name()
268 {
269         return "helvetica";
270 }
271
272
273 string const lyx_gui::typewriter_font_name()
274 {
275         return "courier";
276 }
277
278
279 void lyx_gui::sync_events()
280 {
281         // FIXME
282 }