File: GSSlideView.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (181 lines) | stat: -rw-r--r-- 4,921 bytes parent folder | download | duplicates (5)
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
/** <title>GSSlideView</title>

   Copyright (C) 2002, 2003 Free Software Foundation, Inc.

   Created by: Enrico Sersale <enrico@imago.ro>
   Date: Jan 2002
   Author: Fred Kiefer <fredkiefer@gmx.de>
   Date: Jan 2003
   Removed all dependencies on X and moved to gui.

   This file is part of the GNU Objective C User Interface Library.

   This library 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) any later version.

   This 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 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 this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#import <Foundation/NSDebug.h>
#import "AppKit/NSApplication.h"
#import "AppKit/NSCell.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSView.h"
#import "AppKit/NSWindow.h"

#import "GNUstepGUI/GSDisplayServer.h"
#import "GSSlideView.h"
#include <math.h>

// Minimal slide distance per step in pixel
#define MINDIST 18
// Time for each slide step in seconds
#define SLIDE_TIME_STEP 0.02

@interface GSSlideView (Private)
- (void) _setupImage: (NSImage*) image startPoint: (NSPoint)slideStart;
- (void) _slideFrom: (NSPoint)fromPoint to: (NSPoint)toPoint;
@end

@implementation GSSlideView (Private)

- (void) _setupImage: (NSImage*) image startPoint: (NSPoint)slideStart
{
  NSSize imageSize = [image size];

  [slideCell setImage: image];
  [_window setFrame: NSMakeRect(slideStart.x, slideStart.y,
				imageSize.width, imageSize.height) 
	   display: NO];

  // Only display the image
  [GSServerForWindow(_window) restrictWindow: [_window windowNumber]
		    toImage: image];

  [_window orderFrontRegardless];
}

- (void) _slideFrom: (NSPoint)fromPoint to: (NSPoint)toPoint
{
  float distx = toPoint.x - fromPoint.x;
  float disty = toPoint.y - fromPoint.y;
  float dist = sqrt((distx * distx) + (disty * disty));
//  int steps = (int)(dist / MINDIST);
  NSSize imgSize = [[slideCell image] size];
  float imgDist = sqrt((imgSize.width * imgSize.width) + 
		       (imgSize.height * imgSize.height));
  int steps = (int)(dist/imgDist);
  int windowNumber = [_window windowNumber];
  GSDisplayServer *server = GSServerForWindow(_window);


  if (steps > 2)
    {
      float unitx = distx / steps;
      float unity = disty / steps;

      [NSEvent startPeriodicEventsAfterDelay: SLIDE_TIME_STEP 
	       withPeriod: SLIDE_TIME_STEP];
      while (steps--)
        {
	  NSEvent *theEvent = [NSApp nextEventMatchingMask: NSPeriodicMask
				     untilDate: [NSDate distantFuture]
				     inMode: NSEventTrackingRunLoopMode
				     dequeue: YES];

          if ([theEvent type] == NSPeriodic)
            {
	      fromPoint.x += unitx;
	      fromPoint.y += unity;
	      [server movewindow: fromPoint : windowNumber];
            }
	  else 
            {
              NSDebugLLog (@"NSDragging", 
			   @"Unexpected event type: %d during slide",
                           (int)[theEvent type]);
            }
	}
      [NSEvent stopPeriodicEvents];
    }

  // Go exactly to the point
  [server movewindow: toPoint : windowNumber];
}

@end

@implementation GSSlideView

+ (BOOL) _slideImage: (NSImage *)image
	        from: (NSPoint)fromPoint
		  to: (NSPoint)toPoint
{
  static GSSlideView *v = nil;
  BOOL result = NO;

  if (image != nil)
    {
      if (v == nil)
	{
	  v = [[self alloc] init];
	}
      [NSApp preventWindowOrdering];
      [v _setupImage: image startPoint: fromPoint];
      [v _slideFrom: fromPoint to: toPoint];
      [[v window] orderOut: nil];

      result = YES;
    }
  return result;
}

- (id) init
{
  self = [super init];
  if (self != nil)
    {
      // This is never used, as the window gets resized before displaying
      NSRect winRect = {{0, 0}, {48, 48}};
      NSWindow *slideWindow;

      slideCell = [[NSCell alloc] initImageCell: nil];
      [slideCell setBordered: NO];

      slideWindow = [[NSWindow alloc] initWithContentRect: winRect
					   styleMask: NSBorderlessWindowMask
					     backing: NSBackingStoreNonretained
					       defer: NO];
      [slideWindow setReleasedWhenClosed: YES];
      [slideWindow setExcludedFromWindowsMenu: YES];
      [slideWindow setContentView: self];
      RELEASE (self);
    }

  return self;
}

- (void) dealloc
{
  RELEASE (slideCell);
  [super dealloc];
}

- (void) drawRect: (NSRect)rect
{
  [slideCell drawWithFrame: [self frame] inView: self];
}

@end