⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example-4.c

📁 Developing with Gnome一书examples例子代码
💻 C
字号:
/* This program shows a preferences window, which contains * RadioButtons, a color selection widget, and a font selection * widget.  It does manual signal connection the gtk way (as opposed * to either the automatic libglade way or the manual libglade way, * the latter of which is sort of ugly IMO), pops up a (modal) window * to confirm whether the user really wants to exit, demonstrates how * to use pango markup, and shows how to manually free the memory * used by widgets when they're not wanted any more. */#include <gtk/gtk.h>#include <glade/glade.h>#include <stdio.h>/* A bunch of data to be set and used in the callbacks */typedef struct _QuitData QuitData;struct _QuitData{  GtkFontSelection* font_selection;  GtkDialog       * annoying_confirmation_window;  GtkLabel        * warning_label;  GdkColor          color;  gchar             location[10];};voidnew_location_selected (GtkWidget *widget, gpointer data){  QuitData * quit_choices = (QuitData*) data;  /* Copy the widget name to quit_choices->location */  snprintf(quit_choices->location, 10, gtk_widget_get_name (widget));}voidnew_color_selected (GtkWidget *widget, gpointer data){  QuitData * quit_choices = (QuitData*) data;  gtk_color_selection_get_current_color (GTK_COLOR_SELECTION (widget),                                         &quit_choices->color);}/* This simple helper function which converts an RGB triplet (which is * part of a GdkColor) into a "#RRGGBB" string. */voidconvert_color_to_string (GdkColor * color, gchar * color_string){  color_string[0] = '#';  /* GdkColor stores its red, green, and blue as 0..65536 instead of 0..256 */  sprintf(&color_string[1], "%.2X", color->red  /256);  sprintf(&color_string[3], "%.2X", color->green/256);  sprintf(&color_string[5], "%.2X", color->blue /256);}/* This makes the confirmation window pop up to ask the user if they really * want to quit.  The message to the user changes slightly depending on the * preferences they had set. */voidtime_to_quit (GtkWidget *widget, gpointer data){  QuitData * quit_choices = (QuitData*) data;  /* Create a color string of the form #RRGGBB, where R, G, & B are hex digits */  gchar color_string[8];  convert_color_to_string(&quit_choices->color, color_string);  /* Get the warning message, and find where we want to modify it */  const gchar * message = gtk_label_get_label (quit_choices->warning_label);  gchar * end_span = g_strrstr (message, "span");  gchar * beg_span = g_strrstr_len (message, end_span-message, "span");  /* Now, modify the string--namely the second span markup stuff */  GString * new_message = g_string_new(NULL);  g_string_append_len(new_message, message, beg_span-message-1);  g_string_append_printf(new_message,                         "<span font_desc=\"%s\" foreground=\"%s\">%s</span>",                         gtk_font_selection_get_font_name (quit_choices->                                                           font_selection),                         color_string,                         quit_choices->location);  g_string_append_printf(new_message, "%s", end_span+5);  /* Update the label and free our temporary string */  gtk_label_set_label (quit_choices->warning_label, new_message->str);  g_string_free (new_message, TRUE);  /* Make sure the confirmation window is shown */  gtk_widget_show (GTK_WIDGET (quit_choices->annoying_confirmation_window));}/* This exists simply because delete_event and clicked signals require different * numbers of arguments and return types.  This just calls time_to_quit. */gbooleantime_to_quit_dumb_events (GtkWidget *widget, GdkEvent *event, gpointer data){  time_to_quit (widget, data);  return TRUE;}/* clean_up merely frees the data being passed around to all the callbacks */voidclean_up (GtkWidget *widget, gpointer data){  QuitData * quit_choices = (QuitData*) data;  g_free(quit_choices);}voidhook_up_callbacks_and_set_defaults (GladeXML * the_widgets){  GtkWidget *widget;  /* Get the preferences and confirmation windows */  GtkWidget *preferences  = glade_xml_get_widget (the_widgets, "MainWindow");  GtkWidget *are_you_sure = glade_xml_get_widget (the_widgets, "ConfirmClose");  GtkWidget *font_sel     = glade_xml_get_widget (the_widgets, "FontSelection");  /* FIRST, allocate some data needed by callbacks and initialize some of it. */  QuitData * the_data = (QuitData*)malloc (sizeof (QuitData));  the_data->font_selection = GTK_FONT_SELECTION (font_sel);  the_data->annoying_confirmation_window = GTK_DIALOG (are_you_sure);  sprintf(the_data->location, "undefined");  /* SECOND, we handle setting up defaults and connect callbacks for   * the confirmation window.   */  /* Have the delete event (window close) do nothing */  g_signal_connect (G_OBJECT (are_you_sure), "delete_event",                    G_CALLBACK (gtk_true), NULL);  /* Have the cancel button hide the confirmation window */  widget = glade_xml_get_widget (the_widgets, "CancelButton");  g_signal_connect_swapped (G_OBJECT (widget), "clicked",                            G_CALLBACK (gtk_widget_hide),                            G_OBJECT (are_you_sure));  /* Have the "Exit anyway" button close everything */  widget = glade_xml_get_widget (the_widgets, "ExitButton");  g_signal_connect (G_OBJECT (widget), "clicked",                    G_CALLBACK (gtk_main_quit), NULL);  /* Get the warning label so we can modify it later. */  GtkWidget *info_label = glade_xml_get_widget (the_widgets, "InfoLabel");  the_data->warning_label = GTK_LABEL (info_label);  /* Make the window start hidden */  gtk_widget_hide (are_you_sure);  /* THIRD, we handle setting up defaults and connect callbacks for   * the preferences window.   */  /* Keep track of when the location changes */  widget = glade_xml_get_widget (the_widgets, "Here");  g_signal_connect (G_OBJECT (widget), "toggled",                    G_CALLBACK (new_location_selected),                    (gpointer)the_data);  widget = glade_xml_get_widget (the_widgets, "There");  g_signal_connect (G_OBJECT (widget), "toggled",                    G_CALLBACK (new_location_selected),                    (gpointer)the_data);  /* Make 'There' be the default; note this also emits the toggled signal */  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);  /* Keep track of when the color changes */  widget = glade_xml_get_widget (the_widgets, "ColorSelection");  g_signal_connect (G_OBJECT (widget), "color_changed",                    G_CALLBACK (new_color_selected),                    (gpointer)the_data);  /* Set the default to whatever the ColorSelection widget defaults to */  gtk_color_selection_get_current_color (GTK_COLOR_SELECTION (widget),                                         &the_data->color);  /* Turn off the opacity slider */  gtk_color_selection_set_has_opacity_control (GTK_COLOR_SELECTION (widget),                                               FALSE);  /* Have the quit button show the confirmation window */  widget = glade_xml_get_widget (the_widgets, "QuitButton");  g_signal_connect (G_OBJECT (widget), "clicked",                    G_CALLBACK (time_to_quit),                    (gpointer)the_data);  /* Have the delete event (window close) also show the confirmation window */  g_signal_connect (G_OBJECT (preferences), "delete_event",                    G_CALLBACK (time_to_quit_dumb_events),                    (gpointer)the_data);  /* Have the destroy signal call the cleanup callback, to free the_data */  g_signal_connect (G_OBJECT (preferences), "destroy",                    G_CALLBACK (clean_up),                    (gpointer)the_data);}intmain (int argc, char **argv){  GladeXML *all_da_widgets;  gtk_init (&argc, &argv);  /* load the interface */  all_da_widgets = glade_xml_new ("example-4.glade", NULL, NULL);  /* Connect all the signals to the appropriate callbacks */  hook_up_callbacks_and_set_defaults (all_da_widgets);  /* start the event loop */  gtk_main ();  /* Free the memory used by the widgets we're no longer using */  gtk_widget_destroy( glade_xml_get_widget (all_da_widgets, "MainWindow"));  gtk_widget_destroy( glade_xml_get_widget (all_da_widgets, "ConfirmClose"));  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -