Yale CPSC 427 - Graphical User Interfaces
School name Yale University
Pages 24

Unformatted text preview:

OutlineGraphical User InterfacesThe gtkmm FrameworkOutline GUI gtkmmCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 22November 30, 2010CPSC 427a 1/24Outline GUI gtkmmGraphical User InterfacesThe gtkmm FrameworkCPSC 427a 2/24Outline GUI gtkmmGraphical User InterfacesCPSC 427a 3/24Outline GUI gtkmmUser InterfacesModern computer systems support two primary general-purposeuser interaces:Command line: User input is via a command line typed at thekeyboard. Output is character-based and goes to aphysical or simulated typewriter-like terminal.Graphical User Interface (GUI): User input is via a pointing device(mouse), button clicks, and keyboard. Output isgraphical and goes to a window on the screen.CPSC 427a 4/24Outline GUI gtkmmInterfaces for C++The C++ standard specifies a command line interface: iostreamand associated packages. No similar standard exists for GUIs.De facto GUI standards in the Linux world are GTK+ (used by theGnome desktop) and Qt (used by the KDE desktop).GTK+ is based on C; Qt is based on an extension of C++ andrequires a special preprocessor.gtkmm is a C++ wrapper on top of GTK+.Advantages: Provides type safety, polymorphism, and subclassing.Provides a native interface to C++ code.Disadvantages: Components not so well integrated.Documentation spread between gtkmm, gtk+, andother components but improving.CPSC 427a 5/24Outline GUI gtkmmOverall Structure of a GUIA GUI manages one or more windows.Each window displays one or more widgets.These are objects that provide graphical and textual input andoutput to the program.A GUI package such as gtkmm maintains a widget tree.A widget controls a particular kind of user input or output.Examples: label, text box, drawing area, button, scroll bar, etc.CPSC 427a 6/24Outline GUI gtkmmConcurrency and EventsThe central problem in building a GUI is handling concurrency.Data arrives from multiple concurrent sources – mouse andkeyboard, network, disk, other threads, etc.We call the arrival of a piece of data an event.IEvent arrival times are unpredictable.IEvents should be processed as quickly as possible.For example, to have a good interactive feel, the GUI shouldrespond to a mouse click event within milliseconds.CPSC 427a 7/24Outline GUI gtkmmEvent LoopAn event loop allows a single thread to manage a set of events.At some level, the hardware or software polls for events.When an event is detected, it is dispatched to an event handler.The event handler either processes the event itself, queues a taskfor later processing, or spawns a thread to process it.While the event thread is processing one event, no other eventscan be processed, so event handlers should be short.Problem is to prevent a long-running low-priority event handlerfrom delaying the handling of a high-priority event.CPSC 427a 8/24Outline GUI gtkmmA GUI event structureA GUI typically translates raw hardware events intosemantically-meaningful software events.For example, a mouse click at particular screen coordinates mightturn into a button-pressed event for some widget in the GUI tree.Several system layers may be involved in this translation, from thekernel processing of hardware interrupts at the bottom level, upthrough device drivers, windowing systems such as X, and finally aGUI frameworks such as GTK+.CPSC 427a 9/24Outline GUI gtkmmInterface between user and system codeA major software challenge is how to design the interface betweenthe GUI and the user code that ultimately deals with the events.In a command-line interface, the user code is at the top level.It connects to the lower layers through familiar function calls.With a GUI, things are turned upside down.IThe top level is the main event loop.IIt connects to the user by calling appropriate user-definedfunctions.CPSC 427a 10/24Outline GUI gtkmmBinding system calls to user functionsHow can one write the GUI to call user functions that did not evenexist when the GUI system itself was written?The basic idea is that of interface.IThe interface is a placeholder for the eventual user functions.IIt describes what functions the user will provide and how tocall them but not what the functions themselves are.IThe interface is bound to user code either when the user codeis compiled or dynamically at runtime.CPSC 427a 11/24Outline GUI gtkmmPolymorphic bindingC++ virtual functions provide an elegant way to bind user code toan interface.IThe GUI can provide a virtual default event handler.IThe user can override the default handler in a dervied class.Of course, the actual binding occurs at run time through the use oftype tags and the vtable as we have seen before.CPSC 427a 12/24Outline GUI gtkmmBinding through callback registrationThe user explicitly registers an event handler with the GUI bycalling a special registration function.IThe GUI keeps track of the event handler(s) registered for aparticular event.IWhen the event happens, it calls all registered event handlers.This is sometimes called a callback mechanism since the user asksto be called back when an event occurs.CPSC 427a 13/24Outline GUI gtkmmCallback using function pointers: GUI sideCallbacks can be done directly in C . Here’s the GUI code:1. Define the signature of the handler function:typedef void (*handler t)(int, int);2. Declare a function pointer in which to save the handler:handler t buttonPressHandlerPtr;3. Define a registration function:void systemRegister(int slot, handler t f) {button press handler ptr = f;}4. Perform the callback:buttonPressHandlerPtr(23, 45);CPSC 427a 14/24Outline GUI gtkmmCallback using function pointer: User sideHere’s how the user attaches a handler to the GUI:1. Create an event handler:void myHandler(int x, int y) {printf("My handler (%i, %i) called\n", x, y);}2. Register the handler for event 17:systemRegister(17, myHandler);CPSC 427a 15/24Outline GUI gtkmmType safetyThe above scheme does not generalize well to multiple events withdifferent signatures.IRegistered handlers need to be stored in some kind ofcontainer.IFor type safety, each different handler signature requires adifferent event container and registration function of thecorresponding signature.The alternative is for systemRegister() to take a void* for itssecond argument and to cast function pointers before call them.This is not type safe and can lead to subtle bugs if the wrong typeof function is attached to a callback slot.CPSC 427a 16/24Outline GUI gtkmmSignals and slotsSignals and slots is a more abstract way of linking events tohandlers and can


View Full Document

Yale CPSC 427 - Graphical User Interfaces

Download Graphical User Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Graphical User Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Graphical User Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?