]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/lyx_gui.C
5053f92aee5de89bc3bfc124a1ebe8ea13a013a8
[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 "LColor.h"
30 #include "LyXAction.h"
31 #include "lyx_main.h"
32 #include "lyxrc.h"
33 #include "lyxfont.h"
34 #include "graphics/LoaderQueue.h"
35
36 #include "io_callback.h"
37
38 // FIXME: move this stuff out again
39 #include "bufferlist.h"
40 #include "lyxfunc.h"
41 #include "lyxserver.h"
42 #include "lyxsocket.h"
43 #include "BufferView.h"
44
45 #include "GView.h"
46 #include "GtkmmX.h"
47
48 #include "xftFontLoader.h"
49 #include "GWorkArea.h"
50
51 #include "support/lyxlib.h"
52 #include "support/os.h"
53 #include "support/filetools.h"
54 #include "support/package.h"
55
56 #include <gtkmm.h>
57
58 #include "LyXGdkImage.h"
59
60 #include <boost/bind.hpp>
61 #include <boost/function.hpp>
62 #include <boost/shared_ptr.hpp>
63
64 #include <fcntl.h>
65
66 #include <sstream>
67 #include <iomanip>
68
69 namespace os = lyx::support::os;
70
71 using std::ostringstream;
72 using std::string;
73
74 using lyx::support::package;
75
76 using lyx::frontend::colorCache;
77 using lyx::frontend::GView;
78
79
80 extern BufferList bufferlist;
81
82 // FIXME: wrong place !
83 LyXServer * lyxserver;
84 LyXServerSocket * lyxsocket;
85
86 bool lyx_gui::use_gui = true;
87
88 namespace {
89
90 /// quit lyx
91 bool finished = false;
92
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
106 void lyx_gui::parse_init(int & argc, char * argv[])
107 {
108         new Gtk::Main(argc, argv);
109
110         using namespace lyx::graphics;
111         Image::newImage = boost::bind(&LyXGdkImage::newImage);
112         Image::loadableFormats = boost::bind(&LyXGdkImage::loadableFormats);
113
114         locale_init();
115
116         // must do this /before/ lyxrc gets read
117         lyxrc.dpi = getDPI();
118 }
119
120
121 void lyx_gui::parse_lyxrc()
122 {
123 }
124
125
126 void lyx_gui::start(string const & batch, std::vector<string> const & files)
127 {
128         boost::shared_ptr<GView> view_ptr(new GView);
129         LyX::ref().addLyXView(view_ptr);
130
131         GView & view = *view_ptr.get();
132         view.show();
133         view.init();
134
135         // FIXME: server code below needs moving
136
137         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
138         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
139                           os::internal_path(package().temp_dir() + "/lyxsocket"));
140
141         for_each(files.begin(), files.end(),
142                  bind(&BufferView::loadLyXFile, view.view(), _1, true));
143
144         // handle the batch commands the user asked for
145         if (!batch.empty()) {
146                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
147         }
148
149         // enter the event loop
150         while (!finished) {
151                 while (Gtk::Main::events_pending())
152                         Gtk::Main::iteration(false);
153         }
154
155         // FIXME: breaks emergencyCleanup
156         delete lyxsocket;
157         delete lyxserver;
158 }
159
160
161 void lyx_gui::exit()
162 {
163         finished = true;
164 }
165
166
167 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
168 {
169         FuncStatus flag;
170         switch (ev.action) {
171         case LFUN_DIALOG_SHOW:
172                 if (ev.argument == "preamble")
173                         flag.unknown(true);
174                 break;
175         case LFUN_TOOLTIPS_TOGGLE:
176                 flag.unknown(true);
177                 break;
178         default:
179                 break;
180         }
181
182         return flag;
183 }
184
185
186 string const lyx_gui::hexname(LColor_color col)
187 {
188         Gdk::Color gdkColor;
189         Gdk::Color * gclr = colorCache.getColor(col);
190         if (!gclr) {
191                 gclr = &gdkColor;
192                 gclr->parse(lcolor.getX11Name(col));
193         }
194
195         std::ostringstream os;
196
197         // Note that X stores the RGB values in the range 0 - 65535
198         // whilst we require them in the range 0 - 255.
199         os << std::setbase(16) << std::setfill('0')
200            << std::setw(2) << (gclr->get_red() / 256)
201            << std::setw(2) << (gclr->get_green() / 256)
202            << std::setw(2) << (gclr->get_blue() / 256);
203
204         return os.str();
205 }
206
207
208 void lyx_gui::update_color(LColor_color /*col*/)
209 {
210         colorCache.clear();
211 }
212
213
214 void lyx_gui::update_fonts()
215 {
216         fontLoader.update();
217 }
218
219
220 bool lyx_gui::font_available(LyXFont const & font)
221 {
222         return fontLoader.available(font);
223 }
224
225
226 namespace {
227
228 std::map<int, boost::shared_ptr<io_callback> > callbacks;
229
230 } // NS anon
231
232
233 void lyx_gui::register_socket_callback(int fd,
234                                        boost::function<void()> func)
235 {
236         callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
237 }
238
239
240 void lyx_gui::unregister_socket_callback(int fd)
241 {
242         callbacks.erase(fd);
243 }
244
245
246 string const lyx_gui::roman_font_name()
247 {
248         return "times";
249 }
250
251
252 string const lyx_gui::sans_font_name()
253 {
254         return "helvetica";
255 }
256
257
258 string const lyx_gui::typewriter_font_name()
259 {
260         return "courier";
261 }
262
263
264 void lyx_gui::sync_events()
265 {
266         // FIXME
267 }