Add gtk100

This commit is contained in:
daleclack 2021-08-05 11:26:57 +08:00
parent b12c1f037a
commit 2c6a9f2103
4 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,20 @@
set(CMAKE_CXX_STANDARD 17)
cmake_minimum_required(VERSION 3.0.0)
project(gtk100 VERSION 0.1.0)
include(CTest)
enable_testing()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
include_directories(..)
find_package (PkgConfig REQUIRED)
pkg_check_modules (GTK3 REQUIRED gtk+-3.0)
include_directories (${GTK3_INCLUDE_DIRS})
link_directories (${GTK3_LIBRARY_DIRS})
add_executable(gtk100 src/main.cpp src/WinBtn.cpp)
add_definitions (${GTK3_CFLAGS_OTHER})
target_link_libraries (${PROJECT_NAME} ${GTK3_LIBRARIES})

View File

@ -0,0 +1,44 @@
#include "WinBtn.h"
struct _WinBtn{
GtkButton parent;
GtkApplication *app;
};
G_DEFINE_TYPE(WinBtn,win_btn,GTK_TYPE_BUTTON)
static void win_btn_clicked(GtkButton *button){
GtkWindow *win=gtk_application_get_active_window(WIN_BTN(button)->app);
GdkWindow *gdk_win=gtk_widget_get_window(GTK_WIDGET(win));
if(gdk_win){
GdkWindowState state=gdk_window_get_state(gdk_win);
switch(state){
case GDK_WINDOW_STATE_ICONIFIED:
gtk_window_deiconify(win);
break;
case GDK_WINDOW_STATE_WITHDRAWN:
gtk_widget_show_all(GTK_WIDGET(win));
break;
default:
gtk_window_iconify(win);
}
}else{
gtk_widget_show_all(GTK_WIDGET(win));
}
}
static void win_btn_init(WinBtn *btn1){
}
static void win_btn_class_init(WinBtnClass *class1){
GTK_BUTTON_CLASS(class1)->clicked=win_btn_clicked;
}
WinBtn * win_btn_new(){
return WIN_BTN(g_object_new(WIN_BTN_TYPE,NULL));
}
void win_btn_set_application(WinBtn *btn1,GtkApplication *app1){
btn1->app=app1;
}

View File

@ -0,0 +1,16 @@
#ifndef __WINBTN_H_
#define __WINBTN_H_
#include <gtk/gtk.h>
//Define a type for winbtn
#define WIN_BTN_TYPE (win_btn_get_type())
//WinBtn Type:WinBtn and a window to control
G_DECLARE_FINAL_TYPE(WinBtn,win_btn,WIN,BTN,GtkButton)
WinBtn * win_btn_new(void);
void win_btn_set_application(WinBtn *btn1,GtkApplication *app1);
#endif

View File

@ -0,0 +1,46 @@
#include <gtk/gtk.h>
#include "WinBtn.h"
#include "winpe.xpm"
static void gtkmain(GtkApplication *app,gpointer user_data){
GtkWidget *window, *back, *overlay;
WinBtn * win_btn;
//Create window
window=gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window),640,360);
gtk_window_set_title(GTK_WINDOW(window),"Minimize Test");
gtk_window_set_icon_name(GTK_WINDOW(window),"org.gtk.daleclack");
//Create overlay
overlay=gtk_overlay_new();
//Get Background
GdkPixbuf *pixbuf=gdk_pixbuf_new_from_xpm_data(winpe);
GdkPixbuf *sized=gdk_pixbuf_scale_simple(pixbuf,640,360,GDK_INTERP_BILINEAR);
back=gtk_image_new_from_pixbuf(sized);
gtk_container_add(GTK_CONTAINER(overlay),back);
g_object_unref(pixbuf);
g_object_unref(sized);
//Initalize Panel
win_btn=win_btn_new();
win_btn_set_application(win_btn,app);
gtk_button_set_relief(GTK_BUTTON(win_btn),GTK_RELIEF_NONE);
gtk_button_set_label(GTK_BUTTON(win_btn),"Minimize");
gtk_widget_set_halign(GTK_WIDGET(win_btn),GTK_ALIGN_CENTER);
gtk_widget_set_valign(GTK_WIDGET(win_btn),GTK_ALIGN_CENTER);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay),GTK_WIDGET(win_btn));
//Pack Boxs
gtk_container_add(GTK_CONTAINER(window),overlay);
gtk_widget_show_all(window);
}
int main(int argc,char **argv){
GtkApplication *app;
app=gtk_application_new("org.gtk.daleclack",G_APPLICATION_NON_UNIQUE);
g_signal_connect(app,"activate",G_CALLBACK(gtkmain),NULL);
return g_application_run(G_APPLICATION(app),argc,argv);
}