📄 callbacks.c
字号:
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- *//* * callbacks.c * Copyright (C) Nick Z Liu 2008 <nick@> * * callbacks.c is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * callbacks.c is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see <http://www.gnu.org/licenses/>. */#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <gtk/gtk.h>#include "callbacks.h"/*******************added by nick********************/#include <string.h>static gbooleanis_operator(const gchar c){ switch(c) { case '+': case '-': case '*': case '/': case '(': case ')': return TRUE; default: return FALSE; }}static voidinsert_char(char* str, int index, char c){ int i; g_print("insert_char str:%s; index:%d; c:%c; len:%d\n", str, index, c, strlen(str)); for(i=strlen(str); i>index; i--) str[i] = str[i-1]; str[i] = c; g_print("insert_char str:%s\n", str);}#ifdef USE_GLADEvoid number_button_clicked_cb(gpointer user_data, GtkButton *button)#elsevoid number_button_clicked_cb(GtkButton *button, gpointer user_data)#endif{ gchar* label = (gchar*)gtk_button_get_label(button); gchar* pre_entry_text = (gchar*)gtk_entry_get_text(GTK_ENTRY(user_data)); gint pre_entry_text_len = strlen(pre_entry_text); gint entry_max_len = gtk_entry_get_max_length(GTK_ENTRY(user_data)); g_print("label:%s\n", label); if(g_strcmp0((const char*)label, _(".")) == 0 || g_strcmp0((const char*)label, _("0")) == 0 || g_strcmp0((const char*)label, _("1")) == 0 || g_strcmp0((const char*)label, _("2")) == 0 || g_strcmp0((const char*)label, _("3")) == 0 || g_strcmp0((const char*)label, _("4")) == 0 || g_strcmp0((const char*)label, _("5")) == 0 || g_strcmp0((const char*)label, _("6")) == 0 || g_strcmp0((const char*)label, _("7")) == 0 || g_strcmp0((const char*)label, _("8")) == 0 || g_strcmp0((const char*)label, _("9")) == 0 || g_strcmp0((const char*)label, _("+")) == 0 || g_strcmp0((const char*)label, _("-")) == 0 || g_strcmp0((const char*)label, _("*")) == 0 || g_strcmp0((const char*)label, _("/")) == 0) { if(pre_entry_text_len == 1 && pre_entry_text[0] == '0') { gtk_entry_set_text(GTK_ENTRY(user_data), label); } else { if(pre_entry_text_len < entry_max_len) { gchar* cur_entry_text = (gchar*)g_malloc0(pre_entry_text_len+2); g_assert(cur_entry_text); memcpy(cur_entry_text, pre_entry_text, pre_entry_text_len); cur_entry_text[pre_entry_text_len] = *label; gtk_entry_set_text(GTK_ENTRY(user_data), cur_entry_text); g_free(cur_entry_text); } else { //exceed the max entry len } } } else if(g_strcmp0((const char*)label, _("=")) == 0) { extern double bc(char *in,char *ERR); int i=0,j=0,k=0; char error[20] = {0}; double result; gchar* cur_entry_text; for(i=0; i<pre_entry_text_len; i++) { if(pre_entry_text[i] == '-' && pre_entry_text[i+1] == '(') j++; } cur_entry_text = (gchar*)g_malloc0(pre_entry_text_len+3*j+1); memcpy(cur_entry_text, pre_entry_text, pre_entry_text_len); g_print("in string:%s; j:%d\n", cur_entry_text, j); while(cur_entry_text[k] != '\0') { g_print("k:%d\n", k); if(cur_entry_text[k] == '-' && cur_entry_text[k+1] == '(') { insert_char(cur_entry_text, k++, '('); insert_char(cur_entry_text, k++, '0'); } else if(cur_entry_text[k] == ')') insert_char(cur_entry_text, k++, ')'); else ;//do nothing k++; } g_print("out string:%s\n", cur_entry_text); result = bc(cur_entry_text, error); g_print("result:%f\n", result); if(error[0] == 0) { g_ascii_dtostr(cur_entry_text, entry_max_len, result); gtk_entry_set_text(GTK_ENTRY(user_data), cur_entry_text); } else gtk_entry_set_text(GTK_ENTRY(user_data), error); g_free(cur_entry_text); } else if(g_strcmp0((const char*)label, _("+/-")) == 0) { if(pre_entry_text_len == 1 && pre_entry_text[0] == '0') { //gtk_entry_set_text(GTK_ENTRY(user_data), label); } else { if(pre_entry_text_len+3 <= entry_max_len) { gchar* cur_entry_text = (gchar*)g_malloc0(pre_entry_text_len+4); g_assert(cur_entry_text); cur_entry_text[0] = '-'; cur_entry_text[1] = '('; memcpy(cur_entry_text+2, pre_entry_text, pre_entry_text_len); cur_entry_text[pre_entry_text_len+2] = ')'; gtk_entry_set_text(GTK_ENTRY(user_data), cur_entry_text); g_free(cur_entry_text); } else { //exceed the max entry len } } } else if(g_strcmp0((const char*)label, _("Bksp")) == 0) { if(pre_entry_text_len > 1) { gchar* cur_entry_text = (gchar*)g_malloc0(pre_entry_text_len); g_assert(cur_entry_text); memcpy(cur_entry_text, pre_entry_text, pre_entry_text_len-1); gtk_entry_set_text(GTK_ENTRY(user_data), cur_entry_text); g_free(cur_entry_text); } else if(pre_entry_text_len == 1) { if(pre_entry_text[0] != '0') gtk_entry_set_text(GTK_ENTRY(user_data), "0"); } else { g_assert(0); } } else if(g_strcmp0((const char*)label, _("CE")) == 0) { if(pre_entry_text_len == 1) { gtk_entry_set_text(GTK_ENTRY(user_data), "0"); } else { gint i = pre_entry_text_len-1; gchar* cur_entry_text; while(!is_operator(pre_entry_text[i]) && i>=0) { i--; } g_print("i=%d\n", i); if(i == pre_entry_text_len-1) { if(pre_entry_text[i] == ')')//"-()" { if(pre_entry_text[0] == '-' && pre_entry_text[1] == '(') { cur_entry_text = (gchar*)g_malloc0(i-2); g_assert(cur_entry_text); memcpy(cur_entry_text, pre_entry_text+2, pre_entry_text_len-3); gtk_entry_set_text(GTK_ENTRY(user_data), cur_entry_text); g_free(cur_entry_text); } } else//'+','-','*','/' { gchar* cur_entry_text = (gchar*)g_malloc0(pre_entry_text_len); g_assert(cur_entry_text); memcpy(cur_entry_text, pre_entry_text, pre_entry_text_len-1); gtk_entry_set_text(GTK_ENTRY(user_data), cur_entry_text); g_free(cur_entry_text); } } else if(i == -1) { gtk_entry_set_text(GTK_ENTRY(user_data), "0"); } else { cur_entry_text = (gchar*)g_malloc0(i+2); g_assert(cur_entry_text); memcpy(cur_entry_text, pre_entry_text, i+1); gtk_entry_set_text(GTK_ENTRY(user_data), cur_entry_text); g_free(cur_entry_text); } } } else if(g_strcmp0((const char*)label, _("Clr")) == 0) { gtk_entry_set_text(GTK_ENTRY(user_data), "0"); } else { g_assert(0); }}void on_window_delete_event(gpointer user_data, GtkWidget* widget){ g_print("on_window_delete_event\n");}void on_window_destroy_event(gpointer user_data, GtkWidget* widget){ g_print("on_window_destroy_event\n");}void on_window_remove(gpointer user_data, GtkWidget *widget){ g_print("on_window_remove\n");}#ifdef USE_GLADE//gint on_imagemenuitem_about_delete(GtkWidget *widget, GdkEvent *event, gpointer data) //unknow para order#elsegint on_imagemenuitem_about_delete(GtkWidget *widget, GdkEvent *event, gpointer data)#endif{ GtkWidget **pWindow = (GtkWidget**)data; *pWindow = NULL; g_print ("on_imagemenuitem_about_delete\n"); return FALSE;}#ifdef USE_GLADEvoid on_imagemenuitem_about_activate(gpointer user_data, GtkWidget *widget)#elsevoid on_imagemenuitem_about_activate(GtkWidget *widget, gpointer user_data)#endif{ GtkWidget **pWindow = (GtkWidget**)user_data; GtkLabel *about_content_label; g_print ("on_imagemenuitem_about_select pointer=%d\n", (gint)(*pWindow)); if(*pWindow == NULL) { /* 创建一个新窗口 */ *pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(*pWindow), _("About")); gtk_window_set_resizable(GTK_WINDOW(*pWindow), TRUE); gtk_widget_set_size_request(GTK_WIDGET(*pWindow), 150, 100);//nick temp gtk_window_set_position(GTK_WINDOW(*pWindow), GTK_WIN_POS_CENTER_ALWAYS); gtk_window_set_resizable(GTK_WINDOW(*pWindow), FALSE); g_signal_connect (G_OBJECT(*pWindow), "delete_event", G_CALLBACK(on_imagemenuitem_about_delete), user_data); /* 创建一个vbox */ about_content_label = (GtkLabel*)gtk_label_new(_("Hello!\nThis is about.")); gtk_container_add(GTK_CONTAINER(*pWindow), (GtkWidget*)about_content_label); gtk_widget_show_all(*pWindow); }}/*****************end added by nick******************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -