Graphical User Interfaces: Bjarne Stroustrup
Graphical User Interfaces: Bjarne Stroustrup
Bjarne Stroustrup
www.stroustrup.com/Programming
Overview
Perspective
callbacks
Stroustrup/Programming
I/O alternatives
A strong contender for technical/professional work Command line interface Menu driven interface Use a GUI Library To match the feel of windows/Mac applications When you need drag and drop, WYSIWYG Event driven program design A web browser this is a GUI library application
Stroustrup/Programming
Titles / Text
Input Output Let the user initiate actions Let the user select among a set of alternatives
Buttons
Display results
More advanced
GUI
Object-oriented programming
For organizing program parts with common interfaces and common actions For connecting an event (like a mouse click) with a program action
Events
Stroustrup/Programming
Layers of software
Provides services
Uses services
Stroustrup/Programming
GUI example
Window with
Stroustrup/Programming
GUI example
GUI example
When you hit next point that point becomes the current (x,y) and is displayed in the Out_box
Stroustrup/Programming 10
GUI example
GUI example
GUI example
Stroustrup/Programming
13
How do we define a window? How do we define buttons? How do we define input and output boxes? How do we program that action? How do we connect our code to the button? How do we get that value into our code? How do we convert from a string to numbers? How do we get the values there? How do we store the lines? How do we draw them?
Stroustrup/Programming 14
Mapping
We map our ideas onto the FTLK version of the conventional Graphics/GUI ideas
Stroustrup/Programming
15
Lines_window(Point xy, int w, int h, const string& title); // declare constructor Open_polyline lines; private: Button next_button; Button quit_button; In_box next_x; In_box next_y; Out_box xy_out; void next(); void quit();
static void cb_next(Address, Address window); // callback for next_button static void cb_quit(Address, Address window); // callback for quit_button };
Stroustrup/Programming 16
GUI example
Window with
Stroustrup/Programming
17
A Widget is something you see in the window which has an action associated with it
A Button is a Widget that displays as a labeled rectangle on the screen, and when you click on the button, a Callback is triggered
A Callback connects the button to some function or functions (the action to be performed)
Stroustrup/Programming
19
Stroustrup/Programming
20
How it works
Window
Describe where the button is Describe what the button looks like Register Buttons callback
Attach Button
Our code
FLTK
Call callback when Button is pressed
Stroustrup/Programming
21
GUI example
Widget
Basically anything you can see on the screen and do something with is a widget (also called a "control")
struct Widget { Widget(Point xy, int w, int h, const string& s, Callback cb) :loc(xy), width(w), height(h), label(s), do_it(cb) {} // connection to FLTK };
Stroustrup/Programming
23
Button
struct Button : Widget { Button(Point xy, int w, int h, const string& s, Callback cb) :Widget(xy,w,h,s,cb) { } };
Stroustrup/Programming
24
Callback
Connecting functions to widgets is messy in most GUIs It need not be, but
the system does not know about C++ the style/mess comes from systems designed in/for C/assembler Major systems always use many languages, this is one example of how to cross a language barrier
void Lines_window::cb_quit(Address, Address pw) // Call Lines_window::quit() for the window located at address pw { reference_to<Lines_window>(pw).quit(); // now call our function }
Map an address into a reference to the type of object residing at that address to be explained the following chapters
Stroustrup/Programming 25
Stroustrup/Programming
26
Stroustrup/Programming
27
In_box
// An In_box is a widget into which you can type characters // Its action is to receive characters
struct In_box : Widget { In_box(Point xy, int w, int h, const string& s) :Widget(xy,w,h,s,0) { } int get_int(); string get_string(); };
int In_box::get_int() { // get a reference to the FLTK FL_Input widget: Fl_Input& pi = reference_to<Fl_Input>(pw); // use it: return atoi(pi.value()); // get the value and convert // it from characters (alpha) to int }
Stroustrup/Programming
28
Summary
We have seen Action on buttons Interactive I/O Text input Text output Graphical output Missing Menu (See Section 16.7) Window and Widget (see Appendix E) Anything to do with tracking the mouse Dragging Hovering Free-hand drawing What we havent shown, you can pick up if you need it
Stroustrup/Programming
29
Next lecture
The next three lectures will show how the standard vector is implemented using basic low-level language facilities. This is where we really get down to the hardware and work our way back up to a more comfortable and productive level of programming.
Stroustrup/Programming
30