Ditch the INDEXING system - Use HANDLES

Got an idea for enhancing PureBasic? New command(s) you'd like to see?

Ditch the Indexing system(?)

Poll ended at Mon Jun 14, 2004 10:29 am

Too right Mr Error. You should be a president!
13
19%
Too right Mr Error. You should be a president!
13
19%
I'm easy. Both methods do it for me
13
19%
I'm easy. Both methods do it for me
13
19%
INDEXING - It's the only way to travel
4
6%
INDEXING - It's the only way to travel
4
6%
Don't ask me. I only came out for a loaf of bread
4
6%
Don't ask me. I only came out for a loaf of bread
4
6%
 
Total votes: 68

syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Ditch the INDEXING system - Use HANDLES

Post 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?
Last edited by syntax error on Mon May 31, 2004 10:47 am, edited 1 time in total.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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 :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post 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
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Post by syntax error »

I get the following error warning:

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

The 'win' variable is 8855192
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post 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)
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Post 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) ...
POedBoy
New User
New User
Posts: 8
Joined: Tue Jun 01, 2004 9:31 pm
Location: La Verne, CA, USA

Post 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?
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post 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?)

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

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

Post 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:
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Forgot to mention: in the above example I'm using OS handles only.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

The above mentionned error (with AddKeyboardShortCut) is probably a bug, I will fix it.
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Post 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?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

it will clean it up
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply