//colorbtn.cpp
#include <owl/pch.h>
#include <owl/chooseco.h>
#include "source/app.rh"
#include "source/colorbtn.h"

//drop down color button
//usage:
//button=new TButtonColorPick(TWindow*,int id,TColor color);
//EV_COMMAND(id,userfunction),
//void userfunction() is called whenver user changes the selected Color
//button->Color indicates the current color

DEFINE_RESPONSE_TABLE1(TButtonColorPick, TButtonEx)
  EV_MESSAGE(WM_COLOR_POPUP,EvColorPopup),
END_RESPONSE_TABLE;

TButtonColorPick::TButtonColorPick(TWindow* parent, int id, TColor color,
                          int X, int Y, int W, int H, bool isDefault,
                           TModule* module)
: TButtonEx(parent, id, 0,X,Y,W,H, isDefault,module)
{
  Color=color;
  ColorPopup=new TColorPopup(this);
}

TButtonColorPick::TButtonColorPick(TWindow* parent, int id, TColor color,TModule* module)
:TButtonEx(parent, id, module)
{
  Color=color;
  ColorPopup=new TColorPopup(this);
}

TButtonColorPick::~TButtonColorPick()
{
}

void TButtonColorPick::SetupWindow()
{
  TButtonEx::SetupWindow();
}

void TButtonColorPick::PaintFace(TDC& dc, TRect& rect)
{
  dc.FillRect(rect, TBrush(TColor::Sys3dFace));

  int w=rect.Height();
  TRect downRect=TRect(rect.right-w,rect.top+3,rect.right-3,rect.bottom-3);
  TRect boxRect=TRect(4,4,downRect.left-8,rect.bottom-4);
  if (IsSet(biPushed))
  {
    downRect.Offset(1,1);
    boxRect.Offset(1,1);
  }

  //draw a cheap drop down button
  TRect rc=downRect;
  ::DrawFrameControl(dc,&rc,DFC_SCROLL,DFCS_SCROLLCOMBOBOX|DFCS_FLAT);
//  TUIPart().Paint(dc,downRect,TUIPart::uiScroll,TUIPart::ScrollCombo|TUIPart::Flat  );

  //draw a rectangle using the currently selected Color
  dc.SelectStockObject(BLACK_PEN);
  dc.SelectObject(TBrush(Color));
  dc.Rectangle(boxRect);
  dc.SelectObject(TBrush(TColor::Sys3dShadow));
  dc.PatBlt(boxRect.right+4,boxRect.top,1,boxRect.Height(),PATCOPY);

  if (IsSet(biFocus))
      PaintFocusRect(dc, rect);
}

void TButtonColorPick::SendNotificationEx()
{
  //this function is the default message handler defined in TGlyphDib
  //don't send a message to the parent.
  //instead, open a color window,
  //then after color window  closes, go to EvColorPopup
  ColorPopup->Origin=GetWindowRect().BottomLeft();
  ColorPopup->Color=Color;
  ColorPopup->Create();
}

//This message is processed when ColopPopup Window exits
LRESULT TButtonColorPick::EvColorPopup(WPARAM wparam,LPARAM)
{
  uint i=wparam;
  TColor color;
  if(i==99// "Other" button in the color window was pushed
  {
    TChooseColorDialog::TData choose;
    TColor    custColors[16] = {
    0x20A0A0L, 0x208090L, 0x208080L, 0x207070L,
    0x702020L, 0x802020L, 0x902020L, 0xA02020L,
    0x707020L, 0x808020L, 0x909020L, 0xA0A020L,
    0x202070L, 0x202080L, 0x202090L, 0x2020A0L
    };
    choose.Flags = CC_RGBINIT;
    choose.Color = Color;
    choose.CustColors = custColors;
    if (TChooseColorDialog(this, choose).Execute() == IDOK)
      SetBoxColor(choose.Color);
    return 0;
  }

  if(ColorPopup)
    if(ColorPopup->HWindow)
    {
      SetBoxColor(ColorPopup->Color);
      Invalidate(true);
    }

  return 0;
}

void TButtonColorPick::SetBoxColor(TColor color)
{
  Color=color;
  Invalidate(true);
  //color has been picked, tell parent
  TButtonEx::SendNotificationEx();
}