1/* GormScrollViewEditor.m
2 *
3 * Copyright (C) 2002 Free Software Foundation, Inc.
4 *
5 * Author:	Pierre-Yves Rivaille <pyrivail@ens-lyon.fr>
6 * Author:      Gregory John Casamento <greg_casamento@yahoo.com>
7 * Date:	2002
8 *
9 * This file is part of GNUstep.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
24 */
25
26#include <AppKit/AppKit.h>
27#include <InterfaceBuilder/IBObjectAdditions.h>
28#include "GormPrivate.h"
29#include "GormBoxEditor.h"
30#include "GormViewKnobs.h"
31
32@implementation NSScrollView (IBObjectAdditions)
33- (NSString *) inspectorClassName
34{
35  return @"GormScrollViewAttributesInspector";
36}
37
38- (NSString*) editorClassName
39{
40  return @"GormScrollViewEditor";
41}
42@end
43
44#define _EO ((NSScrollView *)_editedObject)
45
46@interface GormScrollViewEditor : GormViewWithSubviewsEditor
47{
48  GormInternalViewEditor *documentViewEditor;
49}
50@end
51
52@implementation GormScrollViewEditor
53
54- (void) setOpened: (BOOL) flag
55{
56  [super setOpened: flag];
57  if (flag == YES)
58    {
59      [document setSelectionFromEditor: documentViewEditor];
60    }
61}
62
63- (BOOL) activate
64{
65  if ([super activate])
66    {
67      NSView *documentView = [_EO documentView];
68
69      NSDebugLog(@"documentView %@", documentView);
70      documentViewEditor = (GormInternalViewEditor *)[document
71						       editorForObject: documentView
72						       inEditor: self
73						       create: YES];
74      return YES;
75    }
76
77  return NO;
78}
79
80- (void) deactivate
81{
82  if (activated == YES)
83    {
84      [self deactivateSubeditors];
85
86      [super deactivate];
87    }
88}
89
90- (void) mouseDown: (NSEvent *) theEvent
91{
92  BOOL onKnob = NO;
93
94  if ([parent respondsToSelector: @selector(selection)] &&
95      [[parent selection] containsObject: _EO])
96    {
97      IBKnobPosition knob = IBNoneKnobPosition;
98      NSPoint mouseDownPoint =
99	[self convertPoint: [theEvent locationInWindow]
100	      fromView: nil];
101      knob = GormKnobHitInRect([self bounds],
102			       mouseDownPoint);
103      if (knob != IBNoneKnobPosition)
104	{
105	  onKnob = YES;
106	}
107    }
108
109  if (onKnob == YES)
110    {
111      if (parent)
112	{
113	  return [parent mouseDown: theEvent];
114	}
115      else
116	{
117	  return [self noResponderFor: @selector(mouseDown:)];
118	}
119    }
120
121  if (opened == NO)
122    {
123      [super mouseDown: theEvent];
124      return;
125    }
126
127  if ([[_EO hitTest: [theEvent locationInWindow]]
128	isDescendantOf: documentViewEditor])
129    {
130      if (([self isOpened] == YES) && ([documentViewEditor isOpened] == NO))
131	{
132	  [documentViewEditor setOpened: YES];
133	}
134      if ([documentViewEditor isOpened])
135	{
136	  [documentViewEditor mouseDown: theEvent];
137	}
138    }
139  else
140    {
141      NSView *v = [_EO hitTest: [theEvent locationInWindow]];
142      id r = [v nextResponder];
143
144      if([v respondsToSelector: @selector(setNextResponder:)])
145	{
146	  // this is done to prevent a responder loop.
147	  [v setNextResponder: nil];
148	  [v mouseDown: theEvent];
149	  [v setNextResponder: r];
150	}
151      else
152	{
153	  [v mouseDown: theEvent];
154	}
155    }
156}
157
158- (void) dealloc
159{
160  RELEASE(selection);
161  [super dealloc];
162}
163
164- (id) initWithObject: (id)anObject
165	   inDocument: (id<IBDocuments>)aDocument
166{
167  opened = NO;
168  openedSubeditor = nil;
169
170  if ((self = [super initWithObject: anObject
171		     inDocument: aDocument]) == nil)
172    {
173      return nil;
174    }
175
176  selection = [[NSMutableArray alloc] initWithCapacity: 5];
177  [self registerForDraggedTypes: [NSArray arrayWithObjects: IBViewPboardType,
178					  GormLinkPboardType,
179					  IBFormatterPboardType,
180					  nil]];
181
182  return self;
183}
184
185- (NSArray *)destroyAndListSubviews
186{
187  id documentView = [_EO documentView];
188  NSArray *subviews = [documentView subviews];
189  NSMutableArray *newSelection = [NSMutableArray array];
190
191  if([documentView conformsToProtocol: @protocol(IBEditors)] == YES)
192    {
193      id internalView = [subviews objectAtIndex: 0];
194      NSEnumerator *enumerator = [[internalView subviews] objectEnumerator];
195      GormViewEditor *subview;
196
197      if([[documentView editedObject] isKindOfClass: [NSTextView class]])
198        return newSelection;
199
200      [parent makeSubeditorResign];
201      while ((subview = [enumerator nextObject]) != nil)
202	{
203	  id v;
204	  NSRect frame;
205
206	  v = [subview editedObject];
207	  frame = [v frame];
208	  frame = [parent convertRect: frame fromView: _EO];
209	  [subview deactivate];
210	  [v setFrame: frame];
211	  [newSelection addObject: v];
212	}
213    }
214  else
215    {
216      NSRect frame = [documentView frame];
217
218      if([documentView isKindOfClass: [NSTextView class]])
219        return newSelection;
220
221      // In this case the view editor is the documentView and
222      // we need to add the internal view back into the superview
223      frame = [parent convertRect: frame fromView: _EO];
224      [documentView setFrame: frame];
225      [newSelection addObject: documentView];
226      [_EO setDocumentView: nil];
227    }
228
229  [self close];
230  return newSelection;
231}
232@end
233