How to corrent internal path handling?
Added by ioann sys about 9 years ago
#include <Wt/WApplication>
#include <Wt/WStackedWidget>
#include <Wt/WContainerWidget>
#include <Wt/WEnvironment>
#include <Wt/WImage>
#include <iostream>
using namespace std;
using namespace Wt;
class WebApp : public WApplication
{
public:
WebApp(const WEnvironment&);
void path_handler();
// path helpers
void change_path_to_entry(void) { setInternalPath("/entry"); }
void change_path_to_login(void) { setInternalPath("/login"); }
private:
WStackedWidget *content;
WContainerWidget *page_entry, *page_login;
};
// Constructor
WebApp::WebApp(const WEnvironment &env) : WApplication(env)
{
setTitle("demo");
internalPathChanged().connect(this, &WebApp::path_handler);
content = new WStackedWidget(this->root());
setInternalPath("/");
// entry page
WImage *img_entry = new WImage("/resources/login.png", "LOGIN");
img_entry->clicked().connect(this, &WebApp::change_path_to_login);
// login page
WImage *img_login = new WImage("/resources/entry.png", "ENTRY");
img_login->clicked().connect(this, &WebApp::change_path_to_entry);
page_entry = new WContainerWidget;
page_entry->addWidget(img_login);
page_login = new WContainerWidget;
page_login->addWidget(img_entry);
content->addWidget(page_entry);
content->addWidget(page_login);
content->setCurrentWidget(page_entry);
}
void WebApp::path_handler()
{
string current_path = internalPath();
if(current_path == "/login")
content->setCurrentWidget(page_login);
if(current_path == "/entry")
content->setCurrentWidget(page_entry);
cout << "INTERNAL PATH HAS BEEN CHANGED!" << endl;
}
WApplication *create_app(const WEnvironment &env)
{
return new WebApp(env);
}
int main(int argc, char **argv)
{
return WRun(argc, argv, &create_app);
}
In console log output, i can not find my message "INTERNAL PATH HAS BEEN CHANGED!".
Replies (3)
RE: How to corrent internal path handling? - Added by Koen Deforche about 9 years ago
Hey,
You would get it by calling setInternalPath(path, true). But what you really want is this:
WImage *img_entry = new WImage("/resources/login.png", "LOGIN");
WAnchor *a_entry = new WAnchor(WLink(WLink::InternalPath, "/login"), img_entry);
page_login->addWidget(img_login);
It's shorter, but also better, since it allows a user for example to also open the link in a new tab, at least if you replace the 'setInternalPath("/");' with a 'path_handler()' call at the end of the constructor.
Koen
RE: How to corrent internal path handling? - Added by ioann sys about 9 years ago
Koen, thank you for answer!
My problem in function setInternalPath
whith second bool arg. Now it work for me. I try your way and used WAnchor, but my image can not been clickable
// title and theme setup
// Constructor
setTitle(settings.get(SETTING[APP_NAME]));
this->setTheme(new WBootstrapTheme());
// create content
page = new WContainerWidget(this->root());
_content = new WStackedWidget(page);
page_entry = new WContainerWidget;
this->setInternalPath("/entry");
print_h(page_entry, settings.get(SETTING[APP_NAME]), 1);
WImage *img = new WImage("/resources/images/login.png", "Press to login");
img->setId("image_button_login");
img->setToolTip("Press to login");
WAnchor *anch1 = new WAnchor(WLink(WLink::InternalPath, "/login"), img);
page_entry->addWidget(img);
page_entry->setContentAlignment(AlignCenter | AlignMiddle);
_content->addWidget(page_entry);
_content->setCurrentWidget(page_entry);
this->internalPathChanged().connect(this, &_c_web_app::path_handle);
So, my image-button "login.png" not clickable.
s.: sorry for my bad English
RE: How to corrent internal path handling? - Added by ioann sys about 9 years ago
I try next:
WImage *img = new WImage("login.png", "LOGIN");
WAnchor *anch1 = new WAnchor(WLink(WLink::internalPath, "/login"));
anch1->setImage(img);
page_entry-addWidget(anch1);
now it work for me. Koen, big thank you!