Lorenz Cuno Klopfenstein

Articles from May 2009

Years after the first browser wars, nowadays there are lots of different browsers available either for desktop computers, smartphones and portable media devices. The internet is growing steadily, while compatibility and accesibility standards are getting more and more important to ensure an equally acceptable browsing experience on every platform.

On the Windows desktop platform there are several different choices: one of those is the Opera browser. I used it for a week or less when version 6 came out, but then switched back to explorer. Only years later I bought Opera, just weeks before version 8 finally went free (lucky me), and stuck with it till now - eagerly waiting for release 10.0!

Yesterday Opera did celebrate its "15 years of innovation" in the browser market. They changed the frontage of their website to a very funny example of how the web was 15 years ago (that is, cluttered with awful gif animations). If the page has been removed, here's a screenshot of it.

So... why use Opera? Here's a couple of reasons of why I think that Opera deserves some attention and how it can enhance your browsing experience.

More...

Posted on Monday, May 04, 2009
Tagged as
9472 Views
11 comments posted

Double helix by ?mrhappy? I decided to write a couple of posts about a topic I have been working on a couple of years ago and I still find very interesting: Genetic Algorithms.

Genetic Algorithms are very general methods, based on the same mechanisms the biological theory of evolution is built upon, that can be successfully applied to solve optimization or search problems, without having to resort to a well known solution or a fully described algorithm.

Very generally speaking, genetic algorithms can be seen as a form of heuristic that approximates a possible solution for a problem by improving it step by step. Evolution in biology depicts how a population of simple organisms eventually can evolve in functioning complex life forms, whereas genetic algorithms apply the same incremental approach to the solution of mathematically defined problems.

Genetic Algorithms can also be made to mimic intelligent behavior and can be seen as a form of Artificial Intelligence. Even if the algorithm's process is basically "dumb" (and blind), its emergent behavior in response to changes in input can be easily mistaken as "awareness" by a human user.

More...

Posted on Friday, May 08, 2009
4328 Views
1726 comments posted

Yesterday I posted about genetic algorithms. Now it's time to put a GA to use and solve a very simple problem: in this case, generating boolean logic gates until one of them behaves exactly like a logic XOR gate.

XOR truth table Boolean logic gates take one or more boolean values as input (in our case, value A and B) and return a single boolean response value. Their behavior can be expressed by a so called truth table: in XOR's case, the gate will return True if the two input values are different, while it will return False if both are true or both are false.

More...

Posted on Saturday, May 09, 2009
21572 Views
125 comments posted

UTF I already posted about the problems I ran into using UTF-8 encoding on MySQL and PHP. Unfortunately, MySQL still seems to not be completely happy about Unicode characters, even if you work with ASP.NET and NHibernate (which both have complete Unicode support by default).

Therefore, in order to get your UTF strings stored in the database and get them back correctly in .NET, you'll first have to specify the charset you're using for the database connection in your connection string. For instance:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
	<reflection-optimizer use="false"/>
	<session-factory>
		<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
		<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
		<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
		<property name="connection.connection_string">
		Server=localhost;Database=db_name;User ID=user_id;Password=pwd;charset=utf8
		</property>
		<!-- other properties -->
	</session-factory>
</hibernate-configuration>

As far as I know, this should be equal to sending the "SET NAMES utf8;" command I used to send in PHP.

Then, you must ensure that your database tables actually use the Unicode encoding to store strings. This is especially true if your table creation SQL is automatically generated by NHibernate, through the GenerateSchemaCreationScript() method, because NHibernate will just use the default encoding (not Unicode). On Microsoft SQL Server it will instead use the Unicode column types like "nvarchar" and "ntext", preventing any problem.

Hence, when you create a new database, explicitly define the charset to use or MySQL will default to latin1:

CREATE DATABASE db_name
    DEFAULT CHARACTER SET utf8
    DEFAULT COLLATE utf8_unicode_ci;

Then you can normally create all your tables and start using them. Test the database using some uncommon characters, like the "famous" dotless turkish ı, and try to do some upper/lowercase conversion on the database.

Posted on Tuesday, May 12, 2009
799 Views
4 comments posted

I recently received a bug report on OnTopReplica, because it did render incorrectly when in High DPI mode. Some UI elements did slip beyond the window boundaries or were misaligned.

Even if Windows Forms simplifies high DPI mode the problem (Control instances have a special AutoScaleMode property that automatically adapts your UI based on the system font size or the DPI mode), you still must be certain to never ever use constant pixel values in your code.

Essentially, the code that handled the opening and closing of the "region" side box did increase (or decrease) the width of the form of a constant value (which I tought would always match the size of the region box control). Unfortunately, Windows Forms would automatically adapt the size of said control, thus making it bigger than expected. The solution is quite easy: simply drop every constant value and always refer to the actual size of each control (in Control.Size and Control.ClientSize).

OnTopReplica in chromeless mode.

The new version can be downloaded now via ClickOnce. As seen in the screenshot above, OnTopReplica 2.6 now also includes a "chrome less" mode that will remove the classic window borders (and make the application unresizable, even if it can still be moved). Should be much nicer for videos and similar.

Unfortunately, while publishing the new version I discovered that the original license I used to sign the ClickOnce manifest had expired last month (this means OnTopReplica was first published with ClickOnce around one year ago, woot!  :)). I had to sign it with a new key: should you encounter manifest or license problems when updating the application, please update .NET to version 3.5 SP1, which should fix all manifest problems.

Posted on Sunday, May 17, 2009
Tagged as
662 Views
1 comments posted

I have started to work on WPF GUI programming again, and finally found out how to use nested styles in an application (pretty basic, I know, but I'm slowly getting to know the framework on my own  :)).

So, starting from the basics, to embed some global styles in the application, you can define an external ResourceDictionary (much like an external CSS stylesheet):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Colors -->
    <Color x:Key="WhiteDark" A="255" R="238" G="240" B="245" />
    <Color x:Key="WhiteBright" A="255" R="254" G="254" B="254" />
    <!-- ... -->
	
    <RadialGradientBrush x:Key="PaleBackgroundRadial" Center="0.8,0.8" GradientOrigin="0.8,0.8"
                         RadiusX="0.8" RadiusY="0.7">
        <GradientStop Offset="0" Color="{StaticResource WhiteBright}" />
        <GradientStop Offset="1" Color="{StaticResource WhiteDark}" />
    </RadialGradientBrush>
    <!-- ... -->
	
</ResourceDictionary>

In order to reference those resources in your code (using their x:Key name), you must embed the dictionary where it is needed. The simplest solution is to embed all styles at an application level:

<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyResourceDictionary.xaml" />
                <!-- other dictionaries -->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
</Application>

Now, everywhere in your XAML code you'll be able to reference one of your styles, for instance:

<Rectangle Background="{StaticResource PaleBackgroundRadial}" />

Now, if you - like me - are used to web programming using CSS and HTML, you'll expect to be able to define a "class" with a custom style and then specify additional styles for all nested tags in an element of that class. You could for instance define a class for a menu and then define another (nested) style for all <b> bold tags inside it:

.menu {
    width: 200px;
    font-size: 0.8em;
    background: red;
}

.menu b {
    font-size: 1.2em;
    color: blue;
}

The definition for the bold tag will only be applied to <b> elements inside the element with the "menu" class. The same can be done in XAML, albeit in a somewhat different way:

<Style TargetType="{x:Type Window}" x:Key="GlossyWindow">
    <Setter Property="Background" Value="{StaticResource GlossyBackground}" />
    <Setter Property="TextElement.FontFamily" Value="Segoe UI" />
    <Setter Property="TextElement.Foreground" Value="White" />
    <!-- Other setters, trigger, etc... -->

    <!-- NESTED STYLES -->
    <Style.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Margin" Value="3.0" />
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Rectangle />

                            <ContentPresenter />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        
        <Style TargetType="{x:Type Menu}">
            <!-- Setters, triggers, etc... -->
        </Style>
    </Style.Resources>
</Style>

The effect of the XAML code above is that if you define a Window's style as "GlossyWindow", automatically all buttons and menus contained in that same window will have the nested styles applied (in addition to other styles you might have explicitly set on each button or on each menu).

This allows you to efficiently mark only a root element as being styled and apply a custom appearance to all nested elements in the XML structure. However, using nested styles can get you some quite... nested XAML code: the code above is still readable, but obviously if you start using more nested levels you'll end with a huge XML hierarchy.

To clean the code up, define each single style individually and then declare the nested styles hierarchy by "basing" them on the previously defined templates (this is done by using the BasedOn attribute on your styles and referencing the styles above by their dictionary key):

<Style TargetType="{x:Type Button}" x:Key="GlossyButton">
    <Setter Property="Margin" Value="3.0" />
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Rectangle />

                    <ContentPresenter />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type Menu}" x:Key="GlossyMenu">
    <!-- Setters, triggers, etc... -->
</Style>

<Style TargetType="{x:Type Window}" x:Key="GlossyWindow">
    <Setter Property="Background" Value="{StaticResource GlossyBackground}" />
    <Setter Property="TextElement.FontFamily" Value="Segoe UI" />
    <Setter Property="TextElement.Foreground" Value="White" />
    <!-- Other setters, trigger, etc... -->

    <!-- NESTED STYLES -->
    <Style.Resources>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource GlossyButton}" />
        
        <Style TargetType="{x:Type Menu}" BasedOn="{StaticResource GlossyMenu}" />
    </Style.Resources>
</Style>
Posted on Tuesday, May 19, 2009
Tagged as
3961 Views
9 comments posted

The notes gadget One of the few really useful Windows Sidebar Gadgets I often used is the "Notes" gadget: just great to paste snippets of text or to keep a short to-do list. Unfortunately, if you close the gadget by mistake, you'll also lose all the notes you were keeping!

There is a simple solution that works as long as you leave the "shadow copies" enabled on your OS. You'll have to access the folder where all gadgets settings are stored:

C:\Users\<userprofile>\AppData\Local\Microsoft\Windows Sidebar

Open up the folder's "previous versions" dialog and cross fingers. If you're lucky you'll find a version of the Settings.ini file of before you closed the notes gadget. Search for a line that contains the "Notes.gadget" string. Just a couple of lines below you should find something like:

0=Note one.
1=Note two...

Copy the contents of the notes to your current Settings.ini or restore the file completely.

Windows 7 includes an improved version of "Notes", which now has been promoted to a full blown application instead of being a simple gadget. That should also prevent the loss of notes due to accidental closing.  :)

Posted on Saturday, May 30, 2009
Tagged as
188815 Views
28 comments posted
Back to Klopfenstein.net
Clemens Klopfenstein Serena Kiefer Lukas Tiberio Klopfenstein Lorenz Cuno Klopfenstein
English German