Project

General

Profile

Bug #4271 » ClickablePopupItem.cpp

test case - Стойчо Стефанов Stoycho Stefanov, 06/30/2015 09:39 AM

 
#include <Wt/WApplication>
#include <Wt/WEnvironment>
#include <Wt/WTable>
#include <Wt/WTableCell>
#include <Wt/WText>

#include <string>
#include <iostream>

using namespace std;
using namespace Wt;


#include <Wt/WVBoxLayout>
#include <Wt/WContainerWidget>
#include <Wt/WStackedWidget>
#include <Wt/WEvent>
#include <Wt/WPopupMenu>
#include <Wt/WPushButton>
#include <Wt/WDialog>

class TestPopup : public WContainerWidget
{
public:
TestPopup()
: WContainerWidget()
, popup_(nullptr)
{
setAttributeValue("oncontextmenu","event.cancelBubble = true; event.returnValue = false; return false;");
mouseWentUp().connect(boost::bind(&TestPopup::onMouseUp,this,_1));
decorationStyle().setBackgroundColor(Wt::gray);
}
void popupAction(const Wt::WMouseEvent& e);
void showPopup(const Wt::WMouseEvent &e) ;
void onMouseUp(const Wt::WMouseEvent &e) ;
private:
WPopupMenu *popup_;
Wt::Signals::connection onPopupHide_;
};

void TestPopup::popupAction(const Wt::WMouseEvent& e) {
cout << "popup result "<< popup_->result() << endl;
if (popup_->result()) cout << "popup result "<< popup_->result()->text().narrow() << endl;
return; // any Item was selected
}

void TestPopup::showPopup(const Wt::WMouseEvent &e) {
if (!popup_) {
popup_ = (new WPopupMenu());
popup_->addItem("Plain");
popup_->addItem("Check")->setCheckable(true);
}

if(popup_->isHidden()){
popup_->popup(e);
popup_->refresh();
onPopupHide_.disconnect();
// (re)connect in order to use the actual mouse event coordinates
onPopupHide_ = popup_->aboutToHide().connect(boost::bind(&TestPopup::popupAction,this,e));
}
else {
popup_->hide();
}
}

void TestPopup::onMouseUp(const Wt::WMouseEvent &e) {
switch (e.button()) {
case WMouseEvent::RightButton :
showPopup(e);
break;
default: break;
}
}

void GetDialog(){
WDialog dialog("Dialog");
WPushButton *btn = new WPushButton("close");
btn->clicked().connect(&dialog,&WDialog::reject);
dialog.contents()->addWidget(btn);
dialog.rejectWhenEscapePressed(true);
dialog.exec();
}


Wt::WApplication* WMyApp(const Wt::WEnvironment& env) {
WApplication *myApp = new WApplication(env);
myApp->setCssTheme("polished");
WContainerWidget *w = myApp->root();
w->setPadding(0);
WContainerWidget *page_ = new TestPopup();
page_->resize(1000,100);
WPushButton *dialogBtn = new WPushButton("dialog",w);
dialogBtn->clicked().connect(boost::bind(&GetDialog));
w->addWidget(page_);
return myApp;
}

int main(int argc, char** argv) {
return WRun(argc,argv,&WMyApp);
}

    (1-1/1)