/*
 ============================================================================
 xoblite -> an alternative shell based on Blackbox for Windows
 Copyright  2002-2005 Karl-Henrik Henriksson [qwilk]
 Copyright  2001-2004 The Blackbox for Windows Development Team
 http://xoblite.net/ - #bb4win on irc.freenode.net
 ============================================================================

  Blackbox for Windows is free software, released under the
  GNU General Public License (GPL version 2 or later), with an extension
  that allows linking of proprietary modules under a controlled interface.
  What this means is that plugins etc. are allowed to be released
  under any license the author wishes. Please note, however, that the
  original Blackbox gradient math code used in Blackbox for Windows
  is available under the BSD license.

  http://www.fsf.org/licenses/gpl.html
  http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface
  http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  For additional license information, please read the included license.html

 ============================================================================
*/

#include "Toolbar.h" 

const char szToolbarName[] = "BBToolbar"; // Window class etc.

extern BImage *pBImage;
extern Toolbar *pToolbar;
extern Systembar *pSystembar;
extern Settings *pSettings;
extern Workspaces *pWorkspaces;
extern MenuMaker *pMenuMaker;

//===========================================================================

Toolbar::Toolbar(HINSTANCE hInstance)
{
        WNDCLASS wc;
        hToolbarInstance = hInstance;
        hBlackboxWnd = GetBBWnd();

        cachedBackground = CreateCompatibleDC(NULL);
        cachedToolbarButton = CreateCompatibleDC(NULL);
        cachedToolbarButtonPressed = CreateCompatibleDC(NULL);
        cachedBitmapsExist = false;

        cachedToolbarWindowLabel = CreateCompatibleDC(NULL);
        SetRectEmpty(&currentWindowLabelRect);

        ButtonPressed1 = ButtonPressed2 = ButtonPressed3 = ButtonPressed4 = false;
        FirstUpdate = true;
        ShowingExternalLabel = false;
        saveWidthOnRestart = false;
        ToolbarPosInProgress = false;

        //====================

        GetSettings();

        //====================

        // Register our window class...
        ZeroMemory(&wc,sizeof(wc));
        wc.lpfnWndProc = ToolbarWndProc;                                                // our window procedure
        wc.hInstance = hToolbarInstance;                
        wc.lpszClassName = szToolbarName;                                               // our window class name

        if (!RegisterClass(&wc))
        {
                MessageBox(0, "Error registering window class", szToolbarName, MB_OK | MB_ICONERROR | MB_TOPMOST);
                Log("Toolbar: Error registering window class", NULL);
                return;
        }

        hToolbarWnd = CreateWindowEx(
                WS_EX_TOOLWINDOW,                                                                       // exstyles 
                szToolbarName,                                                                          // our window class name
                NULL,                                                                                           // use description for a window title
                WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
                ToolbarX, ToolbarY,                                                                     // position
                ToolbarWidth, ToolbarHeight,                                            // width & height of window
                NULL,                                                                                           // parent window
                NULL,                                                                                           // no menu
                hToolbarInstance,                                                                       // hInstance of DLL
                NULL);

        if (!hToolbarWnd)
        {                                                  
                UnregisterClass(szToolbarName, hToolbarInstance); // unregister window class
                MessageBox(0, "Error creating window", szToolbarName, MB_OK | MB_ICONERROR | MB_TOPMOST);
                Log("Toolbar: Error creating window", NULL);
                return;
        }

        //====================

        int msgs[] = { BB_DESKTOPINFO, BB_RECONFIGURE, BB_TOGGLETOOLBAR, BB_SETTOOLBARLABEL, 0 };
        SendMessage(hBlackboxWnd, BB_REGISTERMESSAGE, (WPARAM)hToolbarWnd, (LPARAM)msgs);

        // Apply transparency depending on extensions.rc setting...
        SetTransparency(hToolbarWnd, pSettings->toolbarTransparencyAlpha);
        // Make the toolbar window sticky...
        MakeSticky(hToolbarWnd);        
        // Set window to accept doubleclicks...
        SetClassLongPtr(hToolbarWnd, GCL_STYLE, CS_DBLCLKS | GetClassLongPtr(hToolbarWnd, GCL_STYLE));
        // Set window to accept drag'n'drop...
        DragAcceptFiles(hToolbarWnd, true);

        ShowWindow(hToolbarWnd, pSettings->toolbarHidden ? SW_HIDE : SW_SHOWNOACTIVATE);
        if (pSettings->toolbarOnTop) SetWindowPos(hToolbarWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);

        // Save original DesktopArea...
        SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&origDesktopArea, SPIF_SENDCHANGE);
        // Update DesktopArea (depending on blackbox.rc fullMaximization setting)...
        UpdateDesktopArea();

        int UpdateInterval = 250;
        if (!SetTimer(hToolbarWnd, TOOLBAR_UPDATE_TIMER, UpdateInterval, (TIMERPROC)NULL))
        {
                MessageBox(0, "Error creating update timer", szToolbarName, MB_OK | MB_ICONERROR | MB_TOPMOST);
                Log("Could not create Toolbar update timer", NULL);
                return;
        }

        //====================

        // Autohide?
        ToolbarAutoHiding = ToolbarAutoShowing = false;
        ToolbarAutoHidden = false;
        ToolbarAutoShown = true;
        if (pSettings->toolbarAutoHide && !CheckIfMouseHover()) AutoHide();
}

//===========================================================================

Toolbar::~Toolbar()
{
        // Kill timers...
        KillTimer(hToolbarWnd, TOOLBAR_UPDATE_TIMER);
        if (ToolbarAutoHiding || ToolbarAutoShowing) KillTimer(hToolbarWnd, TOOLBAR_AUTOHIDE_TIMER);
        
        if (ShowingExternalLabel) KillTimer(hToolbarWnd, TOOLBAR_SETLABEL_TIMER);

        // Restore original DesktopArea...
        SystemParametersInfo(SPI_SETWORKAREA, 0, (PVOID)&origDesktopArea, SPIF_SENDCHANGE);

        int msgs[] = { BB_DESKTOPINFO, BB_RECONFIGURE, BB_TOGGLETOOLBAR, BB_SETTOOLBARLABEL, 0 };
        SendMessage(hBlackboxWnd, BB_UNREGISTERMESSAGE, (WPARAM)hToolbarWnd, (LPARAM)msgs);
        DestroyWindow(hToolbarWnd); // delete our window
        UnregisterClass(szToolbarName, hToolbarInstance); // unregister window class

        // Delete cached bitmaps...
        DeleteDC(cachedToolbarWindowLabel);
        DeleteDC(cachedToolbarButtonPressed);
        DeleteDC(cachedToolbarButton);
        DeleteDC(cachedBackground);
}

//===========================================================================

LRESULT CALLBACK ToolbarWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        switch (message)
        {
                //====================

                case WM_PAINT:
                {
                        PAINTSTRUCT ps;
                        HDC hdc = BeginPaint(hwnd, &ps);
                        HDC buf = CreateCompatibleDC(NULL);
                        RECT r;

                        int updateRectWidth = ps.rcPaint.right - ps.rcPaint.left;
                        int updateRectHeight = ps.rcPaint.bottom - ps.rcPaint.top;

                        HBITMAP bufbmp = CreateCompatibleBitmap(hdc, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight);
                        HBITMAP oldbuf = (HBITMAP)SelectObject(buf, bufbmp);

                        //====================

                        int tbLabelW;
                        int tbClockW;

                        // Calculate segment widths...
                        SIZE size;
                        HDC fonthdc = CreateDC("DISPLAY", NULL, NULL, NULL);
                        HFONT tempfont = CreateFont(pSettings->ToolbarLabel->FontHeight, 0, 0, 0, pSettings->ToolbarLabel->FontWeight, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, pSettings->ToolbarLabel->Font);
                        HGDIOBJ oldtempfont = SelectObject(fonthdc, tempfont);
                        // Get width of current workspace name...
                        if (pWorkspaces) strcpy(pToolbar->ToolbarWorkspaceName, (LPSTR)pWorkspaces->deskNames[pWorkspaces->currentScreen].c_str());
                        GetTextExtentPoint32(fonthdc, pToolbar->ToolbarWorkspaceName, strlen(pToolbar->ToolbarWorkspaceName), &size);
                        tbLabelW = size.cx + 6 + pSettings->ToolbarLabel->borderWidth;
                        // Get width of clock...
                        DeleteObject(SelectObject(fonthdc, oldtempfont));
                        tempfont = CreateFont(pSettings->ToolbarClock->FontHeight, 0, 0, 0, pSettings->ToolbarClock->FontWeight, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, pSettings->ToolbarClock->Font);
                        oldtempfont = SelectObject(fonthdc, tempfont);
                        GetTextExtentPoint32(fonthdc, pToolbar->CurrentTime, strlen(pToolbar->CurrentTime), &size);
                        tbClockW = size.cx + 12 + pSettings->ToolbarClock->borderWidth;

                        // The widest sets the width!
                        if (tbClockW > tbLabelW) { tbLabelW = tbClockW; }
                        else { tbClockW = tbLabelW; }
                        DeleteObject(SelectObject(fonthdc, oldtempfont));
                        DeleteDC(fonthdc);

                        //====================

                        pToolbar->ButtonWH = pToolbar->ToolbarHeight - (pSettings->Toolbar->borderWidth * 2) - 6;

                        int tbLabelX = pSettings->Toolbar->borderWidth + 1;
                        int tbLabelH = pToolbar->ToolbarHeight - (pSettings->Toolbar->borderWidth * 2) - 2;
                        int tbClockX = pToolbar->ToolbarWidth - tbClockW - 1 - pSettings->Toolbar->borderWidth;
                        int tbWinLabelX = tbLabelW + (2 * pToolbar->ButtonWH) + 10 + pSettings->Toolbar->borderWidth;
                        int tbWinLabelW = pToolbar->ToolbarWidth - tbLabelW - tbClockW - (4 * pToolbar->ButtonWH) - 20 - (pSettings->Toolbar->borderWidth * 2);

                        // Make sure windowlabel width is acceptable, could be < 0 if toolbar width (percentage) is too small!
                        if (tbWinLabelW < 0) tbWinLabelW = 0;

                        pToolbar->ButtonX1 = tbLabelX + tbLabelW + 3;
                        pToolbar->ButtonX2 = pToolbar->ButtonX1 + pToolbar->ButtonWH + 3;
                        pToolbar->ButtonX3 = tbClockX - (2 * pToolbar->ButtonWH) - 6;
                        pToolbar->ButtonX4 = pToolbar->ButtonX3 + pToolbar->ButtonWH + 3;
                        pToolbar->ButtonY = pSettings->Toolbar->borderWidth + 3;

                        // Save the element rect's so we can InvalidateRect for a specific element only if needed...
                        SetRect(&pToolbar->labelRect, tbLabelX, 1 + pSettings->Toolbar->borderWidth, tbLabelX + tbLabelW, 1 + pSettings->Toolbar->borderWidth + tbLabelH);
                        SetRect(&pToolbar->winlabelRect, tbWinLabelX, 1 + pSettings->Toolbar->borderWidth, tbWinLabelX + tbWinLabelW, 1 + pSettings->Toolbar->borderWidth + tbLabelH);
                        SetRect(&pToolbar->clockRect, tbClockX, 1 + pSettings->Toolbar->borderWidth, tbClockX + tbClockW, 1 + pSettings->Toolbar->borderWidth + tbLabelH);

                        //====================

                        // If we have not yet created cached bitmaps, let's do that...
                        if (!pToolbar->cachedBitmapsExist)
                        {
                                HBITMAP tempBitmap, oldBitmap;

                                // Create a temporary bitmap...
                                tempBitmap = CreateCompatibleBitmap(hdc, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight);
                                // Delete the previously cached gradient...
                                oldBitmap = (HBITMAP)SelectObject(pToolbar->cachedBackground, tempBitmap);
                                DeleteObject(oldBitmap);
                                // Paint background + border... (now using MakeGradient instead of CreateBorder + CreateGradientByRect)
                                GetClientRect(hwnd, &r);
                                MakeGradient(pToolbar->cachedBackground, r, pSettings->Toolbar->type, pSettings->Toolbar->Color, pSettings->Toolbar->ColorTo, pSettings->Toolbar->interlaced, pSettings->Toolbar->bevelstyle, pSettings->Toolbar->bevelposition, pSettings->bevelWidth, pSettings->Toolbar->borderColor, pSettings->Toolbar->borderWidth);

                                //====================

                                if (!pSettings->ToolbarLabel->parentRelative)
                                {
                                        // Paint workspaces background...
//                                      pBImage->CreateGradientByRect(buf, pToolbar->labelRect, pSettings->ToolbarLabel->type, pSettings->toolbarLabelColor, pSettings->toolbarLabelColorTo, pSettings->ToolbarLabel->interlaced, pSettings->ToolbarLabel->bevelstyle, pSettings->ToolbarLabel->bevelposition, pSettings->bevelWidth);
                                        // *** bbLean experimental StyleItem members ***
                                        MakeGradient(pToolbar->cachedBackground, pToolbar->labelRect, pSettings->ToolbarLabel->type, pSettings->ToolbarLabel->Color, pSettings->ToolbarLabel->ColorTo, pSettings->ToolbarLabel->interlaced, pSettings->ToolbarLabel->bevelstyle, pSettings->ToolbarLabel->bevelposition, pSettings->bevelWidth, pSettings->ToolbarLabel->borderColor, pSettings->ToolbarLabel->borderWidth);
                                }

                                //====================

                                if (!pSettings->ToolbarClock->parentRelative)
                                {
                                        // Paint clock background...
//                                      pBImage->CreateGradientByRect(buf, pToolbar->clockRect, pSettings->ToolbarClock->type, pSettings->toolbarClockColor, pSettings->toolbarClockColorTo, pSettings->ToolbarClock->interlaced, pSettings->ToolbarClock->bevelstyle, pSettings->ToolbarClock->bevelposition, pSettings->bevelWidth);
                                        // *** bbLean experimental StyleItem members ***
                                        MakeGradient(pToolbar->cachedBackground, pToolbar->clockRect, pSettings->ToolbarClock->type, pSettings->ToolbarClock->Color, pSettings->ToolbarClock->ColorTo, pSettings->ToolbarClock->interlaced, pSettings->ToolbarClock->bevelstyle, pSettings->ToolbarClock->bevelposition, pSettings->bevelWidth, pSettings->ToolbarClock->borderColor, pSettings->ToolbarClock->borderWidth);
                                }

                                //====================

                                // Clean up - delete the temporary bitmap...
                                DeleteObject(tempBitmap);

                                //====================

                                if (!pSettings->ToolbarButton->parentRelative)
                                {
                                        // Create a temporary bitmap...
                                        tempBitmap = CreateCompatibleBitmap(hdc, pToolbar->ButtonWH, pToolbar->ButtonWH);
                                        // Delete the previously cached gradient...
                                        oldBitmap = (HBITMAP)SelectObject(pToolbar->cachedToolbarButton, tempBitmap);
                                        DeleteObject(oldBitmap);
                                        // Paint button background...
                                        r.left = 0; r.top = 0;  r.right = pToolbar->ButtonWH; r.bottom = pToolbar->ButtonWH;
                                        // *** bbLean experimental StyleItem members ***
                                        MakeGradient(pToolbar->cachedToolbarButton, r, pSettings->ToolbarButton->type, pSettings->ToolbarButton->Color, pSettings->ToolbarButton->ColorTo, pSettings->ToolbarButton->interlaced, pSettings->ToolbarButton->bevelstyle, pSettings->ToolbarButton->bevelposition, pSettings->bevelWidth, pSettings->ToolbarButton->borderColor, pSettings->ToolbarButton->borderWidth);
                                        // Clean up - delete the temporary bitmap...
                                        DeleteObject(tempBitmap);
                                }

                                //====================

                                if (!pSettings->ToolbarButton->parentRelative)
                                {
                                        // Paint unpressed workspace/task buttons...
                                        BitBlt(pToolbar->cachedBackground, pToolbar->ButtonX1, pToolbar->ButtonY, pToolbar->ButtonWH, pToolbar->ButtonWH, pToolbar->cachedToolbarButton, 0, 0, SRCCOPY);
                                        BitBlt(pToolbar->cachedBackground, pToolbar->ButtonX2, pToolbar->ButtonY, pToolbar->ButtonWH, pToolbar->ButtonWH, pToolbar->cachedToolbarButton, 0, 0, SRCCOPY);
                                        BitBlt(pToolbar->cachedBackground, pToolbar->ButtonX3, pToolbar->ButtonY, pToolbar->ButtonWH, pToolbar->ButtonWH, pToolbar->cachedToolbarButton, 0, 0, SRCCOPY);
                                        BitBlt(pToolbar->cachedBackground, pToolbar->ButtonX4, pToolbar->ButtonY, pToolbar->ButtonWH, pToolbar->ButtonWH, pToolbar->cachedToolbarButton, 0, 0, SRCCOPY);
                                }

                                //====================

                                // Set the "cached bitmaps created" indicator...
                                pToolbar->cachedBitmapsExist = true;
                        }

                        //====================

                        // Copy the cached bitmap into the temporary buffer...
                        BitBlt(buf, ps.rcPaint.left, ps.rcPaint.top, updateRectWidth, updateRectHeight, pToolbar->cachedBackground, ps.rcPaint.left, ps.rcPaint.top, SRCCOPY);

                        //====================
                        
                        // Paint windowlabel background+text or taskbar buttons?
                        if (pSettings->taskbarOnToolbar && pSystembar)
                        {
                                if (tbWinLabelW > (pSystembar->WinEnumeratorCount*16))
                                {
                                        pSystembar->DrawTaskbar(buf, 0, pToolbar->winlabelRect);
                                }
                        }
                        else pToolbar->DrawWindowLabel(buf, pToolbar->winlabelRect);

                        //====================

                        if (!pSettings->ToolbarButtonPressed->parentRelative && (pToolbar->ButtonPressed1 || pToolbar->ButtonPressed2 || pToolbar->ButtonPressed3 || pToolbar->ButtonPressed4))
                        {
                                // Paint pressed workspace/task buttons...
                                // (not cached since they are not normally visible)
                                HDC src = CreateCompatibleDC(NULL);
                                HBITMAP srcbmp = CreateCompatibleBitmap(hdc, pToolbar->ButtonWH, pToolbar->ButtonWH);
                                HBITMAP oldsrc = (HBITMAP)SelectObject(src, srcbmp);

                                r.left = 0; r.top = 0;  r.right = pToolbar->ButtonWH; r.bottom = pToolbar->ButtonWH;
//                              pBImage->CreateGradientByRect(src, r, pSettings->ToolbarButtonPressed->type, pSettings->toolbarButtonPressedColor, pSettings->toolbarButtonPressedColorTo, pSettings->ToolbarButtonPressed->interlaced, pSettings->ToolbarButtonPressed->bevelstyle, pSettings->ToolbarButtonPressed->bevelposition, pSettings->bevelWidth);
                                // *** bbLean experimental StyleItem members ***
                                MakeGradient(src, r, pSettings->ToolbarButtonPressed->type, pSettings->ToolbarButtonPressed->Color, pSettings->ToolbarButtonPressed->ColorTo, pSettings->ToolbarButtonPressed->interlaced, pSettings->ToolbarButtonPressed->bevelstyle, pSettings->ToolbarButtonPressed->bevelposition, pSettings->bevelWidth, pSettings->ToolbarButtonPressed->borderColor, pSettings->ToolbarButtonPressed->borderWidth);
                                if (pToolbar->ButtonPressed1) BitBlt(buf, pToolbar->ButtonX1, pToolbar->ButtonY, pToolbar->ButtonWH, pToolbar->ButtonWH, src, 0, 0, SRCCOPY);
                                if (pToolbar->ButtonPressed2) BitBlt(buf, pToolbar->ButtonX2, pToolbar->ButtonY, pToolbar->ButtonWH, pToolbar->ButtonWH, src, 0, 0, SRCCOPY);
                                if (pToolbar->ButtonPressed3) BitBlt(buf, pToolbar->ButtonX3, pToolbar->ButtonY, pToolbar->ButtonWH, pToolbar->ButtonWH, src, 0, 0, SRCCOPY);
                                if (pToolbar->ButtonPressed4) BitBlt(buf, pToolbar->ButtonX4, pToolbar->ButtonY, pToolbar->ButtonWH, pToolbar->ButtonWH, src, 0, 0, SRCCOPY);

                                SelectObject(src, oldsrc);
                                DeleteDC(src);
                                DeleteObject(srcbmp);
                                DeleteObject(oldsrc);
                        }

                        //====================

                        unsigned int format;

                        // Draw workspace name...
                        HFONT font = CreateFont(pSettings->ToolbarLabel->FontHeight, 0, 0, 0, pSettings->ToolbarLabel->FontWeight, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, pSettings->ToolbarLabel->Font);
                        HGDIOBJ oldfont = SelectObject(buf, font);
                        SetBkMode(buf, TRANSPARENT);
                        format = pSettings->ToolbarLabel->Justify | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE | DT_WORD_ELLIPSIS;
                        CopyRect(&r, &pToolbar->labelRect);
                        r.left = r.left + 3;
                        r.right = r.right - 3;
                        DrawTextWithShadow(buf, pToolbar->ToolbarWorkspaceName, r, format, pSettings->ToolbarLabel->TextColor, pSettings->toolbarLabelShadowColor, pSettings->ToolbarLabel->FontShadow);

                        // Draw clock...
                        DeleteObject(SelectObject(buf, oldfont));
                        font = CreateFont(pSettings->ToolbarClock->FontHeight, 0, 0, 0, pSettings->ToolbarClock->FontWeight, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, pSettings->ToolbarClock->Font);
                        oldfont = SelectObject(buf, font);
                        SetBkMode(buf, TRANSPARENT);
                        format = pSettings->ToolbarClock->Justify | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE | DT_WORD_ELLIPSIS;
                        SetTextColor(buf, pSettings->ToolbarClock->TextColor);
                        CopyRect(&r, &pToolbar->clockRect);
                        r.left = r.left + 3;
                        r.right = r.right - 3;
                        DrawTextWithShadow(buf, pToolbar->CurrentTime, r, format, pSettings->ToolbarClock->TextColor, pSettings->toolbarClockShadowColor, pSettings->ToolbarClock->FontShadow);

                        DeleteObject(SelectObject(buf, oldfont));

                        //====================

                        // Draw arrows on workspace/task buttons (limited gain from
                        // caching since the button itself can be parentrelative)...
                        HPEN activePen, inactivePen, oldPen;

                        activePen = CreatePen(PS_SOLID, 1, pSettings->ToolbarButtonPressed->PicColor);
                        inactivePen = CreatePen(PS_SOLID, 1, pSettings->ToolbarButton->PicColor);

                        oldPen = (HPEN) SelectObject(buf, inactivePen);

                        int xcenter;
                        int xoffset = pToolbar->ButtonWH/2;
                        int ycenter = pToolbar->ToolbarHeight/2;

                        xcenter = pToolbar->ButtonX1 + xoffset;
                        if (pToolbar->ButtonPressed1) SelectObject(buf, activePen);
                        pToolbar->DrawArrowBackward(buf, xcenter, ycenter);
                        if (pToolbar->ButtonPressed1) SelectObject(buf, inactivePen);

                        xcenter = pToolbar->ButtonX2 + xoffset;
                        if (pToolbar->ButtonPressed2) SelectObject(buf, activePen);
                        pToolbar->DrawArrowForward(buf, xcenter, ycenter);
                        if (pToolbar->ButtonPressed2) SelectObject(buf, inactivePen);

                        xcenter = pToolbar->ButtonX3 + xoffset;
                        if (pToolbar->ButtonPressed3) SelectObject(buf, activePen);
                        pToolbar->DrawArrowBackward(buf, xcenter, ycenter);
                        if (pToolbar->ButtonPressed3) SelectObject(buf, inactivePen);

                        xcenter = pToolbar->ButtonX4 + xoffset;
                        if (pToolbar->ButtonPressed4) SelectObject(buf, activePen);
                        pToolbar->DrawArrowForward(buf, xcenter, ycenter);
                        if (pToolbar->ButtonPressed4) SelectObject(buf, inactivePen);

                        SelectObject(buf, oldPen);
                        DeleteObject(inactivePen);
                        DeleteObject(activePen);

                        //====================

                        BitBlt(hdc, 0, 0, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, buf, 0, 0, SRCCOPY);

                        SelectObject(buf, oldbuf);
                        DeleteDC(buf);
                        DeleteObject(bufbmp);
                        DeleteObject(oldbuf);

                        EndPaint(hwnd, &ps);

                        return 0;
                }
                break;

                //====================
/*
                case WM_NCPAINT:
                {
                        if (!wParam) MoveWindow(pToolbar->hToolbarWnd, pToolbar->ToolbarX, pToolbar->ToolbarY, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, true);
                        return 0;
                }
                break;
*/
                //====================

                case BB_SETTOOLBARLABEL:
                {
                        int timeout;
                        if (wParam) timeout = (int)wParam;
                        else timeout = 2000;

                        if (pToolbar->ShowingExternalLabel) KillTimer(pToolbar->hToolbarWnd, TOOLBAR_SETLABEL_TIMER);
                        pToolbar->ShowingExternalLabel = true;
                        strcpy(pToolbar->CurrentWindow, (LPCSTR)lParam);
                        if (!SetTimer(pToolbar->hToolbarWnd, TOOLBAR_SETLABEL_TIMER, timeout, (TIMERPROC)NULL))
                        {
                                MessageBox(0, "Error creating setlabel timer", szToolbarName, MB_OK | MB_ICONERROR | MB_TOPMOST);
                                Log("Could not create Toolbar setlabel timer", NULL);
                                return 0;
                        }
                        if (pSettings->taskbarOnToolbar && pSystembar) InvalidateRect(pSystembar->hSystembarWnd, NULL, false);
                        else InvalidateRect(pToolbar->hToolbarWnd, &pToolbar->winlabelRect, false);
                }
                break;

                //====================

                case WM_TIMER:
                {
                        //====================

                        if (wParam == TOOLBAR_AUTOHIDE_TIMER)
                        {
                                if (pToolbar->ToolbarAutoShowing)
                                {
                                        if (pToolbar->ToolbarAutoCurrentPos == pToolbar->ToolbarAutoShowTarget)
                                        {
                                                KillTimer(pToolbar->hToolbarWnd, TOOLBAR_AUTOHIDE_TIMER);
                                                pToolbar->ToolbarAutoShown = true;
                                                pToolbar->ToolbarAutoShowing = false;
                                                if (!SetTimer(pToolbar->hToolbarWnd, TOOLBAR_MOUSEHOVER_TIMER, 100, (TIMERPROC)NULL))
                                                {
                                                        MessageBox(GetBBWnd(), "Error creating mousehover timer", szToolbarName, MB_OK | MB_ICONERROR | MB_TOPMOST);
                                                        Log("Could not create Toolbar mousehover timer", NULL);
                                                }
                                        }
                                        else
                                        {
                                                if (IsInString(pSettings->toolbarPlacement, "Bottom"))
                                                {
                                                        pToolbar->ToolbarAutoCurrentPos--;
                                                        MoveWindow(pToolbar->hToolbarWnd, pToolbar->ToolbarX, pToolbar->ToolbarAutoCurrentPos, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, true);
                                                        if (!stricmp(pSettings->systembarPlacement, "DockedToToolbar"))
                                                        {
                                                                if (pSystembar) MoveWindow(pSystembar->hSystembarWnd, pSystembar->SystembarX, (pToolbar->ToolbarAutoCurrentPos - pSystembar->SystembarHeight), pSystembar->SystembarWidth, pSystembar->SystembarHeight, true);
                                                        }
                                                }
                                                else
                                                {
                                                        pToolbar->ToolbarAutoCurrentPos++;
                                                        MoveWindow(pToolbar->hToolbarWnd, pToolbar->ToolbarX, pToolbar->ToolbarAutoCurrentPos, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, true);
                                                        if (!stricmp(pSettings->systembarPlacement, "DockedToToolbar"))
                                                        {
                                                                if (pSystembar) MoveWindow(pSystembar->hSystembarWnd, pSystembar->SystembarX, (pToolbar->ToolbarAutoCurrentPos + pToolbar->ToolbarHeight), pSystembar->SystembarWidth, pSystembar->SystembarHeight, true);
                                                        }
                                                }
                                        }
                                }

                                //====================

                                else if (pToolbar->ToolbarAutoHiding)
                                {
                                        if (pToolbar->ToolbarAutoCurrentPos == pToolbar->ToolbarAutoHideTarget)
                                        {
                                                KillTimer(pToolbar->hToolbarWnd, TOOLBAR_AUTOHIDE_TIMER);
                                                pToolbar->ToolbarAutoHidden = true;
                                                pToolbar->ToolbarAutoHiding = false;
                                                SetTransparency(pToolbar->hToolbarWnd, 1);
                                        }
                                        else
                                        {
                                                if (IsInString(pSettings->toolbarPlacement, "Bottom"))
                                                {
                                                        pToolbar->ToolbarAutoCurrentPos++;
                                                        MoveWindow(pToolbar->hToolbarWnd, pToolbar->ToolbarX, pToolbar->ToolbarAutoCurrentPos, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, true);
                                                        if (!stricmp(pSettings->systembarPlacement, "DockedToToolbar"))
                                                        {
                                                                if (pSystembar) MoveWindow(pSystembar->hSystembarWnd, pSystembar->SystembarX, (pToolbar->ToolbarAutoCurrentPos - pSystembar->SystembarHeight), pSystembar->SystembarWidth, pSystembar->SystembarHeight, true);
                                                        }
                                                }
                                                else
                                                {
                                                        pToolbar->ToolbarAutoCurrentPos--;
                                                        MoveWindow(pToolbar->hToolbarWnd, pToolbar->ToolbarX, pToolbar->ToolbarAutoCurrentPos, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, true);
                                                        if (!stricmp(pSettings->systembarPlacement, "DockedToToolbar"))
                                                        {
                                                                if (pSystembar) MoveWindow(pSystembar->hSystembarWnd, pSystembar->SystembarX, (pToolbar->ToolbarAutoCurrentPos + pToolbar->ToolbarHeight), pSystembar->SystembarWidth, pSystembar->SystembarHeight, true);
                                                        }
                                                }
                                        }
                                }
                                return 0;
                        }

                        //====================

                        if (wParam == TOOLBAR_MOUSEHOVER_TIMER)
                        {
                                if (!pToolbar->CheckIfMouseHover())
                                {
                                        KillTimer(pToolbar->hToolbarWnd, TOOLBAR_MOUSEHOVER_TIMER);
                                        pToolbar->AutoHide();
                                }
                                return 0;
                        }

                        //====================

                        if (wParam == TOOLBAR_SETLABEL_TIMER)
                        {
                                KillTimer(pToolbar->hToolbarWnd, TOOLBAR_SETLABEL_TIMER);
                                pToolbar->ShowingExternalLabel = false;
                        }

                        //====================

                        bool UpdateToolbar = false;
                        bool UpdateSystembar = false;

                        if (pToolbar->FirstUpdate)
                        {
                                SendMessage(pToolbar->hBlackboxWnd, BB_LISTDESKTOPS, (WPARAM)pToolbar->hToolbarWnd, 0);
                                UpdateToolbar = true;
                                pToolbar->FirstUpdate = false;
                        }

                        if (!pToolbar->ShowingExternalLabel)
                        {
                                HWND currentwnd = GetForegroundWindow();
                                if (currentwnd)
                                {
                                        GetWindowText(currentwnd, pToolbar->temp, MAX_LINE_LENGTH);
                                        if (_stricmp(pToolbar->CurrentWindow, pToolbar->temp))
                                        {
                                                if (pSettings->taskbarOnToolbar && pSystembar) UpdateSystembar = true;
                                                else UpdateToolbar = true;
                                        }
                                        strcpy(pToolbar->CurrentWindow, pToolbar->temp);
                                }
                        }

                        //====================

                        // Toolbar date/time display
                        time_t systemTime;
                        struct tm *localTime;
                        char *currentTime=(char*)malloc(MAX_LINE_LENGTH);
                        int currentMinute;

                        time(&systemTime);
                        localTime = localtime(&systemTime);
                        currentMinute = localTime->tm_min;

                        // Only update toolbar if minute has changed...
                        if (currentMinute != pToolbar->DisplayedMinute || UpdateToolbar)
                        {
                                // Get blackbox.rc strftimeFormat setting and format the output...
                                strftime(currentTime, MAX_LINE_LENGTH, pSettings->strftimeFormat, localTime);
                                strcpy(pToolbar->CurrentTime, currentTime);
                                // Remember minute shown in display, now wait until minute changes before updating again...
                                pToolbar->DisplayedMinute = currentMinute;
                                UpdateToolbar = true;
                        }
                        free(currentTime);

                        if (UpdateToolbar)
                        {
                                if (pSettings->taskbarOnToolbar && pSystembar) pSystembar->cachedTaskButtonsExist = false;
                                pToolbar->cachedBitmapsExist = false;
                                InvalidateRect(pToolbar->hToolbarWnd, NULL, false);
                        }

                        if (UpdateSystembar) InvalidateRect(pSystembar->hSystembarWnd, NULL, false);

                        return 0;
                }
                break;

                //====================

                case WM_DROPFILES:
                {
                        static TCHAR filename[MAX_LINE_LENGTH];
                        DragQueryFile((HDROP)wParam, 0, filename, sizeof(filename));

                        // Make sure the dropped file isn't a directory...
                        if (PathIsDirectory(filename))
                        {
                                MessageBox(0, "Trying to fool me, eh?\n...drag'n'drop a style file instead!", "xoblite", MB_OK | MB_TOPMOST);
                                DragFinish((HDROP)wParam);
                                return 0;
                        }
                        else
                        {
                                // Look for a line that should "always" exist in a style file...
                                if (strlen(ReadString(filename, "menu.frame", "")))
                                {
                                        // Set new style...
                                        SendMessage(pToolbar->hBlackboxWnd, BB_SETSTYLE, 0, (LPARAM)filename);
                                        DragFinish((HDROP)wParam);
                                        return 0;
                                }
                                else
                                {
                                        MessageBox(0, "Trying to fool me, eh?\n...drag'n'drop a style file instead!", "xoblite", MB_OK | MB_TOPMOST);
                                        DragFinish((HDROP)wParam);
                                        return 0;
                                }
                        }
                }
                break;
 
                //====================

                case BB_RECONFIGURE:
                {
                        SetRectEmpty(&pToolbar->currentWindowLabelRect);
                        pToolbar->UpdatePosition(); // Gets new settings and resizes window if needed...
                }
                break;

                //====================

                case BB_TOGGLETOOLBAR:
                {
                        if (!pSettings->toolbarHidden) pSettings->toolbarHidden = true;
                        else pSettings->toolbarHidden = false;

                        if (pSettings->toolbarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar"))
                        {
                                // Hide the systembar too if docked to the toolbar and visible...
                                if (!pSettings->systembarHidden) PostMessage(GetBBWnd(), BB_TOGGLESYSTEMBAR, 0, 0);
                        }

                        WriteBool(pSettings->extrcFile, "xoblite.toolbar.hidden:", pSettings->toolbarHidden);

                        if (!pSettings->toolbarHidden) pToolbar->UpdatePosition();
                        ShowWindow(pToolbar->hToolbarWnd, pSettings->toolbarHidden ? SW_HIDE : SW_SHOWNOACTIVATE);
                }
                break;

                //====================

                case WM_CLOSE:
                        return 0;

                //====================

                case WM_DISPLAYCHANGE:
                {
                        if (pSettings->toolbarHidden) break;
                        pToolbar->UpdatePosition();
                }
                break;

                //====================
/*
                case WM_MOUSEACTIVATE:
                        return MA_NOACTIVATE;
                
                case WM_ACTIVATE:
                        if (LOWORD(wParam) == WA_ACTIVE || LOWORD(wParam) == WA_CLICKACTIVE) SetActiveWindow(hwnd);
                return 0;
*/
                //====================

                case WM_EXITSIZEMOVE:
                {
                        if (pToolbar->ToolbarPosInProgress)
                        {
                                pToolbar->ToolbarPosInProgress = false;

                                RECT r;
                                GetWindowRect(pToolbar->hToolbarWnd, &r);
                                pToolbar->ToolbarX = r.left;
                                pToolbar->ToolbarY = r.top;
                                sprintf(pSettings->toolbarPlacement, "Manual x%d y%d", pToolbar->ToolbarX, pToolbar->ToolbarY);

                                pToolbar->UpdatePosition(); // NOTE: Including saving placement to extensions.rc
                        }

                        return 0;
                }
                
                //====================

                // Allow window to move if control key is being held down,
                // holding down the shift key and left clicking moves the window
                // back to the default position...
                case WM_NCHITTEST:
                {
                        if ((GetAsyncKeyState(VK_CONTROL) & 0x8000))
                        {
                                if (pSettings->toolbarAutoHide)
                                {
                                        KillTimer(pToolbar->hToolbarWnd, TOOLBAR_AUTOHIDE_TIMER);
                                        pSettings->toolbarAutoHide = false;
                                        WriteBool(bbrcPath(), "session.screen0.toolbar.autoHide:", pSettings->toolbarAutoHide);
                                        pToolbar->ToolbarAutoHiding = pToolbar->ToolbarAutoShowing = false;
                                        pToolbar->ToolbarAutoHidden = false;
                                        pToolbar->ToolbarAutoShown = true;
                                }

                                pToolbar->ToolbarPosInProgress = true;

                                return HTCAPTION;
                        }
                        return HTCLIENT;
                }
                break;

                //====================

                case WM_LBUTTONDBLCLK:
                {
                        POINT mousepos;
                        GetCursorPos(&mousepos);
                        int relX = mousepos.x - pToolbar->ToolbarX;
                        // Doubleclick on the label -> edit current workspace name
                        if (relX <= pToolbar->labelRect.right) BBExecute(GetDesktopWindow(), NULL, pSettings->preferredEditor, bbrcPath(), NULL, SW_SHOWNORMAL, false);
                        // Doubleclick on the clock -> open date/time properties
                        else if (relX >= pToolbar->clockRect.left) BBExecute(GetDesktopWindow(), NULL, "control.exe", "timedate.cpl", NULL, SW_SHOWNORMAL, false);
                }
                break;

                //====================

                case WM_LBUTTONDOWN:
                case WM_LBUTTONUP:
                case WM_RBUTTONDOWN:
                case WM_RBUTTONUP:
                case WM_MBUTTONDOWN:
                case WM_MBUTTONUP:
                case WM_XBUTTONDOWN:
                case WM_XBUTTONUP:
                {
                        if (pSettings->taskbarOnToolbar && pSystembar)
                        {
                                if (pSystembar->MouseButtonOnTaskbar(hwnd, message, wParam, lParam))
                                {
                                        return DefWindowProc(hwnd, message, wParam, lParam);
                                }
                        }

                        //====================

                        if (message == WM_LBUTTONDOWN)
                        {
                                TRACKMOUSEEVENT track;
                                ZeroMemory(&track,sizeof(track));
                                track.cbSize = sizeof(track);
                                track.dwFlags = TME_LEAVE;
                                track.dwHoverTime = HOVER_DEFAULT;
                                track.hwndTrack = pToolbar->hToolbarWnd;
                                _TrackMouseEvent(&track);

                                pToolbar->ClickMouse(LOWORD(lParam), HIWORD(lParam));
                        }

                        //====================

                        else if (message == WM_LBUTTONUP)
                        {
                                if (pToolbar->ButtonPressed1) SendMessage(pToolbar->hBlackboxWnd, BB_WORKSPACE, 0, 0); // DeskLeft
                                //==========
                                else if (pToolbar->ButtonPressed2) SendMessage(pToolbar->hBlackboxWnd, BB_WORKSPACE, 1, 0); // DeskRight
                                //==========
                                else if (pToolbar->ButtonPressed3) // PrevWindow
                                {
                                        if (GetAsyncKeyState(VK_MENU) & 0x8000)
                                                SendMessage(pToolbar->hBlackboxWnd, BB_WORKSPACE, 8, 0); // This workspace
                                        else
                                                SendMessage(pToolbar->hBlackboxWnd, BB_WORKSPACE, 8, 1); // All workspaces
                                }
                                //==========
                                else if (pToolbar->ButtonPressed4) // NextWindow
                                {
                                        if (GetAsyncKeyState(VK_MENU) & 0x8000)
                                                SendMessage(pToolbar->hBlackboxWnd, BB_WORKSPACE, 9, 0); // This workspace
                                        else
                                                SendMessage(pToolbar->hBlackboxWnd, BB_WORKSPACE, 9, 1); // All workspaces
                                }
                                //==========
                                pToolbar->ButtonPressed1 = pToolbar->ButtonPressed2 = pToolbar->ButtonPressed3 = pToolbar->ButtonPressed4 = false;
                                InvalidateRect(pToolbar->hToolbarWnd, NULL, false);
                        }

                        //====================

                        else if (message == WM_RBUTTONUP)
                        {
                                // Is control key held down?
                                if (GetAsyncKeyState(VK_MENU) & 0x8000) SendMessage(GetBBWnd(), BB_MENU, 0, 0);
                                // Is shift key held down?
                                else if (GetAsyncKeyState(VK_SHIFT) & 0x8000) SendMessage(GetBBWnd(), BB_MENU, 1, 0);
                                else SendMessage(GetBBWnd(), BB_MENU, 2, 0);
                        }

                        //====================

                        else if (message == WM_MBUTTONUP)
                        {
                                // Is the alt key held down?
                                if (GetAsyncKeyState(VK_MENU) & 0x8000) PostMessage(GetBBWnd(), BB_TOGGLESLIT, 0, 0);
                                // Is the shift key held down?
                                else if (GetAsyncKeyState(VK_SHIFT) & 0x8000) PostMessage(GetBBWnd(), BB_TOGGLEPLUGINS, 0, 0);
                                // Is the control key held down?
                                else if (GetAsyncKeyState(VK_CONTROL) & 0x8000) PostMessage(GetBBWnd(), BB_TOGGLEPLUGINS, 0, 0);
                                // Otherwise toggle the systembar, or the slit if the systembar is docked to it...
                                else if (!stricmp(pSettings->systembarPlacement, "DockedToSlit")) PostMessage(GetBBWnd(), BB_TOGGLESLIT, 0, 0);
                                else PostMessage(GetBBWnd(), BB_TOGGLESYSTEMBAR, 0, 0);
                        }

                        //====================

                        else if (message == WM_XBUTTONDOWN)
                        {
                                if (HIWORD(wParam) == XBUTTON1)
                                {
                                        if (pSettings->taskbarOnToolbar)
                                        {
                                                if (GetAsyncKeyState(VK_MENU) & 0x8000) pSystembar->RestoreAllWindows();
                                                else pSystembar->MinimizeAllWindows();
                                        }
                                        else BBExecute(GetDesktopWindow(), NULL, "control.exe", "desk.cpl,,3", NULL, SW_SHOWNORMAL, false);
                                }
                                else if (HIWORD(wParam) == XBUTTON2)
                                {
                                        if (pSettings->taskbarOnToolbar) pSystembar->RestoreAllWindows();
                                        else BBExecute(GetDesktopWindow(), NULL, "control.exe", "mmsys.cpl", NULL, SW_SHOWNORMAL, false);
                                }
                        }
                }
                break;

                //====================

                case WM_MOUSEWHEEL:
                {
                        // Only allow mousewheel resizing when the control key is held down...
                        if (!(GetAsyncKeyState(VK_CONTROL) & 0x8000)) break;

                        bool sizechanged = false;

                        if (GET_WHEEL_DELTA_WPARAM(wParam) < 0)
                        {
                                if (pSettings->toolbarWidthPercent > 30)
                                {
                                        sizechanged = true;
                                        pSettings->toolbarWidthPercent--;
                                }
                        }
                        else
                        {
                                if (pSettings->toolbarWidthPercent < 100)
                                {
                                        sizechanged = true;
                                        pSettings->toolbarWidthPercent++;
                                }
                        }

                        if (sizechanged)
                        {
                                pToolbar->GetSettings();
                                // Force re-rendering of the background and task button bitmaps...
                                pToolbar->cachedBitmapsExist = false;
                                if (pSettings->taskbarOnToolbar && pSystembar) pSystembar->cachedTaskButtonsExist = false;
                                InvalidateRect(pToolbar->hToolbarWnd, NULL, false);

                                MoveWindow(pToolbar->hToolbarWnd, pToolbar->ToolbarX, pToolbar->ToolbarY, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, true);
                                if (pSettings->toolbarOnTop) SetWindowPos(pToolbar->hToolbarWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);
                                else SetWindowPos(pToolbar->hToolbarWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);
                                // If the systembar is docked to the toolbar and not in the slit its size should change as well...
                                if (!stricmp(pSettings->systembarPlacement, "DockedToToolbar") && pSystembar) pSystembar->UpdatePosition();

                                pToolbar->saveWidthOnRestart = true;
                        }

                        // Due to the number of repeated updates when using the mousewheel to change the toolbar width,
                        // we do not save the toolbar width to blackbox.rc each time -> on restart/quit instead...
                        // WriteInt(bbrcPath(), "session.screen0.toolbar.widthPercent:", pSettings->toolbarWidthPercent);
                }
                break;

                //====================

                case WM_MOUSEMOVE:
                {
                        if (pSettings->toolbarAutoHide) pToolbar->AutoShow();
                }
                break;

                //====================

                case WM_MOUSELEAVE:
                case WM_NCMOUSELEAVE:
                {
                        pToolbar->ButtonPressed1 = pToolbar->ButtonPressed2 = pToolbar->ButtonPressed3 = pToolbar->ButtonPressed4 = false;
                        InvalidateRect(pToolbar->hToolbarWnd, NULL, false);
                }
                break;

                //====================

                // If moved, snap window to screen edges...
                case WM_WINDOWPOSCHANGING:
                {
                        // Snap window to screen edges when in manual positioning mode?
                        if (!pSettings->toolbarAutoHide && (pSettings->toolbarPlacement[0] == 'M') && pSettings->toolbarSnapToEdges)
                        {
                                if (pToolbar)
                                {
                                        if (IsWindowVisible(hwnd)) SnapWindowToEdge((WINDOWPOS*)lParam, pSettings->edgeSnapThreshold, true);
                                }
                        }
                }
                return 0;

                //====================

                default:
                        return DefWindowProc(hwnd,message,wParam,lParam);

        //====================
        }
        return 0;
}

//===========================================================================

void Toolbar::DrawWindowLabel(HDC hdc, RECT r)
{
        RECT wlRect, wlTextRect;
        CopyRect(&wlRect, &r);
        CopyRect(&wlTextRect, &wlRect);

        //====================

        // Has the windowLabel bounding rect changed since we last
        // cached it? If so, let's create a new cached bitmap...
        if (!EqualRect(&currentWindowLabelRect, &wlRect))
        {
                CopyRect(&currentWindowLabelRect, &wlRect);
                currentWindowLabelWidth = wlRect.right - wlRect.left;
                currentWindowLabelHeight = wlRect.bottom - wlRect.top;

                if (!pSettings->ToolbarWindowLabel->parentRelative)
                {
                        // Create a temporary bitmap...
                        HBITMAP tempBitmap = CreateCompatibleBitmap(hdc, currentWindowLabelWidth, currentWindowLabelHeight);
                        // Delete the previously cached gradient...
                        HBITMAP oldBitmap = (HBITMAP)SelectObject(cachedToolbarWindowLabel, tempBitmap);
                        DeleteObject(oldBitmap);
                        // Paint button background...
                        r.left = 0; r.top = 0;  r.right = currentWindowLabelWidth; r.bottom = currentWindowLabelHeight;
                        // *** bbLean experimental StyleItem members ***
                        MakeGradient(cachedToolbarWindowLabel, r, pSettings->ToolbarWindowLabel->type, pSettings->ToolbarWindowLabel->Color, pSettings->ToolbarWindowLabel->ColorTo, pSettings->ToolbarWindowLabel->interlaced, pSettings->ToolbarWindowLabel->bevelstyle, pSettings->ToolbarWindowLabel->bevelposition, pSettings->bevelWidth, pSettings->ToolbarWindowLabel->borderColor, pSettings->ToolbarWindowLabel->borderWidth);
                        // Clean up - delete the temporary bitmap...
                        DeleteObject(tempBitmap);
                }
        }

        //====================

        if (!pSettings->ToolbarWindowLabel->parentRelative)
        {
                // Copy the cached bitmap into the temporary buffer...
                BitBlt(hdc, wlRect.left, wlRect.top, currentWindowLabelWidth, currentWindowLabelHeight, cachedToolbarWindowLabel, 0, 0, SRCCOPY);
        }

        //====================

        HFONT font = CreateFont(pSettings->ToolbarWindowLabel->FontHeight, 0, 0, 0, pSettings->ToolbarWindowLabel->FontWeight, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, pSettings->ToolbarWindowLabel->Font);
        HGDIOBJ oldfont = SelectObject(hdc, font);
        SetBkMode(hdc, TRANSPARENT);

        unsigned int format = pSettings->ToolbarWindowLabel->Justify | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE | DT_WORD_ELLIPSIS;

        // Draw window label...
        if (!pSettings->taskbarOnToolbar)
        {
                wlTextRect.left = wlTextRect.left + 3;
                if (pSettings->writeProtection && pSettings->checkingForUpdates) wlTextRect.right = wlTextRect.right - 22;
                else if (pSettings->writeProtection) wlTextRect.right = wlTextRect.right - 13;
                else if (pSettings->checkingForUpdates) wlTextRect.right = wlTextRect.right - 14;
                else wlTextRect.right = wlTextRect.right - 3;
        }
        else
        {
                wlTextRect.left = wlTextRect.left + 7;
                if (pSettings->writeProtection && pSettings->checkingForUpdates) wlTextRect.right = wlTextRect.right - 29;
                else if (pSettings->writeProtection) wlTextRect.right = wlTextRect.right - 20;
                else if (pSettings->checkingForUpdates) wlTextRect.right = wlTextRect.right - 21;
                else wlTextRect.right = wlTextRect.right - 7;
        }
        DrawTextWithShadow(hdc, pToolbar->CurrentWindow, wlTextRect, format, pSettings->ToolbarWindowLabel->TextColor, pSettings->toolbarWindowLabelShadowColor, pSettings->ToolbarWindowLabel->FontShadow);

        DeleteObject(SelectObject(hdc, oldfont));

        //====================

        // Draw "lock" icon if write protection is enabled...
        if (pSettings->writeProtection)
        {
                int lockIconX, lockIconY;
                if (!pSettings->taskbarOnToolbar) lockIconX = wlRect.right - 4;
                else lockIconX = wlRect.right - 7;
                lockIconY = (wlRect.bottom - wlRect.top)/2;

                HPEN protectPen = CreatePen(PS_SOLID, 1, pSettings->toolbarWindowLabelShadowColor);
                HPEN oldPen = (HPEN) SelectObject(hdc, protectPen);

                MoveToEx(hdc, lockIconX - 5, pSettings->Toolbar->borderWidth + lockIconY-2, NULL);
                LineTo(hdc, lockIconX - 2, pSettings->Toolbar->borderWidth + lockIconY-2);

                MoveToEx(hdc, lockIconX - 6, pSettings->Toolbar->borderWidth + lockIconY-1, NULL);
                LineTo(hdc, lockIconX - 6, pSettings->Toolbar->borderWidth + lockIconY+1);
                MoveToEx(hdc, lockIconX - 2, pSettings->Toolbar->borderWidth + lockIconY-1, NULL);
                LineTo(hdc, lockIconX - 2, pSettings->Toolbar->borderWidth + lockIconY+1);

                MoveToEx(hdc, lockIconX - 7, pSettings->Toolbar->borderWidth + lockIconY+1, NULL);
                LineTo(hdc, lockIconX, pSettings->Toolbar->borderWidth + lockIconY+1);
                MoveToEx(hdc, lockIconX - 7, pSettings->Toolbar->borderWidth + lockIconY+2, NULL);
                LineTo(hdc, lockIconX, pSettings->Toolbar->borderWidth + lockIconY+2);
                MoveToEx(hdc, lockIconX - 7, pSettings->Toolbar->borderWidth + lockIconY+3, NULL);
                LineTo(hdc, lockIconX, pSettings->Toolbar->borderWidth + lockIconY+3);
                MoveToEx(hdc, lockIconX - 7, pSettings->Toolbar->borderWidth + lockIconY+4, NULL);
                LineTo(hdc, lockIconX, pSettings->Toolbar->borderWidth + lockIconY+4);

                SelectObject(hdc, oldPen);
                DeleteObject(protectPen);
        }

        //====================

        // Draw "<->" icon if a check for updates is in progress...
        if (pSettings->checkingForUpdates)
        {
                int checkIconX, checkIconY;
                if (pSettings->writeProtection)
                {
                        if (!pSettings->taskbarOnToolbar) checkIconX = wlRect.right - 13;
                        else checkIconX = wlRect.right - 16;
                }
                else
                {
                        if (!pSettings->taskbarOnToolbar) checkIconX = wlRect.right - 5;
                        else checkIconX = wlRect.right - 8;
                }
                checkIconY = (wlRect.bottom - wlRect.top)/2;

                HPEN checkPen = CreatePen(PS_SOLID, 1, pSettings->toolbarWindowLabelShadowColor);
                HPEN oldPen = (HPEN) SelectObject(hdc, checkPen);

                MoveToEx(hdc, checkIconX - 5, pSettings->Toolbar->borderWidth + checkIconY-2, NULL);
                LineTo(hdc, checkIconX - 1, pSettings->Toolbar->borderWidth + checkIconY-2);
                MoveToEx(hdc, checkIconX - 1, pSettings->Toolbar->borderWidth + checkIconY-1, NULL);
                LineTo(hdc, checkIconX - 1, pSettings->Toolbar->borderWidth + checkIconY+2);
                MoveToEx(hdc, checkIconX - 6, pSettings->Toolbar->borderWidth + checkIconY-1, NULL);
                LineTo(hdc, checkIconX - 6, pSettings->Toolbar->borderWidth + checkIconY);
                MoveToEx(hdc, checkIconX, pSettings->Toolbar->borderWidth + checkIconY, NULL);
                LineTo(hdc, checkIconX - 3, pSettings->Toolbar->borderWidth + checkIconY);

                MoveToEx(hdc, checkIconX - 5, pSettings->Toolbar->borderWidth + checkIconY+4, NULL);
                LineTo(hdc, checkIconX - 1, pSettings->Toolbar->borderWidth + checkIconY+4);
                MoveToEx(hdc, checkIconX - 6, pSettings->Toolbar->borderWidth + checkIconY+3, NULL);
                LineTo(hdc, checkIconX - 6, pSettings->Toolbar->borderWidth + checkIconY);
                MoveToEx(hdc, checkIconX - 1, pSettings->Toolbar->borderWidth + checkIconY+3, NULL);
                LineTo(hdc, checkIconX - 1, pSettings->Toolbar->borderWidth + checkIconY+2);
                MoveToEx(hdc, checkIconX - 7, pSettings->Toolbar->borderWidth + checkIconY+2, NULL);
                LineTo(hdc, checkIconX - 4, pSettings->Toolbar->borderWidth + checkIconY+2);

                SelectObject(hdc, oldPen);
                DeleteObject(checkPen);
        }
}

//===========================================================================

void Toolbar::DrawArrowBackward(HDC hdc, int xcenter, int ycenter)
{
        if (!pSettings->alternativeBullets)
        {
                // bb4win classic style arrows (qwilk thinks they are really nice and smooth... =] )

                MoveToEx(hdc, xcenter-1, ycenter, NULL);
                LineTo(hdc, xcenter-1, ycenter+1);
                MoveToEx(hdc, xcenter, ycenter-1, NULL);
                LineTo(hdc, xcenter, ycenter+2);
                MoveToEx(hdc, xcenter+1, ycenter-2, NULL);
                LineTo(hdc, xcenter+1, ycenter+3);
        }
        else
        {
                // bb4nix style arrows (...qwilk thinks they are butt ugly! <g>)

                MoveToEx(hdc, xcenter+1, ycenter-1, NULL);
                LineTo(hdc, xcenter+3, ycenter-1);
                MoveToEx(hdc, xcenter-1, ycenter, NULL);
                LineTo(hdc, xcenter+3, ycenter);
                MoveToEx(hdc, xcenter+1, ycenter+1, NULL);
                LineTo(hdc, xcenter+3, ycenter+1);
        }
}

//====================

void Toolbar::DrawArrowForward(HDC hdc, int xcenter, int ycenter)
{
        if (!pSettings->alternativeBullets)
        {
                // bb4win classic style arrows (qwilk thinks they are really nice and smooth... =] )

                MoveToEx(hdc, xcenter-1, ycenter-2, NULL);
                LineTo(hdc, xcenter-1, ycenter+3);
                MoveToEx(hdc, xcenter, ycenter-1, NULL);
                LineTo(hdc, xcenter, ycenter+2);
                MoveToEx(hdc, xcenter+1, ycenter, NULL);
                LineTo(hdc, xcenter+1, ycenter+1);
        }
        else
        {
                // bb4nix style arrows (...qwilk thinks they are butt ugly! <g>)

                MoveToEx(hdc, xcenter-1, ycenter-1, NULL);
                LineTo(hdc, xcenter+1, ycenter-1);
                MoveToEx(hdc, xcenter-1, ycenter, NULL);
                LineTo(hdc, xcenter+3, ycenter);
                MoveToEx(hdc, xcenter-1, ycenter+1, NULL);
                LineTo(hdc, xcenter+1, ycenter+1);
        }
}

//===========================================================================

void Toolbar::GetSettings()
{
        ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
        ScreenHeight = GetSystemMetrics(SM_CYSCREEN);

//      if (pSettings->usingWin2kXP)
//      {
//              ScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
//              ScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
//      }
//      else
//      {
//              ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
//              ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
//      }

        //====================

        ToolbarWidth = (ScreenWidth * pSettings->toolbarWidthPercent) / 100;

        int borderPadding = 0;
        if (pSettings->ToolbarLabel->borderWidth > borderPadding) borderPadding = pSettings->ToolbarLabel->borderWidth;
//      if (!pSettings->taskbarOnToolbar && (pSettings->ToolbarWindowLabel->borderWidth > borderPadding)) borderPadding = pSettings->ToolbarWindowLabel->borderWidth;
        if (pSettings->ToolbarWindowLabel->borderWidth > borderPadding) borderPadding = pSettings->ToolbarWindowLabel->borderWidth;
        if (pSettings->ToolbarClock->borderWidth > borderPadding) borderPadding = pSettings->ToolbarClock->borderWidth;
        if (pSettings->ToolbarButton->borderWidth > borderPadding) borderPadding = pSettings->ToolbarButton->borderWidth;

        int fontPadding = pSettings->Toolbar->FontHeight;
        if (pSettings->ToolbarLabel->FontHeight > fontPadding) fontPadding = pSettings->ToolbarLabel->FontHeight;
//      if (!pSettings->taskbarOnToolbar && (pSettings->ToolbarWindowLabel->FontHeight > fontPadding)) fontPadding = pSettings->ToolbarWindowLabel->FontHeight;
        if (pSettings->ToolbarWindowLabel->FontHeight > fontPadding) fontPadding = pSettings->ToolbarWindowLabel->FontHeight;
        if (pSettings->ToolbarClock->FontHeight > fontPadding) fontPadding = pSettings->ToolbarClock->FontHeight;
        if (fontPadding > 12) fontPadding -= 12;
        else fontPadding = 0;

//      if (pSettings->taskbarOnToolbar) ToolbarHeight = 17;
//      else ToolbarHeight = 15;
//      ToolbarHeight += (pSettings->Toolbar->borderWidth * 2) + (borderPadding*2) + fontPadding;

        ToolbarHeight = 15 + (borderPadding*2) + fontPadding;
        if (pSettings->taskbarOnToolbar && (ToolbarHeight < 17)) ToolbarHeight = 17;
        ToolbarHeight += (pSettings->Toolbar->borderWidth * 2);

        //====================

        if (pSettings->toolbarPlacement[0] == 'M') // Manual positioning
        {
                char tempPlacement[MAX_LINE_LENGTH];
                strcpy(tempPlacement, pSettings->toolbarPlacement);

                char token1[MAX_LINE_LENGTH], token2[MAX_LINE_LENGTH], token3[MAX_LINE_LENGTH];
                LPSTR tokens[2];
                tokens[0] = token1;
                tokens[1] = token2;

                token1[0] = token2[0] = token3[0] = '\0';
                BBTokenize (tempPlacement, tokens, 2, token3);

                ToolbarX = atoi(&token2[1]); // "x123"
                ToolbarY = atoi(&token3[1]); // "y456"

                // Make sure the systembar is inside the visible screen...
                int xmax = ScreenWidth - ToolbarWidth;
                int ymax = ScreenHeight - ToolbarHeight;
                if (ToolbarX < 0) ToolbarX = 0;
                if (ToolbarY < 0) ToolbarY = 0;
                if (ToolbarX > xmax) ToolbarX = xmax;
                if (ToolbarY > ymax) ToolbarY = ymax;

                return;
        }

        //====================

        if (!stricmp(pSettings->toolbarPlacement, "TopLeft"))
        {
                ToolbarX = 0;
                ToolbarY = 0;
                if (!pSettings->systembarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar")) ToolbarAutoHideTarget = 0 - ToolbarHeight;
                else ToolbarAutoHideTarget = 1 - ToolbarHeight;
                ToolbarAutoShowTarget = 0;
        }
        else if (!stricmp(pSettings->toolbarPlacement, "TopCenter"))
        {
                ToolbarX = (ScreenWidth - ToolbarWidth) / 2;
                ToolbarY = 0;
                if (!pSettings->systembarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar")) ToolbarAutoHideTarget = 0 - ToolbarHeight;
                else ToolbarAutoHideTarget = 1 - ToolbarHeight;
                ToolbarAutoShowTarget = 0;
        }
        else if (!stricmp(pSettings->toolbarPlacement, "TopRight"))
        {
                ToolbarX = ScreenWidth - ToolbarWidth;
                ToolbarY = 0;
                if (!pSettings->systembarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar")) ToolbarAutoHideTarget = 0 - ToolbarHeight;
                else ToolbarAutoHideTarget = 1 - ToolbarHeight;
                ToolbarAutoShowTarget = 0;
        }
        else if (!stricmp(pSettings->toolbarPlacement, "BottomLeft"))
        {
                ToolbarX = 0;
                ToolbarY = ScreenHeight - ToolbarHeight;
                if (!pSettings->systembarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar")) ToolbarAutoHideTarget = ScreenHeight;
                else ToolbarAutoHideTarget = ScreenHeight - 1;
                ToolbarAutoShowTarget = ScreenHeight - ToolbarHeight;
        }
        else if (!stricmp(pSettings->toolbarPlacement, "BottomCenter"))
        {
                ToolbarX = (ScreenWidth - ToolbarWidth) / 2;
                ToolbarY = ScreenHeight - ToolbarHeight;
                if (!pSettings->systembarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar")) ToolbarAutoHideTarget = ScreenHeight;
                else ToolbarAutoHideTarget = ScreenHeight - 1;
                ToolbarAutoShowTarget = ScreenHeight - ToolbarHeight;
        }
        else if (!stricmp(pSettings->toolbarPlacement, "BottomRight"))
        {
                ToolbarX = ScreenWidth - ToolbarWidth;
                ToolbarY = ScreenHeight - ToolbarHeight;
                if (!pSettings->systembarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar")) ToolbarAutoHideTarget = ScreenHeight;
                else ToolbarAutoHideTarget = ScreenHeight - 1;
                ToolbarAutoShowTarget = ScreenHeight - ToolbarHeight;
        }
        else ToolbarY = ScreenHeight - ToolbarHeight;
}

//===========================================================================

void Toolbar::UpdatePosition()
{
        // Fetch the new size and position parameters for our window...
        pToolbar->GetSettings();

        // Resize the window and move it to its new position...
        MoveWindow(hToolbarWnd, ToolbarX, ToolbarY, ToolbarWidth, ToolbarHeight, true);
        // Force re-rendering of the background and windowLabel / task button bitmaps...
        cachedBitmapsExist = false;
        if (pSettings->taskbarOnToolbar && pSystembar) pSystembar->cachedTaskButtonsExist = false;
        else SetRectEmpty(&pToolbar->currentWindowLabelRect);
        InvalidateRect(hToolbarWnd, NULL, false);

        if (pSettings->toolbarOnTop) SetWindowPos(hToolbarWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);
        else SetWindowPos(hToolbarWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);
        SetTransparency(hToolbarWnd, pSettings->toolbarTransparencyAlpha);

        // Update desktop area...
        UpdateDesktopArea();

        // Auto hide?
        if (pSettings->toolbarAutoHide)
        {
                ToolbarAutoHiding = ToolbarAutoShowing = false;
                ToolbarAutoHidden = false;
                ToolbarAutoShown = true;
                if (!CheckIfMouseHover()) AutoHide();
        }

        WriteString(bbrcPath(), "session.screen0.toolbar.placement:", pSettings->toolbarPlacement);

        // Update the systembar position too if it is docked to the toolbar...
        if (!stricmp(pSettings->systembarPlacement, "DockedToToolbar")) pSystembar->UpdatePosition();
}

//====================

void Toolbar::UpdateDesktopArea()
{
        RECT r;

        if (pSettings->fullMaximization)
        {
                r.left = 0;
                r.right = ScreenWidth;
                r.top = 0;
                r.bottom = ScreenHeight;
        }
        else if (pSettings->desktopAreaNotDefined)
        {
                r.left = 0;
                r.right = ScreenWidth;

                if (ToolbarY == 0)
                {
                        r.top =  ToolbarHeight + 5;
                        r.bottom = ScreenHeight;
                }
                else
                {
                        r.top = 0;
                        r.bottom = ScreenHeight - ToolbarHeight - 5;
                }
        }
        else
        {
                r.left = pSettings->desktopArea.left;
                r.top = pSettings->desktopArea.top;
                r.right = ScreenWidth - pSettings->desktopArea.right;
                r.bottom = ScreenHeight - pSettings->desktopArea.bottom;
        }

        SystemParametersInfo(SPI_SETWORKAREA, 0, (PVOID)&r, SPIF_SENDCHANGE);
        SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&r, SPIF_SENDCHANGE); // ...is this really needed?
}

//===========================================================================

void Toolbar::ClickMouse(int mouseX, int mouseY)
{
        if (mouseY >= 3 && mouseY <= (ToolbarHeight - 3))
        {
                if (mouseX >= ButtonX1 && mouseX <= (ButtonX1 + ButtonWH))
                {
                        ButtonPressed1 = true;
                        InvalidateRect(hToolbarWnd, NULL, false);
                        return;
                }
                else if (mouseX >= ButtonX2 && mouseX <= (ButtonX2 + ButtonWH))
                {
                        ButtonPressed2 = true;
                        InvalidateRect(hToolbarWnd, NULL, false);
                        return;
                }
                else if (mouseX >= ButtonX3 && mouseX <= (ButtonX3 + ButtonWH))
                {
                        ButtonPressed3 = true;
                        InvalidateRect(hToolbarWnd, NULL, false);
                        return;
                }
                else if (mouseX >= ButtonX4 && mouseX <= (ButtonX4 + ButtonWH))
                {
                        ButtonPressed4 = true;
                        InvalidateRect(hToolbarWnd, NULL, false);
                        return;
                }
                else if (pMenuMaker) pMenuMaker->Hide();
        }
        else if (pMenuMaker) pMenuMaker->Hide();
}

//===========================================================================

void Toolbar::ToggleAlwaysOnTop()
{
        if (!pSettings->toolbarOnTop)
        {
                pSettings->toolbarOnTop = true;
                SetWindowPos(hToolbarWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);
        }
        else
        {
                pSettings->toolbarOnTop = false;
                SetWindowPos(hToolbarWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);
        }

        WriteBool(bbrcPath(), "session.screen0.toolbar.onTop:", pSettings->toolbarOnTop);
}

//===========================================================================

bool Toolbar::CheckIfMouseHover()
{
        POINT mousepos;
        GetCursorPos(&mousepos);
        if (mousepos.x >= ToolbarX && mousepos.x <= (ToolbarX + ToolbarWidth))
        {
                if (mousepos.y >= ToolbarY && mousepos.y <= (ToolbarY + ToolbarHeight)) return true;
        }

        return false;
}

//===========================================================================

void Toolbar::AutoShow()
{
        if (pSettings->toolbarAutoHide && !ToolbarAutoShowing && !ToolbarAutoShown)
        {
                if (ToolbarAutoHiding)
                {
                        KillTimer(hToolbarWnd, TOOLBAR_AUTOHIDE_TIMER);
                        ToolbarAutoHiding = false;
                }
/*
                if (pSettings->autohideSpeed == 0)
                {
                        if (IsInString(pSettings->toolbarPlacement, "Bottom"))
                        {
                                MoveWindow(pToolbar->hToolbarWnd, pToolbar->ToolbarX, pToolbar->ToolbarAutoShowTarget, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, true);
                                if (!stricmp(pSettings->systembarPlacement, "DockedToToolbar") && pSystembar)
                                {
                                        MoveWindow(pSystembar->hSystembarWnd, pSystembar->SystembarX, (pToolbar->ToolbarAutoShowTarget - pSystembar->SystembarHeight), pSystembar->SystembarWidth, pSystembar->SystembarHeight, true);
                                }
                        }
                        else
                        {
                                MoveWindow(pToolbar->hToolbarWnd, pToolbar->ToolbarX, pToolbar->ToolbarAutoShowTarget, pToolbar->ToolbarWidth, pToolbar->ToolbarHeight, true);
                                if (!stricmp(pSettings->systembarPlacement, "DockedToToolbar") && pSystembar)
                                {
                                        MoveWindow(pSystembar->hSystembarWnd, pSystembar->SystembarX, (pToolbar->ToolbarAutoShowTarget + pToolbar->ToolbarHeight), pSystembar->SystembarWidth, pSystembar->SystembarHeight, true);
                                }
                        }
                        ToolbarAutoShown = true;
                        if (!SetTimer(pToolbar->hToolbarWnd, TOOLBAR_MOUSEHOVER_TIMER, 100, (TIMERPROC)NULL))
                        {
                                MessageBox(GetBBWnd(), "Error creating mousehover timer", szToolbarName, MB_OK | MB_ICONERROR | MB_TOPMOST);
                                Log("Could not create Toolbar mousehover timer", NULL);
                        }
                        return;
                }
*/
                if (IsInString(pSettings->toolbarPlacement, "Bottom"))
                {
                        if (!pSettings->systembarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar")) ToolbarAutoCurrentPos = ScreenHeight;
                        else ToolbarAutoCurrentPos = ScreenHeight - 1;
                }
                else
                {
                        if (!pSettings->systembarHidden && !stricmp(pSettings->systembarPlacement, "DockedToToolbar")) ToolbarAutoCurrentPos = 0 - ToolbarHeight;
                        else ToolbarAutoCurrentPos = 1 - ToolbarHeight;
                }

                ToolbarAutoHidden = false;
                ToolbarAutoShowing = true;
                SetTransparency(hToolbarWnd, pSettings->toolbarTransparencyAlpha);

                if (!SetTimer(hToolbarWnd, TOOLBAR_AUTOHIDE_TIMER, pSettings->autohideSpeed, (TIMERPROC)NULL))
                {
                        MessageBox(GetBBWnd(), "Error creating autohide timer", szToolbarName, MB_OK | MB_ICONERROR | MB_TOPMOST);
                        Log("Could not create Toolbar autohide timer", NULL);
                }

                // ...BBSoundFX trigger message here?
                // SendMessage(GetBBWnd(), BB_MENU, -1, 0); // ...doesn't do anything, just to signal e.g. BBSoundFX
        }
}

//====================

void Toolbar::AutoHide()
{
        if (pSettings->toolbarAutoHide && !ToolbarAutoHiding && !ToolbarAutoHidden)
        {
                if (ToolbarAutoShowing)
                {
                        KillTimer(hToolbarWnd, TOOLBAR_AUTOHIDE_TIMER);
                        ToolbarAutoShowing = false;
                }

                ToolbarAutoCurrentPos = ToolbarY;
                ToolbarAutoShown = false;
                ToolbarAutoHiding = true;

                if (!SetTimer(hToolbarWnd, TOOLBAR_AUTOHIDE_TIMER, pSettings->autohideSpeed, (TIMERPROC)NULL))
                {
                        MessageBox(GetBBWnd(), "Error creating autohide timer", szToolbarName, MB_OK | MB_ICONERROR | MB_TOPMOST);
                        Log("Could not create Toolbar autohide timer", NULL);
                }

                // ...BBSoundFX trigger message here?
                // SendMessage(GetBBWnd(), BB_MENU, -1, 0); // ...doesn't do anything, just to signal e.g. BBSoundFX
        }
}

//===========================================================================





syntax highlighting by

w e b c p p
web c plus plus