Link the selected area @ GTK + 2.0 Chinese tutorial

Source: Internet
Author: User
Tags gtk
Provide selected area

It is more complex to provide the selected area. You must register the handler that will be called when the Central Region is requested. Each time you process a pair of selected zones/targets, you need to call the following function:

void gtk_selection_add_target (GtkWidget           *widget, 
GdkAtom selection,
GdkAtom target,
guint info);

Widget
,Selection
AndTarget
Identifies the operation requirements of the processing function. When a requirement for the selected area is received, the "selection_get" signal is called.Info
Can be used to identify the specified target in the callback function.

The callback function format is as follows:

void  "selection_get" (GtkWidget          *widget,
GtkSelectionData *selection_data,
guint info,
guint time);

Gtkselectiondata is the same as described above, but this time, we are responsible for providingType
,Format
,Data
AndLength
Value (Format
The value is important here-the X server determines whether data must be processed in bytes based on it. Generally, the value is 8-For example
Portable-or 32-For example
Integer ). Call the following function to set these values:

void gtk_selection_data_set( GtkSelectionData *selection_data,
GdkAtom type,
gint format,
guchar *data,
gint length );

This function will copy the data appropriately so that you do not have to worry about retaining the data. (Do not manually fill in values in the gtkselectiondata structure .)

After you perform an operation, you can use the following function to claim the ownership of the selected zone.

gboolean gtk_selection_owner_set( GtkWidget *widget,
GdkAtom selection,
guint32 time );

If other applications require the selected area, you will receive the "selection_clear_event" signal.

The following is an example that provides the selected area. It adds the selected area function to the switch button. When the switch button is pressed, the program requires the central area of the primary node. It only supports "string" targets (except targets such as "targets" supported by GTK itself ). When this target is required, a string describing the time is returned.

# Include <stdlib. h>
# Include <GTK/GTK. h>
# Include <time. h>

Gtkwidget * selection_button;
Gtkwidget * selection_widget;

/* Callback when the user triggers the selected area */
Void selection_toggled (gtkwidget * widget,
Gint * have_selection)
{
If (gtk_toggle_button (widget)-> active)
{
* Have_selection = gtk_selection_owner_set (selection_widget,
Gdk_selection_primary,
Gdk_current_time );
/* If it indicates that the selected area fails, the button is returned to the unselected status */
If (! * Have_selection)
Gtk_toggle_button_set_active (gtk_toggle_button (widget), false );
}
Else
{
If (* have_selection)
{
/* Before setting the owner to null to clear the selected area,
* First, check whether you are the real owner */
If (gdk_selection_owner_get (gdk_selection_primary) = widget-> window)
Gtk_selection_owner_set (null, gdk_selection_primary,
Gdk_current_time );
* Have_selection = false;
}
}
}

/* Called when other applications claim the selected area */
Gint selection_clear (gtkwidget * widget,
Gdkeventselection * event,
Gint * have_selection)
{
* Have_selection = false;
Gtk_toggle_button_set_active (gtk_toggle_button (selection_button), false );

Return true;
}

/* Provide the current time as the content of the selected area. */
Void selection_handle (gtkwidget * widget,
Gtkselectiondata * selection_data,
Guint info,
Guint time_stamp,
Gpointer data)
{
Gchar * timestr;
Time_t current_time;

Current_time = Time (null );
Timestr = asctime (localtime (& current_time ));
/* When we return a separate string, it does not have to end with null.
It will be automatically completed */

Gtk_selection_data_set (selection_data, gdk_selection_type_string,
8, timestr, strlen (timestr ));
}

Int main (INT argc,
Char * argv [])
{
Gtkwidget * window;

Static int have_selection = false;

Gtk_init (& argc, & argv );

/* Create a top-level window */

Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "event box ");
Gtk_container_set_border_width (gtk_container (window), 10 );

G_signal_connect (g_object (window), "Destroy ",
G_callback (exit), null );

/* Create a switch button as the selected area */

Selection_widget = gtk_invisible_new ();
Selection_button = gtk_toggle_button_new_with_label ("claim selection ");
Gtk_container_add (gtk_container (window), selection_button );
Gtk_widget_show (selection_button );

G_signal_connect (g_object (selection_button), "toggled ",
G_callback (selection_toggled), & have_selection );
G_signal_connect (g_object (selection_widget), "selection_clear_event ",
G_callback (selection_clear), & have_selection );

Gtk_selection_add_target (selection_widget,
Gdk_selection_primary,
Gdk_selection_type_string,
1 );
G_signal_connect (g_object (selection_widget), "selection_get ",
G_callback (selection_handle), & have_selection );

Gtk_widget_show (selection_button );
Gtk_widget_show (window );

Gtk_main ();

Return 0;
}
<Previous Home Next >>>
Obtain information about the selected area Up Drag and Drop

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.