Easily empty GtkContainer/GtkWindow

This function provides an easy way to empty a GtkWindow/GtkContainer. I use it when i need to refresh a part of my app by replacing some widgets with others. It can destroy or preserve the child widgets depending on your needs.

Sample usage

<?php

# empty my container and destroy its children
empty_widget($myContainer);
# empty widget and preserve children
empty_widget($myContainer,false);
# use it as a callback on a button click
$myButton->connect_simple('clicked','empty_widget',$myContainer);
?>

Note: This is a rewrite for php5/php-gtk2 of the same function i've already poster for php4/php-gtk1. Hope this will be helpful to you.

function empty_widget

<?php
/**
* remove and optionally destroy all child from a widget
* @param GtkWidget    &$call_widget is the calling widget or if
*                     is the only Gtkcontainer is the one to empty
* @param GtkContainer [&$target_widget] is the gtkcontainer to empty
*                     if only one Gtkwidget is passed and is a
*                     Gtkcontainer it would be this one by default
* @param bool         $destroy_childs default is TRUE
*                     so the childs widget will be destroy by default,
*                     pass FALSE as last argument to preserve childs widgets
* @licence LGPL
**/
function empty_widget($call_widget) {
 
# If we receive only one arguments it must be the target widget
 
$n = func_num_args();
  if(
$n>1){
    if(!
is_bool($destroy_childs = func_get_arg($n-1)))
      unset(
$destroy_childs);
    }
  switch(
$n){
    case
1:
      if(! (
$call_widget instanceof GtkContainer || $call_widget instanceof GtkWindow ) )
        return
FALSE;
     
$widget = $call_widget;
     
$destroy_childs = TRUE;
      break;
    case
2:
      if(
is_bool($destroy_childs = func_get_arg(1)) ){
       
$widget = func_get_arg(0);
                if(! (
$widget instanceof GtkContainer || $widget instanceof GtkWindow ) )
          return
FALSE;
      }elseif(
$destroy_childs instanceof GtkContainer || $destroy_childs instanceof GtkWindow ){
       
$widget = $destroy_childs;
       
$destroy_childs = TRUE;
      }else{
        return
FALSE;
      }
      break;
    case
3:
      if(!
is_bool($destroy_childs = func_get_arg(2)) )
                return
false;
           
$widget = func_get_arg(1);
            if(! (
$widget instanceof GtkContainer || $widget instanceof GtkWindow ) )
                return
false;
            break;
    default:
      return
FALSE;
  }
   
   
$childs = $widget->get_children();
    if(
count($childs) ){
        foreach(
$childs as $c ){
           
$widget->remove($c);
            if(
$destroy_childs )
               
$c->destroy();
        }
    return
TRUE;
  }else{
    return
FALSE;
  }
}
?>