// pkg_subtree.h (this is -*-c++-*-)
//
//  Copyright 1999-2002, 2004-2005, 2007-2008 Daniel Burrows
//  Copyright 2015-2016 Manuel A. Fernandez Montecelo
//
//  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 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.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
//  Boston, MA 02110-1301, USA.
//

#ifndef PKG_SUBTREE_H
#define PKG_SUBTREE_H

/** \brief A subtree which contains packages (and other subtrees)
 * 
 *  \file pkg_subtree.h
 */

#include "pkg_node.h"

#include <cwidget/widgets/subtree.h>


class pkg_subtree:public cwidget::widgets::subtree<pkg_tree_node>,
		  public pkg_tree_node
{
  std::wstring name;
  std::wstring description; // This is like a Description: field.

  sigc::signal1<void, std::wstring> *info_signal;
  pkg_subtree *num_packages_parent;

  bool num_packages_known;
  int num_packages;

  void do_highlighted_changed(bool highlighted);
protected:
  void set_label(const std::wstring &_name) {name=_name;}
public:
  pkg_subtree(std::wstring _name, std::wstring _description=L"",
	      sigc::signal1<void, std::wstring> *_info_signal=NULL,
	      bool _expanded=false):
    cwidget::widgets::subtree<pkg_tree_node>(_expanded), name(_name),
    description(_description), info_signal(_info_signal),
    num_packages_parent(NULL),
    num_packages_known(true), num_packages(0)
  {
    highlighted_changed.connect(sigc::mem_fun(this, &pkg_subtree::do_highlighted_changed));
  }

  pkg_subtree(std::wstring _name, bool _expanded):
    cwidget::widgets::subtree<pkg_tree_node>(_expanded), name(_name),
    description(L""), info_signal(NULL),
    num_packages_parent(NULL),
    num_packages_known(true), num_packages(0)
  {
    highlighted_changed.connect(sigc::mem_fun(this, &pkg_subtree::do_highlighted_changed));
  }

  virtual void paint(cwidget::widgets::tree *win, int y, bool hierarchical,
		     const cwidget::style &st);
  virtual const wchar_t *tag();
  virtual const wchar_t *label();

  virtual void select(undo_group *undo);
  virtual void hold(undo_group *undo);
  virtual void keep(undo_group *undo);
  virtual void remove(undo_group *undo);
  virtual void purge(undo_group *undo);
  virtual void reinstall(undo_group *undo);
  virtual void set_auto(bool isauto, undo_group *undo);
  virtual void forbid_upgrade(undo_group *undo);

  /** \brief Set the parent of this tree for the purposes of package counting.
   *
   *  When inc_num_packages() is called on this tree, it's also called
   *  on the parent.
   */
  void set_num_packages_parent(pkg_subtree *new_parent)
  {
    num_packages_parent = new_parent;
  }

  /** \brief Increment the number of packages in this tree and
   *  in the parent (if any).
   *
   *  Has no effect if num_packages_known is false.
   */
  void inc_num_packages();
  /** \brief Discard all information about how many packages
   *  this subtree contains.
   */
  void clear_num_packages();
  /** \brief Set the number of packages that this subtree
   *  contains.
   */
  void set_num_packages(int num);
  bool get_num_packages_known() const { return num_packages_known; }
  int get_num_packages() const { return num_packages; }
  std::wstring get_name() {return name;}
  std::wstring get_description() {return description;}

  bool dispatch_key(const cwidget::config::key &k, cwidget::widgets::tree *owner);
};


/**
 * (Daniel Burrows): This special tree munges its tag to allow an integer to be
 * prepended to it.  Ok, it's a dreadful hack.  I admit it.
 */
class pkg_subtree_with_order:public pkg_subtree
{
  std::wstring my_tag;
public:
  pkg_subtree_with_order(std::wstring name, std::wstring description,
			 sigc::signal1<void, std::wstring> *_info_signal,
			 unsigned char order, bool expand=false)
    :pkg_subtree(name, description, _info_signal, expand)
  {
    my_tag+=order;
    my_tag+=pkg_subtree::tag();
  }

  pkg_subtree_with_order(std::wstring name, unsigned char order, bool expand=false)
    :pkg_subtree(name, expand)
  {
    my_tag+=order;
    my_tag+=pkg_subtree::tag();
  }

  virtual const wchar_t *tag()
  {
    return my_tag.c_str();
  }
};


#endif
