1/** <title>GSSlideView</title>
2
3   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4
5   Created by: Enrico Sersale <enrico@imago.ro>
6   Date: Jan 2002
7   Author: Fred Kiefer <fredkiefer@gmx.de>
8   Date: Jan 2003
9   Removed all dependencies on X and moved to gui.
10
11   This file is part of the GNU Objective C User Interface Library.
12
13   This library is free software; you can redistribute it and/or
14   modify it under the terms of the GNU Lesser General Public
15   License as published by the Free Software Foundation; either
16   version 2 of the License, or (at your option) any later version.
17
18   This library is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
21   Lesser General Public License for more details.
22
23   You should have received a copy of the GNU Lesser General Public
24   License along with this library; see the file COPYING.LIB.
25   If not, see <http://www.gnu.org/licenses/> or write to the
26   Free Software Foundation, 51 Franklin Street, Fifth Floor,
27   Boston, MA 02110-1301, USA.
28*/
29
30#import <Foundation/NSDebug.h>
31#import "AppKit/NSApplication.h"
32#import "AppKit/NSCell.h"
33#import "AppKit/NSEvent.h"
34#import "AppKit/NSImage.h"
35#import "AppKit/NSView.h"
36#import "AppKit/NSWindow.h"
37
38#import "GNUstepGUI/GSDisplayServer.h"
39#import "GSSlideView.h"
40#include <math.h>
41
42// Minimal slide distance per step in pixel
43#define MINDIST 18
44// Time for each slide step in seconds
45#define SLIDE_TIME_STEP 0.02
46
47@interface GSSlideView (Private)
48- (void) _setupImage: (NSImage*) image startPoint: (NSPoint)slideStart;
49- (void) _slideFrom: (NSPoint)fromPoint to: (NSPoint)toPoint;
50@end
51
52@implementation GSSlideView (Private)
53
54- (void) _setupImage: (NSImage*) image startPoint: (NSPoint)slideStart
55{
56  NSSize imageSize = [image size];
57
58  [slideCell setImage: image];
59  [_window setFrame: NSMakeRect(slideStart.x, slideStart.y,
60				imageSize.width, imageSize.height)
61	   display: NO];
62
63  // Only display the image
64  [GSServerForWindow(_window) restrictWindow: [_window windowNumber]
65		    toImage: image];
66
67  [_window orderFrontRegardless];
68}
69
70- (void) _slideFrom: (NSPoint)fromPoint to: (NSPoint)toPoint
71{
72  float distx = toPoint.x - fromPoint.x;
73  float disty = toPoint.y - fromPoint.y;
74  float dist = sqrt((distx * distx) + (disty * disty));
75//  int steps = (int)(dist / MINDIST);
76  NSSize imgSize = [[slideCell image] size];
77  float imgDist = sqrt((imgSize.width * imgSize.width) +
78		       (imgSize.height * imgSize.height));
79  int steps = (int)(dist/imgDist);
80  int windowNumber = [_window windowNumber];
81  GSDisplayServer *server = GSServerForWindow(_window);
82
83
84  if (steps > 2)
85    {
86      float unitx = distx / steps;
87      float unity = disty / steps;
88
89      [NSEvent startPeriodicEventsAfterDelay: SLIDE_TIME_STEP
90	       withPeriod: SLIDE_TIME_STEP];
91      while (steps--)
92        {
93	  NSEvent *theEvent = [NSApp nextEventMatchingMask: NSPeriodicMask
94				     untilDate: [NSDate distantFuture]
95				     inMode: NSEventTrackingRunLoopMode
96				     dequeue: YES];
97
98          if ([theEvent type] == NSPeriodic)
99            {
100	      fromPoint.x += unitx;
101	      fromPoint.y += unity;
102	      [server movewindow: fromPoint : windowNumber];
103            }
104	  else
105            {
106              NSDebugLLog (@"NSDragging",
107			   @"Unexpected event type: %d during slide",
108                           (int)[theEvent type]);
109            }
110	}
111      [NSEvent stopPeriodicEvents];
112    }
113
114  // Go exactly to the point
115  [server movewindow: toPoint : windowNumber];
116}
117
118@end
119
120@implementation GSSlideView
121
122+ (BOOL) _slideImage: (NSImage *)image
123	        from: (NSPoint)fromPoint
124		  to: (NSPoint)toPoint
125{
126  static GSSlideView *v = nil;
127  BOOL result = NO;
128
129  if (image != nil)
130    {
131      if (v == nil)
132	{
133	  v = [[self alloc] init];
134	}
135      [NSApp preventWindowOrdering];
136      [v _setupImage: image startPoint: fromPoint];
137      [v _slideFrom: fromPoint to: toPoint];
138      [[v window] orderOut: nil];
139
140      result = YES;
141    }
142  return result;
143}
144
145- (id) init
146{
147  self = [super init];
148  if (self != nil)
149    {
150      // This is never used, as the window gets resized before displaying
151      NSRect winRect = {{0, 0}, {48, 48}};
152      NSWindow *slideWindow;
153
154      slideCell = [[NSCell alloc] initImageCell: nil];
155      [slideCell setBordered: NO];
156
157      slideWindow = [[NSWindow alloc] initWithContentRect: winRect
158					   styleMask: NSBorderlessWindowMask
159					     backing: NSBackingStoreNonretained
160					       defer: NO];
161      [slideWindow setReleasedWhenClosed: YES];
162      [slideWindow setExcludedFromWindowsMenu: YES];
163      [slideWindow setContentView: self];
164      RELEASE (self);
165    }
166
167  return self;
168}
169
170- (void) dealloc
171{
172  RELEASE (slideCell);
173  [super dealloc];
174}
175
176- (void) drawRect: (NSRect)rect
177{
178  [slideCell drawWithFrame: [self frame] inView: self];
179}
180
181@end
182