[latexila/gnome-3] GNOME 3: fix search and replace (not finished)



commit a5ebf88cbb258be0653f16833ee49b4ff59f3f25
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Sat Nov 26 18:16:12 2011 +0100

    GNOME 3: fix search and replace (not finished)
    
    Before, a glade .ui file was used. But glade is too unstable, I was
    unable to modify the .ui file without getting a segfault, or other bugsâ
    So the "easier" way is to write directly the code, but it's not
    finishedÂ:(

 src/search.vala |  513 ++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 299 insertions(+), 214 deletions(-)
---
diff --git a/src/search.vala b/src/search.vala
index 2f9fb5f..2c3707b 100644
--- a/src/search.vala
+++ b/src/search.vala
@@ -90,301 +90,387 @@ public class GotoLine : Grid
 
 public class SearchAndReplace : GLib.Object
 {
-    private unowned MainWindow main_window;
-    private Document working_document;
-
-    private Widget widget;
+    private unowned MainWindow _main_window;
+    private Document _working_document;
 
-    private Button button_arrow;
-    private Arrow arrow;
+    private Grid _main_grid;
+    private Grid _replace_grid;
 
-    private Entry entry_find;
-    private Label label_find_normal;
-    private Label label_find_error;
+    private Button _button_arrow;
+    private Arrow _arrow;
 
-    private Entry entry_replace;
-    private Frame frame_replace;
+    private Entry _entry_find;
+    private Label _label_find_normal;
+    private Label _label_find_error;
 
-    private Box hbox_replace;
+    private Entry _entry_replace;
 
-    private CheckMenuItem check_case_sensitive;
-    private CheckMenuItem check_entire_word;
+    private CheckMenuItem _check_case_sensitive;
+    private CheckMenuItem _check_entire_word;
 
     private int min_nb_chars_for_incremental_search = 3;
 
-    private bool search_and_replace_mode
+    private enum Mode
+    {
+        SEARCH,
+        SEARCH_AND_REPLACE
+    }
+
+    private Mode get_mode ()
     {
-        get { return arrow.arrow_type == ArrowType.UP; }
+        if (_arrow.arrow_type == ArrowType.UP)
+            return Mode.SEARCH_AND_REPLACE;
+
+        return Mode.SEARCH;
     }
 
     private bool case_sensitive
     {
-        get { return check_case_sensitive.get_active (); }
+        get { return _check_case_sensitive.get_active (); }
     }
 
     private bool entire_word
     {
-        get { return check_entire_word.get_active (); }
+        get { return _check_entire_word.get_active (); }
     }
 
     public SearchAndReplace (MainWindow main_window)
     {
-        this.main_window = main_window;
+        _main_window = main_window;
+
+        _main_grid = new Grid ();
+
+        /* Arrow */
+        _button_arrow = new Button ();
+        _arrow = new Arrow (ArrowType.DOWN, ShadowType.OUT);
+        _button_arrow.add (_arrow);
+        _main_grid.attach (_button_arrow, 0, 0, 1, 1);
+
+        /* Find entry */
+        Frame frame_find = get_find_entry ();
+        _main_grid.attach (frame_find, 1, 0, 1, 1);
+
+        /* Buttons at the right of the find entry */
+        Button button_clear_find = get_button (Stock.CLEAR);
+        Button button_previous = get_button (Stock.GO_UP);
+        Button button_next = get_button (Stock.GO_DOWN);
+        Button button_close = get_button (Stock.CLOSE);
+
+        _main_grid.attach (button_clear_find, 2, 0, 1, 1);
+        _main_grid.attach (button_previous, 3, 0, 1, 1);
+        _main_grid.attach (button_next, 4, 0, 1, 1);
+        _main_grid.attach (button_close, 5, 0, 1, 1);
 
-        string path = Path.build_filename (Config.DATA_DIR, "ui",
-            "search_and_replace.ui");
+        /* Replace entry */
+        _replace_grid = new Grid ();
+        _replace_grid.set_orientation (Orientation.HORIZONTAL);
+        _main_grid.attach (_replace_grid, 1, 1, 5, 1);
 
-        try
+        Frame frame_replace = new Frame (null);
+        frame_replace.width_request = 350;
+
+        _entry_replace = new Entry ();
+        _entry_replace.has_frame = false;
+        _entry_replace.set_tooltip_text (_("Replace with"));
+        _entry_replace.can_focus = true;
+
+        frame_replace.add (_entry_replace);
+        _replace_grid.add (frame_replace);
+
+        /* Buttons at the right of the replace entry */
+        Button button_clear_replace = get_button (Stock.CLEAR);
+        Button button_replace = get_button (Stock.FIND_AND_REPLACE);
+
+        // replace all: image + label
+        Button button_replace_all = new Button ();
+        Grid replace_all_grid = new Grid ();
+        replace_all_grid.set_orientation (Orientation.HORIZONTAL);
+        replace_all_grid.set_column_spacing (8);
+
+        Image image = new Image.from_stock (Stock.FIND_AND_REPLACE, IconSize.MENU);
+        replace_all_grid.add (image);
+
+        Label label = new Label (_("All"));
+        replace_all_grid.add (label);
+        button_replace_all.add (replace_all_grid);
+
+        _replace_grid.add (button_clear_replace);
+        _replace_grid.add (button_replace);
+        _replace_grid.add (button_replace_all);
+
+        /* signal handlers */
+
+        _button_arrow.clicked.connect (() =>
         {
-            Builder builder = new Builder ();
-            builder.add_from_file (path);
-
-            /* get objects */
-            widget = (Widget) builder.get_object ("search_and_replace");
-            // we unparent the main widget because the ui file contains a window
-            widget.unparent ();
-
-            button_arrow = (Button) builder.get_object ("button_arrow");
-            arrow = (Arrow) builder.get_object ("arrow");
-
-            entry_find = (Entry) builder.get_object ("entry_find");
-            label_find_normal = (Label) builder.get_object ("label_find_normal");
-            label_find_error = (Label) builder.get_object ("label_find_error");
-            EventBox eventbox_label1 = (EventBox) builder.get_object ("eventbox_label1");
-            EventBox eventbox_label2 = (EventBox) builder.get_object ("eventbox_label2");
-            Button button_clear_find = (Button) builder.get_object ("button_clear_find");
-
-            entry_replace = (Entry) builder.get_object ("entry_replace");
-            frame_replace = (Frame) builder.get_object ("frame_replace");
-            Button button_clear_replace =
-                (Button) builder.get_object ("button_clear_replace");
-            Button button_replace = (Button) builder.get_object ("button_replace");
-            Button button_replace_all =
-                (Button) builder.get_object ("button_replace_all");
-
-            hbox_replace = builder.get_object ("hbox_replace") as Box;
-
-            Button button_previous = (Button) builder.get_object ("button_previous");
-            Button button_next = (Button) builder.get_object ("button_next");
-
-            Button button_close = (Button) builder.get_object ("button_close");
-
-            /* styles */
-            Gdk.Color white;
-            Gdk.Color.parse ("white", out white);
-            eventbox_label1.modify_bg (StateType.NORMAL, white);
-            eventbox_label2.modify_bg (StateType.NORMAL, white);
-
-            /* options menu */
-            Menu menu = new Menu ();
-            check_case_sensitive = new CheckMenuItem.with_label (_("Case sensitive"));
-            check_entire_word = new CheckMenuItem.with_label (_("Entire words only"));
-            menu.append (check_case_sensitive);
-            menu.append (check_entire_word);
-            menu.show_all ();
-
-            /* signal handlers */
-
-            entry_find.icon_press.connect ((icon_pos, event) =>
+            // search and replace -> search
+            if (get_mode () == Mode.SEARCH_AND_REPLACE)
             {
-                // options menu
-                if (icon_pos == EntryIconPosition.PRIMARY)
-                    menu.popup (null, null, null, event.button.button, event.button.time);
-            });
+                _arrow.arrow_type = ArrowType.DOWN;
+                _replace_grid.hide ();
+            }
 
-            button_arrow.clicked.connect (() =>
-            {
-                // search and replace -> search
-                if (search_and_replace_mode)
-                {
-                    arrow.arrow_type = ArrowType.DOWN;
-                    frame_replace.hide ();
-                    hbox_replace.hide ();
-                }
-
-                // search -> search and replace
-                else
-                {
-                    arrow.arrow_type = ArrowType.UP;
-                    frame_replace.show ();
-                    hbox_replace.show ();
-                }
-            });
-
-            button_close.clicked.connect (hide);
-
-            button_clear_find.clicked.connect (() => entry_find.text = "");
-            button_clear_replace.clicked.connect (() => entry_replace.text = "");
-
-            button_previous.clicked.connect (() =>
+            // search -> search and replace
+            else
             {
-                set_search_text (false);
-                return_if_fail (working_document != null);
-                working_document.search_backward ();
-            });
+                _arrow.arrow_type = ArrowType.UP;
+                _replace_grid.show ();
+            }
+        });
 
-            button_next.clicked.connect (search_forward);
-            entry_find.activate.connect (search_forward);
+        button_close.clicked.connect (hide);
 
-            entry_find.changed.connect (() =>
-            {
-                bool sensitive = entry_find.text_length > 0;
-                button_clear_find.sensitive = sensitive;
-                button_previous.sensitive = sensitive;
-                button_next.sensitive = sensitive;
-                button_replace.sensitive = sensitive;
-                button_replace_all.sensitive = sensitive;
-
-                if (entry_find.text_length == 0)
-                {
-                    label_find_normal.hide ();
-                    label_find_error.hide ();
-                    clear_search ();
-                }
-                else if (entry_find.text_length >= min_nb_chars_for_incremental_search)
-                    set_search_text ();
-            });
-
-            entry_replace.changed.connect (() =>
-            {
-                button_clear_replace.sensitive = entry_replace.text_length > 0;
-            });
+        button_clear_find.clicked.connect (() => _entry_find.text = "");
+        button_clear_replace.clicked.connect (() => _entry_replace.text = "");
 
-            check_case_sensitive.toggled.connect (() => set_search_text ());
-            check_entire_word.toggled.connect (() => set_search_text ());
+        button_previous.clicked.connect (() =>
+        {
+            set_search_text (false);
+            return_if_fail (_working_document != null);
+            _working_document.search_backward ();
+        });
 
-            button_replace.clicked.connect (replace);
-            entry_replace.activate.connect (replace);
+        button_next.clicked.connect (search_forward);
+        _entry_find.activate.connect (search_forward);
 
-            button_replace_all.clicked.connect (() =>
+        _entry_find.changed.connect (() =>
+        {
+            bool sensitive = _entry_find.text_length > 0;
+            button_clear_find.sensitive = sensitive;
+            button_previous.sensitive = sensitive;
+            button_next.sensitive = sensitive;
+            button_replace.sensitive = sensitive;
+            button_replace_all.sensitive = sensitive;
+
+            if (_entry_find.text_length == 0)
             {
-                return_if_fail (entry_find.text_length != 0);
+                _label_find_normal.hide ();
+                _label_find_error.hide ();
+                clear_search ();
+            }
+            else if (_entry_find.text_length >= min_nb_chars_for_incremental_search)
                 set_search_text ();
-                working_document.replace_all (entry_replace.text);
-            });
+        });
+
+        _entry_replace.changed.connect (() =>
+        {
+            button_clear_replace.sensitive = _entry_replace.text_length > 0;
+        });
+
+        _check_case_sensitive.toggled.connect (() => set_search_text ());
+        _check_entire_word.toggled.connect (() => set_search_text ());
+
+        button_replace.clicked.connect (replace);
+        _entry_replace.activate.connect (replace);
+
+        button_replace_all.clicked.connect (() =>
+        {
+            return_if_fail (_entry_find.text_length != 0);
+            set_search_text ();
+            _working_document.replace_all (_entry_replace.text);
+        });
 
-            entry_find.key_press_event.connect ((event) =>
+        _entry_find.key_press_event.connect ((event) =>
+        {
+            // See GDK_KEY_* in gdk/gdkkeysyms.h (not available in Vala)
+            switch (event.keyval)
             {
-                // See GDK_KEY_* in gdk/gdkkeysyms.h (not available in Vala)
-                switch (event.keyval)
-                {
-                    case 0xff09:    // GDK_KEY_Tab
-                        // TAB in find => go to replace
-                        show_search_and_replace ();
-                        entry_replace.grab_focus ();
-                        return true;
-
-                    case 0xff1b:    // GDK_KEY_Escape
-                        // Escape in find => select text and hide search
-                        select_selected_search_text ();
-                        hide ();
-                        return true;
-
-                    default:
-                        // propagate the event further
-                        return false;
-                }
-            });
-        }
-        catch (Error e)
+                case 0xff09:    // GDK_KEY_Tab
+                    // TAB in find => go to replace
+                    show_search_and_replace ();
+                    _entry_replace.grab_focus ();
+                    return true;
+
+                case 0xff1b:    // GDK_KEY_Escape
+                    // Escape in find => select text and hide search
+                    select_selected_search_text ();
+                    hide ();
+                    return true;
+
+                default:
+                    // propagate the event further
+                    return false;
+            }
+        });
+
+        _main_grid.hide ();
+    }
+
+    /* Find entry, with two labels for displaying some information */
+    private Frame get_find_entry ()
+    {
+        Frame frame_find = new Frame (null);
+        frame_find.shadow_type = ShadowType.IN;
+        frame_find.width_request = 350;
+
+        Grid grid_find = new Grid ();
+        grid_find.set_orientation (Orientation.HORIZONTAL);
+
+        /* Entry */
+        _entry_find = new Entry ();
+        _entry_find.set_has_frame (false);
+        _entry_find.primary_icon_stock = Stock.PROPERTIES;
+        _entry_find.primary_icon_activatable = true;
+        _entry_find.set_tooltip_text (_("Search for"));
+        _entry_find.can_focus = true;
+        grid_find.add (_entry_find);
+
+        /* "Normal" information (number of matches, etc.) */
+        EventBox eventbox_normal = new EventBox ();
+        _label_find_normal = new Label (null);
+
+        Pango.AttrList attributes = new Pango.AttrList ();
+
+        // foreground: light gray (#AAAA AAAA AAAA)
+        Pango.Attribute attr_foreground = Pango.attr_foreground_new (43690, 43690, 43690);
+        attributes.insert ((owned) attr_foreground);
+
+        // background: white
+        Pango.Attribute attr_background = Pango.attr_background_new (65535, 65535, 65535);
+        attributes.insert ((owned) attr_background);
+
+        _label_find_normal.set_attributes (attributes);
+        eventbox_normal.add (_label_find_normal);
+        grid_find.add (eventbox_normal);
+
+        /* "Error" information (text not found, etc.) */
+        EventBox eventbox_error = new EventBox ();
+        _label_find_error = new Label (null);
+
+        attributes = new Pango.AttrList ();
+
+        // foreground: white
+        attr_foreground = Pango.attr_foreground_new (65535, 65535, 65535);
+        attributes.insert ((owned) attr_foreground);
+
+        // background: red (#CCCC 0000 0000)
+        attr_background = Pango.attr_background_new (52428, 0, 0);
+        attributes.insert ((owned) attr_background);
+
+        _label_find_error.set_attributes (attributes);
+        eventbox_error.add (_label_find_error);
+        grid_find.add (eventbox_error);
+
+        // eventboxes style
+        Gdk.Color white;
+        Gdk.Color.parse ("white", out white);
+        eventbox_normal.modify_bg (StateType.NORMAL, white);
+        eventbox_error.modify_bg (StateType.NORMAL, white);
+
+        frame_find.add (grid_find);
+
+        /* Options menu */
+        Menu menu = new Menu ();
+        _check_case_sensitive = new CheckMenuItem.with_label (_("Case sensitive"));
+        _check_entire_word = new CheckMenuItem.with_label (_("Entire words only"));
+        menu.append (_check_case_sensitive);
+        menu.append (_check_entire_word);
+        menu.show_all ();
+
+        _entry_find.icon_press.connect ((icon_pos, event) =>
         {
-            warning ("Search and replace: %s", e.message);
-            Label label = new Label (e.message);
-            label.set_line_wrap (true);
-            widget = label;
-        }
+            // options menu
+            if (icon_pos == EntryIconPosition.PRIMARY)
+                menu.popup (null, null, null, event.button.button, event.button.time);
+        });
 
-        widget.hide ();
+        return frame_find;
+    }
+
+    private Button get_button (string stock_id)
+    {
+        Button button = new Button ();
+        Image image = new Image.from_stock (stock_id, IconSize.MENU);
+        button.add (image);
+        return button;
     }
 
     public Widget get_widget ()
     {
-        return widget;
+        return _main_grid;
     }
 
     public void show_search ()
     {
-        arrow.arrow_type = ArrowType.DOWN;
+        _arrow.arrow_type = ArrowType.DOWN;
         show ();
-        frame_replace.hide ();
-        hbox_replace.hide ();
+        _replace_grid.hide ();
     }
 
     public void show_search_and_replace ()
     {
-        arrow.arrow_type = ArrowType.UP;
+        _arrow.arrow_type = ArrowType.UP;
         show ();
     }
 
     private void show ()
     {
-        return_if_fail (main_window.active_tab != null);
+        return_if_fail (_main_window.active_tab != null);
 
-        widget.show_all ();
-        label_find_normal.hide ();
-        label_find_error.hide ();
-        entry_find.grab_focus ();
+        _main_grid.show_all ();
+        _label_find_normal.hide ();
+        _label_find_error.hide ();
+        _entry_find.grab_focus ();
         set_replace_sensitivity ();
 
         // if text is selected in the active document, and if this text contains no \n,
         // search this text
-        Document doc = main_window.active_document;
+        Document doc = _main_window.active_document;
         if (doc.get_selection_type () == SelectionType.ONE_LINE)
         {
             TextIter start, end;
             doc.get_selection_bounds (out start, out end);
-            entry_find.text = doc.get_text (start, end, false);
+            _entry_find.text = doc.get_text (start, end, false);
         }
 
-        main_window.notify["active-document"].connect (active_document_changed);
+        _main_window.notify["active-document"].connect (active_document_changed);
     }
 
     public void hide ()
     {
-        widget.hide ();
-        if (working_document != null)
+        _main_grid.hide ();
+        if (_working_document != null)
             clear_search ();
 
-        if (main_window.active_view != null)
-            main_window.active_view.grab_focus ();
+        if (_main_window.active_view != null)
+            _main_window.active_view.grab_focus ();
 
-        main_window.notify["active-document"].disconnect (active_document_changed);
+        _main_window.notify["active-document"].disconnect (active_document_changed);
     }
 
     private void set_label_text (string text, bool error)
     {
         if (error)
         {
-            label_find_error.set_text (text);
-            label_find_error.show ();
-            label_find_normal.hide ();
+            _label_find_error.set_text (text);
+            _label_find_error.show ();
+            _label_find_normal.hide ();
         }
         else
         {
-            label_find_normal.set_text (text);
-            label_find_normal.show ();
-            label_find_error.hide ();
+            _label_find_normal.set_text (text);
+            _label_find_normal.show ();
+            _label_find_error.hide ();
         }
     }
 
     private void set_search_text (bool select = true)
     {
-        return_if_fail (main_window.active_document != null);
+        return_if_fail (_main_window.active_document != null);
 
-        if (entry_find.text_length == 0)
+        if (_entry_find.text_length == 0)
             return;
 
-        if (main_window.active_document != working_document)
+        if (_main_window.active_document != _working_document)
         {
-            if (working_document != null)
+            if (_working_document != null)
                 clear_search ();
 
-            working_document = main_window.active_document;
-            working_document.search_info_updated.connect (on_search_info_updated);
+            _working_document = _main_window.active_document;
+            _working_document.search_info_updated.connect (on_search_info_updated);
         }
 
         uint nb_matches, num_match;
-        working_document.set_search_text (entry_find.text, case_sensitive, entire_word,
+        _working_document.set_search_text (_entry_find.text, case_sensitive, entire_word,
             out nb_matches, out num_match, select);
 
         on_search_info_updated (nb_matches != 0, nb_matches, num_match);
@@ -392,17 +478,17 @@ public class SearchAndReplace : GLib.Object
 
     private void select_selected_search_text ()
     {
-        return_if_fail (main_window.active_document != null);
+        return_if_fail (_main_window.active_document != null);
 
-        if (working_document != null);
-            working_document.select_selected_search_text ();
+        if (_working_document != null);
+            _working_document.select_selected_search_text ();
     }
 
     private void search_forward ()
     {
         set_search_text (false);
-        return_if_fail (working_document != null);
-        working_document.search_forward ();
+        return_if_fail (_working_document != null);
+        _working_document.search_forward ();
     }
 
     private void on_search_info_updated (bool selected, uint nb_matches, uint num_match)
@@ -419,32 +505,31 @@ public class SearchAndReplace : GLib.Object
 
     private void clear_search ()
     {
-        if (working_document != null)
+        if (_working_document != null)
         {
-            working_document.clear_search ();
-            working_document.search_info_updated.disconnect (on_search_info_updated);
-            working_document = null;
+            _working_document.clear_search ();
+            _working_document.search_info_updated.disconnect (on_search_info_updated);
+            _working_document = null;
         }
     }
 
     private void active_document_changed ()
     {
-        label_find_normal.hide ();
-        label_find_error.hide ();
+        _label_find_normal.hide ();
+        _label_find_error.hide ();
         set_replace_sensitivity ();
     }
 
     private void set_replace_sensitivity ()
     {
-        bool readonly = main_window.active_document.readonly;
-        frame_replace.set_sensitive (! readonly);
-        hbox_replace.set_sensitive (! readonly);
+        bool readonly = _main_window.active_document.readonly;
+        _replace_grid.set_sensitive (! readonly);
     }
 
     private void replace ()
     {
-        return_if_fail (entry_find.text_length != 0);
+        return_if_fail (_entry_find.text_length != 0);
         set_search_text ();
-        working_document.replace (entry_replace.text);
+        _working_document.replace (_entry_replace.text);
     }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]