/*
 ============================================================================
 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 "StringItem.h" 
#include "TitleItem.h" 
#include "Menu.h" 
#include "../Settings.h" 
#include "../BImage.h" 
#include "../Toolbar.h" 

extern MenuMaker* pMenuMaker;
extern Settings *pSettings;
extern BImage *pBImage;
extern Toolbar *pToolbar;

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

StringItem::StringItem(char* pszTitle, char* pszCommand, char* pszInit) : TitleItem(pszTitle, MENU_DEFAULT)
{
        m_pszCommand = pszCommand ? strdup(pszCommand) : NULL;
        m_pszInit = pszInit ? strdup(pszInit) : NULL;

        m_hwndEditor = NULL;

        // Calculate the width of the "OK" button string...
        HDC fonthdc = CreateDC("DISPLAY", NULL, NULL, NULL);
        HGDIOBJ oldEntryfont = SelectObject(fonthdc, pMenuMaker->m_hEntryFont);
        GetTextExtentPoint32(fonthdc, "OK", 3, &buttonSize);
        SelectObject(fonthdc, oldEntryfont);
        DeleteDC(fonthdc);

        editboxActive = false;
}

StringItem::~StringItem()
{
        if (m_hwndEditor) DestroyWindow(m_hwndEditor);

        if(m_pszCommand) free(m_pszCommand);
        m_pszCommand = NULL;
        if(m_pszInit) free(m_pszInit);
        m_pszInit = NULL;
}

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

void StringItem::Paint(HDC hDC)
{
        RECT r;
        GetTitleRect(&r);

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

        if (editboxActive)
        {
                bool status = this->m_bActive;
                this->m_bActive = false;
                MenuItem::Paint(hDC);
                this->m_bActive = status;

                RECT rbutton;
                rbutton.left = GetWidth() - pSettings->menuBorderWidth - buttonSize.cx - 4;
                rbutton.right = rbutton.left + buttonSize.cx;
                rbutton.top = r.top;
                rbutton.bottom = r.bottom;

                pBImage->CreateGradientByRect(hDC, rbutton, pSettings->MenuIndicator->type, pSettings->MenuIndicator->Color, pSettings->MenuIndicator->ColorTo, pSettings->MenuIndicator->interlaced, pSettings->MenuIndicator->bevelstyle, pSettings->MenuIndicator->bevelposition, pSettings->bevelWidth);

                DrawTextWithShadow(hDC, "OK", rbutton, DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOCLIP, pSettings->MenuHilite->TextColor, pSettings->menuHiliteShadowColor, pSettings->MenuHilite->FontShadow);

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

                if (!m_hwndEditor)
                {
                        int editboxX = 3 + pSettings->menuBorderWidth;
                        int editboxY = r.top;
                        int editboxWidth = rbutton.left - 3 - editboxX;
                        int editboxHeight = r.bottom - r.top;
                
                        m_hwndEditor =  CreateWindowEx(
                                WS_EX_TOPMOST,
                                "edit",
                                NULL,
                                WS_CHILD|WS_VISIBLE|WS_BORDER|ES_LEFT|ES_AUTOHSCROLL,
                                editboxX,
                                editboxY,
                                editboxWidth,
                                editboxHeight,
                                m_pParent->GetWindow(),
                                NULL,
                                m_pParent->m_hInstance,
                                NULL
                        );

                        SendMessage(m_hwndEditor, (UINT) WM_SETFONT, (WPARAM) pMenuMaker->m_hEntryFont, TRUE);
                        SendMessage(m_hwndEditor, (UINT) EM_SETLIMITTEXT, (WPARAM) 255, 0);
                        SendMessage(m_hwndEditor, WM_SETTEXT, 0, (LPARAM) m_pszInit);

                        ShowWindow(m_hwndEditor, SW_SHOW);
                }
                else ShowWindow(m_hwndEditor, SW_SHOWNOACTIVATE);
        }
        else
        {
                if (m_hwndEditor) ShowWindow(m_hwndEditor, SW_HIDE);
                MenuItem::Paint(hDC);
                if (IsActive()) DrawTextWithShadow(hDC, m_pszTitle, r, GetDrawTextFormat(), pSettings->MenuHilite->TextColor, pSettings->menuHiliteShadowColor, pSettings->MenuHilite->FontShadow);
                else DrawTextWithShadow(hDC, m_pszTitle, r, GetDrawTextFormat(), pSettings->MenuFrame->TextColor, pSettings->menuFrameShadowColor, pSettings->MenuFrame->FontShadow);
        }
}

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

void StringItem::Invoke(int button)
{
        if (button == 1 && m_hwndEditor) // Left mouse button click?
        {
                editboxActive = false;

                // First we set focus to the toolbar to prevent
                // the focus from going back to the previous task
                // after the menu is closed...
                if (pSettings->followActive) SetForegroundWindow(pToolbar->hToolbarWnd);

                //====================
        
                // Fetch the edited string from the edit box...
                char editedValue[256], broam[MAX_LINE_LENGTH], display[MAX_LINE_LENGTH];;
                SendMessage(m_hwndEditor, WM_GETTEXT, 256, (LPARAM) &editedValue);
                strcpy(broam, m_pszCommand);
                strcat(broam, " ");
                strcat(broam, editedValue);
                strcpy(display, "xoblite -> ");
                strcat(display, broam);

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

                // Send the bro@m command, display it on the toolbar, and hide the menu...
                PostMessage(GetBBWnd(), BB_BROADCAST, 0, (LPARAM)broam);
                SendMessage(GetBBWnd(), BB_SETTOOLBARLABEL, 0, (LPARAM)display);
                PostMessage(GetBBWnd(), BB_HIDEMENU, 0, 0);
        }
        else if (button == 2) // Right mouse button click?
        {
                // Allow only a single menu item editbox to be active at a time...
                if (!editboxActive && !pSettings->menuEditboxAlreadyActive)
                {
                        // Show the editbox + OK button...
                        pSettings->menuEditboxAlreadyActive = true;
                        editboxActive = true;
                }
                else
                {
                        // Hide the editbox + OK button...
                        pSettings->menuEditboxAlreadyActive = false;
                        editboxActive = false;
                }

                this->m_bActive = false;
                MenuItem::Active(true);
        }
}

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





syntax highlighting by

w e b c p p
web c plus plus