From 2c6a9f2103c704dd334b467f70cb9bdba09ac615 Mon Sep 17 00:00:00 2001 From: daleclack Date: Thu, 5 Aug 2021 11:26:57 +0800 Subject: [PATCH] Add gtk100 --- Gtk3/gtk100_winctrl/CMakeLists.txt | 20 +++++++++++++ Gtk3/gtk100_winctrl/src/WinBtn.cpp | 44 ++++++++++++++++++++++++++++ Gtk3/gtk100_winctrl/src/WinBtn.h | 16 +++++++++++ Gtk3/gtk100_winctrl/src/main.cpp | 46 ++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 Gtk3/gtk100_winctrl/CMakeLists.txt create mode 100644 Gtk3/gtk100_winctrl/src/WinBtn.cpp create mode 100644 Gtk3/gtk100_winctrl/src/WinBtn.h create mode 100644 Gtk3/gtk100_winctrl/src/main.cpp diff --git a/Gtk3/gtk100_winctrl/CMakeLists.txt b/Gtk3/gtk100_winctrl/CMakeLists.txt new file mode 100644 index 0000000..ca8f5a0 --- /dev/null +++ b/Gtk3/gtk100_winctrl/CMakeLists.txt @@ -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}) diff --git a/Gtk3/gtk100_winctrl/src/WinBtn.cpp b/Gtk3/gtk100_winctrl/src/WinBtn.cpp new file mode 100644 index 0000000..5ce8cdc --- /dev/null +++ b/Gtk3/gtk100_winctrl/src/WinBtn.cpp @@ -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; +} diff --git a/Gtk3/gtk100_winctrl/src/WinBtn.h b/Gtk3/gtk100_winctrl/src/WinBtn.h new file mode 100644 index 0000000..8c2292a --- /dev/null +++ b/Gtk3/gtk100_winctrl/src/WinBtn.h @@ -0,0 +1,16 @@ +#ifndef __WINBTN_H_ +#define __WINBTN_H_ + +#include + +//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 diff --git a/Gtk3/gtk100_winctrl/src/main.cpp b/Gtk3/gtk100_winctrl/src/main.cpp new file mode 100644 index 0000000..a92fde7 --- /dev/null +++ b/Gtk3/gtk100_winctrl/src/main.cpp @@ -0,0 +1,46 @@ +#include +#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); +}