aboutsummaryrefslogtreecommitdiffstats
path: root/capplet/settings/mail-decoration.c
blob: 51a71d11ef4b00082d54766539dc9b033a7f49b0 (plain) (blame)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program 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 PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Srinivasa Ragavan <sragavan@novell.com>
 *
 * Copyright (C) 2009 Novell, Inc. (www.novell.com)
 *
 */

/* Many thanks to Aaron Bockover & Cubano. This is just a C version Cubano's self decoration */

#include "mail-decoration.h"

#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <gconf/gconf-client.h>

#define MAIL_DECORATION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), MAIL_DECORATION_TYPE, MailDecorationPrivate))

struct _MailDecorationPrivate
{
    GdkCursor *cursors[8];
    gboolean default_cursor;
    gboolean resizing;
    GdkWindowEdge last_edge;
    gint resize_width;
    gint top_height;
    gboolean check_window;
    gboolean can_resize;
    gboolean full_screen;

    gint window_width;
    gint window_height;
};

static GObjectClass *parent_class = NULL;

static void mail_decoration_class_init (MailDecorationClass *klass);
static void mail_decoration_init (MailDecoration *facet);

GType
mail_decoration_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
    {
        static const GTypeInfo mail_decoration_info =
        {
            sizeof (MailDecorationClass),
            NULL,
            NULL,
            (GClassInitFunc) mail_decoration_class_init,
            NULL,
            NULL,
            sizeof (MailDecoration),
            0,
            (GInstanceInitFunc) mail_decoration_init
        };

        type = g_type_register_static (G_TYPE_OBJECT,
                           "MailDecoration",
                           &mail_decoration_info, 0);
    }

    return type;
}

static void
md_translate_position (GdkWindow *w, double ex, double ey, gint *x, gint *y, GtkWidget *window)
{
    *x = (gint)ex;
    *y = (gint)ey;

    while (w && w != gtk_widget_get_window (window)) {
        gint cx, cy, cw, ch;
        gdk_window_get_geometry (w, &cx, &cy, &cw, &ch);
                *x += cx;
                *y += cy;
        w = gdk_window_get_parent (w);
    }
}

static gboolean
in_top (MailDecoration *md, double y)
{
    return y <= md->priv->resize_width;
}

static gboolean
in_left (MailDecoration *md, double x)
{
    return x <= md->priv->resize_width;
}

static gboolean
in_bottom (MailDecoration *md, double y)
{
    GtkAllocation allocation;

    gtk_widget_get_allocation (GTK_WIDGET (md->window), &allocation);

    return y >= allocation.height - md->priv->resize_width;
}

static gboolean
in_right (MailDecoration *md, double x)
{
    GtkAllocation allocation;

    gtk_widget_get_allocation (GTK_WIDGET (md->window), &allocation);

    return x >= allocation.width - md->priv->resize_width;
}

static void
set_cursor (MailDecoration *md, GdkWindowEdge edge)
{
    gdk_window_set_cursor (
        gtk_widget_get_window (GTK_WIDGET (md->window)),
        md->priv->cursors[edge]);
    md->priv->default_cursor = FALSE;
}

static void
reset_cursor (MailDecoration *md)
{
    if (!md->priv->default_cursor) {
        md->priv->default_cursor = TRUE;
        gdk_window_set_cursor (
            gtk_widget_get_window (GTK_WIDGET (md->window)),
            NULL);
    }

}

static void
update_cursor (MailDecoration *md, double x, double y, gboolean update)
{
    if (update)
        md->priv->resizing = TRUE;

    if (in_top (md, y) && in_left (md, x)) {
        md->priv->last_edge = GDK_WINDOW_EDGE_NORTH_WEST;
        set_cursor (md, GDK_WINDOW_EDGE_NORTH_WEST);
    } else if (in_top (md, y) && in_right (md, x)) {
        md->priv->last_edge = GDK_WINDOW_EDGE_NORTH_EAST;
        set_cursor (md, GDK_WINDOW_EDGE_NORTH_EAST);
    } else if (in_bottom (md, y) && in_left (md, x)) {
        md->priv->last_edge = GDK_WINDOW_EDGE_SOUTH_WEST;
        set_cursor (md, GDK_WINDOW_EDGE_SOUTH_WEST);
    } else if (in_bottom (md, y) && in_right (md, x)) {
        md->priv->last_edge = GDK_WINDOW_EDGE_SOUTH_EAST;
        set_cursor (md, GDK_WINDOW_EDGE_SOUTH_EAST);
    } else if (in_top (md, y)) {
        md->priv->last_edge = GDK_WINDOW_EDGE_NORTH;
        set_cursor (md, GDK_WINDOW_EDGE_NORTH);
    } else if (in_bottom (md, y)) {
        md->priv->last_edge = GDK_WINDOW_EDGE_SOUTH;
        set_cursor (md, GDK_WINDOW_EDGE_SOUTH);
    } else if (in_left (md, x)) {
        md->priv->last_edge = GDK_WINDOW_EDGE_WEST;
        set_cursor (md, GDK_WINDOW_EDGE_WEST);
    } else if (in_right (md, x)) {
        md->priv->last_edge = GDK_WINDOW_EDGE_EAST;
        set_cursor (md, GDK_WINDOW_EDGE_EAST);
    } else {
        if (update)
            md->priv->resizing = FALSE;
        reset_cursor (md);
    }
}

static gboolean
md_motion_event (GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
{
    gint x, y;
    MailDecoration *md = (MailDecoration *)user_data;

    md_translate_position (event->window, event->x, event->y, &x, &y, (GtkWidget *)md->window);

    if (md->priv->can_resize) {
        update_cursor  (md, x, y, FALSE);
    }

    return FALSE;
}

static gboolean
md_enter_event (GtkWidget *widget , GdkEventCrossing *event, gpointer user_data)
{
    MailDecoration *md = (MailDecoration *)user_data;
    gint x, y;

    md_translate_position (event->window, event->x, event->y, &x, &y, (GtkWidget *)md->window);

    if (md->priv->can_resize) {
        update_cursor (md, x, y, FALSE);
    }

    return FALSE;
}

static gboolean
md_leave_event (GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
{
    MailDecoration *md = (MailDecoration *)user_data;

    if (md->priv->can_resize)
        reset_cursor (md);

    return FALSE;
}

static void
md_size_allocate_event (GtkWidget *widget, GtkAllocation *allocation, gpointer user_data)
{
    gint width=1024, height=500;
    MailDecoration *md = (MailDecoration *)user_data;

    gtk_widget_queue_draw (widget);
    gtk_window_get_size ((GtkWindow *)widget, &width, &height);
    if (width != md->priv->window_width || height != md->priv->window_height) {
        GConfClient *client = gconf_client_get_default ();

        md->priv->window_height = height;
        md->priv->window_width = width;
        gconf_client_set_int (client, "/apps/anjal/window_width", width, NULL);
        gconf_client_set_int (client, "/apps/anjal/window_height", height, NULL);
        g_object_unref (client);
    }

}

static gboolean
md_button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
    MailDecoration *md = (MailDecoration *)user_data;
    gint x_root = (gint) event->x_root;
    gint y_root = (gint) event->y_root;
    gint x, y;

    if (!md->priv->can_resize) {
        return FALSE;
    }

    md_translate_position (event->window, event->x, event->y, &x, &y, (GtkWidget *)md->window);
    update_cursor (md, x, y, TRUE);
    if (md->priv->resizing && event->button == 1 && event->type != GDK_2BUTTON_PRESS) {
        gtk_window_begin_resize_drag ((GtkWindow *)widget, md->priv->last_edge, 1, x_root, y_root, event->time);

    } else if ((md->priv->resizing && event->button == 2 && event->type != GDK_2BUTTON_PRESS) ||
        (event->button == 1 && y <= md->priv->top_height && event->type != GDK_2BUTTON_PRESS)) {
        gtk_window_begin_move_drag ((GtkWindow *)widget, event->button, x_root, y_root, event->time);
    } else if (y <= md->priv->top_height && event->type == GDK_2BUTTON_PRESS) {
        if (md->priv->full_screen)
            gtk_window_unfullscreen (md->window);
        else
            gtk_window_fullscreen (md->window);

        md->priv->full_screen = md->priv->full_screen != TRUE;
    } else
        return FALSE;

    return TRUE;
}

static gboolean
md_button_release_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
    gint x, y;
    MailDecoration *md = (MailDecoration *)user_data;

    md_translate_position (event->window, event->x, event->y, &x, &y, (GtkWidget *)md->window);
    if (md->priv->resizing) {
        update_cursor (md, x, y, TRUE);
    }

    return FALSE;
}

MailDecoration* mail_decoration_new (GtkWindow *window)
{
    MailDecoration *md = g_object_new (mail_decoration_get_type (), NULL);
    GConfClient *client = gconf_client_get_default ();
    gint width, height;

    md->priv->window_width = width = gconf_client_get_int (client, "/apps/anjal/window_width", NULL);
    if (!width)
        md->priv->window_width = width = 1024;
    md->priv->window_height = height = gconf_client_get_int (client, "/apps/anjal/window_height", NULL);
    if (!height)
        md->priv->window_height = height = 500;
    g_object_unref (client);

    md->window = window;
    gtk_window_set_decorated (window, FALSE);
    gtk_widget_add_events ((GtkWidget *)window, GDK_BUTTON_PRESS_MASK |
                    GDK_POINTER_MOTION_MASK |
                    GDK_ENTER_NOTIFY_MASK |
                    GDK_LEAVE_NOTIFY_MASK |
                    GDK_BUTTON_RELEASE_MASK);

    g_signal_connect (window, "motion-notify-event", G_CALLBACK(md_motion_event), md);
    g_signal_connect (window, "enter-notify-event", G_CALLBACK(md_enter_event), md);
    g_signal_connect (window, "leave-notify-event", G_CALLBACK(md_leave_event), md);
    g_signal_connect (window, "button-press-event", G_CALLBACK(md_button_press_event), md);
    g_signal_connect (window, "button-release-event", G_CALLBACK(md_button_release_event), md);
    g_signal_connect (window, "size-allocate", G_CALLBACK(md_size_allocate_event), md);
    gtk_window_set_default_size ((GtkWindow *)window , width, height);/* We officiall should support 800x600 */

    return md;
}

static void
mail_decoration_class_init (MailDecorationClass *klass)
{

    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    parent_class = g_type_class_peek_parent (klass);

    g_type_class_add_private (object_class, sizeof (MailDecorationPrivate));
}

static void
mail_decoration_init (MailDecoration *md)
{
    MailDecorationPrivate *priv;

    priv = MAIL_DECORATION_GET_PRIVATE (md);
    md->priv = priv;

    priv->cursors[0]= gdk_cursor_new (GDK_TOP_LEFT_CORNER);
    priv->cursors[1]= gdk_cursor_new (GDK_TOP_SIDE);
    priv->cursors[2]= gdk_cursor_new (GDK_TOP_RIGHT_CORNER);
    priv->cursors[3]= gdk_cursor_new (GDK_LEFT_SIDE);
    priv->cursors[4]= gdk_cursor_new (GDK_RIGHT_SIDE);
    priv->cursors[5]= gdk_cursor_new (GDK_BOTTOM_LEFT_CORNER);
    priv->cursors[6]= gdk_cursor_new (GDK_BOTTOM_SIDE);
    priv->cursors[7]= gdk_cursor_new (GDK_BOTTOM_RIGHT_CORNER);

    priv->default_cursor = TRUE;
    priv->resizing = FALSE;
    priv->resize_width = 4;
    priv->top_height = 54;
    priv->check_window = TRUE;
    priv->can_resize = TRUE;
    priv->full_screen = TRUE;
}