DesktopManager::DesktopManager(int width, int height)
{
   Settings settings;

   m_currentDesktop = NULL;

   //Load the number of columns
   m_nbColumn = settings.LoadSetting(Settings::ColumnNumber);

   //Get the size
   m_width = width;
   m_height = height;

   //Initialize the display mode
   m_bkDisplayMode = NULL;
   m_displayMode = (DisplayMode)-1;
   SetDisplayMode((DisplayMode)settings.LoadSetting(Settings::DisplayMode));

   settings.LoadSetting(Settings::PreviewWindowFont, &m_lfPreviewWindowFontInfo);
   m_hPreviewWindowFont = CreateFontIndirect(&m_lfPreviewWindowFontInfo);
   m_crPreviewWindowFontColor = settings.LoadSetting(Settings::PreviewWindowFontColor);

   //Load the desktops
   LoadDesktops();

   //Initialize the OSD
   m_osd.Create();
   m_useOSD = settings.LoadSetting(Settings::DesktopNameOSD);

   vdWindow.SetMessageHandler(WM_VD_SWITCHDESKTOP, this, &DesktopManager::OnCmdSwitchDesktop);
   vdWindow.SetMessageHandler(WM_SETTINGCHANGE, this, &DesktopManager::OnSettingsChange);
}
TrayIcon::TrayIcon(HWND hWnd): m_hWnd(hWnd), m_iconLoaded(false)
{
   Settings settings;

   if (settings.LoadSetting(Settings::HasTrayIcon))
      AddIcon();

   m_closeToTray = settings.LoadSetting(Settings::CloseToTray);
   vdWindow.SetSysCommandHandler(SC_CLOSE, this, &TrayIcon::OnCmdClose);
}
ToolTip::ToolTip(HWND hWnd): m_hOwnerWnd(hWnd)
{
   Settings settings;

   //Create the control
   m_hWnd = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
                           WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,		
                           CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                           hWnd, NULL, vdWindow, NULL);

   //Load from the registry to find out whether to enable tooltips or not
   EnableTooltips(settings.LoadSetting(Settings::EnableToolTips));
}
OnScreenDisplayWnd::OnScreenDisplayWnd(): m_transp(NULL)
{
   Settings settings;

   settings.LoadSetting(Settings::OSDFont, &m_lf);
   m_font = CreateFontIndirect(&m_lf);

   SetDefaultTimeout(settings.LoadSetting(Settings::OSDTimeout));
   m_fgColor = settings.LoadSetting(Settings::OSDFgColor);
   m_bgColor = settings.LoadSetting(Settings::OSDBgColor);
   settings.LoadSetting(Settings::OSDPosition, &m_position);
   m_transpLevel = settings.LoadSetting(Settings::OSDTransparencyLevel);
   m_hasBackground = settings.LoadSetting(Settings::OSDHasBackground);
   m_isTransparent = settings.LoadSetting(Settings::OSDIsTransparent);

   m_bgBrush = m_hasBackground ? CreateSolidBrush(m_bgColor) : (HBRUSH)GetStockObject(HOLLOW_BRUSH);
}
bool VirtualDimension::Start(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	RECT pos;
	Settings settings;
	HWND hwndPrev;
	DWORD dwStyle;

	// If a previous instance is running, activate
	// that instance and terminate this one.
	hwndPrev = FindWindow();
	if (hwndPrev != NULL)
	{
		  SetForegroundWindow (hwndPrev);
		  return false;
	}

	m_hInstance = hInstance;

	InitHotkeyControl();
	InitHyperLinkControl();

	// Register the window class
	RegisterClass();

	// Bind the message handlers
	SetCommandHandler(IDM_ABOUT, this, &VirtualDimension::OnCmdAbout);
	SetSysCommandHandler(IDM_ABOUT, this, &VirtualDimension::OnCmdAbout);
	SetCommandHandler(IDM_CONFIGURE, this, &VirtualDimension::OnCmdConfigure);
	SetSysCommandHandler(IDM_CONFIGURE, this, &VirtualDimension::OnCmdConfigure);
	SetCommandHandler(IDM_EXIT, this, &VirtualDimension::OnCmdExit);
	SetSysCommandHandler(SC_CLOSE, this, &VirtualDimension::OnCmdExit);
	SetCommandHandler(IDM_LOCKPREVIEWWND, this, &VirtualDimension::OnCmdLockPreviewWindow);
	SetSysCommandHandler(IDM_LOCKPREVIEWWND, this, &VirtualDimension::OnCmdLockPreviewWindow);
	SetCommandHandler(IDM_SHOWCAPTION, this, &VirtualDimension::OnCmdShowCaption);
	SetSysCommandHandler(IDM_SHOWCAPTION, this, &VirtualDimension::OnCmdShowCaption);

	SetMessageHandler(WM_DESTROY, this, &VirtualDimension::OnDestroy);
	SetMessageHandler(WM_ENDSESSION, this, &VirtualDimension::OnEndSession);
	SetMessageHandler(WM_MOVE, this, &VirtualDimension::OnMove);
	SetMessageHandler(WM_WINDOWPOSCHANGING, this, &VirtualDimension::OnWindowPosChanging);
	SetMessageHandler(WM_DISPLAYCHANGE, this, &VirtualDimension::OnDisplayChange);
	SetMessageHandler(WM_SHOWWINDOW, this, &VirtualDimension::OnShowWindow);

	SetMessageHandler(WM_LBUTTONDOWN, this, &VirtualDimension::OnLeftButtonDown);
	SetMessageHandler(WM_LBUTTONUP, this, &VirtualDimension::OnLeftButtonUp);
	SetMessageHandler(WM_LBUTTONDBLCLK, this, &VirtualDimension::OnLeftButtonDblClk);
	SetMessageHandler(WM_RBUTTONDOWN, this, &VirtualDimension::OnRightButtonDown);

	SetMessageHandler(WM_MEASUREITEM, this, &VirtualDimension::OnMeasureItem);
	SetMessageHandler(WM_DRAWITEM, this, &VirtualDimension::OnDrawItem);

	m_autoHideTimerId = CreateTimer(this, &VirtualDimension::OnTimer);
	SetMessageHandler(WM_ACTIVATEAPP, this, &VirtualDimension::OnActivateApp);

	SetMessageHandler(WM_MOUSEHOVER, this, &VirtualDimension::OnMouseHover);
	SetMessageHandler(WM_MOUSELEAVE, this, &VirtualDimension::OnMouseLeave);
	SetMessageHandler(WM_NCHITTEST, this, &VirtualDimension::OnNCHitTest);

	SetMessageHandler(WM_VD_HOOK_MENU_COMMAND, this, &VirtualDimension::OnHookMenuCommand);
	SetMessageHandler(WM_VD_PREPARE_HOOK_MENU, this, &VirtualDimension::OnPrepareHookMenu);
	SetMessageHandler(WM_VD_CHECK_MIN_TO_TRAY, this, &VirtualDimension::OnCheckMinToTray);

	// compare the window's style
	m_hasCaption = settings.LoadSetting(Settings::HasCaption);
	dwStyle = WS_POPUP | WS_SYSMENU | (m_hasCaption ? WS_CAPTION : WS_DLGFRAME);

	// Reload the window's position
	settings.LoadSetting(Settings::WindowPosition, &pos);
	AdjustWindowRectEx(&pos, dwStyle, FALSE, WS_EX_TOOLWINDOW);

	// Dock the window to the screen borders
	m_dockedBorders = settings.LoadSetting(Settings::DockedBorders);
	DockWindow(pos);

	// Create the main window
	Create( WS_EX_TOOLWINDOW, m_szWindowClass, m_szTitle, dwStyle,
			pos.left, pos.top, pos.right - pos.left, pos.bottom - pos.top,
			NULL, NULL, hInstance);
	if (!IsValid())
		return false;

	hWnd = *this;

	// Load some settings
	m_snapSize = settings.LoadSetting(Settings::SnapSize);
	m_autoHideDelay = settings.LoadSetting(Settings::AutoHideDelay);
	m_shrinked = false;

	m_tracking = false;

	//Ensure the window gets docked if it is close enough to the borders
	SetWindowPos(hWnd, NULL, pos.left, pos.top, pos.right - pos.left, pos.bottom - pos.top, SWP_NOACTIVATE|SWP_NOZORDER|SWP_NOOWNERZORDER);

	// Setup the system menu
	m_pSysMenu = GetSystemMenu(hWnd, FALSE);
	if (m_pSysMenu != NULL)
	{
		RemoveMenu(m_pSysMenu, SC_RESTORE, MF_BYCOMMAND);
		RemoveMenu(m_pSysMenu, SC_MINIMIZE, MF_BYCOMMAND);
		RemoveMenu(m_pSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
		RemoveMenu(m_pSysMenu, SC_MOVE, MF_BYCOMMAND);
		RemoveMenu(m_pSysMenu, SC_SIZE, MF_BYCOMMAND);
		RemoveMenu(m_pSysMenu, 0, MF_BYCOMMAND);

		AppendMenu(m_pSysMenu, MF_SEPARATOR, 0, NULL);
		AppendMenu(m_pSysMenu, MF_STRING, IDM_CONFIGURE, Locale::GetInstance().GetString(IDS_CONFIGURE)); //"C&onfigure"
		AppendMenu(m_pSysMenu, MF_STRING, IDM_LOCKPREVIEWWND, Locale::GetInstance().GetString(IDS_LOCKPREVIEWWND)); //"&Lock the window"
		AppendMenu(m_pSysMenu, MF_STRING, IDM_SHOWCAPTION, Locale::GetInstance().GetString(IDS_SHOWCAPTION)); //"S&how the caption"

		if (CreateLangMenu())
			AppendMenu(m_pSysMenu, MF_STRING|MF_POPUP, (UINT_PTR)m_pLangMenu, Locale::GetInstance().GetString(IDS_LANGUAGEMENU)); //"L&anguage"
		AppendMenu(m_pSysMenu, MF_STRING, IDM_ABOUT, Locale::GetInstance().GetString(IDS_ABOUT)); //"&About"
		CheckMenuItem(m_pSysMenu, IDM_SHOWCAPTION, m_hasCaption ? MF_CHECKED : MF_UNCHECKED );
	}

	// Lock the preview window as appropriate
	LockPreviewWindow(settings.LoadSetting(Settings::LockPreviewWindow));

	// Bind to explorer
	explorerWrapper = new ExplorerWrapper(this);

	// Initialize the tray icon manager
	trayManager = new TrayIconsManager();

	// Initialize tray icon
	trayIcon = new TrayIcon(hWnd);

	// Initialize transparency (set value two times, to make a fade-in)
	transp = new Transparency(hWnd);
	transp->SetTransparencyLevel(0);
	transp->SetTransparencyLevel(settings.LoadSetting(Settings::TransparencyLevel), true);

	// Initialize always on top state
	ontop = new AlwaysOnTop(hWnd);
	ontop->SetAlwaysOnTop(settings.LoadSetting(Settings::AlwaysOnTop));

	// Create the tooltip
	tooltip = new ToolTip(hWnd);

	// Create mouse warp
	mousewarp = new MouseWarp();

	// Create the windows manager
	winMan = new WindowsManager;

	// Create the desk manager
	settings.LoadSetting(Settings::WindowPosition, &pos);	//use client position
	deskMan = new DesktopManager(pos.right - pos.left, pos.bottom - pos.top);

	// Retrieve the initial list of windows
	winMan->PopulateInitialWindowsSet();

	//Update tray icon tooltip
	trayIcon->Update();

	//Bind some additional message handlers (which need the desktop manager)
	SetMessageHandler(WM_SIZE, this, &VirtualDimension::OnSize);
	SetMessageHandler(WM_PAINT, deskMan, &DesktopManager::OnPaint);

	// Show window if needed
	if (m_isWndVisible = (settings.LoadSetting(Settings::ShowWindow) || !trayIcon->HasIcon()))
	{
		ShowWindow(hWnd, nCmdShow);
		Refresh();
	}

	return true;
}