Приведу без комментариев код.
// gtktable.c
#include <stdio.h>
#include <gtk-3.0/gtk/gtk.h>
GtkWidget* main_window;
GtkWidget* box1;
GtkWidget* scroll_window;
GtkWidget* list_widget;
GtkTreeStore* store_list;
enum{COL1, COL2, COL3, NCOL};
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
GtkTreeSelection* selection;
GtkTreeIter iter1;
gboolean
selection_func (GtkTreeSelection *selection,
GtkTreeModel *model,
GtkTreePath *path,
gboolean path_currently_selected,
gpointer userdata)
{
g_print ("selection_func is called.\n");
return TRUE;
}
void
create_gtk_table(){
store_list = gtk_tree_store_new(NCOL, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);
list_widget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store_list));
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_widget));
gtk_tree_selection_set_select_function(selection, selection_func, NULL, NULL);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list_widget), TRUE);
gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(list_widget),GTK_TREE_VIEW_GRID_LINES_BOTH);
gtk_container_add(GTK_CONTAINER(scroll_window), list_widget);
renderer = gtk_cell_renderer_text_new();
g_object_set(G_OBJECT(renderer), "background", "grey", NULL);
g_object_set(G_OBJECT(renderer), "foreground", "black", NULL);
column = gtk_tree_view_column_new_with_attributes("Title1",renderer, "text", COL1, NULL);
gtk_tree_view_column_set_clickable(column, TRUE);
gtk_tree_view_append_column(GTK_TREE_VIEW(list_widget), column);
column = gtk_tree_view_column_new_with_attributes("Title2",renderer, "text", COL2, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list_widget), column);
column = gtk_tree_view_column_new_with_attributes("Title3",renderer, "text", COL3, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list_widget), column);
g_object_unref(G_OBJECT(store_list));
for(gint i = 0 ; i < 100; i++){
gtk_tree_store_append(store_list, &iter1, NULL);
gtk_tree_store_set(store_list, &iter1, COL1, i+1, COL2, "string ?", COL3, "the string!", -1);
}
}
void
activate (GtkApplication *app,
gpointer user_data)
{
main_window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (main_window), "The Table in The Scrolling");
gtk_window_set_default_size (GTK_WINDOW (main_window), 400, 500);
gtk_container_set_border_width (GTK_CONTAINER (main_window), 20);
box1 = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
gtk_box_set_homogeneous (GTK_BOX(box1), FALSE);
gtk_container_add (GTK_CONTAINER (main_window), box1);
scroll_window = gtk_scrolled_window_new(NULL, NULL);
gtk_widget_set_size_request(scroll_window, 350, 450);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_box_pack_start(GTK_BOX (box1), scroll_window, FALSE, FALSE, 10);
create_gtk_table();
gtk_widget_show_all (main_window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.the_sin", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
Код, который ответственнен за создание списка находится в функции create_gtk_table. После выполнения эта программа выводит на дисплей вот такое:
Если для компиляции ипользовать CMake, то необходимо создать файл CMakeLists.txt следующего содержания:
cmake_minimum_required (VERSION 3.18)
project (gtktable)
find_package (PkgConfig REQUIRED)
pkg_check_modules (GTK3 REQUIRED gtk+-3.0)
include_directories (${GTK3_INCLUDE_DIRS})
link_directories (${GTK3_LIBRARY_DIRS})
add_executable(gtktable gtktable.c)
add_definitions (${GTK3_CFLAGS_OTHER})
target_link_libraries (gtktable ${GTK3_LIBRARIES})
Компилировалось и выполнялось на Debian Bullseye. Собственно, это все, что хотел сказать.
Комментариев нет:
Отправить комментарий