From ccee813f4a6d6d91299f5267483f91219bdc408f Mon Sep 17 00:00:00 2001 From: anthony Date: Thu, 10 Feb 2022 08:54:37 +0000 Subject: [PATCH] Implement dropdown form selection --- frontends/gnustep/BrowserWindowController.h | 4 ++++ frontends/gnustep/BrowserWindowController.m | 5 +++++ frontends/gnustep/PlotView.h | 1 + frontends/gnustep/PlotView.m | 20 ++++++++++++++++++++ frontends/gnustep/tables/window.m | 20 +++++++++++++++++++- 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/frontends/gnustep/BrowserWindowController.h b/frontends/gnustep/BrowserWindowController.h index 8d5dfc0bc..b92fab69a 100644 --- a/frontends/gnustep/BrowserWindowController.h +++ b/frontends/gnustep/BrowserWindowController.h @@ -9,6 +9,8 @@ #import "UrlSuggestionView.h" struct browser_window; +struct form_control; + @interface BrowserWindowController : NSWindowController { id backButton; id forwardButton; @@ -69,6 +71,8 @@ struct browser_window; -(void)showAll: (NSString*)needle matchCase: (BOOL)matchCase sender: (id)sender; -(void)bookmarkPage: (id)sender; +-(void)showDropdownMenuWithOptions: (NSArray*)options atLocation: (NSPoint)location inTab: (id)tab control: (struct form_control*)control; + +(id)newTabTarget; @end diff --git a/frontends/gnustep/BrowserWindowController.m b/frontends/gnustep/BrowserWindowController.m index 45ff31ed9..cabf08fa3 100644 --- a/frontends/gnustep/BrowserWindowController.m +++ b/frontends/gnustep/BrowserWindowController.m @@ -574,6 +574,11 @@ static id newTabTarget; } } +-(void)showDropdownMenuWithOptions: (NSArray*)options atLocation: (NSPoint)location inTab: (id)tab control: (struct form_control*)control { + [[tab plotView] showDropdownMenuWithOptions: options atLocation: location + control: control]; +} + +(id)newTabTarget { return newTabTarget; } diff --git a/frontends/gnustep/PlotView.h b/frontends/gnustep/PlotView.h index e1411f5ae..9a1b95d13 100644 --- a/frontends/gnustep/PlotView.h +++ b/frontends/gnustep/PlotView.h @@ -16,4 +16,5 @@ -(void)removeCaret; -(void)reload: (id)sender; -(void)stopReloading: (id)sender; +-(void)showDropdownMenuWithOptions: (NSArray*)options atLocation: (NSPoint)location control: (struct form_control*)control; @end diff --git a/frontends/gnustep/PlotView.m b/frontends/gnustep/PlotView.m index da0f42720..0c3473c42 100644 --- a/frontends/gnustep/PlotView.m +++ b/frontends/gnustep/PlotView.m @@ -31,6 +31,7 @@ #import "utils/nsoption.h" #import "utils/messages.h" #import "netsurf/content_type.h" +#import "netsurf/form.h" #define colour_red_component( c ) (((c) >> 0) & 0xFF) #define colour_green_component( c ) (((c) >> 8) & 0xFF) @@ -796,5 +797,24 @@ static browser_mouse_state cocoa_mouse_flags_for_event( NSEvent *evt ) { [self openDump: [NSString stringWithCString: fname]]; } +-(void)showDropdownMenuWithOptions: (NSArray*)options atLocation: (NSPoint)location control: (struct form_control*)control { + NSMenu *popupMenu = [[NSMenu alloc] initWithTitle: @""]; + NSMenuItem *opt; + for (NSInteger i = 0; i < [options count]; i++) { + opt = [popupMenu addItemWithTitle: [options objectAtIndex: i] action: + @selector(didPickDropdownOption:) keyEquivalent: @""]; + [opt setTag: i]; + [opt setRepresentedObject: [NSValue valueWithPointer: control]]; + } + [NSMenu popUpContextMenu: popupMenu withEvent: nil forView: self]; + [popupMenu release]; +} + +-(void)didPickDropdownOption: (id)sender { + NSValue *controlPointer = [sender representedObject]; + struct form_control *control = (struct form_control*)[controlPointer pointerValue]; + if (form_select_process_selection(control, [sender tag]) != NSERROR_OK) + NSLog(@"Failed to process selection"); +} @end diff --git a/frontends/gnustep/tables/window.m b/frontends/gnustep/tables/window.m index 6bb03bd3d..c95b949c8 100644 --- a/frontends/gnustep/tables/window.m +++ b/frontends/gnustep/tables/window.m @@ -8,6 +8,7 @@ #import "netsurf/types.h" #import "utils/nsurl.h" #import "netsurf/mouse.h" +#import "netsurf/form.h" struct window_tab { BrowserWindowController *window; @@ -146,6 +147,22 @@ static void gnustep_window_place_caret(struct gui_window *gw, int x, int y, int [wtab->window placeCaretAtX: x y: y height: height inTab: wtab->tab]; } +static void gnustep_window_create_form_select_menu(struct gui_window *gw, struct form_control *control) { + struct form_option *opt; + struct rect rect; + struct window_tab *wtab = (struct window_tab*)gw; + if (form_control_bounding_rect(control, &rect) != NSERROR_OK) { + NSLog("Failed to get control bounding rect, skipping"); + return; + } + NSMutableArray *options = [NSMutableArray array]; + for(opt = form_select_get_option(control, 0); opt != NULL; opt = opt->next) { + [options addObject: [NSString stringWithCString: opt->text]]; + } + [wtab->window showDropdownMenuWithOptions: options atLocation: + NSMakePoint(rect.x0, rect.y1) inTab: wtab->tab control: control]; +} + struct gui_window_table gnustep_window_table = { .create = gnustep_window_create, .destroy = gnustep_window_destroy, @@ -157,5 +174,6 @@ struct gui_window_table gnustep_window_table = { .set_title = gnustep_window_set_title, .set_url = gnustep_window_set_url, .place_caret = gnustep_window_place_caret, - .set_pointer = gnustep_window_set_pointer + .set_pointer = gnustep_window_set_pointer, + .create_form_select_menu = gnustep_window_create_form_select_menu, };