Commit ee3d1376 authored by GMT 1999 Tony Gale's avatar GMT 1999 Tony Gale Committed by Tony Gale
Browse files

threads example from Erik Mouw. New question on GtkLabel background

Sat Nov 13 22:30:29 GMT 1999 Tony Gale  <gale@gtk.org>

        * docs/gtkfaq.sgml: threads example from Erik Mouw.
        New question on GtkLabel background colors.

        * docs/gtk_tut.sgml:
          - Correct the example code callback
            function definitions.
          - Update the gtkdial example code, from Frans van Schaik.
          - Update setselection.c to current API.

        * examples/Makefile examples/*/*.c: Update to code
        listed in tutorial.
parent e4df9fa9
Sat Nov 13 22:30:29 GMT 1999 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: threads example from Erik Mouw.
New question on GtkLabel background colors.
* docs/gtk_tut.sgml:
- Correct the example code callback
function definitions.
- Update the gtkdial example code, from Frans van Schaik.
- Update setselection.c to current API.
* examples/Makefile examples/*/*.c: Update to code
listed in tutorial.
1999-11-10 Tor Lillqvist <tml@iki.fi>
* gdk/win32/gdkfont-win32.c: Fix a couple of bugs. The Unicode
......
Sat Nov 13 22:30:29 GMT 1999 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: threads example from Erik Mouw.
New question on GtkLabel background colors.
* docs/gtk_tut.sgml:
- Correct the example code callback
function definitions.
- Update the gtkdial example code, from Frans van Schaik.
- Update setselection.c to current API.
* examples/Makefile examples/*/*.c: Update to code
listed in tutorial.
1999-11-10 Tor Lillqvist <tml@iki.fi>
* gdk/win32/gdkfont-win32.c: Fix a couple of bugs. The Unicode
......
Sat Nov 13 22:30:29 GMT 1999 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: threads example from Erik Mouw.
New question on GtkLabel background colors.
* docs/gtk_tut.sgml:
- Correct the example code callback
function definitions.
- Update the gtkdial example code, from Frans van Schaik.
- Update setselection.c to current API.
* examples/Makefile examples/*/*.c: Update to code
listed in tutorial.
1999-11-10 Tor Lillqvist <tml@iki.fi>
* gdk/win32/gdkfont-win32.c: Fix a couple of bugs. The Unicode
......
Sat Nov 13 22:30:29 GMT 1999 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: threads example from Erik Mouw.
New question on GtkLabel background colors.
* docs/gtk_tut.sgml:
- Correct the example code callback
function definitions.
- Update the gtkdial example code, from Frans van Schaik.
- Update setselection.c to current API.
* examples/Makefile examples/*/*.c: Update to code
listed in tutorial.
1999-11-10 Tor Lillqvist <tml@iki.fi>
* gdk/win32/gdkfont-win32.c: Fix a couple of bugs. The Unicode
......
Sat Nov 13 22:30:29 GMT 1999 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: threads example from Erik Mouw.
New question on GtkLabel background colors.
* docs/gtk_tut.sgml:
- Correct the example code callback
function definitions.
- Update the gtkdial example code, from Frans van Schaik.
- Update setselection.c to current API.
* examples/Makefile examples/*/*.c: Update to code
listed in tutorial.
1999-11-10 Tor Lillqvist <tml@iki.fi>
* gdk/win32/gdkfont-win32.c: Fix a couple of bugs. The Unicode
......
Sat Nov 13 22:30:29 GMT 1999 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: threads example from Erik Mouw.
New question on GtkLabel background colors.
* docs/gtk_tut.sgml:
- Correct the example code callback
function definitions.
- Update the gtkdial example code, from Frans van Schaik.
- Update setselection.c to current API.
* examples/Makefile examples/*/*.c: Update to code
listed in tutorial.
1999-11-10 Tor Lillqvist <tml@iki.fi>
* gdk/win32/gdkfont-win32.c: Fix a couple of bugs. The Unicode
......
Sat Nov 13 22:30:29 GMT 1999 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: threads example from Erik Mouw.
New question on GtkLabel background colors.
* docs/gtk_tut.sgml:
- Correct the example code callback
function definitions.
- Update the gtkdial example code, from Frans van Schaik.
- Update setselection.c to current API.
* examples/Makefile examples/*/*.c: Update to code
listed in tutorial.
1999-11-10 Tor Lillqvist <tml@iki.fi>
* gdk/win32/gdkfont-win32.c: Fix a couple of bugs. The Unicode
......
......@@ -9,7 +9,7 @@
<!-- NOTE: Use only one author tag, otherwise sgml2txt barfs - TRG -->
<author>Tony Gale, Shawn T. Amundson, Emmanuel Deloget, Nathan Froyd
<date>October 30th 1999
<date>November 9th 1999
<abstract> This document is intended to answer questions that are likely to be
frequently asked by programmers using GTK+ or people who are just looking at
......@@ -950,6 +950,149 @@ are made outside of the GTK+ lock. So, within a signal
handler you do not need to call gdk_threads_enter(), but
within the other types of callbacks, you do.
Erik Mouw contributed the following code example to illustrate how to
use threads within GTK+ programs.
<tscreen><verb>
/*-------------------------------------------------------------------------
* Filename: gtk-thread.c
* Version: 0.99.1
* Copyright: Copyright (C) 1999, Erik Mouw
* Author: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
* Description: GTK threads example.
* Created at: Sun Oct 17 21:27:09 1999
* Modified by: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
* Modified at: Sun Oct 24 17:21:41 1999
*-----------------------------------------------------------------------*/
/*
* Compile with:
*
* cc -o gtk-thread gtk-thread.c `gtk-config --cflags --libs gthread`
*
* Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some
* bugs.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <pthread.h>
#define YES_IT_IS (1)
#define NO_IT_IS_NOT (0)
typedef struct
{
GtkWidget *label;
int what;
} yes_or_no_args;
G_LOCK_DEFINE_STATIC (yes_or_no);
static volatile int yes_or_no = YES_IT_IS;
void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
void *argument_thread(void *args)
{
yes_or_no_args *data = (yes_or_no_args *)args;
gboolean say_something;
for(;;)
{
/* sleep a while */
sleep(rand() / (RAND_MAX / 3) + 1);
/* lock the yes_or_no_variable */
G_LOCK(yes_or_no);
/* do we have to say something? */
say_something = (yes_or_no != data->what);
if(say_something)
{
/* set the variable */
yes_or_no = data->what;
}
/* Unlock the yes_or_no variable */
G_UNLOCK(yes_or_no);
if(say_something)
{
/* get GTK thread lock */
gdk_threads_enter();
/* set label text */
if(data->what == YES_IT_IS)
gtk_label_set_text(GTK_LABEL(data->label), "O yes, it is!");
else
gtk_label_set_text(GTK_LABEL(data->label), "O no, it isn't!");
/* release GTK thread lock */
gdk_threads_leave();
}
}
return(NULL);
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *label;
yes_or_no_args yes_args, no_args;
pthread_t no_tid, yes_tid;
/* init threads */
g_thread_init(NULL);
/* init gtk */
gtk_init(&amp;argc, &amp;argv);
/* init random number generator */
srand((unsigned int)time(NULL));
/* create a window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_signal_connect(GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC(destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER (window), 10);
/* create a label */
label = gtk_label_new("And now for something completely different ...");
gtk_container_add(GTK_CONTAINER(window), label);
/* show everything */
gtk_widget_show(label);
gtk_widget_show (window);
/* create the threads */
yes_args.label = label;
yes_args.what = YES_IT_IS;
pthread_create(&amp;yes_tid, NULL, argument_thread, &amp;yes_args);
no_args.label = label;
no_args.what = NO_IT_IS_NOT;
pthread_create(&amp;no_tid, NULL, argument_thread, &amp;no_args);
/* enter the GTK main loop */
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
return(0);
}
</verb></tscreen>
<!-- This is the old answer - TRG
......@@ -1002,7 +1145,7 @@ carefully.
Regardless, it's especially not a priority since relatively good
workarounds exist. -->
<!-- ----------------------------------------------------------------- -->
<sect1>Why do this strange 'x io error' occur when I <tt/fork()/ in my GTK+ app?
<sect1>Why does this strange 'x io error' occur when I <tt/fork()/ in my GTK+ app?
<p>
This is not really a GTK+ problem, and the problem is not related to <tt/fork()/
either. If the 'x io error' occurs then you probably use the <tt/exit()/ function
......@@ -1014,7 +1157,7 @@ and the underlying X library really doesn't like this.
The right function to use here is <tt/_exit()/.
Erik Mouw gave the following code example to illustrate handling
Erik Mouw contributed the following code example to illustrate handling
fork() and exit().
<tscreen><verb>
......@@ -1833,6 +1976,17 @@ gtk_misc_set_alignment(GTK_MISK(label), 0.5f, 0.5f);
gtk_misc_set_alignment(GTK_MISK(label), 1.0f, 1.0f);
</verb></tscreen>
<!-- ----------------------------------------------------------------- -->
<sect1>How do I set the background color of a GtkLabel widget?
<p>
The Gtklabel widget is one of a few GTK+ widgets that don't create
their own window to render themselves into. Instead, they draw
themselves directly onto their parents window.
This means that in order to set the background color for a GtkLabel
widget, you need to change the background color of its parent,
i.e. the object that you pack it into.
<!-- ----------------------------------------------------------------- -->
<sect1>How do I set the color and font of a GtkLabel using a Resource File?
<p>
......
......@@ -11,7 +11,7 @@ Tony Gale <tt><htmlurl url="mailto:gale@gtk.org"
name="&lt;gale@gtk.org&gt;"></tt>,
Ian Main <tt><htmlurl url="mailto:imain@gtk.org"
name="&lt;imain@gtk.org&gt;"></tt>
<date>April 10th, 1999
<date>November 13th, 1999
<abstract>
This is a tutorial on how to use GTK (the GIMP Toolkit) through its C
interface.
......@@ -343,7 +343,7 @@ program "knows" what compiler switches are needed to compile programs
that use GTK. <tt/gtk-config --cflags/ will output a list of include
directories for the compiler to look in, and <tt>gtk-config --libs</>
will output the list of libraries for the compiler to link with and
the directories to find them in. In the aboce example they could have
the directories to find them in. In the above example they could have
been combined into a single instance, such as
<tt/`gtk-config --cflags --libs`/.
......@@ -768,7 +768,7 @@ events come.
And the final return. Control returns here after gtk_quit() is called.
<tscreen><verb>
return (0;
return (0);
</verb></tscreen>
Now, when we click the mouse button on a GTK button, the widget emits
......@@ -870,11 +870,12 @@ void callback( GtkWidget *widget,
}
/* another callback */
void delete_event( GtkWidget *widget,
gint delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
gpointer data )
{
gtk_main_quit ();
gtk_main_quit();
return(FALSE);
}
int main( int argc,
......@@ -1094,13 +1095,15 @@ it. Compile it yourself and play with it.
/* example-start packbox packbox.c */
#include <stdio.h>
#include <stdlib.h>
#include "gtk/gtk.h"
void delete_event( GtkWidget *widget,
gint delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
gpointer data )
{
gtk_main_quit ();
gtk_main_quit();
return(FALSE);
}
/* Make a new hbox filled with button-labels. Arguments for the
......@@ -1558,11 +1561,12 @@ void callback( GtkWidget *widget,
}
/* This callback quits the program */
void delete_event( GtkWidget *widget,
gint delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
gpointer data )
{
gtk_main_quit ();
return(FALSE);
}
int main( int argc,
......@@ -1952,7 +1956,6 @@ int main( int argc,
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (gtk_exit), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_realize(window);
......@@ -2182,11 +2185,12 @@ The following example creates a radio button group with three buttons.
#include <gtk/gtk.h>
#include <glib.h>
void close_application( GtkWidget *widget,
gint close_application( GtkWidget *widget,
GdkEvent *event,
gpointer data )
gpointer data )
{
gtk_main_quit();
return(FALSE);
}
int main( int argc,
......@@ -4159,18 +4163,24 @@ static const char * xpm_data[] = {
/* when invoked (via signal delete_event), terminates the application.
*/
void close_application( GtkWidget *widget, GdkEvent *event, gpointer data ) {
gint close_application( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit();
return(FALSE);
}
/* is invoked when the button is clicked. It just prints a message.
*/
void button_clicked( GtkWidget *widget, gpointer data ) {
printf( "button clicked\n" );
void button_clicked( GtkWidget *widget,
gpointer data ) {
g_print( "button clicked\n" );
}
int main( int argc, char *argv[] )
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window, *pixmapwid, *button;
......@@ -4362,11 +4372,16 @@ static char * WheelbarrowFull_xpm[] = {
/* When invoked (via signal delete_event), terminates the application */
void close_application( GtkWidget *widget, GdkEvent *event, gpointer data ) {
gint close_application( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit();
return(FALSE);
}
int main (int argc, char *argv[])
int main (int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window, *pixmap, *fixed;
......@@ -4523,12 +4538,17 @@ Placement of the drawing area and the rulers is done using a table.
#define YSIZE 400
/* This routine gets control when the close button is clicked */
void close_application( GtkWidget *widget, GdkEvent *event, gpointer data ) {
gint close_application( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit();
return(FALSE);
}
/* The main routine */
int main( int argc, char *argv[] ) {
int main( int argc,
char *argv[] ) {
GtkWidget *window, *table, *area, *hrule, *vrule;
/* Initialize GTK and create the main window */
......@@ -4654,7 +4674,8 @@ back off.
GtkWidget *status_bar;
void push_item (GtkWidget *widget, gpointer data)
void push_item( GtkWidget *widget,
gpointer data )
{
static int count = 1;
char buff[20];
......@@ -4665,13 +4686,15 @@ void push_item (GtkWidget *widget, gpointer data)
return;
}
void pop_item (GtkWidget *widget, gpointer data)
void pop_item( GtkWidget *widget,
gpointer data )
{
gtk_statusbar_pop( GTK_STATUSBAR(status_bar), GPOINTER_TO_INT(data) );
return;
}
int main (int argc, char *argv[])
int main( int argc,
char *argv[] )
{
GtkWidget *window;
......@@ -4821,28 +4844,30 @@ The following code is an example of using an Entry widget.
#include <gtk/gtk.h>
void enter_callback(GtkWidget *widget, GtkWidget *entry)
void enter_callback( GtkWidget *widget,
GtkWidget *entry )
{
gchar *entry_text;
entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
printf("Entry contents: %s\n", entry_text);
}
void entry_toggle_editable (GtkWidget *checkbutton,
GtkWidget *entry)
void entry_toggle_editable( GtkWidget *checkbutton,
GtkWidget *entry )
{
gtk_entry_set_editable(GTK_ENTRY(entry),
GTK_TOGGLE_BUTTON(checkbutton)->active);
}
void entry_toggle_visibility (GtkWidget *checkbutton,
GtkWidget *entry)
void entry_toggle_visibility( GtkWidget *checkbutton,
GtkWidget *entry )
{
gtk_entry_set_visibility(GTK_ENTRY(entry),
GTK_TOGGLE_BUTTON(checkbutton)->active);
}
int main (int argc, char *argv[])
int main( int argc,
char *argv[] )
{
GtkWidget *window;
......@@ -5104,7 +5129,6 @@ void gtk_spin_button_set_update_policy( GtkSpinButton *spin_button,
The possible values of <tt/policy/ are either <tt/GTK_UPDATE_ALWAYS/ or
<tt/GTK_UPDATE_IF_VALID/.
These policies affect the behavior of a Spin Button when parsing
inserted text and syncing its value with the values of the
......@@ -5446,6 +5470,10 @@ Here's a typical code segment for creating a set of options:
gtk_combo_set_popdown_strings( GTK_COMBO(combo), glist) ;
</verb></tscreen>
The combo widget makes a copy of the strings passed to it in the glist
structure. As a result, you need to make sure you free the memory used
by the list if that is appropriate for your application.
At this point you have a working combo box that has been set up.
There are a few aspects of its behavior that you can change. These
are accomplished with the functions:
......@@ -5655,7 +5683,8 @@ GtkWidget *drawingarea = NULL;
/* Color changed handler */
void color_changed_cb (GtkWidget *widget, GtkColorSelection *colorsel)
void color_changed_cb( GtkWidget *widget,
GtkColorSelection *colorsel )
{
gdouble color[3];
GdkColor gdk_color;
......@@ -5691,7 +5720,9 @@ void color_changed_cb (GtkWidget *widget, GtkColorSelection *colorsel)
/* Drawingarea event handler */
gint area_event (GtkWidget *widget, GdkEvent *event, gpointer client_data)
gint area_event( GtkWidget *widget,
GdkEvent *event,
gpointer client_data )
{
gint handled = FALSE;
GtkWidget *colorsel;
......@@ -5728,14 +5759,18 @@ gint area_event (GtkWidget *widget, GdkEvent *event, gpointer client_data)
/* Close down and exit handler */
void destroy_window (GtkWidget *widget, gpointer client_data)
gint destroy_window( GtkWidget *widget,
GdkEvent *event,
gpointer client_data )
{
gtk_main_quit ();
return(TRUE);
}
/* Main */
gint main (gint argc, gchar *argv[])
gint main( gint argc,
gchar *argv[] )
{
GtkWidget *window;
......@@ -5753,9 +5788,6 @@ gint main (gint argc, gchar *argv[])
gtk_signal_connect (GTK_OBJECT(window), "delete_event",
(GtkSignalFunc)destroy_window, (gpointer)window);
gtk_signal_connect (GTK_OBJECT(window), "destroy",
(GtkSignalFunc)destroy_window, (gpointer)window);
/* Create drawingarea, set size and catch button events */
......@@ -5842,17 +5874,20 @@ screen, it does nothing as there is not a signal attached to it.
#include <gtk/gtk.h>
/* Get the selected filename and print it to the console */
void file_ok_sel (GtkWidget *w, GtkFileSelection *fs)
void file_ok_sel( GtkWidget *w,
GtkFileSelection *fs )
{
g_print ("%s\n", gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs)));
}
void destroy (GtkWidget *widget, gpointer data)
void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main (int argc, char *argv[])
int main( int argc,
char *argv[] )
{
GtkWidget *filew;
......@@ -5931,8 +5966,8 @@ window reveals varying amounts of the label.