SoundBox, another mp3 player.

I started a new personal project, a little Mp3 player, it simple, does not have a lot of controls, visualaztions, and all the rest of everything found with most media players now a days. I just wanted something that would play my music.

This audio player is capable of playing other file types but I have set it to only find and play Mp3′s. The way it works is when started it scans your “My Music” folder for any and all Mp3′s you may have lying around in there and puts them into a large list, after which just waits for you to hit play.

It features Play/Pause, Prev/Next, Mute/Volume Down/Volume Up, and Shuffle. It displays the song title, the artist and the current play time.

As for visuals it currently looks like this.

The name of this audio player is SoundBox. I will release a build as well as the source shortly, I just want to clean up the source some and maybe finish making a halfway decent icon.

PHP Stuff : My database class

PHP Stuff : My database class.

When developing websites I usually have a database class of my own that I use,
I have a new one that I am starting to create now and it is quite simple to use.

Such as a simple query with a single where statement.


include "Database.php";
$db = new Database("localhost", "user", "pass", "dbname");
$db->connect();

$db->where("age", 18, WhereOperator::$MoreThan)
	->where("age", 25, WhereOperator::$LessThan)
	->orWhere("age", 45, WhereOperator::$Equal)
	->select("persons");
	
$row = $db->fetch();

Pretty simple huh? I like how it works.
As for the WhereOperator there are a few operators you can use as you
can see in the WhereOperator class.


class WhereOperator
{
    public static $Equal = 0;
    public static $Like = 1;
    public static $LessThan = 2;
    public static $MoreThan = 3;
    public static $LessThanOrEqual = 4;
    public static $MoreThanOrEqual = 5;
    public static $NotEqual = 6;
    public static $In = 7;
    public static $NotIn = 8;
    public static $Contains = 9;
    public static $NotContains = 10;
    public static $Between = 11;
    public static $NotBewteen = 12;
    public static $BeginsWith = 13;
    public static $NotBeginsWith = 14;
    public static $Contains = 15;
    public static $NotContains = 16;
    public static $NotLike = 17;
    public static $IsNull = 18;
    public static $IsNotNull = 19;
}

I’ll have this class posted up later after I get everything finished up, it
is going to be part of a small framework I am working on for myself.

The framework is really just a little personal project something to have
fun with, but it may get used in a couple of sites I’ll post the code
of that on sourceforge or github or something of the like, until then…
high ho silver away…

Dynamic getter’s and setter’s in PHP

Anyone who has done almost any PHP that required your to make a class
of some sort probably also had to create getters and setters. If you happen
to have a lot of properties that need to have getters and setters it can become
bothersome, however using PHP’s __call magic method this can be cut down to
just a small snippet of code, here’s how.
PHP’s __call magic method allows method’s that do not exist in your class to be
called, so for instance let’s say you have the class.


<?php
	class Foo {
		public function Hello() {
			echo "Hello, world!";
		}
		
		public function __call($name, $args) {
			echo "Calling method {$name}";
		}
	}
?>

Now if you where to call Foo::Hello(); you would get “Hello, world!” printed
to your browser, however calling something like Foo::Bar(); would cause PHP
to send the method name and arguments to your __call method as your class
does not have an actual method name Bar. So how can we use this to our
advantage? Lets see.

I am creating a new project and I have this class which will have many properties,
some stored in an array some actual variables and I need to create getters and setters
for all of them, what to do. Well with the help of the __call magic method it is as easy
as this.


class Foo
{
    private $test;
    private $test2;
    private $bar;

    public function __construct()
    {
        $this->test = "";
        $this->test2 = "";
        $this->bar = array();
        $this->bar['str'] = " world!";
    }

    public function __call($name, $args)
    {
        if(preg_match("/get/i", $name))
        {
            //Set the entire method name to lowercase so we don't have to mess around with
            //anything incase camel case or C# like method names are being used.
            $name = strtolower($name);
            //Find the start and length of the get / set in the method.
            $pos = strpos($name, "get") + strlen("get");
            //Strip the get / set from the getter / setter method.
            $name = substr($name, $pos, strlen($name) - $pos);

            //Create a new reflection object from this class.
            $reflection = new ReflectionClass($this);
            $props = $reflection->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED);

            //Loop through all the properties that we found.
            foreach($props as $prop)
            {
                //Check if it is an array so we can search the keys in an array instead of the
                //name of the property.
                if(is_array($this-> {$prop->getName()}))
                {
                    //So it's an array, does our array contain a key with the name
                    //that we are looking for?
                    if(key_exists($name, $this-> {$prop->getName()}))
                        return $this-> {$prop->getName()} [$name];
                }
                //If it is not an array then does that name match the name we are looking for?
                else if($prop->getName() == $name)
                    return $this->$name;
            }
            return null;
        }
        //Do almost the same thing as in the get option get here but we will be setting a value
        //not retreving one.
        else if(preg_match("/set/i", $name))
        {
            $name = strtolower($name);
            $pos = strpos($name, "set") + strlen("set");
            $name = substr($name, $pos, strlen($name) - $pos);
            $value = "";

            //Make sure we actually have an argument in there.
            if(count($args) >= 1)
                $value = $args[0];

            $reflection = new ReflectionClass(__CLASS__);
            $props = $reflection->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED);

            foreach($props as $prop)
            {
                if(is_array($this-> {$prop->getName()}))
                {
                    if(key_exists($name, $this-> {$prop->getName()}))
                        $this-> {$prop->getName()} [$name] = $value;
                }
                else if($prop->getName() == $name)
                    $this->$name = $value;
            }
        }
    }
}

$foo = new Foo();
$foo->setTest("Hello,");
echo $foo->getTest(); //Prints "Hello,"
echo $foo->getStr(); //Prints " world!"

Quick PHP Functions : preg_match_count, starts_with

Here are a couple more quick php functions for you, something you can throw in your common.php file and be used in the future if ever need be.

Click the function names which link to the code stored at Smipple.net

preg_match_count, takes a pattern, an input string, and returns the amount of times your pattern appears in the string.


preg_match_count("<[^>]*>", "<p><span>test</span></p>"); //Returns 4

starts_with, returns true or false if your input string starts with your needle.


starts_with("testing", "test"); //Returns true
starts_with("testing", "ing"); //Returns false

That’s all I got for now, hopefully I have something more to post then just small bits of simple code in the future.

Quick PHP Functions : str_remove

I had a need for a simple function to remove a portion of a string similar to how it is done with most
functions in string libraries such as .NET’s String.Remove.

Using the function is as simple as


$str = str_remove($str, 10, 2);

and here is the implementation.


if(!function_exists('str_remove')) {
	function str_remove($str, $startOffset, $length) {
		$endPos = ($startOffset + $length);
		$strLeft = substr($str, 0, $startOffset);
		$strRight = substr($str, $endPos, strlen($str) - $length);
		$newStr = $strLeft . $strRight;
		return $newStr;
	}
}

You could also shorten this a couple of lines by using substr_replace.


if(!function_exists('str_remove')) {
	function str_remove($str, $startOffset, $length) {
		$newStr = substr_replace($str, "", $startOffset, $length);
		return $newStr;
	}
}

MyLaunch – A application / whatever else launcher.

You may have seen or used application launchers on diffrent platforms such as Quicksilver on Mac OSX Gnome Do or Linux or Launchy on Windows / Linux.

Well I have used launch before, as well as a couple of other app launchers but never really found exactly what I wanted, so I made my own, firstly because I can make it how I want it, and secondly it’s a good learning experince.

MyLaunch looks similar to launchy however acts more like quicksilver or gnome do in the sense that you type a action and a argument to send to the action.

So if I wanted to start notepad I would type run notepad. If I wanted to search google I would type google mysearch or lets say I wanted to open Explorer into a certin directory well I can type win C:\mydir and open explorer there.

The run command is the only one built it by default the other two I listed google and win are added using plugins, which you can create your self using and .NET language.

So how about some screenshots now.

This is MyLaunch while typing in Notepad, you can see it completion the word notepad.

MyLaunch ready to launch Notepad.

This is MyLaunch while typing google for a google search, as you can see it completes these words as well.

MyLaunch completion a action

It’s a pretty simple launcher, but can be quite powerful with plugins. It’s also
just how I like it.

C# Remove flickering in MDI applications.

If you have ever been working with MDI applications, and while to MDI windows are while the MDI windows are maximized and you switch you see a flicker of the windows.

Well below is a simple quick fix for solving this issue.

So firstly lets define some constants that we will be using.


const int WM_NCPAINT = 0x85;
const int WM_SIZE = 0x05;

Now override the WndProc method in the MDI child window(s).


protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_NCPAINT) 
    {
        if(this.WindowState == FormWindowState.Maximized)
            return;
    }

    if (m.Msg == WM_SIZE)
    {
        if (this.WindowState == FormWindowState.Maximized)
            return;
    }

     base.WndProc(ref m);
}

and viola your flickering issues are gone.