Re: Many questions when I drew a line



On Thu, 14 Aug 2008 15:59:19 +0800
"Lazy Fox" <lazy fox wu gmail com> wrote:

>/***************************************************************************
>I'm new to GTK+, please help me with following simple questions.
>Thanks for your patience.

I'm better at Perl, than c, but I can answer a few of the simpler questions.

>I wrote a simple programme to draw a line, the whole file is below
>these questions:
>1. I use gtk_widget_set_size_request() to resize the drawing_area,
>   but it's useless. The size of the drawing_area will resize to it's
>   parent's size automatically.How to make it's size fixed?

You need to learn about the difference between set_size_request and
set_default_size...... also you need to pack things in a table or nested
hbox/vbox's.   See the code below, and play with the pack_start and pack_end,
and the expand/homogeneous/fill  settings.  You will need to read a bit
on your own, there are some tutorials out there.
In the code below, the TRUE/FALSE settings in the packing statements
control how the window behaves on resizing.

>2. The bg_color I set in redraw() is useless too. Each time I run the
>application,
>   the background color is different. Looks like random color.

Works fine here on linux.

>5. How to use color? The common RGB color is like (255, 255, 255),
>   but in GTK+ this can be (0xffff, 0xffff, 0xffff).
>   I was confused, how to set a right RGB color? Is there a bunch of build in
>   color I can use?

There may (or not) be an alpha setting. 

(255,255,255) would be (0xff,0xff,0xff) in hex.

(0xffff,0xff00,0xff66) would be 
red at full color saturation,
green at no color, 
blue at 0x66 color

Usually there will be a color setting called something like fill_color_rgba.
the "a" signifies alpha levels for the colors.


Try this code for layout, although a table would probably be better for you.
Sorry, I usually do Perl, so my c may not be perfect. Thru pack_end, I put
your drawing_area in the lower right corner.

#include <gtk/gtk.h>

gboolean redraw(GtkWidget *widget, GdkEvent *event, gpointer data)
{
GdkGC *gc = gdk_gc_new(widget->window);
GdkColor bg_color;

/* Draw background */
bg_color.red = 0xffff;
bg_color.green = 0;
bg_color.blue = 0;
gdk_gc_set_foreground(gc, &bg_color);
gdk_draw_rectangle(widget->window, gc, TRUE,
0, 0, widget->allocation.width, widget->allocation.height);
gdk_draw_line(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
0, 0, 120, 100);

return TRUE;
}

int main(int argc, char *argv[])
{
GtkWidget *w;
GtkWidget *drawing_area;

gtk_init(&argc, &argv);

w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(w), 300, 200); 
g_signal_connect(GTK_OBJECT(w), "destroy", G_CALLBACK(gtk_main_quit), NULL);

GtkWidget *VBox = gtk_vbox_new(FALSE,0);
gtk_container_add (GTK_CONTAINER (w), VBox);

GtkWidget *HBox1 = gtk_hbox_new(FALSE,0);
GtkWidget *HBox2 = gtk_hbox_new(FALSE,0);

gtk_box_pack_start(GTK_BOX(VBox), HBox1, FALSE, FALSE, 0);
gtk_box_pack_end(GTK_BOX(VBox), HBox2, FALSE, FALSE, 0);

drawing_area = gtk_drawing_area_new();
gtk_widget_set_size_request(GTK_WIDGET(drawing_area), 100, 100);
g_signal_connect(G_OBJECT(drawing_area), "expose-event",G_CALLBACK(redraw), NULL);

gtk_box_pack_end(GTK_BOX(HBox2), drawing_area, FALSE, FALSE, 0);
                     
gtk_widget_show_all(w);
gtk_main();
return 0;
}





zentara
-- 
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]