summaryrefslogtreecommitdiff
path: root/vcl/inc/unx/gtk/gtkdata.hxx
blob: cf1454b6f589edd675def523499f770abfc5a2f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_VCL_INC_UNX_GTK_GTKDATA_HXX
#define INCLUDED_VCL_INC_UNX_GTK_GTKDATA_HXX

#define GLIB_DISABLE_DEPRECATION_WARNINGS
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#if GTK_CHECK_VERSION(4,0,0)
#include <gdk/x11/gdkx.h>
#else
#include <gdk/gdkx.h>
#endif

#include <com/sun/star/accessibility/XAccessibleContext.hpp>
#include <com/sun/star/accessibility/XAccessibleEventListener.hpp>
#include <unx/gendata.hxx>
#include <unx/saldisp.hxx>
#include <unx/gtk/gtksys.hxx>
#include <vcl/ptrstyle.hxx>
#include <osl/conditn.hxx>
#include <saltimer.hxx>
#include <o3tl/enumarray.hxx>

#include <vector>

namespace com::sun::star::accessibility { class XAccessibleEventListener; }

class GtkSalDisplay;
class DocumentFocusListener;

#if !GTK_CHECK_VERSION(4,0,0)
typedef GdkWindow GdkSurface;
typedef GdkWindowState GdkToplevelState;
#endif

inline void main_loop_run(GMainLoop* pLoop)
{
#if !GTK_CHECK_VERSION(4, 0, 0)
    gdk_threads_leave();
#endif
    g_main_loop_run(pLoop);
#if !GTK_CHECK_VERSION(4, 0, 0)
    gdk_threads_enter();
#endif
}

inline void css_provider_load_from_data(GtkCssProvider *css_provider,
                                        const gchar *data,
                                        gssize length)
{
#if GTK_CHECK_VERSION(4, 0, 0)
    gtk_css_provider_load_from_data(css_provider, data, length);
#else
    gtk_css_provider_load_from_data(css_provider, data, length, nullptr);
#endif
}

inline GtkWidget* widget_get_root(GtkWidget* pWidget)
{
#if GTK_CHECK_VERSION(4, 0, 0)
    return pWidget ? GTK_WIDGET(gtk_widget_get_root(pWidget)) : nullptr;
#else
    return gtk_widget_get_toplevel(pWidget);
#endif
}

inline const char* image_get_icon_name(GtkImage *pImage)
{
#if GTK_CHECK_VERSION(4, 0, 0)
    return gtk_image_get_icon_name(pImage);
#else
    const gchar* icon_name;
    gtk_image_get_icon_name(pImage, &icon_name, nullptr);
    return icon_name;
#endif
}

inline GtkWidget* widget_get_first_child(GtkWidget *pWidget)
{
#if GTK_CHECK_VERSION(4, 0, 0)
    return gtk_widget_get_first_child(pWidget);
#else
    GList* pChildren = gtk_container_get_children(GTK_CONTAINER(pWidget));
    GList* pChild = g_list_first(pChildren);
    GtkWidget* pRet = pChild ? static_cast<GtkWidget*>(pChild->data) : nullptr;
    g_list_free(pChildren);
    return pRet;
#endif
}

inline void style_context_get_color(GtkStyleContext *pStyle, GdkRGBA *pColor)
{
#if GTK_CHECK_VERSION(4, 0, 0)
    return gtk_style_context_get_color(pStyle, pColor);
#else
    return gtk_style_context_get_color(pStyle, gtk_style_context_get_state(pStyle), pColor);
#endif
}

inline GdkSurface* widget_get_surface(GtkWidget* pWidget)
{
#if GTK_CHECK_VERSION(4,0,0)
    return gtk_native_get_surface(gtk_widget_get_native(pWidget));
#else
    return gtk_widget_get_window(pWidget);
#endif
}

inline void widget_set_cursor(GtkWidget *pWidget, GdkCursor *pCursor)
{
#if GTK_CHECK_VERSION(4, 0, 0)
    gtk_widget_set_cursor(pWidget, pCursor);
#else
    gdk_window_set_cursor(gtk_widget_get_window(pWidget), pCursor);
#endif
}

inline cairo_surface_t * surface_create_similar_surface(GdkSurface *pSurface,
                                                        cairo_content_t eContent,
                                                        int nWidth,
                                                        int nHeight)
{
#if GTK_CHECK_VERSION(4, 0, 0)
    return gdk_surface_create_similar_surface(pSurface, eContent, nWidth, nHeight);
#else
    return gdk_window_create_similar_surface(pSurface, eContent, nWidth, nHeight);
#endif
}

#if GTK_CHECK_VERSION(4, 0, 0)
typedef double gtk_coord;
#else
typedef int gtk_coord;
#endif

inline bool surface_get_device_position(GdkSurface* pSurface,
                                        GdkDevice* pDevice,
                                        double& x,
                                        double& y,
                                        GdkModifierType* pMask)
{
#if GTK_CHECK_VERSION(4, 0, 0)
    return gdk_surface_get_device_position(pSurface, pDevice,
                                           &x, &y,
                                           pMask);
#else
    int nX(x), nY(y);
    return gdk_window_get_device_position(pSurface, pDevice,
                                           &nX, &nY,
                                           pMask);
    x = nX;
    y = nY;
#endif
}

#if !GTK_CHECK_VERSION(4, 0, 0)
typedef GtkClipboard GdkClipboard;
#endif

class GtkSalTimer final : public SalTimer
{
    struct SalGtkTimeoutSource *m_pTimeout;
public:
    GtkSalTimer();
    virtual ~GtkSalTimer() override;
    virtual void Start( sal_uInt64 nMS ) override;
    virtual void Stop() override;
    bool         Expired();

    sal_uLong    m_nTimeoutMS;
};

class DocumentFocusListener :
    public ::cppu::WeakImplHelper< css::accessibility::XAccessibleEventListener >
{

    o3tl::sorted_vector< css::uno::Reference< css::uno::XInterface > > m_aRefList;

public:
    /// @throws lang::IndexOutOfBoundsException
    /// @throws uno::RuntimeException
    void attachRecursive(
        const css::uno::Reference< css::accessibility::XAccessible >& xAccessible
    );

    /// @throws lang::IndexOutOfBoundsException
    /// @throws uno::RuntimeException
    void attachRecursive(
        const css::uno::Reference< css::accessibility::XAccessible >& xAccessible,
        const css::uno::Reference< css::accessibility::XAccessibleContext >& xContext
    );

    /// @throws lang::IndexOutOfBoundsException
    /// @throws uno::RuntimeException
    void attachRecursive(
        const css::uno::Reference< css::accessibility::XAccessible >& xAccessible,
        const css::uno::Reference< css::accessibility::XAccessibleContext >& xContext,
        const css::uno::Reference< css::accessibility::XAccessibleStateSet >& xStateSet
    );

    /// @throws lang::IndexOutOfBoundsException
    /// @throws uno::RuntimeException
    void detachRecursive(
        const css::uno::Reference< css::accessibility::XAccessible >& xAccessible
    );

    /// @throws lang::IndexOutOfBoundsException
    /// @throws uno::RuntimeException
    void detachRecursive(
        const css::uno::Reference< css::accessibility::XAccessibleContext >& xContext
    );

    /// @throws lang::IndexOutOfBoundsException
    /// @throws uno::RuntimeException
    void detachRecursive(
        const css::uno::Reference< css::accessibility::XAccessibleContext >& xContext,
        const css::uno::Reference< css::accessibility::XAccessibleStateSet >& xStateSet
    );

    /// @throws lang::IndexOutOfBoundsException
    /// @throws uno::RuntimeException
    static css::uno::Reference< css::accessibility::XAccessible > getAccessible(const css::lang::EventObject& aEvent );

    // XEventListener
    virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;

    // XAccessibleEventListener
    virtual void SAL_CALL notifyEvent( const css::accessibility::AccessibleEventObject& aEvent ) override;
};

class GtkSalData final : public GenericUnixSalData
{
    GSource*        m_pUserEvent;
    osl::Mutex      m_aDispatchMutex;
    osl::Condition  m_aDispatchCondition;
    std::exception_ptr m_aException;

    rtl::Reference<DocumentFocusListener> m_xDocumentFocusListener;

public:
    GtkSalData( SalInstance *pInstance );
    virtual ~GtkSalData() override;

    DocumentFocusListener & GetDocumentFocusListener();

    void Init();
    virtual void Dispose() override;

    static void initNWF();
    static void deInitNWF();

    void TriggerUserEventProcessing();
    void TriggerAllUserEventsProcessed();

    bool Yield( bool bWait, bool bHandleAllCurrentEvents );
    inline GdkDisplay *GetGdkDisplay();

    virtual void ErrorTrapPush() override;
    virtual bool ErrorTrapPop( bool bIgnoreError = true ) override;

    inline GtkSalDisplay *GetGtkDisplay() const;
    void setException(const std::exception_ptr& exception) { m_aException = exception; }
};

class GtkSalFrame;

class GtkSalDisplay : public SalGenericDisplay
{
    GtkSalSystem*                   m_pSys;
    GdkDisplay*                     m_pGdkDisplay;
    o3tl::enumarray<PointerStyle, GdkCursor*> m_aCursors;
    bool                            m_bStartupCompleted;

    GdkCursor* getFromSvg( OUString const & name, int nXHot, int nYHot );

public:
             GtkSalDisplay( GdkDisplay* pDisplay );
    virtual ~GtkSalDisplay() override;

    GdkDisplay* GetGdkDisplay() const { return m_pGdkDisplay; }

    GtkSalSystem* getSystem() const { return m_pSys; }

    GtkWidget* findGtkWidgetForNativeHandle(sal_uIntPtr hWindow) const;

    virtual void deregisterFrame( SalFrame* pFrame ) override;
    GdkCursor *getCursor( PointerStyle ePointerStyle );
    virtual int CaptureMouse( SalFrame* pFrame );

    SalX11Screen GetDefaultXScreen() { return m_pSys->GetDisplayDefaultXScreen(); }
    Size         GetScreenSize( int nDisplayScreen );

    void startupNotificationCompleted() { m_bStartupCompleted = true; }

#if !GTK_CHECK_VERSION(4,0,0)
    void screenSizeChanged( GdkScreen const * );
    void monitorsChanged( GdkScreen const * );
#endif

    virtual void TriggerUserEventProcessing() override;
    virtual void TriggerAllUserEventsProcessed() override;
};

inline GtkSalData* GetGtkSalData()
{
    return static_cast<GtkSalData*>(ImplGetSVData()->mpSalData);
}
inline GdkDisplay *GtkSalData::GetGdkDisplay()
{
    return GetGtkDisplay()->GetGdkDisplay();
}

GtkSalDisplay *GtkSalData::GetGtkDisplay() const
{
    return static_cast<GtkSalDisplay *>(GetDisplay());
}

#endif // INCLUDED_VCL_INC_UNX_GTK_GTKDATA_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */