Prettified ObjC stuff a little

master
Yevgen Muntyan 2007-08-07 23:48:00 -05:00
parent c1f112fc88
commit e0bf3ebda7
12 changed files with 491 additions and 108 deletions

View File

@ -27,6 +27,10 @@ MOO_AC_PROGS
ACLOCAL="$ACLOCAL $ACLOCAL_FLAGS"
if test "x$MOO_USE_PIPE_INPUT" = xyes; then
AC_DEFINE([MOO_USE_PIPE_INPUT], [1], [use pipes for input])
fi
MOO_SRC_PREFIX="moo"
AC_SUBST(MOO_SRC_PREFIX)
MOO_COMPONENTS([app,edit,term],[])

View File

@ -6,21 +6,77 @@ AC_DEFUN_ONCE([MOO_AC_OBJC],[
if test "x$MOO_USE_OBJC" != "xno"; then
_MOO_OBJC_CHECK([
MOO_WARN_OBJC=
MOO_USE_OBJC=yes
MOO_OBJC_LIBS="-lobjc"
AC_DEFINE(MOO_USE_OBJC, 1, [Use Objective-C.])
AC_MSG_NOTICE([Objective-C flags: $MOO_OBJCFLAGS $MOO_OBJC_LIBS])
],[
MOO_WARN_OBJC="Objective-C support is disabled, editor user tools will be disabled"
MOO_USE_OBJC=no
MOO_OBJC_LIBS=""
MOO_OBJCFLAGS=
MOO_OBJC_LIBS=
])
fi
AC_SUBST(MOO_OBJCFLAGS)
AC_SUBST(MOO_OBJC_LIBS)
AM_CONDITIONAL(MOO_USE_OBJC, test $MOO_USE_OBJC = yes)
])
AC_DEFUN([_MOO_OBJC_CHECK],[
#############################################################################
## Try to find Foundation
##
AC_DEFUN([_MOO_OBJC_CHECK_FOUNDATION],[
AC_MSG_CHECKING([for Foundation])
AC_LANG_SAVE
AC_LANG_OBJC
saved_LIBS="$LIBS"
LIBS="-lobjc $LIBS"
_moo_ac_have_foundation=no
# First try to compile and link without any stuff, OBJCFLAGS and LIBS might be good enough
AC_LINK_IFELSE([AC_LANG_PROGRAM([#import <Foundation/Foundation.h>],[NSAutoreleasePool *pool = @<:@@<:@NSAutoreleasePool alloc@:>@ init@:>@;])],[
_moo_ac_have_foundation=yes
MOO_OBJC_LIBS="-lobjc"
AC_MSG_RESULT([yes])
],[
LIBS="$LIBS -framework Foundation -lobjc"
AC_LINK_IFELSE([AC_LANG_PROGRAM([#import <Foundation/Foundation.h>],[NSAutoreleasePool *pool = @<:@@<:@NSAutoreleasePool alloc@:>@ init@:>@;])],[
_moo_ac_have_foundation=yes
MOO_OBJC_LIBS="-framework Foundation -lobjc"
AC_MSG_RESULT([yes])
],[
_moo_ac_have_foundation=no
])
])
LIBS="$saved_LIBS"
AC_LANG_RESTORE
if test "$_moo_ac_have_foundation" = "no"; then
PKG_CHECK_MODULES(OBJCX,[libobjcx],[
MOO_OBJC_LIBS="$OBJCX_LIBS"
MOO_OBJCFLAGS="$OBJCX_CFLAGS"
AC_MSG_RESULT([SideStep])
_moo_ac_have_foundation=yes
],[
AC_MSG_RESULT([no])
])
fi
if test "$_moo_ac_have_foundation" = "yes"; then
MOO_OBJC_USE_FOUNDATION=yes
MOO_OBJCFLAGS="$MOO_OBJCFLAGS -DMOO_OBJC_USE_FOUNDATION"
fi
])
#############################################################################
## Check if basic ObjC runtime is available
##
AC_DEFUN([_MOO_OBJC_CHECK_RUNTIME],[
AC_MSG_CHECKING(whether Objective-C compiler works)
AC_LANG_SAVE
@ -29,28 +85,42 @@ AC_DEFUN([_MOO_OBJC_CHECK],[
LIBS="-lobjc $LIBS"
AC_LINK_IFELSE([AC_LANG_PROGRAM(
[#import <objc/Object.h>
[#import <objc/Object.h>
@interface Foo : Object {
@private
int blah;
}
- foo;
@end
@interface Foo : Object {
@private
int blah;
}
- foo;
@end
@implementation Foo : Object
- foo
{
return self;
}
@end
],
[Foo *obj = @<:@Foo new@:>@;])],
[AC_MSG_RESULT(yes)
$1],
[AC_MSG_RESULT(no)
$2])
@implementation Foo : Object
- foo
{
return self;
}
@end
],
[Foo *obj = @<:@Foo new@:>@;])],
[AC_MSG_RESULT(yes)
MOO_OBJC_LIBS="-lobjc"
$1],
[AC_MSG_RESULT(no)
$2])
LIBS="$save_LIBS"
AC_LANG_RESTORE
])
AC_DEFUN([_MOO_OBJC_CHECK],[
MOO_OBJC_LIBS=
MOO_OBJCFLAGS=
_MOO_OBJC_CHECK_RUNTIME([
MOO_OBJC_USE_FOUNDATION=no
dnl _MOO_OBJC_CHECK_FOUNDATION
$1
],[
$2
])
])

View File

@ -68,10 +68,6 @@ DIST_SUBDIRS = mooutils mooterm mooedit mooapp moopython mooscript moofileview
lib_LTLIBRARIES =
noinst_LTLIBRARIES =
if MOO_USE_OBJC
objc_libs = -lobjc
endif
############################################################################
# libmoo
#
@ -105,7 +101,7 @@ export_flags = -export-symbols-regex "^(moo_).*"
endif
endif
libmoo_la_LIBADD = $(libs) $(MOO_LIBS) $(objc_libs)
libmoo_la_LIBADD = $(libs) $(MOO_LIBS) $(MOO_OBJC_LIBS)
libmoo_la_LDFLAGS = $(libmoo_res_ldflag) -no-undefined -avoid-version $(export_flags)
libmoo_la_SOURCES = $(moo_include_headers)
nodist_libmoo_la_SOURCES += $(nodist_moo_include_headers)

View File

@ -241,7 +241,7 @@ AM_CFLAGS = \
-I$(top_builddir) \
$(MOO_CFLAGS) \
$(MOO_DEBUG_CFLAGS)
AM_OBJCFLAGS = $(AM_CFLAGS)
AM_OBJCFLAGS = $(AM_CFLAGS) $(MOO_OBJCFLAGS)
if MOO_INSTALL_HEADERS
mooedit_includedir = $(moo_includedir)/mooedit

View File

@ -10,7 +10,7 @@ AM_CFLAGS = \
-I$(top_builddir) \
$(MOO_CFLAGS) \
$(MOO_DEBUG_CFLAGS)
AM_OBJCFLAGS = $(AM_CFLAGS)
AM_OBJCFLAGS = $(AM_CFLAGS) $(MOO_OBJCFLAGS)
###########################################################################
# fileselector

View File

@ -45,7 +45,7 @@ AM_CFLAGS = \
-I$(top_builddir) \
$(MOO_CFLAGS) \
$(MOO_DEBUG_CFLAGS)
AM_OBJCFLAGS = $(AM_CFLAGS)
AM_OBJCFLAGS = $(AM_CFLAGS) $(MOO_OBJCFLAGS)
if MOO_INSTALL_HEADERS
mooscript_includedir = $(moo_includedir)/mooscript

View File

@ -99,18 +99,19 @@ mooutils_sources = \
moomenumgr.c \
moomenutoolbutton.c \
moonotebook.c \
mooobjc.h \
moopane.c \
moopaned.c \
mooprefs.c \
mooprefsdialog.c \
mooprefsdialogpage.c \
moopython.c \
moospawn.h \
moospawn.c \
moostock.c \
mootype-macros.h \
moouixml.c \
mooundo.c \
mooutils-fli.c \
mooutils-fs.c \
mooutils-gobject-private.h \
mooutils-gobject.c \
@ -124,6 +125,9 @@ mooutils_sources = \
stock-select-all-24.h \
stock-terminal-24.h
mooobjc.lo: mooutils-fli.lo
moopython.lo: mooutils-fli.lo
objc_sources = \
moocobject.h \
moocobject.m
@ -221,7 +225,7 @@ AM_CFLAGS = \
$(MOO_CFLAGS) \
$(MOO_DEBUG_CFLAGS) \
-DMOO_LOCALE_DIR=\"$(datadir)/locale\"
AM_OBJCFLAGS = $(AM_CFLAGS)
AM_OBJCFLAGS = $(AM_CFLAGS) $(MOO_OBJCFLAGS)
if MOO_INSTALL_HEADERS
mooutils_includedir = $(moo_includedir)/mooutils

View File

@ -11,106 +11,66 @@
* See COPYING file that comes with this distribution.
*/
/* Objective-C objects with GObject goodies: qdata and toggle refs */
#ifndef MOO_COBJECT_H
#define MOO_COBJECT_H
#include <glib-object.h>
#ifdef __OBJC__
#import <objc/objc-api.h>
#ifdef MOO_OBJC_USE_FOUNDATION
#import <Foundation/Foundation.h>
#else
#import <objc/Object.h>
#endif
G_BEGIN_DECLS
#define MOO_UNUSED_VAR(var) ((void)var)
#ifdef CSTR
#ifdef __GNUC__
#warning "CSTR defined"
#endif
#endif
#undef CSTR
typedef const char *CSTR;
#ifndef MOO_OBJC_USE_FOUNDATION
@class MooCObject;
/* stripped down NSObject protocol, to make it possible to substitute
* MooCObject with NSObject when it's available */
@protocol MooCObject
- (Class) class;
- (Class) superclass;
- (BOOL) isKindOfClass: (Class)aClass;
- (id) performSelector: (SEL)aSelector;
- (BOOL) respondsToSelector: (SEL)aSelector;
typedef void (*MooToggleNotify) (gpointer data,
MooCObject *object,
BOOL is_last_ref);
@interface MooCObject : Object {
@private
unsigned moo_c_object_ref_count;
GData *moo_c_object_qdata;
}
- (void) setQData :(GQuark)key
:(gpointer)data;
- (void) setQData :(GQuark)key
:(gpointer)data
withDestroy:(GDestroyNotify)destroy;
- (gpointer) getQData :(GQuark)key;
- (void) setData :(CSTR)key
:(gpointer)data;
- (void) setData :(CSTR)key
:(gpointer)data
withDestroy:(GDestroyNotify)destroy;
- (gpointer) getData :(CSTR)key;
- (void) addToggleRef :(MooToggleNotify)notify
:(gpointer)data;
- (void) removeToggleRef :(MooToggleNotify)notify
:(gpointer)data;
- retain;
- (id) retain;
- (void) release;
- autorelease;
- (id) autorelease;
- (guint) retainCount;
- (void) dealloc;
@end
@interface MooAutoreleasePool : MooCObject
@interface MooCObject : Object <MooCObject>
{
@private
MooAutoreleasePool *parent;
MooAutoreleasePool *child;
GPtrArray *objects;
guint retainCount;
}
+ (void) addObject: (id)anObj;
+ (id) currentPool;
- (void) addObject: (id)anObj;
- (void) emptyPool;
@end
#else // MOO_OBJC_USE_FOUNDATION
GType moo_cboxed_type_new (Class klass,
gboolean copy);
#define MooCObject NSObject
#define MOO_DEFINE_CBOXED_TYPE(copy) \
+ (GType) get_boxed_type \
{ \
static GType type; \
if (G_UNLIKELY (!type)) \
type = moo_cboxed_type_new ([self class], copy);\
return type; \
}
#endif // MOO_OBJC_USE_FOUNDATION
id moo_cobject_check_type_cast (id obj, Class klass);
BOOL moo_cobject_check_type (id obj, Class klass);
#ifndef G_DISABLE_CAST_CHECKS
#define MOO_COBJECT_CHECK_TYPE_CAST(obj,ClassName) ((ClassName*) moo_cobject_check_type_cast (obj, [ClassName class]))
#define MOO_COBJECT_CHECK_TYPE(obj,ClassName) (moo_cobject_check_type (obj, [ClassName class]))
#else /* G_DISABLE_CAST_CHECKS */
#define MOO_COBJECT_CHECK_TYPE_CAST(obj,ClassName) ((ClassName*)obj)
#define MOO_COBJECT_CHECK_TYPE(obj,ClassName) (obj != nil)
#endif /* G_DISABLE_CAST_CHECKS */
void moo_init_objc_api (void);
#endif /* __OBJC__ */
void moo_objc_push_autorelease_pool (void);
void moo_objc_pop_autorelease_pool (void);
#ifdef CSTR
#warning "CSTR defined"
#endif
#undef CSTR
typedef const char *CSTR;
G_END_DECLS
#endif /* MOO_COBJECT_H */
/* -*- objc -*- */

View File

@ -13,6 +13,268 @@
#include <config.h>
#import "moocobject.h"
#import "mooobjc.h"
#import <objc/objc-api.h>
static GSList *autorelease_pools;
#ifndef MOO_OBJC_USE_FOUNDATION
@interface MooAutoreleasePool : MooCObject
{
@private
MooAutoreleasePool *parent;
MooAutoreleasePool *child;
GSList *objects;
}
+ (void) addObject: (id)anObj;
- (void) addObject: (id)anObj;
- (void) emptyPool;
@end
#else
typedef NSAutoreleasePool MooAutoreleasePool;
#endif
static void
moo_objc_send_msg (MooObjCObject *m_obj,
const char *name)
{
SEL aSelector;
id<MooCObject> obj = (id) m_obj;
g_return_if_fail (obj != NULL);
g_return_if_fail (name != NULL);
aSelector = sel_get_any_uid (name);
g_return_if_fail (aSelector != NULL);
g_return_if_fail ([obj respondsToSelector:aSelector]);
[obj performSelector:aSelector];
}
static MooObjCObject *
moo_objc_retain (MooObjCObject *m_obj)
{
id<MooCObject> obj = (id) m_obj;
g_return_val_if_fail (obj != NULL, NULL);
return (MooObjCObject*)[obj retain];
}
static MooObjCObject *
moo_objc_autorelease (MooObjCObject *m_obj)
{
id<MooCObject> obj = (id) m_obj;
g_return_val_if_fail (obj != NULL, NULL);
return (MooObjCObject*)[obj autorelease];
}
static void
moo_objc_release (MooObjCObject *m_obj)
{
id<MooCObject> obj = (id) m_obj;
g_return_if_fail (obj != NULL);
[obj release];
}
static char *
moo_objc_get_info (void)
{
return g_strdup ("Objective-C");
}
void
moo_init_objc_api (void)
{
static MooObjCAPI api = {
moo_objc_retain,
moo_objc_autorelease,
moo_objc_release,
moo_objc_get_info,
moo_objc_send_msg,
moo_objc_push_autorelease_pool,
moo_objc_pop_autorelease_pool
};
static gboolean been_here = FALSE;
if (!been_here)
{
been_here = TRUE;
moo_objc_init (MOO_OBJC_API_VERSION, &api);
}
}
void
moo_objc_push_autorelease_pool (void)
{
MooAutoreleasePool *pool = [[MooAutoreleasePool alloc] init];
g_return_if_fail (pool != nil);
autorelease_pools = g_slist_prepend (autorelease_pools, pool);
}
void
moo_objc_pop_autorelease_pool (void)
{
MooAutoreleasePool *pool;
g_return_if_fail (autorelease_pools != NULL);
pool = autorelease_pools->data;
autorelease_pools = g_slist_delete_link (autorelease_pools, autorelease_pools);
[pool release];
}
#ifndef MOO_OBJC_USE_FOUNDATION
@implementation MooCObject
+ initialize
{
moo_init_objc_api ();
return self;
}
- init
{
[super init];
retainCount = 1;
return self;
}
- (Class) class
{
return [super class];
}
- (Class) superclass
{
return [super superClass];
}
- (BOOL) isKindOfClass: (Class)aClass
{
return [super isKindOf:aClass];
}
- (id) performSelector: (SEL)aSelector
{
return [super perform:aSelector];
}
- (BOOL) respondsToSelector: (SEL)aSelector
{
return [super respondsTo:aSelector];
}
- (id) retain
{
retainCount += 1;
return self;
}
- (void) release
{
if (!--retainCount)
[self dealloc];
}
- (id) autorelease
{
[MooAutoreleasePool addObject:self];
return self;
}
- (guint) retainCount
{
return retainCount;
}
- (void) dealloc
{
[self free];
}
@end
@implementation MooAutoreleasePool
static MooAutoreleasePool *currentPool;
+ (void) addObject: (id)anObj
{
g_return_if_fail (currentPool != nil);
[currentPool addObject: anObj];
}
- (void) addObject: (id)anObj
{
objects = g_slist_prepend (objects, anObj);
}
- (void) emptyPool
{
GSList *list, *l;
list = objects;
objects = NULL;
for (l = list; l != NULL; l = l->next)
{
id<MooCObject> obj = l->data;
[obj release];
}
g_slist_free (list);
}
- (id) autorelease
{
g_return_val_if_reached (self);
}
- init
{
[super init];
if (currentPool)
{
currentPool->child = self;
parent = currentPool;
}
currentPool = self;
return self;
}
- (void) dealloc
{
currentPool = parent;
if (currentPool)
currentPool->child = nil;
[self emptyPool];
[super dealloc];
}
@end
#endif // !MOO_OBJC_USE_FOUNDATION
#if 0
#ifdef MOO_OS_DARWIN
#define class_get_class_name(klass) (klass->name)
@ -367,5 +629,7 @@ moo_cobject_check_type (id obj, Class klass)
return obj != nil && [obj isKindOf :klass];
}
#endif
/* -*- objc -*- */

View File

@ -928,6 +928,7 @@ pack_children (MooGladeXML *xml,
else if (child->widget->props->mask & PROP_RESPONSE_ID)
{
Child *parent = child->parent_node->parent_node;
if (!parent || !parent->internal_child ||
strcmp (parent->internal_child, "action_area"))
{

62
moo/mooutils/mooobjc.h Normal file
View File

@ -0,0 +1,62 @@
/*
* mooobjc.h
*
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* See COPYING file that comes with this distribution.
*/
#ifndef MOO_OBJC_H
#define MOO_OBJC_H
#include <glib.h>
G_BEGIN_DECLS
#define MOO_OBJC_API_VERSION 1
typedef struct _MooObjCAPI MooObjCAPI;
typedef struct _MooObjCObject MooObjCObject;
struct _MooObjCAPI {
MooObjCObject *(*retain) (MooObjCObject *obj);
MooObjCObject *(*autorelease) (MooObjCObject *obj);
void (*release) (MooObjCObject *obj);
char *(*get_info) (void);
void (*send_msg) (MooObjCObject *obj,
const char *name);
void (*push_autorelease_pool) (void);
void (*pop_autorelease_pool) (void);
};
extern MooObjCAPI *moo_objc_api;
gboolean moo_objc_init (guint version,
MooObjCAPI *api);
#ifndef __OBJC__
#define moo_objc_present() (moo_objc_api != NULL)
#define moo_objc_get_info moo_objc_api->get_info
#define moo_objc_retain moo_objc_api->retain
#define moo_objc_release moo_objc_api->release
#define moo_objc_autorelease moo_objc_api->autorelease
#define moo_objc_send_msg moo_objc_api->send_msg
#define moo_objc_push_autorelease_pool moo_objc_api->push_autorelease_pool
#define moo_objc_pop_autorelease_pool moo_objc_api->pop_autorelease_pool
#endif /* __OBJC__ */
G_END_DECLS
#endif /* MOO_OBJC_H */

View File

@ -1,5 +1,5 @@
/*
* moopython.c
* mooutils-fli.c
*
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
@ -12,8 +12,13 @@
*/
#include "mooutils/moopython.h"
#include "mooutils/mooobjc.h"
/**
* Python
*/
typedef struct {
gpointer data;
GDestroyNotify destroy;
@ -92,3 +97,20 @@ moo_Py_DECREF (MooPyObject *obj)
if (obj)
moo_py_api->decref (obj);
}
/**
* Objective-C
*/
MooObjCAPI *moo_objc_api = NULL;
gboolean
moo_objc_init (guint version,
MooObjCAPI *api)
{
g_return_val_if_fail (version == MOO_OBJC_API_VERSION, FALSE);
g_return_val_if_fail (!moo_objc_api || !api, FALSE);
moo_objc_api = api;
return TRUE;
}