]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/lyx_gui.C
Remove quite a few compiler warnings:
[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_DIALOG_SHOW:
191                 if (ev.argument == "preamble")
192                         flag.unknown(true);
193                 break;
194         case LFUN_TOOLTIPS_TOGGLE:
195                 flag.unknown(true);
196                 break;
197         default:
198                 break;
199         }
200
201         return flag;
202 }
203
204
205 bool lyx_gui::getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
206 {
207         Gdk::Color gdkColor;
208         Gdk::Color * gclr = colorCache.getColor(col);
209         if (!gclr) {
210                 gclr = &gdkColor;
211                 if(!gclr->parse(lcolor.getX11Name(col))) {
212                         rgbcol.r = 0;
213                         rgbcol.g = 0;
214                         rgbcol.b = 0;
215                         return false;
216                 }
217         }
218
219         // Note that X stores the RGB values in the range 0 - 65535
220         // whilst we require them in the range 0 - 255.
221         rgbcol.r = gclr->get_red() / 256;
222         rgbcol.g = gclr->get_green() / 256;
223         rgbcol.b = gclr->get_blue() / 256;
224         return true;
225 }
226
227
228 string const lyx_gui::hexname(LColor_color col)
229 {
230         lyx::RGBColor rgbcol;
231         if (!getRGBColor(col, rgbcol)) {
232                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
233                        << '"' << std::endl;
234                 return string();
235         }
236
237         std::ostringstream os;
238
239         os << std::setbase(16) << std::setfill('0')
240            << std::setw(2) << rgbcol.r
241            << std::setw(2) << rgbcol.g
242            << std::setw(2) << rgbcol.b;
243
244         return os.str();
245 }
246
247
248 void lyx_gui::update_color(LColor_color /*col*/)
249 {
250         colorCache.clear();
251 }
252
253
254 void lyx_gui::update_fonts()
255 {
256         fontLoader.update();
257 }
258
259
260 bool lyx_gui::font_available(LyXFont const & font)
261 {
262         return fontLoader.available(font);
263 }
264
265
266 namespace {
267
268 std::map<int, boost::shared_ptr<io_callback> > callbacks;
269
270 } // NS anon
271
272
273 void lyx_gui::register_socket_callback(int fd,
274                                        boost::function<void()> func)
275 {
276         callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
277 }
278
279
280 void lyx_gui::unregister_socket_callback(int fd)
281 {
282         callbacks.erase(fd);
283 }
284
285
286 string const lyx_gui::roman_font_name()
287 {
288         return "times";
289 }
290
291
292 string const lyx_gui::sans_font_name()
293 {
294         return "helvetica";
295 }
296
297
298 string const lyx_gui::typewriter_font_name()
299 {
300         return "courier";
301 }
302
303
304 void lyx_gui::sync_events()
305 {
306         // FIXME
307 }