📄 gtktimeedit.c.svn-base
字号:
/* libgemwidget - Gtk+ Embedded Widget library * * Authors: YE Nan <nan.ye@orange-ftgourp.com> * * This software and associated documentation files (the "Software") * are copyright (C) 2005 LiPS Linux Phone Standards Forum [FranceTelecom] * All Rights Reserved. * * A copyright license is hereby granted for redistribution and use of * the Software in source and binary forms, with or without modification, * provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this copyright license and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this copyright license and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - Neither the name of LiPS nor the names of its Members may be used * to endorse or promote products derived from the Software without * specific prior written permission. * * A patent license for any Necessary Claims owned by Members of LiPS Forum * to make, have made, use, import, offer to sell, lease and sell or otherwise * distribute any implementation compliant with the any specification adopted * by the LiPS Forumcan be obtained from the respective Members on reasonable * and non-discriminatory terms and conditions and under reciprocity, as * regulated in more detail in the Internal Policy of the LiPS Forum. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER, ITS MEMBERS AND CONTRIBUTORS * "AS IS", AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER, * ITS MEMBERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#include <ctype.h>#include <gtk/gtk.h>#include <gdk/gdkkeysyms.h>#include <stdlib.h>#include <string.h>#include <gtktimeedit.h>static GtkHBoxClass * parent_class = NULL;static void gtk_time_edit_class_init (GtkTimeEditClass *klass);static void gtk_time_edit_init (GtkTimeEdit *window);static void gtk_time_edit_finalize (GObject *object);static void gtk_time_edit_destroy (GtkObject *object);GTypegtk_time_edit_get_type (void){ static GType time_edit_type = 0; if (!time_edit_type) { static const GTypeInfo time_edit_info = { sizeof (GtkTimeEditClass), NULL, /* base_init */ NULL, /* base_finalize */ (GClassInitFunc) gtk_time_edit_class_init, NULL, /* class_finalize */ NULL, /* class_data */ sizeof (GtkTimeEdit), 0, /* n_preallocs */ (GInstanceInitFunc) gtk_time_edit_init, }; time_edit_type = g_type_register_static (GTK_TYPE_HBOX, "GtkTimeEdit", &time_edit_info, 0); } return time_edit_type;}static voidgtk_time_edit_class_init (GtkTimeEditClass * klass){ GObjectClass * object_class; GtkObjectClass * gtk_object_class; parent_class = g_type_class_peek_parent (klass); gtk_object_class = (GtkObjectClass *)klass; gtk_object_class->destroy = gtk_time_edit_destroy; object_class = (GObjectClass *)klass; object_class->finalize = gtk_time_edit_finalize; return;}static void gtk_time_edit_destroy (GtkObject * object){ GTK_OBJECT_CLASS(parent_class)->destroy(object); return;}static voidgtk_time_edit_finalize (GObject * object){ G_OBJECT_CLASS(parent_class)->finalize(object); return;}void hour_insert_text_handler (GtkEntry *entry, const gchar *text, gint length, gint *position, gpointer data){ GtkEditable *editable = GTK_EDITABLE(entry); int i, count=0; gchar *result = g_new (gchar, length); for (i=0; i < length; i++) { if (!isdigit(text[i])) continue; result[count++] = text[i]; } if (count > 0) { g_signal_handlers_block_by_func (G_OBJECT (editable), G_CALLBACK (hour_insert_text_handler), data); if (strlen(gtk_entry_get_text (GTK_ENTRY (entry))) == 0) gtk_editable_insert_text (editable, result, count, position); else if (strlen(gtk_entry_get_text (GTK_ENTRY (entry))) == 1) { gint hour = atoi (gtk_entry_get_text (GTK_ENTRY (entry))); hour = hour * 10 + atoi (result); if (hour <= 23) gtk_editable_insert_text (editable, result, count, position); else { hour = 23; sprintf (result,"%d", hour); gtk_entry_set_text (entry, result); } gtk_widget_child_focus (gtk_widget_get_parent (GTK_WIDGET (entry)), GTK_DIR_TAB_FORWARD); } g_signal_handlers_unblock_by_func (G_OBJECT (editable), G_CALLBACK (hour_insert_text_handler), data); } g_signal_stop_emission_by_name (G_OBJECT (editable), "insert_text"); g_free (result); }void min_insert_text_handler (GtkEntry *entry, const gchar *text, gint length, gint *position, gpointer data){ GtkEditable *editable = GTK_EDITABLE(entry); int i, count=0; gchar *result = g_new (gchar, length); for (i=0; i < length; i++) { if (!isdigit(text[i])) continue; result[count++] = text[i]; } if (count > 0) { g_signal_handlers_block_by_func (G_OBJECT (editable), G_CALLBACK (min_insert_text_handler), data); if (strlen(gtk_entry_get_text (GTK_ENTRY (entry))) == 0) gtk_editable_insert_text (editable, result, count, position); else if (strlen(gtk_entry_get_text (GTK_ENTRY (entry))) == 1) { gint min = atoi (gtk_entry_get_text (GTK_ENTRY (entry))); min = min * 10 + atoi (result); if (min <= 59) gtk_editable_insert_text (editable, result, count, position); else { min = 59; sprintf (result,"%d", min); gtk_entry_set_text (entry, result); } gtk_widget_child_focus (gtk_widget_get_parent (GTK_WIDGET (entry)), GTK_DIR_TAB_BACKWARD); } g_signal_handlers_unblock_by_func (G_OBJECT (editable), G_CALLBACK (min_insert_text_handler), data); } g_signal_stop_emission_by_name (G_OBJECT (editable), "insert_text"); g_free (result); }static gboolean hour_key_press_handler (GtkWidget *widget, GdkEventKey *k, gpointer user_data){ if ((k->keyval == GDK_Right) || (k->keyval == GDK_Left)) { gtk_widget_child_focus (gtk_widget_get_parent (widget), GTK_DIR_TAB_FORWARD); return TRUE; } else return FALSE;}static gboolean min_key_press_handler (GtkWidget *widget, GdkEventKey *k, gpointer user_data){ if ((k->keyval == GDK_Right) || (k->keyval == GDK_Left)) { gtk_widget_child_focus (gtk_widget_get_parent (widget), GTK_DIR_TAB_BACKWARD); return TRUE; } else return FALSE;}static gboolean entry_focus_out_cb (GtkWidget * widget, GdkEventFocus * event, gpointer user_data){ gtk_editable_select_region(GTK_EDITABLE(widget), 0, 0); return FALSE; }static voidgtk_time_edit_init (GtkTimeEdit * time_edit){ time_edit->pad1 = gtk_label_new (""); time_edit->entry1 = gtk_entry_new(); time_edit->entry2 = gtk_entry_new (); time_edit->eventbox = gtk_event_box_new (); time_edit->pad2 = gtk_label_new (""); g_signal_connect(G_OBJECT(time_edit->entry1), "insert_text", G_CALLBACK(hour_insert_text_handler), NULL); g_signal_connect(G_OBJECT(time_edit->entry2), "insert_text", G_CALLBACK(min_insert_text_handler), NULL); g_signal_connect(G_OBJECT(time_edit->entry1), "key_press_event", G_CALLBACK(hour_key_press_handler), NULL); g_signal_connect(G_OBJECT(time_edit->entry2), "key_press_event", G_CALLBACK(min_key_press_handler), NULL); g_signal_connect(G_OBJECT(time_edit->entry1), "focus-out-event", G_CALLBACK(entry_focus_out_cb), NULL); g_signal_connect(G_OBJECT(time_edit->entry2), "focus-out-event", G_CALLBACK(entry_focus_out_cb), NULL); gtk_entry_set_has_frame(GTK_ENTRY(time_edit->entry1), FALSE); gtk_entry_set_has_frame(GTK_ENTRY(time_edit->entry2), FALSE); gtk_box_pack_start (GTK_BOX (time_edit), time_edit->pad1, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (time_edit), time_edit->entry1, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (time_edit), time_edit->eventbox, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (time_edit), time_edit->entry2, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (time_edit), time_edit->pad2, FALSE, FALSE, 0); time_edit->label = gtk_label_new (":"); gtk_container_add(GTK_CONTAINER (time_edit->eventbox), time_edit->label); GdkColor white; white.red = 65535; white.green = 65535; white.blue = 65535; gtk_widget_modify_bg (time_edit->eventbox, GTK_STATE_NORMAL, &white); gtk_entry_set_width_chars (GTK_ENTRY (time_edit->entry1), 2); gtk_entry_set_width_chars (GTK_ENTRY (time_edit->entry2), 2); return;}guint gtk_time_edit_get_hour (GtkTimeEdit *timeedit){ guint hour; hour = atoi(gtk_entry_get_text(GTK_ENTRY(timeedit->entry1))); //printf ("%s:day=%d\n", __FUNCTION__, hour); return hour; }guint gtk_time_edit_get_min (GtkTimeEdit *timeedit){ guint min; min = atoi(gtk_entry_get_text(GTK_ENTRY(timeedit->entry2))); //printf ("%s:day=%d\n", __FUNCTION__, min); return min; }void gtk_time_edit_set_value (GtkTimeEdit *timeedit, guint hour, guint min){ gchar textbuf[5]; if (hour > 9) sprintf (textbuf, "%d", hour); else sprintf (textbuf, "0%d", hour); gtk_entry_set_text (GTK_ENTRY (timeedit->entry1), textbuf); if (min > 9) sprintf (textbuf, "%d", min); else sprintf (textbuf, "0%d", min); gtk_entry_set_text (GTK_ENTRY (timeedit->entry2), textbuf); }void gtk_time_edit_set_widget_width (GtkTimeEdit *entry, guint pad1w, guint entry1w, guint entry2w, guint pad2w){ gtk_widget_set_size_request (entry->pad1, pad1w, -1); gtk_widget_set_size_request (entry->entry1, entry1w, -1); gtk_widget_set_size_request (entry->entry2, entry2w, -1); gtk_widget_set_size_request (entry->pad2, pad2w, -1);}GtkWidget *gtk_time_edit_new (void){ return gtk_widget_new (GTK_TYPE_TIME_EDIT, NULL);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -