GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Jul 24, 2014 10:42 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: GtkDialog with text entry (GTK+ | C++)
PostPosted: Mon Feb 18, 2013 2:19 pm 
Offline

Joined: Wed Sep 19, 2012 3:06 pm
Posts: 3
Hi gays, a novice on GTK+ programming and I need your help.

I wanna create dialog box with text entry and buttons "OK", "Cancel".
After click OK a need get entered string to entry otherwise NULL (ok blank stroke "").

Can anybody help?

Thanks in advance!


Top
 Profile  
 
 Post subject: Re: GtkDialog with text entry (GTK+ | C++)
PostPosted: Tue Feb 19, 2013 12:59 am 
Offline

Joined: Wed Sep 19, 2012 3:06 pm
Posts: 3
That's my realization:
Code:
void show_entry_dialog()
{
    GtkWidget *dialog;
    GtkWidget *entry;
    GtkWidget *content_area;

    dialog = gtk_dialog_new();
    gtk_dialog_add_button(GTK_DIALOG(dialog), "OK", 0);
    gtk_dialog_add_button(GTK_DIALOG(dialog), "CANCEL", 1);

    content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
    entry = gtk_entry_new();
    gtk_container_add(GTK_CONTAINER(content_area), entry);

    gtk_widget_show_all(dialog);
    gint result = gtk_dialog_run(GTK_DIALOG(dialog));

    string entry_line = ""; // variable for entered text

    switch(result)
    {
    case 0:
        cout << "OK was clicked\n";
        entry_line = gtk_entry_get_text(GTK_ENTRY(entry));

        // TODO: validation for entry_line
        if(entry_line.length() == 0 || entry_line == "")
        {
            cout << "Line is to short. Nothing to passing\n";
            break;
        }

        cout << "Contact dialog passed text from entry: "
                << entry_line << endl;

        contact_list_add_item(contactlist, entry_line.c_str());
        break;

    case 1:
        cout << "CANCEL was clicked\n"; break;
    default:
        cout << "Undefined. Perhaps dialog was closed\n"; break;
    }

    gtk_widget_destroy(dialog);
}


Top
 Profile  
 
 Post subject: Re: GtkDialog with text entry (GTK+ | C++)
PostPosted: Wed Feb 20, 2013 9:31 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
Hi,

This looks like it should work OK. If you are using C++ there is also the option of using the C++ bindings which may be useful if you prefer that way of programming. It is called gtkmm and you sample code would look like this if I used it
Code:
void show_entry_dialog()
{
    Gtk::Dialog dialog;
    Gtk::Entry entry;

    dialog.add_button(Gtk::StockID(Gtk::Stock::OK), Gtk::RESPONSE_OK);
    dialog.add_button(Gtk::StockID(Gtk::Stock::CANCEL), Gtk::RESPONSE_CANCEL);

    dialog.get_content_area()->add(entry);
    dialog.show_all();

    string entry_line; // variable for entered text

    switch(dialog.run())
    {
    case Gtk::RESPONSE_OK:
        std::cout << "OK was clicked\n";
        entry_line = entry.get_text();

        // TODO: validation for entry_line
        if(entry_line.length() == 0)
        {
            std::cout << "Line is to short. Nothing to passing\n";
            break;
        }

        std::cout << "Contact dialog passed text from entry: "
                << entry_line << endl;

        contact_list_add_item(contactlist, entry_line.c_str());
        break;

    case Gtk::RESPONSE_CANCEL:
        std::cout << "CANCEL was clicked\n"; break;
    default:
        std::cout << "Undefined. Perhaps dialog was closed\n"; break;
    }
}

_________________
E.


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: No registered users and 1 guest


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