/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ /* AbiWord / AbiBits / CarbonToolbar * * Copyright (C) 2003-2005 AbiSource, Inc. * Copyright (C) 2003-2005 Francis James Franklin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #include #import "AbiCarbonToolbar_Window.h" #import "AbiCarbonToolbar_Manager.h" pascal OSStatus CarbonToolbar_DoWindowClose (EventHandlerCallRef nextHandler, EventRef theEvent, void * userData) { AbiCarbonToolbar_Window * toolbar = (AbiCarbonToolbar_Window *) userData; if (toolbar) [toolbar toggle:nil]; return noErr; } @implementation AbiCarbonToolbar_Window - (id)initWithTitle:(NSString *)title frame:(NSRect)frame { if (self = [super initWithWindow:nil]) { NSURL * url = 0; NSBundle * bundle = 0; NSWindow * window = 0; CFBundleRef bundleRef = 0; IBNibRef nibRef = 0; WindowRef winref = 0; OSStatus err; EventTypeSpec eventType; EventHandlerUPP handlerUPP; HISize minSize; HISize maxSize; m_title = title; [m_title retain]; m_menuitem = [[NSMenuItem alloc] initWithTitle:title action:@selector(toggle:) keyEquivalent:@""]; [m_menuitem setTarget:self]; bundle = [NSBundle bundleForClass:[self class]]; url = [NSURL fileURLWithPath:[bundle bundlePath]]; /* Carbon Window */ bundleRef = CFBundleCreate (kCFAllocatorDefault, (CFURLRef) url); err = CreateNibReferenceWithCFBundle (bundleRef, CFSTR("CarbonToolbar"), &nibRef); CFRelease(bundleRef); if (err != noErr) { [self release]; return nil; } err = CreateWindowFromNib (nibRef, CFSTR("CarbonToolbar"), &winref); DisposeNibReference (nibRef); if (err != noErr) { [self release]; return nil; } m_MinimumWidth = 30.0f; m_FixedHeight = frame.size.height; minSize.width = m_MinimumWidth; maxSize.width = 10000.0f; minSize.height = m_FixedHeight; maxSize.height = m_FixedHeight; SetWindowResizeLimits (winref, &minSize, &maxSize); /* Create Cocoa Window from Carbon Window */ window = (NSWindow *) [[NSWindow alloc] initWithWindowRef:winref]; if (!window) { [self release]; return nil; } [window setFrame:frame display:NO]; // [window setMinSize:NSMakeSize(m_MinimumWidth,m_FixedHeight)]; // [window setMaxSize:NSMakeSize(10000.0f,m_FixedHeight)]; /* Carbon Window Events */ eventType.eventClass = kEventClassWindow; eventType.eventKind = kEventWindowClose; handlerUPP = NewEventHandlerUPP (CarbonToolbar_DoWindowClose); InstallWindowEventHandler (winref, handlerUPP, 1, &eventType, self, 0); /* Cocoa Window Setup */ [window setExcludedFromWindowsMenu:YES]; [window setTitle:@""]; [window setReleasedWhenClosed:YES]; [self setWindow:window]; m_Manager = [[AbiCarbonToolbar_Manager alloc] init]; [window setContentView:m_Manager]; [m_Manager release]; } return self; } - (void)dealloc { [m_title release]; [m_menuitem release]; [super dealloc]; } - (void)setMinimumWidth:(float)width { // TODO: change window width if necessary m_MinimumWidth = width; } - (void)setFixedHeight:(float)height { // TODO: change window height if necessary m_FixedHeight = height; } - (NSString *)title { return m_title; } - (NSMenuItem *)menuItem { return m_menuitem; } - (AbiCarbonToolbar_Manager *)manager { return m_Manager; } - (IBAction)toggle:(id)sender { if ([m_menuitem state] == NSOnState) { [[self window] orderOut:self]; [m_menuitem setState:NSOffState]; } else { [[self window] orderFront:self]; [m_menuitem setState:NSOnState]; } } @end