📄 gui_utils.cpp
字号:
gtk_widget_show(hbox); label = gtk_label_new(label_name); gtk_widget_ref(label); gtk_widget_show(label); gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); ret = gtk_entry_new_with_max_length(value_len); SetNumberEntryValue(ret, value); gtk_widget_show(ret); gtk_box_pack_start(GTK_BOX(hbox), ret, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 3); return (ret);}int GetNumberEntryValue (GtkWidget *entry, uint32_t *result){ const char *text; char *endptr; text = gtk_entry_get_text(GTK_ENTRY(entry)); *result = strtoul(text, &endptr, 10); if (*text != '\0' && endptr != NULL && *endptr == '\0') { if (*result == UINT32_MAX && errno == ERANGE) return (0); return (1); } return (0);}GtkWidget *AddButtonToDialog (GtkWidget *dialog, const char *name, GtkSignalFunc on_click){ GtkWidget *button; button = gtk_button_new_with_label(name); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(on_click), NULL); GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); /* --- Add the button to the dialog --- */ gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->action_area), button, TRUE, TRUE, 5); /* --- Make the button visible --- */ gtk_widget_show (button); return (button);}GtkWidget *AddEntryBoxWithLabel (GtkWidget *vbox, const char *label_name, const char *initial_value, uint32_t value_len){ GtkWidget *hbox, *label, *ret; hbox = gtk_hbox_new(FALSE, 1); gtk_widget_show(hbox); label = gtk_label_new(label_name); gtk_widget_ref(label); gtk_widget_show(label); gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); ret = gtk_entry_new_with_max_length(value_len); gtk_entry_set_text(GTK_ENTRY(ret), initial_value == NULL ? "" : initial_value); gtk_widget_show(ret); gtk_box_pack_start(GTK_BOX(hbox), ret, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 3); return (ret);}/* * FreeChild * * Free all the children of the widget * This is called when the button has to display a new image. * The old image is destroyed here. */void FreeChild (GtkWidget *widget){ /* --- Free button children --- */ gtk_container_foreach ( GTK_CONTAINER (widget), (GtkCallback) gtk_widget_destroy, NULL);}void SetEntryValidator(GtkObject* object, GtkSignalFunc changed_func, GtkSignalFunc leave_func){ gtk_signal_connect(object, "changed", GTK_SIGNAL_FUNC(changed_func), object); gtk_signal_connect(object, "focus_out_event", GTK_SIGNAL_FUNC(leave_func), object);}static GtkWidget* filesel;static GtkWidget* fileentry;static void on_filename_selected (GtkDialog *filesel, gint response_id, gpointer data){ const gchar *name; if (GTK_RESPONSE_OK == response_id) { name = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); gtk_entry_set_text(GTK_ENTRY(fileentry), name); GtkSignalFunc okfunc = (GtkSignalFunc)data; gtk_widget_show(fileentry); if (okfunc != NULL) (okfunc)(); } gtk_widget_destroy(GTK_WIDGET(filesel));}void FileBrowser (GtkWidget* entry, GtkWidget *mainwin, GtkSignalFunc okFunc){ fileentry = entry; filesel = gtk_file_selection_new("Select File"); gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(filesel)); gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), gtk_entry_get_text(GTK_ENTRY(entry))); gtk_window_set_modal(GTK_WINDOW(filesel), true); gtk_window_set_transient_for(GTK_WINDOW(filesel), GTK_WINDOW(mainwin)); g_signal_connect((gpointer)filesel, "response", G_CALLBACK(on_filename_selected), (void *)okFunc); gtk_widget_show(filesel);}/* * Showmessage.c from Developing Linux Applications *//* * CloseShowMessage * * Routine to close the about dialog window. */void CloseShowMessage (GtkWidget *widget, gpointer data){ GtkWidget *dialog_widget = (GtkWidget *) data; gtk_grab_remove (dialog_widget); /* --- Close the widget --- */ gtk_widget_destroy (dialog_widget);}/* * ClearShowMessage * * Release the window "grab" * Clear out the global dialog_window since that * is checked when the dialog is brought up. */static void ClearShowMessage (GtkWidget *widget, gpointer data){ gtk_grab_remove (widget);}/* * ShowMessage * * Show a popup message to the user. */GtkWidget* ShowMessage ( const char *szTitle, const char *szMessage, GtkWidget *main_window, bool userDismiss){ GtkWidget *label; GtkWidget *button; GtkWidget *dialog_window; /* --- Create a dialog window --- */ dialog_window = gtk_dialog_new (); if (main_window != NULL) { gtk_window_set_transient_for(GTK_WINDOW(dialog_window), GTK_WINDOW(main_window)); } gtk_signal_connect (GTK_OBJECT (dialog_window), "destroy", GTK_SIGNAL_FUNC (ClearShowMessage), NULL); /* --- Set the title and add a border --- */ gtk_window_set_title (GTK_WINDOW (dialog_window), szTitle); gtk_container_border_width (GTK_CONTAINER (dialog_window), 0); /* --- Create an "Ok" button with the focus --- */ if (userDismiss) { button = gtk_button_new_with_label ("OK"); gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (CloseShowMessage), dialog_window); /* --- Default the "Ok" button --- */ GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog_window)->action_area), button, TRUE, TRUE, 0); gtk_widget_grab_default (button); gtk_widget_show (button); } /* --- Create a descriptive label --- */ label = gtk_label_new (szMessage); /* --- Put some room around the label text --- */ gtk_misc_set_padding (GTK_MISC (label), 10, 10); /* --- Add label to designated area on dialog --- */ gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog_window)->vbox), label, TRUE, TRUE, 0); /* --- Show the label --- */ gtk_widget_show (label); /* --- Show the dialog --- */ gtk_widget_show (dialog_window); /* --- Only this window can have actions done. --- */ gtk_grab_add (dialog_window); return dialog_window;}static GtkSignalFunc local_yesroutine, local_noroutine;static void on_yes_button (GtkWidget *widget, gpointer gdata){ GtkWidget *dialog = (GtkWidget *)gdata; if (local_yesroutine != NULL) { (local_yesroutine)(); } gtk_grab_remove(dialog); gtk_widget_destroy(dialog);}static void on_no_button (GtkWidget *widget, gpointer data){ GtkWidget *dialog = (GtkWidget *)data; if (local_noroutine != NULL) { (local_noroutine)(); } gtk_grab_remove(dialog); gtk_widget_destroy(dialog);}void YesOrNo (const char *szTitle, const char *szMessage, int yes_as_default, GtkSignalFunc yesroutine, GtkSignalFunc noroutine){ GtkWidget *label; GtkWidget *button; GtkWidget *dialog_window; local_yesroutine = yesroutine; local_noroutine = noroutine; /* --- Create a dialog window --- */ dialog_window = gtk_dialog_new (); gtk_signal_connect (GTK_OBJECT (dialog_window), "destroy", GTK_SIGNAL_FUNC (ClearShowMessage), NULL); /* --- Set the title and add a border --- */ gtk_window_set_title (GTK_WINDOW (dialog_window), szTitle); gtk_container_border_width (GTK_CONTAINER (dialog_window), 0); /* --- Create an "Ok" button with the focus --- */ button = gtk_button_new_with_label ("Yes"); gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (on_yes_button), dialog_window); /* --- Default the "Ok" button --- */ if (yes_as_default != 0) GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog_window)->action_area), button, TRUE, TRUE, 0); if (yes_as_default != 0) gtk_widget_grab_default (button); gtk_widget_show (button); button = gtk_button_new_with_label("No"); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(on_no_button), dialog_window); if (yes_as_default == 0) GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_window)->action_area), button, TRUE, TRUE, 0); if (yes_as_default == 0) gtk_widget_grab_default(button); gtk_widget_show(button); /* --- Create a descriptive label --- */ label = gtk_label_new (szMessage); /* --- Put some room around the label text --- */ gtk_misc_set_padding (GTK_MISC (label), 10, 10); /* --- Add label to designated area on dialog --- */ gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog_window)->vbox), label, TRUE, TRUE, 0); /* --- Show the label --- */ gtk_widget_show (label); /* --- Show the dialog --- */ gtk_widget_show (dialog_window); /* --- Only this window can have actions done. --- */ gtk_grab_add (dialog_window);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -