]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/lyx_gui.C
fef13b1af14eba2e3278b660c60121182f08828c
[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 {
125         boost::shared_ptr<GView> view_ptr(new GView);
126         LyX::ref().addLyXView(view_ptr);
127
128         GView & view = *view_ptr.get();
129         view.show();
130         view.init();
131
132         // FIXME: server code below needs moving
133
134         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
135         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
136                           os::internal_path(package().temp_dir() + "/lyxsocket"));
137
138         for_each(files.begin(), files.end(),
139                  bind(&BufferView::loadLyXFile, view.view(), _1, true));
140
141         // handle the batch commands the user asked for
142         if (!batch.empty()) {
143                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
144         }
145
146         Gtk::Main::run();
147
148         // FIXME: breaks emergencyCleanup
149         delete lyxsocket;
150         delete lyxserver;
151 }
152
153
154 void lyx_gui::exit()
155 {
156         Gtk::Main::quit();
157 }
158
159
160 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
161 {
162         FuncStatus flag;
163         switch (ev.action) {
164         case LFUN_DIALOG_SHOW:
165                 if (ev.argument == "preamble")
166                         flag.unknown(true);
167                 break;
168         case LFUN_TOOLTIPS_TOGGLE:
169                 flag.unknown(true);
170                 break;
171         default:
172                 break;
173         }
174
175         return flag;
176 }
177
178
179 bool lyx_gui::getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
180 {
181         Gdk::Color gdkColor;
182         Gdk::Color * gclr = colorCache.getColor(col);
183         if (!gclr) {
184                 gclr = &gdkColor;
185                 if(!gclr->parse(lcolor.getX11Name(col))) {
186                         rgbcol.r = 0;
187                         rgbcol.g = 0;
188                         rgbcol.b = 0;
189                         return false;
190                 }
191         }
192
193         // Note that X stores the RGB values in the range 0 - 65535
194         // whilst we require them in the range 0 - 255.
195         rgbcol.r = gclr->get_red() / 256;
196         rgbcol.g = gclr->get_green() / 256;
197         rgbcol.b = gclr->get_blue() / 256;
198         return true;
199 }
200
201
202 string const lyx_gui::hexname(LColor_color col)
203 {
204         lyx::RGBColor rgbcol;
205         if (!getRGBColor(col, rgbcol)) {
206                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
207                        << '"' << std::endl;
208                 return string();
209         }
210
211         std::ostringstream os;
212
213         os << std::setbase(16) << std::setfill('0')
214            << std::setw(2) << rgbcol.r
215            << std::setw(2) << rgbcol.g
216            << std::setw(2) << rgbcol.b;
217
218         return os.str();
219 }
220
221
222 void lyx_gui::update_color(LColor_color /*col*/)
223 {
224         colorCache.clear();
225 }
226
227
228 void lyx_gui::update_fonts()
229 {
230         fontLoader.update();
231 }
232
233
234 bool lyx_gui::font_available(LyXFont const & font)
235 {
236         return fontLoader.available(font);
237 }
238
239
240 namespace {
241
242 std::map<int, boost::shared_ptr<io_callback> > callbacks;
243
244 } // NS anon
245
246
247 void lyx_gui::register_socket_callback(int fd,
248                                        boost::function<void()> func)
249 {
250         callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
251 }
252
253
254 void lyx_gui::unregister_socket_callback(int fd)
255 {
256         callbacks.erase(fd);
257 }
258
259
260 string const lyx_gui::roman_font_name()
261 {
262         return "times";
263 }
264
265
266 string const lyx_gui::sans_font_name()
267 {
268         return "helvetica";
269 }
270
271
272 string const lyx_gui::typewriter_font_name()
273 {
274         return "courier";
275 }
276
277
278 void lyx_gui::sync_events()
279 {
280         // FIXME
281 }