Convert game1.ui to template

This commit is contained in:
daleclack 2024-02-10 20:40:05 +08:00
parent c92d951f20
commit 1f0e4ebd7a
5 changed files with 179 additions and 103 deletions

View File

@ -26,7 +26,7 @@ link_directories (${GTK_LIBRARY_DIRS})
set(SOURCES src/core/main.cpp src/core/MainWin.cpp src/core/MyStack.cpp
src/core/MyItem.cpp src/ui/MyDock.cpp src/ui/MyFinder.cpp src/ui/MyPrefs.cpp
src/ui/AppView.cpp src/file_app/FileColumnView.cpp src/file_app/FileGridView.cpp
src/file_app/FileWindow.cpp)
src/file_app/FileWindow.cpp src/game_app/GameApp.cpp)
#Compile resources with GCR_CMake

View File

@ -1,157 +1,140 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow" id="window">
<property name="can-focus">False</property>
<property name="border-width">15</property>
<property name="title" translatable="yes">Guess Game</property>
<property name="window-position">center</property>
<property name="destroy-with-parent">True</property>
<child>
<!-- n-columns=5 n-rows=7 -->
<requires lib="gtk" version="4.0"/>
<!-- <object class="GtkWindow" id="window"> -->
<template class="GameApp" parent="GtkWindow">
<property name="title" translatable="1">Guess Game</property>
<property name="destroy-with-parent">1</property>
<property name="child">
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="visible">1</property>
<child>
<object class="GtkLabel" id="label">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Select a button</property>
<property name="label" translatable="1">Select a button</property>
<property name="visible">1</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
<property name="column-span">3</property>
</layout>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
<property name="width">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_1">
<property name="label" translatable="yes">1</property>
<property name="label" translatable="1">1</property>
<property name="width-request">100</property>
<property name="height-request">100</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="relief">none</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="has-frame">0</property>
<property name="visible">1</property>
<layout>
<property name="column">1</property>
<property name="row">2</property>
</layout>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_2">
<property name="label" translatable="yes">2</property>
<property name="label" translatable="1">2</property>
<property name="width-request">100</property>
<property name="height-request">100</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="relief">none</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="has-frame">0</property>
<property name="visible">1</property>
<layout>
<property name="column">3</property>
<property name="row">2</property>
</layout>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_3">
<property name="label" translatable="yes">3</property>
<property name="label" translatable="1">3</property>
<property name="width-request">100</property>
<property name="height-request">100</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="relief">none</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="has-frame">0</property>
<property name="visible">1</property>
<layout>
<property name="column">1</property>
<property name="row">4</property>
</layout>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_4">
<property name="label" translatable="yes">4</property>
<property name="label" translatable="1">4</property>
<property name="width-request">100</property>
<property name="height-request">100</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="relief">none</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="has-frame">0</property>
<property name="visible">1</property>
<layout>
<property name="column">3</property>
<property name="row">4</property>
</layout>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_go">
<property name="label" translatable="yes">Go</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="label" translatable="1">Go</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="visible">1</property>
<layout>
<property name="column">1</property>
<property name="row">6</property>
</layout>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">6</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_exit">
<property name="label" translatable="yes">Exit</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="label" translatable="1">Exit</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="visible">1</property>
<layout>
<property name="column">3</property>
<property name="row">6</property>
</layout>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">6</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="width-request">20</property>
<property name="height-request">20</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="visible">1</property>
<layout>
<property name="column">2</property>
<property name="row">3</property>
</layout>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="width-request">20</property>
<property name="height-request">20</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="visible">1</property>
<layout>
<property name="column">2</property>
<property name="row">5</property>
</layout>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="width-request">20</property>
<property name="height-request">20</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="visible">1</property>
<layout>
<property name="column">2</property>
<property name="row">1</property>
</layout>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<placeholder/>
@ -223,6 +206,7 @@
<placeholder/>
</child>
</object>
</child>
</object>
</property>
</template>
<!-- </object> -->
</interface>

View File

@ -0,0 +1,7 @@
#pragma once
#include <gtk/gtk.h>
G_DECLARE_FINAL_TYPE(GameApp, game_app, GAME, APP, GtkWindow)
GameApp *game_app_new();

View File

@ -0,0 +1,34 @@
#include "GameApp.h"
struct _GameApp
{
GtkWindow parent_instance;
GtkWidget *label;
GtkWidget *btn_1, *btn_2, *btn_3, *btn_4;
GtkWidget *btn_go, *btn_exit;
};
G_DEFINE_TYPE(GameApp, game_app, GTK_TYPE_WINDOW)
static void game_app_init(GameApp *self)
{
gtk_widget_init_template(GTK_WIDGET(self));
}
static void game_app_class_init(GameAppClass *klass)
{
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(klass),
"/org/gtk/daleclack/game1.ui");
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), GameApp, label);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), GameApp, btn_1);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), GameApp, btn_2);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), GameApp, btn_3);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), GameApp, btn_4);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), GameApp, btn_go);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), GameApp, btn_exit);
}
GameApp *game_app_new()
{
return GAME_APP(g_object_new(game_app_get_type(), NULL));
}

View File

@ -2,6 +2,7 @@
#include "MyFinder.h"
#include "AppView.h"
#include "FileWindow.h"
#include "GameApp.h"
enum PadPage
{
@ -34,6 +35,7 @@ struct _MyDock
GtkWidget *context_menu; // Context menu
MyPrefs *prefs_win; // Prefs window
FileWindow *file_win; // File Broswer window
GameApp *game_win; // The Guess Game
};
G_DEFINE_TYPE(MyDock, my_dock, GTK_TYPE_BOX)
@ -105,6 +107,7 @@ static void window_ctrl(GtkWindow *window, GtkWindow *parent)
}
}
// Prefs funcs
static void padset_clicked(GtkWidget *widget, MyDock *dock)
{
// When the window visible, unminimize it
@ -151,7 +154,7 @@ static gboolean prefs_win_closed(GtkWidget *window, MyDock *dock)
return TRUE;
}
static void btnfiles_clicked(GtkWindow *window, MyDock *dock)
static void padfiles_clicked(GtkWindow *window, MyDock *dock)
{
// When the window visible, unminimize it
if (gtk_widget_get_visible(GTK_WIDGET((dock->file_win))))
@ -165,9 +168,10 @@ static void btnfiles_clicked(GtkWindow *window, MyDock *dock)
gtk_window_present(GTK_WINDOW(dock->file_win));
}
gtk_image_set_from_icon_name(GTK_IMAGE(dock->image_file), "file-app_running");
btnlaunch_clicked(NULL, dock);
}
static void padfiles_clicked(GtkWindow *window, MyDock *dock)
static void btnfiles_clicked(GtkWindow *window, MyDock *dock)
{
// When the window visible, control window state
if (gtk_widget_get_visible(GTK_WIDGET((dock->file_win))))
@ -191,6 +195,47 @@ static gboolean file_window_closed(GtkWidget *window, MyDock *dock)
return TRUE;
}
static void padgame_clicked(GtkWindow *window, MyDock *dock)
{
// When the window visible, unminimize it
if (gtk_widget_get_visible(GTK_WIDGET((dock->game_win))))
{
gtk_window_unminimize(GTK_WINDOW(dock->game_win));
}
else
{
// Show the window
gtk_window_set_transient_for(GTK_WINDOW(dock->game_win), dock->parent_win);
gtk_window_present(GTK_WINDOW(dock->game_win));
}
gtk_image_set_from_icon_name(GTK_IMAGE(dock->image_game), "game_running");
btnlaunch_clicked(NULL, dock);
}
static void btngame_clicked(GtkWidget *widget, MyDock *dock)
{
// When the window visible, control window state
if (gtk_widget_get_visible(GTK_WIDGET((dock->game_win))))
{
window_ctrl(GTK_WINDOW(dock->game_win), dock->parent_win);
}
else
{
// Show the window
gtk_window_set_transient_for(GTK_WINDOW(dock->game_win), dock->parent_win);
gtk_window_present(GTK_WINDOW(dock->game_win));
}
gtk_image_set_from_icon_name(GTK_IMAGE(dock->image_game), "game_running");
}
static gboolean game_win_closed(GtkWidget *game_win, MyDock *dock)
{
// Hide the window
gtk_widget_set_visible(game_win, FALSE);
gtk_image_set_from_icon_name(GTK_IMAGE(dock->image_game), "game");
return TRUE;
}
static void my_dock_get_widgets(MyDock *self)
{
// Get widgets
@ -295,6 +340,12 @@ static void my_dock_init(MyDock *self)
g_signal_connect(self->padfile, "clicked", G_CALLBACK(padfiles_clicked), self);
g_signal_connect(self->file_win, "close-request", G_CALLBACK(file_window_closed), self);
// Create Game App Window
self->game_win = game_app_new();
g_signal_connect(self->btngame, "clicked", G_CALLBACK(btngame_clicked), self);
g_signal_connect(self->padgame, "clicked", G_CALLBACK(padgame_clicked), self);
g_signal_connect(self->game_win, "close-request", G_CALLBACK(game_win_closed), self);
// Add finder
self->finder = my_finder_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_box_append(GTK_BOX(self->finder_box), self->finder);