GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Sep 24, 2014 4:28 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Simple Dialogs in C/GTK+ (w/libglade) using GtkMessageDialog
PostPosted: Fri Mar 10, 2006 1:08 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Image

glade file and source files here: dialogs1.tar.gz

Uses libglade for the main window but the dialogs are implemented with code. Just shows the 4 types of GtkMessageDialog.

Compiles on my machine (FC4) using:
Code:
gcc -o dialogs -Wall -g main.c `pkg-config gtk+-2.0 --cflags --libs `-I/usr/include/libglade-2.0 -lglade-2.0


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

/* glade interface file is in same path as the executable */
#define GLADE_FILE "gui.glade"
     
/* signal callbacks */
void on_window1_destroy (GtkWidget *widget, gpointer user_data);
void on_quit_button_clicked (GtkWidget *widget, gpointer user_data);
void on_info_button_clicked (GtkWidget *widget, gpointer user_data);
void on_warning_button_clicked (GtkWidget *widget, gpointer user_data);
void on_error_button_clicked (GtkWidget *widget, gpointer user_data);
void on_question_button_clicked (GtkWidget *widget, gpointer user_data);

/* functions */
GtkResponseType message_dialog( GtkMessageType type,
                                GtkWindow *parent,
                                const gchar *title,
                                const gchar *message);
               
int
main (int argc, char *argv[])
{
       
        GtkWidget *main_window; 
        GladeXML *gxml;
       
        /* initialize the GTK+ library */
        gtk_init (&argc, &argv);

        /*
        create an instance of the GladeXML object and build widgets within
        the window1 root node.
        */
        gxml = glade_xml_new (GLADE_FILE, NULL, NULL);
       
        /* get the window widget from the glade XML file */
        main_window = glade_xml_get_widget (gxml, "window1");
       
        /* connect signals */
        glade_xml_signal_connect (gxml, "on_window1_destroy",
                G_CALLBACK(on_window1_destroy));
       
        glade_xml_signal_connect (gxml, "on_quit_button_clicked",
                G_CALLBACK(on_quit_button_clicked));
               
        glade_xml_signal_connect_data (gxml, "on_info_button_clicked",
                G_CALLBACK(on_info_button_clicked), main_window);

        glade_xml_signal_connect_data (gxml, "on_warning_button_clicked",
                G_CALLBACK(on_warning_button_clicked), main_window);
               
        glade_xml_signal_connect_data (gxml, "on_error_button_clicked",
                G_CALLBACK(on_error_button_clicked), main_window);

        glade_xml_signal_connect_data (gxml, "on_question_button_clicked",
                G_CALLBACK(on_question_button_clicked), main_window);
               
               
        /* show the main window */
        gtk_widget_show (main_window);

        /* begin main GTK loop */
        gtk_main ();
       
        return 0;     
}

void
on_window1_destroy (GtkWidget *widget, gpointer user_data)
{
        /* break gtk_main() loop */
        gtk_main_quit();                       
}

void
on_quit_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /* break gtk_main() loop */
        gtk_main_quit();                       
}

void
on_info_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        Show message dialog.  We're casting the user_data into a GtkWindow
        because we passed window1 into the user_data when we connected the
        signals in main().  So window1 is going to the the "parent" for the
        dialog.
        */
        message_dialog(GTK_MESSAGE_INFO,
                        GTK_WINDOW(user_data),
                        "Information",
                        "Did you know that it snowed today in Portland?");                     
}

void
on_warning_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        Show message dialog.  We're casting the user_data into a GtkWindow
        because we passed window1 into the user_data when we connected the
        signals in main().  So window1 is going to the the "parent" for the
        dialog.
        */
        message_dialog(GTK_MESSAGE_WARNING,
                        GTK_WINDOW(user_data),
                        "Warning",
                        "I'm warning you for the last time!");                 
}

void
on_error_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        Show message dialog.  We're casting the user_data into a GtkWindow
        because we passed window1 into the user_data when we connected the
        signals in main().  So window1 is going to the the "parent" for the
        dialog.
        */
        message_dialog(GTK_MESSAGE_ERROR,
                        GTK_WINDOW(user_data),
                        "Error!",
                        "BOOM! You messed up bad this time buddy!");
                       
}

void
on_question_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        Show message dialog.  We're casting the user_data into a GtkWindow
        because we passed window1 into the user_data when we connected the
        signals in main().  So window1 is going to the the "parent" for the
        dialog.
        */
       
        GtkResponseType answer;
       
        answer = message_dialog(GTK_MESSAGE_QUESTION,
                        GTK_WINDOW(user_data),
                        "Question",
                        "Do you like GTK+ programming?");
                       
        if (answer == GTK_RESPONSE_YES)
        {
                message_dialog(GTK_MESSAGE_INFO,
                                GTK_WINDOW(user_data),
                                "Good!",
                                "Darn right you do!");
        }
        else if (answer == GTK_RESPONSE_NO)
        {
                message_dialog(GTK_MESSAGE_ERROR,
                                GTK_WINDOW(user_data),
                                "Uh Oh!",
                                "That's a horrible answer!");
        }
        else
        {
                message_dialog(GTK_MESSAGE_WARNING,
                                GTK_WINDOW(user_data),
                                "Hey!",
                                "Don't you ignore my question!");
        }
                       
}

GtkResponseType
message_dialog(GtkMessageType type,
                GtkWindow *parent,
                const gchar *title,
                const gchar *message)
{
        /*
        This function is used for all 4 types of GtkMessageDialog. See the API
        documentation for GtkMessageDialog for further information.
       
                type - 1 of 4 enum values for the type of message dialog.
                parent - The parent window for the dialog (window1)
                title - A string for the dialog box title
                message - A string to display in the dialog box
        */
       
        GtkWidget *dialog;
        GtkResponseType response;
       GtkButtonsType buttons;

        if (type == GTK_MESSAGE_QUESTION) buttons = GTK_BUTTONS_YES_NO;
        else buttons = GTK_BUTTONS_OK;
       
       /* create the dialog */
       dialog = gtk_message_dialog_new (parent,
                    GTK_DIALOG_DESTROY_WITH_PARENT,
                          type,
                        buttons,
                        message);
                       
        /* set title */
        gtk_window_set_title(GTK_WINDOW(dialog), title);
       
        /* show dialog */
   response = gtk_dialog_run (GTK_DIALOG (dialog));
   gtk_widget_destroy (dialog);   
       
        return response;
}

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 09, 2007 11:32 am 
Offline
Familiar Face

Joined: Sun Mar 04, 2007 8:25 pm
Posts: 19
Hello,

I'm getting a problem compiling this program dialogs1.tar.gz.

The error is:
Code:
c:\program files\libglade\glade\glade-xml.h(26) : fatal error C1083: Cannot open include file: 'gtk/gtkdata.h': No such file or directory


I'm using Visual C++. I don't have gtkdata.h I think.

Can you please tell me everything (include files, etc) that I need in order to get this program running smoothly and also where can I get them?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 29, 2007 6:47 am 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
You need
#include <gtk/gtk.h>
#include <glade/glade.h>

It looks like libglade can't find the gtk libs.

Im not sure how you would place the gtk+ libs in your path on ms windows.

Just go here http://gimp-win.sourceforge.net/stable.html and download the gtk libs
and install them.

And here is where you will find libglade
http://gladewin32.sourceforge.net/modul ... .php?cid=3

Install the gtk libs first then libglade

Are you planing on doing some cross platform development using visual C++ because
im not sure if an application built using visual c++ will work on linux or mac you will have to
make sure that your c++ application is compatable with glibc.


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