<commit_before>/**
 * Appcelerator Titanium - licensed under the Apache Public License 2
 * see LICENSE in the root folder for details on the license.
 * Copyright (c) 2008 Appcelerator, Inc. All Rights Reserved.
 */

#include "win32_user_window.h"
#include "webkit_frame_load_delegate.h"
#include "webkit_ui_delegate.h"
#include "win32_tray_item.h"
#include "string_util.h"
#include "../url/app_url.h"
#include <math.h>
#include <shellapi.h>

#define STUB() printf("Method is still a stub, %s:%i\n", __FILE__, __LINE__)
#define SetFlag(x,flag,b) ((b) ? x |= flag : x &= ~flag)
#define UnsetFlag(x,flag) (x &= ~flag)=

using namespace ti;

bool Win32UserWindow::ole_initialized = false;

static void* SetWindowUserData(HWND hwnd, void* user_data) {
	return
		reinterpret_cast<void*>(SetWindowLongPtr(hwnd, GWLP_USERDATA,
			reinterpret_cast<LONG_PTR>(user_data)));
}

static void* GetWindowUserData(HWND hwnd) {
	return reinterpret_cast<void*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
}

/*static*/
Win32UserWindow* Win32UserWindow::FromWindow(HWND hWnd)
{
	return reinterpret_cast<Win32UserWindow*>(GetWindowUserData(hWnd));
}


const TCHAR *windowClassName = "Win32UserWindow";

/*static*/
void Win32UserWindow::RegisterWindowClass (HINSTANCE hInstance)
{
	static bool class_initialized = false;
	if (!class_initialized) {
		//LoadString(hInstance, IDC_TIUSERWINDOW, windowClassName, 100);

		WNDCLASSEX wcex;
		wcex.cbSize = sizeof(WNDCLASSEX);

		wcex.style			= CS_HREDRAW | CS_VREDRAW;
		wcex.lpfnWndProc	= Win32UserWindow::WndProc;
		wcex.cbClsExtra		= 0;
		wcex.cbWndExtra		= 0;
		wcex.hInstance		= hInstance;
		wcex.hIcon			= 0;
		wcex.hIconSm		= 0;
		wcex.hCursor		= LoadCursor(hInstance, IDC_ARROW);
		wcex.hbrBackground	= (HBRUSH)(COLOR_BACKGROUND+1);
		wcex.lpszMenuName	= "";
		wcex.lpszClassName	= windowClassName;

		ATOM result = RegisterClassEx(&wcex);
		if (result == NULL) {
			std::cout << "Error Registering Window Class: " << GetLastError() << std::endl;
		}

		class_initialized = true;
	}
}

void Win32UserWindow::AddMessageHandler(const ValueList& args, SharedValue result)
{
	if (args.size() < 2 || !args.at(0)->IsNumber() || !args.at(1)->IsMethod())
		return;

	long messageCode = (long)args.at(0)->ToDouble();
	SharedBoundMethod callback = args.at(1)->ToMethod();

	messageHandlers[messageCode] = callback;
}

/*static*/
LRESULT CALLBACK
Win32UserWindow::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	Win32UserWindow *window = Win32UserWindow::FromWindow(hWnd);

	if (window && window->messageHandlers.find(message) != window->messageHandlers.end())
	{
		SharedBoundMethod handler = window->messageHandlers[message];
		ValueList args;
		handler->Call(args);

		return 0;
	}

	switch (message)
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			window->Close();
			break;
		case WM_SIZE:
			if (!window->web_view) break;
			window->ResizeSubViews();
			break;
		case TI_TRAY_CLICKED:
			{
				UINT uMouseMsg = (UINT) lParam;<commit_msg>fix runtime error in win32<commit_after>/**
 * Appcelerator Titanium - licensed under the Apache Public License 2
 * see LICENSE in the root folder for details on the license.
 * Copyright (c) 2008 Appcelerator, Inc. All Rights Reserved.
 */

#include "win32_user_window.h"
#include "webkit_frame_load_delegate.h"
#include "webkit_ui_delegate.h"
#include "win32_tray_item.h"
#include "string_util.h"
#include "../url/app_url.h"
#include <math.h>
#include <shellapi.h>

#define STUB() printf("Method is still a stub, %s:%i\n", __FILE__, __LINE__)
#define SetFlag(x,flag,b) ((b) ? x |= flag : x &= ~flag)
#define UnsetFlag(x,flag) (x &= ~flag)=

using namespace ti;

bool Win32UserWindow::ole_initialized = false;

static void* SetWindowUserData(HWND hwnd, void* user_data) {
	return
		reinterpret_cast<void*>(SetWindowLongPtr(hwnd, GWLP_USERDATA,
			reinterpret_cast<LONG_PTR>(user_data)));
}

static void* GetWindowUserData(HWND hwnd) {
	return reinterpret_cast<void*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
}

/*static*/
Win32UserWindow* Win32UserWindow::FromWindow(HWND hWnd)
{
	return reinterpret_cast<Win32UserWindow*>(GetWindowUserData(hWnd));
}


const TCHAR *windowClassName = "Win32UserWindow";

/*static*/
void Win32UserWindow::RegisterWindowClass (HINSTANCE hInstance)
{
	static bool class_initialized = false;
	if (!class_initialized) {
		//LoadString(hInstance, IDC_TIUSERWINDOW, windowClassName, 100);

		WNDCLASSEX wcex;
		wcex.cbSize = sizeof(WNDCLASSEX);

		wcex.style			= CS_HREDRAW | CS_VREDRAW;
		wcex.lpfnWndProc	= Win32UserWindow::WndProc;
		wcex.cbClsExtra		= 0;
		wcex.cbWndExtra		= 0;
		wcex.hInstance		= hInstance;
		wcex.hIcon			= 0;
		wcex.hIconSm		= 0;
		wcex.hCursor		= LoadCursor(hInstance, IDC_ARROW);
		wcex.hbrBackground	= (HBRUSH)(COLOR_BACKGROUND+1);
		wcex.lpszMenuName	= "";
		wcex.lpszClassName	= windowClassName;

		ATOM result = RegisterClassEx(&wcex);
		if (result == NULL) {
			std::cout << "Error Registering Window Class: " << GetLastError() << std::endl;
		}

		class_initialized = true;
	}
}

void Win32UserWindow::AddMessageHandler(const ValueList& args, SharedValue result)
{
	if (args.size() < 2 || !args.at(0)->IsNumber() || !args.at(1)->IsMethod())
		return;

	long messageCode = (long)args.at(0)->ToDouble();
	SharedBoundMethod callback = args.at(1)->ToMethod();

	messageHandlers[messageCode] = callback;
}

/*static*/
LRESULT CALLBACK
Win32UserWindow::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	Win32UserWindow *window = Win32UserWindow::FromWindow(hWnd);

	if (window && (window->messageHandlers.size() > 0) && (window->messageHandlers.find(message) != window->messageHandlers.end()))
	{
		SharedBoundMethod handler = window->messageHandlers[message];
		ValueList args;
		handler->Call(args);

		return 0;
	}

	switch (message)
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			window->Close();
			break;
		case WM_SIZE:
			if (!window->web_view) break;
			window->ResizeSubViews();
			break;
		case TI_TRAY_CLICKED:
			{
				UINT uMouseMsg = (UINT) lParam;
{'fraction_non_alphanumeric': 0.1367038084151006, 'fraction_numerical': 0.015799101945784134, 'mean_word_length': 5.232124352331606, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 39, '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': '356812', 'n_tokens_mistral': 2402, 'n_tokens_neox': 2196, 'n_words': 449}