Page 1 of 1

Ditch the INDEXING system - Use HANDLES

Posted: Mon May 31, 2004 10:29 am
by syntax error
Being a Blitz programmer I love the handle system it uses for everything.

PureBasic also allows handles but it seems not every command/function supports them.

Code: Select all

; example 1 - INDEX method

Enumeration
    #win=0
    #sg=1
    #menu_bar
    #menu_quit
EndEnumeration

If OpenWindow(#win,176,193,228,88, #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "index method")
    If CreateMenu(#menu_bar, WindowID())
     MenuTitle("File")
     MenuItem(#menu_quit, "Quit")
    EndIf
EndIf

AddKeyboardShortcut(#win,#PB_SHORTCUT_Q,#menu_quit)

quit.b=#FALSE

Repeat
    ev=WaitWindowEvent()
    If ev=#PB_EventCloseWindow : quit=#TRUE : EndIf
    If ev=#PB_EventMenu
        If EventMenuID()=#menu_quit : quit=#TRUE : EndIf
    EndIf
Until quit=#TRUE

End

But here I cannot use AddKeyboardShortcut()
It only accepts INDEX values:

Code: Select all

; example 2 - HANDLE method

win=OpenWindow(#PB_ANY,176,193,228,88, #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "handle method")
CreateMenu(#PB_ANY, WindowID(win))
MenuTitle("File")
MenuItem(menu_quit, "Quit")

;;; AddKeyboardShortcut(WindowID(win),#PB_SHORTCUT_Q,menu_quit)

quit.b=#FALSE

Repeat
    ev=WaitWindowEvent()
    If ev=#PB_EventCloseWindow : quit=#TRUE : EndIf
    If ev=#PB_EventMenu
        If EventMenuID()=menu_quit : quit=#TRUE : EndIf
    EndIf
Until quit=#TRUE

End
Every command/function needs to accept either and INDEX number or a HANDLE.

In an ideal situation I would prefer to drop functions like WindowID() and have the OpenWindow() return the id directly so as to make things a little more open. There are probably good reasons why Fred needs to do this though.

To me this is how the handle method *should* work.

Code: Select all

; example 3 - ideal method

win=OpenWindow(176,193,228,88, #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "new method")
CreateMenu(win)
MenuTitle("File")
menu_quit=MenuItem("Quit")

AddKeyboardShortcut(win,#PB_SHORTCUT_Q,menu_quit)

quit.b=#FALSE

Repeat
    ev=WaitWindowEvent()
    If ev=#PB_EventCloseWindow : quit=#TRUE : EndIf
    If ev=#PB_EventMenu
        If EventMenuID()=menu_quit : quit=#TRUE : EndIf
    EndIf
Until quit=#TRUE

End
I don't expect the INDEXING system to go. Maybe too late now.

What say you?

Posted: Mon May 31, 2004 10:34 am
by blueznl
keep both
why?

1. it's there
2. it's not going to change
3. sometimes handles make sense
4. all old code uses them
5. if will confuse the hell out of newcomers

oh...

that last point is not a valid reason :-)

Posted: Mon May 31, 2004 10:36 am
by Pupil
This works for me:

Code: Select all

; example 2 - HANDLE method

win=OpenWindow(#PB_ANY,176,193,228,88, #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "handle method")
CreateMenu(#PB_ANY, WindowID(win))
MenuTitle("File")
MenuItem(menu_quit, "Quit")

AddKeyboardShortcut(win,#PB_SHORTCUT_Q,menu_quit)

quit.b=#FALSE

Repeat
    ev=WaitWindowEvent()
    If ev=#PB_EventCloseWindow : quit=#TRUE : EndIf
    If ev=#PB_EventMenu
        If EventMenuID()=menu_quit : quit=#TRUE : EndIf
    EndIf
Until quit=#TRUE

End

Posted: Mon May 31, 2004 10:45 am
by syntax error
I get the following error warning:

"Window object number is very high (over 5000)"

The 'win' variable is 8855192

Posted: Mon May 31, 2004 11:52 am
by Pupil
syntax error wrote:I get the following error warning:

"Window object number is very high (over 5000)"

The 'win' variable is 8855192
Which version are you using?
(i used pb3.90, without debugger)

Posted: Mon May 31, 2004 3:00 pm
by syntax error
3.90 SP1.
It works without the debugger here as well. That's no good to me.
I always develop with the debugger on until the app is completed.

Thing is, AddKeyboardShortcut() is expecting an index ...
AddKeyboardShortcut(#Window, Shortcut, EventID)

I think I read somewhere that PB will create indices up to the highest number you use. So, where win=8855192 I believe there will be 885192 indices initialised. Hence the warning message.

What's needed is
AddKeyboardShortcut(WindowID, Shortcut, EventID)

Then I can do this:

Code: Select all

AddKeyboardShortcut(WindowID(win) ...

Posted: Tue Jun 01, 2004 10:03 pm
by POedBoy
Hmmm........If I knew how to vote I would. Maybe the voting buttons don't show up in Opera :? *EDIT* now they show up :D *EDIT*

Anyways, I've been using blitz for a long time, and finally decided to purchase pure a couple of days ago. I prefer using handles to an indexing system, but with #PB_Any, I think you've got a good way to use either method.

Only problem being, #PB_Any makes the debugger throw a fit just like Syntax has noted("Window object number is very high (over 5000)"). I spoke with another person, who also uses both blitz and pure, and hes had the same issue. Is this a known bug? I did a quick search and this was the only thread I saw that mentioned it.

I don't want to deal with index's, but I'd like to be able to use the debugger. Anybody able to provide some insights on this?

Posted: Tue Jun 01, 2004 10:10 pm
by LarsG
Well hello there p0edboy.. welcome to our community...

I just wanted to say that with using both systems makes people confused, 'coz some places you need the xxxID() function, and sometimes you don't.. It would be much easier to either loose the xxID() and just pass the handles, or use xxxID() consistantly...(sp?)

Don't ask me. I only came out for a loaf of bread...

Posted: Wed Jun 02, 2004 2:32 am
by fsw
Don't ask me, I'm in the process of not using the PureBasic Toolkit at all.
  • My Prefered

Code: Select all

; Example with GUI CLASSES...
;
IncludeFile "PureGUI.pbi" ; will be a LIB when it's done...

Declare Button1()

;- Program Start

;- Create Frame
MainWindow.FSW_Frame = FSW_Frame()
MainWindow\Create(#TRUE, 0, 0, 300, 300, "Main Window")
MainWindow\SetMinSize(200,200)
MainWindow\SetMaxSize(400,400)
MainWindow\Centre()
MainWindowHandle.l = MainWindow\GetHandle()

  ;- Create Gadgets 
  ; The Button Class inherited Functions [Methods] and Structures [Data]
  ; from the 'Control Class' ...it seems to work! 
  Global TestButton1.FSW_Button
  TestButton1 = FSW_Button()
  TestButton1\Create(MainWindowHandle, 5, 5, 50, 25, "Button 1")
  TestButton1\Connect(@Button1())
  TestButton1\LockSides(#TRUE,#TRUE,#FALSE,#FALSE)
  
  TestButton2.FSW_Button = FSW_Button()
  TestButton2\Create(MainWindowHandle, 245, 5, 50, 25, "Button 2")
  TestButton2\Connect(?Button2)
  TestButton2\LockSides(#FALSE,#TRUE,#TRUE,#FALSE)
  
  TestButton3.FSW_Button = FSW_Button()
  TestButton3\Create(MainWindowHandle, 5, 270, 50, 25, "Button 3")
  TestButton3\Connect(?Button3)
  TestButton3\LockSides(#TRUE,#FALSE,#FALSE,#TRUE)

  TestButton4.FSW_Button = FSW_Button()
  TestButton4\Create(MainWindowHandle, 245, 270, 50, 25, "Button 4")
  TestButton4\Connect(?Button4)
  TestButton4\LockSides(#FALSE,#FALSE,#TRUE,#TRUE)

  TestEdit.FSW_Edit = FSW_Edit()
  TestEdit\Create(MainWindowHandle, 55, 30, 190, 240, "My Edit")
  TestEdit\Connect(?Edit)
  TestEdit\LockSides(#TRUE,#TRUE,#TRUE,#TRUE)

MainWindow\Show(#TRUE)

End
Procedure Button1()
  Debug "BUTTON 1 EVENT FUNCTION"
  MessageBox_(MainWindowHandle, "BUTTON 1 HANDLE: "+ Str(TestButton1\GetHandle()), "FUNCTION", #MB_OK)
EndProcedure

Button2:
  Debug "BUTTON 2 EVENT FUNCTION"
  MessageBox_(MainWindowHandle, "BUTTON 2 HANDLE: "+ Str(TestButton2\GetHandle()), "FUNCTION", #MB_OK)
Return

Button3:
  Debug "BUTTON 3 EVENT FUNCTION"
  MessageBox_(MainWindowHandle, "BUTTON 3 HANDLE: "+ Str(TestButton3\GetHandle()), "FUNCTION", #MB_OK)
Return

Button4:
  Debug "BUTTON 4 EVENT FUNCTION"
  MessageBox_(MainWindowHandle, "BUTTON 4 HANDLE: "+ Str(TestButton4\GetHandle()), "FUNCTION", #MB_OK)
Return

Edit:
  Debug "EDIT EVENT FUNCTION"
  MessageBox_(MainWindowHandle, "EDIT HANDLE: "+ Str(TestEdit\GetHandle()), "FUNCTION", #MB_OK)
Return
I'm using plain Win API right now but I'm not quite sure if the syntax will be the same when I'm done. It's alot of work :?
Maybe I will adapt the syntax to wxWidget, who knows...

It would be nicer without the backslash :wink:

Posted: Wed Jun 02, 2004 2:34 am
by fsw
Forgot to mention: in the above example I'm using OS handles only.

Posted: Wed Jun 02, 2004 7:31 am
by GedB
There are advantages to using an index, so they should definately stay.

1) If you find you need to use throw-away elements, you just keep using the low numbers. Each time you reuse the index the previous element is automatically deallocated.

2) The combination of Enumeration of Index makes it much easier when dealing with indexes used for identification. For example, when dealing with gadgets you can just create an enumeration for the elements. With #PB_Any you have the overhead of storing the handles returned by the creation routine.

So my vote is definately to keep both.

Posted: Wed Jun 02, 2004 9:02 am
by Fred
The above mentionned error (with AddKeyboardShortCut) is probably a bug, I will fix it.

Posted: Sat Jun 12, 2004 8:48 am
by syntax error
Thanks Fred once again.
Each time you reuse the index the previous element is automatically deallocated
Interesting. So, if I do this ...

Code: Select all

#img=3
CreateImage(#img, x,y)
CreateImage(#img, x,y)
CreateImage(#img, x,y)
CreateImage(#img, x,y)
Will PB automatically free up an existing image before creating the new one?
Or, should I call FreeImage anyway?

Posted: Sat Jun 12, 2004 10:04 am
by blueznl
it will clean it up