phpgtk__legacy/wiki1/wiki.d/Code.MenuIcon

33 lines
18 KiB
Text

version=pmwiki-1.0.5
newline=²
text=[[include:CodeSnippetsInc#TopBar#TopBarEnd]]²!! menu class with icons support²²http://jonathan.gotti.free.fr/phpgtkwikki/menuicon.png²²* [-[[#sample How to use it?]]-]²* [-[[#classcode the class code]]-]²* [-[[#changelog changelog]]-]²* [-%target=_blank%[[http://opensource.org/licenses/lgpl-license.php licence: LGPL]]-]²-----²²When creating a soft, you'll often want to have a menu like in the majority of graphical softs. It's often clear in your mind but require²more code than you want to do simple things.²So here's a class to help you to quickly design such menus and will also allow you to display nice icon in your menus.²----²²!!! [[#sample]]How to use it ?²²first of all copy the class [[#classcode code]] to a file (ie: class-menu.php) and then include it in your code. Then here is a sample of code to show you how simple it is to create menu in your application so lets see:²²[[include:Main/CodeTable]]²<?²if(strtoupper(substr(PHP_OS, 0,3)) == 'WIN')² dl('php_gtk.dll');²else² dl('php_gtk.so');²include_once('path_to_class-menu.php');²²$w = &new gtkwindow();²$w->connect('destroy-event','shutdown');²$w->connect('delete-event','shutdown');²$w->set_position('center');²²// We realize the window to get the gdkwindow needed to ²// create Xpm if we want to display icons in our menu²$w->realize();²$gdkwindow = $w->window;²²// We also create a gtkaccelgroup to manage accelkeys²// the class menu will attempt to create its own if none is given at creation time²$accelgroup = &new gtkaccelgroup();²$w->add_accel_group($accelgroup);²²// So we declare the menu and add it to the window²$menu = &new menu($gdkwindow,$accelgroup);²$w->add($menu->bar);²²// We now can add entrys to the menu²// first the file menu ²$menu->add_menu_item('file',null,'file');²// Second we want a open file option in the file menu²$menu->add_menu_item($keyid='open',$parent='file',$label='_open',² $callback='openfilefunctionname',$imgfile='img/fileopen.xpm');²//we also want a separator in the menu file²$menu->add_sep('file');²$menu->add_menu_item('quit','file','_quit','shutdown','img/exit.xpm');²$menu->add_menu_item('help',null,'help');²$menu->add_menu_item('about','help','about','about_function_name');²²$w->show_all();²gtk::main();²²function shutdown(){² gtk::main_quit();²}²?>[[include:Main/CodeTableEnd]]²²this would display a window with only a nice menu with icons and accelkey already defined.[[<<]]²[[#top top]]²----²²!!! [[#classcode]]So now here's the menu class code²check the code to find more details on how to use methods²[[include:Main/CodeTable]]²<?php²/**² Easy menu manipulation² @author Jonathan Gotti <nathan at the-ring dot homelinux dot net>² @copyright &copy; 2004 Jonathan Gotti² @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public Licence²*/²²class menu{²² var $bar;² /** the gdkwindow used to create pixmaps */² var $gdkwindow;² /** the accel group to manage accelkeys */² var $accel_group;² /** used for easy access to menus */² var $menus;² /** used for easy access to menuitems */² var $items;² /**² * constructor function² * @param gdkwindow $gdkwindow² * @param gtkaccelgroup $accel_group² * @return menu object² **/² function menu($gdkwindow=null,$accel_group=null){² if(is_a($gdkwindow,'gdkwindow'))² $this->gdkwindow = &$gdkwindow;² $this->bar = &new GtkMenuBar();² $this->bar->set_shadow_type(GTK_SHADOW_ETCHED_IN);² if(is_a($accel_group,'GtkAccelGroup')){² $this->accel_group = &$accel_group;² }else{² $this->accel_group = &new GtkAccelGroup();² }² # showvar($GLOBALS['MAIN']);² }² /**² add an handlebox as box propertie ² and pack the bar into it so you will need to pack $this->box instead of $this->bar² */² function add_handlebox(){² $this->box = &new Gtkhandlebox();² $this->box->add($this->bar);² }² /**² will add a separator to $parent menu ² @param string $parent the parent menu keyindex² */² function add_sep($parent){² $this->add_menu_item('SEP',$parent);² }² /**² add an item to the menu² @param string $key is the keyindex to acces the item inside the objects² @param string $parent the parent's keyindex ² @param string $label the text to display for this item (can be null to get a separator)² @param mixed $callback the function name or array(object,methodname) to call on select² @param string $imgfile optionnal file path to an icon² @param bool $sensitive² @param bool $is_checkmenuitem² @param bool $is_checked² */² function add_menu_item($key,$parent,$label=null,$callback=null,$imgfile=null,² $sensitive=TRUE,$is_checkmenuitem=FALSE,$is_checked=FALSE){² while($this->items[$key]){ # ensure a unike keyid² preg_match("!(\d+)$!",$key,$m);² $int=$m[1];² if(is_numeric($int))² $key = substr($key,0,- strlen($int)).($int+1);² else² $key.='1';² }² if( (!$label)&& $this->menus[$parent]){² $this->items[$key] = &new gtkmenuitem();² $this->items[$key]->set_sensitive(FALSE);² $this->menus[$parent]->append($this->items[$key]);² return;² }² if(!$is_checkmenuitem){² $this->items[$key]= &new gtkmenuitem($label);² }else{² $this->items[$key]= &new gtkcheckmenuitem($label);² $this->items[$key]->set_active($is_checked);² }² $lb = $this->items[$key]->child;² # add icon if needed² if(file_exists($imgfile)){² if(! $this->gdkwindow){² $this->bar->realize();² $this->gdkwindow = $this->bar->window;² }² if($img = Gdk::pixmap_create_from_xpm($this->gdkwindow,null,$imgfile)){² $img = &new GtkPixmap($img[0], $img[1]);² $this->items[$key]->remove($lb);² $box = &new gtkhbox();² $box->pack_start($img,0,0,2);² $box->pack_start($lb,0,0,2);² $this->items[$key]->add($box);² }² }² # adding to parent menu² if(!$parent){² $this->bar->append($this->items[$key]);² }else{² if(! $this->items[$parent]){² echo "[ERROR] menu::add_menu_item to non-existent parent '$parent'\n";² return FALSE;² }² if(! $this->menus[$parent]){² $this->menus[$parent] = &new GtkMenu();² $this->items[$parent]->set_submenu($this->menus[$parent]);² }² $this->menus[$parent]->append($this->items[$key]);² }² # add accellkey if needed² if(substr_count($label,'_')){ # ajoute un raccourcis clavier² $acckey = $lb->parse_uline($label);² $this->items[$key]->add_accelerator('activate',$this->accel_group,$acckey,² GDK_CONTROL_MASK,GTK_ACCEL_VISIBLE);² }² #connect to callback² if($callback)² $this->items[$key]->connect('activate',$callback,$key);² }² function destroy(){² if($this->box)² $this->box->destroy();² else² $this->bar->destroy();² }²}²?>[[include:Main/CodeTableEnd]]²If you have any idea to enhance this piece of code don't hesitate to mail me, i'll surely enjoy.²²Hope this will help {{~Nathan}} at the-ring dot homelinux dot net[[<<]]²[[#top top]]²----²²!!! [[#changelog]]Change log ²* 2004-07-01 ²**now support checkmenuitem %bgcolor=#a0F0a0 color=green%[-new-]²**ensure unique keyid for items[[<<]]²[[include:CodeSnippetsInc#BottomBar#BottomBarEnd]]
time=1091464296
diff:1090770075:1090770075:=1,201c1²< %define=codelines bgcolor=#ffffd0 color=navy font-size=12px%[[notitle]]²< ! MenuIcon class ²< ²< http://jonathan.gotti.free.fr/phpgtkwikki/menuicon.png²< ²< * [-[[#sample How to use it?]]-]²< * [-[[#classcode the class code]]-]²< * [-[[#changelog changelog]]-]²< -----²< When creating a soft, you'll often want to have a menu like in the majority of graphical softs. It's often clear in your mind but require²< more code than you want to do simple things.²< So here's a class to help you to quickly design such menus and will also allow you to display nice icon in your menus.²< ----²< !!! [[#sample]]How to use it ?²< ²< first of all copy the class [[#classcode code]] to a file (ie: class-menu.php) and then include it in your code. Then here is a sample of code to show you how simple it is to create menu in your application so lets see:²< ²< %codelines%[=²< <?²< if(strtoupper(substr(PHP_OS, 0,3)) == 'WIN')²< dl('php_gtk.dll');²< else²< dl('php_gtk.so');²< include_once('path_to_class-menu.php');²< ²< $w = &new gtkwindow();²< $w->connect('destroy-event','shutdown');²< $w->connect('delete-event','shutdown');²< $w->set_position('center');²< ²< // We realize the window to get the gdkwindow needed to ²< // create Xpm if we want to display icons in our menu²< $w->realize();²< $gdkwindow = $w->window;²< ²< // We also create a gtkaccelgroup to manage accelkeys²< // the class menu will attempt to create its own if none is given at creation time²< $accelgroup = &new gtkaccelgroup();²< $w->add_accel_group($accelgroup);²< ²< // So we declare the menu and add it to the window²< $menu = &new menu($gdkwindow,$accelgroup);²< $w->add($menu->bar);²< ²< // We know can add entrys to the menu²< // first the file menu ²< $menu->add_menu_item('file',null,'file');²< // Second we want a open file option in the file menu²< $menu->add_menu_item($keyid='open',$parent='file',$label='_open',²< $callback='openfilefunctionname',$imgfile='img/fileopen.xpm');²< //we also want a separator in the menu file²< $menu->add_sep('file');²< $menu->add_menu_item('quit','file','_quit','shutdown','img/exit.xpm');²< $menu->add_menu_item('help',null,'help');²< $menu->add_menu_item('about','help','about','about_function_name');²< ²< $w->show_all();²< gtk::main();²< ²< function shutdown(){²< gtk::main_quit();²< }²< ?>=]²< ²< this would display a window with only a nice menu with icons and accelkey already defined.²< ----²< ²< !!! [[#classcode]]So now here's the menu class code²< check the code to find more details on how to use methods²< %codelines%[=²< <?php²< /**************************************************²< Easy menu manipulation²< Copyright (c) Jonathan Gotti <jonathan.gotti@the-ring.homelinux.net>²< **************************************************/²< ²< class menu{²< ²< var $bar;²< /** the gdkwindow used to create pixmaps */²< var $gdkwindow;²< /** the accel group to manage accelkeys */²< var $accel_group;²< /** used for easy access to menus */²< var $menus;²< /** used for easy access to menuitems */²< var $items;²< /**²< * constructor function²< * @param gdkwindow $gdkwindow²< * @param gtkaccelgroup $accel_group²< * @return menu object²< **/²< function menu($gdkwindow=null,$accel_group=null){²< if(is_a($gdkwindow,'gdkwindow'))²< $this->gdkwindow = &$gdkwindow;²< $this->bar = &new GtkMenuBar();²< $this->bar->set_shadow_type(GTK_SHADOW_ETCHED_IN);²< if(is_a($accel_group,'GtkAccelGroup')){²< $this->accel_group = &$accel_group;²< }else{²< $this->accel_group = &new GtkAccelGroup();²< }²< # showvar($GLOBALS['MAIN']);²< }²< /**²< add an handlebox as box propertie ²< and pack the bar into it so you will need to pack $this->box instead of $this->bar²< */²< function add_handlebox(){²< $this->box = &new Gtkhandlebox();²< $this->box->add($this->bar);²< }²< /**²< will add a separator to $parent menu ²< @param string $parent the parent menu keyindex²< */²< function add_sep($parent){²< $this->add_menu_item('SEP',$parent);²< }²< /**²< add an item to the menu²< @param string $key is the keyindex to acces the item inside the objects²< @param string $parent the parent's keyindex ²< @param string $label the text to display for this item (can be null to get a separator)²< @param mixed $callback the function name or array(object,methodname) to call on select²< @param string $imgfile optionnal file path to an icon²< @param bool $sensitive²< @param bool $is_checkmenuitem²< @param bool $is_checked²< */²< function add_menu_item($key,$parent,$label=null,$callback=null,$imgfile=null,²< $sensitive=TRUE,$is_checkmenuitem=FALSE,$is_checked=FALSE){²< while($this->items[$key]){ # ensure a unike keyid²< if(is_numeric($int = match("!(\d+)$!",$key)))²< $key = substr($key,0,- strlen($int)).($int+1);²< else²< $key.='1';²< echo "int $int / ".is_numeric($int)."\n";²< }²< if( (!$label)&& $this->menus[$parent]){²< $this->items[$key] = &new gtkmenuitem();²< $this->items[$key]->set_sensitive(FALSE);²< $this->menus[$parent]->append($this->items[$key]);²< return;²< }²< if(!$is_checkmenuitem){²< $this->items[$key]= &new gtkmenuitem($label);²< }else{²< $this->items[$key]= &new gtkcheckmenuitem($label);²< $this->items[$key]->set_active($is_checked);²< }²< $lb = $this->items[$key]->child;²< # add icon if needed²< if(file_exists($imgfile)){²< if(! $this->gdkwindow){²< $this->bar->realize();²< $this->gdkwindow = $this->bar->window;²< }²< if($img = file2pixmap($this->gdkwindow,$imgfile)){²< $this->items[$key]->remove($lb);²< $box = &new gtkhbox();²< multi_pack_start($box,$img,$lb,array(0,0,2));²< $this->items[$key]->add($box);²< }²< }²< # adding to parent menu²< if(!$parent){²< $this->bar->append($this->items[$key]);²< }else{²< if(! $this->items[$parent]){²< echo "[ERROR] menu::add_menu_item to non-existent parent '$parent'\n";²< return FALSE;²< }²< if(! $this->menus[$parent]){²< $this->menus[$parent] = &new GtkMenu();²< $this->items[$parent]->set_submenu($this->menus[$parent]);²< }²< $this->menus[$parent]->append($this->items[$key]);²< }²< # add accellkey if needed²< if(substr_count($label,'_')){ # ajoute un raccourcis clavier²< $acckey = $lb->parse_uline($label);²< $this->items[$key]->add_accelerator('activate',$this->accel_group,$acckey,²< GDK_CONTROL_MASK,GTK_ACCEL_VISIBLE);²< }²< #connect to callback²< if($callback)²< $this->items[$key]->connect('activate',$callback,$key);²< }²< }²< ?>=]²< If you have any idea to enhance this piece of code don't hesitate to mail me, i'll surely enjoy.²< ²< Hope this will help {{~Nathan}} at the-ring dot homelinux dot net²< ----²< ²< !!! [[#changelog]]Change log ²< * 2004-07-01 ²< **now support checkmenuitem %bgcolor=#a0F0a0 color=green%[-new-]²< **ensure unique keyid for items²\ No newline at end of file²---²> Describe MenuIcon here.²\ No newline at end of file²
author=nathan
author:1090770075=nathan
host:1090770075=82.120.18.177
name=Code.MenuIcon
host=82.124.242.167
agent=Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040615 Firefox/0.8 StumbleUpon/1.908
rev=8
diff:1090770784:1090770075:=2c2²< ![[#top]] MenuIcon class ²---²> ! MenuIcon class ²10d9²< ²15d13²< ²67,68c65²< this would display a window with only a nice menu with icons and accelkey already defined.[[<<]]²< [[#top top]]²---²> this would display a window with only a nice menu with icons and accelkey already defined.²198,199c195²< Hope this will help {{~Nathan}} at the-ring dot homelinux dot net[[<<]]²< [[#top top]]²---²> Hope this will help {{~Nathan}} at the-ring dot homelinux dot net²205,206c201²< **ensure unique keyid for items[[<<]]²< [[#top top]]²\ No newline at end of file²---²> **ensure unique keyid for items²\ No newline at end of file²
author:1090770784=nathan
host:1090770784=82.120.18.177
diff:1090780690:1090770784:minor=9d8²< * [-%target=_blank%[[http://opensource.org/licenses/lgpl-license.php licence: LGPL]]-]²76c75²< /**²---²> /**************************************************²78,81c77,78²< @author Jonathan Gotti <nathan at the-ring dot homelinux dot net>²< @copyright &copy; 2004 Jonathan Gotti²< @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public Licence²< */²---²> Copyright (c) Jonathan Gotti <jonathan.gotti@the-ring.homelinux.net>²> **************************************************/²
author:1090780690=nathan
host:1090780690=82.124.244.245
diff:1090807160:1090780690:minor=1,2c1,2²< [[include:CodeSnippetsInc#TopBar#TopBarEnd]]²< !! menu class with icons support²---²> %define=codelines bgcolor=#ffffd0 color=navy font-size=12px%[[notitle]]²> ![[#top]] MenuIcon class ²209c209²< [[include:CodeSnippetsInc#BottomBar#BottomBarEnd]]²\ No newline at end of file²---²> [[#top top]]²\ No newline at end of file²
author:1090807160=nathan
host:1090807160=82.124.244.245
diff:1090874468:1090807160:minor=21c21²< [[include:Main/CodeTable]]²---²> %codelines%[=²66c66²< ?>[[include:Main/CodeTableEnd]]²---²> ?>=]²74c74²< [[include:Main/CodeTable]]²---²> %codelines%[=²198c198²< ?>[[include:Main/CodeTableEnd]]²---²> ?>=]²
author:1090874468=nathan
host:1090874468=82.120.149.253
diff:1091455815:1090874468:=48c48²< // We now can add entrys to the menu²---²> // We know can add entrys to the menu²196,201d195²< }²< function destroy(){²< if($this->box)²< $this->box->destroy();²< else²< $this->bar->destroy();²
author:1091455815=nathan
host:1091455815=82.124.242.167
diff:1091462179:1091455815:=166,167c166²< if($img = Gdk::pixmap_create_from_xpm($this->gdkwindow,null,$imgfile)){²< $img = &new GtkPixmap($img[0], $img[1]);²---²> if($img = file2pixmap($this->gdkwindow,$imgfile)){²170,171c169²< $box->pack_start($img,0,0,2);²< $box->pack_start($lb,0,0,2);²---²> multi_pack_start($box,$img,$lb,array(0,0,2));²
author:1091462179=nathan
host:1091462179=82.124.242.167
diff:1091464296:1091462179:=141,143c141²< preg_match("!(\d+)$!",$key,$m);²< $int=$m[1];²< if(is_numeric($int))²---²> if(is_numeric($int = match("!(\d+)$!",$key)))²146a145²> echo "int $int / ".is_numeric($int)."\n";²
author:1091464296=nathan
host:1091464296=82.124.242.167