#include <owl/pch.h>
#include <owl/uihelper.h>
#include "app.rh"
#include "colorwin.h"

//Color pick window
//called by TButtonColorPick
//on EvLButtonDown it sends a message to TButtonColorPick (its parent)
//and closes. The parent 'TButtonColorPick' will notify its parent
//(the actual window/dialog) that color has changed

DEFINE_RESPONSE_TABLE1(TColorPopup, TWindow)
 EV_WM_PAINT,
 EV_WM_LBUTTONDOWN,
 EV_WM_MOUSEMOVE,
END_RESPONSE_TABLE;

TColorPopup::TColorPopup(TWindow* parent) : TWindow(parent)
{
 ColorIndex[0] =TColor::Black;
 ColorIndex[1] =TColor(128,0,0);
 ColorIndex[2] =TColor(0,128,0);
 ColorIndex[3] =TColor(128,128,0);
 ColorIndex[4] =TColor(0,0,128);
 ColorIndex[5] =TColor(128,0,128);
 ColorIndex[6] =TColor(0,128,128);
 ColorIndex[7] =TColor::Gray;
 ColorIndex[8] =TColor::LtGray;
 ColorIndex[9] =TColor::LtRed;
 ColorIndex[10]=TColor::LtGreen;
 ColorIndex[11]=TColor::LtYellow;
 ColorIndex[12]=TColor::LtBlue;
 ColorIndex[13]=TColor::LtMagenta;
 ColorIndex[14]=TColor::LtCyan;
 ColorIndex[15]=TColor::White;

 HasOtherButton=true;
 Width=94;
 Height=94;
 if(HasOtherButton) Height+=24;
 Origin=TPoint(0,0);

 Attr.Style |= WS_POPUPWINDOW;
 Attr.ExStyle |=WS_EX_DLGMODALFRAME;
 Attr.Style &= ~(WS_VISIBLE|WS_SYSMENU|WS_CAPTION);
 SetBkgndColor(TColor::Sys3dFace);

 Attr.X=0;Attr.Y=0;
 Attr.W=Width;Attr.H=Height;
 OtherButtonRect=TRect(2,Width-1,Width-7,Height-7);

 BW=BH=22;
 TSize size(BW-4,BH-4);
 int i,j,k=0;
 for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   rect[k++]=TRect( TPoint(2 + BW*j, 2 + BH*i), size );
}

TColorPopup::~TColorPopup()
{
}

void TColorPopup::SetupWindow()
{
 TWindow::SetupWindow();
 if(Origin.x==0) return;
 Index=-1;
 for(int i=0;i<16;i++)
  if(Color==ColorIndex[i])
   Index=i;

 MoveWindow(Origin.x,Origin.y,Width,Height,false);
 ShowWindow(SW_SHOWNOACTIVATE);
 SetCapture();
}

void TColorPopup::Paint(TDC& dc, bool , TRect& )
{
 int i;
 //fill the color boxes
 for(i=0;i<16;i++){
  dc.SelectObject(TBrush(ColorIndex[i]));
  dc.PatBlt(rect[i],PATCOPY);
  TUIBorder(rect[i].InflatedBy(2,2),TUIBorder::WndRecessed).Paint(dc);
 }

 //draw the "Other..." button at the bottom
 if(HasOtherButton)
 {
  TUIBorder(OtherButtonRect,TUIBorder::ButtonUp).Paint(dc);
  dc.SelectObject(TFont("MS Sans Serif",-12));
  dc.SetBkMode(TRANSPARENT);
  dc.SetTextAlign(TA_TOP|TA_CENTER);
  dc.SetTextColor(TColor::SysBtnText);
  dc.TextOut(Width/2,OtherButtonRect.top+2,"Other...");
 }

 FocusRect(dc);
}

void TColorPopup::EvMouseMove(uint key,TPoint& point)
{
 SetCapture();
 TWindow::EvMouseMove(key,point);
 int i;
 TRect rc;
 for(i=0;i<16;i++)
 {
  if( (rect[i].InflatedBy(2,2)).Contains(point) )
  {
   if(i==Index) return;

   TClientDC dc(*this);
   if((Index>=0)&(Index<16))
   {
    TUIBorder(rect[Index].InflatedBy(2,2),TUIBorder::WndRecessed).Paint(dc);
    dc.SelectObject(TBrush(ColorIndex[Index]));
    dc.PatBlt(rect[Index],PATCOPY);
   }
   //remember where we parked
   Index=i;

   //draw a focus around this color box
   FocusRect(dc);
  }
 }
}

void TColorPopup::FocusRect(TDC &dc)
{
 if((Index>=0)&(Index<16))
 {
  dc.SelectStockObject(NULL_BRUSH);

  dc.SelectStockObject(BLACK_PEN);
  dc.Rectangle(rect[Index].InflatedBy( 2, 2));

  dc.SelectStockObject(WHITE_PEN);
  dc.Rectangle(rect[Index].InflatedBy( 1, 1));

  dc.SelectStockObject(BLACK_PEN);
  dc.Rectangle(rect[Index].InflatedBy( 0, 0));

 }
 else
 {
  TRect rc=OtherButtonRect;
  rc=TRect(rc.left+3,rc.top+3,rc.left+rc.Height()-3,rc.bottom-3);
  dc.SelectObject(TBrush(Color));
  dc.PatBlt(rc,PATCOPY);
  TUIBorder(rc,TUIBorder::WndRecessed).Paint(dc);
 }
}

void TColorPopup::EvLButtonDown(uint key,TPoint& point)
{
 ReleaseCapture();
 TWindow::EvLButtonDown(key,point);
 for(int i=0;i<16;i++)
  if( (rect[i].InflatedBy(2,2)).Contains(point))
   CmSendMessage(i);

 if( OtherButtonRect.Contains(point) )
  CmSendMessage(99);

 if(!(GetClientRect().Contains(point)))
  CloseWindow();
}

void TColorPopup::CmSendMessage(int i)
{
 if((i>=0)&(i<16)){
  Color=ColorIndex[i];
  Parent->SendMessage(WM_COLOR_POPUP,i,0);
  Parent->Invalidate(true);
 }
 else
  Parent->PostMessage(WM_COLOR_POPUP,99,0);
 CloseWindow();
}