
    BPc                     L    d Z ddlZddlZddlZddlmZmZ  G d d          ZdS )z
Gtk Application base classes, providing a way to load a GtkBuilder
with a specific glade/ui file conaining windows, and building
a usable pythonic interface from them.
    N)GtkGLibc                       e Zd ZdZed             Zed             Zed             Zed             Zed             Z	ed             Z
dd
Zd Zd Zd Zd Zd Zd Zd Zd ZdS )GtkAppa  
    This wraps gtk builder and allows for some extra functionality with
    windows, especially the management of gtk main loops.

    Args:
        start_loop (bool, optional): If set to true will start a new gtk main loop.
            Defaults to False.
        start_gui (bool, optional): Used as local propertes if unset and passed to
                primary window when loaded. Defaults to True.
    c                 8    | j                             dd          S )zFolder prefix added to ui_dirprefix kwargsgetselfs    //usr/share/inkscape/extensions/inkex/gui/app.pyr   zGtkApp.prefix*   s     {x,,,    c                 8    | j                             dg           S )z&Returns a list of windows for this appwindowsr
   r   s    r   r   zGtkApp.windows/   s     {y"---r   c                 8    | j                             dd          S )z!This is often the local directoryui_dirz./r
   r   s    r   r   zGtkApp.ui_dir4   s     {x...r   c                 8    | j                             dd          S )z-If a single file is used for multiple windowsui_fileNr
   r   s    r   r   zGtkApp.ui_file9   s     {y$///r   c                 X    	 | j         d         S # t          $ r t          d          w xY w)zSet this variable in your classapp_namez8App name is not set, pass in or set 'app_name' in class.)r   KeyErrorNotImplementedErrorr   s    r   r   zGtkApp.app_name>   sD    	;z** 	 	 	%J  	s    )c                     | j         S )zReturn the primary window)_primaryr   s    r   windowzGtkApp.windowH   s     }r   FTc                     || _         i | _        i | _        d| _        t	          j                    | _        |r|                                  |r|                                  dS dS )zCreates a new GtkApp.N)	r   _loaded_initialr   r   
main_depth	main_loopinit_guirun)r   
start_loop	start_guir   s       r   __init__zGtkApp.__init__M   sg    **  	MMOOO 	HHJJJJJ	 	r   c                 N   t          j                    d         st          d          	 t          j        t          j        t          j                   t          j                     n$# t          $ r t          j	        d           Y nw xY wt          j
        d| j                   dS )z@Run the gtk mainloop with ctrl+C and keyboard interupt additionsr   z9Gtk failed to start. Make sure $DISPLAY variable is set.
zUser Interputedz
Exiting %sN)r   
init_checkRuntimeErrorsignalSIGINTSIG_DFLmainKeyboardInterruptlogginginfodebugr   r   s    r   r$   z
GtkApp.run]   s    ~" 	O  	,M&-888HJJJJ  	, 	, 	,L*+++++	,lDM22222s   <A' 'BBc                 8   t           j                            | j        | j        | d          t           j                            | j        | j        | j         d          g}|D ]%}t           j                            |          r|c S &t          d|           )z9Load any given gtk builder file from a standard location.z.uizGtk Builder file is missing: )ospathjoinr   r   r   isfileFileNotFoundError)r   r   pathsr5   s       r   get_ui_filezGtkApp.get_ui_filek   s     GLLdkf>>>BBGLLdkdl3G3G3GHH
  	 	Dw~~d##  G G GHHHr   c                 \   | j         r~| j         D ]-}|}t          j        d|j                   || j        |j        <   .| j                                        D ]/}|j        r&| j        s|                     |j                  | _        0| j         r| j        st          d| j
         d          dS )z3Initalise all of our windows and load their signalszAdding window %s to GtkAppzNo primary window found for 'z' app.N)r   r0   r2   namer    valuesprimaryr   load_windowr   r   )r   clsr   s      r   r#   zGtkApp.init_guiv   s    < 	F| 4 4:FKHHH-3fk**-..00 F F> F= F(,(8(8(E(E| 	R4= 	RP4=PPPQQQ	R 	Rr   c                 J    |                      |          } |j        |i | |S )z0Load a specific window from our group of windows)proto_windowinit)r   r<   argsr   r   s        r   r?   zGtkApp.load_window   s1    ""4((T$V$$$r   c                 n    |                      |          } |j        di | |                                S )z)Load a child window as a widget container )rB   load_widgetsextract)r   r<   r   r   s       r   load_window_extractzGtkApp.load_window_extract   s>    ""4((%%f%%%~~r   c                     t          j        d|| j                   || j        v r' | j        |         |           }|| j        |j        <   |S t          d| d          )z
        Loads a glade window as a window without initialisation, used for
        extracting widgets from windows without loading them as windows.
        zLoading '%s' from %szCan't load window 'z', class not found.)r0   r2   r    r   widr   )r   r<   r   s      r   rB   zGtkApp.proto_window   sk    
 	,dDMBBB4=  (T]4(..F'-DL$MFTFFFGGGr   c                    |j         | j        v r | j                            |j                    nt          j        d|j                   t          j        d| j                   | j        s|                                  dS dS )z7Remove the window from the list and exit if none remainzMissing window '%s' on exit.zLoaded windows: %sN)rK   r   popr0   warningr<   r2   exit)r   r   s     r   remove_windowzGtkApp.remove_window   s{    :%%LVZ((((O:FKHHH*DL999| 	IIKKKKK	 	r   c                     | j         t          j                    k     r=| j        r| j        j        nd}t          j        d|           t          j                     dS )z8Exit our gtk application and kill gtk main if we have toprogramzQuit '%s' Main Loop.r   )	r"   r   r!   r   r<   r0   r2   r   	main_quit)r   tags     r   rO   zGtkApp.exit   sP    >DO----(,D$-$$9CM0#666MOOOqr   N)FT)__name__
__module____qualname____doc__propertyr   r   r   r   r   r   r'   r$   r:   r#   r?   rI   rB   rP   rO   rF   r   r   r   r      sU       	 	 - - X- . . X. / / X/ 0 0 X0   X   X    3 3 3	I 	I 	IR R R       H H H      r   r   )rX   r4   r+   r0   gi.repositoryr   r   r   rF   r   r   <module>r[      s   $ 
 
			   # # # # # # # #R R R R R R R R R Rr   