Thread: How are GUI APIs made?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    How are GUI APIs made?

    Just out of interest really, as I've never been able to picture in my mind how one would be made (Buttons, text boxes etc.).

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    From an object-oriented perspective, you make an object that is capable of responding to events. Then you extend that and make an object capable of displaying an image. Then you extend that and you make an object capable of storing some input from the user. You keep extending these basic components, adding little bits of functionality at a time.

    A button, for instance, is just an object that responds to clicks. It displays an image and maybe some text. When you click it, it might change the image and play a sound, then perform some action. When it's done, it returns to it's original state.

    A test box is just an image, but when you click in it and start typing, it displays and stores your text, too.

    Then people can declare instances of these objects, change their basic properties, and integrate them into their app.

    edit:

    I recently did this in Flex - we were trying to create a web-based front-end to an already made Java app, so we had to replicate the old look exactly. All we did we define an object that responded to clicks, and then we defined a button, and sliders, and menus, etc... that extended from that. When we needed a "PlayBack" button - we just extended the base button, and added a little more functionality. Each file ended up only being like 15 or 20 lines - really simple way to look at it.

    If I were to make an API in C, I would look at it more in terms of just data. I would maintain a list of all objects on the screen, and provide methods to add objects, move them forward or back etc... Each object would be a struct that had their x-y coordinates, their status (enabled vs. disabled), and "type". Then depending on the "type", I would call a certain method to display that type, and based on it's x-y coordinates, size, status, and other data, it could display it on screen.

    Then any time someone clicked, I could go through the list, see if the click occurred on that object, and then execute any commands that were associated with a "click" for that object.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    How are GUI APIs made?
    A gigantic stork brings them; sometimes they are made of sugar and spice and everything nice, sometimes just hair and nails and puppy dog tails.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    There are made using existing libraries.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Quote Originally Posted by sean View Post
    From an object-oriented perspective, you make an object that is capable of responding to events. Then you extend that and make an object capable of displaying an image. Then you extend that and you make an object capable of storing some input from the user. You keep extending these basic components, adding little bits of functionality at a time.

    A button, for instance, is just an object that responds to clicks. It displays an image and maybe some text. When you click it, it might change the image and play a sound, then perform some action. When it's done, it returns to it's original state.

    A test box is just an image, but when you click in it and start typing, it displays and stores your text, too.

    Then people can declare instances of these objects, change their basic properties, and integrate them into their app.

    edit:

    I recently did this in Flex - we were trying to create a web-based front-end to an already made Java app, so we had to replicate the old look exactly. All we did we define an object that responded to clicks, and then we defined a button, and sliders, and menus, etc... that extended from that. When we needed a "PlayBack" button - we just extended the base button, and added a little more functionality. Each file ended up only being like 15 or 20 lines - really simple way to look at it.

    If I were to make an API in C, I would look at it more in terms of just data. I would maintain a list of all objects on the screen, and provide methods to add objects, move them forward or back etc... Each object would be a struct that had their x-y coordinates, their status (enabled vs. disabled), and "type". Then depending on the "type", I would call a certain method to display that type, and based on it's x-y coordinates, size, status, and other data, it could display it on screen.

    Then any time someone clicked, I could go through the list, see if the click occurred on that object, and then execute any commands that were associated with a "click" for that object.
    But how would you go about putting the image for a button on the screen? Or even turning the console into a window that could have more than text put in it?



    Quote Originally Posted by MK27 View Post
    sometimes just hair and nails and puppy dog tails.
    I guess stalks don't like Microsoft then...

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what Sean says is pretty much how it works - you have a basic object that can handle events, then a hierarchy of objects that do other things before/during/after receiving the events and handle that.

    Drawing an image involves first loading the image into memory from a file in some way (it maybe part of the executable file, as a "resource", created by a resource editor).

    A text is a bit more complex in the basic font handling, but once you have fonts and drawing glyphs (symbols within the font, usually a letter, but sometimes a letter consists of more than one glyph - e.g. Ä may be drawn as A and then ¨drawn above it - that depends on how the font-designer decided to go about it - it obviously takes up more space and a bit more effort to have two different characters that are almost the same, compared to making a small addition onto an existing font).

    Now that we can draw glyphs/characters, we can quite easily handle keyboard events to fill in the text box, and draw events to draw the text-box.

    Of course, it is fairly simple to describe, not quite so easy to actually do this.

    We have a GUI API at work, and it's by far the largest component in the "graphics" subsystem (and most likely the largest single component in the whole Embedded OS product).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    But how would you go about putting the image for a button on the screen?
    Using a graphics library, the same as any game, like OpenGL or something would work. However, I would imagine that more professional GUIs have their own libraries. At some point, you have to use assembly and place your pixels - anything above that is really just a graphics library of sorts.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sean View Post
    At some point, you have to use assembly and place your pixels - anything above that is really just a graphics library of sorts.
    Most of the graphics code nowadays is rarely written in assembler code. C compilers are pretty darn good, and for high performance graphics, you'd use a graphics processor to do the ACTUAL placing of pixels in the frame-buffer.

    I do definitely agree that graphics libraries are the way to go - OpenVG and QT are among the popular in embedded systems for the 2D side of things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using C++ for GUI with Database Application
    By bugmenot in forum Windows Programming
    Replies: 7
    Last Post: 12-14-2005, 08:24 PM
  2. Convert Windows GUI to Linux GUI
    By BobS0327 in forum Linux Programming
    Replies: 21
    Last Post: 11-27-2005, 04:39 AM
  3. How to move a sprite around inside a windows GUI
    By BuezaWebDev in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2005, 09:42 PM
  4. GUI Design Question (Seeking an opinion)
    By Exile in forum C++ Programming
    Replies: 9
    Last Post: 02-10-2005, 07:47 AM
  5. Other GUI API's that work in .NET?
    By subnet_rx in forum Tech Board
    Replies: 5
    Last Post: 09-02-2004, 03:23 PM