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

📄 main.c

📁 用GTK实现的一个简单的计算器程序
💻 C
字号:
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- *//* * main.c * Copyright (C) Nick Z Liu 2008 <nick@> *  * main.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. *  * main.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/>. */#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <string.h>#include <stdio.h>#include <config.h>#include <gtk/gtk.h>#include <glade/glade.h>#include "mult.h"#include "callbacks.h"#include <dbus/dbus-glib.h>#include <libosso.h>#ifdef USE_LIBDBUSextern int start_service (void);#elseextern int start_service (osso_context_t* osso_context);#endif/* For testing propose use the local (not installed) glade file *//* #define GLADE_FILE PACKAGE_DATA_DIR"/calculator/glade/calculator.glade" */#define GLADE_FILE "calculator.glade"GtkWidget*create_window (void){	GtkWidget *window;	GladeXML *gxml;		gxml = glade_xml_new (GLADE_FILE, NULL, NULL);		/* This is important */	glade_xml_signal_autoconnect (gxml);	window = glade_xml_get_widget (gxml, "window");		return window;}/* 按下关闭窗口按钮会发出delete_event*/gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data){    /* 如果你的 "delete_event" 信号处理函数返回 FALSE,GTK 会发出 "destroy" 信号。     * 返回 TRUE,你不希望关闭窗口。     * 当你想弹出“你确定要退出吗?”对话框时它很有用。*/    g_print ("delete event occurred\n");    /* 改 TRUE 为 FALSE 程序会关闭。*/    return FALSE;}#ifdef USE_GLADEvoid destroy( gpointer   data, GtkWidget *widget)#elsevoid destroy( GtkWidget *widget, gpointer   data )#endif{	g_print ("destroy event occurred\n");    gtk_main_quit ();}intmain (int argc, char *argv[]){ 	GtkWidget *window;#ifndef USE_GLADE 	GtkVBox* vbox; 	GtkMenuBar* menubar;    GtkMenuItem* menuitem_about; 	GtkWidget *about_window = NULL; 	GtkEntry* entry; 	GtkTable* table; 	GtkButton* button[5][4];    gint i, j;#endif    osso_context_t* ossoContext = NULL;#ifdef ENABLE_NLS	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");	textdomain (GETTEXT_PACKAGE);#endif	gtk_set_locale ();	gtk_init (&argc, &argv);    /*********LibOSSO init************/    g_print("Initializing LibOSSO\n");	/* The program name for registration is communicated from the		Makefile via a -D preprocessor directive. Since it doesn't		 contain any dots in it, a prefix of "com.nokia." will be added		 to it internally within osso_initialize(). */	ossoContext = osso_initialize("SampleService", "1.0", FALSE, NULL);    if (ossoContext == NULL) {       g_error("Failed to initialize LibOSSO\n");    }    /*******end of LibOSSO init*******/#ifdef USE_LIBDBUS    if(start_service() != 0)#else    if(start_service(ossoContext) != 0)#endif    {        g_print("There is a ap instance is running already!\n");        return 0;    }	#ifdef USE_GLADE	window = create_window ();	gtk_widget_show (window);#else 	/* 创建一个新窗口 */ 	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);    gtk_window_set_title(GTK_WINDOW(window), _("Calculator"));	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);	gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 	/* 创建一个vbox */ 	vbox = (GtkVBox*)gtk_vbox_new(FALSE, 10);//nick temp 	gtk_container_add(GTK_CONTAINER(window), (GtkWidget*)vbox);		 	/* 创建一个菜单条 */ 	menubar = (GtkMenuBar*)gtk_menu_bar_new();    menuitem_about = (GtkMenuItem*)gtk_menu_item_new_with_label(_("About"));    //sub menu&short cut	g_signal_connect (G_OBJECT(menuitem_about), "activate", G_CALLBACK(on_imagemenuitem_about_activate), (gpointer)(&about_window));	gtk_menu_bar_append(menubar,(GtkWidget*)menuitem_about); 	/* 创建一个输入框 */ 	entry = (GtkEntry*)gtk_entry_new_with_max_length(20);//nick temp    gtk_entry_set_alignment(entry, 1); 	gtk_entry_set_text(entry, "0"); 	/* 创建一个table */ 	table = (GtkTable*)gtk_table_new(5, 4, TRUE);		 	button[0][0] = (GtkButton*)gtk_button_new_with_label(_("Bksp")); 	button[0][1] = (GtkButton*)gtk_button_new_with_label(_("CE")); 	button[0][2] = (GtkButton*)gtk_button_new_with_label(_("Clr")); 	button[0][3] = (GtkButton*)gtk_button_new_with_label(_("+/-")); 	button[1][0] = (GtkButton*)gtk_button_new_with_label(_("7")); 	button[1][1] = (GtkButton*)gtk_button_new_with_label(_("8")); 	button[1][2] = (GtkButton*)gtk_button_new_with_label(_("9")); 	button[1][3] = (GtkButton*)gtk_button_new_with_label(_("/")); 	button[2][0] = (GtkButton*)gtk_button_new_with_label(_("4")); 	button[2][1] = (GtkButton*)gtk_button_new_with_label(_("5")); 	button[2][2] = (GtkButton*)gtk_button_new_with_label(_("6")); 	button[2][3] = (GtkButton*)gtk_button_new_with_label(_("*")); 	button[3][0] = (GtkButton*)gtk_button_new_with_label(_("1")); 	button[3][1] = (GtkButton*)gtk_button_new_with_label(_("2")); 	button[3][2] = (GtkButton*)gtk_button_new_with_label(_("3")); 	button[3][3] = (GtkButton*)gtk_button_new_with_label(_("-")); 	button[4][0] = (GtkButton*)gtk_button_new_with_label(_("0")); 	button[4][1] = (GtkButton*)gtk_button_new_with_label(_(".")); 	button[4][2] = (GtkButton*)gtk_button_new_with_label(_("=")); 	button[4][3] = (GtkButton*)gtk_button_new_with_label(_("+")); 	for(i=0; i<5; i++) 	 	for(j=0; j<4; j++)        { 	 	 	g_signal_connect(G_OBJECT(button[i][j]), "clicked", G_CALLBACK(number_button_clicked_cb), (gpointer)entry);            gtk_table_attach_defaults(table, (GtkWidget*)button[i][j], j, j+1, i, i+1);		} 	gtk_table_set_row_spacings(table, 3); 	gtk_table_set_col_spacings(table, 3); 	/* 创建一个table */ 	gtk_box_pack_start(GTK_BOX(vbox), (GtkWidget*)menubar, TRUE, TRUE, 10); 	gtk_box_pack_start(GTK_BOX(vbox), (GtkWidget*)entry, TRUE, TRUE, 10); 	gtk_box_pack_start(GTK_BOX(vbox), (GtkWidget*)table, TRUE, TRUE, 10);	g_signal_connect (G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);    g_signal_connect (G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);    gtk_widget_show_all(window);#endif	gtk_main ();	/* Deinitialize LibOSSO. The function doesn't return status code so		 we cannot know whether it succeeded or failed. We assume that it		 always succeeds. */	osso_deinitialize(ossoContext);	ossoContext = NULL;	return 0;}

⌨️ 快捷键说明

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