Add TopPanel and custom background

This commit is contained in:
daleclack 2021-10-23 22:12:20 +08:00
parent 7f1d24365a
commit 0c6cc4c1b7
8 changed files with 364 additions and 126 deletions

View File

@ -54,7 +54,8 @@ set(RESOURCE_LIST
icons/scalable/status/system.svg
gnome-fs-directory.png
gnome-fs-regular.png
STRIPBLANKS appmenu.xml)
STRIPBLANKS appmenu.xml
STRIPBLANKS stack.ui)
compile_gresources(RESOURCE_FILE
XML_OUT
@ -67,7 +68,7 @@ compile_gresources(RESOURCE_FILE
# It depends on the output RESOURCE_FILE.
add_custom_target(resource ALL DEPENDS ${RESOURCE_FILE})
set(SOURCES src/main.cpp src/MainWin.cpp)
set(SOURCES src/main.cpp src/MainWin.cpp src/MainStack.cpp)
# Step 4:Add the resource to compile list and compile
if(WIN32)

216
Gtk4/res/stack.ui Normal file
View File

@ -0,0 +1,216 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkImage" id="imghome">
<property name="visible">1</property>
<property name="icon-name">user_home</property>
<property name="pixel-size">48</property>
</object>
<object class="GtkAdjustment" id="adjustment1">
<property name="upper">100</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
<object class="GtkStack" id="stack">
<property name="visible">1</property>
<child>
<object class="GtkStackPage">
<property name="name">login_page</property>
<property name="title" translatable="yes">page1</property>
<property name="child">
<object class="GtkBox" id="login_box">
<property name="visible">1</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkImage" id="imglogin">
<property name="visible">1</property>
<property name="icon-name">My_GtkUI</property>
<property name="pixel-size">48</property>
</object>
</child>
<child>
<object class="GtkLabel" id="label_user">
<property name="visible">1</property>
<property name="label" translatable="yes">dale</property>
</object>
</child>
<child>
<object class="GtkButton" id="btnlogin">
<property name="visible">1</property>
<property name="label" translatable="yes">login</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="has-frame">0</property>
</object>
</child>
<child>
<placeholder/>
</child>
</object>
</property>
</object>
</child>
<child>
<object class="GtkStackPage">
<property name="name">main_page</property>
<property name="title" translatable="yes">page0</property>
<property name="child">
<object class="GtkBox" id="main_box">
<property name="visible">1</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox" id="top_panel">
<property name="visible">1</property>
<child>
<object class="GtkLabel" id="label_app">
<property name="visible">1</property>
<property name="label" translatable="yes">My GtkUI Desktop</property>
</object>
</child>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox" id="notice_box">
<property name="visible">1</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkLabel" id="seprator_label">
<property name="visible">1</property>
<property name="hexpand">1</property>
<property name="label" translatable="yes"> </property>
</object>
</child>
<child>
<object class="GtkLabel" id="label_time">
<property name="visible">1</property>
<property name="hexpand">0</property>
<property name="label" translatable="yes">20:00 2021/2/14</property>
</object>
</child>
<child>
<object class="GtkButton" id="help_button">
<property name="visible">1</property>
<property name="hexpand">0</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="icon-name">system-help</property>
<property name="has-frame">0</property>
</object>
</child>
<child>
<object class="GtkButton" id="ac_button">
<property name="visible">1</property>
<property name="hexpand">0</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="icon-name">ac-adapter-symbolic</property>
<property name="has-frame">0</property>
</object>
</child>
<child>
<object class="GtkButton" id="volume_button">
<property name="visible">1</property>
<property name="focusable">1</property>
<property name="receives-default">1</property>
<property name="icon-name">audio-volume-high-symbolic</property>
<property name="has-frame">0</property>
</object>
</child>
<child>
<object class="GtkMenuButton" id="menu_button">
<property name="visible">1</property>
<property name="focusable">1</property>
<property name="focus-on-click">0</property>
<property name="receives-default">1</property>
<property name="has-frame">0</property>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox" id="desktop_box">
<property name="visible">1</property>
<child>
<object class="GtkBox" id="leftbox">
<property name="visible">1</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
<child>
<object class="GtkGrid">
<property name="visible">1</property>
<child>
<object class="GtkButton">
<property name="visible">1</property>
<property name="label" translatable="yes">Home</property>
<property name="focusable">1</property>
<property name="has-frame">0</property>
<property name="child">imghome</property>
<property name="receives-default">1</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="home_label">
<property name="visible">1</property>
<property name="hexpand">0</property>
<property name="label" translatable="yes">Home</property>
<layout>
<property name="column">0</property>
<property name="row">1</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>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</child>
</object>
</interface>

View File

@ -1,51 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">user-home</property>
</object>
<object class="GtkWindow" id="window">
<property name="can-focus">False</property>
<property name="title" translatable="yes">gtk(42)</property>
<property name="default-width">640</property>
<property name="default-height">360</property>
<property name="hide-titlebar-when-maximized">True</property>
<property name="icon-name">gtk4-icon</property>
<child>
<object class="GtkFixed" id="fixed">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkImage" id="background">
<property name="width-request">100</property>
<property name="height-request">80</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-missing-image</property>
</object>
</child>
<child>
<object class="GtkButton" id="btn_home">
<property name="label" translatable="yes">Home</property>
<property name="width-request">100</property>
<property name="height-request">80</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image1</property>
<property name="relief">none</property>
<property name="image-position">top</property>
<property name="always-show-image">True</property>
</object>
<packing>
<property name="x">70</property>
<property name="y">30</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

48
Gtk4/src/MainStack.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "MainStack.h"
static gboolean change_time(gpointer data){
//Get local time
time_t t;
struct tm *local;
t=time(NULL);
local=localtime(&t);
//Format the time and set the time
char current_time[20];
sprintf(current_time,"%02d:%02d %04d/%02d/%02d",
local->tm_hour,local->tm_min,local->tm_year+1900,local->tm_mon+1,local->tm_mday);
//g_print("%s\n",current_time);
gtk_label_set_label(GTK_LABEL(data),current_time);
return TRUE;
}
static void stack_login(GtkWidget *widget,GtkStack * stack){
gtk_stack_set_visible_child_name(stack,"main_page");
}
GtkWidget * create_main_stack(GtkWidget * left_box,GMenuModel * model){
GtkBuilder * stack_builder;
stack_builder = gtk_builder_new_from_resource("/org/gtk/daleclack/stack.ui");
//Get Main Widget
GtkWidget * main_stack;
main_stack = (GtkWidget*)gtk_builder_get_object(stack_builder,"stack");
//Login Button
GtkWidget * btnlogin = (GtkWidget*)gtk_builder_get_object(stack_builder,"btnlogin");
g_signal_connect(btnlogin,"clicked",G_CALLBACK(stack_login),main_stack);
//Time Label
GtkWidget * label_time = (GtkWidget*)gtk_builder_get_object(stack_builder,"label_time");
g_timeout_add(100,change_time,label_time);
//Menu Button
GtkWidget * menubtn = (GtkWidget*)gtk_builder_get_object(stack_builder,"menu_button");
GtkWidget * popover = gtk_popover_menu_new_from_model(model);
gtk_widget_set_halign(popover,GTK_ALIGN_END);
gtk_menu_button_set_popover(GTK_MENU_BUTTON(menubtn),popover);
//Box for LeftPanel
left_box = (GtkWidget*)gtk_builder_get_object(stack_builder,"leftbox");
return main_stack;
}

6
Gtk4/src/MainStack.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <gtk/gtk.h>
#include <ctime>
GtkWidget * create_main_stack(GtkWidget * left_box,GMenuModel * model);

View File

@ -1,6 +1,8 @@
#include "MainWin.h"
#include "MainStack.h"
#include "winpe.xpm"
#include "img7.xpm"
#include "image_types.h"
enum class BackMode{
DEFAULT_1,
@ -14,17 +16,70 @@ struct _MainWin{
GtkWidget * overlay;
GtkGesture * gesture;
GtkWidget * popover;
GtkWidget * stack;
GtkWidget * leftbox;
int width,height;
BackMode back_mode;
};
G_DEFINE_TYPE(MainWin,main_win,GTK_TYPE_APPLICATION_WINDOW)
static void change_background(GtkWindow * dialog,int response,MainWin *win){
gtk_window_destroy(dialog);
static void change_background(GtkNativeDialog * dialog,int response,MainWin *win){
if(response == GTK_RESPONSE_ACCEPT){
GFile * file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(dialog));
char * filename = g_file_get_path(file);
//Set Image from the file
GdkPixbuf * pixbuf, * sized;
pixbuf = gdk_pixbuf_new_from_file(filename,NULL);
sized = gdk_pixbuf_scale_simple(pixbuf,win->width,win->height,GDK_INTERP_BILINEAR);
gtk_picture_set_pixbuf(GTK_PICTURE(win->background),sized);
//Change Mode and free memory
win->back_mode = BackMode::CUSTOM;
g_object_unref(pixbuf);
g_object_unref(sized);
g_object_unref(file);
g_free(filename);
}
gtk_native_dialog_destroy(dialog);
}
static void background_dialog(GSimpleAction * action, GVariant * parmeter, gpointer data){
GtkFileChooserNative * dialog = gtk_file_chooser_native_new("Open Image File",GTK_WINDOW(data),
GTK_FILE_CHOOSER_ACTION_OPEN,"OK","Cancel");
GtkFileFilter * filter_image = gtk_file_filter_new();
gtk_file_filter_set_name(filter_image,"Image Files");
//For Systems supported mime type and not supported, use different way
if(mime_type_supported()){
gtk_file_filter_add_mime_type(filter_image,"image/*");
}else{
for(int i=0;supported_globs !=NULL && supported_globs[i]!=NULL;i++){
const char * glob = supported_globs[i];
gtk_file_filter_add_pattern(filter_image,glob);
}
}
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter_image);
GtkFileFilter * filter_any = gtk_file_filter_new();
gtk_file_filter_set_name(filter_any,"Any Files");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter_any);
g_signal_connect(dialog,"response",G_CALLBACK(change_background),data);
gtk_native_dialog_show(GTK_NATIVE_DIALOG(dialog));
}
static void logout_activated(GSimpleAction * action, GVariant * parmeter, gpointer data){
MainWin * win = MAIN_WIN(data);
gtk_stack_set_visible_child_name(GTK_STACK(win->stack),"login_page");
}
static void quit_activated(GSimpleAction * action, GVariant * parmeter, gpointer data){
gtk_window_destroy(GTK_WINDOW(data));
}
static void winsize_activated(GSimpleAction * action, GVariant * parmeter, gpointer data){
}
static void default_background1(GSimpleAction * action, GVariant * parmeter, gpointer data){
@ -72,7 +127,29 @@ static void refresh_activated(GSimpleAction * action, GVariant * parmeter, gpoin
static void about_activated(GSimpleAction * action, GVariant * parmeter, gpointer data){
GtkWindow * win =GTK_WINDOW(data);
const char *authors[]={
"Dale Clack",
"GCR_CMake on github https://github.com/Makman2/GCR_CMake",
NULL
};
char *version;
version=g_strdup_printf("4.0-Beta1\nRunning Against GTK %d.%d.%d",
gtk_get_major_version(),
gtk_get_minor_version(),
gtk_get_micro_version());
GtkWidget *dialog;
dialog=gtk_about_dialog_new();
gtk_show_about_dialog(win,
"program-name","My_GtkUI",
"version",version,
"copyright", "©2019—2021 The Xe Project",
"comments", "A program that simulates desktop",
"authors",authors,
"license-type",GTK_LICENSE_GPL_3_0,
"logo-icon-name","My_GtkUI",
"title","About My GtkUI (Gtk4 Version)",
NULL);
g_free(version);
}
static void gesture_pressed(GtkGestureClick *self,int n_press,double x,double y,MainWin * win){
@ -90,7 +167,11 @@ static GActionEntry entries[] = {
{"back",background_dialog,NULL,NULL,NULL},
{"default1",default_background1,NULL,NULL,NULL},
{"default2",default_background2,NULL,NULL,NULL},
{"refresh",refresh_activated,NULL,NULL,NULL}
{"refresh",refresh_activated,NULL,NULL,NULL},
{"about",about_activated,NULL,NULL,NULL},
{"logout",logout_activated,NULL,NULL,NULL},
{"quit",quit_activated,NULL,NULL,NULL},
{"size",winsize_activated,NULL,NULL,NULL}
};
static void main_win_init(MainWin * win){
@ -125,6 +206,12 @@ static void main_win_init(MainWin * win){
g_signal_connect(win->gesture,"pressed",G_CALLBACK(gesture_pressed),win);
gtk_widget_add_controller(win->overlay,GTK_EVENT_CONTROLLER(win->gesture));
//Add Main Page
win->stack = create_main_stack(win->leftbox,model);
gtk_widget_set_halign(GTK_WIDGET(win->stack),GTK_ALIGN_FILL);
gtk_widget_set_valign(GTK_WIDGET(win->stack),GTK_ALIGN_FILL);
gtk_overlay_add_overlay(GTK_OVERLAY(win->overlay),GTK_WIDGET(win->stack));
gtk_window_set_child(GTK_WINDOW(win),win->overlay);
g_object_unref(menu_builder);
}

View File

@ -1,59 +0,0 @@
#include <ctime>
#include "panel1.h"
#include "winconf.h"
#include "background.h"
static gboolean change_time(gpointer data){
//Get local time
time_t t;
struct tm *local;
t=time(NULL);
local=localtime(&t);
//Format the time and set the time
char current_time[20];
sprintf(current_time,"%02d:%02d %04d/%02d/%02d",
local->tm_hour,local->tm_min,local->tm_year+1900,local->tm_mon+1,local->tm_mday);
//g_print("%s\n",current_time);
gtk_label_set_label(GTK_LABEL(data),current_time);
return TRUE;
}
void add_toppanel(GtkBuilder *builder,GtkFixed *fixed){
//Get position
int width=640,height=480;
get_config(&width,&height);
//Get panel
GtkBuilder *panel=gtk_builder_new_from_resource("/gtk42/toppanel.ui");
GObject *panel1=gtk_builder_get_object(panel,"btnbox");
gtk_widget_set_size_request(GTK_WIDGET(panel1),width,15);
//Get timer label and set time
GObject *label_time=gtk_builder_get_object(panel,"label_time");
g_timeout_add(100,change_time,label_time);
//Get popover window
GObject *popover=gtk_builder_get_object(panel,"popover1");
//Get button for change background
GObject *btn_back=gtk_builder_get_object(panel,"btnback");
GObject *img_back=gtk_builder_get_object(panel,"image3");
gtk_image_set_from_resource(GTK_IMAGE(img_back),"/gtk42/graphics.png");
//gtk_button_set_image(GTK_BUTTON(btn_back),GTK_WIDGET(img_back));
gtk_button_set_always_show_image(GTK_BUTTON(btn_back),TRUE);
g_signal_connect(btn_back,"clicked",G_CALLBACK(fileopen),builder);
g_signal_connect_swapped(btn_back,"clicked",G_CALLBACK(gtk_widget_hide),popover);
//Config button
GObject *btn_conf=gtk_builder_get_object(panel,"btnset");
GObject *img_set=gtk_builder_get_object(panel,"image5");
gtk_image_set_from_resource(GTK_IMAGE(img_set),"/gtk42/system.png");
//gtk_button_set_image(GTK_BUTTON(btn_conf),GTK_WIDGET(img_set));
gtk_button_set_always_show_image(GTK_BUTTON(btn_conf),TRUE);
g_signal_connect(btn_conf,"clicked",G_CALLBACK(conf_dialog),builder);
g_signal_connect_swapped(btn_conf,"clicked",G_CALLBACK(gtk_widget_hide),popover);
//Get Exit button
GObject *btn_exit=gtk_builder_get_object(panel,"PanelExit");
GObject *img_exit=gtk_builder_get_object(panel,"image4");
gtk_image_set_from_resource(GTK_IMAGE(img_exit),"/gtk42/log-out.png");
//gtk_button_set_image(GTK_BUTTON(btn_exit),GTK_WIDGET(img_exit));
gtk_button_set_always_show_image(GTK_BUTTON(btn_exit),TRUE);
GObject *window=gtk_builder_get_object(builder,"window");
g_signal_connect_swapped(btn_exit,"clicked",G_CALLBACK(gtk_widget_destroy),window);
gtk_fixed_put(fixed,GTK_WIDGET(panel1),0,0);
}

View File

@ -1,10 +0,0 @@
#ifndef __TIMER_H_
#define __TIMER_H_
#include <gtk/gtk.h>
//gboolean change_time(gpointer data);
void add_toppanel(GtkBuilder *builder,GtkFixed *fixed);
#endif