File: Gdk.lua

package info (click to toggle)
lua-lgi 0.9.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,388 kB
  • sloc: ansic: 5,082; makefile: 169; sh: 31
file content (133 lines) | stat: -rw-r--r-- 4,251 bytes parent folder | download | duplicates (5)
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
------------------------------------------------------------------------------
--
--  LGI Gdk3 override module.
--
--  Copyright (c) 2011, 2014 Pavel Holejsovsky
--  Licensed under the MIT license:
--  http://www.opensource.org/licenses/mit-license.php
--
------------------------------------------------------------------------------

local select, type, pairs, unpack, rawget = select, type, pairs, unpack, rawget

local lgi = require 'lgi'

local core = require 'lgi.core'
local ffi = require 'lgi.ffi'
local ti = ffi.types

local Gdk = lgi.Gdk
local cairo = lgi.cairo

-- Take over internal GDK synchronization lock.
core.registerlock(core.gi.Gdk.resolve.gdk_threads_set_lock_functions)
Gdk.threads_init()

-- Gdk.Rectangle does not exist at all in older GOI, because it is
-- aliased to cairo.RectangleInt.  Make sure that we have it exists,
-- because it is very commonly used in API documentation.
if not Gdk.Rectangle then
   Gdk.Rectangle = lgi.cairo.RectangleInt
   Gdk.Rectangle._method = rawget(Gdk.Rectangle, '_method') or {}
   Gdk.Rectangle._method.intersect = Gdk.rectangle_intersect
   Gdk.Rectangle._method.union = Gdk.rectangle_union
end

-- Declare GdkAtoms which are #define'd in Gdk sources and not
-- introspected in gir.
local _ = Gdk.KEY_0
for name, val in pairs {
   SELECTION_PRIMARY = 1,
   SELECTION_SECONDARY = 2,
   SELECTION_CLIPBOARD = 69,
   TARGET_BITMAP = 5,
   TARGET_COLORMAP = 7,
   TARGET_DRAWABLE = 17,
   TARGET_PIXMAP = 20,
   TARGET_STRING = 31,
   SELECTION_TYPE_ATOM = 4,
   SELECTION_TYPE_BITMAP = 5,
   SELECTION_TYPE_COLORMAP = 7,
   SELECTION_TYPE_DRAWABLE = 17,
   SELECTION_TYPE_INTEGER = 19,
   SELECTION_TYPE_PIXMAP = 20,
   SELECTION_TYPE_WINDOW = 33,
   SELECTION_TYPE_STRING = 31,
} do Gdk._constant[name] = Gdk.Atom(val) end

-- Easier-to-use Gdk.RGBA.parse() override.
if Gdk.RGBA then
    local parse = Gdk.RGBA.parse
    function Gdk.RGBA.parse(arg1, arg2)
       if Gdk.RGBA:is_type_of(arg1) then
          -- Standard member method.
          return parse(arg1, arg2)
       else
          -- Static constructor variant.
          local rgba = Gdk.RGBA()
          return parse(rgba, arg1) and rgba or nil
       end
    end
end

-- Gdk.Window.destroy() actually consumes 'self'.  Prepare workaround
-- with override doing ref on input arg first.
local destroy = Gdk.Window.destroy
local ref = core.callable.new {
   addr = core.gi.GObject.resolve.g_object_ref,
   ret = ti.ptr, ti.ptr
}
function Gdk.Window:destroy()
   ref(self._native)
   destroy(self)
end

-- Better integrate Gdk cairo helpers.
Gdk.Window.cairo_create = Gdk.cairo_create
cairo.Region.create_from_surface = Gdk.cairo_region_create_from_surface

local cairo_set_source_rgba = cairo.Context.set_source_rgba
function cairo.Context:set_source_rgba(...)
   if select('#', ...) == 1 then
      return Gdk.cairo_set_source_rgba(self, ...)
   else
      return cairo_set_source_rgba(self, ...)
   end
end

local cairo_rectangle = cairo.Context.rectangle
function cairo.Context:rectangle(...)
   if select('#', ...) == 1 then
      return Gdk.cairo_rectangle(self, ...)
   else
      return cairo_rectangle(self, ...)
   end
end

for _, name in pairs { 'get_clip_rectangle', 'set_source_color',
		       'set_source_pixbuf', 'set_source_window',
		       'region' } do
   cairo.Context._method[name] = Gdk['cairo_' .. name]
end
for _, name in pairs { 'clip_rectangle', 'source_color', 'source_pixbuf',
		       'source_window' } do
   cairo.Context._attribute[name] = {
      get = cairo.Context._method['get_' .. name],
      set = cairo.Context._method['set_' .. name],
   }
end

-- Gdk events have strange hierarchy; GdkEvent is union of all known
-- GdkEventXxx specific types.  This means that generic gdk_event_xxx
-- methods are not available on GdkEventXxxx specific types.  Work
-- around this by setting GdkEvent as parent for GdkEventXxxx specific
-- types.
for _, event_type in pairs {
   'Any', 'Expose', 'Visibility', 'Motion', 'Button', 'Touch', 'Scroll', 'Key',
   'Crossing', 'Focus', 'Configure', 'Property', 'Selection', 'OwnerChange',
   'Proximity', 'DND', 'WindowState', 'Setting', 'GrabBroken' } do
   local event = Gdk['Event' .. event_type]
   if event then
      event._parent = Gdk.Event
   end
end