GRAphical Programming for Eiffel index contents

Appendix G. Splitter


Ideology

Have you ever seen a Splitter dividing the window into two parts? You can find it in the Microsoft Word, Visual Basic and in a dozen of other good programs.


Figure G.1 Two splitters.

The idea of splitter is to divide (split) a window onto two independent parts. You can ask: why not to make two windows? The answer is simple - in any cases you need in simultaneously viewing two parts of a document. It can be useful to see two pieces of the same document. In case of two independent windows you must perform much more actions to make one tile bigger and the second smaller. In case of splitter you can simply drag the split line up'n'down


Implementation

It's a very simple widget. The implementation has only two classes. The main idea is to join two windows into one object of type SPLITTER. And use this object instead of traditional WINDOW.


Short form of class SPLITTER

class SPLITTER
   inherit
     CONTROL
   creation
     make 
   feature
     make (typ : BOOLEAN; pos : REAL; fixed : INTEGER)
       -- To create the splitter you need to specify the
       -- following:
       -- type - false - HORIZONTAL, true - VERTICAL
       -- pos - relative position of splitter inside parent [0,1]
       -- fixed - description of siblings -- 0 - proportional items
       -- 1 - first item fixed
       -- 2 - last item fixed
 
     set_siblings (first, second : TILE)
       -- set two windows for up/down or left/right subwindows
       require
         not_void_children : first /= Void AND THEN second /= Void

     set_margins (left, top, right, bottom : INTEGER)
       -- set margins of splitter window

end -- class SPLITTER

Short form of class SPLITTED_DESKTOP_APPLICATION

class SPLITTED_DESKTOP_APPLICATION
   inherit
     APPLICATION
   creation
     make, make_at_pos

   feature
     make_user_window : TILE
       -- This feature should be redefined by application programmers
       -- in order to create "the bottom" of the splitted desktop.

     get_user_window : TILE
       -- return bottom window

end -- class SPLITTED_DESKTOP_APPLICATION

Example

In this example, as you will see, there are almost no differences in using splitter instead of using class WINDOW.

class DEMO_APPLICATION
   inherit
     SPLITTED_DESKTOP_APPLICATION

   creation
     make

   feature

     make_user_window : TILE is
         -- feature, creating down window for splitter
         -- create Simple window with text
       local
         wp : WINDOW_WITH_TEXT
       do
         !!wp.make("This is a test window with simple text string")
         Result := wp
       end;

     make_menu : MENU is
       do
         ...
       end;

     on_command ( cmd : INTEGER ) : INTEGER is
         -- This is a switch which responds to menu commands.
       local
         desktop_element : WINDOW_WITH_PICTURE;
         fd : FILE_DIALOG
         font_d : FONT_DIALOG
         i : INTEGER
         wp : WINDOW_WITH_TEXT
       do
         Result := processed;
         inspect cmd
           ...
           when IDM_NEW then
             ... 
           when IDM_SELECT_FONT then
             !!font_d.make
             if Current.execute(font_d) /= 0 then
               wp ?= get_user_window
               if wp /= Void then
                 wp.set_font(font_d.selected_font)
                 wp.repaint
               end
             end
           ... 
           else
             Result := not_processed;
         end; -- inspect
       end; -- on_command
     ...
end -- DEMO_APPLICATION

© Object Tools -- info@object-tools.com -- December 1999