Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TNZ_POPUPMENU_INCLUDED
Toshihiro Shimizu 890ddd
#define TNZ_POPUPMENU_INCLUDED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tw/action.h"
Toshihiro Shimizu 890ddd
#include "tw/popup.h"
Toshihiro Shimizu 890ddd
#include "tw/button.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TWIN_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TPopupMenu;
Toshihiro Shimizu 890ddd
class TPopupMenuItem;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TPopupMenuListener {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TPopupMenuListener() {}
Shinya Kitaoka 120a6e
  virtual ~TPopupMenuListener() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  virtual void onMenuSelect(TPopupMenuItem *item) = 0;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TPopupMenuItem : public TButton {
Shinya Kitaoka 120a6e
  wstring m_title;
Shinya Kitaoka 120a6e
  bool m_selected;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TPopupMenuItem(TPopupMenu *parent, string name);
Shinya Kitaoka 120a6e
  void repaint();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void enter(const TPoint &);
Shinya Kitaoka 120a6e
  void leave(const TPoint &);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void leftButtonDown(const TMouseEvent &e);
Shinya Kitaoka 120a6e
  void leftButtonUp(const TMouseEvent &e);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void setTitle(wstring title);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPopupMenu *getPopup();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void select(bool on);
Shinya Kitaoka 120a6e
  bool isSelected() const { return m_selected; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  virtual int getWidth();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TPopupMenuSeparator : public TWidget {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TPopupMenuSeparator(TPopupMenu *parent);
Shinya Kitaoka 120a6e
  void repaint();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Note: to implement right-mouse-bottom menu use
Toshihiro Shimizu 890ddd
// the class TContextMenu (see below)
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TPopupMenu : public TPopup {
Shinya Kitaoka 120a6e
  TPopupMenuItem *m_currentItem;
Shinya Kitaoka 120a6e
  TPopupMenuListener *m_listener;
Shinya Kitaoka 120a6e
  int m_firstVisibleItem;
Shinya Kitaoka 120a6e
  int m_visibleItemCount;
Shinya Kitaoka 120a6e
  bool m_leftAlignment;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TPopupMenu(TWidget *parent, string name = "popupMenu");
Shinya Kitaoka 120a6e
  void draw();
Shinya Kitaoka 120a6e
  void configureNotify(const TDimension &);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void leftButtonDrag(const TPoint &, UCHAR);
Shinya Kitaoka 120a6e
  void leftButtonUp(const TPoint &);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void popup(const TPoint &pos, int width = 120);
Shinya Kitaoka 120a6e
  void close();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPopupMenuItem *getCurrentItem() { return m_currentItem; }
Shinya Kitaoka 120a6e
  void setListener(TPopupMenuListener *);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  static TPopupMenu *getCurrentMenu();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void setLeftAlignment(bool a) { m_leftAlignment = a; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  void updateItemsVisibility();
Shinya Kitaoka 120a6e
  void updateSize();
Shinya Kitaoka 120a6e
  void computeSize(TPoint &pos, int maxY);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  friend class TPopupMenuArrow;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// TContextMenu :
Toshihiro Shimizu 890ddd
//  a class specifically dedicated to
Toshihiro Shimizu 890ddd
//  right-mouse-button popup menu (aka Context Menu)
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Note: DON'T use the method popup() (inherited from TPopupMenu)
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Note: the context menu will be automatically destroyed
Toshihiro Shimizu 890ddd
// DON'T keep a reference to the context menu
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// see examples below
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---- Example 1 (position indipendent) ----
Shinya Kitaoka 120a6e
Toshihiro Shimizu 890ddd
   void Pluto::rightButtonUp(const TMouseEvent &e) {
Toshihiro Shimizu 890ddd
     TContextMenu *menu = new TContextMenu(this);
Toshihiro Shimizu 890ddd
     menu->addCommand("MI_Cut");
Toshihiro Shimizu 890ddd
     menu->addCommand("MI_Copy");
Toshihiro Shimizu 890ddd
     menu->addCommand("MI_Paste");
Toshihiro Shimizu 890ddd
     menu->addCommand("MI_Clear");
Toshihiro Shimizu 890ddd
     TContextMenu::open(menu, e.m_pos);
Toshihiro Shimizu 890ddd
   }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  ---- Example 2 (position dependent) ----
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
   class MyContextMenu : public TContextMenu {
Toshihiro Shimizu 890ddd
     int m_myValue;
Toshihiro Shimizu 890ddd
   public:
Shinya Kitaoka 120a6e
     MyContextMenu(TWidget*parent, int myValue)
Toshihiro Shimizu 890ddd
     : TContextMenu(parent), m_myValue(myValue), m_col(col) {
Toshihiro Shimizu 890ddd
        tconnect(this, "foo",  this, &CellContextMenu::onFoo);
Toshihiro Shimizu 890ddd
        tconnect(this, "bar",  this, &CellContextMenu::onBar);
Toshihiro Shimizu 890ddd
     }
Toshihiro Shimizu 890ddd
     void onFoo() { ... }
Toshihiro Shimizu 890ddd
     void onBar() { ... }
Toshihiro Shimizu 890ddd
   };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
   void Pluto::rightButtonUp(const TMouseEvent &e) {
Toshihiro Shimizu 890ddd
     int row = ....
Toshihiro Shimizu 890ddd
     int col = ....
Toshihiro Shimizu 890ddd
     TContextMenu::open(new CellContextMenu(this, row, col), e.m_pos);
Toshihiro Shimizu 890ddd
   }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  -----------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TContextMenu : public TPopupMenu {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  class CommandFilter {
Shinya Kitaoka 120a6e
  public:
Shinya Kitaoka 120a6e
    virtual bool check(string cmdName) const = 0;
Shinya Kitaoka 120a6e
    virtual ~CommandFilter() {}
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TContextMenu(TWidget *parent);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void add(string name, TGenericCommandAction *action);
Shinya Kitaoka 120a6e
  void addCommand(string cmdName);
Shinya Kitaoka 120a6e
  void addCommand(string cmdName, TGenericCommandAction *action);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void addSeparator() { new TPopupMenuSeparator(this); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
template <class t=""></class>
Shinya Kitaoka 120a6e
inline void addCommand(string cmdName, T*target, void (T::*method)()) {
Shinya Kitaoka 120a6e
//addCommand(cmdName, new TCommandAction<t>(target, method));</t>
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  static void open(TContextMenu *menu, const TPoint &pos);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  static void setCommandFilter(CommandFilter *filter);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void onTimer(int);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
inline void tconnect(TContextMenu *menu, string cmdName, T *target,
Shinya Kitaoka 120a6e
                     void (T::*method)()) {
Shinya Kitaoka 120a6e
  menu->addCommand(cmdName, new TCommandAction<t>(target, method));</t>
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class arg="" class="" t,=""></class>
Shinya Kitaoka 120a6e
inline void tconnect(TContextMenu *menu, string cmdName, T *target,
Shinya Kitaoka 120a6e
                     void (T::*method)(Arg), Arg arg) {
Shinya Kitaoka 120a6e
  menu->addCommand(cmdName, new TCommandAction1<t, arg="">(target, method, arg));</t,>
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif