第16章 GTK+ gnome编程

时间:2022-03-20 06:56:30

gtk-demo 展示所有构建外观

pkg-config --cflag --libs gtk+-2.0   //man pkg-config to see information

编译是:

  gcc file.c -o filename `pkg-config --cflag --libs gtk+-2.0`  // $(pkg-config ..........)  as well

必须先初始化:

  gtk_init( &arc, &argv);    // int main( int argc, char *argv[]);

创建:

  GtkWidget * gtk_window_new( GtkWindowType type); 

  type可以取2个值中的一个:GTK_WINDOW_TOPLEVEL: 标准有框架窗口

              GTK_WINDOW_POPUP: 适用于对话框无框架窗口

显示:

  gtk_widget_show(GtkWidget * window);

 

  gtk_main: 把控制权交给GTK+,一直运行直到gtk_main_quit 返回

 

回调函数:

  void a_callback_function( GtkWidget * widget, gpointer user_data);  //gpointer为void *型

连接回调函数:

  gulong g_signal_connect( gpointer *object, const gchar *name, Gcallback func,

              gpointer user_data);

gtk_container_add( GtkContainer *container,Gtkwidget *widget);

使用宏转换类型:

GTK_WIDGET(widget)
GTK_OBJECT(object)
GTK_SIGNAL_FUNC(function)    //G_CALLBACK        USED IN 2.0?
GTK_CONTAINER(container)
GTK_WINDOW(window)
GTK_BOX(box)

A GtkButton is a simple button widget that can contain text, in this case “Hello World,” and emits a signal called “clicked” whenever the button is pressed with the mouse.

盒子:

  GtkWidget*  gtk_hbox_new (gboolean homogeneous, gint spacing); //horizontal
  GtkWidget* gtk_vbox_new (gboolean homogeneous, gint spacing);  //vertical

homogeneous is a boolean that, when set to TRUE, forces contained widgets to occupy equal space, regardless of individual size. spacing sets the gap between widgets in pixels.

 

add widgets using gtk_box_pack_start and gtk_box_pack_end functions:
  void gtk_box_pack_start (GtkBox *box, GtkWidget *child, gboolean expand, gboolean fill,

              guint padding);
  void gtk_box_pack_end (GtkBox *box, GtkWidget *child, gboolean expand, gboolean fill,
              guint padding);

 

GtkWindow:

GtkWidget
  +----GtkContainer
    +----GtkBin
      +----GtkWindow

GtkWidget* gtk_window_new (GtkWindowType type);
void gtk_window_set_title (GtkWindow *window, const gchar *title);
void gtk_window_set_position (GtkWindow *window, GtkWindowPosition position);
void gtk_window_set_default_size (GtkWindow *window, gint width, gint height);
void gtk_window_resize (GtkWindow *window, gint width, gint height);
void gtk_window_set_resizable (GtkWindow *window, gboolean resizable);
void gtk_window_present (GtkWindow *window);
void gtk_window_maximize (GtkWindow *window);
void gtk_window_unmaximize (GtkWindow *window);

 

 

GtkEntry   — A single line text entry field

GtkWidget* gtk_entry_new (void);
GtkWidget* gtk_entry_new_with_max_length (gint max);
void gtk_entry_set_max_length (GtkEntry *entry, gint max);
G_CONST_RETURN gchar* gtk_entry_get_text (GtkEntry *entry);
void gtk_entry_set_text (GtkEntry *entry, const gchar *text);
void gtk_entry_append_text (GtkEntry *entry, const gchar *text);
void gtk_entry_prepend_text (GtkEntry *entry, const gchar *text);
void gtk_entry_set_visibility (GtkEntry *entry, gboolean visible);
void gtk_entry_set_invisible_char (GtkEntry *entry, gchar invch);
void gtk_entry_set_editable (GtkEntry *entry, gboolean editable);

 

GtkSpinButton

 只输入数字字符. 带+ - 号用鼠标点击以增减.

函数:

GtkWidget* gtk_spin_button_new (GtkAdjustment *adjustment, gdouble climb_rate,guint digits);     //climb_rate 压住鼠标,数字变化的速度 , digits, 数字精度
GtkWidget* gtk_spin_button_new_with_range (gdouble min, gdouble max, gdouble step);
void gtk_spin_button_set_digits (GtkSpinButton *spin_button, guint digits);
void gtk_spin_button_set_increments (GtkSpinButton *spin_button, gdouble step,gdouble page);
void gtk_spin_button_set_range (GtkSpinButton *spin_button, gdouble min,gdouble max);
gdouble gtk_spin_button_get_value (GtkSpinButton *spin_button);
gint gtk_spin_button_get_value_as_int (GtkSpinButton *spin_button);
void gtk_spin_button_set_value (GtkSpinButton *spin_button, gdouble value);

 

GtkObject* gtk_adjustment_new (gdouble value, gdouble lower, gdouble upper,

              gdouble step_increment, gdouble page_increment,gdouble page_size);

例:

adjustment = gtk_adjustment_new(100.0, 50.0, 150.0, 0.5, 0.05, 0.05);  //adjustment为GtkObject型 0.05 为页控制,无用
spinbutton = gtk_spin_button_new(GTK_ADJUSTMENT(adjustment), 0.01, 2);

 

GtkButton:

 GtkToggleButton:

    GtkWidget* gtk_toggle_button_new (void);
    GtkWidget* gtk_toggle_button_new_with_label (const gchar *label);
    gboolean gtk_toggle_button_get_active (GtkToggleButton *toggle_button);
    void gtk_toggle_button_set_active (GtkToggleButton *toggle_button,gboolean is_active);

 GtkCheckButton:  GtkCheckButton appears as an exciting checkbox with text at the side.

    GtkWidget* gtk_check_button_new (void);
    GtkWidget* gtk_check_button_new_with_label (const gchar *label);

 GtkRadioButton:  it can be grouped with other buttons of the same type

    GtkWidget* gtk_radio_button_new (GSList *group);  //group为NULL,创建新组
    GtkWidget* gtk_radio_button_new_from_widget (GtkRadioButton *group);
    GtkWidget* gtk_radio_button_new_with_label (GSList *group, const gchar *label);
    void gtk_radio_button_set_group (GtkRadioButton *radio_button, GSList *group);
    GSList* gtk_radio_button_get_group (GtkRadioButton *radio_button);

 

GtkTreeView: 有四个组件

    GtkTreeView : The Tree and List View
    GtkTreeViewColumn : Represents a list or tree column
    GtkCellRenderer : Controls drawing cells
    GtkTreeModel : Represents Tree and List data

    前3个为视图,后一个为模型      //mvc模式: model,view,control

 

  模型组件:  GtkTreeStore,GtkListStore

  Gtkwidget *store = gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_INT,G_TYPE_BOOLEAN);

  GtkTreeIter iter;
  gtk_tree_store_append (store, &iter, NULL);

 

  gtk_tree_store_set (store, &iter,0, “Def Leppard”,1, 1987,2, TRUE,-1);  //terminated by –1

 

To add a branch to this row (a child row), you just need an iterator for the child row that you get by
calling gtk_tree_store_append again, passing in the top-level row this time:
  GtkTreeIter child;
  gtk_tree_store_append (store, &child, &iter);

 

Creating a GtkTreeView is simplicity itself; just pass in the GtkTreeStore or GtkListStore model to
the constructor:

  GtkWidget *view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));

use the text renderer , GtkCellRendererText :
  GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(view),0,"This is the column title”,renderer,“text”, 0,NULL);

最后:

gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);  

 

使用gnome库:

  gcc a.c `pkg-config --cflags --libs libgnome-2.0 libgnomeui-2.0`

初始化:   GnomeProgram* gnome_program_init (const char *app_id, const char *app_version,const GnomeModuleInfo *module_info,

                    int argc,char **argv,const char *first_property_name,...)

例:   GtkWidget *app;
    gnome_program_init (“gnome1”, “1.0”, MODULE, argc, argv, NULL);
    app = gnome_app_new (“gnome1”, “The Window Title”);
    gtk_widget_show(app);
    gtk_main ();
    return 0;

gnome 菜单: 由一个GNOMEUIInfo结构数组表示

  typedef struct {

    GnomeUIInfoType type;
    gchar const *label;
    gchar const *hint;
    gpointer moreinfo;
    gpointer user_data;
    gpointer unused_data;
    GnomeUIPixmapType pixmap_type;
    gconstpointer pixmap_info;
    guint accelerator_key;
    GdkModifierType ac_mods;
    GtkWidget *widget;
  } GnomeUIInfo;

The first item in the struct, GnomeUIInfoType 含10种类型:

NOME_APP_UI_ENDOFINFO       Denotes that this is last menu item in the array.


GNOME_APP_UI_ITEM          A normal menu item or a radio button if preceded by
                      a GNOME_APP_UI_RADIOITEMS entry.


GNOME_APP_UI_TOGGLEITEM      A toggle button or check button menu item.


GNOME_APP_UI_RADIOITEMS       A radio button group.


GNOME_APP_UI_SUBTREE         Denotes this element is a submenu. Set moreinfo to
                       point to the submenu array.


GNOME_APP_UI_SEPARATOR        Inserts a separator line in the menu.


GNOME_APP_UI_HELP           Creates a help topic list for use in a Help menu.


GNOME_APP_UI_BUILDER_DATA       Specifies builder data for the following entries.


GNOME_APP_UI_ITEM_CONFIGURABLE     A configurable menu item.


GNOME_APP_UI_SUBTREE_STOCK       Same as GNOME_APP_UI_SUBTREE except                         that the label text should be looked up in the                           gnome-libs catalog.


GNOME_APP_UI_INCLUDE           Same as GNOME_APP_UI_SUBTREE                           except that the items are included in the current                         menu and not as a submenu.

 

The second and third members of the struct define the text of the menu item and the pop-up hint. (The hint is shown in the status bar at the bottom of the window.)  

The purpose of moreinfo depends on type. For ITEM and TOGGLEITEM , it points to the callback function to be called when the menu item is activated. For RADIOITEMS it points to an array of GnomeUIInfo structs that are grouped radio buttons.

user_data is an arbitrary pointer passed to the callback function. pixmap_type and pixmap_info
allow you to add a bitmap icon to the menu item, and accelerator_key and ac_mods help you define
a keyboard shortcut.

Finally, widget is used to internally hold the menu item widget pointer by the menu creation function

 

 gnome_app_create_menus(GNOME_APP(app),menubar);

 

 

 

There are two sets of macros, the first of which defines individual menu items. They take two parameters,the callback function pointer and user data.

#include <libgnomeui/libgnomeui.h>

#define  GNOMEUIINFO_MENU_OPEN_ITEM  (cb, data)
#define  GNOMEUIINFO_MENU_SAVE_ITEM  (cb, data)
#define  GNOMEUIINFO_MENU_SAVE_AS_ITEM  (cb, data)
#define  GNOMEUIINFO_MENU_PRINT_ITEM  (cb, data)
#define  GNOMEUIINFO_MENU_PRINT_SETUP_ITEM  (cb, data)
#define  GNOMEUIINFO_MENU_CLOSE_ITEM  (cb,data)
#define  GNOMEUIINFO_MENU_EXIT_ITEM  (cb,data)
#define  GNOMEUIINFO_MENU_QUIT_ITEM  (cb,data)
#define  GNOMEUIINFO_MENU_CUT_ITEM  (cb,data)
#define  GNOMEUIINFO_MENU_COPY_ITEM  (cb,data)
#define  GNOMEUIINFO_MENU_PASTE_ITEM  (cb,data)
#define  GNOMEUIINFO_MENU_SELECT_ALL_ITEM(cb,data)
... etc

And the second set is for top-level definitions to which you simply pass the array:

#define  GNOMEUIINFO_MENU_FILE_TREE  (tree)
#define  GNOMEUIINFO_MENU_EDIT_TREE  (tree)
#define  GNOMEUIINFO_MENU_VIEW_TREE  (tree)
#define  GNOMEUIINFO_MENU_SETTINGS_TREE  (tree)
#define  GNOMEUIINFO_MENU_FILES_TREE  (tree)
#define  GNOMEUIINFO_MENU_WINDOWS_TREE  (tree)
#define  GNOMEUIINFO_MENU_HELP_TREE  (tree)
#define  GNOMEUIINFO_MENU_GAME_TREE  (tree)