<commit_before>//
//  GuiObjectView.mm
//  MusicPlayer
//
//  Created by <NAME> on 23.01.14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

#import "GuiObjectView.hpp"
#include "PythonHelpers.h"

@implementation GuiObjectView

- (void)dealloc
{
	PyGILState_STATE gstate = PyGILState_Ensure();
	Py_CLEAR(controlRef);
	PyGILState_Release(gstate);
}

- (id)initWithControl:(CocoaGuiObject*)control;
{
	NSRect frame = NSMakeRect(0, 0, control->PresetSize.x, control->PresetSize.y);
    self = [super initWithFrame:frame];
    if(!self) return nil;

	PyGILState_STATE gstate = PyGILState_Ensure();
	controlRef = (PyWeakReference*) PyWeakref_NewRef((PyObject*) control, NULL);
	canHaveFocus = attrChain_bool_default(control->attr, "canHaveFocus", false);
	PyGILState_Release(gstate);

	if(!controlRef) {
		printf("Cocoa GuiObject: cannot create controlRef\n");
		return nil;
	}

	//if(canHaveFocus)
	//	[self setDrawsBackground:YES];

	return self;
}

- (CocoaGuiObject*)getControl;
{
	CocoaGuiObject* control = (CocoaGuiObject*) PyWeakref_GET_OBJECT(controlRef);
	if(!control) return NULL;
	if(!PyType_IsSubtype(Py_TYPE(control), &CocoaGuiObject_Type)) {
		printf("Cocoa GuiObjectView: control is wrong type\n");
		return NULL;
	}
	Py_INCREF(control);
	return control;
}


- (BOOL)acceptsFirstResponder
{
	return canHaveFocus;
}

- (BOOL)becomeFirstResponder
{
	if(![super becomeFirstResponder]) return NO;
	[self setDrawsFocusRing:YES];
	//[self setBackgroundColor:[NSColor selectedTextBackgroundColor]];
	return YES;
}

- (BOOL)resignFirstResponder
{
	if(![super resignFirstResponder]) return NO;
	[self setDrawsFocusRing:NO];
	//[self setBackgroundColor:[NSColor textBackgroundColor]];
	return YES;
}

- (void)mouseDown:(NSEvent *)theEvent
{
	[super mouseDown:theEvent];
	
	PyGILState_STATE gstate = PyGILState_Ensure();
	CocoaGuiObject* control = [self getControl];
	if(control) control->handleCurSelectedSong();
	Py_XDECREF(control);
	PyGILState_Release(gstate);
}

- (void)mouseDragged:(NSEvent *)ev
{
	bool res = false;
	
	PyGILState_STATE gstate = PyGILState_Ensure();
	CocoaGuiObject* control = [self getControl];
	PyObject* subjectObj = control ? control->subjectObject : NULL;
	Py_XINCREF(subjectObj);
	if(control && subjectObj) {
		// For example, if this is a Song object, we have the "url" attrib.
		PyObject* filename = PyObject_GetAttrString(subjectObj, "url");
		if(!filename) {
			if(PyErr_ExceptionMatches(PyExc_AttributeError))
				PyErr_Clear();
			else
				PyErr_Print();
		}
		int ret = filename ? PyObject_IsTrue(filename) : 0;
		if(ret < 0 && PyErr_Occurred())
			PyErr_Print();
		NSString* fn = (ret > 0) ? convertToStr(filename) : nil;
		if(fn) {
			NSPasteboard* pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
			[pboard declareTypes:@[NSFilenamesPboardType] owner:nil];
			[pboard setPropertyList:@[fn] forType:NSFilenamesPboardType];
			NSImage* dragImage = [[NSWorkspace sharedWorkspace] iconForFile:fn];
			NSPoint dragPosition = [self convertPoint:[ev locationInWindow] toView:nil];
			dragPosition.x -= 16;
			dragPosition.y += 32;
			
			[self
			 dragImage:dragImage
			 at:dragPosition
			 offset:NSZeroSize
			 event:ev
			 pasteboard:pboard
			 source:self
			 slideBack:NO];
			
			res = true;
		}
		Py_XDECREF(filename);
	}
	Py_XDECREF(subjectObj);
	Py_XDECREF(control);
	PyGILState_Release(gstate);<commit_msg>guiCocoa: fix lock when dragging<commit_after>//
//  GuiObjectView.mm
//  MusicPlayer
//
//  Created by <NAME> on 23.01.14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

#import "GuiObjectView.hpp"
#include "PythonHelpers.h"

@implementation GuiObjectView

- (void)dealloc
{
	PyGILState_STATE gstate = PyGILState_Ensure();
	Py_CLEAR(controlRef);
	PyGILState_Release(gstate);
}

- (id)initWithControl:(CocoaGuiObject*)control;
{
	NSRect frame = NSMakeRect(0, 0, control->PresetSize.x, control->PresetSize.y);
    self = [super initWithFrame:frame];
    if(!self) return nil;

	PyGILState_STATE gstate = PyGILState_Ensure();
	controlRef = (PyWeakReference*) PyWeakref_NewRef((PyObject*) control, NULL);
	canHaveFocus = attrChain_bool_default(control->attr, "canHaveFocus", false);
	PyGILState_Release(gstate);

	if(!controlRef) {
		printf("Cocoa GuiObject: cannot create controlRef\n");
		return nil;
	}

	//if(canHaveFocus)
	//	[self setDrawsBackground:YES];

	return self;
}

- (CocoaGuiObject*)getControl;
{
	CocoaGuiObject* control = (CocoaGuiObject*) PyWeakref_GET_OBJECT(controlRef);
	if(!control) return NULL;
	if(!PyType_IsSubtype(Py_TYPE(control), &CocoaGuiObject_Type)) {
		printf("Cocoa GuiObjectView: control is wrong type\n");
		return NULL;
	}
	Py_INCREF(control);
	return control;
}


- (BOOL)acceptsFirstResponder
{
	return canHaveFocus;
}

- (BOOL)becomeFirstResponder
{
	if(![super becomeFirstResponder]) return NO;
	[self setDrawsFocusRing:YES];
	//[self setBackgroundColor:[NSColor selectedTextBackgroundColor]];
	return YES;
}

- (BOOL)resignFirstResponder
{
	if(![super resignFirstResponder]) return NO;
	[self setDrawsFocusRing:NO];
	//[self setBackgroundColor:[NSColor textBackgroundColor]];
	return YES;
}

- (void)mouseDown:(NSEvent *)theEvent
{
	[super mouseDown:theEvent];
	
	PyGILState_STATE gstate = PyGILState_Ensure();
	CocoaGuiObject* control = [self getControl];
	if(control) control->handleCurSelectedSong();
	Py_XDECREF(control);
	PyGILState_Release(gstate);
}

- (void)mouseDragged:(NSEvent *)ev
{
	bool res = false;
	
	PyGILState_STATE gstate = PyGILState_Ensure();
	CocoaGuiObject* control = [self getControl];
	PyObject* subjectObj = control ? control->subjectObject : NULL;
	Py_XINCREF(subjectObj);
	if(control && subjectObj) {
		// For example, if this is a Song object, we have the "url" attrib.
		PyObject* filename = PyObject_GetAttrString(subjectObj, "url");
		if(!filename) {
			if(PyErr_ExceptionMatches(PyExc_AttributeError))
				PyErr_Clear();
			else
				PyErr_Print();
		}
		int ret = filename ? PyObject_IsTrue(filename) : 0;
		if(ret < 0 && PyErr_Occurred())
			PyErr_Print();
		NSString* fn = (ret > 0) ? convertToStr(filename) : nil;
		if(fn) {
			NSPasteboard* pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
			[pboard declareTypes:@[NSFilenamesPboardType] owner:nil];
			[pboard setPropertyList:@[fn] forType:NSFilenamesPboardType];
			NSImage* dragImage = [[NSWorkspace sharedWorkspace] iconForFile:fn];
			NSPoint dragPosition = [self convertPoint:[ev locationInWindow] toView:nil];
			dragPosition.x -= 16;
			dragPosition.y += 32;
			
			Py_BEGIN_ALLOW_THREADS
			[self
			 dragImage:dragImage
			 at:dragPosition
			 offset:NSZeroSize
			 event:ev
			 pasteboard:pboard
			 source:self
			 slideBack:NO];
			Py_END_ALLOW_THREADS
			
			res = true;
		}
		Py_XDECREF(filename);
	}
	Py_XDECREF(subjectObj);
	Py_XDECREF(control);
	PyGILState_Release(gstate);
{'fraction_non_alphanumeric': 0.1274812527569475, 'fraction_numerical': 0.0055874136156447585, 'mean_word_length': 5.292321924144311, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2816558', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2446, 'n_words': 459}