LibreOffice Module vcl (master)  1
gtkdata.hxx
Go to the documentation of this file.
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 #ifndef INCLUDED_VCL_INC_UNX_GTK_GTKDATA_HXX
21 #define INCLUDED_VCL_INC_UNX_GTK_GTKDATA_HXX
22 
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
24 #include <gtk/gtk.h>
25 #include <gdk/gdk.h>
26 #if GTK_CHECK_VERSION(4,0,0)
27 #include <gdk/x11/gdkx.h>
28 #else
29 #include <gdk/gdkx.h>
30 #endif
31 
32 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
33 #include <com/sun/star/accessibility/XAccessibleEventListener.hpp>
34 #include <unx/gendata.hxx>
35 #include <unx/saldisp.hxx>
36 #include <unx/gtk/gtksys.hxx>
37 #include <vcl/ptrstyle.hxx>
38 #include <osl/conditn.hxx>
39 #include <saltimer.hxx>
40 #include <o3tl/enumarray.hxx>
41 
42 #include <string_view>
43 #include <vector>
44 
45 namespace com::sun::star::accessibility { class XAccessibleEventListener; }
46 
47 class GtkSalDisplay;
49 
50 #if !GTK_CHECK_VERSION(4,0,0)
51 typedef GdkWindow GdkSurface;
52 typedef GdkWindowState GdkToplevelState;
53 #endif
54 
55 inline void main_loop_run(GMainLoop* pLoop)
56 {
57 #if !GTK_CHECK_VERSION(4, 0, 0)
58  gdk_threads_leave();
59 #endif
60  g_main_loop_run(pLoop);
61 #if !GTK_CHECK_VERSION(4, 0, 0)
62  gdk_threads_enter();
63 #endif
64 }
65 
66 inline void css_provider_load_from_data(GtkCssProvider *css_provider,
67  const gchar *data,
68  gssize length)
69 {
70 #if GTK_CHECK_VERSION(4, 0, 0)
71  gtk_css_provider_load_from_data(css_provider, data, length);
72 #else
73  gtk_css_provider_load_from_data(css_provider, data, length, nullptr);
74 #endif
75 }
76 
77 inline GtkWidget* widget_get_toplevel(GtkWidget* pWidget)
78 {
79 #if GTK_CHECK_VERSION(4, 0, 0)
80  GtkRoot* pRoot = gtk_widget_get_root(pWidget);
81  return pRoot ? GTK_WIDGET(pRoot) : pWidget;
82 #else
83  return gtk_widget_get_toplevel(pWidget);
84 #endif
85 }
86 
87 inline const char* image_get_icon_name(GtkImage *pImage)
88 {
89 #if GTK_CHECK_VERSION(4, 0, 0)
90  return gtk_image_get_icon_name(pImage);
91 #else
92  const gchar* icon_name;
93  gtk_image_get_icon_name(pImage, &icon_name, nullptr);
94  return icon_name;
95 #endif
96 }
97 
98 inline GtkWidget* widget_get_first_child(GtkWidget *pWidget)
99 {
100 #if GTK_CHECK_VERSION(4, 0, 0)
101  return gtk_widget_get_first_child(pWidget);
102 #else
103  GList* pChildren = gtk_container_get_children(GTK_CONTAINER(pWidget));
104  GList* pChild = g_list_first(pChildren);
105  GtkWidget* pRet = pChild ? static_cast<GtkWidget*>(pChild->data) : nullptr;
106  g_list_free(pChildren);
107  return pRet;
108 #endif
109 }
110 
111 inline void style_context_get_color(GtkStyleContext *pStyle, GdkRGBA *pColor)
112 {
113 #if GTK_CHECK_VERSION(4, 0, 0)
114  return gtk_style_context_get_color(pStyle, pColor);
115 #else
116  return gtk_style_context_get_color(pStyle, gtk_style_context_get_state(pStyle), pColor);
117 #endif
118 }
119 
120 inline GdkSurface* widget_get_surface(GtkWidget* pWidget)
121 {
122 #if GTK_CHECK_VERSION(4,0,0)
123  return gtk_native_get_surface(gtk_widget_get_native(pWidget));
124 #else
125  return gtk_widget_get_window(pWidget);
126 #endif
127 }
128 
129 inline void widget_set_cursor(GtkWidget *pWidget, GdkCursor *pCursor)
130 {
131 #if GTK_CHECK_VERSION(4, 0, 0)
132  gtk_widget_set_cursor(pWidget, pCursor);
133 #else
134  gdk_window_set_cursor(gtk_widget_get_window(pWidget), pCursor);
135 #endif
136 }
137 
139  cairo_content_t eContent,
140  int nWidth,
141  int nHeight)
142 {
143 #if GTK_CHECK_VERSION(4, 0, 0)
144  return gdk_surface_create_similar_surface(pSurface, eContent, nWidth, nHeight);
145 #else
146  return gdk_window_create_similar_surface(pSurface, eContent, nWidth, nHeight);
147 #endif
148 }
149 
150 inline void im_context_set_client_widget(GtkIMContext *pIMContext, GtkWidget *pWidget)
151 {
152 #if GTK_CHECK_VERSION(4, 0, 0)
153  gtk_im_context_set_client_widget(pIMContext, pWidget);
154 #else
155  gtk_im_context_set_client_window(pIMContext, pWidget ? gtk_widget_get_window(pWidget) : nullptr);
156 #endif
157 }
158 
159 #if GTK_CHECK_VERSION(4, 0, 0)
160 typedef double gtk_coord;
161 #else
162 typedef int gtk_coord;
163 #endif
164 
166  GdkDevice* pDevice,
167  double& x,
168  double& y,
169  GdkModifierType* pMask)
170 {
171 #if GTK_CHECK_VERSION(4, 0, 0)
172  return gdk_surface_get_device_position(pSurface, pDevice,
173  &x, &y,
174  pMask);
175 #else
176  int nX(x), nY(y);
177  bool bRet = gdk_window_get_device_position(pSurface, pDevice,
178  &nX, &nY,
179  pMask);
180  x = nX;
181  y = nY;
182  return bRet;
183 #endif
184 }
185 
186 inline GdkGLContext* surface_create_gl_context(GdkSurface* pSurface)
187 {
188 #if GTK_CHECK_VERSION(4, 0, 0)
189  return gdk_surface_create_gl_context(pSurface, nullptr);
190 #else
191  return gdk_window_create_gl_context(pSurface, nullptr);
192 #endif
193 }
194 
195 #if !GTK_CHECK_VERSION(4, 0, 0)
196 typedef GtkClipboard GdkClipboard;
197 #endif
198 
199 int getButtonPriority(std::string_view rType);
200 
201 class GtkSalTimer final : public SalTimer
202 {
203  struct SalGtkTimeoutSource *m_pTimeout;
204 public:
205  GtkSalTimer();
206  virtual ~GtkSalTimer() override;
207  virtual void Start( sal_uInt64 nMS ) override;
208  virtual void Stop() override;
209  bool Expired();
210 
212 };
213 
215  public ::cppu::WeakImplHelper< css::accessibility::XAccessibleEventListener >
216 {
217 
219 
220 public:
223  void attachRecursive(
224  const css::uno::Reference< css::accessibility::XAccessible >& xAccessible
225  );
226 
229  void attachRecursive(
230  const css::uno::Reference< css::accessibility::XAccessible >& xAccessible,
231  const css::uno::Reference< css::accessibility::XAccessibleContext >& xContext
232  );
233 
236  void attachRecursive(
237  const css::uno::Reference< css::accessibility::XAccessible >& xAccessible,
238  const css::uno::Reference< css::accessibility::XAccessibleContext >& xContext,
239  const css::uno::Reference< css::accessibility::XAccessibleStateSet >& xStateSet
240  );
241 
244  void detachRecursive(
245  const css::uno::Reference< css::accessibility::XAccessible >& xAccessible
246  );
247 
250  void detachRecursive(
251  const css::uno::Reference< css::accessibility::XAccessibleContext >& xContext
252  );
253 
256  void detachRecursive(
257  const css::uno::Reference< css::accessibility::XAccessibleContext >& xContext,
258  const css::uno::Reference< css::accessibility::XAccessibleStateSet >& xStateSet
259  );
260 
263  static css::uno::Reference< css::accessibility::XAccessible > getAccessible(const css::lang::EventObject& aEvent );
264 
265  // XEventListener
266  virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
267 
268  // XAccessibleEventListener
269  virtual void SAL_CALL notifyEvent( const css::accessibility::AccessibleEventObject& aEvent ) override;
270 };
271 
272 class GtkSalData final : public GenericUnixSalData
273 {
274  GSource* m_pUserEvent;
275  osl::Mutex m_aDispatchMutex;
276  osl::Condition m_aDispatchCondition;
277  std::exception_ptr m_aException;
278 
280 
281 public:
282  GtkSalData( SalInstance *pInstance );
283  virtual ~GtkSalData() override;
284 
286 
287  void Init();
288  virtual void Dispose() override;
289 
290  static void initNWF();
291  static void deInitNWF();
292 
295 
296  bool Yield( bool bWait, bool bHandleAllCurrentEvents );
297  inline GdkDisplay *GetGdkDisplay();
298 
299  virtual void ErrorTrapPush() override;
300  virtual bool ErrorTrapPop( bool bIgnoreError = true ) override;
301 
302  inline GtkSalDisplay *GetGtkDisplay() const;
303  void setException(const std::exception_ptr& exception) { m_aException = exception; }
304 };
305 
306 class GtkSalFrame;
307 
308 class GtkSalDisplay final : public SalGenericDisplay
309 {
311  GdkDisplay* m_pGdkDisplay;
314 
315  GdkCursor* getFromSvg( OUString const & name, int nXHot, int nYHot );
316 
317 public:
318  GtkSalDisplay( GdkDisplay* pDisplay );
319  virtual ~GtkSalDisplay() override;
320 
321  GdkDisplay* GetGdkDisplay() const { return m_pGdkDisplay; }
322 
323  GtkSalSystem* getSystem() const { return m_pSys; }
324 
325  GtkWidget* findGtkWidgetForNativeHandle(sal_uIntPtr hWindow) const;
326 
327  virtual void deregisterFrame( SalFrame* pFrame ) override;
328  GdkCursor *getCursor( PointerStyle ePointerStyle );
329  virtual int CaptureMouse( SalFrame* pFrame );
330 
332  Size GetScreenSize( int nDisplayScreen );
333 
334  void startupNotificationCompleted() { m_bStartupCompleted = true; }
335 
336 #if !GTK_CHECK_VERSION(4,0,0)
337  void screenSizeChanged( GdkScreen const * );
338  void monitorsChanged( GdkScreen const * );
339 #endif
340 
341  virtual void TriggerUserEventProcessing() override;
342  virtual void TriggerAllUserEventsProcessed() override;
343 };
344 
346 {
347  return static_cast<GtkSalData*>(ImplGetSVData()->mpSalData);
348 }
349 inline GdkDisplay *GtkSalData::GetGdkDisplay()
350 {
351  return GetGtkDisplay()->GetGdkDisplay();
352 }
353 
355 {
356  return static_cast<GtkSalDisplay *>(GetDisplay());
357 }
358 
359 #endif // INCLUDED_VCL_INC_UNX_GTK_GTKDATA_HXX
360 
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void deregisterFrame(SalFrame *pFrame) override
GdkGLContext * surface_create_gl_context(GdkSurface *pSurface)
Definition: gtkdata.hxx:186
static void initNWF()
osl::Condition m_aDispatchCondition
Definition: gtkdata.hxx:276
void TriggerUserEventProcessing()
SalX11Screen GetDisplayDefaultXScreen()
Definition: gtksys.hxx:36
SalData * mpSalData
Definition: svdata.hxx:384
o3tl::sorted_vector< css::uno::Reference< css::uno::XInterface > > m_aRefList
Definition: gtkdata.hxx:218
void screenSizeChanged(GdkScreen const *)
virtual ~GtkSalTimer() override
void TriggerAllUserEventsProcessed()
sal_uIntPtr sal_uLong
virtual ~GtkSalData() override
void widget_set_cursor(GtkWidget *pWidget, GdkCursor *pCursor)
Definition: gtkdata.hxx:129
bool m_bStartupCompleted
Definition: gtkdata.hxx:313
GtkSalDisplay * GetGtkDisplay() const
Definition: gtkdata.hxx:354
SalX11Screen GetDefaultXScreen()
Definition: gtkdata.hxx:331
void style_context_get_color(GtkStyleContext *pStyle, GdkRGBA *pColor)
Definition: gtkdata.hxx:111
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
SalGenericDisplay * GetDisplay() const
Definition: gendata.hxx:46
void main_loop_run(GMainLoop *pLoop)
Definition: gtkdata.hxx:55
virtual ~GtkSalDisplay() override
void startupNotificationCompleted()
Definition: gtkdata.hxx:334
static void deInitNWF()
GdkWindow GdkSurface
Definition: gtkdata.hxx:48
GtkClipboard GdkClipboard
Definition: gtkdata.hxx:196
void attachRecursive(const css::uno::Reference< css::accessibility::XAccessible > &xAccessible)
void css_provider_load_from_data(GtkCssProvider *css_provider, const gchar *data, gssize length)
Definition: gtkdata.hxx:66
int getButtonPriority(std::string_view rType)
int gtk_coord
Definition: gtkdata.hxx:162
virtual void TriggerAllUserEventsProcessed() override
virtual void Stop() override
GtkWidget * widget_get_first_child(GtkWidget *pWidget)
Definition: gtkdata.hxx:98
GdkDisplay * m_pGdkDisplay
Definition: gtkdata.hxx:311
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:75
GSource * m_pUserEvent
Definition: gtkdata.hxx:274
GtkSalData(SalInstance *pInstance)
GdkCursor * getFromSvg(OUString const &name, int nXHot, int nYHot)
GtkWidget * widget_get_toplevel(GtkWidget *pWidget)
Definition: gtkdata.hxx:77
GtkWidget * findGtkWidgetForNativeHandle(sal_uIntPtr hWindow) const
o3tl::enumarray< PointerStyle, GdkCursor * > m_aCursors
Definition: gtkdata.hxx:312
void im_context_set_client_widget(GtkIMContext *pIMContext, GtkWidget *pWidget)
Definition: gtkdata.hxx:150
virtual void ErrorTrapPush() override
GdkWindowState GdkToplevelState
Definition: gtkdata.hxx:52
sal_uLong m_nTimeoutMS
Definition: gtkdata.hxx:211
GtkSalData * GetGtkSalData()
Definition: gtkdata.hxx:345
cairo_surface_t * surface_create_similar_surface(GdkSurface *pSurface, cairo_content_t eContent, int nWidth, int nHeight)
Definition: gtkdata.hxx:138
virtual void Start(sal_uInt64 nMS) override
GtkSalSystem * m_pSys
Definition: gtkdata.hxx:310
std::exception_ptr m_aException
Definition: gtkdata.hxx:277
void setException(const std::exception_ptr &exception)
Definition: gtkdata.hxx:303
void monitorsChanged(GdkScreen const *)
bool Expired()
bool surface_get_device_position(GdkSurface *pSurface, GdkDevice *pDevice, double &x, double &y, GdkModifierType *pMask)
Definition: gtkdata.hxx:165
GtkSalSystem * getSystem() const
Definition: gtkdata.hxx:323
A SalFrame is a system window (e.g. an X11 window).
Definition: salframe.hxx:114
GdkDisplay * GetGdkDisplay()
Definition: gtkdata.hxx:349
static css::uno::Reference< css::accessibility::XAccessible > getAccessible(const css::lang::EventObject &aEvent)
DocumentFocusListener & GetDocumentFocusListener()
virtual void TriggerUserEventProcessing() override
virtual bool ErrorTrapPop(bool bIgnoreError=true) override
GdkCursor * getCursor(PointerStyle ePointerStyle)
PointerStyle
Definition: ptrstyle.hxx:25
rtl::Reference< DocumentFocusListener > m_xDocumentFocusListener
Definition: gtkdata.hxx:279
void Init()
virtual void Dispose() override
virtual int CaptureMouse(SalFrame *pFrame)
struct SalGtkTimeoutSource * m_pTimeout
Definition: gtkdata.hxx:203
GdkDisplay * GetGdkDisplay() const
Definition: gtkdata.hxx:321
bool Yield(bool bWait, bool bHandleAllCurrentEvents)
Size GetScreenSize(int nDisplayScreen)
const char * image_get_icon_name(GtkImage *pImage)
Definition: gtkdata.hxx:87
GdkSurface * widget_get_surface(GtkWidget *pWidget)
Definition: gtkdata.hxx:120
struct _cairo_surface cairo_surface_t
Definition: svpgdi.hxx:71
osl::Mutex m_aDispatchMutex
Definition: gtkdata.hxx:275
GtkSalDisplay(GdkDisplay *pDisplay)
virtual void SAL_CALL notifyEvent(const css::accessibility::AccessibleEventObject &aEvent) override
void detachRecursive(const css::uno::Reference< css::accessibility::XAccessible > &xAccessible)