/*****************************************************************************\
*                                                                             *
*  Name   : tree_window                                                       *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*******************************************************************************
* Copyright (c) 1998-$now By Author.  This program is free software; you can  *
* redistribute it and/or modify it under the terms of the GNU General Public  *
* License as published by the Free Software Foundation; either version 2 of   *
* the License or (at your option) any later version.  This is online at:      *
*     http://www.fsf.org/copyleft/gpl.html                                    *
* Please send any updates to: fred@gruntose.com                               *
\*****************************************************************************/

#include "bitmap_tree.h"
#include "mfc_extensions.rh"
#include "tree_window.h"

#include <basis/utility.h>

////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(tree_window, CMRCSizeControlBar)
  ON_WM_CLOSE()
  ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////////

// this class just routes the two special events we handle to the parent.

class derived_bitmap_tree : public bitmap_tree
{
public:
  derived_bitmap_tree(tree_window &parent) : _parent(parent) {}

  virtual BOOL OnRClick(NMHDR *pNMHDR, LRESULT *pResult) {
    _parent.handle_right_click();
    *pResult = 0;
    return true;
  }
  virtual BOOL OnDoubleClick(NMHDR *pNMHDR, LRESULT *pResult) {
    _parent.handle_double_click();
    *pResult = 0;
    return true;
  }

private:
  tree_window &_parent;
};

////////////////////////////////////////////////////////////////////////////

tree_window::tree_window() : _tree_control(new derived_bitmap_tree(*this)) {}

tree_window::~tree_window() { WHACK(_tree_control); }

bitmap_tree &tree_window::tree_control() { return *_tree_control; }

void tree_window::OnClose() { CMRCSizeControlBar::OnClose(); }

CRect tree_window::initial_window() { return CRect(0, 0, 150, 300); }

void tree_window::OnContextMenu(CWnd* pWnd, CPoint point)
{ handle_right_click(); }

BOOL tree_window::Create(CWnd *pParent, LPCTSTR lpszTitle, UINT nID,
    DWORD dwStyle, const RECT &rect)
{
  CMRCSizeControlBar::Create(pParent, lpszTitle, nID, dwStyle, rect);
  tree_control().Create(TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT
      | TVS_SHOWSELALWAYS | WS_BORDER | WS_CHILD, rect, this,
//testing if this id is needed.
0);
//      IDC_TREE_CONTROL);

//hmmm: this is really the job of the derived guy...
////  tree_control().SetNormalImages(IDB_TREE_ICONS, 5, RGB(255, 0, 0));

  tree_control().ShowWindow(SW_SHOWNORMAL);  // show tree control.
  return true;
}

void tree_window::OnSizedOrDocked(int cx, int cy, BOOL bFloating, int flags)
{
  CRect rect(0, 0, cx, cy);
  // give some space around the controls.
  if (IsProbablyFloating()) rect.InflateRect(-2, -2);
  else rect.InflateRect(-4, -4);

  // reposition the tree.
  _tree_control->MoveWindow(rect, true);
}

