Http://rat.nutn.edu.tw /~ Slayer/myarticle/gtk_tree_view_tutorial.html
Author: qrtt1 2006/07/11 Preface
It is not a "simple" task to use gtktreeview. I specifically included the ticket here because I want to remind you. I don't want to imply that it is very difficult for you to think of him. Here I chose another meaning-traditional. Step by step is a little more, but it is not a concept of understanding. Maybe you have already taught the long-standing GTK + 2.0 Tree View tutorial (Tim-Philipp mler, 2005) author to hope that he can solve most of the problems, therefore, the degree of space and microservices is of course required. However, every author has a different article, and his younger brother will write a short article about it.
Do you need the MVC concept?
While absorbing the GTK + 2.0 Tree View tutorial, I wrote the MVC design pattern. In retrospect, do you really need the concept of MVC to learn how to use gtktreeview? Although the library of GTK must use some MVC design ideas, but for users, we do not necessarily understand where MVC is focused, it is confusing to keep coming up with the idea. But when you want to learn how to use gtktreeview, you can no longer ignore MVC because it is a semi-finished product of MVC. Just like playing with the word, you need to fill in useful information on the grid, and then submit it to make the entire website perfect.
Here, I am not going to explain MVC in depth. However, you must first define the "semantic meaning" for the MVC letters; m-model, you can regard it as "material ", there are also a set of built-in functions (such as adding, removing, sorting, and querying) for these "resources"; V-view, in GTK, you can think of it as Widgets. You can refer to view any information that can be presented to users. C-controller, which uses a method that is less accurate than the limit. The response of lateral navigation view and model is highlighted.
After naming, we can understand the interaction mode of MVC. This is like a triangular relationship, but it is just like a matter of fact that each role has its own personality. View is always a matter of relief. I didn't have my own opinions. I thought the model was my own idea. The model was always self-centered and the whole world was subject to him, the Controller is the only friend who trusts the model. The model only makes changes for the Controller, and the relationship between the Controller and the view is also wonderful, view is the only one who allows the Controller to do something. They look at the back of a person, and they just look at the two.
After the story is heard, we will return to the situations where the story is written. The model is the information that the program needs to manipulate. Without the information program, it does not have the meaning of existence. However, we want to compile a GUI program. When a button on the left and a button on the right are very easy to trigger something accidentally, the model can be changed directly when this button is released, however, the component components that apply to model compute components on the GUI have not changed to dirty data, of course, you can also choose to update the components that display the metric chart at a more dynamic time. However, this is not ideal. When the value of these components needs to interact with other non-dynamic resources, the program's attention will increase. Gui (View) is not an ideal design.
Here, you need to know whether the model has been changed, and view should also make a response to the change. Our predecessors thought about not changing the model in the background, but updating the view awkwardly. Come up with another kind of design: Don't call me, I'll call you "[1]. The model said to the view, don't look for me. I will look for you if there is something. In this way, the role of the primary node changes accordingly, so that the model primary node notifies the view. For a view, he never takes the initiative. Some changes have been made in life. It's quite easy, so I can't find something to do. A common update function is provided for this change, and you can log on to the well-known feature of the model so that the model can notify him of the update function when the model changes. (M and v)
Zookeeper mentioned "changing data" and thought about the starting point of the change. Isn't it intended for all users before the TV industry? You are enjoying the GUI program. There may be many Referer threads on it, and there may be some places for you to share your emotions. Any such action may change the model. However, in the MVC thinking mode, the view does not directly change the model, but asks the Controller to change the model. The Controller is usually a set of functions provided in the view, representing the rows in the view. Of course, some operations won't change the model. (V and C)
Model/View/controller in gtktree * Groups
In the previous section, I introduced the concept of MVC. In fact, I agree that this is a very stealing method. MVC is actually a big problem! Let's take a look at gtktreeview, gtktreeviewcolumn, gtktreemodel, gtkcellrenderer, and gtktreeiter. First, let's take a look at the classification of "regionalization. Gtktreeview is the interface of the entire widget. Only the gtktreeview does not really make our program useful. You also need the assistance of the gtktreemodel to hold the information. The present mode of each item is not the same, so you still need gtkcellrenderer for help.
- let's take a look at the gtktreeview and gtktreemodel sections. In the preceding example, the model notifies the view to update itself. The view changes the model through the Controller. Here, I wrote an imaginary imaginarygtkcontroller, which represents the set of all the gtktreemodel "available" rows. Generally, the controller is designed in the form of strategy design pattern (the following zookeeper DP. When DP is seen, it is replaced by a pattern that combines the names of previous users with different names. Some things are often used by us, and some may not be used, but they go deep into the design philosophy of various suites. The main spirit of strategy is the interval between change and change. We operate the model through a group of methods, which is our controller. However, we didn't want to think about this by setting up a controller, instead of setting up the operations that the model should have, create an interface for every line as an unrealistic interface. Different models are operated by different controllers, but they all use the same standardized method. In fact, the operations are different. In practice, it can be extracted, and the adequacy of the cage structure. However, GTK is made of C segments. If it is not true to support the statement of OO, it is necessary to introduce similar concepts. Therefore, in this part of my imagination, we have actually implemented all the Controller class differences related to these operations. Maybe you want to understand what the controller is about to use? In this example, two devices are provided: gtk_list_store_append and gtk_list_store_set, which are all assigned to the resource used to operate the model content. Therefore, they are controller.
- In view, the other person who uses the library feels that the view is not controlled by gtktreeview, it also involves gtktreeviewcolumn and various types of gtkcellrenderer. They contain one in sequence. Note! I have mentioned the content here. Please use it as a "container ". Gtktreeview is the container of javasgtktreeviewcolumn and gtktreeviewcolumn is the container of gtkcellrenderer. However, they place different "dynamic words. Only one or two objects are added in the topology. The gtktreeview adds a new column in append mode, and the gtktreeviewcolumn adds a new cell Renderer in pack mode. Finally, you may want to see how to put the model into the view. Then, let's look back at the progress on the view. The gtk_tree_view_set_model is his.
-
- After the concept of "detail" is greater than "detail", you need to remind me of "detail is Ghost ". This is related to the gobject memory management mode. This document is not intended to be written here. It only reminds you that you must call g_object_unref once to use some objects, let the picture taken by this item be taken. Gobject has its own garbage collection mechanism to recycle memories that are no longer in use, in this example, "When a gobject is no longer referenced by other objects (that is, when the reference count is 0), it is automatically recycled. When using the components and objects related to gtktreeview, we need to focus on whether they will reach the auto-recovery standard, however, there are no rules in this article. Please refer to these tips :)
In terms of model, gtktreemodel and its derivative types such as gtktreestore and gtkliststore require g_object_unref processing. In terms of view, gtkcellrendererpixbuf also requires such processing.
A example based on gtkliststore
Using the hands on the gtktreeview may also be a little complicated, but that is the case where the view creates the gtktreeview, gtktreeviewcolumn, and gtkcellrenderer. The model creates the gtkliststore or gtktreestore. Finally, use the gtk_tree_view_set_model to bring them together.
GTK application sketch
# Include <GTK/GTK. h>
# Include <glib. h>
Int main (INT argc,
Char * argv []) {
/* Gtkwidget is the storage type for widgets */
Gtkwidget * window;
/* This is called in all GTK applications. arguments are parsed
* From the command line and are returned to the application .*/
Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "Tree ");
G_signal_connect (g_object (window), "Destroy", gtk_main_quit, null );
Gtk_widget_show_all (window );
/* All GTK applications must have a gtk_main (). Control ends here
* And waits for an event to occur (like a key press or
* Mouse event ).*/
Gtk_main ();
Return 0;
}
Add gtktreeview widget
# Include <GTK/GTK. h>
# Include <glib. h>
Int main (INT argc,
Char * argv []) {
/* Gtkwidget is the storage type for widgets */
Gtkwidget * window;
/* This is called in all GTK applications. arguments are parsed
* From the command line and are returned to the application .*/
Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "Tree ");
G_signal_connect (g_object (window), "Destroy", gtk_main_quit, null );
Gtkwidget * view;
View = gtk_tree_view_new ();
Gtk_container_add (gtk_container (window), view );
Gtk_widget_show_all (window );
/* All GTK applications must have a gtk_main (). Control ends here
* And waits for an event to occur (like a key press or
* Mouse event ).*/
Gtk_main ();
Return 0;
}
- We added a new gtktreeview, but we haven't set its content, so it seems to be blank.
- Next, you can choose whether to set the view first, or click Configure model first. No matter which one should first think about the name and resource type of each region.
Prepare 'model'
Here we show you how to use the gtkliststore sub-class of the gtktreemodel, however, it only contains the relational relation, so the indexing method on the data is not the same, therefore, the iterator adopts different methods.
# Include <GTK/GTK. h>
# Include <glib. h>
Enum {
Col_name = 0,
Col_date,
Col_size,
N_cols
};
Void model_data_new (gtktreemodel * store,
Const gchar * Name, const gchar * Date, const guint size ){
Gtktreeiter ITER;
Gtk_list_store_append (gtk_list_store (store), & ITER );
Gtk_list_store_set (gtk_list_store (store), & ITER,
Col_name, name,
Col_date, date,
Col_size, size,
-1 );
}
Gtktreemodel * create_model (){
Gtkliststore * store;
Store = gtk_list_store_new (n_cols,
G_type_string, g_type_string, g_type_uint );
Return gtk_tree_model (store );
}
Int main (INT argc,
Char * argv []) {
/* Gtkwidget is the storage type for widgets */
Gtkwidget * window;
/* This is called in all GTK applications. arguments are parsed
* From the command line and are returned to the application .*/
Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "Tree ");
G_signal_connect (g_object (window), "Destroy", gtk_main_quit, null );
Gtkwidget * view;
View = gtk_tree_view_new ();
Gtk_container_add (gtk_container (window), view );
Gtk_widget_show_all (window );
/* All GTK applications must have a gtk_main (). Control ends here
* And waits for an event to occur (like a key press or
* Mouse event ).*/
Gtk_main ();
Return 0;
}
-
- In this case, we haven't really added a gtkliststore class. Instead, we first added the function of creating the gtkliststore and the function of adding the expected gtkliststore. Render manager GTK is an unlimited library in design, but it is too difficult to use it directly in this part. The suggestion is to package functions as needed. How can I directly use them, unless you are using a library or a general application program, this is enough.
-
- To create a gtkliststore, we need to deliver messages, "store data with several buckets", and "what is the type of each region ". These resources happen to be the number of records required by gtk_list_store_new. The first numeric value is the expected numeric value. In the program, you will find that I entered the value in an enum. Many tags in Enum are used in the controller function of gtkliststore. When you add a token (a column), you must specify the value of each token, the benchmark of the bitwise is similar to the primary column since 0. Using the auto-increment feature of Enum, set the first tag to 0 until the last tag (the latest tag name ), adding one more tag makes it easy to obtain the number of second bits. The remaining value of gtk_list_store_new () indicates a Variable Length Index column, because it is impossible to estimate the number of memory locations required by the user, you will often find this method in this series of related functions. Back to our questions, next we want to fill in the level of information type. Here, gtype is required, such as g_type_string (string data), g_type_int (digital data), g_type_pixbuf (image ), I want to know more about GTK + 2.0 Tree View tutorial. In this case, we have used the specified delimiter to fill in g_type_string, g_type_string, and g_type_uint. Finally, we converted the gtkliststore cast into a gtktreemodel when returning the create_model function.
- After the model is created, we need to perform operations on the data through the iterator, which shows how to add a simple method. For the model, we need to add new materials to do two things. First, we need to add a column of Blank materials to store the information; once there is space for storing data, we can specify the data in each region. The new blank space is created as gtk_list_store_append. It requires the model and iterator indicators. After adding a blank space, you can use gtk_list_store_set to specify the content of the column. In addition to the same number as that in gtk_list_stroe_append, you also need the location and content of the second bit, the delimiter location is the one previously set by enum. In the end, add-1 to indicate that there is no information to be added.
Add gtktreeviewcolumn and
Gtkcellrenderer
# Include <GTK/GTK. h>
# Include <glib. h>
Enum {
Col_name = 0,
Col_date,
Col_size,
N_cols
};
Void model_data_new (gtktreemodel * store,
Const gchar * Name, const gchar * Date, const guint size ){
Gtktreeiter ITER;
Gtk_list_store_append (gtk_list_store (store), & ITER );
Gtk_list_store_set (gtk_list_store (store), & ITER,
Col_name, name,
Col_date, date,
Col_size, size,
-1 );
}
Gtktreemodel * create_model (){
Gtkliststore * store;
Store = gtk_list_store_new (n_cols,
G_type_string, g_type_string, g_type_uint );
Return gtk_tree_model (store );
}
Void arrange_tree_view (gtkwidget * view ){
Gtkcellrenderer * Renderer;
// Col 1: Name
Renderer = gtk_cell_renderer_text_new ();
Gtk_tree_view_insert_column_with_attributes (
Gtk_tree_view (view),-1, "name", Renderer, "text", col_name, null );
// Col 2: Date
Gtk_tree_view_insert_column_with_attributes (
Gtk_tree_view (view),-1, "date", Renderer, "text", col_date, null );
// Col 3: Size
Gtk_tree_view_insert_column_with_attributes (
Gtk_tree_view (view),-1, "size", Renderer, "text", col_size, null );
}
Int main (INT argc,
Char * argv []) {
/* Gtkwidget is the storage type for widgets */
Gtkwidget * window;
/* This is called in all GTK applications. arguments are parsed
* From the command line and are returned to the application .*/
Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "test ");
G_signal_connect (g_object (window), "Destroy", gtk_main_quit, null );
Gtkwidget * view;
View = gtk_tree_view_new ();
Gtk_container_add (gtk_container (window), view );
// Arrange view Columns
Arrange_tree_view (View );
// Set model
Gtktreemodel * store = create_model ();
Gtk_tree_view_set_model (gtk_tree_view (view), store );
Model_data_new (store, "test. c", "", 2224 );
Model_data_new (store, "XD. c", "", 454 );
G_object_unref (store );
Gtk_widget_show_all (window );
/* All GTK applications must have a gtk_main (). Control ends here
* And waits for an event to occur (like a key press or
* Mouse event ).*/
Gtk_main ();
Return 0;
}
- We added an arrange_tree_view function. In fact, we only handed in the new column operation. Here, we use the response () function to add a new column. This function helps us call gtk_tree_view_column_new (), gtk_tree_view_column_set_title (), and gtk_tree_view_column_pack_start (), gtk_tree_view_column_set_attributes (), because gtktreeviewcolumn is a gobject, therefore, if you do not use gtk_tree_view_column_new_with_attributes, remember to use g_object_unref () after the gobject is delivered to gtktreeview ().
- In gtk_tree_view_column_new_with_attributes, the "adequacy" to be set is used by gtkcellrenderer. In our example, we hope that the last section in the table contains text information, therefore, set it to "text ". (? Does anyone know that I want to go there? orz) (For details about attribute, refer to gtk2.0 + Tree View tutorial 5.2 attributes)
- So far, we have finished setting up the base keys of the model and view, and then we can organize them in the main function! First, we use the arrange_tree_view function to put the successfully uploaded gtktreeviewcolumn, and then create a model using the create_model function. Then, use the gtk_tree_view_set_model to create a sequence between the gtktreeview and the gtktreemodel. Finally, do not forget to call g_object_unref for the model.
Endless
Although the file is only stored here, it is the starting point of your GTK Tree View course. Here is only the part that allows you to preview the workflow. First, let's understand the workflow used. First, you have mastered the process and then learned about the procedures. Instead of digging into the WeChat part from the beginning, we can't see the forest. From this fear of harm and stop :)