summaryrefslogtreecommitdiff
path: root/mate-dictionary/src/gdict-aligned-window.c
blob: 9d8330ab8377866cc8b32ad282fdc148cbf44cb8 (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
/* gdict-aligned-window.c - Popup window aligned to a widget
 *
 * Copyright (c) 2005-2006  Emmanuele Bassi <ebassi@gmail.com>
 *
 * This 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.
 * 
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License  
 * along with this program; if not, write to the Free Software  
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Ported from Seth Nickell's Python class:
 *   Copyright (c) 2003 Seth Nickell
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <gtk/gtk.h>

#include "gdict-aligned-window.h"

#define GDICT_ALIGNED_WINDOW_GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GDICT_TYPE_ALIGNED_WINDOW, GdictAlignedWindowPrivate))

struct _GdictAlignedWindowPrivate
{
  GtkWidget *align_widget;
  
  guint motion_id;
};

enum
{
  PROP_0,
  
  PROP_ALIGN_WIDGET
};

static void gdict_aligned_window_finalize     (GObject      *object);
static void gdict_aligned_window_get_property (GObject      *object,
					       guint         prop_id,
					       GValue       *value,
					       GParamSpec   *pspec);
static void gdict_aligned_window_set_property (GObject      *object,
					       guint         prop_id,
					       const GValue *value,
					       GParamSpec   *pspec);

static void     gdict_aligned_window_realize          (GtkWidget        *widget);
static void     gdict_aligned_window_show             (GtkWidget        *widget);

static gboolean gdict_aligned_window_motion_notify_cb (GtkWidget        *widget,
						       GdkEventMotion   *event,
						       GdictAlignedWindow *aligned_window);


G_DEFINE_TYPE (GdictAlignedWindow, gdict_aligned_window, GTK_TYPE_WINDOW);



static void
gdict_aligned_window_class_init (GdictAlignedWindowClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  
  gobject_class->set_property = gdict_aligned_window_set_property;
  gobject_class->get_property = gdict_aligned_window_get_property;
  gobject_class->finalize = gdict_aligned_window_finalize;
  
  widget_class->realize = gdict_aligned_window_realize;
  widget_class->show = gdict_aligned_window_show;
  
  g_object_class_install_property (gobject_class, PROP_ALIGN_WIDGET,
  				   g_param_spec_object ("align-widget",
  				   			"Align Widget",
  				   			"The widget the window should align to",
  				   			GTK_TYPE_WIDGET,
  				   			G_PARAM_READWRITE));
  
  g_type_class_add_private (klass, sizeof (GdictAlignedWindowPrivate));
}

static void
gdict_aligned_window_init (GdictAlignedWindow *aligned_window)
{
  GdictAlignedWindowPrivate *priv = GDICT_ALIGNED_WINDOW_GET_PRIVATE (aligned_window);
  GtkWindow *window = GTK_WINDOW (aligned_window);
  
  aligned_window->priv = priv;
  
  priv->align_widget = NULL;
  priv->motion_id = 0;
  
  /* set window properties */
#if 0
  gtk_window_set_modal (window, TRUE);
#endif
  gtk_window_set_decorated (window, FALSE);
  gtk_window_set_type_hint (window, GDK_WINDOW_TYPE_HINT_DOCK);
}

static void
gdict_aligned_window_get_property (GObject    *object,
				   guint       prop_id,
				   GValue     *value,
				   GParamSpec *pspec)
{
  GdictAlignedWindow *aligned_window = GDICT_ALIGNED_WINDOW (object);
  
  switch (prop_id)
    {
    case PROP_ALIGN_WIDGET:
      g_value_set_object (value, aligned_window->priv->align_widget);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdict_aligned_window_set_property (GObject      *object,
				   guint         prop_id,
				   const GValue *value,
				   GParamSpec   *pspec)
{
  GdictAlignedWindow *aligned_window = GDICT_ALIGNED_WINDOW (object);
  
  switch (prop_id)
    {
    case PROP_ALIGN_WIDGET:
      gdict_aligned_window_set_widget (aligned_window,
      				       g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdict_aligned_window_position (GdictAlignedWindow *window)
{
  GdictAlignedWindowPrivate *priv;
  GtkWidget *align_widget;
  gint our_width, our_height;
  gint sc_width, sc_height;
  gint entry_x, entry_y, entry_width, entry_height;
  gint x, y;
  GdkGravity gravity = GDK_GRAVITY_NORTH_WEST;
  GdkWindow *gdk_window;

  g_assert (GDICT_IS_ALIGNED_WINDOW (window));
  priv = window->priv;

  if (!priv->align_widget)
    return;

  align_widget = priv->align_widget;
  gdk_window = gtk_widget_get_window (align_widget);

  gdk_flush ();

  gdk_window_get_geometry (gtk_widget_get_window (GTK_WIDGET (window)), NULL, NULL, &our_width, &our_height);

  /* stick, skip taskbar and pager */
  gtk_window_stick (GTK_WINDOW (window));
  gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
  gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);

  /* make sure the align_widget is realized before we do anything */
  gtk_widget_realize (align_widget);
  
  /* get the positional and dimensional attributes of the align widget */
  gdk_window_get_origin (gdk_window,
  			 &entry_x,
  			 &entry_y);
  gdk_window_get_geometry (gdk_window, NULL, NULL, &entry_width, &entry_height);

  gdk_window_get_geometry (gdk_screen_get_root_window (gdk_screen_get_default()),
                           NULL, NULL, &sc_width, &sc_height);

  if (entry_x + our_width < sc_width)
    x = entry_x + 1;
  else
    {
      x = entry_x + entry_width - our_width - 1;
      
      gravity = GDK_GRAVITY_NORTH_EAST;
    }
  
  if (entry_y + entry_height + our_height < sc_height)
    y = entry_y + entry_height - 1;
  else
    {
      y = entry_y - our_height + 1;
      
      if (gravity == GDK_GRAVITY_NORTH_EAST)
	gravity = GDK_GRAVITY_SOUTH_EAST;
      else
	gravity = GDK_GRAVITY_SOUTH_WEST;
    }
  
  gtk_window_set_gravity (GTK_WINDOW (window), gravity);
  gtk_window_move (GTK_WINDOW (window), x, y);
}

static void
gdict_aligned_window_realize (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (gdict_aligned_window_parent_class)->realize (widget);

  gdict_aligned_window_position (GDICT_ALIGNED_WINDOW (widget));
}

static void
gdict_aligned_window_show (GtkWidget *widget)
{
  gdict_aligned_window_position (GDICT_ALIGNED_WINDOW (widget));
  
  GTK_WIDGET_CLASS (gdict_aligned_window_parent_class)->show (widget);
}

static void
gdict_aligned_window_finalize (GObject *object)
{
  G_OBJECT_CLASS (gdict_aligned_window_parent_class)->finalize (object);
}

static gboolean
gdict_aligned_window_motion_notify_cb (GtkWidget        *widget,
				       GdkEventMotion   *event,
				       GdictAlignedWindow *aligned_window)
{
  GtkAllocation alloc;
  GdkRectangle rect;

  gtk_widget_get_allocation (GTK_WIDGET (aligned_window), &alloc);
  
  rect.x = 0;
  rect.y = 0;
  rect.width = alloc.width;
  rect.height = alloc.height;

  gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (aligned_window)),
		  	      &rect,
			      FALSE);
  
  return FALSE;
}


/**
 * gdict_aligned_window_new:
 * @align_widget: a #GtkWidget to which the window should align
 *
 * Creates a new window, aligned to a previously created widget.
 *
 * Return value: a new #GdictAlignedWindow
 */
GtkWidget *
gdict_aligned_window_new (GtkWidget *align_widget)
{
  return g_object_new (GDICT_TYPE_ALIGNED_WINDOW,
  		       "align-widget", align_widget,
  		       NULL);
}

/**
 * gdict_aligned_window_set_widget:
 * @aligned_window: a #GdictAlignedWindow
 * @align_widget: the #GtkWidget @aligned_window should align to
 *
 * Sets @align_widget as the #GtkWidget to which @aligned_window should
 * align.
 *
 * Note that @align_widget must have a #GdkWindow in order to
 * #GdictAlignedWindow to work.
 */
void
gdict_aligned_window_set_widget (GdictAlignedWindow *aligned_window,
			         GtkWidget          *align_widget)
{
  GdictAlignedWindowPrivate *priv;
  
  g_return_if_fail (GDICT_IS_ALIGNED_WINDOW (aligned_window));
  g_return_if_fail (GTK_IS_WIDGET (align_widget));

#if 0  
  if (GTK_WIDGET_NO_WINDOW (align_widget))
    {
      g_warning ("Attempting to set a widget of class '%s' as the "
                 "align widget, but widgets of this class does not "
                 "have a GdkWindow.",
                 g_type_name (G_OBJECT_TYPE (align_widget)));
      
      return;
    }
#endif

  priv = GDICT_ALIGNED_WINDOW_GET_PRIVATE (aligned_window);
  
  if (priv->align_widget)
    {
      g_signal_handler_disconnect (priv->align_widget, priv->motion_id);
      priv->align_widget = NULL;
    }

  priv->align_widget = align_widget;
  priv->motion_id = g_signal_connect (priv->align_widget, "motion-notify-event",
				      G_CALLBACK (gdict_aligned_window_motion_notify_cb),
				      aligned_window);
}

/**
 * gdict_aligned_window_get_widget:
 * @aligned_window: a #GdictAlignedWindow
 *
 * Retrieves the #GtkWidget to which @aligned_window is aligned to.
 *
 * Return value: the align widget.
 */
GtkWidget *
gdict_aligned_window_get_widget (GdictAlignedWindow *aligned_window)
{
  g_return_val_if_fail (GDICT_IS_ALIGNED_WINDOW (aligned_window), NULL);
  
  return aligned_window->priv->align_widget;
}