Commit efa2f96d authored by Brendon Justin's avatar Brendon Justin Committed by Jean-Baptiste Kempf

macosx: Lua extensions infrastructure

Add files for extension creation management, add them to the build process, and
add them to the XCode project.
Signed-off-by: default avatarFelix Paul Kühne <fkuehne@videolan.org>
parent 4c0f1687
...@@ -286,6 +286,12 @@ ...@@ -286,6 +286,12 @@
1058C7ADFEA557BF11CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; }; 1058C7ADFEA557BF11CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
2AEF857609A5FEC900130822 /* fspanel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = fspanel.h; path = ../../../modules/gui/macosx/fspanel.h; sourceTree = SOURCE_ROOT; }; 2AEF857609A5FEC900130822 /* fspanel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = fspanel.h; path = ../../../modules/gui/macosx/fspanel.h; sourceTree = SOURCE_ROOT; };
2AEF857709A5FEC900130822 /* fspanel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = fspanel.m; path = ../../../modules/gui/macosx/fspanel.m; sourceTree = SOURCE_ROOT; }; 2AEF857709A5FEC900130822 /* fspanel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = fspanel.m; path = ../../../modules/gui/macosx/fspanel.m; sourceTree = SOURCE_ROOT; };
5CCED71014C0D4A90057F8D1 /* ExtensionsDialogProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ExtensionsDialogProvider.h; path = ../../../modules/gui/macosx/ExtensionsDialogProvider.h; sourceTree = SOURCE_ROOT; };
5CCED71114C0D4A90057F8D1 /* ExtensionsDialogProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ExtensionsDialogProvider.m; path = ../../../modules/gui/macosx/ExtensionsDialogProvider.m; sourceTree = SOURCE_ROOT; };
5CCED71214C0D4A90057F8D1 /* ExtensionsManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ExtensionsManager.h; path = ../../../modules/gui/macosx/ExtensionsManager.h; sourceTree = SOURCE_ROOT; };
5CCED71314C0D4A90057F8D1 /* ExtensionsManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ExtensionsManager.m; path = ../../../modules/gui/macosx/ExtensionsManager.m; sourceTree = SOURCE_ROOT; };
5CCED71414C0D4A90057F8D1 /* VLCUIWidgets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = VLCUIWidgets.h; path = ../../../modules/gui/macosx/VLCUIWidgets.h; sourceTree = SOURCE_ROOT; };
5CCED71514C0D4A90057F8D1 /* VLCUIWidgets.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = VLCUIWidgets.m; path = ../../../modules/gui/macosx/VLCUIWidgets.m; sourceTree = SOURCE_ROOT; };
8E49720006417F6800370C9F /* playlistinfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = playlistinfo.h; path = ../../../modules/gui/macosx/playlistinfo.h; sourceTree = SOURCE_ROOT; }; 8E49720006417F6800370C9F /* playlistinfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = playlistinfo.h; path = ../../../modules/gui/macosx/playlistinfo.h; sourceTree = SOURCE_ROOT; };
8E49720106417F6800370C9F /* playlistinfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = playlistinfo.m; path = ../../../modules/gui/macosx/playlistinfo.m; sourceTree = SOURCE_ROOT; }; 8E49720106417F6800370C9F /* playlistinfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = playlistinfo.m; path = ../../../modules/gui/macosx/playlistinfo.m; sourceTree = SOURCE_ROOT; };
8E55FB7F0459B0FD00FB3317 /* output.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = output.h; path = ../../../modules/gui/macosx/output.h; sourceTree = SOURCE_ROOT; }; 8E55FB7F0459B0FD00FB3317 /* output.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = output.h; path = ../../../modules/gui/macosx/output.h; sourceTree = SOURCE_ROOT; };
...@@ -706,6 +712,12 @@ ...@@ -706,6 +712,12 @@
CC448A6313B68A0B009F72E0 /* MainWindow.m */, CC448A6313B68A0B009F72E0 /* MainWindow.m */,
CC4A33220F8CB017000FC4A7 /* coredialogs.h */, CC4A33220F8CB017000FC4A7 /* coredialogs.h */,
CC4A33210F8CB017000FC4A7 /* coredialogs.m */, CC4A33210F8CB017000FC4A7 /* coredialogs.m */,
5CCED71014C0D4A90057F8D1 /* ExtensionsDialogProvider.h */,
5CCED71114C0D4A90057F8D1 /* ExtensionsDialogProvider.m */,
5CCED71214C0D4A90057F8D1 /* ExtensionsManager.h */,
5CCED71314C0D4A90057F8D1 /* ExtensionsManager.m */,
5CCED71414C0D4A90057F8D1 /* VLCUIWidgets.h */,
5CCED71514C0D4A90057F8D1 /* VLCUIWidgets.m */,
8E6BC6FA041684EC0059A3A7 /* controls.h */, 8E6BC6FA041684EC0059A3A7 /* controls.h */,
8ED6C27B03E2EB1C0059A3A7 /* controls.m */, 8ED6C27B03E2EB1C0059A3A7 /* controls.m */,
8E6BC6F6041643860059A3A7 /* applescript.h */, 8E6BC6F6041643860059A3A7 /* applescript.h */,
......
/*****************************************************************************
* ExtensionsDialogProvider.h: Mac OS X Extensions Dialogs
*****************************************************************************
* Copyright (C) 2005-2012 VLC authors and VideoLAN
* $Id$
*
* Authors: Brendon Justin <brendonjustin at gmail.com>,
* Derk-Jan Hartman <hartman at videolan dot org>,
* Felix Paul Kühne <fkuehne at videolan dot org>
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#import "coredialogs.h"
#import "intf.h"
#import "VLCUIWidgets.h"
#import <vlc_common.h>
#import <vlc_dialog.h>
#import <vlc_extensions.h>
#import <Cocoa/Cocoa.h>
/*****************************************************************************
* ExtensionsDialogProvider interface
*****************************************************************************/
at interface ExtensionsDialogProvider : NSObject <NSWindowDelegate>
{
intf_thread_t *p_intf;
}
+ (ExtensionsDialogProvider *)sharedInstance:(intf_thread_t *)_p_intf;
+ (void)killInstance;
- (id)initWithIntf:(intf_thread_t *)_p_intf;
- (void)performEventWithObject: (NSValue *)o_value ofType:(const char*)type;
- (void)triggerClick:(id)sender;
- (void)syncTextField:(NSNotification *)notifcation;
- (void)tableViewSelectionDidChange:(NSNotification *)notifcation;
- (void)popUpSelectionChanged:(id)sender;
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize;
- (BOOL)windowShouldClose:(id)sender;
- (void)updateWidgets:(extension_dialog_t *)dialog;
- (VLCDialogWindow *)createExtensionDialog:(extension_dialog_t *)p_dialog;
- (int)destroyExtensionDialog:(extension_dialog_t *)o_value;
- (VLCDialogWindow *)updateExtensionDialog:(NSValue *)o_value;
- (void)manageDialog:(extension_dialog_t *)p_dialog;
at end
/*****************************************************************************
* ExtensionsDialogProvider.m: Mac OS X Extensions Dialogs
*****************************************************************************
* Copyright (C) 2005-2012 VLC authors and VideoLAN
* $Id$
*
* Authors: Brendon Justin <brendonjustin at gmail.com>,
* Derk-Jan Hartman <hartman at videolan dot org>,
* Felix Paul Kühne <fkuehne at videolan dot org>
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#import "ExtensionsDialogProvider.h"
#import "intf.h"
#import "ExtensionsManager.h"
#import "misc.h"
#import "VLCUIWidgets.h"
#import <WebKit/WebKit.h>
#import <stdlib.h>
/*****************************************************************************
* VLCExtensionsDialogProvider implementation
*****************************************************************************/
static int dialogCallback( vlc_object_t *p_this, const char *psz_variable,
vlc_value_t old_val, vlc_value_t new_val,
void *param );
static NSView *createControlFromWidget(extension_widget_t *widget, id self)
{
assert(!widget->p_sys_intf);
switch (widget->type)
{
case EXTENSION_WIDGET_HTML:
{
WebView *webView = [[WebView alloc] initWithFrame:NSMakeRect (0,0,1,1)];
[webView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
[webView setDrawsBackground:NO];
return webView;
}
case EXTENSION_WIDGET_LABEL:
{
NSTextField *field = [[NSTextField alloc] init];
[field setEditable:NO];
[field setBordered:NO];
[field setDrawsBackground:NO];
[field setFont:[NSFont systemFontOfSize:0]];
[[field cell] setControlSize:NSRegularControlSize];
[field setAutoresizingMask:NSViewNotSizable];
return field;
}
case EXTENSION_WIDGET_TEXT_FIELD:
{
VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
[field setWidget:widget];
[field setAutoresizingMask:NSViewWidthSizable];
[field setFont:[NSFont systemFontOfSize:0]];
[[field cell] setControlSize:NSRegularControlSize];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:) name:NSControlTextDidChangeNotification object:field];
return field;
}
case EXTENSION_WIDGET_BUTTON:
{
VLCDialogButton *button = [[VLCDialogButton alloc] init];
[button setBezelStyle:NSRoundedBezelStyle];
[button setWidget:widget];
[button setAction:@selector(triggerClick:)];
[button setTarget:self];
[[button cell] setControlSize:NSRegularControlSize];
[button setAutoresizingMask:NSViewNotSizable];
return button;
}
case EXTENSION_WIDGET_DROPDOWN:
{
VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
[popup setAction:@selector(popUpSelectionChanged:)];
[popup setTarget:self];
[popup setWidget:widget];
return popup;
}
case EXTENSION_WIDGET_LIST:
{
NSScrollView *scrollView = [[NSScrollView alloc] init];
[scrollView setHasVerticalScroller:YES];
VLCDialogList *list = [[VLCDialogList alloc] init];
[list setUsesAlternatingRowBackgroundColors:YES];
[list setHeaderView:nil];
[scrollView setDocumentView:list];
[scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
NSTableColumn *column = [[NSTableColumn alloc] init];
[list addTableColumn:column];
[column release];
[list setDataSource:list];
[list setDelegate:self];
[list setWidget:widget];
[list release];
return scrollView;
}
case EXTENSION_WIDGET_IMAGE:
{
NSImageView *imageView = [[NSImageView alloc] init];
[imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
[imageView setImageFrameStyle:NSImageFramePhoto];
[imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
return imageView;
}
case EXTENSION_WIDGET_SPIN_ICON:
{
NSProgressIndicator *spinner = [[NSProgressIndicator alloc] init];
[spinner setUsesThreadedAnimation:YES];
[spinner setStyle:NSProgressIndicatorSpinningStyle];
[spinner setDisplayedWhenStopped:YES];
[spinner startAnimation:self];
return spinner;
}
default:
return nil;
}
}
static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
{
switch (widget->type)
{
case EXTENSION_WIDGET_HTML:
{
// Get the web view
assert([control isKindOfClass:[WebView class]]);
WebView *webView = (WebView *)control;
NSString *string = [NSString stringWithUTF8String:widget->psz_text];
[[webView mainFrame] loadHTMLString:string baseURL:[NSURL URLWithString:@""]];
[webView setNeedsDisplay:YES];
break;
}
{
assert([control isKindOfClass:[NSTextView class]]);
NSTextView *textView = (NSTextView *)control;
NSString *string = [NSString stringWithUTF8String:widget->psz_text];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
[[textView textStorage] setAttributedString:attrString];
[textView setNeedsDisplay:YES];
[textView scrollRangeToVisible:NSMakeRange(0, 0)];
[attrString release];
break;
}
case EXTENSION_WIDGET_LABEL:
case EXTENSION_WIDGET_PASSWORD:
case EXTENSION_WIDGET_TEXT_FIELD:
{
if (!widget->psz_text)
break;
assert([control isKindOfClass:[NSControl class]]);
NSControl *field = (NSControl *)control;
NSString *string = [NSString stringWithUTF8String:widget->psz_text];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
[field setAttributedStringValue:attrString];
[attrString release];
break;
}
case EXTENSION_WIDGET_CHECK_BOX:
case EXTENSION_WIDGET_BUTTON:
{
assert([control isKindOfClass:[NSButton class]]);
NSButton *button = (NSButton *)control;
if (!widget->psz_text)
break;
[button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
break;
}
case EXTENSION_WIDGET_DROPDOWN:
{
assert([control isKindOfClass:[NSPopUpButton class]]);
NSPopUpButton *popup = (NSPopUpButton *)control;
[popup removeAllItems];
struct extension_widget_value_t *value;
for(value = widget->p_values; value != NULL; value = value->p_next)
{
[popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
}
[popup synchronizeTitleAndSelectedItem];
[self popUpSelectionChanged:popup];
break;
}
case EXTENSION_WIDGET_LIST:
{
assert([control isKindOfClass:[NSScrollView class]]);
NSScrollView *scrollView = (NSScrollView *)control;
assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
NSMutableArray *contentArray = [NSMutableArray array];
struct extension_widget_value_t *value;
for(value = widget->p_values; value != NULL; value = value->p_next)
{
NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:value->i_id], @"id",
[NSString stringWithUTF8String:value->psz_text], @"text",
nil];
[contentArray addObject:entry];
}
list.contentArray = contentArray;
[list reloadData];
break;
}
case EXTENSION_WIDGET_IMAGE:
{
assert([control isKindOfClass:[NSImageView class]]);
NSImageView *imageView = (NSImageView *)control;
NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
NSImage *image = nil;
if (string)
image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
[imageView setImage:image];
[image release];
break;
}
case EXTENSION_WIDGET_SPIN_ICON:
{
assert([control isKindOfClass:[NSProgressIndicator class]]);
NSProgressIndicator *progressIndicator = (NSProgressIndicator *)control;
if( widget->i_spin_loops != 0 )
[progressIndicator startAnimation:self];
else
[progressIndicator stopAnimation:self];
break;
}
}
}
/**
* Ask the dialogs provider to create a new dialog
**/
static int dialogCallback( vlc_object_t *p_this, const char *psz_variable,
vlc_value_t old_val, vlc_value_t new_val,
void *param )
{
(void) p_this;
(void) psz_variable;
(void) old_val;
(void) param;
ExtensionsDialogProvider *p_edp = [ExtensionsDialogProvider sharedInstance:(intf_thread_t *)p_this];
if( !p_edp )
return VLC_EGENERIC;
if( !new_val.p_address )
return VLC_EGENERIC;
extension_dialog_t *p_dialog = ( extension_dialog_t* ) new_val.p_address;
[p_edp manageDialog:p_dialog];
return VLC_SUCCESS;
}
at implementation ExtensionsDialogProvider
static ExtensionsDialogProvider *_o_sharedInstance = nil;
+ (ExtensionsDialogProvider *)sharedInstance:(intf_thread_t *)_p_intf
{
return _o_sharedInstance ? _o_sharedInstance : [[self alloc] initWithIntf:_p_intf];
}
+ (void)killInstance
{
if (_o_sharedInstance)
{
[_o_sharedInstance release];
}
}
- (id)initWithIntf:(intf_thread_t *)_p_intf
{
if( _o_sharedInstance )
[self dealloc];
if ((self = [super init]))
{
_o_sharedInstance = self;
p_intf = _p_intf;
// The Cocoa interface already called dialog_Register()
var_Create( p_intf, "dialog-extension", VLC_VAR_ADDRESS );
var_AddCallback( p_intf, "dialog-extension", dialogCallback, NULL );
}
return _o_sharedInstance;
}
- (void)dealloc
{
msg_Dbg( p_intf, "ExtensionsDialogProvider is quitting..." );
var_DelCallback( p_intf, "dialog-extension", dialogCallback, NULL );
[super dealloc];
}
- (void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
{
NSString *o_type = [NSString stringWithUTF8String:type];
if( [o_type isEqualToString: @"dialog-extension"] )
{
[self performSelectorOnMainThread:@selector(updateExtensionDialog:)
withObject:o_value
waitUntilDone:YES];
}
else
msg_Err( VLCIntf, "unhandled dialog type: '%s'", type );
}
- (void)triggerClick:(id)sender
{
assert([sender isKindOfClass:[VLCDialogButton class]]);
VLCDialogButton *button = sender;
extension_widget_t *widget = [button widget];
vlc_mutex_lock(&widget->p_dialog->lock);
extension_WidgetClicked(widget->p_dialog, widget);
vlc_mutex_unlock(&widget->p_dialog->lock);
}
- (void)syncTextField:(NSNotification *)notifcation
{
id sender = [notifcation object];
assert([sender isKindOfClass:[VLCDialogTextField class]]);
VLCDialogTextField *field = sender;
extension_widget_t *widget = [field widget];
vlc_mutex_lock(&widget->p_dialog->lock);
free(widget->psz_text);
widget->psz_text = strdup([[field stringValue] UTF8String]);
vlc_mutex_unlock(&widget->p_dialog->lock);
}
- (void)tableViewSelectionDidChange:(NSNotification *)notifcation
{
id sender = [notifcation object];
assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
VLCDialogList *list = sender;
struct extension_widget_value_t *value;
unsigned i = 0;
for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
value->b_selected = (i == [list selectedRow]);
}
- (void)popUpSelectionChanged:(id)sender
{
assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
VLCDialogPopUpButton *popup = sender;
struct extension_widget_value_t *value;
unsigned i = 0;
for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
value->b_selected = (i == [popup indexOfSelectedItem]);
}
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
NSView *contentView = [sender contentView];
assert([contentView isKindOfClass:[VLCDialogGridView class]]);
VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;
NSRect rect = NSMakeRect(0, 0, 0, 0);
rect.size = frameSize;
rect = [sender contentRectForFrameRect:rect];
rect.size = [gridView flexSize:rect.size];
rect = [sender frameRectForContentRect:rect];
return rect.size;
}
- (BOOL)windowShouldClose:(id)sender
{
assert([sender isKindOfClass:[VLCDialogWindow class]]);
VLCDialogWindow *window = sender;
extension_dialog_t *dialog = [window dialog];
extension_DialogClosed(dialog);
dialog->p_sys_intf = NULL;
return YES;
}
- (void)updateWidgets:(extension_dialog_t *)dialog
{
extension_widget_t *widget;
VLCDialogWindow *dialogWindow = dialog->p_sys_intf;
FOREACH_ARRAY(widget, dialog->widgets)
{
if (!widget)
continue; /* Some widgets may be NULL at this point */
BOOL shouldDestroy = widget->b_kill;
NSView *control = widget->p_sys_intf;
BOOL update = widget->b_update;
if (!control && !shouldDestroy)
{
control = createControlFromWidget(widget, self);
updateControlFromWidget(control, widget, self);
widget->p_sys_intf = control;
update = YES; // Force update and repositionning
[control setHidden:widget->b_hide];
}
if (update && !shouldDestroy)
{
updateControlFromWidget(control, widget, self);
[control setHidden:widget->b_hide];
int row = widget->i_row - 1;
int col = widget->i_column - 1;
int hsp = __MAX( 1, widget->i_horiz_span );
int vsp = __MAX( 1, widget->i_vert_span );
if( row < 0 )
{
row = 4;
col = 0;
}
VLCDialogGridView *gridView = (VLCDialogGridView *)[dialogWindow contentView];
[gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
widget->b_update = false;
}
if (shouldDestroy)
{
VLCDialogGridView *gridView = (VLCDialogGridView *)[dialogWindow contentView];
[gridView removeSubview:control];
[control release];
widget->p_sys_intf = NULL;
}
}
FOREACH_END()
}
/** Create a dialog
* Note: Lock on p_dialog->lock must be held. */
- (VLCDialogWindow *)createExtensionDialog:(extension_dialog_t *)p_dialog
{
VLCDialogWindow *dialogWindow = nil;
BOOL shouldDestroy = p_dialog->b_kill;
if (!shouldDestroy)
{
NSRect content = NSMakeRect(0, 0, 1, 1);
dialogWindow = [[VLCDialogWindow alloc] initWithContentRect:content
styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[dialogWindow setDelegate:self];
[dialogWindow setDialog:p_dialog];
[dialogWindow setTitle:[NSString stringWithUTF8String:p_dialog->psz_title]];
VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
[gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
[dialogWindow setContentView:gridView];
[gridView release];
p_dialog->p_sys_intf = (void *)dialogWindow;
}
[self updateWidgets:p_dialog];
if (shouldDestroy)
{
[dialogWindow setDelegate:nil];
[dialogWindow close];
p_dialog->p_sys_intf = NULL;
dialogWindow = nil;
}
return dialogWindow;
}
/** Destroy a dialog
* Note: Lock on p_dialog->lock must be held. */
- (int)destroyExtensionDialog:(extension_dialog_t *)p_dialog
{
assert( p_dialog );
VLCDialogWindow *dialogWindow = ( VLCDialogWindow* ) p_dialog->p_sys_intf;
if( !dialogWindow )
return VLC_EGENERIC;
[VLCDialogWindow release];
p_dialog->p_sys_intf = NULL;
vlc_cond_signal( &p_dialog->cond );
return VLC_SUCCESS;
}
/**
* Update/Create/Destroy a dialog
**/
- (VLCDialogWindow *)updateExtensionDialog:(NSValue *)o_value
{
extension_dialog_t *p_dialog = [o_value pointerValue];
VLCDialogWindow *dialogWindow = ( VLCDialogWindow* ) p_dialog->p_sys_intf;
if( p_dialog->b_kill && !dialogWindow )
{
/* This extension could not be activated properly but tried
to create a dialog. We must ignore it. */
return NULL;
}
vlc_mutex_lock(&p_dialog->lock);
if( !p_dialog->b_kill && !dialogWindow )
{
dialogWindow = [self createExtensionDialog:p_dialog];
BOOL visible = !p_dialog->b_hide;
if (visible)
{
[dialogWindow center];
[dialogWindow makeKeyAndOrderFront:self];
}
else
{
[dialogWindow orderOut:nil];
}
[dialogWindow setHas_lock:NO];
}
else if( !p_dialog->b_kill && dialogWindow )
{
[dialogWindow setHas_lock:YES];
[self updateWidgets:p_dialog];
if( strcmp( [[dialogWindow title] UTF8String],
p_dialog->psz_title ) != 0 )
{
NSString *titleString = [NSString stringWithCString:p_dialog->psz_title
encoding:NSASCIIStringEncoding];
[dialogWindow setTitle:titleString];
}
[dialogWindow setHas_lock:NO];
BOOL visible = !p_dialog->b_hide;
if (visible)
{
[dialogWindow center];
[dialogWindow makeKeyAndOrderFront:self];
}
else
{
[dialogWindow orderOut:nil];
}
}
else if( p_dialog->b_kill )
{
[self destroyExtensionDialog:p_dialog];
}
vlc_cond_signal( &p_dialog->cond );
vlc_mutex_unlock( &p_dialog->lock );
return dialogWindow;
}
/**
* Ask the dialog manager to create/update/kill the dialog. Thread-safe.
**/
- (void)manageDialog:( extension_dialog_t *)p_dialog
{
assert( p_dialog );
ExtensionsManager *extMgr = [ExtensionsManager getInstance:p_intf];
assert( extMgr != NULL );
NSValue *o_value = [NSValue valueWithPointer:p_dialog];
[self performSelectorOnMainThread:@selector(updateExtensionDialog:)
withObject:o_value
waitUntilDone:YES];
}
at end
/*****************************************************************************
* extensions_manager.h: Extensions manager for Cocoa
****************************************************************************
* Copyright (C) 2012 VideoLAN and authors
* $Id$
*
* Authors: Brendon Justin <brendonjustin at gmail.com>
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#import "ExtensionsDialogProvider.h"
#import "intf.h"
#import <vlc_extensions.h>
#import <Cocoa/Cocoa.h>
at class ExtensionsDialogProvider;
at protocol ExtensionsDelegate <NSObject>
- (void)extensionsUpdated;
at end
at interface ExtensionsManager : NSObject
{
intf_thread_t *p_intf;
extensions_manager_t *p_extensions_manager;
ExtensionsDialogProvider *p_edp;
NSMutableDictionary *p_extDict;
BOOL b_unloading; ///< Work around threads + emit issues, see isUnloading
BOOL b_failed; ///< Flag set to true if we could not load the module
id <ExtensionsDelegate> delegate;
};
+ (ExtensionsManager *)getInstance:(intf_thread_t *)_p_intf;
- (id)initWithIntf:(intf_thread_t *)_p_intf;
- (void)buildMenu:(NSMenu *)extMenu;
- (extensions_manager_t *)getManager;
- (BOOL)loadExtensions;
- (void)unloadExtensions;
- (void)reloadExtensions;
- (void)triggerMenu:(id)sender;
- (void)inputChanged:(input_thread_t *)p_input;
- (void)playingChanged:(int)state;
- (void)metaChanged:(input_item_t *)p_input;
- (BOOL)isLoaded;
- (BOOL)cannotLoad;
at property (readonly) BOOL isUnloading;
at end
/*****************************************************************************
* extensions_manager.cpp: Extensions manager for Cocoa
****************************************************************************
* Copyright (C) 2009-2012 VideoLAN and authors
* $Id$
*
* Authors: Brendon Justin <brendonjustin at gmail.com>,
* Jean-Philippe André < jpeg # videolan.org >
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#import "ExtensionsManager.h"
#import "ExtensionsDialogProvider.h"
#import <vlc_modules.h>
#import "assert.h"
#define MENU_MAP(a,e) ((uint32_t)( (((uint16_t)a) << 16) | ((uint16_t)e) ))
#define MENU_GET_ACTION(a) ( (uint16_t)( ((uint32_t)a) >> 16 ) )
#define MENU_GET_EXTENSION(a) ( (uint16_t)( ((uint32_t)a) & 0xFFFF ) )
at implementation ExtensionsManager
static ExtensionsManager* instance = nil;
at synthesize isUnloading = b_unloading;
+ (ExtensionsManager *)getInstance:( intf_thread_t *)_p_intf
{
if( !instance )
instance = [[[ExtensionsManager alloc] initWithIntf:_p_intf] autorelease];
return instance;
}
- (id)initWithIntf:(intf_thread_t *)_p_intf
{
if ((self = [super init]))
{
p_intf = _p_intf;
p_extensions_manager = NULL;
p_edp = NULL;
p_extDict = [[NSMutableDictionary alloc] init];
b_unloading = false;
b_failed = false;
delegate = nil;
}
return self;
}
/** Get the extensions_manager_t if it is loaded and hold the object */
- (extensions_manager_t *)getManager
{
if( !p_extensions_manager )
return NULL;
vlc_object_hold( p_extensions_manager );
return p_extensions_manager;
}
- (void)buildMenu:(NSMenu *)extMenu
{
assert( extMenu != nil );
if( ![self isLoaded] )
{
// This case can happen: do nothing
return;
}
vlc_mutex_lock( &p_extensions_manager->lock );
extension_t *p_ext = NULL;
int i_ext = 0;
FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
{
bool b_Active = extension_IsActivated( p_extensions_manager, p_ext );
NSString *titleString = [NSString stringWithCString:p_ext->psz_title
encoding:NSASCIIStringEncoding];
if( b_Active && extension_HasMenu( p_extensions_manager, p_ext ) )
{
NSMenu *submenu = [[NSMenu alloc] initWithTitle:titleString];
NSMenuItem *submenuItem = [extMenu addItemWithTitle:titleString
action:nil
keyEquivalent:@""];
[extMenu setSubmenu:submenu forItem:submenuItem];
[submenu release];
char **ppsz_titles = NULL;
uint16_t *pi_ids = NULL;
size_t i_num = 0;
if( extension_GetMenu( p_extensions_manager, p_ext,
&ppsz_titles, &pi_ids ) == VLC_SUCCESS )
{
for( int i = 0; ppsz_titles[i] != NULL; ++i )
{
++i_num;
titleString = [NSString stringWithCString:ppsz_titles[i]
encoding:NSASCIIStringEncoding];
NSMenuItem *menuItem = [submenu addItemWithTitle:titleString
action:@selector(triggerMenu:)
keyEquivalent:@""];
[menuItem setTarget:self];
menuItem.tag = MENU_MAP(pi_ids[i], i_ext);
free( ppsz_titles[i] );
}
if( !i_num )
{
NSMenuItem *menuItem = [submenu addItemWithTitle:@"Empty"
action:@selector(triggerMenu:)
keyEquivalent:@""];
[menuItem setEnabled:NO];
}
free( ppsz_titles );
free( pi_ids );
}
else
{
msg_Warn( p_intf, "Could not get menu for extension '%s'",
p_ext->psz_title );
NSMenuItem *menuItem = [submenu addItemWithTitle:@"Empty"
action:@selector(triggerMenu:)
keyEquivalent:@""];
[menuItem setEnabled:NO];
}
[submenu addItem:[NSMenuItem separatorItem]];
NSMenuItem *deactivateItem = [submenu addItemWithTitle:@"Deactivate"
action:@selector(triggerMenu:)
keyEquivalent:@""];
[deactivateItem setTarget:self];
deactivateItem.tag = MENU_MAP(0, i_ext);
}
else
{
NSMenuItem *menuItem = [extMenu addItemWithTitle:titleString
action:@selector(triggerMenu:)
keyEquivalent:@""];
[menuItem setTarget:self];
if( !extension_TriggerOnly( p_extensions_manager, p_ext ) )
{
if ( b_Active )
[menuItem setState:NSOnState];
}
menuItem.tag = MENU_MAP(0, i_ext);
}
i_ext++;
}
FOREACH_END()
vlc_mutex_unlock( &p_extensions_manager->lock );
}
- (BOOL)loadExtensions
{
if( !p_extensions_manager )
{
p_extensions_manager = ( extensions_manager_t* )
vlc_object_create( p_intf, sizeof( extensions_manager_t ) );
if( !p_extensions_manager )
{
b_failed = true;
[delegate extensionsUpdated];
return false;
}
p_extensions_manager->p_module =
module_need( p_extensions_manager, "extension", NULL, false );
if( !p_extensions_manager->p_module )
{
msg_Err( p_intf, "Unable to load extensions module" );
vlc_object_release( p_extensions_manager );
p_extensions_manager = NULL;
b_failed = true;
[delegate extensionsUpdated];
return false;
}
/* Initialize dialog provider */
p_edp = [ExtensionsDialogProvider sharedInstance:p_intf];
[p_edp retain];
if( !p_edp )
{
msg_Err( p_intf, "Unable to create dialogs provider for extensions" );
module_unneed( p_extensions_manager,
p_extensions_manager->p_module );
vlc_object_release( p_extensions_manager );
p_extensions_manager = NULL;
b_failed = true;
[delegate extensionsUpdated];
return false;
}
b_unloading = false;
}
b_failed = false;
[delegate extensionsUpdated];
return true;
}
- (void)unloadExtensions
{
if( !p_extensions_manager )
return;
b_unloading = true;
[p_edp release];
module_unneed( p_extensions_manager, p_extensions_manager->p_module );
vlc_object_release( p_extensions_manager );
p_extensions_manager = NULL;
}
- (void)reloadExtensions
{
[self unloadExtensions];
[self loadExtensions];
if (delegate)
[delegate extensionsUpdated];
}
- (void)triggerMenu:(id)sender
{
uint32_t identifier = [(NSMenuItem *)sender tag];
uint16_t i_ext = MENU_GET_EXTENSION( identifier );
uint16_t i_action = MENU_GET_ACTION( identifier );
vlc_mutex_lock( &p_extensions_manager->lock );
if( (int) i_ext > p_extensions_manager->extensions.i_size )
{
msg_Dbg( p_intf, "can't trigger extension with wrong id %d",
(int) i_ext );
return;
}
extension_t *p_ext = ARRAY_VAL( p_extensions_manager->extensions, i_ext );
assert( p_ext != NULL);
vlc_mutex_unlock( &p_extensions_manager->lock );
if( i_action == 0 )
{
msg_Dbg( p_intf, "activating or triggering extension '%s', id %d",
p_ext->psz_title, i_ext );
if( extension_TriggerOnly( p_extensions_manager, p_ext ) )
{
extension_Trigger( p_extensions_manager, p_ext );
}
else
{
if( !extension_IsActivated( p_extensions_manager, p_ext ) )
extension_Activate( p_extensions_manager, p_ext );
else
extension_Deactivate( p_extensions_manager, p_ext );
}
}
else
{
msg_Dbg( p_intf, "triggering extension '%s', on menu with id = 0x%x",
p_ext->psz_title, i_action );
extension_TriggerMenu( p_extensions_manager, p_ext, i_action );
}
}
- (void)inputChanged:(input_thread_t *)p_input
{
//This is unlikely, but can happen if no extension modules can be loaded.
if ( p_extensions_manager == NULL )
return ;
vlc_mutex_lock( &p_extensions_manager->lock );
extension_t *p_ext;
FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
{
if( extension_IsActivated( p_extensions_manager, p_ext ) )
{
extension_SetInput( p_extensions_manager, p_ext, p_input );
}
}
FOREACH_END()
vlc_mutex_unlock( &p_extensions_manager->lock );
}
- (void)playingChanged:(int)state
{
//This is unlikely, but can happen if no extension modules can be loaded.
if ( p_extensions_manager == NULL )
return ;
vlc_mutex_lock( &p_extensions_manager->lock );
extension_t *p_ext;
FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
{
if( extension_IsActivated( p_extensions_manager, p_ext ) )
{
extension_PlayingChanged( p_extensions_manager, p_ext, state );
}
}
FOREACH_END()
vlc_mutex_unlock( &p_extensions_manager->lock );
}
- (void)metaChanged:(input_item_t *)p_input
{
//This is unlikely, but can happen if no extension modules can be loaded.
if ( p_extensions_manager == NULL )
return ;
vlc_mutex_lock( &p_extensions_manager->lock );
extension_t *p_ext;
FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
{
if( extension_IsActivated( p_extensions_manager, p_ext ) )
{
extension_MetaChanged( p_extensions_manager, p_ext );
}
}
FOREACH_END()
vlc_mutex_unlock( &p_extensions_manager->lock );
}
- (void)dealloc
{
msg_Dbg( p_intf, "Killing extension dialog provider" );
[ExtensionsDialogProvider killInstance];
vlc_object_release( p_extensions_manager );
[p_extDict release];
[super dealloc];
}
- (BOOL)isLoaded
{
return p_extensions_manager != NULL;
}
- (BOOL)cannotLoad
{
return b_unloading || b_failed;
}
at end
...@@ -17,6 +17,12 @@ SOURCES_macosx = \ ...@@ -17,6 +17,12 @@ SOURCES_macosx = \
MainWindow.m \ MainWindow.m \
CoreInteraction.h \ CoreInteraction.h \
CoreInteraction.m \ CoreInteraction.m \
ExtensionsManager.h \
ExtensionsManager.m \
ExtensionsDialogProvider.h \
ExtensionsDialogProvider.m \
VLCUIWidgets.h \
VLCUIWidgets.m \
about.h \ about.h \
about.m \ about.m \
applescript.h \ applescript.h \
......
/*****************************************************************************
* VLCUIWidgets.h: Widgets for VLC's Minimal Dialog Provider for Mac OS X
*****************************************************************************
* Copyright (C) 2009-2012 the VideoLAN team
* $Id$
*
* Authors: Pierre d'Herbemont <pdherbemont # videolan dot>
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#import <Cocoa/Cocoa.h>
#import <vlc_extensions.h>
at class VLCDialogGridView;
at interface VLCDialogButton : NSButton
at property (readwrite) extension_widget_t *widget;
at end
at interface VLCDialogPopUpButton : NSPopUpButton
at property (readwrite) extension_widget_t *widget;
at end
at interface VLCDialogTextField : NSTextField
at property (readwrite) extension_widget_t *widget;
at end
at interface VLCDialogWindow : NSWindow
at property (readwrite) extension_dialog_t *dialog;
at property (readwrite) BOOL has_lock;
at end
at interface VLCDialogList : NSTableView <NSTableViewDataSource>
at property (readwrite) extension_widget_t *widget;
at property (readwrite, retain) NSMutableArray *contentArray;
at end
at interface VLCDialogGridView : NSView {
NSUInteger _rowCount, _colCount;
NSMutableArray *_griddedViews;
}
- (void)addSubview:(NSView *)view atRow:(NSUInteger)row column:(NSUInteger)column rowSpan:(NSUInteger)rowSpan colSpan:(NSUInteger)colSpan;
- (NSSize)flexSize:(NSSize)size;
- (void)removeSubview:(NSView *)view;
at property (readonly) NSUInteger numViews;
at end
\ No newline at end of file
/*****************************************************************************
* VLCUIWidgets.m: Widgets for VLC's Minimal Dialog Provider for Mac OS X
*****************************************************************************
* Copyright (C) 2009-2010 the VideoLAN team
* $Id$
*
* Authors: Pierre d'Herbemont <pdherbemont # videolan dot>
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#import "VLCUIWidgets.h"
#import <stdlib.h>
at implementation VLCDialogButton
at synthesize widget;
at end
at implementation VLCDialogPopUpButton
at synthesize widget;
at end
at implementation VLCDialogTextField
at synthesize widget;
at end
at implementation VLCDialogWindow
at synthesize dialog;
at synthesize has_lock;
at end
at implementation VLCDialogList
at synthesize widget;
at synthesize contentArray;
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [contentArray count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return [[contentArray objectAtIndex:rowIndex] objectForKey:@"text"];
}
at end
at implementation VLCDialogGridView
- (NSUInteger)numViews
{
return [_griddedViews count];
}
- (id)init
{
if ((self = [super init]))
{
_colCount = 0;
_rowCount = 0;
_griddedViews = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[_griddedViews release];
[super dealloc];
}
- (void)recomputeCount
{
_colCount = 0;
_rowCount = 0;
for (NSDictionary *obj in _griddedViews)
{
NSUInteger row = [[obj objectForKey:@"row"] intValue];
NSUInteger col = [[obj objectForKey:@"col"] intValue];
if (col + 1 > _colCount)
_colCount = col + 1;
if (row + 1 > _rowCount)
_rowCount = row + 1;
}
}
- (void)recomputeWindowSize
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
NSWindow *window = [self window];
NSRect frame = [window frame];
NSRect contentRect = [window contentRectForFrameRect:frame];
contentRect.size = [self flexSize:frame.size];
NSRect newFrame = [window frameRectForContentRect:contentRect];
newFrame.origin.y -= newFrame.size.height - frame.size.height;
newFrame.origin.x -= (newFrame.size.width - frame.size.width) / 2;
[window setFrame:newFrame display:YES animate:YES];
}
- (NSSize)objectSizeToFit:(NSView *)view
{
if ([view isKindOfClass:[NSControl class]]) {
NSControl *control = (NSControl *)view;
return [[control cell] cellSize];
}
return [view frame].size;
}
- (CGFloat)marginX
{
return 16;
}
- (CGFloat)marginY
{
return 8;
}
- (CGFloat)constrainedHeightOfRow:(NSUInteger)targetRow
{
CGFloat height = 0;
for(NSDictionary *obj in _griddedViews) {
NSUInteger row = [[obj objectForKey:@"row"] intValue];
if (row != targetRow)
continue;
NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
if (rowSpan != 1)
continue;
NSView *view = [obj objectForKey:@"view"];
if ([view autoresizingMask] & NSViewHeightSizable)
continue;
NSSize sizeToFit = [self objectSizeToFit:view];
if (height < sizeToFit.height)
height = sizeToFit.height;
}
return height;
}
- (CGFloat)remainingRowsHeight
{
NSUInteger height = [self marginY];
if (!_rowCount)
return 0;
NSUInteger autosizedRows = 0;
for (NSUInteger i = 0; i < _rowCount; i++) {
CGFloat constrainedHeight = [self constrainedHeightOfRow:i];
if (!constrainedHeight)
autosizedRows++;
height += constrainedHeight + [self marginY];
}
CGFloat remaining = 0;
if (height < self.bounds.size.height && autosizedRows)
remaining = (self.bounds.size.height - height) / autosizedRows;
if (remaining < 0)
remaining = 0;
return remaining;
}
- (CGFloat)heightOfRow:(NSUInteger)targetRow
{
NSAssert(targetRow < _rowCount, @"accessing a non existing row");
CGFloat height = [self constrainedHeightOfRow:targetRow];
if (!height)
height = [self remainingRowsHeight];
return height;
}
- (CGFloat)topOfRow:(NSUInteger)targetRow
{
CGFloat top = [self marginY];
for (NSUInteger i = 1; i < _rowCount - targetRow; i++)
{
top += [self heightOfRow:_rowCount - i] + [self marginY];
}
return top;
}
- (CGFloat)constrainedWidthOfColumn:(NSUInteger)targetColumn
{
CGFloat width = 0;
for(NSDictionary *obj in _griddedViews) {
NSUInteger col = [[obj objectForKey:@"col"] intValue];
if (col != targetColumn)
continue;
NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
if (colSpan != 1)
continue;
NSView *view = [obj objectForKey:@"view"];
if ([view autoresizingMask] & NSViewWidthSizable)
return 0;
NSSize sizeToFit = [self objectSizeToFit:view];
if (width < sizeToFit.width)
width = sizeToFit.width;
}
return width;
}
- (CGFloat)remainingColumnWidth
{
NSUInteger width = [self marginX];
if (!_colCount)
return 0;
NSUInteger autosizedCol = 0;
for (NSUInteger i = 0; i < _colCount; i++) {
CGFloat constrainedWidth = [self constrainedWidthOfColumn:i];
if (!constrainedWidth)
autosizedCol++;
width += constrainedWidth + [self marginX];
}
CGFloat remaining = 0;
if (width < self.bounds.size.width && autosizedCol)
remaining = (self.bounds.size.width - width) / autosizedCol;
if (remaining < 0)
remaining = 0;
return remaining;
}
- (CGFloat)widthOfColumn:(NSUInteger)targetColumn
{
CGFloat width = [self constrainedWidthOfColumn:targetColumn];
if (!width)
width = [self remainingColumnWidth];
return width;
}
- (CGFloat)leftOfColumn:(NSUInteger)targetColumn
{
CGFloat left = [self marginX];
for (NSUInteger i = 0; i < targetColumn; i++)
{
left += [self widthOfColumn:i] + [self marginX];
}
return left;
}
- (void)relayout
{
for(NSDictionary *obj in _griddedViews) {
NSUInteger row = [[obj objectForKey:@"row"] intValue];
NSUInteger col = [[obj objectForKey:@"col"] intValue];
NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
NSView *view = [obj objectForKey:@"view"];
NSRect rect;
// Get the height
if ([view autoresizingMask] & NSViewHeightSizable || rowSpan > 1) {
CGFloat height = 0;
for (NSUInteger r = 0; r < rowSpan; r++) {
if (row + r >= _rowCount)
break;
height += [self heightOfRow:row + r] + [self marginY];
}
rect.size.height = height - [self marginY];
}
else
rect.size.height = [self objectSizeToFit:view].height;
// Get the width
if ([view autoresizingMask] & NSViewWidthSizable) {
CGFloat width = 0;
for (NSUInteger c = 0; c < colSpan; c++)
width += [self widthOfColumn:col + c] + [self marginX];
rect.size.width = width - [self marginX];
}
else
rect.size.width = [self objectSizeToFit:view].width;
// Top corner
rect.origin.y = [self topOfRow:row] + ([self heightOfRow:row] - rect.size.height) / 2;
rect.origin.x = [self leftOfColumn:col];
[view setFrame:rect];
[view setNeedsDisplay:YES];
}
}
- (NSMutableDictionary *)objectForView:(NSView *)view
{
for (NSMutableDictionary *dict in _griddedViews)
{
if ([dict objectForKey:@"view"] == view)
return dict;
}
return nil;
}
- (void)addSubview:(NSView *)view atRow:(NSUInteger)row column:(NSUInteger)column
rowSpan:(NSUInteger)rowSpan
colSpan:(NSUInteger)colSpan
{
if (row + 1 > _rowCount)
_rowCount = row + 1;
if (column + 1 > _colCount)
_colCount = column + 1;
NSMutableDictionary *dict = [self objectForView:view];
if (!dict) {
dict = [NSMutableDictionary dictionary];
[dict setObject:view forKey:@"view"];
[_griddedViews addObject:dict];
}
[dict setObject:[NSNumber numberWithInt:rowSpan] forKey:@"rowSpan"];
[dict setObject:[NSNumber numberWithInt:colSpan] forKey:@"colSpan"];
[dict setObject:[NSNumber numberWithInt:row] forKey:@"row"];
[dict setObject:[NSNumber numberWithInt:column] forKey:@"col"];
[self addSubview:view];
[self relayout];
// Recompute the size of the window after making sure we won't see anymore update
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
[self performSelector:@selector(recomputeWindowSize) withObject:nil afterDelay:0.1];
}
- (void)removeSubview:(NSView *)view
{
NSDictionary *dict = [self objectForView:view];
if (dict)
[_griddedViews removeObject:dict];
[view removeFromSuperview];
[self recomputeCount];
[self recomputeWindowSize];
[self relayout];
[self setNeedsDisplay:YES];
}
- (void)setFrame:(NSRect)frameRect
{
[super setFrame:frameRect];
[self relayout];
}
- (NSSize)flexSize:(NSSize)size
{
if (!_rowCount || !_colCount)
return size;
CGFloat minHeight = [self marginY];
BOOL canFlexHeight = NO;
for (NSUInteger i = 0; i < _rowCount; i++) {
CGFloat constrained = [self constrainedHeightOfRow:i];
if (!constrained) {
canFlexHeight = YES;
constrained = 128;
}
minHeight += constrained + [self marginY];
}
CGFloat minWidth = [self marginX];
BOOL canFlexWidth = NO;
for (NSUInteger i = 0; i < _colCount; i++) {
CGFloat constrained = [self constrainedWidthOfColumn:i];
if (!constrained) {
canFlexWidth = YES;
constrained = 128;
}
minWidth += constrained + [self marginX];
}
if (size.width < minWidth)
size.width = minWidth;
if (size.height < minHeight)
size.height = minHeight;
if (!canFlexHeight)
size.height = minHeight;
if (!canFlexWidth)
size.width = minWidth;
return size;
}
at end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment