第一个GTK程序:中国农历 之一

这个程序就是使用lunar-calendar控件,然后显示出农历来。

先用glade-2画出一个日历窗口,设置窗口的销毁事件为调用gtk_main_quit函数,设置一下窗口图标,然后生成代码

第一个GTK程序:中国农历 之一 设计后的glade-2的界面代码
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->

<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">



<glade-interface>



<widget class="GtkWindow" id="lunarcalendar">

  <property name="visible">True</property>

  <property name="title" translatable="yes">Lunar Calendar</property>

  <property name="type">GTK_WINDOW_TOPLEVEL</property>

  <property name="window_position">GTK_WIN_POS_NONE</property>

  <property name="modal">False</property>

  <property name="resizable">True</property>

  <property name="destroy_with_parent">False</property>

  <property name="icon">lunarcalendar.png</property>

  <property name="decorated">True</property>

  <property name="skip_taskbar_hint">False</property>

  <property name="skip_pager_hint">False</property>

  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>

  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>

  <property name="focus_on_map">True</property>

  <property name="urgency_hint">False</property>

  <signal name="destroy" handler="gtk_main_quit" last_modification_time="Sat, 20 Apr 2013 11:12:31 GMT"/>



  <child>

    <widget class="GtkCalendar" id="calendar1">

      <property name="visible">True</property>

      <property name="can_focus">True</property>

      <property name="display_options">GTK_CALENDAR_SHOW_HEADING|GTK_CALENDAR_SHOW_DAY_NAMES|GTK_CALENDAR_NO_MONTH_CHANGE|GTK_CALENDAR_SHOW_WEEK_NUMBERS|GTK_CALENDAR_SHOW_DETAILS</property>

    </widget>

  </child>

</widget>



</glade-interface>

生成的源代码文件有:

src/callbacks.c
callbacks.h
interface.c
interface.h
src/main.c
src/Makefile.am
support.c
support.h

support.c/.h文件中就是一些支撑函数,一般不用管

我们需要关注就是interface.c/.h,这个文件就是创建控件的函数。

按照lunar-calendar说明,将interface.c中调用gtk_calendar_new替换成lunar_calendar_new,同时增加lunar-calendar/lunar-calendar.h的包含

修改完后的源代码如下:

第一个GTK程序:中国农历 之一 生成并修改后的interface.c文件
/*

 * DO NOT EDIT THIS FILE - it is generated by Glade.

 */



#ifdef HAVE_CONFIG_H

#  include <config.h>

#endif



#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <string.h>

#include <stdio.h>



#include <gdk/gdkkeysyms.h>

#include <gtk/gtk.h>

#include <lunar-calendar/lunar-calendar.h> 



#include "callbacks.h"

#include "interface.h"

#include "support.h"



#define GLADE_HOOKUP_OBJECT(component,widget,name) \

  g_object_set_data_full (G_OBJECT (component), name, \

    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)



#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \

  g_object_set_data (G_OBJECT (component), name, widget)



GtkWidget*

create_lunarcalendar (void)

{

  GtkWidget *lunarcalendar;

  GdkPixbuf *lunarcalendar_icon_pixbuf;

  GtkWidget *calendar1;



  lunarcalendar = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_widget_set_name (lunarcalendar, "lunarcalendar");

  gtk_window_set_title (GTK_WINDOW (lunarcalendar), _("Lunar Calendar"));

  lunarcalendar_icon_pixbuf = create_pixbuf ("lunarcalendar.png");

  if (lunarcalendar_icon_pixbuf)

    {

      gtk_window_set_icon (GTK_WINDOW (lunarcalendar), lunarcalendar_icon_pixbuf);

      gdk_pixbuf_unref (lunarcalendar_icon_pixbuf);

    }



  calendar1 = lunar_calendar_new ();

  gtk_widget_set_name (calendar1, "calendar1");

  gtk_widget_show (calendar1);

  gtk_container_add (GTK_CONTAINER (lunarcalendar), calendar1);

  gtk_calendar_display_options (GTK_CALENDAR (calendar1),

                                GTK_CALENDAR_SHOW_HEADING

                                | GTK_CALENDAR_SHOW_DAY_NAMES

                                | GTK_CALENDAR_NO_MONTH_CHANGE

                                | GTK_CALENDAR_SHOW_WEEK_NUMBERS);



  g_signal_connect ((gpointer) lunarcalendar, "destroy",

                    G_CALLBACK (gtk_main_quit),

                    NULL);



  /* Store pointers to all widgets, for use by lookup_widget(). */

  GLADE_HOOKUP_OBJECT_NO_REF (lunarcalendar, lunarcalendar, "lunarcalendar");

  GLADE_HOOKUP_OBJECT (lunarcalendar, calendar1, "calendar1");



  return lunarcalendar;

}

到此,我们完成了第一步,生成代码

代码压缩为:lunarcalendar.0.0.1.zip

你可能感兴趣的:(gtk)