Clean up your code by fixing up Win32Lib

Posted

Photo by Hal Gatewood on Unsplash

Contents

Getting started

Starting with version 4.0, Euphoria supports default parameters. We can use this feature to our advantage by adding some defaults to commonly-used Win32Lib routines. A couple small tweaks will make your code cleaner and easier to maintain.

Typically, when you have to create a lot of controls, your code ends up looking like this:

constant
	FileMenu        = create( Menu, "&File", Main, 0, 0, 0, 0, 0 ),
	FileNew         = create( MenuItem, "&New\tCtrl+N", FileMenu, 0, 0, 0, 0, 0 ),
	FileOpen        = create( MenuItem, "&Open\tCtrl+O", FileMenu, 0, 0, 0, 0, 0 ),
	FileSave        = create( MenuItem, "&Save\tCtrl+S", FileMenu, 0, 0, 0, 0, 0 ),
	FileSaveAs      = create( MenuItem, "Save &As...", FileMenu, 0, 0, 0, 0, 0 ),
	FileSep1        = create( MenuItem, "-", FileMenu ),
	FilePageSetup   = create( MenuItem, "Page Set&up...", FileMenu, 0, 0, 0, 0, 0 ),
	FilePrint       = create( MenuItem, "&Print...\tCtrl+P", FileMenu, 0, 0, 0, 0, 0 ),
	FileSep2        = create( MenuItem, "-", FileMenu, 0, 0, 0, 0, 0 ),
	FileExit        = create( MenuItem, "E&xit", FileMenu, 0, 0, 0, 0, 0 ),
$

Yuck. All those zeros create a lot of visual noise. Ugly code is hard to maintain.

After making these tweaks, your code can look like this:

constant
	FileMenu        = create( Menu, "&File", Main ),
	FileNew         = create( MenuItem, "&New\tCtrl+N", FileMenu ),
	FileOpen        = create( MenuItem, "&Open\tCtrl+O", FileMenu ),
	FileSave        = create( MenuItem, "&Save\tCtrl+S", FileMenu ),
	FileSaveAs      = create( MenuItem, "Save &As...", FileMenu ),
	FileSep1        = create( MenuItem, "-", FileMenu ),
	FilePageSetup   = create( MenuItem, "Page Set&up...", FileMenu ),
	FilePrint       = create( MenuItem, "&Print...\tCtrl+P", FileMenu ),
	FileSep2        = create( MenuItem, "-", FileMenu ),
	FileExit        = create( MenuItem, "E&xit", FileMenu ),
$

Now that looks a lot cleaner, doesn’t it? You can also skip parameters, like this:

integer Text = create( MleText, "", Main, ,,,, ES_WORDWRAP )

Modify the code

Open up your local copy of Win32Lib.ew in Notepad++. (It’s probably under C:\Euphoria\Win32Lib\Include\win32lib.ew)

Remember, I’m using Jean-Marc DURO’s “win32lib_r2” release: Setup Win32Lib 0.70 on Windows 10

You can press Ctrl+G and to jump to a specific line. Add the following default parameters:

Line 19797

global function createEx( object pControl, sequence pCaption = "", atom pOwner = 0,
            object pLeft = 0, object pTop = 0, object pWidth = 0, object pHeight = 0,
            object styleFlags = 0, object exFlags = 0 )

Line 21111

global function create( integer pControl, sequence caption = "", atom pOwner = 0,
            object x = 0, object y = 0, object cx = 0, object cy = 0,
            object styleFlags = 0 )

That’s it! Now go forth and write cleaner code!

Author
Categories Beginner, Win32Lib