From fcf078c25d81ca2a9b75f6baec18ce7eb477a8f3 Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Sat, 1 Nov 2008 17:44:57 -0400 Subject: Added support for some AppleEvents, use Scintilla namespace on OSX. --- core/events.lua | 5 +++ core/ext/keys.lua | 2 +- src/lua_interface.c | 10 +++++- src/textadept.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++------ src/textadept.h | 4 ++- 5 files changed, 96 insertions(+), 13 deletions(-) diff --git a/core/events.lua b/core/events.lua index 48575b8e..451e3af1 100644 --- a/core/events.lua +++ b/core/events.lua @@ -521,6 +521,11 @@ add_handler('quit', return true end) +if MAC then + function appleevent_odoc(uri) return handle('appleevent_odoc', uri) end + add_handler('appleevent_odoc', function(uri) textadept.io.open(uri) end) +end + --- -- Default error handler. -- Opens a new buffer (if one hasn't already been opened) for printing errors. diff --git a/core/ext/keys.lua b/core/ext/keys.lua index 7b62c151..22ec2def 100644 --- a/core/ext/keys.lua +++ b/core/ext/keys.lua @@ -130,7 +130,7 @@ local function keypress(code, shift, control, alt) local key --print(code, string.char(code)) if code < 256 then - key_seq = key_seq..string.char(code):lower() + key = string.char(code):lower() if MAC and not shift and not control and not alt then local ch = string.char(code) -- work around native GTK-OSX's handling of Alt key diff --git a/src/lua_interface.c b/src/lua_interface.c index 4ae435f0..beac8b40 100644 --- a/src/lua_interface.c +++ b/src/lua_interface.c @@ -16,6 +16,10 @@ l_cfunc(l, ni, "__newindex"); \ } lua_setmetatable(l, -2); } +#ifdef MAC +using namespace Scintilla; +#endif + LS *lua; bool closing = false; @@ -523,7 +527,7 @@ bool l_is_ta_table_function(const char *table, const char *function) { * argument. * @param nargs The number of arguments to pass to the Lua function to call. * @param retn Optional number of expected return values. Defaults to 0. - * @param keep_return Optoinal flag indicating whether or not to keep the return + * @param keep_return Optional flag indicating whether or not to keep the return * values at the top of the stack. If false, discards the return values. * Defaults to false. */ @@ -1247,7 +1251,11 @@ LF l_cf_buffer_find(LS *lua) { LF l_cf_buffer_text_range(LS *lua) { l_check_focused_buffer(lua, 1); +#ifndef MAC TextRange tr; +#else + Scintilla::Textrange tr; +#endif tr.chrg.cpMin = luaL_checkinteger(lua, 2); tr.chrg.cpMax = luaL_checkinteger(lua, 3); char *text = new char[tr.chrg.cpMax - tr.chrg.cpMin + 1]; diff --git a/src/textadept.c b/src/textadept.c index 071c96f0..4f5a510a 100644 --- a/src/textadept.c +++ b/src/textadept.c @@ -5,6 +5,10 @@ #define gbool gboolean #define signal(o, s, c) g_signal_connect(G_OBJECT(o), s, G_CALLBACK(c), 0) +#ifdef MAC +using namespace Scintilla; +#endif + // Textadept GtkWidget *window, *focused_editor, *command_entry, @@ -24,6 +28,11 @@ static gbool t_keypress(GtkWidget*, GdkEventKey *event, gpointer); static gbool w_focus(GtkWidget*, GdkEventFocus *, gpointer); static gbool w_keypress(GtkWidget*, GdkEventKey *event, gpointer); static gbool w_exit(GtkWidget*, GdkEventAny*, gpointer); +#ifdef MAC +static OSErr w_ae_open(const AppleEvent *event, AppleEvent *, long); +static OSErr w_ae_quit(const AppleEvent *event, AppleEvent *, long); +void cfurlref_to_char(CFURLRef url, char *path, int len); +#endif // Project Manager GtkWidget *pm_view, *pm_entry, *pm_container; @@ -72,16 +81,13 @@ int main(int argc, char **argv) { #ifdef MAC CFBundleRef bundle = CFBundleGetMainBundle(); if (bundle) { - char path[260]; + char *bundle_path = static_cast(malloc(FILENAME_MAX * sizeof(char))); CFURLRef bundle_url = CFBundleCopyBundleURL(bundle); - CFStringRef bundle_path = - CFURLCopyFileSystemPath(bundle_url, kCFURLPOSIXPathStyle); - CFStringGetCString(bundle_path, path, 260, kCFStringEncodingASCII); - char *resources_path = g_strconcat(path, "/Contents/Resources/", NULL); - textadept_home = static_cast(resources_path); - g_free(path); - CFRelease(bundle_url); - CFRelease(bundle_path); + cfurlref_to_char(bundle_url, bundle_path, FILENAME_MAX); + char *res_path = g_strconcat(bundle_path, "/Contents/Resources/", NULL); + textadept_home = static_cast(res_path); + g_free(bundle_path); + CFRelease(bundle_url); } else textadept_home = ""; #endif gtk_init(&argc, &argv); @@ -102,7 +108,7 @@ int main(int argc, char **argv) { * @see main */ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int) { - char path[260]; + char path[FILENAME_MAX]; GetModuleFileName(0, path, sizeof(path)); char *last_slash = strrchr(path, '\\'); if (last_slash) *last_slash = '\0'; @@ -146,6 +152,13 @@ void create_ui() { signal(window, "delete_event", w_exit); signal(window, "focus-in-event", w_focus); signal(window, "key_press_event", w_keypress); + +#ifdef MAC + AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, + NewAEEventHandlerUPP(w_ae_open), 0, false); + AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, + NewAEEventHandlerUPP(w_ae_quit), 0, false); +#endif GtkWidget *vbox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(window), vbox); @@ -507,7 +520,11 @@ static void t_command(GtkWidget *editor, gint wParam, gpointer, gpointer) { static gbool t_keypress(GtkWidget*, GdkEventKey *event, gpointer) { bool shift = event->state & GDK_SHIFT_MASK; bool control = event->state & GDK_CONTROL_MASK; +#ifndef MAC bool alt = event->state & GDK_MOD1_MASK; +#else + bool alt = event->state & GDK_META_MASK; +#endif return l_handle_keypress(event->keyval, shift, control, alt) ? TRUE : FALSE; } @@ -547,6 +564,57 @@ static gbool w_exit(GtkWidget*, GdkEventAny*, gpointer) { return FALSE; } +#ifdef MAC +/** + * Signal for an Open Document AppleEvent. + * Generates a 'appleevent_odoc' event for each document sent. + * @see l_handle_event + */ +static OSErr w_ae_open(const AppleEvent *event, AppleEvent*, long) { + AEDescList file_list; + if (AEGetParamDesc(event, keyDirectObject, typeAEList, &file_list) == noErr) { + long count = 0; + AECountItems(&file_list, &count); + for (int i = 1; i <= count; i++) { + FSRef fsref; + char *path = static_cast(malloc(FILENAME_MAX * sizeof(char))); + AEGetNthPtr( + &file_list, i, typeFSRef, NULL, NULL, &fsref, sizeof(FSRef), NULL); + CFURLRef url = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsref); + if (url) { + cfurlref_to_char(url, path, FILENAME_MAX); + l_handle_event("appleevent_odoc", path); + CFRelease(url); + } + g_free(path); + } + AEDisposeDesc(&file_list); + } + return noErr; +} + +/** + * Signal for a Quit Application AppleEvent. + * Calls the signal for exiting Textadept. + * @see w_exit + */ +static OSErr w_ae_quit(const AppleEvent *event, AppleEvent*, long) { + if (w_exit(NULL, NULL, NULL)) return noErr; + return errAEEventNotHandled; +} + +/** + * Helper function to convert an Apple CFURLRef to a char*. + * @param url The CFURLRef for a file URL. + * @return char* containing the filepath in POSIX style. + */ +void cfurlref_to_char(CFURLRef url, char *path, int len) { + CFStringRef str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle); + CFStringGetCString(str, path, len, kCFStringEncodingASCII); + CFRelease(str); +} +#endif + // Project Manager /** diff --git a/src/textadept.h b/src/textadept.h index ad490cf2..4b042a27 100644 --- a/src/textadept.h +++ b/src/textadept.h @@ -3,6 +3,7 @@ #ifndef TEXTADEPT_H #define TEXTADEPT_H +#include #include #include #include @@ -24,7 +25,8 @@ extern "C" { #endif #ifdef MAC -#include "CoreFoundation/CoreFoundation.h" +#include +using namespace Scintilla; #endif // globals -- cgit v1.2.3