Hi all, For the laast few weeks, i was trying to learn nim with little project. A GUI library for windows is my project. Not fully finished yet, but i think if i show it you guys, i can improve it more. So for your criticism, i am posting a sample here. Please take a look .
import GuiLib # My Gui Library header file. It contains all necessary module imports.
# This Application class will do the initial jobs such as getting the hInstance.
# You can pass a string for your window class name. It's optional.
var app = Application("Window-Nim")
# Forward declarations. When will nim avoid the need for forward declaration ?
proc OnFormCreate( e : eventArgs)
proc OnLoad( e : eventArgs)
proc OnNCCreate(e : eventArgs)
proc OnButtonClick( e : eventArgs)
proc OnButtonClick2( e : eventArgs)
proc b1OnMouseEnter( e : eventArgs)
proc OnClosing( e : eventArgs)
proc OnSetFocus( e : eventArgs)
proc OnKillFocus( e : eventArgs)
proc OnMouseLeave_cbt(e : eventArgs)
proc OnMouseMove_Cal(e : eventArgs)
proc OnClick_CustomBTN(e : eventArgs)
# Creating an instance of a form class. We need to pass the Application object as parameter--
# -- in order to get the hInstance and other stuff.
var Form1 = NewForm(app)
# RegisterMsg is for handling wm_create msg. I can't find a better way to do this.
Form1.RegisterMsg(WM_CREATE, OnFormCreate)
# Now we are creating the form. If you want to set the form properties, you can--
# --set it like this.
# Form1.posx = 150
# Form1.posy = 200
# Form1.width = 800
# Form1.height = 600
# Form1.text = "My Nim Window"
# But i am using default values here.
Form1.CreateForm()
# Create some control classes
var
b1, b2 = Button()
lv = ListView()
cal = Calendar()
ud = UpDown()
dtp = DateTimePicker()
trb = TrackBar()
red = RichEdit()
cbt = CustomButton()
# Here you can add handler functions to control events lile click, mouse move etc.
b1.AddHandler(b1.event.click, OnButtonClick)
b2.AddHandler(b2.event.click, OnButtonClick2)
b2.AddHandler(b2.event.mouseLeave, b2_OnMouseLeave)
b1.AddHandler(b1.event.mouseEnter, b1OnMouseEnter)
Form1.AddHandler(Form1.event.mouseRightUp, OnDblClk_Form1)
Form1.AddHandler(Form1.event.closing, OnClosing)
Form1.AddHandler(Form1.event.load, OnLoad)
Form1.AddHandler(Form1.event.setFocus, OnSetFocus)
Form1.AddHandler(Form1.event.killFocus, OnKillFocus)
cal.AddHandler(cal.event.mouseEnter, OnMouseMove_Cal)
red.AddHandler(red.event.mouseLeave, OnMouseLeave_Red)
cal.AddHandler(cal.event.mouseHover,OnMouseHover_Cal)
cbt.AddHandler(cbt.event.click, OnClick_CustomBTN)
cbt.AddHandler(cbt.event.mouseLeave, OnMouseLeave_cbt)
# Now, we can set the control properties.
b2.posx =140
b2.posy = 10
b2.text = "My Button"
b2.width = 150
b2.font.name = "Calibri" # Tahoma is default
b2.font.size = 12
lv.posx = 350
lv.posy = 10
lv.width = 300
cal.posx = 350
cal.posy = 10
ud.posx = 350
ud.posy = 320
# See, custom button class has so many color properties.
cbt.posx= 350
cbt.posy= 220
cbt.text = "Owner Drawn BTN"
cbt.backColor = NewColor(80, 16, 44) # NewColor function receives RGB values and returns a COLORREFF
cbt.foreColor = NewColor(15, 15, 80)
cbt.hoverBackColor = cbt.backColor
cbt.hoverForeColor = NewColor(238,20,49)
cbt.width = 200
cbt.height = 80
dtp.posx = 150
dtp.posy = 150
trb.posx = 150
trb.posy = 120
red.posx = 10
red.posy = 200
# Now we can create our controls.
b1.CreateButton()
b2.CreateButton()
cal.CreateCalendar()
ud.CreateUpDown()
dtp.CreateDateTimePicker()
trb.CreateTrackBar()
red.CreateRichEdit()
cbt.CreateCustomButton()
# Let's display our form
Form1.ShowForm()
# This is the main loop.
app.Run()
# Now, these are the event handler function area.
# Argument "e" is a structure. It contains all necessary elements to handle the--
# --WndProc function
proc OnCreate( e : eventArgs) =
echo "Window created....."
echo e.handle # e.handle is the window HWND
proc OnLoad( e : eventArgs) =
echo "Window Loaded...."
proc OnButtonClick( e : eventArgs) =
b2.text = "Changed Text"
proc OnButtonClick2( e : eventArgs) =
msgBox("A Message")
proc b1OnMouseEnter( e : eventArgs) =
echo "Mouse entered on button 1"
proc b2_OnMouseLeave( e : eventArgs) =
echo "Mouse leaved from Button 2"
proc OnNCCreate( e : eventArgs) =
echo "NC CREATE handled"
proc OnClosing( e : eventArgs) =
echo "Form is closing..."
proc OnSetFocus( e : eventArgs) =
echo "Form is in focus...."
proc OnKillFocus( e : eventArgs) =
echo "Form lost focus...."
proc OnMouseLeave_cbt(e : eventArgs) =
echo "Mouse leaved from Custom button..."
proc OnMouseMove_Cal(e : eventArgs) =
echo "Mouse hovered over Calendar..."
proc OnMouseLeave_Red(e : eventArgs) =
echo "Mouse leave from Rich Edit..."
# End
Hi @citycide, Thanks for the reply.
This library is for windows only, since it is based on Win API like wNim. If you try to compare it to wNim, there is some slight differences. This library tends to mimic VB.Net style. But wNim tends to mimic wxPython style.
1. I didn't get the idea correctly. What is the problem with PascalCase ? Do you mean there are same functions like AddHandler or NewColor ? Anyway, I decided to avoid NewColor and introduce a new type named "ColorType". This is the syntax.
btn1.BackColor = Color.DarkGrey
I this this will be more elegant. This is the same style used in wNim also.
2. If i avoid control names from the "CreateXXXXX" function, the readability will be lost. Assume that you named your button as "b1". Then the create button function will look like this.
b1.Create() # Less readability, less elegant, less expressiveness.
b1.CreateButton() # More readable, more elegant, more expressiveness.
CreateButton(b1) # Also good
When you write like this --
var b1 = Button()
3. You are not creating a button. You just create a new instance of a button class. So "b1.CreateButton is where you really create the button.
What is the problem with PascalCase ?
You are making your code harder to understand. For some style guidelines see https://nim-lang.org/docs/nep1.html
So how about this ?
type
ListBox*= ref object of Control
items* : ItemsList # all type members are starting at lower case
ItemsList = object
iList* = seq[string]
proc createListBox(me : ListBox) : HWND = # procs starting at lowercase.
Is it ok ?