Example #1
0
std::string FileEncoding::convertToUTF8(const std::string& raw_bytes, long enc_hint)
{
	DWORD cp = investigate(raw_bytes);

	codePage_ = CP_UTF8;
	switch(cp)
	{
		case CP_UTF8:
		{
			break;
		}
		case CP_WINUNICODE:
		{
			codePage_ = CP_WINUNICODE;
			break;
		}
		case -1:
		{
			codePage_ = enc_hint;
			break;
		}
		default:
		{
			if ( enc_hint == CP_UTF8 )
				codePage_ = CP_UTF8;
			else
				codePage_ = cp;
		}
	}

	if ( isBinary() )
	{
		return "";
	}

	switch ( codePage_ )
	{
		case CP_WINUNICODE :
		{
			// jump over BOM
			std::string s2(raw_bytes);
			if ( raw_bytes.substr(0,2) == std::string((char*)mol::FileEncoding::UTF16LE_BOM,2) )
				s2 = std::string(raw_bytes.data()+2,raw_bytes.size()-2);

			// it really is a UCS-2 string, so cast to wchar_t (WIN32)
			std::wstring ws((wchar_t*)(s2.data()),s2.size()/sizeof(wchar_t));

			// now convert UTF16-LE to UTF-8
			std::string u = mol::toUTF8(ws);
			return mol::unix2dos(u);
		}
		case CP_UTF8 :
		{
			std::string s2;
			if ( raw_bytes.substr(0,3) == std::string((char*)mol::FileEncoding::UTF8_BOM,3) )
				s2 = std::string(raw_bytes.c_str()+3,raw_bytes.size()-3);
			else
				s2 = raw_bytes;

			s2 = mol::unix2dos(s2);
			return s2;
		}
		default:
		{
			std::string s2(mol::toUTF8(raw_bytes,codePage_));
			s2 = mol::unix2dos(s2);
			return s2;
		}
	}

	return "";
}
Example #2
0
static LRESULT CALLBACK
TS_WindowProcedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg) {
	default:
		break;


	/* ======== TSM_XXX ======== */

	/* -------- TSM_XXX (public) -------- */

	case TSM_ACTIVATE:
		if (g_Activeness)
			return TRUE;

		g_Activeness = Engine_Activate(g_Engine);
		return g_Activeness;

	case TSM_INACTIVATE:
		if (!g_Activeness)
			return TRUE;

		if (g_Selecting)
			SendMessage(hwnd, TSM_CANCEL, 0, 0);
		g_Activeness = FALSE;
		return Engine_Inactivate(g_Engine);

	case TSM_NEXT:
		DEBUG_PRINTF((T("TSM_NEXT")));
		if (!g_Activeness)
			return FALSE;

		if (!g_Selecting) {
			if (!SendMessage(hwnd, TSM_STARTSEL, 0, 0))
				return FALSE;
		}

		g_TaskList->selected_task_index++;
		while (g_TaskList->count <= g_TaskList->selected_task_index)
			g_TaskList->selected_task_index -= g_TaskList->count;
		SendMessage(hwnd, TSM_REDRAW, 0, 0);
		return TRUE;

	case TSM_PREV:
		DEBUG_PRINTF((T("TSM_PREV")));
		if (!g_Activeness)
			return FALSE;

		if (!g_Selecting) {
			if (!SendMessage(hwnd, TSM_STARTSEL, 0, 0))
				return FALSE;
		}

		g_TaskList->selected_task_index--;
		while (g_TaskList->selected_task_index < 0)
			g_TaskList->selected_task_index += g_TaskList->count;
		SendMessage(hwnd, TSM_REDRAW, 0, 0);
		return TRUE;

	case TSM_SWITCH:
		DEBUG_PRINTF((T("TSM_SWITCH %c"), (g_Selecting ? 'o' : 'x')));
		if ((!g_Activeness) || (!g_Selecting))
			return FALSE;

		(*g_OnSwitch)(TS_FROM_HWND(hwnd));
		SendMessage(hwnd, TSM_ENDSEL, 0, 0);
		return TRUE;

	case TSM_CANCEL:
		DEBUG_PRINTF((T("TSM_CANCEL %c"), (g_Selecting ? 'o' : 'x')));
		if ((!g_Activeness) || (!g_Selecting))
			return FALSE;

		SendMessage(hwnd, TSM_ENDSEL, 0, 0);
		return TRUE;

	/* -------- TSM_XXX (internal use) -------- */

	case TSM_SHOW: ShowWindow(hwnd, SW_SHOW); return TRUE;
	case TSM_HIDE: ShowWindow(hwnd, SW_HIDE); return TRUE;
	case TSM_REDRAW: InvalidateRect(hwnd, NULL, TRUE); return TRUE;

	/* FIXME: Reexamine the order of processes. */
	case TSM_STARTSEL:
		DEBUG_PRINTF((T("TSM_STARTSEL %c"), (g_Selecting?'o':'x')));

		if (g_Selecting)
			return FALSE;

		g_TaskList = EnumerateTasks(g_SieveProcedure);
		if (g_TaskList == NULL) {
			FAILURE(T("TSM_STARTSEL/EnumerateTasks"));
			return FALSE;
		}

		/**
		 * Deny to select tasks when there is no task,
		 * because it causes infinite loop.
		 */
		if (g_TaskList->count < 1) {
			TSTaskList_Del(g_TaskList);
			g_TaskList = NULL;
			return FALSE;
		}

		g_Selecting = TRUE;

		if (SHOW_HEAVY_THINGS) {
			int i;

			for (i = 0; i < g_TaskList->count; i++) {
#if USE_INVESTIGATE
				investigate(g_TaskList->list[i]->window);
#else
				DEBUG_PRINTF((
				  T("%s %2d %s"),
				  ( i == g_TaskList->selected_task_index
				    ? T("(*)") : T("   ") ),
				  i,
				  g_TaskList->list[i]->title
				));
#endif
			}
		}

		g_TaskList->selected_task_index
		  = get_current_task_index( g_TaskList,
		                            get_last_active_window() );
		(*g_OnStartSelecting)(TS_FROM_HWND(hwnd));
		if (g_Engine != TSEngine_NONE) {
			/* [TSEngine_HOOK_PROBLEM] */
			if (!SetForegroundWindow(hwnd))
				FAILURE(T("SetForegroundWindow"));
		}

		/**
		 * Switch automatically when there is only one task
		 * like the default one.
		 */
		if (g_TaskList->count == 1) {
			SendMessage(hwnd, TSM_SWITCH, 0, 0);
			return FALSE;
		}

		/* See [TSEngine_SIMPLE_PROBLEM]. */
		if ((g_Engine == TSEngine_SIMPLE) && !is_pressed_now(VK_MENU))
		{
			DEBUG_PRINTF((T("Missing U-Alt")));
			SendMessage(hwnd, TSM_SWITCH, 0, 0);
			return FALSE;
		}

		SendMessage(hwnd, TSM_SHOW, 0, 0);
		return TRUE;

	case TSM_ENDSEL:
		DEBUG_PRINTF((T("TSM_ENDSEL %c"), (g_Selecting ? 'o' : 'x')));

		if (!g_Selecting)
			return FALSE;

		TSTaskList_Del(g_TaskList);
		g_TaskList = NULL;
		g_Selecting = FALSE;
		SendMessage(hwnd, TSM_HIDE, 0, 0);
		return TRUE;


	/* ======== WM_XXX ======== */

	/* -------- WM_XXX (to check key operations) -------- */

	case WM_HOTKEY:
		switch (wp) {
		default:
			break;

		case IDHK_NEXT:
			SendMessage(hwnd, TSM_NEXT, 0, 0);
			break;

		case IDHK_PREV:
			SendMessage(hwnd, TSM_PREV, 0, 0);
			break;
		}
		break;

	case WM_ACTIVATE:  /* Inactivated -- cancel */
		if (wp == WA_INACTIVE) {
			if ((g_Engine != TSEngine_NONE) && g_Selecting) {
				DEBUG_PRINTF((T("WM_ACTIVATE (WA_INACTIVE)")));
				SendMessage(hwnd, TSM_CANCEL, 0, 0);
			}
		}
		break;

	case WM_KILLFOCUS:  /* Lost focus -- cancel */
		if ((g_Engine != TSEngine_NONE) && g_Selecting) {
			DEBUG_PRINTF((T("WM_KILLFOCUS")));
			SendMessage(hwnd, TSM_CANCEL, 0, 0);
		}
		break;

	case WM_KEYDOWN:  /* A-NonAltNonShiftNonTab -- cancel */
		DEBUG_PRINTF((T("WM_KEYDOWN (%02X)"), wp));
		if ( (g_Engine == TSEngine_SIMPLE)
		     && g_Selecting
		     && (wp != g_HotKey)
		     && (!is_alt_vk(wp)) && (!is_shift_vk(wp)) )
		{
			SendMessage(hwnd, TSM_CANCEL, 0, 0);
		}
		break;

	case WM_SYSKEYDOWN:  /* A-NonAltNonShiftNonTab -- cancel */
		DEBUG_PRINTF((T("WM_SYSKEYDOWN (%02X)"), wp));
		if ( (g_Engine == TSEngine_SIMPLE)
		     && g_Selecting
		     && (wp != g_HotKey)
		     && (!is_alt_vk(wp)) && (!is_shift_vk(wp)) )
		{
			SendMessage(hwnd, TSM_CANCEL, 0, 0);
		}
		break;

	case WM_KEYUP:  /* U-Alt -- switch */
		DEBUG_PRINTF((T("WM_KEYUP (%02X)"), wp));
		if ( (g_Engine == TSEngine_SIMPLE)
		     && g_Selecting
		     && is_alt_vk(wp) )
		{
			SendMessage(hwnd, TSM_SWITCH, 0, 0);
		}
		break;

	case WM_SYSKEYUP: /* U-Alt -- switch */
		DEBUG_PRINTF((T("WM_SYSKEYUP (%02X)"), wp));
		if ( (g_Engine == TSEngine_SIMPLE)
		     && g_Selecting
		     && is_alt_vk(wp) )
		{
			SendMessage(hwnd, TSM_SWITCH, 0, 0);
		}
		break;

	/* -------- WM_XXX (Misc.) -------- */

	case WM_CREATE: {
		BOOL r;

		r = ( SetProp(hwnd, TSKey_OnStartSelecting,&g_OnStartSelecting)
		      && SetProp(hwnd, TSKey_OnPaint, &g_OnPaint)
		      && SetProp(hwnd, TSKey_OnCancel, &g_OnCancel)
		      && SetProp(hwnd, TSKey_OnSwitch, &g_OnSwitch)
		      && SetProp(hwnd, TSKey_SieveProcedure, &g_SieveProcedure)
		      && SetProp(hwnd, TSKey_Window, &g_TSWindow)
		      && SetProp(hwnd, TSKey_Activeness, &g_Activeness)
		      && SetProp(hwnd, TSKey_Selecting, &g_Selecting)
		      && SetProp(hwnd, TSKey_TaskList, &g_TaskList)
		      && SetProp(hwnd, TSKey_HotKey, &g_HotKey)
		      && SetProp(hwnd, TSKey_Engine, &g_Engine)
		      && SetProp(hwnd, TSKey_DebugWindow, &g_DebugWindow) );
		if (!r)
			return 1;
		g_TSWindow = hwnd;
		} break;

	case WM_DESTROY:
		g_TSWindow = NULL;
		if (g_Activeness)
			TS_Inactivate(TS_FROM_HWND(hwnd));
		TS_UseDebugWindow(TS_FROM_HWND(hwnd), FALSE);
		RemoveAllProps(hwnd);
		break;

	case WM_PAINT:
		(*g_OnPaint)(TS_FROM_HWND(hwnd));
		break;
	}

	return DefWindowProc(hwnd, msg, wp, lp);
}
Example #3
0
int  FileEncoding::investigate(const std::string& c)
{
	return investigate(c,"");
}