1/* GormViewWindow.m
2 *
3 * Copyright (C) 2004 Free Software Foundation, Inc.
4 *
5 * Author:	Gregory John Casamento <greg_casamento@yahoo.com>
6 * Date:	2004
7 *
8 * This file is part of GNUstep.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
23 */
24
25#include "GormViewWindow.h"
26#include <AppKit/NSWindow.h>
27#include <AppKit/NSView.h>
28#include <AppKit/NSColor.h>
29#include <Foundation/NSNotification.h>
30#include <Foundation/NSException.h>
31#include <InterfaceBuilder/InterfaceBuilder.h>
32#include "GormFunctions.h"
33
34@interface GormViewWindowDelegate : NSObject
35{
36  NSView *_view;
37}
38
39- (id) initWithView: (NSView *)view;
40- (void) initialResize;
41@end
42
43@implementation GormViewWindowDelegate
44
45- (id) initWithView: (NSView *)view;
46{
47  if((self = [super init]) != nil)
48    {
49      _view = view;
50      [self initialResize];
51    }
52  return self;
53}
54
55- (void) initialResize
56{
57  NSWindow *window = [_view window];
58  NSRect windowFrame = [window frame];
59
60  // if the view is uninitialized,
61  // it's new... give it size.
62  if(NSIsEmptyRect([_view frame]))
63    {
64      NSArray *subs = [_view subviews];
65      NSRect newFrame;
66
67      if([subs count] > 0)
68	{
69	  newFrame = minimalContainerFrame(subs);
70	  newFrame.size.height += 70;
71	  newFrame.size.width += 40;
72	  [window setFrame: newFrame display: YES];
73	  [_view setPostsFrameChangedNotifications: YES];
74	}
75      else
76	{
77	  newFrame = windowFrame;
78
79	  newFrame.origin.x = 10;
80	  newFrame.origin.y = 20;
81	  newFrame.size.height -= 70;
82	  newFrame.size.width -= 20;
83	}
84
85      [_view setPostsFrameChangedNotifications: NO];
86      [_view setFrame: newFrame];
87      [_view setPostsFrameChangedNotifications: YES];
88    }
89  else // otherwise take size from it.
90    {
91      NSRect newFrame = [_view frame];
92
93      newFrame.origin.x = windowFrame.origin.x+10;
94      newFrame.origin.y = windowFrame.origin.y+20;
95      newFrame.size.height += 100;
96      newFrame.size.width += 20;
97
98      [_view setPostsFrameChangedNotifications: NO];
99      [_view setFrame: newFrame];
100      [_view setPostsFrameChangedNotifications: YES];
101      [window setFrame: newFrame display: YES];
102    }
103
104  [window center];
105}
106
107- (void) windowDidResize: (NSNotification *)notification
108{
109  NSWindow *window = [_view window];
110  NSRect windowFrame = [window frame];
111  NSRect newFrame = windowFrame;
112  NSRect viewFrame = [_view frame];
113
114  newFrame.origin.x = 10;
115  newFrame.origin.y = 20;
116  newFrame.size.height -= 70;
117  newFrame.size.width -= 20;
118
119  if(NSIsEmptyRect(viewFrame))
120    {
121      [_view setPostsFrameChangedNotifications: NO];
122      [_view setFrame: newFrame];
123      [_view setPostsFrameChangedNotifications: YES];
124    }
125  else
126    {
127      [_view setFrame: newFrame];
128      [_view setNeedsDisplay: YES];
129    }
130}
131
132@end
133
134
135@implementation GormViewWindow
136
137- (id) initWithView: (NSView *)view
138{
139  if((self = [super init]) != nil)
140    {
141      NSString *className = NSStringFromClass([view class]);
142      NSString *objectName = [[(id<IB>)NSApp activeDocument] nameForObject: view];
143      NSString *title = [NSString stringWithFormat: @"Standalone View Window: (%@, %@)",
144				  className, objectName];
145      NSColor *color = [NSColor lightGrayColor];
146
147      [self setTitle: title];
148      [self setFrame: NSMakeRect(0,0,400,300) display: YES];
149      [self setBackgroundColor: color];
150      [self setReleasedWhenClosed: NO];
151      [self setView: view];
152    }
153  return self;
154}
155
156- (void) setView: (NSView *)view
157{
158  if(_view != nil)
159    {
160      [_view removeFromSuperviewWithoutNeedingDisplay];
161    }
162
163  _view = view;
164
165  [[self contentView] addSubview: _view];
166  RELEASE([self delegate]);
167  [self setDelegate: [[GormViewWindowDelegate alloc] initWithView: _view]];
168}
169
170- (NSView *) view
171{
172  return _view;
173}
174
175- (void) activateEditorForView
176{
177  id editor = [[(id<IB>)NSApp activeDocument] editorForObject: _view create: YES];
178  // NSArray *subviews = [_view subviews];
179  // NSEnumerator *en = [subviews objectEnumerator];
180  // id sub = nil;
181
182  // activate the parent and all subview editors...
183  [editor activate];
184  /*
185  while((sub = [en nextObject]) != nil)
186    {
187      editor = [[(id<IB>)NSApp activeDocument] editorForObject: sub create: YES];
188      [editor activate];
189    }
190  */
191}
192
193- (void) encodeWithCoder: (NSCoder *)coder
194{
195  [NSException raise: NSInternalInconsistencyException
196	       format: @"Cannot encode a GormViewWindow"];
197}
198
199- (void) orderFront: (id)sender
200{
201  [super orderFront: sender];
202  [self activateEditorForView];
203}
204
205- (void) dealloc
206{
207  RELEASE([self delegate]);
208  [self setDelegate: nil];
209  [super dealloc];
210}
211
212@end
213
214