~zanneth/hirogen

91fe59c0f984d08d205cd2114795d3269d405951 — Charles Magahern 2 years ago 0668f95
Implement status bar that shows current offset and selection length
2 files changed, 87 insertions(+), 2 deletions(-)

M res/main.ui
M src/document_window.rs
M res/main.ui => res/main.ui +54 -0
@@ 382,6 382,60 @@
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkFrame">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label_xalign">0</property>
            <child>
              <object class="GtkBox">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="homogeneous">True</property>
                <child>
                  <object class="GtkLabel" id="offset-label">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="halign">start</property>
                    <property name="label" translatable="yes">Offset: XX</property>
                    <attributes>
                      <attribute name="font-desc" value="Monospace 10"/>
                    </attributes>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkLabel" id="length-label">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="halign">start</property>
                    <property name="label" translatable="yes">Length: XX</property>
                    <attributes>
                      <attribute name="font-desc" value="Monospace 10"/>
                    </attributes>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
              </object>
            </child>
            <child type="label_item">
              <placeholder/>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>

M src/document_window.rs => src/document_window.rs +33 -2
@@ 61,6 61,9 @@ pub struct DocumentWindow
    hex_adjustment:             gtk::Adjustment,
    scrollbar:                  gtk::Scrollbar,
    
    offset_label:               gtk::Label,
    length_label:               gtk::Label,
    
    clipboard:                  Option<gtk::Clipboard>,
    undo_action:                Option<gio::SimpleAction>,
    redo_action:                Option<gio::SimpleAction>,


@@ 82,6 85,8 @@ impl DocumentWindow
        let hex_view: gtk::DrawingArea = builder.get_object("hex-view").unwrap();
        let hex_adjustment: gtk::Adjustment = builder.get_object("hex-adjustment").unwrap();
        let scrollbar: gtk::Scrollbar = builder.get_object("hex-scrollbar").unwrap();
        let offset_label: gtk::Label = builder.get_object("offset-label").unwrap();
        let length_label: gtk::Label = builder.get_object("length-label").unwrap();
        
        let clipboard = gtk::Clipboard::get_default(&window.get_display());



@@ 92,6 97,8 @@ impl DocumentWindow
            hex_adjustment,
            hex_view,
            scrollbar,
            offset_label,
            length_label,
            
            clipboard,
            undo_action: None,


@@ 820,6 827,7 @@ impl DocumentWindow
            let mut document = self.document.borrow_mut();
            document.move_selection(&direction);
            self.hex_view_queue_redraw();
            self.reload_status_bar();

            // check to see if we must page up/down the scrolling window based on the new selection.
            if let Some(selection) = document.checked_selection() {


@@ 1018,8 1026,7 @@ impl DocumentWindow
                self.scroll_to_selection();
            }

            self.reload_actions_state();
            self.hex_view_queue_redraw();
            self.reload_ui();
        }
        
        found_result_and_updated_sel


@@ 1263,12 1270,36 @@ impl DocumentWindow
        }
    }
    
    fn reload_status_bar(self: &DocumentWindowRef)
    {
        let document = self.document.borrow();
        match document.checked_selection() {
            Some(selection) => {
                let range = &selection.byte_range;
                self.offset_label.set_text(&format!("Offset: 0x{:x}", range.start));
                
                let length = range.end - range.start;
                if length > 1 {
                    self.length_label.set_text(&format!("Length: 0x{:x}", range.end - range.start));
                } else {
                    self.length_label.set_text("");
                }
            },
            
            None => {
                self.offset_label.set_text("");
                self.length_label.set_text("");
            }
        }
    }
    
    fn reload_ui(self: &DocumentWindowRef)
    {
        self.hex_view_queue_redraw();
        self.reload_actions_state();
        self.reload_scrollbar_visibility();
        self.reload_window_title();
        self.reload_status_bar();
    }
}