A Gtkapplicaton class has been added to the GTK3 to allow us to handle multi-window programs, and with gtkapplication we are also more likely to create applications that are flexible, easy to use, and aesthetically pleasing.
In the previous examples, we demonstrated how to create a single window directly with Gtkapplication and know where to design our UI. And simply tried the gtkbuilder.
Next, we went into gtkapplication. Design our applications with an object-oriented design approach.
We want to encapsulate a MyApp and Myappwindow, which inherits the Gtkapplcation and Gtkapplicationwindow.
You first need to create the following files:
MAIN.C: The main function in this file.
MYAPP.C Mypp.h: The encapsulated MYAPP
MYAPPWINDOW.C myappwindow.h: Encapsulated Myappwindow
In this way, the main function only needs a sentence, main.c content as follows:
#include <gtk/gtk.h>"myapp.h"int main (intChar *argv[]) { //my_app_new () is our encapsulated, create a MYAPP interface return G_application_run (G_application (My_app_new ()), argc, argv);}
Next up is myapp.c.
#include <gtk/gtk.h>#include"MYAPP.h"#include"myappwindow.h" //using the interface provided to our encapsulated Myappwindow//using the C language structure, create the MyApp type, whose parent type is gtkapplicationstruct_myapp{gtkapplication Parent;};//same definition Myappclass classstruct_myappclass{gtkapplicationclass Parent_class;};//using the G_define_type macro, register to define the MYAPP type, the corresponding parameters are: type name, lowercase type name (separated by underscore), parent typeG_define_type (MyApp, My_app, gtk_type_application);//My_app initialization function prototypeStatic voidMy_app_init (MYAPP *app) {}//My_app Activate signal prototype, equivalent to the previous sections we manually linked activate functionStatic voidMy_app_activate (Gapplication *app) {Myappwindow*win; //so create a window here, with our encapsulated MyappwindowWin =My_app_window_new (My_app (APP)); //Place the window in the foreground, automatically call Gtk_widget_show_all ()Gtk_window_present (Gtk_window (Win));}//function Prototypes for handling command-line argumentsStatic voidMy_app_open (Gapplication *app, Gfile**files, Gint n_files,ConstGchar *hint) {GList*windows; Myappwindow*win; inti; Windows=Gtk_application_get_windows (gtk_application (APP)); if(Windows) win= My_app_window (windows->data); Elsewin=My_app_window_new (My_app (APP)); for(i =0; i < n_files; i++) My_app_window_open (Win, Files[i]); Gtk_window_present (Gtk_window (Win));}//The initialization function of the MyApp class, in which the replication MyApp class inherits the default signal handler of the Gtkappliation class. Static voidMy_app_class_init (Myappclass *class){ //change the Activate signal and the open signal processing function to what we wrote.G_application_class (class)->activate =my_app_activate; G_application_class (class)->open =My_app_open;}//This is an outward-provided interface without static modification. MYAPP *my_app_new (void){ //Create a MYAPP according to the type we created returng_object_new (My_app_type,"Application-id" , "Org.gtk.myapp" , "Flags", G_application_handles_open, NULL);}
MYAPP.h content is as follows:
#ifndef _my_app_h#define_my_app_h#include<gtk/gtk.h>//My_app_get_type () These functions, although we did not write, but in the register to define the MYAPP type, according to the completed My_app will be automatically generated. #defineMy_app_type (My_app_get_type ())#defineMy_app (obj) (G_type_check_instance_cast ((obj), My_app_type, MyApp))typedefstruct_myapp myapp;typedefstruct_myappclass Myappclass;//other functions in myapp.c are private and do not need to be provided out of the box for the time being. GType My_app_get_type (void); MYAPP*my_app_new (void);#endif //_my_app_
The following package Myappwindow type is the same way as above, as follows MYAPPWINDOW.C
#include <gtk/gtk.h>#include"MYAPP.h"#include"myappwindow.h"struct_myappwindow{Gtkapplicationwindow Parent;};struct_myappwindowclass{gtkapplicationwindowclass Parent_class;}; G_define_type (Myappwindow, My_app_window, Gtk_type_application_window);Static voidMy_app_window_init (Myappwindow *app) {}Static voidMy_app_window_class_init (Myappwindowclass *class) {}myappwindow*my_app_window_new (MYAPP *app) { returng_object_new (My_app_window_type,"Application", app, NULL); }voidMy_app_window_open (Myappwindow *win, Gfile *file) {}
and Myappwindow.h
#ifndef _my_app_window_h#define_my_app_window_h#include<gtk/gtk.h>#include"MYAPP.h"#defineMy_app_window_type (My_app_window_get_type ())#defineMy_app_window (obj) (G_type_check_instance_cast ((obj), My_app_window_type, Myappwindow))typedefstruct_myappwindow myappwindow;typedefstruct_myappwindowclass Myappwindowclass; GType My_app_window_get_type (void); Myappwindow*my_app_window_new (MYAPP *app);voidMy_app_window_open (Myappwindow *win, Gfile *file);#endif //_my_app_window_
Compile and run:
gcc main.c myapp.c myappwindow.c 'pkg-config --cflags--libs gtk+-3.0 '-Wall. /a.out
The results of the operation are as follows:
Linux from the beginning again gtk+-3.0 (vi)