require 'wx' # Return a new bitmap corresponding to the specified built-in artwork def bitmap_from_art_provider(art_id, art_client = Wx::ART_TOOLBAR, art_size = [16, 16]) bm = Wx::Bitmap.new bm.copy_from_icon(Wx::ArtProvider::get_icon(art_id, art_client, art_size)) bm end # The TreeCtrl dedicated to the management of articles class ArticleTree < Wx::TreeCtrl def initialize(parent, editor, status_bar) super(parent, :style => Wx::TR_DEFAULT_STYLE | Wx::TR_EDIT_LABELS | Wx::TR_HIDE_ROOT) @closed = false @article_number = 0 @editor = editor # set up the hidden root item @hidden_root_item = add_root "Outline" evt_tree_sel_changing self, :selection_changing evt_tree_sel_changed self, :selection_change evt_tree_begin_label_edit self, :begin_label_edit evt_tree_end_label_edit self, :end_label_edit end private def log message @editor.append_text "#{message}\n" end # Private method for handling a selection change of article def selection_changing(event) # if @label_editing # event.veto # else # event.skip # end end # Private method for handling a selection change of article def selection_change(event) if @closed # trace residual selection change after the application closure puts "selection change event after application closure !" else # trace selection change log "selection changed : #{event.old_item} => #{event.item}" end end # Private method for adding an article to the outline def add_article parent # add a new article @article_number += 1 new_article = append_item(parent, "Article \##{@article_number}") # select the new article and start the rename of its default label select_item new_article edit_label new_article end def begin_label_edit @label_editing = true log "begin label edit" end def end_label_edit @label_editing = false log "end label edit" end public # Acknowledge the closure of the application # (Required to ignore residual event on Windows platform # during the application closure) # Test case : add 2 root nodes, on the first root, add a child then close def close @closed = true end # Add a root article to the outline def add_root_article # if !@label_editing add_article(@hidden_root_item) # end end # Add a child article to the selected article def add_child_article # if !@label_editing && selection != 0 add_article(selection) # end end # Rename the selected article def rename_article edit_label selection end # Delete the selected article (after confirmation) def delete_article # delete the selected article delete selection end end # The Application Wx::App.run do frame = Wx::Frame.new(nil, :title => "Outliner", :size => [800, 600]) do # create the status bar status_bar = create_status_bar # create the toolbar toolbar = create_tool_bar(Wx::TB_HORIZONTAL | Wx::NO_BORDER | Wx::TB_FLAT | Wx::TB_TEXT) toolbar.add_tool(50, "Add Root", bitmap_from_art_provider(Wx::ART_NORMAL_FILE), "Add Root Article") toolbar.add_tool(55, "Add Child", bitmap_from_art_provider(Wx::ART_NORMAL_FILE), "Add Child Article") toolbar.add_tool(60, "Rename", bitmap_from_art_provider(Wx::ART_HELP_SETTINGS), "Rename Article") toolbar.add_tool(70, "Delete", bitmap_from_art_provider(Wx::ART_DELETE), "Delete Article") toolbar.realize splitter = Wx::SplitterWindow.new(self, :style => Wx::SP_BORDER) do set_minimum_pane_size(100) end article_editor = Wx::TextCtrl.new(splitter, :style => Wx::TE_MULTILINE) article_editor.disable article_tree = ArticleTree.new(splitter, article_editor, status_bar) # handle the 'Add Root' toolbar button evt_tool(50) { article_tree.add_root_article } # handle the 'Add Child' toolbar button evt_tool(55) { article_tree.add_child_article } # handle the 'Rename' toolbar button evt_tool(60) { article_tree.rename_article } # handle the 'Delete' toolbar button evt_tool(70) { article_tree.delete_article } # handle the closure of the application (after confirmation) evt_close do |e| # notify the article tree of the application closure article_tree.close e.skip end splitter.split_vertically(article_tree, article_editor, 200) end frame.show end puts "Outliner exited"