Xfce Wiki

Sub domains
 

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
windowgrid [2011/04/05 01:00] – created killermoehrewindowgrid [2012/08/05 09:50] – got the panel spacing right… and help is available, too… oh, and argparse is used killermoehre
Line 3: Line 3:
  
 TODO: TODO:
-  * wrote the help 
   * find some way to store the window state (int; 1, 2, or 3) with the window   * find some way to store the window state (int; 1, 2, or 3) with the window
   * hope for fixing of [[https://bugzilla.xfce.org/show_bug.cgi?id=4178|this]] bug   * hope for fixing of [[https://bugzilla.xfce.org/show_bug.cgi?id=4178|this]] bug
  
-If there are questions, please ask.+If there are questions, please ask (and help).
  
 ---- ----
  
-    #!/usr/bin/python2 +#!/usr/bin/python2 
-    # -*- coding: utf-8 -*-+# -*- coding: utf-8 -*-
  
-    import sys +import sys 
-    import os +import argparse 
-    import wnck +from gtk import gdk 
-    from Xlib import display +import wnck
-    from xfce4 import xfconf+
  
-    PACKAGE = "window_grid"+PACKAGE = "window_grid"
  
-    def _help_(): +def argument_parser(): 
-        print(u'Help:') +    """ 
-        print(u'Lorem ipsum dolores…') +    The argument parser. Takes only one number from the cmd line in the range of 
-        return(0)+    0 to 10. 
 +    """ 
 +    parser = argparse.ArgumentParser(description='Tile the current window.') 
 +    parser.add_argument('position'
 +        metavar='N', 
 +        type=int, 
 +        help='an integer describing the position', 
 +        choices = xrange(10
 +        
 +    return parser.parse_args()
  
-    def active_window(): +def active_window(): 
-        # returns the default screen, but it fails when not called from X +    """ 
-        # TODO: connect to the dbus-session-bus to get the right environment +    Returns the default screen, but it fails uncatchable when not called from X. 
-        # (like $DISPLAY) +    """ 
-        screen = wnck.screen_get_default() +    screen = wnck.screen_get_default() 
-        screen.force_update() +    screen.force_update() 
-        return(screen.get_active_window())+    return(screen.get_active_window())
  
-    def get_resolution(orientation): +def get_geometry(): 
-        Accept only 'width' and 'height' for the specific resolution +    just a shorting for wnck.Window.get_geometry(active_window()) 
-        return display.Display.screen(display.Display())[orientation + +    return(active_window().get_geometry())
-           '_in_pixels']+
  
-    def get_geometry(): +def window_state(head, sub = int(1)): 
-        # just a shorting for wnck.Window.get_geometry(active_window()) +    """ 
-        return(active_window().get_geometry()) +    Returns the upper left edge and the dimensions of the current window. 
- +    The HeadMap showing the screen; 4, 5 and 6 are vertical full, 
-    def window_state(head, sub = int(1)): +                                   2, 5 and 8 are horizontal full. 
-        # The HeadMap showing the screen; 4, 5 and 6 are vertical full, +    ┌─────────────────┬─────────────────┐ 
-        #                                 2, 5 and 8 are horizontal full. +    │                 ┆                 │ 
-        # ┌─────────────────┬─────────────────┐ +    │        7        8        9        │ 
-        # │                 ┆                 │ +    │                 ┆                 │ 
-        # │        7        8        9        │ +    ├┄┄┄┄┄┄┄╴4╶┄┄┄┄┄┄╴5╶┄┄┄┄┄┄╴6╶┄┄┄┄┄┄┄┤ 
-        # │                 ┆                 │ +    │                 ┆                 │ 
-        # ├┄┄┄┄┄┄┄╴4╶┄┄┄┄┄┄╴5╶┄┄┄┄┄┄╴6╶┄┄┄┄┄┄┄┤ +    │        1        2        3        │ 
-        # │                 ┆                 │ +    │                 ┆                 │ 
-        # │        1        2        3        │ +    └─────────────────┴─────────────────┘ 
-        # │                 ┆                 │ +    The SubMap showing the widths 
-        # └─────────────────┴─────────────────┘ +    ┌─────────────────┐ 
-        # The SubMap showing the widths +    │                 │ 
-        # ┌─────────────────┐ +    │        1        │ 
-        # │                 │ +    │                 │ 
-        # │        1        │ +    └─────────────────┘ 
-        # │                 │ +    ┌───────────────────────┐ 
-        # └─────────────────┘ +    │                       │ 
-        # ┌───────────────────────┐ +    │                     │ 
-        # │                       │ +    │                       │ 
-        # │                     │ +    └───────────────────────┘ 
-        # │                       │ +    ┌───────────┐ 
-        # └───────────────────────┘ +    │           │ 
-        # ┌───────────┐ +    │         │ 
-        # │           │ +    │           │ 
-        # │         │ +    └───────────┘ 
-        # │           │ +    """ 
-        # └───────────┘ +    head = int(head) 
-        head = int(head) +    root_win gdk.get_default_root_window() 
-        x_res get_resolution(u'width'+    win_property gdk.atom_intern("_NET_WORKAREA"
-        y_res get_resolution(u'height'+    win_property_array = root_win.property_get(win_property)[2] 
-        x_sub_map = {1: x_res / 2, 2: x_res / 3 * 2, 3: x_res / 3} +    x_offset = win_property_array[0] 
-        if (head % 3) == 1: +    y_offset = win_property_array[1] 
-            x = 0 +    x_wa = win_property_array[2] 
-            width = x_sub_map[sub] +    y_wa = win_property_array[3] 
-        elif (head % 3) == 2: +    x_sub_map = {1: x_wa / 2, 2: x_wa / 3 * 2, 3: x_wa / 3} 
-            if sub == 1: +    if (head % 3) == 1: 
-                x = +        x = x_offset 
-                width = x_res +        width = x_sub_map[sub] 
-            else: +    elif (head % 3) == 2: 
-                x = x_res / 3 +        if sub == 1: 
-                width = x_res / 3+            x = x_offset 
 +            width = x_wa
         else:         else:
-            x = x_res - x_sub_map[sub] +            x = x_wa / 3 + x_offset 
-            width = x_sub_map[sub] +            width = x_wa / 3 
-        if (7 <= head <= 9): +    else: 
-            y = 0 +        x = x_wa - x_sub_map[sub] + x_offset 
-            height = y_res / 2 +        width = x_sub_map[sub] 
-        elif (4 <= head <= 6): +    if (7 <= head <= 9): 
-            y = 0 +        y = y_offset 
-            height = y_res +        height = y_wa / 2 
-        else: +    elif (4 <= head <= 6): 
-            y = y_res / 2 +        y = y_offset 
-            height = y_res / 2 +        height = y_wa 
-        return(int(x), int(y), int(width), int(height))+    else: 
 +        y = y_wa / 2 + y_offset 
 +        height = y_wa / 2 
 +    return(int(x), int(y), int(width), int(height))
  
-    def set_geometry(geo_list): +def set_geometry(geo_list): 
-        # DOC: http://library.gnome.org/devel/libwnck/stable/WnckWindow.html +    """ 
-        #      #wnck-window-set-geometry +    DOC: http://library.gnome.org/devel/libwnck/stable/WnckWindow.html#wnck-window-set-geometry 
-        # gravity:  0   - current gravity +    gravity: 0   - current gravity 
-        #           1   - top left +             1   - top left 
-        #           2   - top center                    +             2   - top center               
-        #           3   - top right               ┏━╾─┴─╼━┓ +             3   - top right           ┏━╾─┴─╼━┓ 
-        #           4   - center left             ╿       ╿ +             4   - center left         ╿       ╿ 
-        #           5   - center center         4 ┤     ├ 6 +             5   - center center      4┤     ├6 
-        #           6   - center right            ╽       ╽ +             6   - center right        ╽       ╽ 
-        #           7   - bottom left             ┗━╾─┬─╼━┛ +             7   - bottom left         ┗━╾─┬─╼━┛ 
-        #           8   - bottom center                 +             8   - bottom center            
-        #           9   - bottom right +             9   - bottom right 
-        #           10  - static (top left) +             10  - static (top left) 
-        # only 10 does what I want… all the other gravities (even 1) don't set +    Only 10 does what I want… all the other gravities (even 1) don't set 
-        # the windows right +    the windows right (Kind of bug? Maybe. Does it bother me? No, not really). 
-        active_window().unmaximize_horizontally() +    """ 
-        active_window().unmaximize_vertically() +    window = active_window() 
-        active_window().set_geometry(10, 15, geo_list[0], geo_list[1], geo_list[2], geo_list[3]) +    window.unmaximize_horizontally() 
-        return 0 +    window.unmaximize_vertically() 
-     +    window.set_geometry(10, 15, geo_list[0], geo_list[1], geo_list[2], geo_list[3]) 
-    def main(): +    return 0 
-        if len(sys.argv> 2: + 
-            print('Too many arguments! Only one allowed!'+def main(): 
-            _help_() +    args = argument_parser() 
-            sys.exit(1) +    if args.position == 0: 
-        if len(sys.argv) < 2: +        print get_geometry() 
-            print('Too few arguments! At least one required!'+        sys.exit(0) 
-            _help_() +    #print('%s' % active_window().get_data('submap')) 
-            sys.exit(1) +    if active_window().get_data('submap') == '1': 
-        try: +        #print('Known state! Switch to SubMap 2!') 
-            cmd_arg = int(str(sys.argv[1])) +        set_geometry(window_state(args.position, 2)) 
-        except ValueError: +        active_window().set_data('submap', 2) 
-            print('Wrong argument!'+    elif active_window().get_data('submap') == '2': 
-            _help_() +        if (args.position % 3) == 2: 
-            sys.exit(1) +            #print('Known state! Switch to SubMap 1!') 
-        if cmd_arg == 0: +            set_geometry(window_state(args.position, 1))
-            print get_geometry() +
-            sys.exit(0) +
-        print('%s' % active_window().get_data('submap')) +
-        if active_window().get_data('submap') == '1': +
-            print('Known state! Switch to SubMap 2!') +
-            set_geometry(window_state(cmd_arg, 2)) +
-            active_window().set_data('submap', 2) +
-        elif active_window().get_data('submap') == '2': +
-            if (cmd_arg % 3) == 2: +
-                print('Known state! Switch to SubMap 1!'+
-                set_geometry(window_state(cmd_arg, 1)) +
-                active_window().set_data('submap', 1+
-            else: +
-                print('Known state! Switch to SubMap 3!') +
-                set_geometry(window_state(cmd_arg, 3)) +
-                active_window().set_data('submap', 3) +
-        elif active_window().get_data('submap') == '3': +
-            print('Known state! Switch to SubMap 1!') +
-            set_geometry(window_state(cmd_arg, 1))+
             active_window().set_data('submap', 1)             active_window().set_data('submap', 1)
         else:         else:
-            set_geometry(window_state(cmd_arg1)) +            #print('Known state! Switch to SubMap 3!') 
-            active_window().set_data('submap', 1) +            set_geometry(window_state(args.position3)) 
-        print('%s' % active_window().get_data('submap')) +            active_window().set_data('submap', 3) 
-        sys.exit(0)+    elif active_window().get_data('submap') == '3': 
 +        #print('Known state! Switch to SubMap 1!') 
 +        set_geometry(window_state(args.position, 1)) 
 +        active_window().set_data('submap', 1) 
 +    else: 
 +        set_geometry(window_state(args.position, 1)) 
 +        active_window().set_data('submap', 1) 
 +        #print('%s' % active_window().get_data('submap')) 
 +    sys.exit((0))
  
-    if __name__ == '__main__': +if __name__ == '__main__': 
-        main()+    main()