Starting off in Glade/GTK+

400 篇文章 3 订阅
147 篇文章 0 订阅

 

Starting off in Glade/GTK+

Tutorial By: Ishan Chattopadhyaya
Original Source: geocities.com/ichattopadhyaya/linux/glade.htm

Introduction

How often do we admire the work which the GNOME project has brought out and wish that we too could have created something which could be called "my software" on Linux? Well, if you are interested in developing Linux programs, the best tool that can come to your rescue is Glade .

Now, the question arises, "how do we learn Glade?". Glade is actually a tool that creates the interface, i.e. the windows, the dialogs, the widgets, and creates a framework for your code. It creates the necessary files in the source code where you have to do all the coding manually. Therefore it is quite easy building an interface with this tool. This means, you:

  • Use Glade and create the interface
  • Build the source code using Glade
  • Edit the source code manually (through emacs or gedit)
  • Compile your project files

In this tutorial, you will see how to create an interface and how to link certain widgets with one another.

 

Creating the interface


The Glade Environment (Click for a larger view)

Open Glade. You will see a number of component windows of Glade. They are Glade (Main), Palette, Properties, etc. (See figure above for an example environment ) Proceed as follows:

  •  

  • Click on Window in the Palette (usually the first icon). By this, a window is created which will form the main window for this tutorial program. You’ll see that window1 is added to the Glade window . Double click it. This displays the windows for editing.
  • Select Fixed Position in the Palette (you’ll find it somewhere down) and click in the window1.

[Note for other programmers: Using a fixed position container is easier for all who have previous experience with Microsoft Visual Basic/C++ or Power Builder. Using vertical and horizontal box containers is demonstrated in other tutorials. (See the links below).]

  • Add two text entry widgets , two buttons , one pixmap and two radio buttons to your container in the window1. Also add a list widget.

[Tip: You can move your mouse over the icons in the palette and wait for a second for the ‘tool tips’ to popup. Take a while exploring these tool tips so you are comfortable with the development environment.]

  • Name one button as BT_OK and label it as OK . Name the other button as BT_EXIT and label it as Exit . (You can select the button and change the Name and Label from the Properties window.) Similarly name the two text entry boxes as entry1 and entry2 . Name the radio buttons as radiobutton1 and radiobutton2 and label them as Show Picture and Hide Picture respectively. Name the list as list1 . Name the pixmap as pixmap1 and assign any xpm file of your choice to it.
  • Click on the window1 on the Glade window and from the Properties window, select suitable width and height for your window.
  • Your interface should now look somewhat like this:

  • Now select your OK button. Click on the Signals tab on the Properties window. Here, add a signal as clicked (Click at "..." button and select "Clicked" in the windows that pops up and then click Add in the Properties window. See the figures below! ) with the suggested function name on_BT_OK_clicked . Do the same for the other button . For the radio buttons , add the released signals. In both the occasions, accept the default function name which Glade provides. Click at window1 from the Glade window to change to properties of the Window. In the signals tab of the property window, add a destroy signal.

Adding the Signals (After Clicking "..." button)

Adding the Signals (After Clicking Add button)

 

    • Now save your project. The Project Options dialog will appear. Here select the path of your project, e.g. /home/ [username] /Projects/Project1 . Select C as the programming language. Once you are through with Project Options, click at the Build Source button from the toolbar for Glade to write the code for your interface.

Editing the code

Now that we have successfully created the interface and built the source for it, the time has come to edit the code. Since Glade is not an Integrated Development Environment (IDE) like Visual Basic, Visual C++ etc., you will need to edit the code using an external text editor. Most programmers use emacs as their favorite text editor for coding, but I prefer using gedit , since it has a multiple document interface (MDI). The choice of editor is up to you!

Whichever the text editor it might be, fire it up. Assuming that during building your source you gave /home/ [username] /Projects/Project1 as the directory of your project, open the file /home/ [username] /Projects/Project1/src/callbacks.c in your editor. This is the file where all the callback functions are written. What I mean is that you can assign any functions to the widgets that you connected during adding the signals. So, this file is mainly responsible for the interactivity between the widgets, e.g. buttons, etc.

Scroll down the file until you come across the function on_BT_EXIT_clicked . In the space that is provided within the function, add a function: gtk_main_quit(); Your function should now look like this:

void 
on_BT_EXIT_clicked (GtkButton *button, gpointer user_data)
{

gtk_main_quit();

}

Do the same for the on_window1_destroy function. That is add gtk_main_quit() function there also. This ensures that when the window is closed using the 'X ' button on the upper right, the program ends.

Similarly, add the following code to the on_BT_OK_clicked function:

void
on_BT_OK_clicked (GtkButton *button,
gpointer user_data)

{

/* INITIALIZATIONS START HERE */

GtkWidget *two;
GtkWidget * listitem;
gchar str[50], *text, *p_str = str;
GtkWidget *dialog, *label, *okay_button;

/* COPYING THE TEXT ENTERED IN ENTRY1 TO THE LIST WIDGET */

GtkWidget * entry = lookup_widget(GTK_WIDGET(button), "entry1"); // Getting pointers to
GtkWidget * list = lookup_widget(GTK_WIDGET(button), "list1"); // entry1 & list1
  
strncpy(p_str, gtk_entry_get_text(GTK_ENTRY(entry)),50); // Storing the text of entry1 in the variable p_str
listitem = gtk_list_item_new_with_label (p_str); // Adding p_str to the list
gtk_container_add (GTK_CONTAINER (list), listitem);  
gtk_widget_show(listitem); // Showing the updated list

  

/* COPYING THE TEXT ENTERED IN entry1 TO entry2 */

text = gtk_entry_get_text(entry); // obtain the text from the widget
two = GTK_ENTRY(lookup_widget(button, "entry2")); // obtain the pointer to the second GtkEntry widget
gtk_entry_set_text(two, text); // set the text for the widget

 

/* SHOWING A DIALOG BOX WITH THE TEXT OF entry1 IN IT */

dialog = gtk_dialog_new();  
label = gtk_label_new (text); // Create the widgets of the dialog box
okay_button = gtk_button_new_with_label("Okay");  
 

gtk_signal_connect_object(GTK_OBJECT (okay_button), "clicked", gtk_widget_destroy, GTK_OBJECT(dialog));

// Ensure that the dialog box is destroyed when the user clicks ok.
gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area), okay_button); // Add the Okay Button to the dialof
 
gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label); // Add the label
gtk_widget_show_all (dialog); // Show everything the dialog.
}

 

  Now add the following code to the two radiobutton functions, like this:

void
on_radiobutton1_released (GtkButton *button,
gpointer user_data)

{

GtkPixmap *pix; // Initialisation
pix = GTK_PIXMAP(lookup_widget(button, "pixmap1")); // Getting the pointer to the pixmap
gtk_widget_show(pix); // Show the pixmap

}

void
on_radiobutton2_released (GtkButton *button,
gpointer user_data)

{

GtkPixmap *pix; // Initialisation
pix = GTK_PIXMAP(lookup_widget(button, "pixmap1")); // Getting the pointer to the pixmap
gtk_widget_hide(pix); // Hide the pixmap

}

 

This piece of code is quite simple to understand. What the on_BT_OK_clicked function does is as follows:

  • Copies the text entered by the user in entry1 to entry2
  • Copying the same text to the list widget as an item
  • Creating a dialog box displaying the text of the entry1

Now that the coding part is done, now you have to proceed compiling your project.

Compiling the Project

Open a terminal window. Go to the project directory, i.e. /home/<username>/Projects/Project1 . Before making the project, one needs to make check the configurations of the current setup. For that run ./autogen.sh . After that, type make and when you have successfully made the files without any errors, type make install to complete the installation. Type project1 to run the program!

Overview of this tutorial

This small tutorial will let you understand simple interactions between widgets. This will also explain how to compile and run your projects. I hope you benefit from this tutorial. Please mail in your suggestions to me at ishanchattopadhyaya@hclinfinet.com . Enjoy Glade!

Ishan Chattopadhyaya
Ishan Chattopadhyaya

Other tutorials by Ishan Chattopadhyaya

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值