Resizable Borderless Window
Resizable Borderless Window
- Subject: Resizable Borderless Window
- From: Darkshadow <email@hidden>
- Date: Mon, 23 Feb 2004 20:02:19 -0500
Hey all, after reading the post by someone wondering how to make a
resizable borderless window, I thought I'd give it a try. I came up
with something that works, but I do have a couple of questions and
issues. Code for the window is at the bottom. Feel free to use it as
you wish. :)
First, does anybody see any resize related items that I may have
missed? I know I don't have the windowWillResize:toSize: delegate
message; I couldn't figure out how to send an NSSize to the delegate.
If anyone knows how I can achieve that, let me know!
Second, can somebody help me with drawing a resize grabber? In my
particular case, I'm using this subclass to display a NSTextView. The
window is totally invisible - all you see is the NSTextView. I can't
figure out how to draw one in this case. To make things more
difficult, the textview may or may not have a vertical scrollbar
(though it never has a horizontal one). I tried to simply fill the
resize rect with a solid color to see if I could get it to show up, but
that didn't work, in either the window subclass or the textview
subclass.
Also, two issues with the resizing:
1) once you start dragging, the bottom of the window is positioned at
the cursor, rather than however many pixels below the start position of
the cursor was. I know that this is because of how I'm setting the
sizes, but I'm not sure of how to calculate this number and add that to
the frame.
2) If you resize the window past its minSize and keep moving the cursor
to the left, once you start moving it to the right again, the window
starts resizing immediately. I'm not sure of how to keep the window
from resizing until the cursor is back to it's "original" position in
the window.
If anyone can point out what I'm doing wrong/need to change for those,
I'd appreciate it. The window resizing just doesn't seem quite right
with those two issues.
Note that the below code is generic - it doesn't make an invisible
borderless window, just a borderless window.
DSResizableBorderlessWindow.h
------------------------------
#import <Cocoa/Cocoa.h>
@interface DSResizableBorderlessWindow : NSWindow
{
NSPoint initialLocation;
}
- (NSSize)checkSize:(NSSize)currentSize;
@end
DSResizableBorderlessWindow.m
------------------------------
#import "DSResizableBorderlessWindow.h"
static BOOL resizing=NO;
@implementation DSResizableBorderlessWindow
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned
int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
if ((self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered
defer:NO])) {
//place any further initialization you need here
}
return self;
}
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (BOOL)canBecomeMainWindow
{
return YES;
}
- (NSSize)checkSize:(NSSize)currentSize
{
NSSize returnedSize = currentSize;
if (currentSize.width < [self minSize].width) {
returnedSize.width = [self minSize].width;
}
if (currentSize.height < [self minSize].height) {
returnedSize.height = [self minSize].height;
}
if (currentSize.width > [self maxSize].width) {
returnedSize.width = [self maxSize].width;
}
if (currentSize.height > [self maxSize].height) {
returnedSize.height = [self maxSize].height;
}
return returnedSize;
}
-(void)mouseDragged:(NSEvent *)theEvent
{
NSPoint currentLocation;
NSPoint newOrigin;
NSRect windowFrame = [self frame];
NSRect resizeRect = NSMakeRect((windowFrame.size.width - 26), 0,
windowFrame.size.width, 26);
currentLocation = [NSEvent mouseLocation];
if (resizing || NSMouseInRect(initialLocation, resizeRect, NO)) {
NSRect newFrame;
resizing=YES;
currentLocation.x -= windowFrame.origin.x;
currentLocation.y -= windowFrame.origin.y;
newFrame.size.width = windowFrame.size.width - (initialLocation.x -
currentLocation.x);
newFrame.size.height = windowFrame.size.height - currentLocation.y;
newFrame.size = [self checkSize:newFrame.size];
newFrame.origin = NSMakePoint(NSMinX(windowFrame),
NSMaxY(windowFrame) - NSHeight(newFrame));
[self setFrame:newFrame display:YES];
initialLocation = currentLocation;
} else {
newOrigin.x = currentLocation.x - initialLocation.x;
newOrigin.y = currentLocation.y - initialLocation.y;
[self setFrameOrigin:newOrigin];
}
}
-(void)mouseDown:(NSEvent *)theEvent
{
NSRect windowFrame = [self frame];
initialLocation = [self convertBaseToScreen:[theEvent
locationInWindow]];
initialLocation.x -= windowFrame.origin.x;
initialLocation.y -= windowFrame.origin.y;
}
- (void)mouseUp:(NSEvent *)theEvent
{
if (resizing) {
[[NSNotificationCenter defaultCenter]
postNotificationName:NSWindowDidResizeNotification object:self];
if (![[self frameAutosaveName] isEqualToString:@""]) {
NSRect frameRect=[self frame];
NSRect screenRect=[[self screen] frame];
BOOL mainScreen = [[[NSScreen screens] objectAtIndex:0]
isEqual:[self screen]];
screenRect.size.height = (mainScreen ? screenRect.size.height -
22 : screenRect.size.height);
NSString *frameString=[NSString stringWithFormat:@"%.0f %.0f %.0f
%.0f %.0f %.0f %.0f %.0f ",frameRect.origin.x, frameRect.origin.y,
frameRect.size.width, frameRect.size.height, screenRect.origin.x,
screenRect.origin.y, screenRect.size.width, screenRect.size.height];
NSString *keyString=[NSString stringWithFormat:@"NSWindow Frame
%@",[self frameAutosaveName]];
[[NSUserDefaults standardUserDefaults] setObject:frameString
forKey:keyString];
frameString=nil;
keyString=nil;
}
resizing=NO;
}
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.