Creating select menus for forms

svn path=/trunk/netsurf/; revision=11484
This commit is contained in:
Sven Weidauer 2011-01-25 15:09:06 +00:00
parent 5615eb65af
commit cca607fbe8
5 changed files with 143 additions and 2 deletions

33
cocoa/FormSelectMenu.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf 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; version 2 of the License.
*
* NetSurf 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, see <http://www.gnu.org/licenses/>.
*/
#import <Cocoa/Cocoa.h>
@interface FormSelectMenu : NSObject {
NSMenu *menu;
NSPopUpButtonCell *cell;
struct browser_window *browser;
struct form_control *control;
}
- initWithControl: (struct form_control *) control forWindow: (struct browser_window *) window;
- (void) runInView: (NSView *) view;
@end

98
cocoa/FormSelectMenu.m Normal file
View File

@ -0,0 +1,98 @@
/*
* Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf 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; version 2 of the License.
*
* NetSurf 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, see <http://www.gnu.org/licenses/>.
*/
#import "FormSelectMenu.h"
#import "render/form.h"
#import "render/box.h"
@interface FormSelectMenu ()
- (void) itemSelected: (id) sender;
@end
@implementation FormSelectMenu
- initWithControl: (struct form_control *) c forWindow: (struct browser_window *) w;
{
if ((self = [super init]) == nil) return nil;
control = c;
browser = w;
menu = [[NSMenu alloc] initWithTitle: @"Select"];
if (menu == nil) {
[self release];
return nil;
}
[menu addItemWithTitle: @"" action: NULL keyEquivalent: @""];
NSInteger currentItemIndex = 0;
for (struct form_option *opt = control->data.select.items; opt != NULL; opt = opt->next) {
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle: [NSString stringWithUTF8String: opt->text]
action: @selector( itemSelected: )
keyEquivalent: @""];
if (opt->selected) [item setState: NSOnState];
[item setTarget: self];
[item setTag: currentItemIndex++];
[menu addItem: item];
[item release];
}
[menu setDelegate: self];
return self;
}
- (void) dealloc;
{
[cell release];
[menu release];
[super dealloc];
}
- (void) runInView: (NSView *) view;
{
[self retain];
cell = [[NSPopUpButtonCell alloc] initTextCell: @"" pullsDown: YES];
[cell setMenu: menu];
struct rect r;
box_bounds( control->box, &r );
[cell performClickWithFrame: NSMakeRect( r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0 )
inView: view];
}
- (void) itemSelected: (id) sender;
{
form_select_process_selection( browser->current_content, control, [sender tag] );
}
- (void) menuDidClose: (NSMenu *) sender;
{
[self release];
}
@end

View File

@ -74,6 +74,7 @@ S_COCOA := \
URLFieldCell.m \
TreeView.m \
HistoryView.m \
FormSelectMenu.m \
bitmap.m \
fetch.m \
font.m \

View File

@ -142,6 +142,7 @@
26CDD0F612E726E0004FC66B /* BrowserViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 26CDD0F512E726E0004FC66B /* BrowserViewController.m */; };
26EC3B6A12ED62C0000A960C /* URLFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 26EC3B6912ED62C0000A960C /* URLFieldCell.m */; };
26EC3C4412ED8202000A960C /* HistoryView.m in Sources */ = {isa = PBXBuildFile; fileRef = 26EC3C4312ED8202000A960C /* HistoryView.m */; };
26EC3F1812EF0CBD000A960C /* FormSelectMenu.m in Sources */ = {isa = PBXBuildFile; fileRef = 26EC3F1712EF0CBD000A960C /* FormSelectMenu.m */; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
/* End PBXBuildFile section */
@ -407,6 +408,8 @@
26EC3B6912ED62C0000A960C /* URLFieldCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = URLFieldCell.m; sourceTree = "<group>"; };
26EC3C4212ED8202000A960C /* HistoryView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HistoryView.h; sourceTree = "<group>"; };
26EC3C4312ED8202000A960C /* HistoryView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HistoryView.m; sourceTree = "<group>"; };
26EC3F1612EF0CBD000A960C /* FormSelectMenu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FormSelectMenu.h; sourceTree = "<group>"; };
26EC3F1712EF0CBD000A960C /* FormSelectMenu.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FormSelectMenu.m; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* NetSurf.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NetSurf.app; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
@ -805,6 +808,8 @@
26EC3B6912ED62C0000A960C /* URLFieldCell.m */,
26EC3C4212ED8202000A960C /* HistoryView.h */,
26EC3C4312ED8202000A960C /* HistoryView.m */,
26EC3F1612EF0CBD000A960C /* FormSelectMenu.h */,
26EC3F1712EF0CBD000A960C /* FormSelectMenu.m */,
);
name = Views;
sourceTree = "<group>";
@ -1074,6 +1079,7 @@
26CDD0F612E726E0004FC66B /* BrowserViewController.m in Sources */,
26EC3B6A12ED62C0000A960C /* URLFieldCell.m in Sources */,
26EC3C4412ED8202000A960C /* HistoryView.m in Sources */,
26EC3F1812EF0CBD000A960C /* FormSelectMenu.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -21,6 +21,7 @@
#import "BrowserView.h"
#import "BrowserViewController.h"
#import "BrowserWindowController.h"
#import "FormSelectMenu.h"
#import "desktop/gui.h"
#import "desktop/netsurf.h"
@ -199,6 +200,7 @@ void gui_window_set_pointer(struct gui_window *g, gui_pointer_shape shape)
break;
case GUI_POINTER_POINT:
case GUI_POINTER_MENU:
[[NSCursor pointingHandCursor] set];
break;
@ -298,10 +300,11 @@ void gui_drag_save_selection(struct selection *s, struct gui_window *g)
void gui_create_form_select_menu(struct browser_window *bw,
struct form_control *control)
{
UNIMPL();
FormSelectMenu *menu = [[FormSelectMenu alloc] initWithControl: control forWindow: bw];
[menu runInView: [(BrowserViewController *)bw->window browserView]];
[menu release];
}
void gui_launch_url(const char *url)
{
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: [NSString stringWithUTF8String: url]]];