egami
C++ Image Processing program
/homes/esi30/DCS339/coursework/src/display_page.cpp
Go to the documentation of this file.
00001 #include <algorithm>
00002 #include <memory>
00003 #include <string>
00004 #include <tuple>
00005 
00006 #include <gtkmm/button.h>
00007 #include <gtkmm/enums.h>
00008 #include <gtkmm/image.h>
00009 #include <gtkmm/scrolledwindow.h>
00010 #include <gtkmm/stock.h>
00011 #include <gtkmm/widget.h>
00012 
00013 #include "catalogue.hpp"
00014 #include "display_page.hpp"
00015 #include "display_unit.hpp"
00016 
00017 Display_page::Display_page(): s_y1(-1), s_x1(-1), s_y2(-1), s_x2(-1) {}
00018 
00019 void Display_page::detach_child(){
00020     if(!display_unit)
00021         return;
00022 
00023     remove(c2);
00024     c2.remove();
00025     reinterpret_cast<Gtk::Widget*>(display_unit.get())->unparent();
00026 
00027     if(Gtk::Widget *father_widget=reinterpret_cast<Gtk::Widget*>(display_unit->father.get())){
00028         remove(c1);
00029         c1.remove();
00030         father_widget->unparent();
00031     }
00032 }
00033 
00034 void Display_page::attach_child(){
00035     if(!display_unit)
00036         return;
00037     add2(c2);
00038     c2.add(*reinterpret_cast<Gtk::Widget*>(display_unit.get()));
00039 
00040     if(Gtk::Widget *father_widget=reinterpret_cast<Gtk::Widget*>(display_unit->father.get())){
00041         add1(c1);
00042         c1.add(*father_widget);
00043     }
00044 
00045     set_position(get_allocated_width()/2);
00046     show_all_children();
00047 }
00048 
00049 void Display_page::redo(){
00050     if(display_unit->child){
00051         detach_child();
00052         display_unit=display_unit->child;
00053         attach_child();
00054     }
00055 }
00056 
00057 bool Display_page::save(){
00058     return display_unit->save();
00059 }
00060 
00061 void Display_page::save_as(const std::string &fp){
00062     return display_unit->save_as(fp);
00063 }
00064 
00065 void Display_page::undo(){
00066     if(display_unit->father){
00067         detach_child();
00068         display_unit=display_unit->father;
00069         attach_child();
00070     }
00071 }
00072 
00073 void swap(Display_page &lhs, Display_page &rhs) noexcept {
00074     using std::swap; // Enable ADL
00075 
00076     swap(lhs.display_unit, rhs.display_unit);
00077 }
00078 
00079 bool Display_page::on_button_press_event(GdkEventButton *event){
00080     if(event->type==Gdk::BUTTON_PRESS){
00081         static bool arg;
00082         if(arg){
00083             s_y1=event->y;
00084             s_x1=event->x;
00085         }else{
00086             s_y2=event->y;
00087             s_x2=event->x;
00088         }
00089         arg=!arg;
00090     }
00091     return Gtk::HPaned::on_button_press_event(event);
00092 }
00093 
00094 std::tuple<int,int,int,int> Display_page::selection() const {
00095     int ya=std::min(s_y1, s_y2), xa=std::min(s_x1, s_x2), yb=std::max(s_y1, s_y2), xb=std::max(s_x1, s_x2);
00096     if(s_y1<0 || s_x1<0 || s_y2<0 || s_x2<0)
00097         return std::tuple<int,int,int,int>(-1, -1, -1, -1);
00098     return std::tuple<int,int,int,int>(ya, xa, yb-ya, xb-xa);
00099 }
00100 
00101 Display_page_header::Display_page_header(const std::string &text): title(text), close_img(Gtk::Stock::CLOSE, Gtk::ICON_SIZE_MENU) {
00102     close.set_image(close_img);
00103     close.signal_clicked().connect(sigc::mem_fun(*this, &Display_page_header::on_close));
00104     pack_start(title);
00105     pack_end(close);
00106     show_all_children();
00107 }
00108 
00109 void Display_page_header::assign(Display_catalogue *f, unsigned i){
00110     father=f;
00111     id=i;
00112 }
00113 
00114 void Display_page_header::on_close(){
00115     father->remove(id);
00116     /* XXX Warning: *this object already destructed here.
00117      * The program is still well-formed and conforming implementations should run the program without error.
00118      * C.f. ISO WG21 n3290 Working Draft, Standard for Programming Language C++ Section 3.8 [basic.life] Paragraph 5.
00119      */
00120 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends