#include "wx/cocoa/string.h"
#include "wx/cocoa/trackingrectmanager.h"
+#import <Foundation/NSArray.h>
#import <Foundation/NSRunLoop.h>
#include "wx/cocoa/objc/NSView.h"
#import <AppKit/NSEvent.h>
void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
{
+ // Pool here due to lack of one during wx init phase
+ wxAutoNSAutoreleasePool pool;
+
NSView *childView = child->GetNSViewForSuperview();
wxASSERT(childView);
return true;
}
+bool wxWindowCocoa::SetCursor(const wxCursor &cursor)
+{
+ if(!wxWindowBase::SetCursor(cursor))
+ return false;
+ // Invalidate the cursor rects so the cursor will change
+ [[GetNSView() window] invalidateCursorRectsForView:GetNSView()];
+ return true;
+}
+
bool wxWindowCocoa::Cocoa_viewDidMoveToWindow()
{
wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::viewDidMoveToWindow"),this);
bool wxWindow::SetFont(const wxFont& font)
{
- // TODO
- return true;
+ // FIXME: We may need to handle wx font inheritance.
+ return wxWindowBase::SetFont(font);
}
-static CocoaWindowCompareFunctionResult CocoaRaiseWindowCompareFunction(id first, id second, void *target)
+#if 0 // these are used when debugging the algorithm.
+static char const * const comparisonresultStrings[] =
+{ "<"
+, "=="
+, ">"
+};
+#endif
+
+class CocoaWindowCompareContext
{
+ DECLARE_NO_COPY_CLASS(CocoaWindowCompareContext)
+public:
+ CocoaWindowCompareContext(); // Not implemented
+ CocoaWindowCompareContext(NSView *target, NSArray *subviews)
+ {
+ m_target = target;
+ // Cocoa sorts subviews in-place.. make a copy
+ m_subviews = [subviews copy];
+ }
+ ~CocoaWindowCompareContext()
+ { // release the copy
+ [m_subviews release];
+ }
+ NSView* target()
+ { return m_target; }
+ NSArray* subviews()
+ { return m_subviews; }
+ /* Helper function that returns the comparison based off of the original ordering */
+ CocoaWindowCompareFunctionResult CompareUsingOriginalOrdering(id first, id second)
+ {
+ NSUInteger firstI = [m_subviews indexOfObjectIdenticalTo:first];
+ NSUInteger secondI = [m_subviews indexOfObjectIdenticalTo:second];
+ // NOTE: If either firstI or secondI is NSNotFound then it will be NSIntegerMax and thus will
+ // likely compare higher than the other view which is reasonable considering the only way that
+ // can happen is if the subview was added after our call to subviews but before the call to
+ // sortSubviewsUsingFunction:context:. Thus we don't bother checking. Particularly because
+ // that case should never occur anyway because that would imply a multi-threaded GUI call
+ // which is a big no-no with Cocoa.
+
+ // Subviews are ordered from back to front meaning one that is already lower will have an lower index.
+ NSComparisonResult result = (firstI < secondI)
+ ? NSOrderedAscending /* -1 */
+ : (firstI > secondI)
+ ? NSOrderedDescending /* 1 */
+ : NSOrderedSame /* 0 */;
+
+#if 0 // Enable this if you need to debug the algorithm.
+ NSLog(@"%@ [%d] %s %@ [%d]\n", first, firstI, comparisonresultStrings[result+1], second, secondI);
+#endif
+ return result;
+ }
+private:
+ /* The subview we are trying to Raise or Lower */
+ NSView *m_target;
+ /* A copy of the original array of subviews */
+ NSArray *m_subviews;
+};
+
+/* Causes Cocoa to raise the target view to the top of the Z-Order by telling the sort function that
+ * the target view is always higher than every other view. When comparing two views neither of
+ * which is the target, it returns the correct response based on the original ordering
+ */
+static CocoaWindowCompareFunctionResult CocoaRaiseWindowCompareFunction(id first, id second, void *ctx)
+{
+ CocoaWindowCompareContext *compareContext = (CocoaWindowCompareContext*)ctx;
// first should be ordered higher
- if(first==target)
+ if(first==compareContext->target())
return NSOrderedDescending;
// second should be ordered higher
- if(second==target)
+ if(second==compareContext->target())
return NSOrderedAscending;
- return NSOrderedSame;
+ return compareContext->CompareUsingOriginalOrdering(first,second);
}
// Raise the window to the top of the Z order
{
// wxAutoNSAutoreleasePool pool;
NSView *nsview = GetNSViewForSuperview();
- [[nsview superview] sortSubviewsUsingFunction:
+ NSView *superview = [nsview superview];
+ CocoaWindowCompareContext compareContext(nsview, [superview subviews]);
+
+ [superview sortSubviewsUsingFunction:
CocoaRaiseWindowCompareFunction
- context: nsview];
+ context: &compareContext];
}
-static CocoaWindowCompareFunctionResult CocoaLowerWindowCompareFunction(id first, id second, void *target)
+/* Causes Cocoa to lower the target view to the bottom of the Z-Order by telling the sort function that
+ * the target view is always lower than every other view. When comparing two views neither of
+ * which is the target, it returns the correct response based on the original ordering
+ */
+static CocoaWindowCompareFunctionResult CocoaLowerWindowCompareFunction(id first, id second, void *ctx)
{
+ CocoaWindowCompareContext *compareContext = (CocoaWindowCompareContext*)ctx;
// first should be ordered lower
- if(first==target)
+ if(first==compareContext->target())
return NSOrderedAscending;
// second should be ordered lower
- if(second==target)
+ if(second==compareContext->target())
return NSOrderedDescending;
- return NSOrderedSame;
+ return compareContext->CompareUsingOriginalOrdering(first,second);
}
// Lower the window to the bottom of the Z order
void wxWindow::Lower()
{
NSView *nsview = GetNSViewForSuperview();
- [[nsview superview] sortSubviewsUsingFunction:
+ NSView *superview = [nsview superview];
+ CocoaWindowCompareContext compareContext(nsview, [superview subviews]);
+
+#if 0
+ NSLog(@"Target:\n%@\n", nsview);
+ NSLog(@"Before:\n%@\n", compareContext.subviews());
+#endif
+ [superview sortSubviewsUsingFunction:
CocoaLowerWindowCompareFunction
- context: nsview];
+ context: &compareContext];
+#if 0
+ NSLog(@"After:\n%@\n", [superview subviews]);
+#endif
}
bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
void wxCocoaTrackingRectManager::BuildTrackingRect()
{
+ // Pool here due to lack of one during wx init phase
+ wxAutoNSAutoreleasePool pool;
+
wxASSERT_MSG(!m_isTrackingRectActive, wxT("Tracking rect was not cleared"));
if([m_window->GetNSView() window] != nil)
{