summaryrefslogtreecommitdiff
path: root/eel/eel-art-gtk-extensions.c
blob: f3ccef3ce76593ec5fc4b21e27cd09d58fb7c2a9 (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
339
340
341
342
343
344
345
346
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* eel-eel-gtk-extensions.c - Access gtk/gdk attributes as libeel rectangles.

   Copyright (C) 2000 Eazel, Inc.

   The Mate Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Mate Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PEELICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Mate Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: Ramiro Estrugo <ramiro@eazel.com>
*/

#include <config.h>

#include "eel-art-gtk-extensions.h"
#include <gdk/gdkx.h>

/**
 * eel_gdk_rectangle_to_eel_irect:
 * @gdk_rectangle: The source GdkRectangle.
 *
 * Return value: An EelIRect representation of the GdkRectangle.
 *
 * This is a very simple conversion of rectangles from the Gdk to the Libeel
 * universe.  This is useful in code that does clipping (or other operations)
 * using libeel and has a GdkRectangle to work with - for example expose_event()
 * in GtkWidget's.
 */
EelIRect
eel_gdk_rectangle_to_eel_irect (GdkRectangle gdk_rectangle)
{
    EelIRect rectangle;

    rectangle.x0 = gdk_rectangle.x;
    rectangle.y0 = gdk_rectangle.y;
    rectangle.x1 = rectangle.x0 + (int) gdk_rectangle.width;
    rectangle.y1 = rectangle.y0 + (int) gdk_rectangle.height;

    return rectangle;
}

/**
 * eel_screen_get_dimensions:
 *
 * Return value: The screen dimensions.
 *
 */
EelDimensions
eel_screen_get_dimensions (void)
{
    EelDimensions screen_dimensions;

    screen_dimensions.width = gdk_screen_width ();
    screen_dimensions.height = gdk_screen_height ();

    g_assert (screen_dimensions.width > 0);
    g_assert (screen_dimensions.height > 0);

    return screen_dimensions;
}

/**
 * eel_gdk_window_get_bounds:
 * @gdk_window: The source GdkWindow.
 *
 * Return value: An EelIRect representation of the given GdkWindow's geometry
 * relative to its parent in the Gdk window hierarchy.
 *
 */
EelIRect
eel_gdk_window_get_bounds (GdkWindow *gdk_window)
{
    EelIRect bounds;
    int width;
    int height;

    g_return_val_if_fail (gdk_window != NULL, eel_irect_empty);

    gdk_window_get_position (gdk_window, &bounds.x0, &bounds.y0);

#if GTK_CHECK_VERSION(3, 0, 0)
    width = gdk_window_get_width(gdk_window);
    height = gdk_window_get_height(gdk_window);
#else
    gdk_drawable_get_size (gdk_window, &width, &height);
#endif


    bounds.x1 = bounds.x0 + width;
    bounds.y1 = bounds.y0 + height;

    return bounds;
}

/**
 * eel_gdk_window_get_bounds:
 * @gdk_window: The source GdkWindow.
 *
 * Return value: An EelIRect representation of the given GdkWindow's geometry
 * relative to the screen.
 *
 */
EelIRect
eel_gdk_window_get_screen_relative_bounds (GdkWindow *gdk_window)
{
    EelIRect screen_bounds;
    int width;
    int height;

    g_return_val_if_fail (gdk_window != NULL, eel_irect_empty);

    if (!gdk_window_get_origin (gdk_window,
                                &screen_bounds.x0,
                                &screen_bounds.y0))
    {
        return eel_irect_empty;
    }

#if GTK_CHECK_VERSION(3, 0, 0)
    width = gdk_window_get_width(gdk_window);
    height = gdk_window_get_height(gdk_window);
#else
    gdk_drawable_get_size(gdk_window, &width, &height);
#endif

    screen_bounds.x1 = screen_bounds.x0 + width;
    screen_bounds.y1 = screen_bounds.y0 + height;

    return screen_bounds;
}

/**
 * eel_gtk_widget_get_bounds:
 * @gtk_widget: The source GtkWidget.
 *
 * Return value: An EelIRect representation of the given GtkWidget's geometry
 * relative to its parent.  In the Gtk universe this is known as "allocation."
 *
 */
EelIRect
eel_gtk_widget_get_bounds (GtkWidget *gtk_widget)
{
    GtkAllocation allocation;
    g_return_val_if_fail (GTK_IS_WIDGET (gtk_widget), eel_irect_empty);

    gtk_widget_get_allocation (gtk_widget, &allocation);
    return eel_irect_assign (allocation.x,
                             allocation.y,
                             (int) allocation.width,
                             (int) allocation.height);
}

/**
 * eel_gtk_widget_get_dimensions:
 * @gtk_widget: The source GtkWidget.
 *
 * Return value: The widget's dimensions.  The returned dimensions are only valid
 *               after the widget's geometry has been "allocated" by its container.
 */
EelDimensions
eel_gtk_widget_get_dimensions (GtkWidget *gtk_widget)
{
    EelDimensions dimensions;
    GtkAllocation allocation;

    g_return_val_if_fail (GTK_IS_WIDGET (gtk_widget), eel_dimensions_empty);

    gtk_widget_get_allocation (gtk_widget, &allocation);
    dimensions.width = (int) allocation.width;
    dimensions.height = (int) allocation.height;

    return dimensions;
}

/**
 * eel_gtk_widget_get_preferred_dimensions:
 * @gtk_widget: The source GtkWidget.
 *
 * Return value: The widget's preferred dimensions.  The preferred dimensions are
 *               computed by calling the widget's 'size_request' method and thus
 *               could potentially be expensive for complicated widgets.
 */
EelDimensions
eel_gtk_widget_get_preferred_dimensions (GtkWidget *gtk_widget)
{
    GtkRequisition requisition;
    EelDimensions preferred_dimensions;

    g_return_val_if_fail (GTK_IS_WIDGET (gtk_widget), eel_dimensions_empty);

    gtk_widget_size_request (gtk_widget, &requisition);

    preferred_dimensions.width = (int) requisition.width;
    preferred_dimensions.height = (int) requisition.height;

    return preferred_dimensions;
}

/**
 * eel_gdk_window_clip_dirty_area_to_screen:
 * @gdk_window: The GdkWindow that the damage occured on.
 * @dirty_area: The dirty area as an EelIRect.
 *
 * Return value: An EelIRect of the dirty area clipped to the screen.
 *
 * This function is useful to do less work in expose_event() GtkWidget methods.
 * It also ensures that any drawing that the widget does is actually onscreen.
 */
EelIRect
eel_gdk_window_clip_dirty_area_to_screen (GdkWindow *gdk_window,
        EelIRect dirty_area)
{
    EelIRect clipped;
    EelDimensions screen_dimensions;
    EelIRect screen_relative_bounds;
    int dirty_width;
    int dirty_height;

    g_return_val_if_fail (gdk_window != NULL, eel_irect_empty);

    dirty_width = dirty_area.x1 - dirty_area.x0;
    dirty_height = dirty_area.y1 - dirty_area.y0;

    g_return_val_if_fail (dirty_width > 0, eel_irect_empty);
    g_return_val_if_fail (dirty_height > 0, eel_irect_empty);

    screen_dimensions = eel_screen_get_dimensions ();
    screen_relative_bounds = eel_gdk_window_get_screen_relative_bounds (gdk_window);

    /* Window is obscured by left edge of screen */
    if ((screen_relative_bounds.x0 + dirty_area.x0) < 0)
    {
        int clipped_width = screen_relative_bounds.x0 + dirty_area.x0 + dirty_width;
        clipped.x0 = dirty_area.x0 + dirty_width - clipped_width;
        clipped.x1 = clipped.x0 + clipped_width;
    }
    else
    {
        clipped.x0 = dirty_area.x0;
        clipped.x1 = clipped.x0 + dirty_width;
    }

    /* Window is obscured by right edge of screen */
    if (screen_relative_bounds.x1 > screen_dimensions.width)
    {
        int obscured_width;

        obscured_width =
            screen_relative_bounds.x0 + dirty_area.x0 + dirty_width - screen_dimensions.width;

        if (obscured_width > 0)
        {
            clipped.x1 -= obscured_width;
        }
    }

    /* Window is obscured by top edge of screen */
    if ((screen_relative_bounds.y0 + dirty_area.y0) < 0)
    {
        int clipped_height = screen_relative_bounds.y0 + dirty_area.y0 + dirty_height;
        clipped.y0 = dirty_area.y0 + dirty_height - clipped_height;
        clipped.y1 = clipped.y0 + clipped_height;
    }
    else
    {
        clipped.y0 = dirty_area.y0;
        clipped.y1 = clipped.y0 + dirty_height;
    }

    /* Window is obscured by bottom edge of screen */
    if (screen_relative_bounds.y1 > screen_dimensions.height)
    {
        int obscured_height;

        obscured_height =
            screen_relative_bounds.y0 + dirty_area.y0 + dirty_height - screen_dimensions.height;

        if (obscured_height > 0)
        {
            clipped.y1 -= obscured_height;
        }
    }

    if (eel_irect_is_empty (&clipped))
    {
        clipped = eel_irect_empty;
    }

    return clipped;
}

GdkRectangle
eel_irect_to_gdk_rectangle (EelIRect rectangle)
{
    GdkRectangle gdk_rect;

    gdk_rect.x = rectangle.x0;
    gdk_rect.y = rectangle.y0;
    gdk_rect.width = eel_irect_get_width (rectangle);
    gdk_rect.height = eel_irect_get_height (rectangle);

    return gdk_rect;
}

EelDimensions
eel_gdk_window_get_dimensions (GdkWindow *gdk_window)
{
    EelDimensions dimensions;

    g_return_val_if_fail (gdk_window != NULL, eel_dimensions_empty);

#if GTK_CHECK_VERSION(3, 0, 0)
    dimensions.width = gdk_window_get_width(gdk_window);
    dimensions.height = gdk_window_get_height(gdk_window);
#else
    gdk_drawable_get_size (gdk_window, &dimensions.width, &dimensions.height);
#endif

    return dimensions;
}

EelIPoint
eel_gdk_get_pointer_position (void)
{

    EelIPoint position;

    gdk_window_get_pointer (gdk_get_default_root_window (),
                            &position.x,
                            &position.y,
                            NULL);

    return position;
}