Add gtk139

This commit is contained in:
daleclack 2022-08-08 17:49:30 +08:00
parent 99338dad1c
commit 4784308c67
5 changed files with 339 additions and 0 deletions

View File

@ -0,0 +1,81 @@
{
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"files.associations": {
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"hash_map": "cpp",
"strstream": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"source_location": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp"
}
}

View File

@ -0,0 +1,72 @@
set(CMAKE_CXX_STANDARD 17)
cmake_minimum_required(VERSION 3.0.0)
project(gtk139_media 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/MediaPlayer.cc)
#Compile Resource
# set(RESOURCE_LIST
# text_menu.xml)
# 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 x86_64-w64-mingw32-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})
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})
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,130 @@
#include "MediaPlayer.hh"
#include "../json_nlohmann/json.hpp"
#include <iostream>
#include <fstream>
#include <vector>
using json = nlohmann::json;
MediaPlayer::MediaPlayer()
: vbox(Gtk::Orientation::VERTICAL, 5),
btnbox(Gtk::Orientation::HORIZONTAL, 5)
{
// Set icon and title
set_title("Gtk4 media player");
set_icon_name("org.gtk.daleclack");
set_default_size(400, 500);
// Add widgets
video1.set_size_request(400, 225);
vbox.append(video1);
// Controllers
btnadd.set_image_from_icon_name("list-add");
btnremove.set_image_from_icon_name("list-remove");
btnload.set_image_from_icon_name("go-up");
btnsave.set_image_from_icon_name("document-save");
btnbox.append(btnadd);
btnbox.append(btnremove);
btnbox.append(btnload);
btnbox.append(btnsave);
vbox.append(btnbox);
btnadd.signal_clicked().connect(sigc::mem_fun(*this, &MediaPlayer::btnadd_clicked));
btnremove.signal_clicked().connect(sigc::mem_fun(*this, &MediaPlayer::btnremove_clicked));
btnload.signal_clicked().connect(sigc::mem_fun(*this, &MediaPlayer::btnload_clicked));
btnsave.signal_clicked().connect(sigc::mem_fun(*this, &MediaPlayer::btnsave_clicked));
// List of media files
scroll_win.set_hexpand();
scroll_win.set_vexpand();
scroll_win.set_child(treeview);
vbox.append(scroll_win);
// Initalize treeview
store = Gtk::ListStore::create(n_columns);
treeview.set_model(store);
treeview.append_column("File Name", n_columns.file_name);
selection = treeview.get_selection();
// Add vbox to the window
set_child(vbox);
}
void MediaPlayer::btnadd_clicked()
{
// Create Native dialog
dialog = Gtk::FileChooserNative::create("Open Media File", *this,
Gtk::FileChooser::Action::OPEN, "OK", "Cancel");
dialog->signal_response().connect(sigc::mem_fun(*this, &MediaPlayer::dialog_response));
// Add Filter
auto filter = Gtk::FileFilter::create();
filter->add_pattern("*");
filter->set_name("All Files");
dialog->add_filter(filter);
dialog->show();
}
void MediaPlayer::dialog_response(int response_id)
{
if (response_id == Gtk::ResponseType::ACCEPT)
{
// Get Path and basename
auto file = dialog->get_file();
auto filename = file->get_basename();
auto path = file->get_path();
// Append the item
auto row = *(store->append());
row[n_columns.file_name] = filename;
row[n_columns.file_path] = path;
file.reset();
}
dialog.reset();
}
void MediaPlayer::btnremove_clicked()
{
auto row = selection->get_selected();
store->erase(row);
}
void MediaPlayer::btnload_clicked()
{
// Clear current store
store->clear();
// Get data
std::ifstream playlist("playlist.json");
if (playlist.is_open())
{
// Get data
json data = json::parse(playlist);
std::vector<std::string> name_tmp = data["name"];
std::vector<std::string> path_tmp = data["path"];
// Initalize Liststore
if (name_tmp.size() != path_tmp.size())
{
std::cout << "Data Invaild!" << std::endl;
}
else
{
for (int i = 0; i < name_tmp.size(); i++)
{
// Append the item
auto row = *(store->append());
row[n_columns.file_name] = name_tmp[i];
row[n_columns.file_path] = path_tmp[i];
}
}
}
}
void MediaPlayer::btnsave_clicked()
{
}

View File

@ -0,0 +1,48 @@
#pragma once
#include <gtkmm.h>
class MediaPlayer : public Gtk::ApplicationWindow
{
public:
MediaPlayer();
private:
// Main video widget
Gtk::Video video1;
// Model Columns
class ModelColumns : public Gtk::TreeModelColumnRecord
{
public:
ModelColumns()
{
add(file_name);
add(file_path);
}
Gtk::TreeModelColumn<std::string> file_name;
Gtk::TreeModelColumn<std::string> file_path;
};
ModelColumns n_columns;
Glib::RefPtr<Gtk::ListStore> store;
// Listview widget
Gtk::TreeView treeview;
Glib::RefPtr<Gtk::TreeSelection> selection;
// Main widgets
Gtk::Box vbox, btnbox;
Gtk::ScrolledWindow scroll_win;
Gtk::Button btnadd, btnremove, btnload, btnsave;
// FileChooser
Glib::RefPtr<Gtk::FileChooserNative> dialog;
void dialog_response(int response_id);
// Signal Handlers
void btnadd_clicked();
void btnremove_clicked();
void btnload_clicked();
void btnsave_clicked();
};

View File

@ -0,0 +1,8 @@
#include "MediaPlayer.hh"
int main(int argc, char **argv)
{
// Create a application
auto app = Gtk::Application::create("org.gtk.daleclack");
app->make_window_and_run<MediaPlayer>(argc, argv);
}