]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/lyx_gui.C
* gtk/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 "GuiImplementation.h"
47 #include "GView.h"
48 #include "GtkmmX.h"
49
50 #include "xftFontLoader.h"
51 #include "GWorkArea.h"
52
53 #include "support/lyxlib.h"
54 #include "support/os.h"
55 #include "support/filetools.h"
56 #include "support/package.h"
57
58 #include <gtkmm.h>
59
60 #include "LyXGdkImage.h"
61
62 #include <boost/bind.hpp>
63 #include <boost/function.hpp>
64 #include <boost/shared_ptr.hpp>
65
66 #include <fcntl.h>
67
68 #include <sstream>
69 #include <iomanip>
70
71 namespace os = lyx::support::os;
72
73 using std::ostringstream;
74 using std::string;
75
76 using lyx::support::package;
77
78 using lyx::frontend::colorCache;
79 using lyx::frontend::Gui;
80 using lyx::frontend::GuiImplementation;
81 using lyx::frontend::GView;
82
83
84 extern BufferList bufferlist;
85
86 // FIXME: wrong place !
87 LyXServer * lyxserver;
88 LyXServerSocket * lyxsocket;
89
90 bool lyx_gui::use_gui = true;
91
92 namespace {
93
94 /// estimate DPI from X server
95 int getDPI()
96 {
97         //TODO use GDK instead
98         Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
99         return int(((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
100                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2);
101 }
102
103 } // namespace anon
104
105 class Application: public Gtk::Main
106 {
107 public:
108         ///
109         Application(int & argc, char * argv[]): Gtk::Main(argc, argv)
110         {}
111         ///
112         Gui & gui() { return gui_; }
113
114 private:
115         ///
116         GuiImplementation gui_;
117 };
118
119 Application * theApp;
120
121 int lyx_gui::exec(int & argc, char * argv[])
122 {
123         theApp = new Application(argc, argv);
124
125         using namespace lyx::graphics;
126         Image::newImage = boost::bind(&LyXGdkImage::newImage);
127         Image::loadableFormats = boost::bind(&LyXGdkImage::loadableFormats);
128
129         locale_init();
130
131         // must do this /before/ lyxrc gets read
132         lyxrc.dpi = getDPI();
133
134         return LyX::ref().exec2(argc, argv);
135 }
136
137
138 void lyx_gui::parse_lyxrc()
139 {
140 }
141
142
143 int lyx_gui::start(string const & batch, std::vector<string> const & files,
144                    unsigned int width, unsigned int height,
145                    int /*posx*/, int /*posy*/, bool)
146 {
147         int view_id = theApp->gui().newView(width, height);
148         GView & view = static_cast<GView &> (theApp->gui().view(view_id));
149         theApp->gui().newWorkArea(width, height, 0);
150
151         LyX::ref().addLyXView(&view);
152
153         view.show();
154         view.init();
155
156         // FIXME: server code below needs moving
157
158         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
159         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
160                           os::internal_path(package().temp_dir() + "/lyxsocket"));
161
162         for_each(files.begin(), files.end(),
163                  bind(&BufferView::loadLyXFile, view.view(), _1, true));
164
165         // handle the batch commands the user asked for
166         if (!batch.empty()) {
167                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
168         }
169
170         theApp->run();
171
172         // FIXME: breaks emergencyCleanup
173         delete lyxsocket;
174         delete lyxserver;
175         return EXIT_SUCCESS;
176 }
177
178
179 void lyx_gui::exit(int /*status*/)
180 {
181         // FIXME: Don't ignore status
182         theApp->quit();
183 }
184
185
186 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
187 {
188         FuncStatus flag;
189         switch (ev.action) {
190         case LFUN_TOOLTIPS_TOGGLE:
191                 flag.unknown(true);
192                 break;
193         default:
194                 break;
195         }
196
197         return flag;
198 }
199
200
201 bool lyx_gui::getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
202 {
203         Gdk::Color gdkColor;
204         Gdk::Color * gclr = colorCache.getColor(col);
205         if (!gclr) {
206                 gclr = &gdkColor;
207                 if(!gclr->parse(lcolor.getX11Name(col))) {
208                         rgbcol.r = 0;
209                         rgbcol.g = 0;
210                         rgbcol.b = 0;
211                         return false;
212                 }
213         }
214
215         // Note that X stores the RGB values in the range 0 - 65535
216         // whilst we require them in the range 0 - 255.
217         rgbcol.r = gclr->get_red() / 256;
218         rgbcol.g = gclr->get_green() / 256;
219         rgbcol.b = gclr->get_blue() / 256;
220         return true;
221 }
222
223
224 string const lyx_gui::hexname(LColor_color col)
225 {
226         lyx::RGBColor rgbcol;
227         if (!getRGBColor(col, rgbcol)) {
228                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
229                        << '"' << std::endl;
230                 return string();
231         }
232
233         std::ostringstream os;
234
235         os << std::setbase(16) << std::setfill('0')
236            << std::setw(2) << rgbcol.r
237            << std::setw(2) << rgbcol.g
238            << std::setw(2) << rgbcol.b;
239
240         return os.str();
241 }
242
243
244 void lyx_gui::update_color(LColor_color /*col*/)
245 {
246         colorCache.clear();
247 }
248
249
250 void lyx_gui::update_fonts()
251 {
252         fontLoader.update();
253 }
254
255
256 bool lyx_gui::font_available(LyXFont const & font)
257 {
258         return fontLoader.available(font);
259 }
260
261
262 namespace {
263
264 std::map<int, boost::shared_ptr<io_callback> > callbacks;
265
266 } // NS anon
267
268
269 void lyx_gui::register_socket_callback(int fd,
270                                        boost::function<void()> func)
271 {
272         callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
273 }
274
275
276 void lyx_gui::unregister_socket_callback(int fd)
277 {
278         callbacks.erase(fd);
279 }
280
281
282 string const lyx_gui::roman_font_name()
283 {
284         return "times";
285 }
286
287
288 string const lyx_gui::sans_font_name()
289 {
290         return "helvetica";
291 }
292
293
294 string const lyx_gui::typewriter_font_name()
295 {
296         return "courier";
297 }
298
299
300 void lyx_gui::sync_events()
301 {
302         // FIXME
303 }