?? box.c
字號(hào):
/* * * Sample Code for Gtk+ Programming in C, 1st Edition, Prentice Hall * * Copyright 2000 - 2002 Syd Logan, All Rights Reserved * * This code is distributed without warranty. You are free to use this * code for any purpose, however, if this code is republished or * redistributed in its original form, as hardcopy or electronically, * then you must include this copyright notice along with the code. * * Questions or comments regarding this code or any bugs it contains can * be directed to the author via e-mail at slogan@cts.com **/#include <gtk/gtk.h>// forward declarations to avoid compiler warningsvoid ResetSettingsMenuItems( void );void UpdateChildren( void );void MessageBox( char *message );#define VERSION_MAJOR 1#define VERSION_MINOR 0// some globals to make life easystatic GtkWidget *container;static GNode *root;static GNode *current;static int n = 1;// tree nodetypedef struct _boxdata { GtkWidget *container; // the vbox or hbox instance int spacing; // current spacing gboolean homogeneous; // current homogeneous int padding; // current padding gboolean fill; // current child fill gboolean expand; // current child expand } BoxData;// used to hold callback data for settings dialogtypedef struct _settingsData { GtkWidget *dialog; // Padding/Spacing dialog GtkWidget *pad_entry; // Padding entry widget GtkWidget *space_entry; // Space entry widget} SettingsData;static SettingsData settingsData; // dialog is modal, only need one// dump tree to stdoutgbooleantraverse_func( GNode *node, gpointer data ){ int i; // indent for ( i = 0; i < g_node_depth( node ) - 1; i++ ) printf( " " ); // print the node printf( "Node %x parent %x [%x] %s %s\n", node, node->parent, ((BoxData *) node->data)->container, (GTK_IS_VBOX(((BoxData *)node->data)->container) ? "VBox" : "Hbox" ), (node == current ? "<--" : "" ) ); return( FALSE ); // continue traversal}voiddump_tree( gpointer data, guint callback_action, GtkWidget *w ){ printf( "Tree Dump ********************************************************************\n" ); printf( "<-- is current box\n" ); g_node_traverse( root, G_PRE_ORDER, G_TRAVERSE_ALL, 999, traverse_func, NULL );}// traversal functionsstatic void go_up( gpointer data, guint callback_action, GtkWidget *w ){ if ( current != root ) { current = current->parent; container = (GtkWidget *)((BoxData *)current->data)->container; } dump_tree( (gpointer) NULL, 0, (GtkWidget *) NULL ); ResetSettingsMenuItems();}static void go_down( gpointer data, guint callback_action, GtkWidget *w ){ GNode *node; node = g_node_first_child( current ); if ( node != (GNode *) NULL ) current = node; container = (GtkWidget *)((BoxData *)current->data)->container; dump_tree( (gpointer) NULL, 0, (GtkWidget *) NULL ); ResetSettingsMenuItems();}static void go_previous( gpointer data, guint callback_action, GtkWidget *w ){ GNode *node; node = g_node_prev_sibling( current ); if ( node != (GNode *) NULL ) current = node; container = (GtkWidget *)((BoxData *)current->data)->container; dump_tree( (gpointer) NULL, 0, (GtkWidget *) NULL ); ResetSettingsMenuItems();}static void go_next( gpointer data, guint callback_action, GtkWidget *w ){ GNode *node; node = g_node_next_sibling( current ); if ( node != (GNode *) NULL ) current = node; container = (GtkWidget *)((BoxData *)current->data)->container; dump_tree( (gpointer) NULL, 0, (GtkWidget *) NULL ); ResetSettingsMenuItems();}// box add functionsstatic void add_horz_box( gpointer data, guint callback_action, GtkWidget *w ){ GtkWidget *hbox; GNode *node; BoxData *boxData; boxData = g_malloc( sizeof( BoxData ) ); hbox = gtk_hbox_new(FALSE, 0); boxData->container = hbox; boxData->homogeneous = FALSE; boxData->fill = TRUE; boxData->expand = TRUE; boxData->padding = 0; boxData->spacing = 0; if ( callback_action == 0 ) gtk_box_pack_start( GTK_BOX(container), hbox, boxData->expand, boxData->fill, boxData->padding ); else gtk_box_pack_end( GTK_BOX(container), hbox, boxData->expand, boxData->fill, boxData->padding ); node = g_node_new( (gpointer) boxData ); g_node_insert( current, -1, node ); dump_tree( (gpointer) NULL, 0, (GtkWidget *) NULL ); ResetSettingsMenuItems(); gtk_widget_show (hbox);}static void add_vert_box( gpointer data, guint callback_action, GtkWidget *w ){ GtkWidget *vbox; GNode *node; BoxData *boxData; boxData = g_malloc( sizeof( BoxData ) ); vbox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(container), vbox); boxData->container = vbox; boxData->homogeneous = FALSE; boxData->fill = TRUE; boxData->expand = TRUE; boxData->padding = 0; boxData->spacing = 0; if ( callback_action == 0 ) gtk_box_pack_start( GTK_BOX(container), vbox, boxData->expand, boxData->fill, boxData->padding ); else gtk_box_pack_end( GTK_BOX(container), vbox, boxData->expand, boxData->fill, boxData->padding ); node = g_node_new( (gpointer) boxData ); g_node_insert( current, -1, node ); dump_tree( (gpointer) NULL, 0, (GtkWidget *) NULL ); ResetSettingsMenuItems(); gtk_widget_show (vbox);}// child (button) widget add functionsstatic void add_button_to_box_start( gpointer data, guint callback_action, GtkWidget *w ){ GtkWidget *button; char buf[ 64 ]; sprintf( buf, "Start %d Box %x", n++, container ); button = gtk_button_new_with_label( buf ); gtk_box_pack_start( GTK_BOX(container), button, ((BoxData *) current->data)->expand, ((BoxData *) current->data)->fill, ((BoxData *) current->data)->padding ); gtk_widget_show(button);}static void add_button_to_box_end( gpointer data, guint callback_action, GtkWidget *w ){ GtkWidget *button; char buf[ 64 ]; sprintf( buf, "End %d Box %x", n++, container ); button = gtk_button_new_with_label( buf ); gtk_box_pack_end( GTK_BOX(container), button, ((BoxData *) current->data)->expand, ((BoxData *) current->data)->fill, ((BoxData *) current->data)->padding ); gtk_widget_show (button);}// padding and spacing dialogstatic voidClickedCallback(GtkWidget *widget, gpointer unused){ char *data; int spacing; data = gtk_entry_get_text( GTK_ENTRY(settingsData.pad_entry) ); ((BoxData *) current->data)->padding = atoi( data ); // get the spacing data = gtk_entry_get_text( GTK_ENTRY(settingsData.space_entry) ); spacing = ((BoxData *) current->data)->spacing = atoi( data ); // make the change to spacing gtk_box_set_spacing(GTK_BOX(((BoxData *) current->data)->container), spacing); UpdateChildren(); gtk_widget_destroy( settingsData.dialog );}// make changes to children after a setting changevoidUpdateChildren( void ){ GtkBoxChild *child; GList *children; GtkBox *box; gboolean expand, fill; int padding; GtkPackType type; box = GTK_BOX(((BoxData *) current->data)->container); children = box->children; while ( children != (GList *) NULL ) { child = (GtkBoxChild *) children->data; gtk_box_query_child_packing(box, (GtkWidget *)child->widget, &expand, &fill, &padding, &type ); gtk_box_set_child_packing(box, (GtkWidget *) child->widget, ((BoxData *) current->data)->expand, ((BoxData *) current->data)->fill, ((BoxData *) current->data)->padding, type ); children = children->next; }}// user canceled the settings dialogstatic voidCancelCallback(GtkWidget *widget, GtkWidget *dialog_window){ gtk_widget_destroy( dialog_window );}// display a dialog to get padding and spacing valuesvoidGetPaddingAndSpacing( void ){ GtkWidget *button, *label, *entry, *hbox, *dialog_window; char buf[ 64 ]; // create the dialog and set attributes settingsData.dialog = dialog_window = gtk_dialog_new(); gtk_window_set_modal(GTK_WINDOW(dialog_window), TRUE); gtk_window_position(GTK_WINDOW (dialog_window), GTK_WIN_POS_MOUSE); gtk_window_set_title(GTK_WINDOW(dialog_window), "Padding and Spacing"); // fill in the content area hbox = gtk_hbox_new (FALSE, 1); gtk_box_pack_start(GTK_BOX(GTK_DIALOG (dialog_window)->vbox), hbox, FALSE, FALSE, 0);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -