~kamstrup/winwrangler/winwrangler-0.2

« back to all changes in this revision

Viewing changes to src/ww-utils.c

  • Committer: "Mikkel Kamstrup Erlandsen"
  • Date: 2009-09-27 11:30:53 UTC
  • mfrom: (49.1.13 winwrangler-spatial-switch)
  • Revision ID: mikkel.kamstrup@gmail.com-20090927113053-gn95snpkcxr22miu
MergeĀ lp:~kamstrup/winwrangler/winwrangler-spatial-switch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * This file is part of WinWrangler.
3
3
 * Copyright (C) Mikkel Kamstrup Erlandsen 2008 <mikkel.kamstrup@gmail.com>
 
4
 *               Alessio 'molok' Bolognino <themolok@gmail.com>
4
5
 *
5
6
 *  WinWrangler is free software: you can redistribute it and/or modify
6
7
 *  it under the terms of the GNU General Public License as published by
16
17
 *  along with WinWranger.  If not, see <http://www.gnu.org/licenses/>.
17
18
 */
18
19
 
 
20
#include <math.h>
19
21
 
20
22
#include "winwrangler.h"
21
23
 
 
24
static guint32 _event_time = 0;
 
25
 
22
26
/**
23
27
 * ww_filter_user_windows
24
28
 * @windows: List of %WnckWindow<!-- -->s to filter
240
244
        *right = edge_r;
241
245
        *bottom = edge_b;
242
246
}
 
247
 
 
248
/**
 
249
 * ww_find_neighbour
 
250
 * @screen:
 
251
 * @windows:
 
252
 * @active:
 
253
 * @direction:
 
254
 *
 
255
 * Return value: The neighbouring window from @windows in the given direction
 
256
 *               or %NULL in case no window is found or @active is %NULL
 
257
 */
 
258
WnckWindow*
 
259
ww_find_neighbour (WnckScreen   *screen,
 
260
                   GList                *windows,
 
261
                   WnckWindow   *active,
 
262
                   WwDirection  direction)
 
263
{
 
264
        WnckWindow      *neighbour;
 
265
        GList           *next;
 
266
        int                     ax, ay, aw, ah; /* active window geometry */
 
267
        int                     wx, wy, ww, wh; /* geometry for currently checked window */ 
 
268
        int                     nx, ny, nw, nh; /* geometry of neighbour */
 
269
 
 
270
        neighbour = NULL;
 
271
        
 
272
        g_return_val_if_fail (WNCK_IS_SCREEN(screen), NULL);
 
273
        
 
274
        if (g_list_length(windows) == 0)
 
275
    {
 
276
                return NULL;
 
277
    }
 
278
        
 
279
        /* If there is no active window, do nothing */
 
280
        if (active == NULL
 
281
                || wnck_window_is_skip_tasklist (active)) {
 
282
                g_debug ("No active window");
 
283
                return NULL;
 
284
        }
 
285
 
 
286
        nx = ny = nw = nh = 0; 
 
287
 
 
288
        wnck_window_get_geometry (active, &ax, &ay, &aw, &ah);
 
289
        g_debug("Active window '%s' (%d, %d) @ %d x %d",
 
290
                wnck_window_get_name (active), ax, ay, aw, ah);
 
291
 
 
292
 
 
293
        if ( direction == LEFT )
 
294
        {
 
295
                for ( next = windows; next; next = next->next )
 
296
                {
 
297
                        wnck_window_get_geometry (next->data, &wx, &wy, &ww, &wh);
 
298
                        if ( wx < ax )
 
299
                        {
 
300
                                if ( neighbour == NULL )
 
301
                                {
 
302
                                        neighbour = WNCK_WINDOW (next->data);
 
303
                                        nx = wx; ny = wy; nw = ww; nh = wh;
 
304
                                }
 
305
                                else if ( wx == nx )
 
306
                                {
 
307
                                        if ( abs(wy - ay) < abs(ny - ay) )
 
308
                                        {
 
309
                                                neighbour = WNCK_WINDOW (next->data);
 
310
                                                nx = wx; ny = wy; nw = ww; nh = wh;
 
311
                                        }
 
312
                                }
 
313
                                else if ( wx > nx )
 
314
                                {
 
315
                                        neighbour = WNCK_WINDOW (next->data);
 
316
                                        nx = wx; ny = wy; nw = ww; nh = wh;
 
317
                                }
 
318
                        } 
 
319
                }
 
320
        }
 
321
        else if ( direction == RIGHT )
 
322
        {
 
323
                int a_right = ax + aw;
 
324
                int w_right;
 
325
                
 
326
                for ( next = windows; next; next = next->next )
 
327
                {
 
328
                        wnck_window_get_geometry (next->data, &wx, &wy, &ww, &wh);
 
329
                        w_right = wx + ww;
 
330
                        
 
331
                        if ( w_right > a_right )
 
332
                        {
 
333
                                if ( neighbour == NULL )
 
334
                                {
 
335
                                        neighbour = WNCK_WINDOW (next->data);
 
336
                                        nx = wx; ny = wy; nw = ww; nh = wh;
 
337
                                }
 
338
                                else if ( w_right == (nx + nw) )
 
339
                                {
 
340
                                        if ( abs(wy - ay) < abs(ny - ay) )
 
341
                                        {
 
342
                                                neighbour = WNCK_WINDOW (next->data);
 
343
                                                nx = wx; ny = wy; nw = ww; nh = wh;
 
344
                                        }
 
345
                                }
 
346
                                else if ( w_right < (nx + nw) ) {
 
347
                                        neighbour = WNCK_WINDOW (next->data);
 
348
                                        nx = wx; ny = wy; nw = ww; nh = wh;
 
349
                                }
 
350
                        }
 
351
                }
 
352
        }
 
353
        else if ( direction == DOWN )
 
354
        {
 
355
                int a_bottom = ay + ah;
 
356
                int w_bottom;
 
357
                
 
358
                for ( next = windows; next; next = next->next )
 
359
                {
 
360
                        wnck_window_get_geometry (next->data, &wx, &wy, &ww, &wh);
 
361
                        w_bottom = wy + wh;
 
362
                        
 
363
                        if ( w_bottom > a_bottom )
 
364
                        {
 
365
                                if ( neighbour == NULL )
 
366
                                {
 
367
                                        neighbour = WNCK_WINDOW (next->data);
 
368
                                        nx = wx; ny = wy; nw = ww; nh = wh;
 
369
                                }
 
370
                                else if ( w_bottom == (ny + nh) )
 
371
                                {
 
372
                                        if ( abs(wx - ax) < abs(nx - ax) )
 
373
                                        {
 
374
                                                neighbour = WNCK_WINDOW (next->data);
 
375
                                                nx = wx; ny = wy; nw = ww; nh = wh;
 
376
                                        }
 
377
                                }
 
378
                                else if ( w_bottom < (ny + nh) ) {
 
379
                                        neighbour = WNCK_WINDOW (next->data);
 
380
                                        nx = wx; ny = wy; nw = ww; nh = wh;
 
381
                                }
 
382
                        }
 
383
                }
 
384
        }
 
385
        else if ( direction == UP )
 
386
        {
 
387
                for ( next = windows; next; next = next->next )
 
388
                {
 
389
                        wnck_window_get_geometry (next->data, &wx, &wy, &ww, &wh);
 
390
                        if ( wy < ay )
 
391
                        {
 
392
                                if ( neighbour == NULL )
 
393
                                {
 
394
                                        neighbour = WNCK_WINDOW (next->data);
 
395
                                        nx = wx; ny = wy; nw = ww; nh = wh;
 
396
                                }
 
397
                                else if ( wy == ny )
 
398
                                {
 
399
                                        if ( abs(wx - ax) < abs(nx - ax) )
 
400
                                        {
 
401
                                                neighbour = WNCK_WINDOW (next->data);
 
402
                                                nx = wx; ny = wy; nw = ww; nh = wh;
 
403
                                        }
 
404
                                }
 
405
                                else if ( wy > ny ) {
 
406
                                        neighbour = WNCK_WINDOW (next->data);
 
407
                                        nx = wx; ny = wy; nw = ww; nh = wh;
 
408
                                }
 
409
                        }
 
410
                }
 
411
        }
 
412
 
 
413
        if (neighbour)
 
414
                g_debug ("Found neighbour '%s' (%d, %d) @ %d x %d",
 
415
                         wnck_window_get_name (neighbour), nx, ny, nw, nh);                     
 
416
        
 
417
        return neighbour; 
 
418
}
 
419
 
 
420
guint32
 
421
ww_get_event_time (void)
 
422
{
 
423
        return _event_time;
 
424
}
 
425
 
 
426
void
 
427
ww_set_event_time (guint32 event_time)
 
428
{
 
429
        _event_time = event_time;
 
430
}
 
 
b'\\ No newline at end of file'