WBHelper for wbObjects makes your life easier

The OO Layer and how to use it.

Moderator: rubem

WBHelper for wbObjects makes your life easier

Postby alecgorge » Tue Apr 20, 2010 8:37 pm

I like using wbObjects for my WinBinder projects, but for some common actions it is too verbose for my liking. So I made WBHelper. It provides a slightly shorter way to create and use controls and dialogs.

The code is below but let me give you some examples:
Code: Select all
<?php
// include wbObjects
require 'wbObjects/wbObjects.php';

// include WBHelper and Dialog
require 'wbObjects/WBHelper.php';
require 'wbObjects/WBDialog.php';

// create the window in wbObjects
$main = $wbSystem->createWindow(AppWindow, 'main', 'window title', WBC_CENTER, WBC_CENTER, 532, 540);

// set it as the window in WBHelper
WB::setWindow($main);

// add an icon for effect
WB::setIcon('cool-icon.ico');

// add a control
// format:
//             public static function add( (string) $alias, (int) $winbinder_control_type, (mixed) $inital_contents, (int) $xpos, (int) $ypos, (int) $width, (int) $height [, (int) $style [, (int) $param [, int ntab]]] );
WB::add("alias", PushButton, "value", 10, 15, 90, 25);

// add a click event to the button
WB::bindClick("alias", function () {
     Dialog::alert('This is a alert box!', 'Title of the box!');
});

// you can use the get method to get the raw wbControl instance:
// create a Gauge
WB::add("progress_bar", Gauge, "0", 10, 445, 505, 20, 0x00000000, 50, 0);

// set the min and max (uses the raw winbinder object)
wb_set_range(WB::get('progress_bar')->wbObj, 0, 100);

// set the value uses the wbControl instance:
WB::get('progress_bar')->setValue(0);

/* Complete list of methods for WBHelper:
   getText( (string) $alias);
   setText( (string) $alias, (string) $set_text_to );
   appendText( (string) $alias, (string) $text_to_append );
   getSelected( (string) $alias); // get the selected item in a dropdown
   scrollToBottom( (string) $alias); // scroll the bottom of a multiline editbox
   get( (string) $alias);
   add( (string) $alias, (int) $winbinder_control_type, (mixed) $inital_contents, (int) $xpos, (int) $ypos, (int) $width, (int) $height [, (int) $style [, (int) $param [, int ntab]]] );
   bindClick( (string) $alias, (callback) $callback);

Complete list of methods for Dialog:
   alert( same as http://winbinder.org/manual/functions/wb_message_box.html );
   color( same as http://winbinder.org/manual/functions/wb_sys_dlg_color.html );
   open( same as http://winbinder.org/manual/functions/wb_sys_dlg_open.html );
   folder( same as http://winbinder.org/manual/functions/wb_sys_dlg_path.html );
   saveAs( same as http://winbinder.org/manual/functions/wb_sys_dlg_save.html );
*/
?>


An example of WBHelper in action is in viewtopic.php?f=8&t=1148 , in the source files gui.php and binds.php.

I have intentions of updating this occasionally, notably to automagically handle tabbing and shift+tab'ing.

Code: Select all
class WBHelper {
   protected static $window, $items = array();
   public static function setWindow ($window) {
      self::$window = $window;
   }
   public static function setIcon ($icon) {
      self::$window->setIcon($icon);
   }
   public static function add() {
      $args = func_get_args();
      $name = array_shift($args);

      $args = array_insert(array($name), $args, 1);

      if(empty($name))
         self::$items[] = call_user_func_array(array(self::$window, 'createControl'), $args);
      else
         self::$items[$name] = call_user_func_array(array(self::$window, 'createControl'), $args);
         
      return end(self::$items);
   }
   public static function get($item) {
      return self::$items[$item];
   }
   public static function bindClick($item, $callback) {
      return self::get($item)->onMainEvent = $callback;
   }
   public static function getText($item) {
      return self::get($item)->getText();
   }
   public static function setText($item, $text) {
      return self::get($item)->setText($text);
   }
   public static function appendText($item, $text) {
      $x = self::get($item);
      return $x->setText($x->getText().$text);
   }
   public static function getSelected($item) {
      return self::get($item)->getSelected();
   }
   public static function scrollToBottom ($item) {
      $WM_VSCROLL = 277;
      $SB_BOTTOM  = 7;
      wb_send_message(WB::get($item)->wbObj,$WM_VSCROLL,$SB_BOTTOM,0);
   }
}
class_alias('WBHelper', 'WB');

class Dialog extends WBHelper {
   public static function open ($title=null, $filter = array(array('All Files', '*.*')), $path = null, $filename = null) {
      return parent::$window->dialogOpen($title, $filter, $path, $filename);
   }
   public static function folder ($title=null, $path = null) {
      return parent::$window->dialogPath($title, $path);
   }
   public static function alert ($message, $title = null, $style=null) {
      return parent::$window->messageBox($message, $title, $style);
   }
   public static function color ($title=null, $color = null) {
      return parent::$window->dialogColor($title, $color);
   }
   public static function saveAs ($title=null, $filter = array(array('All Files', '*.*')), $path = null, $filename = null, $def = null) {
      return parent::$window->dialogSave($title, $filter, $path, $filename, $def);
   }
}
phpack, the best PHP compiler ever (unicode support): http://bit.ly/d1PXGF
alecgorge
 
Posts: 222
Joined: Thu Dec 03, 2009 2:05 am

Re: WBHelper for wbObjects makes your life easier

Postby frantik » Mon Apr 26, 2010 8:52 pm

cool :) glad you use wbObjects
frantik
 
Posts: 600
Joined: Tue Mar 22, 2005 7:17 am
Location: California

Re: WBHelper for wbObjects makes your life easier

Postby alecgorge » Mon Apr 26, 2010 10:54 pm

Always happy to use it!
phpack, the best PHP compiler ever (unicode support): http://bit.ly/d1PXGF
alecgorge
 
Posts: 222
Joined: Thu Dec 03, 2009 2:05 am

Re: WBHelper for wbObjects makes your life easier

Postby dchriz01 » Wed Sep 08, 2010 7:13 am

I'm glad your using WBObjects. I'm always happy using it.
[url=http://www.nogoodwar.org/] No Good War [/url]
dchriz01
 
Posts: 1
Joined: Fri Sep 03, 2010 12:55 am


Return to Object-Oriented Programming

cron