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 : #if GTK_CHECK_VERSION(3,0,0)
111 : GType gdk_x11_display_get_type (void);
112 : #endif
113 : int gdk_x11_screen_get_screen_number (GdkScreen *screen);
114 : }
115 :
116 : SalX11Screen
117 0 : GtkSalSystem::getXScreenFromDisplayScreen(unsigned int nScreen)
118 : {
119 : gint nMonitor;
120 :
121 0 : GdkScreen *pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
122 0 : if (!pScreen)
123 0 : return SalX11Screen (0);
124 : #if GTK_CHECK_VERSION(3,0,0)
125 : if (!G_TYPE_CHECK_INSTANCE_TYPE (mpDisplay, gdk_x11_display_get_type ()))
126 : return SalX11Screen (0);
127 : #endif
128 0 : return SalX11Screen (gdk_x11_screen_get_screen_number (pScreen));
129 : }
130 :
131 : GdkScreen *
132 0 : GtkSalSystem::getScreenMonitorFromIdx (int nIdx, gint &nMonitor)
133 : {
134 0 : GdkScreen *pScreen = NULL;
135 0 : for (ScreenMonitors_t::const_iterator aIt(maScreenMonitors.begin()), aEnd(maScreenMonitors.end()); aIt != aEnd; ++aIt)
136 : {
137 0 : pScreen = aIt->first;
138 0 : if (!pScreen)
139 0 : break;
140 0 : if (nIdx >= aIt->second)
141 0 : nIdx -= aIt->second;
142 : else
143 0 : break;
144 : }
145 0 : nMonitor = nIdx;
146 :
147 : // handle invalid monitor indexes as non-existent screens
148 0 : if (nMonitor < 0 || (pScreen && nMonitor >= gdk_screen_get_n_monitors (pScreen)))
149 0 : pScreen = NULL;
150 :
151 0 : return pScreen;
152 : }
153 :
154 : int
155 0 : GtkSalSystem::getScreenIdxFromPtr (GdkScreen *pScreen)
156 : {
157 0 : int nIdx = 0;
158 0 : for (ScreenMonitors_t::const_iterator aIt(maScreenMonitors.begin()), aEnd(maScreenMonitors.end()); aIt != aEnd; ++aIt)
159 : {
160 0 : if (aIt->first == pScreen)
161 0 : return nIdx;
162 0 : nIdx += aIt->second;
163 : }
164 0 : g_warning ("failed to find screen %p", pScreen);
165 0 : return 0;
166 : }
167 :
168 0 : int GtkSalSystem::getScreenMonitorIdx (GdkScreen *pScreen,
169 : int nX, int nY)
170 : {
171 : // TODO: this will fail horribly for exotic combinations like two
172 : // monitors in mirror mode and one extra. Hopefully such
173 : // abominations are not used (or, even better, not possible) in
174 : // practice .-)
175 0 : return getScreenIdxFromPtr (pScreen) +
176 0 : gdk_screen_get_monitor_at_point (pScreen, nX, nY);
177 : }
178 :
179 0 : unsigned int GtkSalSystem::GetDisplayScreenCount()
180 : {
181 : gint nMonitor;
182 0 : (void)getScreenMonitorFromIdx (G_MAXINT, nMonitor);
183 0 : return G_MAXINT - nMonitor;
184 : }
185 :
186 0 : bool GtkSalSystem::IsUnifiedDisplay()
187 : {
188 0 : return gdk_display_get_n_screens (mpDisplay) == 1;
189 : }
190 :
191 : namespace {
192 : #if GTK_CHECK_VERSION(2,14,0)
193 0 : static int _fallback_get_primary_monitor (GdkScreen *pScreen)
194 : {
195 : // Use monitor name as primacy heuristic
196 0 : int max = gdk_screen_get_n_monitors (pScreen);
197 0 : for (int i = 0; i < max; ++i)
198 : {
199 0 : char *name = gdk_screen_get_monitor_plug_name (pScreen, i);
200 0 : bool bLaptop = (name && !g_ascii_strncasecmp (name, "LVDS", 4));
201 0 : g_free (name);
202 0 : if (bLaptop)
203 0 : return i;
204 : }
205 0 : return 0;
206 : }
207 : #endif
208 :
209 0 : static int _get_primary_monitor (GdkScreen *pScreen)
210 : {
211 : static int (*get_fn) (GdkScreen *) = NULL;
212 : #if GTK_CHECK_VERSION(3,0,0)
213 : get_fn = gdk_screen_get_primary_monitor;
214 : #endif
215 : // Perhaps we have a newer gtk+ with this symbol:
216 0 : if (!get_fn)
217 : {
218 : get_fn = (int(*)(GdkScreen*))osl_getAsciiFunctionSymbol(NULL,
219 0 : "gdk_screen_get_primary_monitor");
220 : }
221 : #if GTK_CHECK_VERSION(2,14,0)
222 0 : if (!get_fn)
223 0 : get_fn = _fallback_get_primary_monitor;
224 : #endif
225 0 : if (get_fn)
226 0 : return get_fn (pScreen);
227 : else
228 0 : return 0;
229 : }
230 : } // end anonymous namespace
231 :
232 0 : unsigned int GtkSalSystem::GetDisplayBuiltInScreen()
233 : {
234 0 : GdkScreen *pDefault = gdk_display_get_default_screen (mpDisplay);
235 0 : int idx = getScreenIdxFromPtr (pDefault);
236 0 : return idx + _get_primary_monitor (pDefault);
237 : }
238 :
239 0 : Rectangle GtkSalSystem::GetDisplayScreenPosSizePixel (unsigned int nScreen)
240 : {
241 : gint nMonitor;
242 : GdkScreen *pScreen;
243 : GdkRectangle aRect;
244 0 : pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
245 0 : if (!pScreen)
246 0 : return Rectangle();
247 0 : gdk_screen_get_monitor_geometry (pScreen, nMonitor, &aRect);
248 0 : return Rectangle (Point(aRect.x, aRect.y), Size(aRect.width, aRect.height));
249 : }
250 :
251 0 : OUString GtkSalSystem::GetDisplayScreenName(unsigned int nScreen)
252 : {
253 : gchar *pStr;
254 : gint nMonitor;
255 : GdkScreen *pScreen;
256 0 : pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
257 0 : if (!pScreen)
258 0 : return OUString();
259 :
260 : #if GTK_CHECK_VERSION(3,0,0) || GTK_CHECK_VERSION(2,14,0)
261 0 : pStr = gdk_screen_get_monitor_plug_name (pScreen, nMonitor);
262 : #else
263 : static gchar * (*get_fn) (GdkScreen *, int) = NULL;
264 :
265 : GModule *module = g_module_open (NULL, (GModuleFlags) 0);
266 : if (!g_module_symbol (module, "gdk_screen_get_monitor_plug_name",
267 : (gpointer *)&get_fn))
268 : get_fn = NULL;
269 : g_module_close (module);
270 :
271 : if (get_fn)
272 : pStr = get_fn (pScreen, nMonitor);
273 : else
274 : pStr = g_strdup_printf ("%d", nScreen);
275 : #endif
276 0 : OUString aRet (pStr, strlen (pStr), RTL_TEXTENCODING_UTF8);
277 0 : g_free (pStr);
278 0 : return aRet;
279 : }
280 :
281 : // convert ~ to indicate mnemonic to '_'
282 0 : static OString MapToGtkAccelerator(const OUString &rStr)
283 : {
284 0 : return OUStringToOString(rStr.replaceFirst("~", "_"), RTL_TEXTENCODING_UTF8);
285 : }
286 :
287 0 : int GtkSalSystem::ShowNativeDialog (const OUString& rTitle, const OUString& rMessage,
288 : const std::list< OUString >& rButtonNames,
289 : int nDefaultButton)
290 : {
291 0 : OString aTitle (OUStringToOString (rTitle, RTL_TEXTENCODING_UTF8));
292 0 : OString aMessage (OUStringToOString (rMessage, RTL_TEXTENCODING_UTF8));
293 :
294 0 : GtkDialog *pDialog = GTK_DIALOG (
295 : g_object_new (GTK_TYPE_MESSAGE_DIALOG,
296 : "title", aTitle.getStr(),
297 : "message-type", (int)GTK_MESSAGE_WARNING,
298 : "text", aMessage.getStr(),
299 : NULL));
300 0 : int nButton = 0;
301 0 : std::list< OUString >::const_iterator it;
302 0 : for (it = rButtonNames.begin(); it != rButtonNames.end(); ++it)
303 0 : gtk_dialog_add_button (pDialog, MapToGtkAccelerator(*it).getStr(), nButton++);
304 0 : gtk_dialog_set_default_response (pDialog, nDefaultButton);
305 :
306 0 : nButton = gtk_dialog_run (pDialog);
307 0 : if (nButton < 0)
308 0 : nButton = -1;
309 :
310 0 : gtk_widget_destroy (GTK_WIDGET (pDialog));
311 :
312 0 : return nButton;
313 0 : }
314 :
315 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|