Drawing area component and drawing @ GTK + 2.0 Chinese tutorial serialization

Source: Internet
Author: User
Tags gtk
Component and drawing in the drawing Area

Now, let's start plotting to the screen. The component we use is a drawing area component. A component in a drawing area is essentially an X Window, and there is nothing else. It is a blank canvas on which we can draw what we need. A widget is created using the following functions:

GtkWidget* gtk_drawing_area_new        (void);

Use the following function to set the default component size:

void       gtk_drawing_area_size       (GtkDrawingArea      *darea,
gint width,
gint height);

When a function is calledGtk_widget_set_size_request ()
Or you can manually adjust the size of the window that contains the drawing area. The default size can be invalid, which is the same for all components.

Note the following when creating a drawing area component:Complete
Draws content on it. If our window is hidden and exposed, we get an exposure event. We must re-paint the previously hidden part.

In order to correct the re-painting, we must remember the content drawn on the screen. In addition, this is obviously troublesome. If a part of the window is cleared, we need to re-paint it step by step. The solution is to useBackend bitmap
. We use Image Rendering instead of drawing directly to the screen. When the image changes or a part of the image needs to be displayed, we copy the corresponding part to the screen.

Use the following function to create a backend bitmap:

GdkPixmap* gdk_pixmap_new               (GdkWindow  *window,
gint width,
gint height,
gint depth);

The parameter sets a gdk window. Bitmap inherits all properties of the window.Width
Set the bitmap size.Depth
SetColor Depth
That is the binary number of each pixel. If depth is set-1
It automatically matches the color depth of the window.

We create a bitmap in the processing function of the event "configure_event. This event will be generated when we change the window size, including when the window is created.

/* Backend bitmap of the drawing area */
Static gdkpixmap * pixmap = NULL;

/* Create a backend bitmap of an appropriate size */
Static Gint
Configure_event (gtkwidget * widget, gdkeventconfigure * event)
If (pixmap)
Gdk_pixmap_unref (pixmap );

Pixmap = gdk_pixmap_new (widget-> window,
Widget-> allocation. Width,
Widget-> allocation. height,
-1 );
Gdk_draw_rectangle (pixmap,
Widget-> style-> white_gc,
0, 0,
Widget-> allocation. Width,
Widget-> allocation. Height );

Return true;

Call a functionGdk_draw_rectangle ()
Clear the bitmap and initialize it to white. We will explain in detail later.

Our exposure event processing function simply copies the corresponding part of the bitmap to the screen (the event-> area of the exposure event is used to determine the re-painting area ):

/* Re-draw the screen from the backend bitmap */
Static Gint
Expose_event (gtkwidget * widget, gdkeventexpose * event)
Gdk_draw_pixmap (widget-> window,
Widget-> style-> fg_gc [gtk_widget_state (widget)],
Event-> area. X, event-> area. y,
Event-> area. X, event-> area. y,
Event-> area. Width, event-> area. Height );

Return false;

Now let's take a look at how to keep the screen updated with the bitmap and how to draw what we need on the map? The GTK gdk library has many functions used inPrintable Area
Drawing. The printable area can be a window, a bitmap, or a black-and-white image. We have seen two above,Gdk_draw_rectangle ()
AndGdk_draw_pixmap ()
. The complete list of these functions is as follows:

gdk_draw_line ()
gdk_draw_rectangle ()
gdk_draw_arc ()
gdk_draw_polygon ()
gdk_draw_string ()
gdk_draw_text ()
gdk_draw_pixmap ()
gdk_draw_bitmap ()
gdk_draw_image ()
gdk_draw_points ()
gdk_draw_segments ()

For more information, see references or header files.<Gdk/gdk. h>
. The first two parameters of these functions are the same. The first parameter is the printable area. The second parameter isImage Association
(GC ).

Image Association encapsulates some information, such as the foreground color, background color, and line width. Gdk has a set of functions for creating and modifying image associations, but for convenience, we only use predefined image associations. Each component has a phase
Associated style. (You can modify it in the gtkrc file. For details, see the RC file of GTK.) Many image associations are stored. Examples of accessing these image associations are as follows:


Domain valueFg_gc
The index value depends on oneGtkstatetype
Type parameter, which can take the following values:


For example,Gtk_state_selected
The default foreground color is white, and the default background color is dark blue.

Our FunctionsDraw_brush ()
To draw the screen. The function is as follows:

/* Draw a rectangle on the screen */
Static void
Draw_brush (gtkwidget * widget, gdouble X, gdouble y)
Gdkrectangle update_rect;

Update_rect.x = x-5;
Update_rect.y = Y-5;
Update_rect.width = 10;
Update_rect.height = 10;
Gdk_draw_rectangle (pixmap,
Widget-> style-> black_gc,
Update_rect.x, update_rect.y,
Update_rect.width, update_rect.height );
Gtk_widget_draw (widget, & update_rect );

After a rectangle is drawn on the in-place graph, we call the following function:

void       gtk_widget_draw                (GtkWidget           *widget,
GdkRectangle *area);

It notifies the X ParameterArea
The specified region must be updated. X will eventually generate an exposure event (the mixed area needs to call the function multiple timesGtk_widget_draw ()
), And then calls the exposure event processing function to copy the corresponding part to the screen.

Now we have a relatively complete Plotting Program, only the main window.

<Previous Home Next >>>
Event Processing Up Add xinput support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.