LCOV - code coverage report
Current view: top level - vcl/unx/gtk/app - gtksys.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 67 119 56.3 %
Date: 2015-06-13 12:38:46 Functions: 18 23 78.3 %
Legend: Lines: hit not hit

          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           5 : GtkSalSystem *GtkSalSystem::GetSingleton()
      27             : {
      28             :     static GtkSalSystem *pSingleton = NULL;
      29           5 :     if (!pSingleton)
      30           3 :         pSingleton = new GtkSalSystem();
      31           5 :     return pSingleton;
      32             : }
      33             : 
      34           2 : SalSystem *GtkInstance::CreateSalSystem()
      35             : {
      36           2 :     return GtkSalSystem::GetSingleton();
      37             : }
      38             : 
      39           3 : GtkSalSystem::GtkSalSystem() : SalGenericSystem()
      40             : {
      41           3 :     mpDisplay = gdk_display_get_default();
      42           3 :     countScreenMonitors();
      43           3 : }
      44             : 
      45           2 : GtkSalSystem::~GtkSalSystem()
      46             : {
      47           2 : }
      48             : 
      49             : int
      50           3 : GtkSalSystem::GetDisplayXScreenCount()
      51             : {
      52           3 :     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 gtk+'s rather more
      77             :  * complicated screen + monitor concept.
      78             :  */
      79             : void
      80           9 : GtkSalSystem::countScreenMonitors()
      81             : {
      82           9 :     maScreenMonitors.clear();
      83          18 :     for (gint i = 0; i < gdk_display_get_n_screens(mpDisplay); i++)
      84             :     {
      85           9 :         GdkScreen* const pScreen(gdk_display_get_screen(mpDisplay, i));
      86           9 :         gint nMonitors(pScreen ? gdk_screen_get_n_monitors(pScreen) : 0);
      87           9 :         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           9 :         maScreenMonitors.push_back(std::make_pair(pScreen, nMonitors));
     104             :     }
     105           9 : }
     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();
     111             : #endif
     112             :     int   gdk_x11_screen_get_screen_number (GdkScreen *screen);
     113             : }
     114             : 
     115             : SalX11Screen
     116          16 : GtkSalSystem::getXScreenFromDisplayScreen(unsigned int nScreen)
     117             : {
     118             :     gint nMonitor;
     119             : 
     120          16 :     GdkScreen *pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
     121          16 :     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          16 :     return SalX11Screen (gdk_x11_screen_get_screen_number (pScreen));
     128             : }
     129             : 
     130             : GdkScreen *
     131          76 : GtkSalSystem::getScreenMonitorFromIdx (int nIdx, gint &nMonitor)
     132             : {
     133          76 :     GdkScreen *pScreen = NULL;
     134          98 :     for (ScreenMonitors_t::const_iterator aIt(maScreenMonitors.begin()), aEnd(maScreenMonitors.end()); aIt != aEnd; ++aIt)
     135             :     {
     136          76 :         pScreen = aIt->first;
     137          76 :         if (!pScreen)
     138           0 :             break;
     139          76 :         if (nIdx >= aIt->second)
     140          22 :             nIdx -= aIt->second;
     141             :         else
     142          54 :             break;
     143             :     }
     144          76 :     nMonitor = nIdx;
     145             : 
     146             :     // handle invalid monitor indexes as non-existent screens
     147          76 :     if (nMonitor < 0 || (pScreen && nMonitor >= gdk_screen_get_n_monitors (pScreen)))
     148          22 :         pScreen = NULL;
     149             : 
     150          76 :     return pScreen;
     151             : }
     152             : 
     153             : int
     154          41 : GtkSalSystem::getScreenIdxFromPtr (GdkScreen *pScreen)
     155             : {
     156          41 :     int nIdx = 0;
     157          41 :     for (ScreenMonitors_t::const_iterator aIt(maScreenMonitors.begin()), aEnd(maScreenMonitors.end()); aIt != aEnd; ++aIt)
     158             :     {
     159          41 :         if (aIt->first == pScreen)
     160          41 :             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          25 : 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          25 :     return getScreenIdxFromPtr (pScreen) +
     175          25 :         gdk_screen_get_monitor_at_point (pScreen, nX, nY);
     176             : }
     177             : 
     178          22 : unsigned int GtkSalSystem::GetDisplayScreenCount()
     179             : {
     180             :     gint nMonitor;
     181          22 :     (void)getScreenMonitorFromIdx (G_MAXINT, nMonitor);
     182          22 :     return G_MAXINT - nMonitor;
     183             : }
     184             : 
     185          22 : bool GtkSalSystem::IsUnifiedDisplay()
     186             : {
     187          22 :     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          16 : 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          16 :     if (!get_fn)
     216             :     {
     217             :         get_fn = reinterpret_cast<int(*)(GdkScreen*)>(osl_getAsciiFunctionSymbol(NULL,
     218           3 :             "gdk_screen_get_primary_monitor"));
     219             :     }
     220             : #if GTK_CHECK_VERSION(2,14,0)
     221          16 :     if (!get_fn)
     222           0 :         get_fn = _fallback_get_primary_monitor;
     223             : #endif
     224          16 :     if (get_fn)
     225          16 :         return get_fn (pScreen);
     226             :     else
     227           0 :         return 0;
     228             : }
     229             : } // end anonymous namespace
     230             : 
     231          16 : unsigned int GtkSalSystem::GetDisplayBuiltInScreen()
     232             : {
     233          16 :     GdkScreen *pDefault = gdk_display_get_default_screen (mpDisplay);
     234          16 :     int idx = getScreenIdxFromPtr (pDefault);
     235          16 :     return idx + _get_primary_monitor (pDefault);
     236             : }
     237             : 
     238          38 : Rectangle GtkSalSystem::GetDisplayScreenPosSizePixel (unsigned int nScreen)
     239             : {
     240             :     gint nMonitor;
     241             :     GdkScreen *pScreen;
     242             :     GdkRectangle aRect;
     243          38 :     pScreen = getScreenMonitorFromIdx (nScreen, nMonitor);
     244          38 :     if (!pScreen)
     245           0 :         return Rectangle();
     246          38 :     gdk_screen_get_monitor_geometry (pScreen, nMonitor, &aRect);
     247          38 :     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           9 : }
     313             : 
     314             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11