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 Apr 21, 2014 4:39 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Font and Color Selection dialogs in C/GTK+ with libglade
PostPosted: Fri Mar 10, 2006 10:09 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Screenshots are here: http://www.micahcarrick.com/v2/content/view/30/20/

Simple little example of how to use GtkFontSelectionDialog and GtkColorSelectionDialog with C/GTK+ and libglade.

main.c and gui.glade can be downloaded here: http://www.micahcarrick.com/files/dialogs2/dialogs2.tar.gz

Code:
/*
* main.c
* Copyright (C) Micah Carrick 2006 <email@micahcarrick.com>
*
* main.c is free software.
*
* You may redistribute it and/or modify it under the terms of the
* GNU General Public License, as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* main.c is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with main.c.  If not, write to:
*    The Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor
*    Boston, MA  02110-1301, USA.
*/

/*
gcc -o dialogs2 -Wall -g main.c `pkg-config gtk+-2.0 --cflags --libs `-I/usr/include/libglade-2.0 -lglade-2.0
*/

#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_window_destroy (GtkWidget *widget, gpointer user_data);
void on_font_button_clicked (GtkWidget *widget, gpointer user_data);
void on_color_button_clicked (GtkWidget *widget, gpointer user_data);
               
int
main (int argc, char *argv[])
{
       
        GtkWidget *window; 
        GtkWidget *label;
        GladeXML *gxml;
       
        /* initialize the GTK+ library */
        gtk_init (&argc, &argv);

        /*
        create an instance of the GladeXML object and build widgets within
        the "window" root node.  We don't want to build the other nodes (the
        two dialogs) until/unless we need them.
        */
        gxml = glade_xml_new (GLADE_FILE, "window", NULL);
       
        /* get the window & label widgets from the glade XML file */
        window = glade_xml_get_widget(gxml, "window");
        label = glade_xml_get_widget(gxml, "website_label");
       
        /* connect destroy signal */
        glade_xml_signal_connect (gxml, "on_window_destroy",
                G_CALLBACK(on_window_destroy));
       
        /* connect button signals and pass reference to label */
        glade_xml_signal_connect_data (gxml, "on_color_button_clicked",
                G_CALLBACK(on_color_button_clicked), label);
               
        glade_xml_signal_connect_data (gxml, "on_font_button_clicked",
                G_CALLBACK(on_font_button_clicked), label);

                       
        /* show the main window */
        gtk_widget_show (window);

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

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

void
on_font_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        The font dialog is inside the glade file.  We build the widgets in
        the glade file starting with "font_dialog" as the root node so as not
        to re-build the window1 and asscoiated widgets. 
        */
       
        GladeXML *gxml = glade_xml_new (GLADE_FILE, "font_dialog", NULL);
        GtkWidget *dialog = glade_xml_get_widget(gxml, "font_dialog");
        GtkResponseType result;
       
        result = gtk_dialog_run(GTK_DIALOG(dialog));

        if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY)
        {
                /* If user clicks "Ok" or "Apply" we will set the font */
               
                PangoFontDescription *font_desc;
                gchar *font_name = gtk_font_selection_dialog_get_font_name(
                                        GTK_FONT_SELECTION_DIALOG(dialog));
               
                font_desc = pango_font_description_from_string (font_name);
               
                /* assign font to the label widget which is passed in user_data */
                gtk_widget_modify_font (GTK_WIDGET(user_data), font_desc);
               
                /* free memory used by font name string */
                g_free(font_name);
        }
       
        /* destroy widget */
        gtk_widget_destroy(dialog);
}

void
on_color_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        The color dialog is inside the glade file.  We build the widgets in
        the glade file starting with "color_dialog" as the root node so as not
        to re-build the window1 and asscoiated widgets. 
        */
       
        GladeXML *gxml = glade_xml_new (GLADE_FILE, "color_dialog", NULL);
        GtkWidget *dialog = glade_xml_get_widget(gxml, "color_dialog");
        GtkResponseType result;
       
        result = gtk_dialog_run(GTK_DIALOG(dialog));

        if (result == GTK_RESPONSE_OK)
        {
                /*
                Get the GtkColorSelection widget which is part of the
                GtkColorSelectionDialog named "color_select" in the glade file.
                */
                GdkColor color;
                GtkWidget *color_select = glade_xml_get_widget(
                                gxml,
                                "color_select");
                               
                gtk_color_selection_get_current_color(
                                GTK_COLOR_SELECTION(color_select),
                                &color);
               
                /* modify the foreground color of the lable widget */
                gtk_widget_modify_fg (GTK_WIDGET(user_data),
                                GTK_STATE_NORMAL,
                                &color);
               
        }
       
        /* destroy widget */
        gtk_widget_destroy(dialog);                         
}


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


Top
 Profile  
 
 Post subject: very nice
PostPosted: Fri Oct 06, 2006 6:39 am 
Offline
GTK+ Geek

Joined: Fri Oct 06, 2006 6:37 am
Posts: 57
iam new to gtk
iam getting thsese warning
while compling this coe
they are
(dialogs2:4413): libglade-WARNING **: unknown property `focus_on_map' for class `GtkWindow'

(dialogs2:4413): libglade-WARNING **: unknown property `ellipsize' for class `GtkLabel'

(dialogs2:4413): libglade-WARNING **: unknown property `width_chars' for class `GtkLabel'

(dialogs2:4413): libglade-WARNING **: unknown property `single_line_mode' for class `GtkLabel'

(dialogs2:4413): libglade-WARNING **: unknown property `angle' for class `GtkLabel'


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 06, 2006 4:25 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
What command did you use to compile and what Linux distro are you running?

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


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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group