вторник, 25 августа 2009 г.

DPI independent Delphi application (problem with large fonts)

When run-time DPI varies from design-time DPI you usually get a messed up GUI with Delphi.
For example, I develop at 150 DPI:
If user has the same DPI everything looks fine:
When user uses a smaller DPI, e.g., 96 DPI, everything is messed up:
When user has a larger DPI, e.g., 200 DPI, everything is messed up again:
Articles on the web suggest to use ScaleBy procedure, Scale property, or PixelsPerInch property in various combinations.
If you set TForm.Scale to true, Delphi does scale some controls for you, but not all of them. For example, it scales TButton, TEdit, TPanel, but does not scale TForm, TPageControl. There's one simple trick to fix that.
Suppose you want to put TEdit on TForm and have it look good with any DPI.
1. Set TForm.Scale property to true.
2. Put TPanel on top of TForm and make it occupy the whole form, DO NOT alter the Align property, you MUST have it set to alNone.
3. Put TEdit on top of TPanel and place it as you would place it on TForm.
4. In TForm's constructor (or OnCreate procedure) do the following:
// DPI fix begin
ClientWidth := TPanel.ClientWidth;
ClientHeight := TPanel.ClientHeight;
TPanel.Align := alClient;
// DPI fix end

Delphi scales TPanel correctly, and TForm keeps the design time size. That's why we make TForm have TPanel's size.
Then we expand TPanel to occupy the whole TForm as it was intended at design time.
That's all. Just 3 lines of code.

Now when user has 96 DPI:
When user has 200 DPI:
Source code of the example for Delphi7:

Unit1.pas:
unit Unit1;

interface

uses
Classes,
ComCtrls,
Controls,
ExtCtrls,
Forms,
StdCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
PageControl2: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Panel2: TPanel;
Edit2: TEdit;
PageControl1: TPageControl;
TabSheet3: TTabSheet;
Panel3: TPanel;
TabSheet4: TTabSheet;
Edit3: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
// DPI fix begin
ClientWidth := Panel1.ClientWidth;
ClientHeight := Panel1.ClientHeight;
Panel1.Align := alClient;
Panel2.Align := alClient;
Panel3.Align := alClient;
// DPI fix end
end;

end.


Unit1.dfm:
object Form1: TForm1
Left = 317
Top = 190
Width = 588
Height = 339
Anchors = [akLeft, akTop, akRight, akBottom]
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -17
Font.Name = 'Arial'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 150
TextHeight = 19
object Panel1: TPanel
Left = 0
Top = 0
Width = 580
Height = 305
BevelOuter = bvNone
TabOrder = 0
DesignSize = (
580
305)
object Edit1: TEdit
Left = 8
Top = 8
Width = 564
Height = 27
Anchors = [akLeft, akTop, akRight]
TabOrder = 0
Text = 'Edit1'
end
object PageControl2: TPageControl
Left = 8
Top = 40
Width = 561
Height = 257
ActivePage = TabSheet1
Anchors = [akLeft, akTop, akRight, akBottom]
TabOrder = 1
object TabSheet1: TTabSheet
Caption = 'TabSheet1'
object Panel2: TPanel
Left = 4
Top = 4
Width = 541
Height = 205
BevelOuter = bvNone
TabOrder = 0
DesignSize = (
541
205)
object Edit2: TEdit
Left = 8
Top = 8
Width = 525
Height = 27
Anchors = [akLeft, akTop, akRight]
TabOrder = 0
Text = 'Edit2'
end
object PageControl1: TPageControl
Left = 8
Top = 48
Width = 525
Height = 148
ActivePage = TabSheet3
Anchors = [akLeft, akTop, akRight, akBottom]
TabOrder = 1
object TabSheet3: TTabSheet
Caption = 'TabSheet1'
object Panel3: TPanel
Left = 0
Top = 0
Width = 513
Height = 113
BevelOuter = bvNone
TabOrder = 0
DesignSize = (
513
113)
object Edit3: TEdit
Left = 8
Top = 8
Width = 497
Height = 27
Anchors = [akLeft, akTop, akRight]
TabOrder = 0
Text = 'Edit3'
end
end
end
object TabSheet4: TTabSheet
Caption = 'TabSheet2'
ImageIndex = 1
end
end
end
end
object TabSheet2: TTabSheet
Caption = 'TabSheet2'
ImageIndex = 1
end
end
end
end

Note that my form is designed at 150 DPI so it will open distorted in your IDE if you use other DPI value. This article talks about run time DPI fix only :)

пятница, 14 ноября 2008 г.

Cisco Raw Data Record format

PRELUDE:

Recently the company I work at bought SCE which reports data throught Netflow v9. Though, in the other docs the SCE is said not to support Netflow v9. So we decided to catch RDRs and convert it to Netflow v5.
I was disappointed that the Raw Data Records: Formats and Field Contents document didn't contain full format description of RDR. It misses the main point: it doesn't tell you how the fields are layed out after you meet certain TAG.
Hopefully, we found a man who have already done RDR to Netflow v5 converter and asked him to help us out with RDR format. He sent us his RDR to Netflow v5 converter which I was observing. When I found out the RDR layout I was once again disappointed, because the first time I tried to catch and read RDRs, I used plain C structures.
For example, Transaction usage RDR contains almost all we need for a Netflow v5 packet. So I used
struct TUR {
char *subscriberID;
unsigned short packageID;
int serviceID;
- - - -
};

When thinking how they could send strings I assumed they simply send zero terminated strings like C does. That was false.

TOPIC:

GCC typedefs I use:
//! Typedef for signed 8-bit integer
typedef signed char s8;
//! Typedef for signed 16-bit integer
typedef signed short s16;
//! Typedef for signed 32-bit integer
typedef signed int s32;
//! Typedef for unsigned 8-bit integer
typedef unsigned char u8;
//! Typedef for unsigned 16-bit integer
typedef unsigned short u16;
//! Typedef for unsigned 32-bit integer
typedef unsigned int u32;


Every RDR field has the following format:
struct RDRField {
u8 type; // Field type
u32 size; // Field size
void *data;
};


Every RDR packet has the following format (RDRHeader format is unknown to me):
struct RDRPacket {
RDRHeader header; // sizeof(RDRHeader) == 20?
u32 tag; // Transaction usage, Subscriber, etc.
u8 fieldsNb; // Number of fields following (25 for TUR, 12 for SUR, etc.)
RDRField *fields;
};


For example, STRING field format is this:
struct STRING {
u8 type; // 0x29
u32 length;
char *str;
};

PACKAGE_ID:
struct PACKAGE_ID {
u8 type;
u32 size;
u16 value;
};

SERVICE_ID:
struct SERVICE_ID {
u8 type;
u32 size;
s32 value;
};

And so on.
Type and size are useful probably only for STRING fields to determine their lengths. Other fields don't need them really.
Note that most signed and unsigned 32-bit integers are in big endian (for example, a field's u32 size).

пятница, 26 сентября 2008 г.

Filesystem's system space size: ReiserFS > NTFS > Ext3





PRELUDE:

Recently I bought the second 750G hard drive Seagate with 32M cache. The first one was 750G Western digital with 16M cache. I bought it because I was out of space because I kept all I have on hard drive, rather than kept it on DVDs, because I share data with other users (local P2P atm).
So I decided to use the old Western digital as data one to keep music, movies, install files and all that is mostly read-only accessed thus giving my old hard drive a break after running for over half a year mostly 24h a day. So I went on copying data from old hard drive to the new one. First I found out that the disk with 32M cache is about 2-4 times faster on writing! So that's only sane it is used for system read-write. When I backed up everything I decided I will have single partition on the old hard drive for data. I was wondering which filesystem to choose. As a Linux user, I like ReiserFS as it is faster than Ext3 (while Ext4 is not yet out atm). But as a sane person I know I need to access the data from Windows too (playing games). First, I thought to use tools like Ext2FS for Windows and ReiserFS tool to access either volume from Windows. Both sucked too much. Ext2 one couldn't deal with Russian language, though I tried cp866, cp1251, utf-8, nothing helped. And ReiserFS one only works in cmd.exe. So back to old good NTFS, thanks ntfs-3g driver is now shipped by default with Slackware (distro I use since 10.0).

TOPIC:

Choosing among the three filesystems gave me some interesting results on the amount of system data they require to work. At the top you can see images with the results. It was really disgusting to know Ext3 eats about 1.5% of your space (which results in 11G of literally wasted space here) just to keep files. While NTFS and ReiserFS both eat under 100M of space, about 0.12% and 0.08% respectively. Notice, that ReiserFS eats about 1.6 times less than NTFS. I didn't know ReiserFS also rules in the space too :)

Though, the ReiserFS4 was sabotaged: http://linuxhelp.150m.com/jews/saboteurs.htm (in flavor of Ext4?)

вторник, 22 июля 2008 г.

Theading, Multitheaded game engine

Multithreaded Game Engine Architectures:

http://www.gamasutra.com/features/20060906/monkkonen_01.shtml

Threading 3D Game Engine Basics:

http://www.gamasutra.com/features/20051117/gabb_01.shtml

POSIX Threads Programming (good article to get understanding what thread is):

https://computing.llnl.gov/tutorials/pthreads/

четверг, 3 апреля 2008 г.

Shortcut tags I use

WIR = What I've Read

Links, books, articles, etc. which were useful to me.

CBF = Can Be Found (in Google)

The book is free for download if you bother to search for it. No direct links.