GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Sep 01, 2014 1:44 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Gtk_message_type and icon
PostPosted: Wed Feb 01, 2012 1:11 pm 
Offline
Familiar Face

Joined: Fri Jul 01, 2011 6:40 pm
Posts: 48
Location: Croatia
Hello,
How can I get error icon on following example of dialog created with code?
For now, dialog pops but icon is missing.

Code:
#include <stdlib.h>
#include <gtk/gtk.h>

GtkWidget *win;
GtkWidget *vbox;
GtkWidget *button;

static void helloDialog (GtkWidget *wid, GtkWidget *win, gpointer user_data)
{
    char* message = "Shit happens\nalmost all the time.";
    GtkWidget *dialog, *label, *content_area;
    dialog = gtk_dialog_new_with_buttons ("May be error",
                                          GTK_WINDOW(win),
                                          GTK_DIALOG_DESTROY_WITH_PARENT|GTK_MESSAGE_ERROR,
                                          "Maybe", 1,
                                          "Possible", 2,
                                          NULL);

    gtk_widget_set_size_request(dialog, 350, 200);
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), 2);
    content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
    label = gtk_label_new(message);
    g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
    gtk_container_add(GTK_CONTAINER(content_area), label);
    gtk_widget_show_all(dialog);
}

int main (int argc, char *argv[])
{
    /* Initialize GTK+ */
    g_log_set_handler("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
    gtk_init(&argc, &argv);
    g_log_set_handler("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

    /* Create the main window */
    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win), 8);
    gtk_window_set_title(GTK_WINDOW(win), "Hello");
    gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win);
    g_signal_connect(win, "destroy", gtk_main_quit, NULL);

    /* Create a vertical box with buttons */
    vbox = gtk_vbox_new(TRUE, 6);
    gtk_container_add(GTK_CONTAINER (win), vbox);

    button = gtk_button_new_from_stock(GTK_STOCK_DIALOG_INFO);
    g_signal_connect(G_OBJECT (button), "clicked", G_CALLBACK(helloDialog), (gpointer)win);
    gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);

    button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
    g_signal_connect(button, "clicked", gtk_main_quit, NULL);
    gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);

    /* Enter the main loop */
    gtk_widget_show_all(win);
    gtk_main();
    return 0;
}


Thanks, nime.


Top
 Profile  
 
 Post subject: Re: Gtk_message_type and icon
PostPosted: Wed Feb 01, 2012 6:54 pm 
Offline
Never Seen the Sunlight

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

You are using gtk_dialog_new_with_buttons() incorrectly and it will not give you the result you want. You will need to use a higher level dialog window called a GtkMessageDialog (this has a built in icon and text area) unless you want to manually display the icon yourself like what you have done with the GtkLabel text and have full control. You may also want to consider making the dialog modal.

Below is a corrected version of the call-back function using the GtkMessageDialog widget.

Code:
static void helloDialog (GtkWidget *wid, GtkWidget *win)
{
    char* message = "Things happen\nalmost all the time.";
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new(
        GTK_WINDOW(win),
        GTK_DIALOG_DESTROY_WITH_PARENT|GTK_DIALOG_MODAL,
        GTK_MESSAGE_ERROR,
        GTK_BUTTONS_NONE,
        message
    );
    gtk_dialog_add_buttons(GTK_DIALOG(dialog),
        "Maybe", 1,
        "Possible", 2,
        NULL
    );

    gtk_widget_set_size_request(dialog, 350, 200);
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), 2);
    g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
    gtk_widget_show_all(dialog);
}

_________________
E.


Top
 Profile  
 
 Post subject: Re: Gtk_message_type and icon
PostPosted: Wed Feb 01, 2012 8:50 pm 
Offline
Familiar Face

Joined: Fri Jul 01, 2011 6:40 pm
Posts: 48
Location: Croatia
Hi errol,
This days working much on dialogs so now I'm totally confused.
Thank you for the "tips".

Regarding "things which happens" I have to add format specifier to get dialog to work:
Code:
    dialog = gtk_message_dialog_new
             (GTK_WINDOW(win),
              GTK_DIALOG_DESTROY_WITH_PARENT,
              GTK_MESSAGE_ERROR,
              GTK_BUTTONS_NONE,
              "%s", message);

... but I luckily know for that from before.

nime


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 2 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