aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2023-09-09 20:35:01 +0200
committerTeddy Wing2023-09-09 20:35:01 +0200
commit70a98332cba2a71fe6ffb109e40877bf48e89386 (patch)
tree3a7c68ed82409d2175b0604550c4d20214cccdc6 /src
parent059f8a488eae81aa5a023e7fb0df6a437684106b (diff)
downloadBase-Windowed-Application-70a98332cba2a71fe6ffb109e40877bf48e89386.tar.bz2
AppDelegate: Add NSTextFinder find bar
Add a find bar leveraging NSTextFinder. It turns out I didn't need to do anything custom with `NSTextFinder` since `NSTextView` conveniently already has a `usesFindBar` property to turn on built-in support for it. I discovered that by looking at the TextEdit source code available here: https://developer.apple.com/library/archive/samplecode/TextEdit/Introduction/Intro.html I added an `NSScrollView` because you kind of need one to implement `NSTextFinder`. Not sure if the finding functionality would still work without the scroll view. Also use the proper `-performTextFinderAction:` selector for the "Find…" menu item to open the find bar. Now we can confirm that the "Find" menu items work correctly.
Diffstat (limited to 'src')
-rw-r--r--src/AppDelegate.h2
-rw-r--r--src/AppDelegate.m22
-rw-r--r--src/MainMenu.m4
3 files changed, 25 insertions, 3 deletions
diff --git a/src/AppDelegate.h b/src/AppDelegate.h
index a9570b8..4dba427 100644
--- a/src/AppDelegate.h
+++ b/src/AppDelegate.h
@@ -2,6 +2,8 @@
@interface AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *_window;
+ NSScrollView *_scroll_view;
NSTextView *_text_view;
+ // NSTextFinder *_text_finder;
}
@end
diff --git a/src/AppDelegate.m b/src/AppDelegate.m
index d6cef3e..3df04e0 100644
--- a/src/AppDelegate.m
+++ b/src/AppDelegate.m
@@ -6,7 +6,9 @@
- (void)dealloc
{
[_window release];
+ [_scroll_view release];
[_text_view release];
+ // [_text_finder release];
[super dealloc];
}
@@ -34,8 +36,20 @@
_text_view = [[NSTextView alloc]
initWithFrame:NSMakeRect(0, 0, 400, 400)];
[_text_view setAllowsUndo:YES];
- [[_window contentView] addSubview:_text_view];
+ [_text_view setUsesFindBar:YES];
+
+ _scroll_view = [[NSScrollView alloc]
+ initWithFrame:NSMakeRect(0, 0, 400, 400)];
+ [_scroll_view setAutohidesScrollers:YES];
+ [_scroll_view setHasVerticalScroller:YES];
+ [_scroll_view setDocumentView:_text_view];
+
+ [[_window contentView] addSubview:_scroll_view];
// [_window setContentView:_text_view];
+
+ // _text_finder = [[NSTextFinder alloc] init];
+ // [_text_finder setClient:(id<NSTextFinderClient>)_text_view];
+ // [_text_finder setFindBarContainer:_scroll_view];
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification
@@ -44,4 +58,10 @@
[_window makeKeyAndOrderFront:nil];
}
+// - (IBAction)performTextFinderAction:(id)sender
+// {
+// NSLog(@"performTextFinderAction: performing %ld", [sender tag]);
+// [_text_finder performAction:[sender tag]];
+// }
+
@end
diff --git a/src/MainMenu.m b/src/MainMenu.m
index a961f65..afc62b6 100644
--- a/src/MainMenu.m
+++ b/src/MainMenu.m
@@ -358,8 +358,8 @@ NSMenuItem *MainMenuCreateEditMenuItem()
@"Find…",
@"Find… menu item."
)
- action:@selector(performFindPanelAction:)
- // action:@selector(performTextFinderAction:)
+ // action:@selector(performFindPanelAction:)
+ action:@selector(performTextFinderAction:)
keyEquivalent:@"f"];
[find_ellipsis_menu_item setTag:NSTextFinderActionShowFindInterface];
// [find_ellipsis_menu_item setTag:1];