GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Dec 20, 2014 11:31 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: invisible parent window/filechooser only
PostPosted: Sun Aug 25, 2013 7:16 am 
Offline

Joined: Sun Aug 25, 2013 6:18 am
Posts: 2
I am trying to use gtkmm 3.0 to provide an open/save file dialog. Otherwise my app uses SDL most things.
The problem that I am trying to over come is that when I create a file chooser dialog and click open/save the dialog window essentually hangs.
it returns the right values, and I am able to extract the file selected however the windows stays on the screen but doesn not get updated and wont close untill
the program terminates. as far as I can tell, this behavior is releated to the fact that the dialog box has no parent window.
So I think I need a way to create a parent window that isnt visisble to the user (I dont need gtk to create a windows because my windows are all handled by sdl).

here is some sample code that has a freezing dialog box (its largely copied from examples)

Code:
#include <gtkmm.h>
#include <iostream>
class myDialog
{
    public:
        myDialog() {};
        bool run();
        std::string getFile() {return dirName;};
        std::string getDir() {return fileName;};
        myDialog(std::string name, Gtk::FileChooserAction action=Gtk::FILE_CHOOSER_ACTION_OPEN);
        ~myDialog();
    private:
        Gtk::FileChooserDialog *dialog;
        std::string fileName,dirName;

};


int main( int   argc,
          char *argv[] )
{   
    Glib::RefPtr<Gtk::Application> app =Gtk::Application::create(argc, argv,"org.gtkmm.examples.base");
    myDialog dig("openINI");
    dig.run();
    //at this point the file chooser dialog should no longer be drawn
    std::cin.get();
    return 0;
}


myDialog::myDialog(std::string name, Gtk::FileChooserAction action)
{
    dialog = new Gtk::FileChooserDialog(name.c_str(),action);
    dialog->add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
    dialog->add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
}
myDialog::~myDialog()
{
    delete dialog;
}

bool myDialog::run()
{
//Show the dialog and wait for a user response:
  int result = dialog->run();
  //Handle the response:
  switch(result)
  {
    case(Gtk::RESPONSE_OK):
    {
      std::cout << "Open clicked." << std::endl;
      //Notice that this is a std::string, not a Glib::ustring.
      fileName = dialog->get_filename();
      dirName = dialog->get_current_folder();
      std::cout << "File selected: " <<  fileName << std::endl;
      return true;
      break;
    }
    case(Gtk::RESPONSE_CANCEL):
    {
      std::cout << "Cancel clicked." << std::endl;
      return false;
      break;
    }
    default:
    {
      std::cout << "Unexpected button clicked." << std::endl;
      return false;
      break;
    }
  }
}


any help/suggestions would be much appricated, thanks


Top
 Profile  
 
 Post subject: Re: invisible parent window/filechooser only
PostPosted: Wed Aug 28, 2013 8:23 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Hello and welcome,

You have two problems with your code.

- In your main function you create the file chooser dialog on the stack. This is never hidden or deleted at any point except on application exit, which is not what you want. GTK keeps the dialog open and displayed as you can carry on interacting with it if you choose to. To correct this I put the creation and running of the file chooser dialog in its own scope so that the C++ instance is deleted when the running of the code exits that scope.

- GTK is event driven. To improve efficiency requests to delete windows, do redraws and many other things are queued as events (repeated events are ignored, redraws optimised etc.). Normally control would be returned to the GTK main loop and these events processed as normal. In your case you are using SDL and without knowing how you are interacting with the user I have used a small loop which processes the events. All it does is check that there is any pending events and if there is process it. This will complete the deletion of the file chooser dialog and redraw the screen.

Your main function should now read as follows.
Code:
int main( int   argc,
          char *argv[] )
{   
    Glib::RefPtr<Gtk::Application> app =Gtk::Application::create(argc, argv,"org.gtkmm.examples.base");
    {
    // Create the chooser dialog and run in this scope
        myDialog dig("openINI");
        dig.run();
    }
    // C++ chooser dialog is now out of scope and now deleted
    // Request has also been sent to GTK to delete the chooser dialog

    // GTK is event driven and redraws, deletions etc are events
    // So run through the pending events to redraw the screen and delete the dialog
    while( Gtk::Main::events_pending() )
        Gtk::Main::iteration();
    //at this point the file chooser dialog should no longer be drawn
    std::cin.get();
    return 0;
}

_________________
E.


Top
 Profile  
 
 Post subject: Re: invisible parent window/filechooser only
PostPosted: Wed Aug 28, 2013 8:46 am 
Offline

Joined: Sun Aug 25, 2013 6:18 am
Posts: 2
Thanks a lot. my program now works very nicely. I wrapped had it wrapped in another class so that I can just call run on my dialog class (and not worry about the switch statments for) and then just get what I need. however your explination made it very clear what I was missing, which was very help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Exabot [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group