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 GdkRectangleCoincident
59 : {
60 : // fdo#78799 - detect and elide overlaying monitors of different sizes
61 0 : bool operator()(GdkRectangle const& rLeft, GdkRectangle const& rRight)
62 : {
63 : return
64 0 : rLeft.x == rRight.x
65 0 : && rLeft.y == rRight.y
66 : ;
67 : }
68 : };
69 :
70 : }
71 :
72 : /**
73 : * GtkSalSystem::countScreenMonitors()
74 : *
75 : * This method builds the vector which allows us to map from VCL's
76 : * idea of linear integer ScreenNumber to to gtk+'s rather more
77 : * complicated screen + monitor concept.
78 : */
79 : void
80 0 : GtkSalSystem::countScreenMonitors()
81 : {
82 0 : maScreenMonitors.clear();
83 0 : for (gint i = 0; i < gdk_display_get_n_screens(mpDisplay); i++)
84 : {
85 0 : GdkScreen* const pScreen(gdk_display_get_screen(mpDisplay, i));
86 0 : gint nMonitors(pScreen ? gdk_screen_get_n_monitors(pScreen) : 0);
87 0 : if (nMonitors > 1)
88 : {
89 0 : std::vector<GdkRectangle> aGeometries;
90 0 : aGeometries.reserve(nMonitors);
91 0 : for (gint j(0); j != nMonitors; ++j)
92 : {
93 : GdkRectangle aGeometry;
94 0 : gdk_screen_get_monitor_geometry(pScreen, j, &aGeometry);
95 0 : aGeometries.push_back(aGeometry);
96 : }
97 : GdkRectangleCoincident aCmp;
98 0 : std::sort(aGeometries.begin(), aGeometries.end(), aCmp);
99 : const std::vector<GdkRectangle>::iterator aUniqueEnd(
100 0 : std::unique(aGeometries.begin(), aGeometries.end(), aCmp));
101 0 : nMonitors = std::distance(aGeometries.begin(), aUniqueEnd);
102 : }
103 0 : maScreenMonitors.push_back(std::make_pair(pScreen, nMonitors));
104 : }
105 0 : }
106 :
107 : // Including gdkx.h kills us with the Window / XWindow conflict
108 : extern "C" {
109 : #if GTK_CHECK_VERSION(3,0,0)
110 : GType gdk_x11_display_get_type (void);
111 : #endif
112 : int gdk_x11_screen_get_screen_number (GdkScreen *screen);
113 : }
114 :
115 : SalX11Screen
116 0 : GtkSalSystem::getXScreenFromDisplayScreen(unsigned int nScreen)
117 : {
118 : gint nMonitor;
119 :
120 0 : GdkScreen *pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
121 0 : if (!pScreen)
122 0 : return SalX11Screen (0);
123 : #if GTK_CHECK_VERSION(3,0,0)
124 : if (!G_TYPE_CHECK_INSTANCE_TYPE (mpDisplay, gdk_x11_display_get_type ()))
125 : return SalX11Screen (0);
126 : #endif
127 0 : return SalX11Screen (gdk_x11_screen_get_screen_number (pScreen));
128 : }
129 :
130 : GdkScreen *
131 0 : GtkSalSystem::getScreenMonitorFromIdx (int nIdx, gint &nMonitor)
132 : {
133 0 : GdkScreen *pScreen = NULL;
134 0 : for (ScreenMonitors_t::const_iterator aIt(maScreenMonitors.begin()), aEnd(maScreenMonitors.end()); aIt != aEnd; ++aIt)
135 : {
136 0 : pScreen = aIt->first;
137 0 : if (!pScreen)
138 0 : break;
139 0 : if (nIdx >= aIt->second)
140 0 : nIdx -= aIt->second;
141 : else
142 0 : break;
143 : }
144 0 : nMonitor = nIdx;
145 :
146 : // handle invalid monitor indexes as non-existent screens
147 0 : if (nMonitor < 0 || (pScreen && nMonitor >= gdk_screen_get_n_monitors (pScreen)))
148 0 : pScreen = NULL;
149 :
150 0 : return pScreen;
151 : }
152 :
153 : int
154 0 : GtkSalSystem::getScreenIdxFromPtr (GdkScreen *pScreen)
155 : {
156 0 : int nIdx = 0;
157 0 : for (ScreenMonitors_t::const_iterator aIt(maScreenMonitors.begin()), aEnd(maScreenMonitors.end()); aIt != aEnd; ++aIt)
158 : {
159 0 : if (aIt->first == pScreen)
160 0 : return nIdx;
161 0 : nIdx += aIt->second;
162 : }
163 0 : g_warning ("failed to find screen %p", pScreen);
164 0 : return 0;
165 : }
166 :
167 0 : int GtkSalSystem::getScreenMonitorIdx (GdkScreen *pScreen,
168 : int nX, int nY)
169 : {
170 : // TODO: this will fail horribly for exotic combinations like two
171 : // monitors in mirror mode and one extra. Hopefully such
172 : // abominations are not used (or, even better, not possible) in
173 : // practice .-)
174 0 : return getScreenIdxFromPtr (pScreen) +
175 0 : gdk_screen_get_monitor_at_point (pScreen, nX, nY);
176 : }
177 :
178 0 : unsigned int GtkSalSystem::GetDisplayScreenCount()
179 : {
180 : gint nMonitor;
181 0 : (void)getScreenMonitorFromIdx (G_MAXINT, nMonitor);
182 0 : return G_MAXINT - nMonitor;
183 : }
184 :
185 0 : bool GtkSalSystem::IsUnifiedDisplay()
186 : {
187 0 : return gdk_display_get_n_screens (mpDisplay) == 1;
188 : }
189 :
190 : namespace {
191 : #if GTK_CHECK_VERSION(2,14,0)
192 0 : static int _fallback_get_primary_monitor (GdkScreen *pScreen)
193 : {
194 : // Use monitor name as primacy heuristic
195 0 : int max = gdk_screen_get_n_monitors (pScreen);
196 0 : for (int i = 0; i < max; ++i)
197 : {
198 0 : char *name = gdk_screen_get_monitor_plug_name (pScreen, i);
199 0 : bool bLaptop = (name && !g_ascii_strncasecmp (name, "LVDS", 4));
200 0 : g_free (name);
201 0 : if (bLaptop)
202 0 : return i;
203 : }
204 0 : return 0;
205 : }
206 : #endif
207 :
208 0 : static int _get_primary_monitor (GdkScreen *pScreen)
209 : {
210 : static int (*get_fn) (GdkScreen *) = NULL;
211 : #if GTK_CHECK_VERSION(3,0,0)
212 : get_fn = gdk_screen_get_primary_monitor;
213 : #endif
214 : // Perhaps we have a newer gtk+ with this symbol:
215 0 : if (!get_fn)
216 : {
217 : get_fn = (int(*)(GdkScreen*))osl_getAsciiFunctionSymbol(NULL,
218 0 : "gdk_screen_get_primary_monitor");
219 : }
220 : #if GTK_CHECK_VERSION(2,14,0)
221 0 : if (!get_fn)
222 0 : get_fn = _fallback_get_primary_monitor;
223 : #endif
224 0 : if (get_fn)
225 0 : return get_fn (pScreen);
226 : else
227 0 : return 0;
228 : }
229 : } // end anonymous namespace
230 :
231 0 : unsigned int GtkSalSystem::GetDisplayBuiltInScreen()
232 : {
233 0 : GdkScreen *pDefault = gdk_display_get_default_screen (mpDisplay);
234 0 : int idx = getScreenIdxFromPtr (pDefault);
235 0 : return idx + _get_primary_monitor (pDefault);
236 : }
237 :
238 0 : Rectangle GtkSalSystem::GetDisplayScreenPosSizePixel (unsigned int nScreen)
239 : {
240 : gint nMonitor;
241 : GdkScreen *pScreen;
242 : GdkRectangle aRect;
243 0 : pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
244 0 : if (!pScreen)
245 0 : return Rectangle();
246 0 : gdk_screen_get_monitor_geometry (pScreen, nMonitor, &aRect);
247 0 : return Rectangle (Point(aRect.x, aRect.y), Size(aRect.width, aRect.height));
248 : }
249 :
250 0 : OUString GtkSalSystem::GetDisplayScreenName(unsigned int nScreen)
251 : {
252 : gchar *pStr;
253 : gint nMonitor;
254 : GdkScreen *pScreen;
255 0 : pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
256 0 : if (!pScreen)
257 0 : return OUString();
258 :
259 : #if GTK_CHECK_VERSION(3,0,0) || GTK_CHECK_VERSION(2,14,0)
260 0 : pStr = gdk_screen_get_monitor_plug_name (pScreen, nMonitor);
261 : #else
262 : static gchar * (*get_fn) (GdkScreen *, int) = NULL;
263 :
264 : GModule *module = g_module_open (NULL, (GModuleFlags) 0);
265 : if (!g_module_symbol (module, "gdk_screen_get_monitor_plug_name",
266 : (gpointer *)&get_fn))
267 : get_fn = NULL;
268 : g_module_close (module);
269 :
270 : if (get_fn)
271 : pStr = get_fn (pScreen, nMonitor);
272 : else
273 : pStr = g_strdup_printf ("%d", nScreen);
274 : #endif
275 0 : OUString aRet (pStr, strlen (pStr), RTL_TEXTENCODING_UTF8);
276 0 : g_free (pStr);
277 0 : return aRet;
278 : }
279 :
280 : // convert ~ to indicate mnemonic to '_'
281 0 : static OString MapToGtkAccelerator(const OUString &rStr)
282 : {
283 0 : return OUStringToOString(rStr.replaceFirst("~", "_"), RTL_TEXTENCODING_UTF8);
284 : }
285 :
286 0 : int GtkSalSystem::ShowNativeDialog (const OUString& rTitle, const OUString& rMessage,
287 : const std::list< OUString >& rButtonNames,
288 : int nDefaultButton)
289 : {
290 0 : OString aTitle (OUStringToOString (rTitle, RTL_TEXTENCODING_UTF8));
291 0 : OString aMessage (OUStringToOString (rMessage, RTL_TEXTENCODING_UTF8));
292 :
293 0 : GtkDialog *pDialog = GTK_DIALOG (
294 : g_object_new (GTK_TYPE_MESSAGE_DIALOG,
295 : "title", aTitle.getStr(),
296 : "message-type", (int)GTK_MESSAGE_WARNING,
297 : "text", aMessage.getStr(),
298 : NULL));
299 0 : int nButton = 0;
300 0 : std::list< OUString >::const_iterator it;
301 0 : for (it = rButtonNames.begin(); it != rButtonNames.end(); ++it)
302 0 : gtk_dialog_add_button (pDialog, MapToGtkAccelerator(*it).getStr(), nButton++);
303 0 : gtk_dialog_set_default_response (pDialog, nDefaultButton);
304 :
305 0 : nButton = gtk_dialog_run (pDialog);
306 0 : if (nButton < 0)
307 0 : nButton = -1;
308 :
309 0 : gtk_widget_destroy (GTK_WIDGET (pDialog));
310 :
311 0 : return nButton;
312 0 : }
313 :
314 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|