Sign in Join
Wiki copytemporary

KOL: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Introduction
22
33![](/wiki-images/a/a5/Kolmck.png)\
44
55The Key Objects Library, a.k.a. KOL, is an object oriented framework created by Vladimir Kladov ([website](http://f0460945.xsph.ru/rindex.htm)) in 2000. It is the successor to the XCL library by the same author. The Mirror Classes Kit, a.k.a. MCK, is a set of visual components to write KOL applications in a visual design way, from within the Delphi IDE.
66
77The current point release is 3.23 for KOL and 3.23 for MCK.
88
99New versions appear with Irregular intervals, but the library is currently maintained. There is an svn repository for KOL and MCK at <https://svn.code.sf.net/p/kolmck/code/> which is usually more up to date than the point releases.
1010
1111The main reason creating this library is explained through the dissatisfaction of Vladimir and others towards ever increasing code bloat. Nobody seemed to care anymore about *efficient* coding. According to KOL users there a strong correlation between Moore's law and degrading code optimizing efficiency.
1212
1313An MCK project designed in a Delphi IDE can be recompiled with both 32 bit and 64 bit FPC 2.7.1 or later compilers without change. This way you can without having to upgrade your Delphi to a recent version:
1414
1515- obtain a 64 bit version of a project.
1616- obtain a unicode version of a Delphi project (5-2007) by simply defining -dUNICODE_CTRLS.
1717- or both!
1818
1919The framework centers around three basic concepts:
2020
2121- It uses - some say old school - pascal objects, not classes.
2222- Most of the widgets are basically one and the same object. Polymorphism is through constructing functions.
2323- It ignores the VCL/LCL, in fact it attempts to replace them.
2424
2525This has several **advantages**, some of which are:
2626
2727- The size of your binary is very tiny, on average more than 20 times smaller than a VCL/LCL binary, but a factor 100 or more is not unusual.
2828- KOL binaries (both executables and dll's) have much much faster load times and feel snappier.
2929- KOL is very easy to extend once you understand its concepts. It is a fully object oriented framework.
3030
3131It also has very obvious **disadvantages**, some of which are:
3232
3333- KOL is a framework. It doesn't mix very well with classes and it can't be sensibly mixed with LCL/VCL code because you will loose the size advantage.
3434- If you are not familiar with the old Object Pascal paradigm from Turbo Pascal the learning curve can be steep.
3535- You will miss some functionality that has become common place, like RTTI, but most of these features are often codebloat and the reason why KOL exists in the first place.
3636- Because you are often working on the same object with multiple appearances, it may be confusing that some methods or properties that you can see in the code editor do not apply.
3737
3838## Requirements
3939
4040- Free Pascal compiler 2.6.4 or later for Win32
4141- Free Pascal compiler 2.7.1 or later for Win64
4242- [KOL 3.23 or later](http://f0460945.xsph.ru/rindex.htm) for win32
4343- The Mirror Classes Kit a.k.a. MCK for those who want to develop using a visual designer like Lazarus or Delphi.
4444- [Release preview of KOL 3.23-64](https://yadi.sk/d/Z0uMP31_67nJY) for KOL win32/64
4545- [[KOL-CE|See the KOL CE page]] for win-ce development and requirements.
4646- Not required, but recommended is a copy of FPC's RTL and/or Delphi's RTL sources
4747
4848## Supported targets
4949
5050- All 32-bit Windows: from Windows 95 to Windows 8.1.
5151- All 64-bit x86_64 from Windows 2000 to Windows 8.1.
5252- [[WinCE port|Windows CE]] based PocketPC and Smartphones. More information on the [[KOL-CE|KOL CE page]]
5353
5454## Example
5555
5656Let's illustrate KOL with a simple example. A form, a button and some action. You will notice that the code resembles that of a LCL/VCL application and basically: It does exactly the same! The main difference is the use of objects and pointer to objects instead of classes and the use of constructing functions instead of constructors.
5757
5858Here's the main program file:
5959
6060``` pascal
6161 program koldemo;
6262 {This line makes us FPC and Delphi compatible}
6363 {$IFDEF FPC}{$MODE DELPHI}{$H+}{$ENDIF}
6464 {$APPTYPE GUI}
6565 uses
6666 Kol,
6767 koldemo1 in 'koldemo1.pas';
6868
6969 begin
7070 NewForm1( Form1, nil);
7171 Run(Form1.form);
7272 end.
7373```
7474
7575And here is the main unit, again, you will notice it is very similar to LCL/VCL code:\
7676
7777``` pascal
7878 unit koldemo1;
7979
8080 interface
8181 uses
8282 Windows, Messages, Kol;
8383 const
8484 cps:KolString = 'Kol project in ' + {$IFDEF FPC}'Freepascal '{$ELSE}'Delphi '{$ENDIF} +
8585 {$IFDEF WIN64}'64 bits '{$ELSE}'32 bits '{$ENDIF} +
8686 {$IFDEF UNICODE_CTRLS}'Unicode'{$ELSE}'Ansi'{$ENDIF};
8787 type
8888
8989 PForm1=^TForm1;
9090 TForm1=object(Tobj)
9191 {Note both widgets are of the same type}
9292 Form, Button:pControl;
9393 public
9494 {The message handler for our Button}
9595 procedure ButtonClick(Sender:PObj);
9696 end;
9797
9898 procedure NewForm1( var Result: PForm1; AParent: PControl );
9999
100100 var
101101 Form1:pForm1;
102102
103103 implementation
104104
105105 procedure NewForm1( var Result: PForm1; AParent: PControl );
106106 begin
107107 New(Result,Create);
108108 with Result^ do
109109 begin
110110 {The appearance of a widget depends on its creating function}
111111 Form := NewForm(AParent,cps);
112112 Button := NewButton(Form,'&Click Me');
113113 {Attach the MessageHandler}
114114 Button.OnClick := ButtonClick;
115115 { The applet variable is the Application object
116116 Assigning the main form to it assigns the main message loop to the form.}
117117 Applet:=Form;
118118 Form.Add2AutoFree(Result);
119119 end;
120120 end;
121121
122122 procedure TForm1.ButtonClick(Sender: PObj);
123123 begin
124124 ShowMessage('Ouch!');
125125 end;
126126
127127 end.
128128```
129129
130130So what's so special about it? Well, for one, compile this and look at the executable size. A similar program, Form + Button + ShowMessage compiled with Delphi 7 is **394 Kilobytes** and it gets much worse for progressive newer versions of Delphi:2267 Kilobytes XE7-32, 3672 Kilobytes XE7-64. For comparison: the KOL version compiled with D7 is **156** times smaller than the VCL version compiled with XE7.
131131
132132Compiled with PPC386 it renders an executable of 48128 bytes. Compiled like:
133133
134-`ppc386 -Mdelphi -CX -XX -Xs -Os koldemo.dpr `
134+```text
135+ppc386 -Mdelphi -CX -XX -Xs -Os koldemo.dpr
136+```
135137
136138Compiled with PPCX64 for Windows it renders 65536 bytes. Compiled like:
137139
138-`ppcx64 -Mdelphi -Rintel -CX -XX -Xs -Os koldemo.dpr`
140+```text
141+ppcx64 -Mdelphi -Rintel -CX -XX -Xs -Os koldemo.dpr
142+```
139143
140144The above examples are for an ANSI application. To compile a UNICODE application, simply add the UNICODE_CTRLS define, like:
141145
142-`ppcx64 -Mdelphi -Rintel -dUNICODE_CTRLS -CX -XX -Xs -Os koldemo.dpr`
146+```text
147+ppcx64 -Mdelphi -Rintel -dUNICODE_CTRLS -CX -XX -Xs -Os koldemo.dpr
148+```
143149
144150Note that especially for 64 bit, Free Pascal renders consistently smaller code than its expensive cousin. Also note that 32 bit Free Pascal has a slightly bigger runtime payload than its cousin. The size difference disappears when your application becomes more complex.
145151
146152## Tip
147153
148154For size, KOL defaults to an ugly system font. Here are multiple ways to remedy that:
149155
150156``` pascal
151157 {Add this code after the NewForm Function}
152158 {Release the font}
153159 Form.Font.ReleaseHandle;
154160 {Assign the default windows GUI font object}
155161 Form.Font.AssignHandle(GetStockObject(DEFAULT_GUI_FONT));
156162```
157163
158164The second method is slightly more involved, but is the [\|MSDN](http://msdn.microsoft.com/en-us/library/windows/desktop/dd144925%28v=vs.85%29.aspx) recommended way:
159165
160166``` pascal
161167 {Add this function}
162168 function GetGuiFont:TLogFont;
163169 var Metrics:TNonClientMetrics;
164170 begin
165171 Metrics.cbSize := SizeOf(Metrics);
166172 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, SizeOf(Metrics), @Metrics, 0);
167173 Result := Metrics.lfMessageFont;
168174 end;
169175 {And call it after the form creation}
170176 Form.Font.LogFontStruct :=GetGuiFont;
171177```
172178
173179Fonts in KOL are objects of type TGraphicTool. This object can also represent pens and brushes. We can also create a new GraphicTool object representing the GUI font:
174180
175181``` pascal
176182 {The KOL way to assign the GUI font}
177183 function GuiFont:PGraphicTool;
178184 var Metrics:TNonClientMetrics;
179185 begin
180186 Result := NewFont;
181187 Metrics.cbSize := SizeOf(Metrics);
182188 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, SizeOf(Metrics), @Metrics, 0);
183189 Result.LogFontStruct := Metrics.lfMessageFont;
184190 end;
185191 {We can now assign the new font to the Form's font property}
186192 Form.Font.Assign(GuiFont);
187193```
188194
189195And this is the result: ![](/wiki-images/0/0f/Kolscreenshot.png)
190196
191197---
192198*From the Free Pascal wiki, page "KOL", as it stood on 2023-02-04 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
193199Category: Tutorials

Back to history