CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a...
-
Upload
ilene-hood -
Category
Documents
-
view
212 -
download
0
Transcript of CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a...
CNS 1410Graphical User Interfaces
Obectives
• Students should understand the difference between a procedural program and an Event Driven Program. • Students should be able to describe the basic components of a GUI.• Students should be able to create a simple GUI, using wxWidgets• Students should be able to describe the event handling mechanism of wxWidgets.
Traditional Model – Command Line Programs
Object
Object
main ( ){ declare variables create objects send messages to objects . . . send messages to objects . . . return 0}
Graphical User Interface Model
ApplicationObject
FrameObject
Domain Objects
Graphical User Interface Model
ApplicationObject
The application object “listens”for events to occur, then callsevent handlers in the FrameObject.
Graphical User Interface Model
FrameObject
GUIComponent
GUI Component (widgets)generate events.
Event handlers reactto events
GUIComponent
GUIComponent
xFile Edit View Help
Name:
Phone:
PUSH
GUI Components
Frame
Graphical User Interface Model
Domain Objects
Domain objectsdo the work ofthe application
File Edit View Help
PUSH
ApplicationObject
WindowsEvent Queue
ButtonEvent
ApplicationObject
WindowsEvent Queue
FrameObject
ButtonEvent
ApplicationObject
WindowsEvent Queue
FrameObject
ButtonEvent
Call event handler
Graphical User Interfaces
ApplicationObject
WindowsEvent Queue
FrameObject
ButtonEvent
Call event handler
Domain Objects
Call functions indomain objectsto do some work.
#include <wx/wx.h>class BasicApplication : public wxApp{public:
bool OnInit( );};
ApplicationObject
The application class only needs oneFunction named OnInit. This functionis used to create the User Interface.
bool BasicApplication::OnInit( ){ BasicFrame *frame = new BasicFrame("Hello World", 50, 50, 450, 340); frame->Show(true); SetTopWindow(frame); return TRUE;}
This statement creates a Frame object. Framesrepresent windows. By default, Frame objectscontain all of the function of a simple window.You can drag them, resize them, and close them.
bool BasicApplication::OnInit( ){ BasicFrame *frame = new BasicFrame("Hello World", 50, 50, 450, 340); frame->Show(true); SetTopWindow(frame); return TRUE;}
This statement makes the Frame object visible.
bool BasicApplication::OnInit( ){ BasicFrame *frame = new BasicFrame("Hello World", 50, 50, 450, 340); frame->Show(true); SetTopWindow(frame); return TRUE;}
This statement makes the Frame the topmostFrame.
#include <wx/wx.h>class BasicFrame : public wxFrame{public: BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height); void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event);private:
DECLARE_EVENT_TABLE( ) };
Event handlers
BEGIN_EVENT_TABLE(BasicFrame, wxFrame) EVT_MENU(ID_QUIT, BasicFrame::OnQuit) EVT_MENU(ID_ABOUT, BasicFrame::OnAbout)END_EVENT_TABLE( )
The Event Table for the Frame
When you receive an event named ID_QUIT, call thefunction OnQuit in the BasicFrame class.
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructortext for title bar
Position of upper left hand corner of thewindow on the screen
window size
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), // wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructor
pointer to parent (containing) class
ID – default is -1
title bar text
position// size
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructor
create a menu object
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructor
Define the menuitems in the menu
AboutExit
generate an event with this name
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructor
Define a MenuBar object
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructor
AboutQuit
File
Store the Menu object in the MenuBar and label it.
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructor
Make the MenuBar object thecurrently active menu bar.
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructor
Create a StatusBar object
BasicFrame::BasicFrame(const wxChar *title, int xpos, int ypos, int width, int height) : wxFrame( (wxFrame *)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height)) { wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_ABOUT, "&About..."); menuFile->AppendSeparator( ); menuFile->Append(ID_QUIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar( ); SetStatusText("Welcome to wxWidgets"); }
Frame Constructor
Store this text in the status bar
The Event Handlers
void BasicFrame::OnQuit(wxCommandEvent& event) { Close(TRUE); } void BasicFrame::OnAbout(wxCommandEvent& event) { wxMessageBox("The Hello World Sample", "About Hello World", wxOK | wxICON_INFORMATION); }
Closes the window and terminates the application.
Finishing Touches
enum{ ID_QUIT = 1, ID_ABOUT};
Use an enumeration to define event names
IMPLEMENT_APP(BasicApplication)
This macro starts the Windows event loop running
Write the code using the minGW Compiler
This code is available on the course web site as part of Lab #6.