On 11/27/2013 12:59 PM, Kjell Ahlstedt
wrote:
Here is how I solved it the problem, probably not the best way, but it works kind of: //mainwindow.h class MainWindow : public Gtk::Window { public: MainWindow(); virtual ~MainWindow(); protected: bool txtChange(GdkEventKey*, Gtk::Entry&, bool override = false); void txtDelete(int start_pos, int end_pos, Gtk::Entry&, bool override = false); void boxMainChange(Gtk::Allocation&); Gtk::Box box_main_, box_split_, box_ans_, box_exp_; Gtk::Label lbl_exp_, lbl_ans_; Gtk::Entry txt_exp_, txt_ans_; private: inline int charToPixals(int chars) const; int getSize(int diff); inline int txt_length_to_pix(int txt_length) const; const int default_txtbox_size_; static constexpr int kchar_limit_ = 20; static constexpr int kavg_char_size = 2; static constexpr int kchars_width_ = 4; int win_width_; }; //mainwinoh.cc MainWindow::MainWindow() : default_txtbox_size_(txt_exp_.get_allocated_width()) { Gtk::Window::set_default_size(400, 320); //box_main box_main_.set_orientation(Gtk::ORIENTATION_VERTICAL); //box_split box_split_.set_orientation(Gtk::ORIENTATION_VERTICAL); //box_exp box_exp_.set_orientation(Gtk::ORIENTATION_HORIZONTAL); box_exp_.set_halign(Gtk::ALIGN_CENTER); box_exp_.set_margin_top(30); box_exp_.set_margin_bottom(30); //box_ans box_ans_.set_orientation(Gtk::ORIENTATION_HORIZONTAL); box_ans_.set_halign(Gtk::ALIGN_CENTER); box_ans_.set_margin_bottom(30); //lbl_exp lbl_exp_.set_text("_expression_: "); lbl_exp_.set_margin_right(5); //lbl_ans lbl_ans_.set_text("Answer: "); lbl_ans_.set_margin_right(5); //Setup MainWindow Gtk::Window::add(box_main_); box_main_.pack_start(box_split_); box_split_.pack_start(box_exp_); box_split_.pack_start(box_ans_); box_exp_.pack_start(lbl_exp_); box_exp_.pack_start(txt_exp_); box_ans_.pack_start(lbl_ans_); box_ans_.pack_start(txt_ans_); //update txtbox size txt_exp_.signal_key_press_event().connect(sigc::bind<Gtk::Entry&, bool>(sigc::mem_fun(*this, &MainWindow::txtChange), txt_exp_, true), false); txt_exp_.signal_delete_text().connect(sigc::bind<Gtk::Entry&, bool>(sigc::mem_fun(*this, &MainWindow::txtDelete), txt_exp_, true)); //txt_ans_.signal_changed() txt_ans_.signal_delete_text().connect(sigc::bind<Gtk::Entry&, bool>(sigc::mem_fun(*this, &MainWindow::txtDelete), txt_ans_, false)); txt_ans_.signal_key_press_event().connect(sigc::bind<Gtk::Entry&, bool>(sigc::mem_fun(*this, &MainWindow::txtChange), txt_ans_, false), false); //increases txt box size when window size is increased signal_size_allocate().connect(sigc::mem_fun(*this, &MainWindow::boxMainChange)); MainWindow::show_all_children(); } MainWindow::~MainWindow() { } inline int MainWindow::charToPixals(int chars) const { return kavg_char_size * chars; } bool MainWindow::txtChange(GdkEventKey* key, Gtk::Entry& entry, bool override) { if(key->keyval == 65288) //Gdk number for backspace return false; int txt_length = entry.get_text_length(); int txtl_p = txt_length_to_pix(txt_length); if(entry.get_allocated_width() > txtl_p) //txtbox is big enough already from Mainwindow return false; if(txt_length < kchar_limit_) return false; if(override) txt_ans_.set_size_request(entry.get_width() + charToPixals(kchars_width_), -1); entry.set_size_request(entry.get_width() + charToPixals(kchars_width_), -1); return false; } void MainWindow::txtDelete(int start_pos, int end_pos, Gtk::Entry& entry, bool override) { int txt_length = entry.get_text_length(); if(txt_length < kchar_limit_) return; if(override) txt_ans_.set_size_request(entry.get_width() - charToPixals(kchars_width_), -1); entry.set_size_request(entry.get_width() - charToPixals(kchars_width_), -1); if(txt_length < kchar_limit_) entry.set_size_request(default_txtbox_size_, -1); } int MainWindow::getSize(int diff) { if(diff == 1) return 1; else if(diff < 6) return 2; else if(diff < 11) return 3; else if(diff < 21) return 10; else if(diff < 31) return 25; else if(diff < 41) return 35; else if(diff < 51) return 40; else return 60; } inline int MainWindow::txt_length_to_pix(int txt_length) const { return txt_length * 10; } void MainWindow::boxMainChange(Gtk::Allocation& a) { if(txt_length_to_pix(txt_exp_.get_text_length()) > txt_exp_.get_width()) return; if(win_width_ < get_width()) { txt_exp_.set_size_request(txt_exp_.get_width() + getSize(a.get_width() - win_width_), -1); txt_ans_.set_size_request(txt_ans_.get_width() + getSize(a.get_width() - win_width_), -1); win_width_ = a.get_width(); } else if (win_width_ > get_width()) { txt_exp_.set_size_request(txt_exp_.get_width() - getSize(win_width_ - a.get_width()), -1); txt_ans_.set_size_request(txt_ans_.get_width() - getSize(win_width_ - a.get_width()), -1); win_width_ = a.get_width(); } //return false; } |