Add gtk146

This commit is contained in:
daleclack 2023-03-16 11:34:52 +08:00
parent 1f28a8a8e9
commit b6ebc908e5
14 changed files with 734 additions and 0 deletions

View File

@ -0,0 +1,3 @@
{
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
}

View File

@ -0,0 +1,76 @@
set(CMAKE_CXX_STANDARD 17)
cmake_minimum_required(VERSION 3.0.0)
project(gtk145_gtkui6 VERSION 1.0.0)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../GCR_CMake/macros)
include(GlibCompileResourcesSupport)
include(CTest)
enable_testing()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
include_directories(.)
include_directories(..)
#Find PkgConfig to use gtkmm3
find_package (PkgConfig REQUIRED)
pkg_check_modules (GTKMM4 REQUIRED gtkmm-4.0)
include_directories (${GTKMM4_INCLUDE_DIRS})
link_directories (${GTKMM4_LIBRARY_DIRS})
#Find Gettext
# find_package (Gettext REQUIRED)
# set(PO_DIR ${CMAKE_BINARY_DIR}/po/zh_CN/LC_MESSAGES)
#Source files
set(SOURCE_FILE src/main.cc src/MainWin.cc src/MenuBar.cc src/MyPrefs.cc)
#Compile Resource
set(RESOURCE_LIST
custom_style.css
STRIPBLANKS context_menu.xml
STRIPBLANKS default_menu.xml
STRIPBLANKS prefs_stack.ui
)
compile_gresources(RESOURCE_FILE
XML_OUT
TYPE EMBED_C
RESOURCES ${RESOURCE_LIST}
PREFIX "/org/gtk/daleclack"
SOURCE_DIR ${PROJECT_SOURCE_DIR}/res)
# Add a custom target to the makefile. Now make builds our resource file.
# It depends on the output RESOURCE_FILE.
add_custom_target(resource ALL DEPENDS ${RESOURCE_FILE})
#For win32 platform,use rc resource and .ico icon
if(WIN32)
SET(CMAKE_RC_COMPILER windres)
set(app_WINRC ../icon.rc)
set_property(SOURCE ../icon.rc APPEND PROPERTY
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/../icon.ico
)
add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} ${SOURCE_FILE} ${RESOURCE_FILE})
add_custom_command( TARGET ${PROJECT_NAME}
COMMAND echo * > ${CMAKE_BINARY_DIR}/.gitignore
COMMAND echo **/* > ${CMAKE_BINARY_DIR}/.hgignore)
else()
add_executable(${PROJECT_NAME} ${SOURCE_FILE} ${RESOURCE_FILE})
add_custom_command( TARGET ${PROJECT_NAME}
COMMAND echo \"*\" > ${CMAKE_BINARY_DIR}/.gitignore
COMMAND echo \"**/*\" > ${CMAKE_BINARY_DIR}/.hgignore)
endif(WIN32)
#Add command to generate .gitignore and .mo files
# add_custom_command( TARGET ${PROJECT_NAME}
# COMMAND mkdir -p ${PO_DIR}
# COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} ${CMAKE_SOURCE_DIR}/po/zh_CN.po -o ${PO_DIR}/${PROJECT_NAME}.mo)
SET (CMAKE_EXTRA_CXX_FLAGS ${GTKMM4_CFLAGS_OTHER})
target_link_libraries (${PROJECT_NAME} ${GTKMM4_LIBRARIES} -lpthread)

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="context_menu">
<section>
<item>
<attribute name="label">About</attribute>
<attribute name="action">win.main_about</attribute>
</item>
<item>
<attribute name="label">Customize</attribute>
<attribute name="action">win.custom</attribute>
</item>
</section>
</menu>
</interface>

View File

@ -0,0 +1,18 @@
/*
You can type here any CSS rule recognized by GTK.
You can temporarily disable this custom CSS by clicking on the Pause button above.
Changes are applied instantly and globally, for the whole application.
*/
.accdoration {
background:rgba(255, 255, 255, 0.5);
}
.accdoration menubar{
background: rgba(255, 255, 255, 0.5);
}
.accdoration box{
background: rgba(255, 255, 255, 0.5);
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id = "default_menu">
<submenu>
<attribute name="label">Test</attribute>
<item>
<attribute name="label">About</attribute>
<attribute name="action">win.main_about</attribute>
</item>
</submenu>
<submenu>
<attribute name="label">Test1</attribute>
<item>
<attribute name="label">Cut</attribute>
</item>
</submenu>
</menu>
</interface>

View File

@ -0,0 +1,354 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkAdjustment" id="adjustment1">
<property name="upper">9999</property>
<property name="value">1024</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
<object class="GtkAdjustment" id="adjustment2">
<property name="upper">9999</property>
<property name="value">576</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
<object class="GtkStackSwitcher" id="stack_switch">
<property name="halign">center</property>
<property name="stack">main_stack</property>
</object>
<object class="GtkBox" id="stack_box">
<property name="hexpand">1</property>
<property name="vexpand">1</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkStack" id="main_stack">
<child>
<object class="GtkStackPage">
<property name="title" translatable="1">Background Preferences</property>
<property name="child">
<object class="GtkBox" id="back_page">
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</property>
</object>
</child>
<child>
<object class="GtkStackPage">
<property name="title" translatable="1">Window Size Config</property>
<property name="child">
<object class="GtkBox" id="winsize_page">
<property name="halign">center</property>
<property name="valign">center</property>
<property name="hexpand">1</property>
<property name="vexpand">1</property>
<property name="orientation">vertical</property>
<property name="spacing">5</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="halign">center</property>
<property name="valign">center</property>
<property name="spacing">5</property>
<child>
<object class="GtkImage">
<property name="pixel-size">128</property>
<property name="icon-name">display_prefs</property>
</object>
</child>
<child>
<object class="GtkGrid">
<property name="row-spacing">5</property>
<property name="column-spacing">5</property>
<child>
<object class="GtkComboBoxText" id="combo_default">
<property name="active">2</property>
<items>
<item translatable="yes">640x360</item>
<item translatable="yes">800x450</item>
<item translatable="yes">1024x576</item>
<item translatable="yes">1280x720</item>
<item translatable="yes">1366x768</item>
</items>
<layout>
<property name="column">1</property>
<property name="row">1</property>
<property name="column-span">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkCheckButton" id="radio_default">
<property name="label" translatable="1">Use default window size</property>
<property name="active">1</property>
<property name="group">radio_custom</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
<property name="column-span">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkCheckButton" id="radio_custom">
<property name="label" translatable="1">User defined window size</property>
<layout>
<property name="column">1</property>
<property name="row">2</property>
<property name="column-span">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkSpinButton" id="spin_width">
<property name="sensitive">0</property>
<property name="focusable">1</property>
<property name="adjustment">adjustment1</property>
<property name="value">1024</property>
<layout>
<property name="column">1</property>
<property name="row">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkSpinButton" id="spin_height">
<property name="sensitive">0</property>
<property name="focusable">1</property>
<property name="adjustment">adjustment2</property>
<property name="value">576</property>
<layout>
<property name="column">3</property>
<property name="row">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label" translatable="1">x</property>
<layout>
<property name="column">2</property>
<property name="row">3</property>
</layout>
</object>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="hexpand">1</property>
<property name="vexpand">1</property>
<child>
<object class="GtkButton" id="btnapply">
<property name="label" translatable="1">Apply</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="halign">end</property>
</object>
</child>
<child>
<object class="GtkButton" id="btnGet">
<property name="label" translatable="1">Get Current</property>
<property name="sensitive">0</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
</object>
</child>
<child>
<object class="GtkLabel" id="label_size">
<property name="label" translatable="1">Current Config: </property>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</child>
<child>
<object class="GtkStackPage">
<property name="title" translatable="1">Dock Preferences</property>
<property name="child">
<object class="GtkBox" id="dock_page">
<property name="halign">center</property>
<property name="valign">center</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="halign">center</property>
<property name="valign">center</property>
<property name="spacing">5</property>
<child>
<object class="GtkImage">
<property name="pixel-size">128</property>
<property name="icon-name">display_prefs</property>
</object>
</child>
<child>
<object class="GtkGrid">
<property name="row-spacing">5</property>
<property name="column-spacing">5</property>
<child>
<object class="GtkCheckButton" id="mode_check">
<property name="label" translatable="1">Use panel mode:expand the dock</property>
<property name="focusable">1</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
<property name="column-span">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Position:</property>
<layout>
<property name="column">1</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="GtkCheckButton" id="radio_left">
<property name="label" translatable="1">Left</property>
<property name="active">1</property>
<layout>
<property name="column">2</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="GtkCheckButton" id="radio_right">
<property name="label" translatable="1">Right</property>
<property name="group">radio_left</property>
<layout>
<property name="column">2</property>
<property name="row">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkCheckButton" id="radio_bottom">
<property name="label" translatable="1">Bottom</property>
<property name="group">radio_left</property>
<layout>
<property name="column">2</property>
<property name="row">4</property>
</layout>
</object>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="hexpand">1</property>
<property name="vexpand">1</property>
<child>
<object class="GtkButton" id="btnapply1">
<property name="label" translatable="1">Apply</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="halign">end</property>
</object>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</property>
</object>
</child>
</object>
</child>
<child>
<placeholder/>
</child>
</object>
</interface>

View File

@ -0,0 +1,95 @@
#include "MainWin.hh"
#include "jsonfile.hh"
#include "../winpe.xpm"
WinShown curr_win;
MainWin::MainWin()
: main_box(Gtk::Orientation::VERTICAL),
btn_box(Gtk::Orientation::HORIZONTAL),
btn_main("Main"),
btn_win1("Win1"),
btn_win2("Win2"),
label1(" ")
{
// Initalize window
set_icon_name("org.gtk.daleclack");
set_default_size(1024, 576);
// Add style provider
provider = Gtk::CssProvider::create();
provider->load_from_resource("/org/gtk/daleclack/custom_style.css");
// Add popover menu bar
//menu_bar = new Gtk::PopoverMenuBar(model_default);
auto menubar_display = menu_bar.menubar->get_display();
Gtk::StyleProvider::add_provider_for_display(menubar_display, provider,
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
menu_bar.menubar->add_css_class("accdoration");
main_box.append(*(menu_bar.menubar));
// (menu_bar.menubar)->show();
// Add context menu
// Gesture for right click
gesture_right = Gtk::GestureClick::create();
gesture_right->set_button(GDK_BUTTON_SECONDARY);
gesture_right->signal_pressed().connect(sigc::mem_fun(*this, &MainWin::press));
overlay1.add_controller(gesture_right);
// Add background image
auto pixbuf = Gdk::Pixbuf::create_from_xpm_data(winpe);
auto sized = pixbuf->scale_simple(1024, 576, Gdk::InterpType::BILINEAR);
background.set_pixbuf(sized);
overlay1.set_child(background);
// The Context menu
context_builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/context_menu.xml");
auto gmenu = context_builder->get_object<Gio::MenuModel>("context_menu");
context_menu.set_menu_model(gmenu);
context_menu.set_parent(overlay1);
context_menu.set_has_arrow(false);
// Signal for context menu
add_action("main_about", sigc::mem_fun(*this, &MainWin::main_about));
add_action("custom", sigc::mem_fun(*this, &MainWin::prefs_activated));
// Add a label
label1.set_expand();
label1.set_halign(Gtk::Align::FILL);
label1.set_valign(Gtk::Align::FILL);
main_box.append(label1);
// Add buttons]
btn_box.add_css_class("accdoration");
btn_main.set_has_frame(false);
btn_win1.set_has_frame(false);
btn_win2.set_has_frame(false);
btn_box.append(btn_main);
btn_box.append(btn_win1);
btn_box.append(btn_win2);
btn_box.set_halign(Gtk::Align::CENTER);
btn_box.set_valign(Gtk::Align::END);
main_box.append(btn_box);
// Add widgets to the window
main_box.set_halign(Gtk::Align::FILL);
main_box.set_valign(Gtk::Align::FILL);
overlay1.add_overlay(main_box);
set_child(overlay1);
}
void MainWin::press(int n_press, double x, double y){
// g_print("%f %f\n", x, y);
// Show the context menu and the pointer position
Gdk::Rectangle point(x, y, 1, 1);
context_menu.set_pointing_to(point);
context_menu.popup();
}
void MainWin::main_about(){
}
void MainWin::prefs_activated(){
prefs_win.show();
}

View File

@ -0,0 +1,36 @@
#pragma once
#include <gtkmm.h>
#include "MenuBar.hh"
#include "MyPrefs.hh"
class MainWin : public Gtk::ApplicationWindow
{
public:
MainWin();
private:
// Child Widgets
MenuBar menu_bar;
Gtk::Box main_box, btn_box;
Gtk::Button btn_main, btn_win1, btn_win2;
Gtk::Picture background;
Gtk::Label label1;
Gtk::Overlay overlay1;
// Style provider
Glib::RefPtr<Gtk::CssProvider> provider;
// Menu for right click
Glib::RefPtr<Gtk::GestureClick> gesture_right;
Glib::RefPtr<Gtk::Builder> context_builder;
Gtk::PopoverMenu context_menu;
void press(int n_press, double x, double y);
// Preferences window
MyPrefs prefs_win;
// Signal Handlers for context menu
void main_about();
void prefs_activated();
};

View File

@ -0,0 +1,29 @@
#include "MenuBar.hh"
MenuBar::MenuBar()
{
// Create models
builder_def = Gtk::Builder::create_from_resource("/org/gtk/daleclack/default_menu.xml");
model_default = builder_def->get_object<Gio::MenuModel>("default_menu");
menubar = Gtk::make_managed<Gtk::PopoverMenuBar>(model_default);
}
void MenuBar::change_menu(WinShown win_id)
{
// Switch the menu for a window
switch (win_id)
{
case WinShown::DEFAULT:
menubar->set_menu_model(model_default);
break;
case WinShown::WIN_1:
// menubar->set_menu_model(model_win1);
break;
case WinShown::WIN_2:
// menubar->set_menu_model(model_win2);
break;
}
}
MenuBar::~MenuBar(){
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <gtkmm.h>
enum class WinShown
{
DEFAULT,
WIN_1,
WIN_2
};
class MenuBar
{
public:
MenuBar();
~MenuBar();
void change_menu(WinShown win_id);
Gtk::PopoverMenuBar *menubar;
private:
// Menu Models
Glib::RefPtr<Gtk::Builder> builder_def, builder_win1, builder_win2;
Glib::RefPtr<Gio::MenuModel> model_default, model_win1, model_win2;
};

View File

@ -0,0 +1,25 @@
#include "MyPrefs.hh"
MyPrefs::MyPrefs(){
// Set default size
set_default_size(800, 450);
// Create builder
ref_builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/prefs_stack.ui");
// Initalize title widget
// Add stack switcher to the titlebar
stack_switcher = ref_builder->get_widget<Gtk::StackSwitcher>("stack_switch");
header.set_title_widget(*stack_switcher);
header.set_decoration_layout("close:menu");
header.set_show_title_buttons();
set_titlebar(header);
// Get Widgets
main_stack = ref_builder->get_widget<Gtk::Stack>("main_stack");
stack_box = ref_builder->get_widget<Gtk::Box>("stack_box");
stack_box->set_halign(Gtk::Align::FILL);
stack_box->set_valign(Gtk::Align::FILL);
set_child(*stack_box);
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <gtkmm.h>
class MyPrefs : public Gtk::Window
{
public:
MyPrefs();
private:
// Title Widget
Gtk::HeaderBar header;
Gtk::StackSwitcher *stack_switcher;
// Child Widget
Gtk::Stack *main_stack;
Gtk::Box *stack_box;
Glib::RefPtr<Gtk::Builder> ref_builder;
};

View File

@ -0,0 +1,5 @@
#pragma once
#include "../../json_nlohmann/json.hpp"
using json = nlohmann::json;

View File

@ -0,0 +1,18 @@
#include "MainWin.hh"
/*
The test application for My GtkUi 6.0,
In this version of My Gtk UI, more features will be added.
Because the Gtkmm4 has major changes with the Gtkmm3,
so some test is needed.
This is the first test, hello Gtkmm4!
Written by dale clack, 2023/1/15
*/
int main(int argc, char **argv){
// Create a application
auto app = Gtk::Application::create("org.gtk.daleclack");
// Create a window and run the application
app->make_window_and_run<MainWin>(argc, argv);
}