
FreeBASIC's Official Forums
|
| View previous topic :: View next topic |
| Author |
Message |
|
|
Posted: Oct 01, 2005 22:13 Post subject: Thing I whipped up today: |
|
|
I wrote part of a GUI library today:
| Code:
|
|
Option Explicit
#include "fbgui.bi"
Declare Sub MyCloseFunction(Byval Event As FBGui.EventInfo Ptr)
Dim MyWindow As FBGui.Window
MyWindow = FBGui.CreateWindow(400, 300, "My Window") FBGui.HookEvent MyWindow, FBGui.OnClose, @MyCloseFunction
FBGui.WaitUntilClosed(MyWindow)
Sub MyCloseFunction(Byval Event As FBGui.EventInfo Ptr) MessageBox 0, "Hello", "Hey!", 0 End Sub |
http://www.betterwebber.com/stuff/fbgui_test.exe
FreeBASIC sort of needs a good GUI library. Right now the two biggies are GTK and WxWidgets, and both make you haul a 3mb DLL with your program. This program compiles to 20kb, that's about 5kb of overhead.
I guess the basic idea is to make a multi-module static library for GUIs. No runtimes.
This is just what I could do this morning. Basic "CreateWindow" routine, multiple-window support, event latching (though only OnClose is worked into the WndProc), and a waituntilclosed routine I wrote for my own convenience.
But before I go any further with this, I kinda want to know a few things. Like, how should I do the interface?
I have two options, in my eyes:
1) Make a module for each widget:
FBGui.Button.ChangeText(MyButton, "Hey")
etc...
2) Make an encompassing set of functions that applies to
FBGui.ChangeSetting(MyButton, FBGui.Text, "Hey")
etc...
With method 1, you get API hell, 2000 functions to memorize, the GTK & WxWidgets syntax of choice. But the pro here is that you only need to package the modules you use. E.g.: Don't use any scrollbars? You won't be packing 20 routines involving scrollbars into your exe. Only a fraction of the static library would actually get included with your code.
With method 2, it's very easy to memorize the library syntax. It's how I would rather implement it. But it's hard to do without packaging a lot of routines, but you'd have one parent routine that calls a bunch of subroutines, which FB would have no choice to include in the EXE even if you never used them. So you'd get huge .exes.
I'm thiking of a few ways to slim down #2. I could include every TYPE of object into the header, and then use overloading to use the same function names for everything. But then you'd have to include a ton of ".bi" files, and it would be a pain in the butt for me. And the user would still have to specify what kind of widget he's using twice, which would not be preferable.
So you kind of catch my drift. Any thoughts? My goal here is to create a library that's small and simple. So you don't have a 3mb temperature calculator, and you don't have to write 300 lines of code if you want anything smaller. |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 02, 2005 0:11 Post subject: |
|
|
Hi jofers:
I prefer Option 1 because it will give smaller exe sizes and that would be my goal, and you have stated that it is also your goal. I think you can cover 90% of needs with a core set of "widgets", and then just leave a general catch all messages function for the remaining 10% of more advanced code. With time others could add more “widgets” to your library without any increase in size of existing user code. With Option 2, the way you described it, I can imagine your code base growing over time and exe size growing and growing with it.
People don’t need to memorize 200 or 2000 function names. They need a list to look up.
Your hooked HookEvent functions must be real functions returning integers, not just subs as in your small example. Then a user can cancel a close event, etc. Perhaps you already are doing this, but you just made a simple example.
Don’t leave out the option for icons!
And yes, I think there is a need for a light weight GUI library for FB.
Have fun
Garvan |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 02, 2005 0:58 Post subject: |
|
|
Hi jofers,
This is a great idea/project! It would be a real boost.
If I understood your drift ..
Couple of thoughts. As Garvan said, a reference text is all that is needed. Needed anyhow.
Also, syntax could make the calls seem intuitive. Eg:ButtonTextSet, ButtonTextGet, ButtonStateSet, ... EditorTextGet, EditorTextSet, etc
If the dependencies behind the scenes are worked out well, then with Include Once -ing shared bi files will help keep size down. So, for eg, only structures, udts, api-calls and etc that needed are actually every used.
And finally, things like myHandle = ButtonCreate(x,y,w,h,etc) gives the way the thing can be manipulated later, via myHandle, eg ButtonTextSet(myHandle,"TEXT")
If I missed the point, ignore this! :)
However, a GUI lib sounds ace!
You talking windows or linux.
If windows: I am very scratchy on FB and don't spend a lot of time with it, but time and ability permitting I would be willing to give some help. I have some small experience with windows API, mostly through wading through the SDK documentation trying to learn how to do things.
Mind you, I might waste more of your time asking questions initially than I save by helping. I won't be insulted if you decide to play it safe and decline the offer.
Edit: In fact, thinking it through, perhaps best if I don't participate. But if I see things I can help with I will pass them on to you to use as you see fit. |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 02, 2005 9:21 Post subject: |
|
|
| Great!!! My vote: #1 |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 02, 2005 9:56 Post subject: |
|
|
| Yeah, sounds really neat :) My vote is #1 too. |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 02, 2005 14:04 Post subject: |
|
|
It's not gonna work on linux is it? =(
winAPIs? |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 02, 2005 14:36 Post subject: |
|
|
I don't know much about Linux programming. I'm trying to keep the function names unique so if someone ever wanted to make a GTK wrapper they could.
However, it would be difficult to make a single GUI library that would compile on any platform, because almost every action or function is dependent on the OS. Two separate ports would have to be maintained (and I encourage it, if I go through with it).
You guys really want #1? I'm a little protestant towards it because I work with Java. Dealing with that API is a gaddanged nightmare. You have to look up every little thing, even stuff like remembering string functions. And there are soooo many packages to keep track of. Yuck.
I might meet you guys halfway and remove the FBGui.Button.ChangeText() bit to FBGui.ChangeText() with overloading and unions. It just seems that so much in terms of GUIs overlaps, I shouldn't have to write "button" over and over and over again. But I'm in favor of keeping as few functions to remember as possible. |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 02, 2005 15:13 Post subject: |
|
|
| [Content removed at author's request] |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 02, 2005 16:51 Post subject: |
|
|
| I agree with Z!re... |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 03, 2005 16:34 Post subject: |
|
|
Okay, here is the system I've decided on, after some careful thought.
1) Users will only use (DIM) two type structures: FBGui.Window and FBGui.Object
2) Object-specific data will handled internally by a void pointer to another structure.
3) Objects will be initialized with control-specific functions: CreateWindow, AddButton, AddScrollBar, etc. These functions will return the object.
4) Users will actually deal with pointers, but that'll be hidden with Type definitions (already implemented in the demo).
5) Each initializing function will load all control-specific routines (SetText, SetTitle, SetWidth, etc) for that control into a big master structure with room for function pointers for every control supported by the library (~1kb in memory)
6) All control-specific functions will be stored in separate modules. Adding a button with AddButton will load all the button routines into your compiled program.
7) When you do call SetText, SetWidth, etc... That function will look up the type from the object you feed it, and then look up the appropriate function to use in the control-specific functions stucture, and then use it.
Pros:
* Users will only have to specify what type of widget their dealing with once, when they initialize it.
* The actual number of manipulation functions is kept low.
* The compiled program will only contain the routines it needs, so simpler programs will be very small.
Cons:
* The simplicity means there will be some monkeying around to find the right functions to use for the right object.
Justification:
* That overhead is miniscule, unless you call those functions 1000s of times a second.
If there are any major protests to this format, let me know, or otherwise this is how the library will work.
Now, I just need one more bit of input from you guys: how should changing widget properties be done? There are two ways:
1) Many functions:
FBGui.SetText(MyObject, ...)
FBGui.SetWidth(MyObject, ...)
FBGui.SetBgColor(MyObject, ...)
2) One function, one extra parameter:
FBGui.Set(MyObject, FBGui.Text, ...)
FBGui.Set(MyObject, FBGui.Width, ...)
FBGui.Set(MyObject, FBGui.BgColor, ...)
Now, this is one occasion where consolidating functions doesn't add overhead, since these functions are just lookups anyway. So #1 is actually bigger than #2. Also, #2 may appear like a more uniform syntax. However, #1 involves less typing, and probably looks clearer to beginners.
So fire away, which should I use? Since I would not like to have functions with dozens of parameters (e.g., many WinAPI functions) or functions that take in a huge user-defined structure (e.g., many WinAPI functions), users will probably have to set some properties after the control has been added to the Window if they don't like the default values.
Also: You can expect very simple programs to be small enough. As you can see in the demo, there will be about 20-25kb in runtime library overhead, .exe format, and window set-up. Simple controls like buttons and input boxes will probably only have about 5-6 functions apiece. |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 03, 2005 16:45 Post subject: |
|
|
| #1 looks cleaner, and you've still gotta memorise/lookup stuff for either method, so I'd go with #1. |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 04, 2005 14:07 Post subject: |
|
|
1) Many functions:
FBGui.SetText(MyObject, ...)
FBGui.SetWidth(MyObject, ...)
FBGui.SetBgColor(MyObject, ...
i prefer this way, looks like the win32 api, which would make it easier for those of us with a modicum of win32 knowledge.
what about the VB style way?
MyObject.Text = "Text"
MyObject.Width = 200
etc?
i'd guess this would mean doing it in a OOP way that i dont know if its possible in FB?
edit.
just seen number #2 is kind of like this but not quite, so maybe i prefer #2 !?!! i dont know! |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 04, 2005 15:23 Post subject: |
|
|
This library's goal is in no way to resemble the Win32 API. Using structure properties would be nice, but you would have to scan them pierodically, as FreeBASIC cannot overload operators yet (that will come with object orientation, another feature not available).
I think I will go with #1. #2 is smaller, but I'll trade a few extra kb for clarity. |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 04, 2005 15:23 Post subject: |
|
|
The way of
MyObject.Text = "SomeText"
is called a property, and it's actually another way of calling a function (declared as property set). FreeBASIC doesn't support that, so you'd have to call an Update function every time you did that and I doubt anyone would like that. |
| |
|
| Back to top |
|
 |
|
|
Posted: Oct 04, 2005 18:22 Post subject: |
|
|
| Quote: | | FreeBASIC cannot overload operators |
Is that what this is called? ive been eating a book on C++ OOP but its not going in my head yet. All i wanted was this exact thing for one of my projects. Ie. Object.Property = , and then a function is called when you change a property. |
| |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|