#! /usr/bin/env python
''' Program to test the gi gtk interface.

'''

from gi.repository import Gtk, Gdk, GObject, Pango

def quit_callback(widget):
    """Callback for file->quit menu item.
    
    """

    Gtk.main_quit()

class dialogTest:
    ''' A class to test dialog.

    '''
    
    ui = '''<ui>
    <menubar name="MenuBar">
      <menu action="File">
        <menuitem action="Quit"/>
      </menu>
    </menubar>
    <popup name="Popup">
    </popup>
    <toolbar name="Toolbar">
      <toolitem action="Dialog"/>
    </toolbar>
    </ui>'''    

    def __init__(self):
        """ Initialize a new dialogTest instance.

        """
        
        self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        self.window.set_default_size(200, 200)
        self.window.show()

        box = Gtk.VBox(False, 0)
        box.show()
        self.window.add(box)

        uimanager = Gtk.UIManager()
        accelgroup = uimanager.get_accel_group()
        self.window.add_accel_group(accelgroup)
        actiongroup = Gtk.ActionGroup('UIManagerGiTest')
        actiongroup.add_actions([('Quit', Gtk.STOCK_QUIT, '_Quit', None,
                                  'Quit the Program', quit_callback),
                                 ('File', None, '_File'),
                                 ('Dialog', Gtk.STOCK_CONNECT, '_Dialog', None,
                                  'Open Dialog', self.dialog_callback)])
        uimanager.insert_action_group(actiongroup, 0)
        uimanager.add_ui_from_string(self.ui)
        menubar = uimanager.get_widget('/MenuBar')
        box.pack_start(menubar, False, True, 0)
        toolbar = uimanager.get_widget('/Toolbar')
        for n in range(0, toolbar.get_n_items()):
            toolbar.get_nth_item(n).set_is_important(True)
        box.pack_start(toolbar, False, True, 0)

    def dialog_callback(self, widget):
        ''' Callback when the Dialog button is hit.
            A dialog is presented.
        
        '''

        # Create a dialog for when the Dialog button is hit.
        
        dialog_label = Gtk.Label(label='Whatever')
        test_dialog = Gtk.Dialog('Whatever',
                                 self.window,
                                 Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                                 (Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT,
                                  Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT))

        # Probably don't have to use a table but the original code used a table with multiple rows so use one here also.
        
        table = Gtk.Table(1, 2, False)
        test_dialog.vbox.pack_start(dialog_label, True, True, 0)
        dialog_label.show()
        test_dialog.vbox.pack_start(table, True, True, 0)

        # Make a selection list big enough to require scrolling.
        
        liststore = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING)
        for ii in range(500):
            liststore.append(['%d' % ii, 'row %d, col 1' % ii, 'row %d, col 2' % ii, ''])

        # Make a combo box and connect the liststore to it for selection.
            
        name = Gtk.ComboBox.new_with_model_and_entry(liststore)
        name.set_entry_text_column(0)
        name.show()

        label = Gtk.Label(label='Name')
        label.set_alignment(0, 0)
        label.show()

        table.attach(label, 0, 1, 0, 1)
        table.attach(name, 1, 2, 0, 1)

        table.show()

        # Run the dialog and get the response.
        # Note: the first time the selection list is expanded, it is scrollable, each subsequent time, it is not scrollable.
        #       If the dialog is closed and reopened it will be scrollable the first time again.
        
        response = test_dialog.run()
        if response == Gtk.ResponseType.ACCEPT:
            print('accepted')
        elif response == Gtk.ResponseType.REJECT:
            print('rejected')

        test_dialog.hide()
        
    def main(self):
        ''' The main function.

        '''
        
        Gtk.main()


# The main program starts here.
        
if __name__ == '__main__':
    test_dialog = dialogTest()
    test_dialog.main()
