diff --git a/Gtkmm3/gtk79_livetest/CMakeLists.txt b/Gtkmm3/gtk79_livetest/CMakeLists.txt new file mode 100644 index 0000000..fea7e84 --- /dev/null +++ b/Gtkmm3/gtk79_livetest/CMakeLists.txt @@ -0,0 +1,22 @@ +set(CMAKE_CXX_STANDARD 17) +cmake_minimum_required(VERSION 3.0.0) +project(gtk79 VERSION 0.1.0) + +include(CTest) +enable_testing() + +set(CPACK_PROJECT_NAME ${PROJECT_NAME}) +set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) +include(CPack) + +find_package (PkgConfig REQUIRED) +pkg_check_modules (GTKMM3 REQUIRED gtkmm-3.0) +include_directories (${GTKMM3_INCLUDE_DIRS}) +include_directories (..) +link_directories (${GTKMM3_LIBRARY_DIRS}) + +#names of source files +add_executable(gtk79 src/main.cc src/MyWin.cc) + +add_definitions (${GTKMM3_CFLAGS_OTHER}) +target_link_libraries (${PROJECT_NAME} ${GTKMM3_LIBRARIES}) diff --git a/Gtkmm3/gtk79_livetest/src/MyWin.cc b/Gtkmm3/gtk79_livetest/src/MyWin.cc new file mode 100644 index 0000000..d74c600 --- /dev/null +++ b/Gtkmm3/gtk79_livetest/src/MyWin.cc @@ -0,0 +1,67 @@ +#include "MyWin.hh" +#include "winpe.xpm" + +MyWin::MyWin() +:btn_back("Change Background") +{ + set_icon_name("org.gtk.daleclack"); + set_default_size(640,360); + //Add Overlay and image to the window + add(overlay); + overlay.add(back); + //Add the button + btn_back.set_halign(Gtk::ALIGN_CENTER); + btn_back.set_valign(Gtk::ALIGN_CENTER); + btn_back.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::btnback_clicked)); + overlay.add_overlay(btn_back); + //Background + Glib::RefPtr pixbuf=Gdk::Pixbuf::create_from_xpm_data(winpe); + Glib::RefPtr sized=pixbuf->scale_simple(640,360,Gdk::INTERP_BILINEAR); + gtk_image_set_from_pixbuf(back.gobj(),sized->gobj()); + //Release Memory + pixbuf.reset(); + sized.reset(); + //Show all children + show_all_children(); +} + +void MyWin::btnback_clicked(){ + Gtk::FileChooserDialog dialog1("Choose an image file", + Gtk::FILE_CHOOSER_ACTION_OPEN); + dialog1.set_transient_for(*this); + + //Add reponse buttons + dialog1.add_button("_Cancel",Gtk::RESPONSE_CANCEL); + dialog1.add_button("_OK",Gtk::RESPONSE_OK); + + //Add filters + auto filter_image=Gtk::FileFilter::create(); + filter_image->set_name("Image Files"); + filter_image->add_mime_type("image/*"); + dialog1.add_filter(filter_image); + + auto filter_any = Gtk::FileFilter::create(); + filter_any->set_name("Any files"); + filter_any->add_pattern("*"); + dialog1.add_filter(filter_any); + + //Show the dialog and wait for a user response: + int result = dialog1.run(); + + if(result=Gtk::RESPONSE_OK){ + //Get the image file and set the image from the file + std::string filename; + filename=dialog1.get_filename(); + Glib::RefPtr pixbuf=Gdk::Pixbuf::create_from_file(filename); + Glib::RefPtr sized=pixbuf->scale_simple(640,360,Gdk::INTERP_BILINEAR); + gtk_image_set_from_pixbuf(back.gobj(),sized->gobj()); + //Free the memory + pixbuf.reset(); + sized.reset(); + } + filter_image.reset(); + filter_any.reset(); +} + +MyWin::~MyWin(){ +} diff --git a/Gtkmm3/gtk79_livetest/src/MyWin.hh b/Gtkmm3/gtk79_livetest/src/MyWin.hh new file mode 100644 index 0000000..99bec9b --- /dev/null +++ b/Gtkmm3/gtk79_livetest/src/MyWin.hh @@ -0,0 +1,16 @@ +#pragma once + +#include + +class MyWin : public Gtk::Window{ +public: + MyWin(); + virtual ~MyWin(); +protected: + //Signal handlers + void btnback_clicked(); + //Child widgets + Gtk::Overlay overlay; + Gtk::Image back; + Gtk::Button btn_back; +}; diff --git a/Gtkmm3/gtk79_livetest/src/main.cc b/Gtkmm3/gtk79_livetest/src/main.cc new file mode 100644 index 0000000..0c49a3a --- /dev/null +++ b/Gtkmm3/gtk79_livetest/src/main.cc @@ -0,0 +1,7 @@ +#include "MyWin.hh" + +int main(int argc,char **argv){ + auto app=Gtk::Application::create(argc,argv,"org.gtk.daleclack"); + MyWin window; + return app->run(window); +} \ No newline at end of file