tahoma2d/toonz/sources/include/tw/treeview.h

289 lines
7.6 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TNZ_TREEVIEW_INCLUDED
#define TNZ_TREEVIEW_INCLUDED
#include "tw/tw.h"
#include "tw/dragdrop.h"
#include <set>
#undef DVAPI
#undef DVVAR
#ifdef TWIN_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif
#ifdef WIN32
#pragma warning(disable : 4251)
#endif
//-------------------------------------------------------------------
class TTreeView;
class TTreeViewItem;
class TTreeViewItemParent;
class TScrollbar;
class TTextField;
class TContextMenu;
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI TTreeViewItemUpdater {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
virtual bool done() const = 0;
virtual void next() = 0;
virtual bool matchItem(const TTreeViewItem *) const = 0;
virtual TTreeViewItem *createItem(TTreeViewItemParent *parent) const = 0;
virtual ~TTreeViewItemUpdater() {}
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI TTreeViewItemParent {
TTreeView *m_treeView;
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
bool m_valid;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
typedef vector<TTreeViewItem *> ItemContainer;
ItemContainer m_children;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TTreeViewItemParent() : m_treeView(0), m_valid(true) {}
virtual ~TTreeViewItemParent();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void clearItems();
int getItemCount() const;
TTreeViewItem *getItem(int index) const;
TTreeViewItem *getItem(wstring name) const;
TTreeViewItem *getItem(string name) const;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void addItem(TTreeViewItem *item);
void removeItem(TTreeViewItem *item);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void updateItems(TTreeViewItemUpdater &updater);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool hasChild(TTreeViewItem *item) const;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual void onItemChange();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void setTreeView(TTreeView *treeView) { m_treeView = treeView; }
TTreeView *getTreeView() const { return m_treeView; }
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// not implemented
TTreeViewItemParent(const TTreeViewItemParent &);
TTreeViewItemParent &operator=(const TTreeViewItemParent &);
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI TTreeViewItem : public TTreeViewItemParent {
unsigned char m_status;
TTreeViewItemParent *m_parent;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TTreeViewItem(TTreeViewItemParent *parent);
virtual ~TTreeViewItem();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TTreeViewItemParent *getParent() const { return m_parent; }
void setParent(TTreeViewItemParent *parent);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void clearParent() { m_parent = 0; }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual void onOpen() {}
virtual void onClose() {}
virtual void onDoubleClick();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual TTreeViewItem *getSon(string sonName) { return 0; }
virtual TTreeViewItem *getSon(wstring sonName) { return 0; }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool isLeaf() const;
bool isEmpty() const;
bool isOpen() const;
bool isSelected() const;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void open();
void close();
virtual void refresh();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void setIsLeaf(bool isLeaf);
void select(bool on);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool unselectDescendents(); // ritorna true se ce n'era uno selezionato
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// "origin" sono le coordinate del punto in basso a sinistra dell'icona
virtual void drawIcon(TTreeView *w, const TPoint &origin);
virtual void drawName(TTreeView *w, const TPoint &origin);
virtual void draw(TTreeView *w, const TPoint &origin);
virtual int getWidth(TTreeView *w);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual wstring getName() const { return L"noname"; }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual TDimension getIconSize() const;
virtual wstring getTooltipString() { return wstring(); }
virtual string getContextHelpReference() { return string(); }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual bool isRenameEnabled() const { return false; }
virtual void rename(wstring name){};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual TContextMenu *createContextMenu() { return 0; }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual bool acceptDrop(const TData *data) const { return false; }
virtual bool drop(const TData *data) { return false; }
virtual bool click(const TPoint &p) { return false; }
virtual bool drag(const TPoint &p) { return false; }
virtual bool rightClick(const TPoint &p);
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// not implemented
TTreeViewItem(const TTreeViewItem &);
TTreeViewItem &operator=(const TTreeViewItem &);
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI TTreeView : public TWidget,
public TTreeViewItemParent,
public TDragDropListener {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
class Listener {
public:
virtual void onTreeViewSelect(TTreeView *treeView) = 0;
virtual ~Listener() {}
};
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
struct VisibleItem {
TPoint m_pos;
// m_pos e' la coordinata del punto in basso a sinistra dell'icona
// (l'origine e' il punto in alto a sinistra della prima icona)
int m_height;
// m_height == getIconSize().ly
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TTreeViewItem *m_item;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
enum { LastSiblingFlag = 0x1, RootFlag = 0x2, SonFlag = 0x4 };
int m_status;
// int m_siblingLinkLength;
// int m_childLinkLength;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
VisibleItem();
};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int m_xMargin, m_yMargin,
m_iconMargin; // distanza orizzontale fra il bottone "+/-" e l'icona
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
vector<VisibleItem> m_visibleItems;
2016-03-19 06:57:51 +13:00
#ifndef MACOSX
2016-06-15 18:43:10 +12:00
// togliere l'ifdef con il gcc3.3.2
bool m_valid;
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
int m_height;
int m_yoffset;
int m_xoffset;
// int m_selectedItemIndex;
std::set<int> m_selectedItemIndices;
TScrollbar *m_vScb, *m_hScb;
TTextField *m_renameTextField;
bool m_multiSelectionEnabled;
int m_dropIndex;
vector<Listener *> m_listeners;
TPoint m_lastMousePos;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
// costruttore/distruttore
TTreeView(TWidget *parent, string name = "treeView");
~TTreeView();
// listeners
void addListener(Listener *listener);
void removeListener(Listener *listener);
void notifyListeners();
// configure
void setScrollbars(TScrollbar *h, TScrollbar *v);
void enableMultiSelection(bool on);
bool isMultiSelectionEnabled() const { return m_multiSelectionEnabled; }
// resize
void configureNotify(const TDimension &s);
// draw
void repaint();
virtual void drawButton(const TPoint &p, bool open);
// event handlers
void leftButtonDown(const TMouseEvent &e);
void leftButtonDrag(const TMouseEvent &e);
void leftButtonUp(const TMouseEvent &e);
void rightButtonDown(const TMouseEvent &e);
void rightButtonDrag(const TMouseEvent &e);
void rightButtonUp(const TMouseEvent &e);
void middleButtonDown(const TMouseEvent &e);
void middleButtonDrag(const TMouseEvent &e);
void middleButtonUp(const TMouseEvent &e);
void mouseWheel(const TMouseEvent &, int wheel);
void leftButtonDoubleClick(const TMouseEvent &);
TDropSource::DropEffect onEnter(const TDragDropListener::Event &event);
TDropSource::DropEffect onOver(const TDragDropListener::Event &event);
TDropSource::DropEffect onDrop(const TDragDropListener::Event &event);
void onLeave();
// to override
virtual void onSelect(TTreeViewItem *item){};
virtual bool startDragDrop(TTreeViewItem *item) { return false; }
virtual void onExpand(TTreeViewItem *item){};
virtual void onCollapse(TTreeViewItem *item){};
// queries
int getSelectedItemCount() const;
TTreeViewItem *getSelectedItem(int index = 0) const;
// selection
void selectNone();
void select(TTreeViewItem *item);
void selectParent();
void selectSon(string sonName);
void selectItem(int index); // provvisorio
// refresh
void refreshCurrentItem();
void refresh();
// tooltips &helps
wstring getTooltipString(const TPoint &);
string getContextHelpReference(const TPoint &p);
2016-03-19 06:57:51 +13:00
#ifndef MACOSX
2016-06-15 18:43:10 +12:00
// togliere l'ifdef con il gcc3.3.2
void onItemChange();
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
// misc
void scrollToX(int x);
void scrollToY(int y);
void renameCurrentItem(wstring s);
void updateVisibleItems();
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
void getPlacement(int index, TRect &rect);
int getIndexFromY(int y);
void addItemAndSons(TTreeViewItem *item, int indentation);
void updateScrollbars();
2016-03-19 06:57:51 +13:00
};
#endif