Project

General

Profile

Support #2670 ยป WTableView_DisableContextMenu.cpp

Roel Standaert, 03/11/2014 06:19 PM

 
#include <Wt/WApplication>
#include <Wt/WEvent>
#include <Wt/WPopupMenu>
#include <Wt/WPoint>
#include <Wt/WTableView>
#include <Wt/WText>
#include <Wt/WStandardItem>
#include <Wt/WStandardItemModel>

class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment &env);
private:
void HandleTableMouseEvent(Wt::WModelIndex index, Wt::WMouseEvent event);
Wt::WTableView *tableView_;
};

HelloApplication::HelloApplication(const Wt::WEnvironment &env)
: Wt::WApplication(env)
{
setTitle("Hello world");

root()->addWidget(new Wt::WText("Hello world!"));

Wt::WStandardItemModel *model = new Wt::WStandardItemModel(2, 2, root());
Wt::WStandardItem *item0_0 = new Wt::WStandardItem();
item0_0->setText("One");
model->setItem(0, 0, item0_0);
Wt::WStandardItem *item0_1 = new Wt::WStandardItem();
item0_1->setText("Two");
model->setItem(0, 1, item0_1);
Wt::WStandardItem *item1_0 = new Wt::WStandardItem();
item1_0->setText("Three");
model->setItem(1, 0, item1_0);
Wt::WStandardItem *item1_1 = new Wt::WStandardItem();
item1_1->setText("Four");
model->setItem(1, 1, item1_1);

Wt::WTableView *tableView = new Wt::WTableView();
tableView->setModel(model);

tableView->setAttributeValue("oncontextmenu", "event.cancelBubble = true; event.returnValue = false; return false;");
tableView->mouseWentDown().connect(this, &HelloApplication::HandleTableMouseEvent);

root()->addWidget(tableView);
}

void HelloApplication::HandleTableMouseEvent(Wt::WModelIndex index, Wt::WMouseEvent event)
{
if (event.button() == Wt::WMouseEvent::RightButton) {
auto coord = event.document();
auto pm = new Wt::WPopupMenu();
pm->addItem("Hi!");
pm->popup(Wt::WPoint(coord.x, coord.y));
}
}

int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, [](const Wt::WEnvironment &env) {
return new HelloApplication(env);
});
}
    (1-1/1)