Add label to show config

This commit is contained in:
daleclack 2022-02-03 15:38:26 +08:00
parent 41c83c45eb
commit 89e8eafc46
4 changed files with 188 additions and 27 deletions

View File

@ -4,11 +4,13 @@
<requires lib="gtk+" version="3.24"/>
<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>
@ -96,7 +98,7 @@
<property name="row-spacing">5</property>
<property name="column-spacing">5</property>
<child>
<object class="GtkComboBoxText">
<object class="GtkComboBoxText" id="combo_default">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="active">2</property>
@ -114,12 +116,14 @@
</packing>
</child>
<child>
<object class="GtkRadioButton">
<object class="GtkRadioButton" id="radio_default">
<property name="label" translatable="yes">Use default window size</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
<property name="group">radio_custom</property>
</object>
<packing>
<property name="left-attach">1</property>
@ -128,7 +132,7 @@
</packing>
</child>
<child>
<object class="GtkRadioButton">
<object class="GtkRadioButton" id="radio_custom">
<property name="label" translatable="yes">User defined window size</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
@ -142,8 +146,9 @@
</packing>
</child>
<child>
<object class="GtkSpinButton">
<object class="GtkSpinButton" id="spin_width">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
<property name="adjustment">adjustment1</property>
<property name="value">1024</property>
@ -154,8 +159,9 @@
</packing>
</child>
<child>
<object class="GtkSpinButton">
<object class="GtkSpinButton" id="spin_height">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
<property name="adjustment">adjustment2</property>
<property name="value">576</property>
@ -221,13 +227,7 @@
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton">
<object class="GtkButton" id="btnapply">
<property name="label" translatable="yes">Apply</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
@ -238,6 +238,33 @@
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnGet">
<property name="label" translatable="yes">Get Current</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label_size">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Current Config: </property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>

View File

@ -4,12 +4,17 @@
#include "image_types.hh"
#include "cfgfile/cfgfile.hh"
#include <iostream>
#include <fstream>
MyPrefs::MyPrefs()
: main_box(Gtk::ORIENTATION_VERTICAL, 10),
views_box(Gtk::ORIENTATION_HORIZONTAL, 5),
btnbox(Gtk::ORIENTATION_HORIZONTAL, 5)
btnbox(Gtk::ORIENTATION_HORIZONTAL, 5),
width(1024),
height(576)
{
/*Step 1: Initalize widget that without Gtk::Builder*/
// Initalize Window
set_title("Window Preferences");
set_default_size(800, 450);
@ -96,10 +101,35 @@ MyPrefs::MyPrefs()
main_box.set_margin_start(margin_value);
main_box.set_margin_end(margin_value);
//Get Widgets for multi pages
/*Step 2: Initalize widgets from glade xml file ('.ui' file)*/
// Get Widgets for multi pages
stackbuilder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/prefs_stack.ui");
stackbuilder->get_widget("stack_box",stack_box);
stackbuilder->get_widget("back_page",back_page);
stackbuilder->get_widget("stack_box", stack_box);
stackbuilder->get_widget("back_page", back_page);
stackbuilder->get_widget("combo_default", combo_default);
stackbuilder->get_widget("radio_default", radio_default);
stackbuilder->get_widget("radio_custom", radio_custom);
stackbuilder->get_widget("spin_width", spin_width);
stackbuilder->get_widget("spin_height", spin_height);
stackbuilder->get_widget("btnapply", btnapply);
stackbuilder->get_widget("btnGet",btnGet);
stackbuilder->get_widget("label_size",label_size);
// Initalize radio buttons
radio_default->set_active();
radio_default->signal_toggled().connect(sigc::mem_fun(*this, &MyPrefs::radiobutton_toggled));
radio_custom->signal_toggled().connect(sigc::mem_fun(*this, &MyPrefs::radiobutton_toggled));
// Initalize other widgets
btnapply->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnapply_clicked));
btnGet->signal_clicked().connect(sigc::mem_fun(*this,&MyPrefs::btnGet_clicked));
// Initalize Label
load_winsize_config();
char * size_str = g_strdup_printf("Current Config: %d x %d", width, height);
label_size->set_label(size_str);
g_free(size_str);
back_page->pack_start(main_box);
add(*stack_box);
@ -295,7 +325,7 @@ void MyPrefs::images_view_changed()
auto row = *(image_selection->get_selected());
if (row[n_columns.m_col_internal])
{
std::string path = row[n_columns.m_col_path];
path = row[n_columns.m_col_path];
switch (path[1])
{
case '1':
@ -308,8 +338,8 @@ void MyPrefs::images_view_changed()
}
else
{
std::string path = row[n_columns.m_col_path];
set_background_file(path);
path = row[n_columns.m_col_path];
set_background_file();
}
}
}
@ -318,23 +348,108 @@ void MyPrefs::set_background_internal(const char *const *data)
{
// Set a internal background
auto pixbuf = Gdk::Pixbuf::create_from_xpm_data(data);
auto sized = pixbuf->scale_simple(1024, 576, Gdk::INTERP_BILINEAR);
auto sized = pixbuf->scale_simple(width, height, Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(background1->gobj(), sized->gobj());
pixbuf.reset();
sized.reset();
background_internal = true;
}
void MyPrefs::set_background_file(std::string &path)
void MyPrefs::set_background_file()
{
// Set Background from a file
auto pixbuf = Gdk::Pixbuf::create_from_file(path);
auto sized = pixbuf->scale_simple(1024, 576, Gdk::INTERP_BILINEAR);
auto sized = pixbuf->scale_simple(width, height, Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(background1->gobj(), sized->gobj());
pixbuf.reset();
sized.reset();
background_internal = false;
}
void MyPrefs::update_background_size()
{}
void MyPrefs::set_background(Gtk::Image *back)
{
background1 = back;
}
void MyPrefs::radiobutton_toggled()
{
// Change sensitive state of buttons
bool mode = radio_default->get_active();
combo_default->set_sensitive(mode);
spin_height->set_sensitive(!mode);
spin_width->set_sensitive(!mode);
btnGet->set_sensitive(!mode);
}
void MyPrefs::btnapply_clicked()
{
// Get Config
if (radio_default->get_active())
{
int mode = combo_default->get_active_row_number();
switch (mode)
{
case 0:
width = 640;
height = 360;
break;
case 1:
width = 800;
height = 576;
break;
case 2:
width = 1024;
height = 576;
break;
case 3:
width = 1280;
height = 720;
break;
}
}
else
{
width = spin_width->get_value_as_int();
height = spin_height->get_value_as_int();
}
// Open the file for configs
std::fstream outfile;
outfile.open("config", std::ios_base::out);
if (outfile.is_open())
{
outfile << "width=" << width << std::endl;
outfile << "height=" << height << std::endl;
outfile.close();
}
}
void MyPrefs::btnGet_clicked()
{
//Get Current Window Size
width = background1->get_width();
height = background1->get_height();
spin_width->set_value(width);
spin_height->set_value(height);
}
void MyPrefs::load_winsize_config(){
std::string height_str, width_str;
// Read values from a file
if (readCfgFile("config", "width", width_str) && readCfgFile("config", "height", height_str))
{
height = atoi(height_str.c_str());
width = atoi(width_str.c_str());
}
}
void MyPrefs::get_winsize_config(int &width1, int &height1)
{
// Apply Config
width1 = width;
height1 = height;
}

View File

@ -8,6 +8,9 @@ class MyPrefs : public Gtk::Window
public:
MyPrefs();
void set_background(Gtk::Image *back);
void load_winsize_config();
void get_winsize_config(int &width1,int &height1);
void update_background_size();
protected:
class ModelColumns : public Gtk::TreeModelColumnRecord
@ -32,12 +35,20 @@ protected:
bool has_selection;
private:
// Widget for parent window
// Background widget and properties
int width, height;
Gtk::Image *background1;
std::string path;
bool background_internal;
//Page switcher and another page
Glib::RefPtr<Gtk::Builder> stackbuilder;
Gtk::Box *stack_box, *back_page;
Gtk::Box *stack_box, *back_page, *winsize_page;
Gtk::RadioButton *radio_default, *radio_custom;
Gtk::ComboBoxText *combo_default;
Gtk::SpinButton *spin_width, *spin_height;
Gtk::Button *btnapply, *btnGet;
Gtk::Label *label_size;
// Child Widgets
Gtk::TreeView folders_view, images_view;
@ -62,5 +73,8 @@ private:
void default_folders_view();
void update_images_view(std::string &folder_path);
void set_background_internal(const char *const *data);
void set_background_file(std::string &path);
void set_background_file();
void radiobutton_toggled();
void btnapply_clicked();
void btnGet_clicked();
};

View File

@ -3,14 +3,17 @@
MyWin::MyWin()
{
int width, height;
// Initalize Window
set_icon_name("org.gtk.daleclack");
set_default_size(1024, 576);
set_title("Background Preferences Test");
prefs_win.get_winsize_config(width, height);
// Add Background
auto pixbuf = Gdk::Pixbuf::create_from_xpm_data(winpe);
auto sized = pixbuf->scale_simple(1024, 576, Gdk::INTERP_BILINEAR);
auto sized = pixbuf->scale_simple(width, height, Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(m_back.gobj(), sized->gobj());
pixbuf.reset();
sized.reset();
@ -24,6 +27,8 @@ MyWin::MyWin()
btnback.signal_clicked().connect(sigc::mem_fun(*this, &MyWin::btnback_clicked));
// Add widgets
m_back.set_hexpand();
m_back.set_vexpand();
m_overlay.add(m_back);
add(m_overlay);