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