Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <string.h>
21 : #include <gmodule.h>
22 : #include <gtk/gtk.h>
23 : #include <unx/gtk/gtkinst.hxx>
24 : #include <unx/gtk/gtksys.hxx>
25 :
26 0 : GtkSalSystem *GtkSalSystem::GetSingleton()
27 : {
28 : static GtkSalSystem *pSingleton = NULL;
29 0 : if (!pSingleton)
30 0 : pSingleton = new GtkSalSystem();
31 0 : return pSingleton;
32 : }
33 :
34 0 : SalSystem *GtkInstance::CreateSalSystem()
35 : {
36 0 : return GtkSalSystem::GetSingleton();
37 : }
38 :
39 0 : GtkSalSystem::GtkSalSystem() : SalGenericSystem()
40 : {
41 0 : mpDisplay = gdk_display_get_default();
42 0 : countScreenMonitors();
43 0 : }
44 :
45 0 : GtkSalSystem::~GtkSalSystem()
46 : {
47 0 : }
48 :
49 : int
50 0 : GtkSalSystem::GetDisplayXScreenCount()
51 : {
52 0 : return gdk_display_get_n_screens (mpDisplay);
53 : }
54 :
55 : namespace
56 : {
57 :
58 : struct GdkRectangleEqual
59 : {
60 0 : bool operator()(GdkRectangle const& rLeft, GdkRectangle const& rRight)
61 : {
62 : return
63 0 : rLeft.x == rRight.x
64 0 : && rLeft.y == rRight.y
65 0 : && rLeft.width == rRight.width
66 0 : && rLeft.height == rRight.height
67 : ;
68 : }
69 : };
70 :
71 : }
72 :
73 : /**
74 : * GtkSalSystem::countScreenMonitors()
75 : *
76 : * This method builds the vector which allows us to map from VCL's
77 : * idea of linear integer ScreenNumber to to gtk+'s rather more
78 : * complicated screen + monitor concept.
79 : */
80 : void
81 0 : GtkSalSystem::countScreenMonitors()
82 : {
83 0 : maScreenMonitors.clear();
84 0 : for (gint i = 0; i < gdk_display_get_n_screens(mpDisplay); i++)
85 : {
86 0 : GdkScreen* const pScreen(gdk_display_get_screen(mpDisplay, i));
87 0 : gint nMonitors(pScreen ? gdk_screen_get_n_monitors(pScreen) : 0);
88 0 : if (nMonitors > 1)
89 : {
90 0 : std::vector<GdkRectangle> aGeometries;
91 0 : aGeometries.reserve(nMonitors);
92 0 : for (gint j(0); j != nMonitors; ++j)
93 : {
94 : GdkRectangle aGeometry;
95 0 : gdk_screen_get_monitor_geometry(pScreen, j, &aGeometry);
96 0 : aGeometries.push_back(aGeometry);
97 : }
98 : GdkRectangleEqual aCmp;
99 0 : std::sort(aGeometries.begin(), aGeometries.end(), aCmp);
100 : const std::vector<GdkRectangle>::iterator aUniqueEnd(
101 0 : std::unique(aGeometries.begin(), aGeometries.end(), aCmp));
102 0 : nMonitors = std::distance(aGeometries.begin(), aUniqueEnd);
103 : }
104 0 : maScreenMonitors.push_back(std::make_pair(pScreen, nMonitors));
105 : }
106 0 : }
107 :
108 : // Including gdkx.h kills us with the Window / XWindow conflict
109 : extern "C" {
110 : GType gdk_x11_display_get_type (void);
111 : int gdk_x11_screen_get_screen_number (GdkScreen *screen);
112 : }
113 :
114 : SalX11Screen
115 0 : GtkSalSystem::getXScreenFromDisplayScreen(unsigned int nScreen)
116 : {
117 : gint nMonitor;
118 :
119 0 : GdkScreen *pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
120 0 : if (!pScreen)
121 0 : return SalX11Screen (0);
122 : #if GTK_CHECK_VERSION(3,0,0)
123 : if (!G_TYPE_CHECK_INSTANCE_TYPE (mpDisplay, gdk_x11_display_get_type ()))
124 : return SalX11Screen (0);
125 : #endif
126 0 : return SalX11Screen (gdk_x11_screen_get_screen_number (pScreen));
127 : }
128 :
129 : GdkScreen *
130 0 : GtkSalSystem::getScreenMonitorFromIdx (int nIdx, gint &nMonitor)
131 : {
132 0 : GdkScreen *pScreen = NULL;
133 0 : for (ScreenMonitors_t::const_iterator aIt(maScreenMonitors.begin()), aEnd(maScreenMonitors.end()); aIt != aEnd; ++aIt)
134 : {
135 0 : pScreen = aIt->first;
136 0 : if (!pScreen)
137 0 : break;
138 0 : if (nIdx >= aIt->second)
139 0 : nIdx -= aIt->second;
140 : else
141 0 : break;
142 : }
143 0 : nMonitor = nIdx;
144 :
145 : // handle invalid monitor indexes as non-existent screens
146 0 : if (nMonitor < 0 || (pScreen && nMonitor >= gdk_screen_get_n_monitors (pScreen)))
147 0 : pScreen = NULL;
148 :
149 0 : return pScreen;
150 : }
151 :
152 : int
153 0 : GtkSalSystem::getScreenIdxFromPtr (GdkScreen *pScreen)
154 : {
155 0 : int nIdx = 0;
156 0 : for (ScreenMonitors_t::const_iterator aIt(maScreenMonitors.begin()), aEnd(maScreenMonitors.end()); aIt != aEnd; ++aIt)
157 : {
158 0 : if (aIt->first == pScreen)
159 0 : return nIdx;
160 0 : nIdx += aIt->second;
161 : }
162 0 : g_warning ("failed to find screen %p", pScreen);
163 0 : return 0;
164 : }
165 :
166 0 : int GtkSalSystem::getScreenMonitorIdx (GdkScreen *pScreen,
167 : int nX, int nY)
168 : {
169 : // TODO: this will fail horribly for exotic combinations like two
170 : // monitors in mirror mode and one extra. Hopefully such
171 : // abominations are not used (or, even better, not possible) in
172 : // practice .-)
173 0 : return getScreenIdxFromPtr (pScreen) +
174 0 : gdk_screen_get_monitor_at_point (pScreen, nX, nY);
175 : }
176 :
177 0 : unsigned int GtkSalSystem::GetDisplayScreenCount()
178 : {
179 : gint nMonitor;
180 0 : (void)getScreenMonitorFromIdx (G_MAXINT, nMonitor);
181 0 : return G_MAXINT - nMonitor;
182 : }
183 :
184 0 : bool GtkSalSystem::IsUnifiedDisplay()
185 : {
186 0 : return gdk_display_get_n_screens (mpDisplay) == 1;
187 : }
188 :
189 : namespace {
190 : #if GTK_CHECK_VERSION(2,14,0)
191 0 : static int _fallback_get_primary_monitor (GdkScreen *pScreen)
192 : {
193 : // Use monitor name as primacy heuristic
194 0 : int max = gdk_screen_get_n_monitors (pScreen);
195 0 : for (int i = 0; i < max; ++i)
196 : {
197 0 : char *name = gdk_screen_get_monitor_plug_name (pScreen, i);
198 0 : bool bLaptop = (name && !g_ascii_strncasecmp (name, "LVDS", 4));
199 0 : g_free (name);
200 0 : if (bLaptop)
201 0 : return i;
202 : }
203 0 : return 0;
204 : }
205 : #endif
206 :
207 0 : static int _get_primary_monitor (GdkScreen *pScreen)
208 : {
209 : static int (*get_fn) (GdkScreen *) = NULL;
210 : #if GTK_CHECK_VERSION(3,0,0)
211 : get_fn = gdk_screen_get_primary_monitor;
212 : #endif
213 : // Perhaps we have a newer gtk+ with this symbol:
214 0 : if (!get_fn)
215 : {
216 : get_fn = (int(*)(GdkScreen*))osl_getAsciiFunctionSymbol(NULL,
217 0 : "gdk_screen_get_primary_monitor");
218 : }
219 : #if GTK_CHECK_VERSION(2,14,0)
220 0 : if (!get_fn)
221 0 : get_fn = _fallback_get_primary_monitor;
222 : #endif
223 0 : if (get_fn)
224 0 : return get_fn (pScreen);
225 : else
226 0 : return 0;
227 : }
228 : } // end anonymous namespace
229 :
230 0 : unsigned int GtkSalSystem::GetDisplayBuiltInScreen()
231 : {
232 0 : GdkScreen *pDefault = gdk_display_get_default_screen (mpDisplay);
233 0 : int idx = getScreenIdxFromPtr (pDefault);
234 0 : return idx + _get_primary_monitor (pDefault);
235 : }
236 :
237 0 : Rectangle GtkSalSystem::GetDisplayScreenPosSizePixel (unsigned int nScreen)
238 : {
239 : gint nMonitor;
240 : GdkScreen *pScreen;
241 : GdkRectangle aRect;
242 0 : pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
243 0 : if (!pScreen)
244 0 : return Rectangle();
245 0 : gdk_screen_get_monitor_geometry (pScreen, nMonitor, &aRect);
246 0 : return Rectangle (Point(aRect.x, aRect.y), Size(aRect.width, aRect.height));
247 : }
248 :
249 0 : Rectangle GtkSalSystem::GetDisplayScreenWorkAreaPosSizePixel (unsigned int nScreen)
250 : {
251 : // FIXME: in theory we need extra code here to collect
252 : // the work area, ignoring fixed panels etc. on the screen.
253 : // surely gtk+ should have API to get this for us (?)
254 0 : return GetDisplayScreenPosSizePixel( nScreen );
255 : }
256 :
257 0 : OUString GtkSalSystem::GetDisplayScreenName(unsigned int nScreen)
258 : {
259 : gchar *pStr;
260 : gint nMonitor;
261 : GdkScreen *pScreen;
262 0 : pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
263 0 : if (!pScreen)
264 0 : return OUString();
265 :
266 : #if GTK_CHECK_VERSION(3,0,0) || GTK_CHECK_VERSION(2,14,0)
267 0 : pStr = gdk_screen_get_monitor_plug_name (pScreen, nMonitor);
268 : #else
269 : static gchar * (*get_fn) (GdkScreen *, int) = NULL;
270 :
271 : GModule *module = g_module_open (NULL, (GModuleFlags) 0);
272 : if (!g_module_symbol (module, "gdk_screen_get_monitor_plug_name",
273 : (gpointer *)&get_fn))
274 : get_fn = NULL;
275 : g_module_close (module);
276 :
277 : if (get_fn)
278 : pStr = get_fn (pScreen, nMonitor);
279 : else
280 : pStr = g_strdup_printf ("%d", nScreen);
281 : #endif
282 0 : OUString aRet (pStr, strlen (pStr), RTL_TEXTENCODING_UTF8);
283 0 : g_free (pStr);
284 0 : return aRet;
285 : }
286 :
287 : // convert ~ to indicate mnemonic to '_'
288 0 : static OString MapToGtkAccelerator(const OUString &rStr)
289 : {
290 0 : return OUStringToOString(rStr.replaceFirst("~", "_"), RTL_TEXTENCODING_UTF8);
291 : }
292 :
293 0 : int GtkSalSystem::ShowNativeDialog (const OUString& rTitle, const OUString& rMessage,
294 : const std::list< OUString >& rButtonNames,
295 : int nDefaultButton)
296 : {
297 0 : OString aTitle (OUStringToOString (rTitle, RTL_TEXTENCODING_UTF8));
298 0 : OString aMessage (OUStringToOString (rMessage, RTL_TEXTENCODING_UTF8));
299 :
300 0 : GtkDialog *pDialog = GTK_DIALOG (
301 : g_object_new (GTK_TYPE_MESSAGE_DIALOG,
302 : "title", aTitle.getStr(),
303 : "message-type", (int)GTK_MESSAGE_WARNING,
304 : "text", aMessage.getStr(),
305 : NULL));
306 0 : int nButton = 0;
307 0 : std::list< OUString >::const_iterator it;
308 0 : for (it = rButtonNames.begin(); it != rButtonNames.end(); ++it)
309 0 : gtk_dialog_add_button (pDialog, MapToGtkAccelerator(*it).getStr(), nButton++);
310 0 : gtk_dialog_set_default_response (pDialog, nDefaultButton);
311 :
312 0 : nButton = gtk_dialog_run (pDialog);
313 0 : if (nButton < 0)
314 0 : nButton = -1;
315 :
316 0 : gtk_widget_destroy (GTK_WIDGET (pDialog));
317 :
318 0 : return nButton;
319 0 : }
320 :
321 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|