How do I re-bind the content in a WStackedWidget to a WWidget?
I have created a smaller example of my question, its a modified wt-home
example, it has two menus, one Static, and one Dynamic, meaning you can
change the content based on the URL so I can bookmark it or combo box, I
know that once the menu is created, it will not change unless the object
is rebound to it, which is where my problem begins, I set contents = new
Wt::WStackedWidget(); to private so I can rebind to it from a call back,
but I am not sure how I delete the old object or bind the new object,
since its late bound to the stack via a call back from the Menu, so I do
not have a reference to it, only the base pointer to it, and the
navigation bar is also bound to it, and I don't want to delete it, just
the content bound to content by result->bindWidget("contents",
contents);, what puzzles me is do I bind it after the return from the
function Dynamically() or in it, and exactly what function do I use to
do this with?
I am totally lost at this point and really stuck in progress, so I left
good comments in the Code, and I hope I made myself clear this time, I
know this question is like 101 basics for Wt, but I don't get it.

Thanks, and thanks for the help on Popup, I never thought to bind to its
return value, I did not see any examples of this till now, and that's a
mistake I will remember.

On Wed, 2014-02-26 at 13:18 +0400, Dmitriy Igrishin wrote:
> 
> 
> 
> 
> 
> 2014-02-26 12:56 GMT+04:00 Dmitriy Igrishin <[email protected]>:
> 
>         
>         
>         
>         
>         
>         2014-02-26 12:06 GMT+04:00 Jeffrey Scott Flesher Gmail
>         <[email protected]>:
>         
>         
>         
>                 Thanks replying.
>                 
>                 This is my problem:
>                 I have a menu with several items, one is Video, 
>                 I added the Menu item like this:
>                 mainMenu_ = new Wt::WMenu(contentsStack, contents);
>                 mainMenu_->addItem(tr("video"),
>                 deferCreate(boost::bind(&Home::video, this)));
>                 in the video function:
>                 Wt::WWidget *Home::video()
>                 ...
>                 set the video up based on the path or combo boxes.
>                 This is a modified wt-home example. 
>                 where I have two WComboBox,Category and Video with a
>                 list of video's to play, I change the WComboBox:
>                 videoCombo->activated().connect(this,
>                 &Home::videoComboChanged);
>                 where videoComboChanged has:
>                 Wt::WApplication::instance()->setInternalPath("/video/" + 
> std::to_string(categoryCombo->currentIndex()) + "/" + 
> std::to_string(videoCombo->currentIndex()), true);
>                 which sets the path to /video/1/2 for example, but the
>                 menu item /video seems to be cached, since even
>                 clicking on a different menu item and back on video
>                 does not make the page refresh, in fact the player is
>                 at the same position, and continues to play even
>                 though I clicked on another menu item, which is not
>                 what I want either, I want it to stop, but that is
>                 another issue that needs to be addresses... How do I
>                 delete the object if the menu changes? Maybe this is
>                 what the problem is, if I delete the object, maybe it
>                 will recreate it, but I don't know the API call to do
>                 that, and could not find it, or an example of how to
>                 do this, you guys have a lot of examples, but they are
>                 far to simple, you need real life examples so people
>                 like me have a chance at figuring things like this
>                 out.
>                 
>                 I don't see this cached feature in the API, but I can
>                 see why you would want it for static content, but I
>                 need a way to override this behavior for dynamic
>                 content, and that's where I am stuck, how do you do
>                 that?
>                 
>                 my thought was that:
>                 mainMenu_->addItem(tr("video"),
>                 deferCreate(boost::bind(&Home::video, this)),
>                 WMenuItem::PreLoading);
>                 that maybe it was the default of LazyLoading that was
>                 doing this, but this did not work either, so I have no
>                 idea what this feature does (Enumeration that
>                 determines when contents should be loaded. on first
>                 use seems right, so this has nothing to do with my
>                 problem, but it seems it needs more options, like to
>                 destroy the object on menu change), calling the
>                 function directly is an ugly hack, but didn't work
>                 either.
>                 
>                 Yes /video?v=1 is what I wanted, but what I go was
>                 duplicates like /video?v=1&v=2&v=3, which is not what
>                 I wanted, but as you pointed out /video/1 is a pretty
>                 URL, I do listen to InternalPathChange, but in this
>                 case, I don't see how I can use it to override the
>                 cached behavior of the Menu, without an ugly hack of
>                 logic, when I am sure there is a way to override this
>                 behavior, I just don't see it in the API. 
>                 
>                 I am very old school, still want to use queries to do
>                 things when I don't need to, and still trying to learn
>                 this API, its like my problem with the Popup menu, I
>                 don't see a way to handle a call back, and still stuck
>                 on that problem.
>                 
>                 Your example for the media
>                 http://www.webtoolkit.eu/widgets/media and video
>                 http://www.webtoolkit.eu/widgets/media/wvideo player
>                 both keep playing after a video is started, this is
>                 not good behavior, very bad in fact, so maybe if you
>                 fix that example, you would see what I mean, you need
>                 a call back to either delete the object or stop the
>                 video, in my case deleting would be best, so at this
>                 point it looks like this is a feature behavior that
>                 needs to be fixed.
>                 
>         In your case you may want to write an internal path changed
>         handler like this:
>         void
>         Home::internalPathChangedHandler()
>         {
>           WApplication* app = WApplication::instance();
>           if (app->internalPathMatches("/video/")) {
>         
>             std::string videoNumber =
>         app->internalPathNextPart("/video/")
>             if (!video.empty())
>               setCurrentVideo(videoNumber);
>         }
>         
>         
>         
>         setCurrentVideo() here is intended to replace the video in the
>         widget that
>         created by WWidget* Home::video() of your code.
> Ah, and don't forget to attach this handler to
> WApplication::internalPathChanged
> signal.
> And, I am sorry, I meant !videoNumber.empty() rather than !
> video.empty()
> in the previous post.
> 
> 
> 
> 
> 
> 
> -- 
> // Dmitriy.
> 
> 
> 
> ------------------------------------------------------------------------------
> Flow-based real-time traffic analytics software. Cisco certified tool.
> Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer
> Customize your own dashboards, set traffic alerts and generate reports.
> Network behavioral analysis & security monitoring. All-in-one tool.
> http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk
> _______________________________________________
> witty-interest mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/witty-interest
#include <iostream>
#include <fstream>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <Wt/WApplication>
#include <Wt/WMenu>
#include <Wt/WMenuItem>
#include <Wt/WPopupMenu>
#include <Wt/WStackedWidget>
#include <Wt/WText>
#include <Wt/WBootstrapTheme>
#include <Wt/WTemplate>
#include <Wt/WNavigationBar>
#include <Wt/WComboBox>
#include <Wt/WVideo>
#include <Wt/WImage>
#include <Wt/WLineEdit>
/* ****************************************************************************
 * DeferredWidget
 * A utility container widget which defers creation of its single
 * child widget until the container is loaded (which is done on-demand by a WMenu).
 * The constructor takes the create function for the widget as a parameter.
 * We use this to defer widget creation until needed.
 */
template <typename Function>
class DeferredWidget : public Wt::WContainerWidget
{
    public:
        DeferredWidget(Function f) : f_(f) { }
    private:
        void load() { Wt::WContainerWidget::load(); if (count() == 0) { addWidget(f_()); } }
        Function f_;
};
template <typename Function>
DeferredWidget<Function> *deferCreate(Function f)
{
    return new DeferredWidget<Function>(f);
}
/* ****************************************************************************
 * App
 */
class App : public Wt::WApplication
{
    public:
        App(const Wt::WEnvironment& e);
    protected:
        Wt::WString tr(const char *key);
    private:
        Wt::WWidget *homePage_;
        void create_menu();
        Wt::WWidget *Statically();
        Wt::WWidget *Dynamically();
        //
        void logInternalPath(const std::string& path);
        void categoryComboChanged();
        void messageComboChanged();
        //
        Wt::WComboBox *categoryCombo;
        Wt::WComboBox *messageCombo;
        Wt::WStackedWidget *contents;
        //
        bool isComboBoxInit=false;
        bool bindMessage = false;
        bool isChanged=false;
};
/* ****************************************************************************
 * App Contructor
 */
App::App(const Wt::WEnvironment& e) : Wt::WApplication(e)
{
    messageResourceBundle().use(appRoot() + "dynamic-menu", false);
    setTheme(new Wt::WBootstrapTheme());
    internalPathChanged().connect(this, &App::logInternalPath);
    create_menu();
}
/* ****************************************************************************
 * create_menu
 */
void App::create_menu()
{
    Wt::WTemplate *result = new Wt::WTemplate(tr("template"), root()); //  <message id="template">
    homePage_ = result;
    // Create private contents
    contents = new Wt::WStackedWidget();
    Wt::WAnimation fade(Wt::WAnimation::Fade, Wt::WAnimation::Linear, 250);
    contents->setTransitionAnimation(fade);
    contents->setId("main_page");
    // Create a navigation bar with a link to a web page.
    Wt::WNavigationBar *navigation = new Wt::WNavigationBar(contents);
    navigation->setTitle("Witty Wizard");
    // Create contentsStack
    Wt::WStackedWidget *contentsStack = new Wt::WStackedWidget(contents);
    //contentsStack->setId("contents");
    contentsStack->addStyleClass("contents");
    // Setup a Main menu.
    Wt::WMenu *mainMenu_ = new Wt::WMenu(contentsStack, contents);
    mainMenu_->setId("mainmenu");
    navigation->addMenu(mainMenu_);
    // Add two menu items for example
    mainMenu_->addItem(tr("static"),  Statically())->setPathComponent("");
    mainMenu_->addItem(tr("dynamic"),  deferCreate(boost::bind(&App::Dynamically, this)));
    // add contents to contentsStack
    contents->addWidget(contentsStack);
    // Make the menu is internal-path aware.
    mainMenu_->setInternalPathEnabled("/");
    // Bind to Template
    result->bindWidget("menu", navigation);
    result->bindWidget("contents", contents);
}
/* ****************************************************************************
 * Dynamically based on URL or Comoboxes
 * I want to be able to bookmark a message based on Category and Message ID
 * I want URL pattern /dynamic/categoryNumber/messageNumber to control Combo box
 */
Wt::WWidget *App::Dynamically()
{
    Wt::log("notice") << "( *** run function Dynamically *** " << ")";
    std::string message_ = "default-0";
    Wt::WContainerWidget *result = new Wt::WContainerWidget();
    // Check to see if Combo Box has been initalized
    if (!isComboBoxInit)
    {
        categoryCombo = new Wt::WComboBox(result);
        categoryCombo->addItem("Default");
        categoryCombo->addItem("Extra");
        categoryCombo->setCurrentIndex(0);
        categoryCombo->activated().connect(this, &App::categoryComboChanged);

        messageCombo = new Wt::WComboBox(result);
        messageCombo->addItem("Default 0");
        messageCombo->addItem("Default 1");
        messageCombo->addItem("Default 2");
        messageCombo->setCurrentIndex(0); // Trailer
        messageCombo->activated().connect(this, &App::messageComboChanged);
        isComboBoxInit = true;
    }
    // Check internal Path and Set Combo Box
    WApplication* app = WApplication::instance();
    std::string categoryNumber = app->internalPathNextPart("/dynamic/");
    if (!categoryNumber.empty())
    {
        if (categoryCombo->currentIndex() != std::stoi(categoryNumber))
        {
            // FIX add boundry test
            isChanged = true; // Prevent Combo Box from updating internalPath
            Wt::log("notice") << "(categoryCombo: " << categoryNumber << ")";
            categoryCombo->setCurrentIndex(std::stoi(categoryNumber));
            categoryComboChanged(); // Rebuild list in combo box
        }
        std::string messageNumber = app->internalPathNextPart("/dynamic/" + categoryNumber + "/");
        if (!messageNumber.empty())
        {
            if (messageCombo->currentIndex() != std::stoi(messageNumber))
            {
                // FIX add boundry test
                isChanged = true; // Prevent Combo Box from updating internalPath
                Wt::log("notice") << "(messageCombo: " << messageNumber << ")";
                messageCombo->setCurrentIndex(std::stoi(messageNumber));
            }
        }
        isChanged = false; // Allow Combo Box to update internalPath
    }
    // Set content based on Combo boxes, based on URL internal path /dynamic/categoryNumber/messageNumber if set
    if (categoryCombo->currentIndex() == 0)
    {
        switch (messageCombo->currentIndex())
        {
            case 0:
                message_ = "default-0";
                break;
            case 1:
                message_ = "default-1";
                break;
            case 2:
                message_ = "default-2";
                break;
        }
    }
    else if (categoryCombo->currentIndex() == 1)
    {
        switch (messageCombo->currentIndex())
        {
            case 0:
                message_ = "extra-0";
                break;
            case 1:
                message_ = "extra-1";
                break;
            case 2:
                message_ = "extra-2";
                break;
            case 3:
                message_ = "extra-3";
                break;
        }
    }
    // Dynamic content based on message ID
    new Wt::WText(Wt::WString::tr(message_), result);
    /* It seems that I need a referance to bind to contents in the stack
     * so I made Wt::WStackedWidget *contents private
     * or do I do this after return?
     */
    if (bindMessage)
    {
        bindMessage = false;
        // result->bindWidget("contents", contents);
    }
    // If called from menu, this will bind with contents in create_menu result->bindWidget("contents", contents);
    return result;
}
/* ****************************************************************************
 * categoryComboChanged
 * Repopulate messageCombo based on categoryCombo
 */
void App::categoryComboChanged()
{
    messageCombo->clear();
    messageCombo->setCurrentIndex(0);

    switch (categoryCombo->currentIndex())
    {
        case 0:
            messageCombo->addItem("Default 0");
            messageCombo->addItem("Default 1");
            messageCombo->addItem("Default 2");
            break;
        case 1:
            messageCombo->addItem("Extra 0");
            messageCombo->addItem("Extra 1");
            messageCombo->addItem("Extra 2");
            messageCombo->addItem("Extra 3");
            break;
    }
    // Make sure we want to update URL
    if (!isChanged)
    {
        Wt::log("notice") << "(categoryComboChanged: " << std::to_string(categoryCombo->currentIndex()) << ")";
        Wt::WApplication::instance()->setInternalPath("/dynamic/" + std::to_string(categoryCombo->currentIndex()) + "/" + std::to_string(messageCombo->currentIndex()), true);
    }
}
/* ****************************************************************************
 * messageComboChanged
 */
void App::messageComboChanged()
{
    // Make sure we want to update URL
    if (!isChanged)
    {
        Wt::log("notice") << "(messageComboChanged: " << std::to_string(messageCombo->currentIndex()) << ")";
        Wt::WApplication::instance()->setInternalPath("/dynamic/" + std::to_string(categoryCombo->currentIndex()) + "/" + std::to_string(messageCombo->currentIndex()), true);
    }
}
/* ****************************************************************************
 * Statically
 */
Wt::WWidget *App::Statically()
{
    return new Wt::WText(tr("static.intro"));
}
/* ****************************************************************************
 * logInternalPath
 * This is a generic path change hook, I used it to to see if I need to update dynamic content
 */
void App::logInternalPath(const std::string& path)
{
    // simulate an access log for the interal paths
    log("Path Change") << path;

    // As far as I can tell, this would be the same if I did it in combobox change event
    Wt::log("notice") << "(logInternalPath: " << ")";
    WApplication* app = WApplication::instance();
    if (app->internalPathMatches("/dynamic/"))
    {
        std::string categoryNumber = app->internalPathNextPart("/dynamic/");
        if (!categoryNumber.empty())
        {
            std::string messageNumber = app->internalPathNextPart("/dynamic/" + categoryNumber + "/");
            if (!messageNumber.empty())
            {
                Wt::log("notice") << "(logInternalPath: " << categoryNumber + "/" + messageNumber << ")";
                bindMessage = true;
                /* We call Dynamically which returns a WWidget,
                 * do I bind it to something here?
                 */
                Dynamically();
            }
        }
    }
}
/* ****************************************************************************
 * tr
 */
Wt::WString App::tr(const char *key)
{
    return Wt::WString::tr(key);
}
/* ****************************************************************************
 * create_app
 */
Wt::WApplication* create_app(const Wt::WEnvironment& env)
{
    return new App(env);
}
/* ****************************************************************************
 * main
 */
int main(int argc, char* argv[])
{
    return Wt::WRun(argc, argv, create_app);
}
// --- End of File ------------------------------------------------------------

Attachment: dynamic-menu.xml
Description: XML document

------------------------------------------------------------------------------
Flow-based real-time traffic analytics software. Cisco certified tool.
Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer
Customize your own dashboards, set traffic alerts and generate reports.
Network behavioral analysis & security monitoring. All-in-one tool.
http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to