Yingcai Xiao 10/01/2008: (Not in The Textbook)
Yingcai Xiao 10/01/2008: (Not in The Textbook)
Programming Paradigms
Sequential: Program statements are executed one at a time. The order of execution is predetermined by the programmer. The user has no or little control of the order. Not user friendly. Event-driven: Program idles after initialization, waits for events. When an event is generated by the user, it will be sent to the program to be processed. The event is first send to the event-mapper, which dispatches the event to its handler. The event-handler process the event. The program goes to idle mode again. The order of events is not predetermined by the programmer. The user can select what the program should do next. User friendly.
Programming Paradigms
Text-based: Users issue commands as texts. Users have to remember the texts of the commands and their format. Not user friendly. GUI-based: Users issue commands by point-and-click on GUI (Graphical User Interface) objects. Users do not have to remember the texts of the commands (but have to remember where the GUI items are). WYSIWYG (What you see is what you get). User friendly.
Text GUI
least no use
modest most
EDP: The application continuously waits until interrupted by some input events to respond. The application has an event-handler for each event it wants to handle. The event-handler is invoked when the corresponding event occurs.
GUI-EDP: Application waits (idles) until the user generates an event trough an input device (keyboard, mouse, ). The OS dispatches the event to the application who owns the window. The corresponding event handler(s) of the application is invoked to process the event.
Text-Based EDP
#include <iostream> using namespace std; int value; // global int main() { // Initialization char s = '+'; value = 0; while(1) { // event loop cout << "Please select an operation (+,-,q): \n"; cin >> s; //wait for an event to be generated by the user switch(s){ //event mapper case '+': //event registration add(); break; //event handler case '-': //event registration sub(); break; //event handler case 'q': //event registration exit(1); //event handler } } return(1); }
Text-Based EDP
// event handlers void add () { // "+" event handler int in; cout << "Please select an integer: \n"; cin >> in; value += in; cout << "The current value is: " << value << "\n"; }
void sub () { // "-" event handler int in; cout << "Please select an integer: \n"; cin >> in; value -= in; cout << "The current value is: " << value << "\n"; }
Event-driven Programming
The six major components of an EDP program
1. 2. 3. 4. Event generators: keyboard and mouse Events: system and user defined Event loop: continuously waits for events Event handlers: methods to process the events (mostly user defined, some system defined) 5. Event mapper: dispatches events to their corresponding event handlers 6. Event registration: inform the event mapper which event an event hander is for.
EDP: Introduction
App
: UI
: Event
: Event
User
Object
Mapper
registers event handler
Handler
processes events
Event-driven Programming
The major task of EDP is to write the event handlers. event loop and event mapper are usually implemented by the system applications only implement handlers as desired handlers need to follow the standard interfaces handlers need to be registered
EDP: Benefits
Promotes code reuse. Event loop and event mapper can be shared by all applications. Event handlers have standardized interfaces and can be shared by different applications. Some are built by the system. OOP makes it easier to implement EDP: Events are defined as classes, easier to pass around and easier to modify. Java and C# have EDP components (loop, mapper, system events, event generators) built in C# defines event as a built-in type and provides delegate for type-safe registration of event handlers.
GUI-EDP
GUI-EDP Key Components of GUI-EDP: (1) GUI items (buttons, menus, ). (2) Events / Messages (Mouse Enter, Key Down, ) (3) Event Loop (an infinite loop constantly waits for events) (4) Event Handlers (methods for processing the events: OnMouseEnter(), ) (5) Event Mapper (dispatches events to corresponding event handlers) (6) Event Registration: inform event mapper which event an event hander is for.
C++ has no language-level support for GUI or EDP. MS Visual Studio: GUI-based IDE (Integrated Development Environment). IDE - Integrated Development Environment includes: editor, compiler, linker, loader, debugger, profiler, context-sensitive help, form designer. MS Visual Studio supports the development of text, text-EDP and GUI-EDP applications. GDI (Graphical Device Interface): API to the graphics hardware.
MFC-defined class CWinApp is the parent of all applications. It contains: main event loop event mapper/dispatcher
Dialog GUI-EDP with MS Visual Studio 2005 Start with a dialog application.
Dialog GUI-EDP with MS Visual Studio 2005 All the basic files of a do-nothing dialog app are created. Build->Build Solution Debug->Start Without Debugging Or double click: My Documents\Visual Studio 2005\Projects\oopdialog\debug\oop-dialog.exe
MFC-based Dialog App : Understanding the Code Source Files (Class Implementation Files) oop-dialog.cpp (the app) oop-dialogDlg.cpp (the dialog) sdtafx.cpp (the standard AFX file for precompiled headers) Resource Files (for GUI) oop-dialog.ico (the app icon) oop-dialog.rc(2) (the app resource files containing specifications of GUI items.) Header Files (Class Declaration Files) oop-dialog.h (the app class definition) oop-dialogDlg.h (the dialog class definition) sdtafx.h (the standard AFX precompiled header) Resource.h (define GUI item IDs)
};
MFC generated event handlers have predefined headers. They must be named as OnMessage, e.g. OnPaint is the handler for the PAINT message. Other app specific event handlers are declared in DECLARE_MESSAGE_MAP() All application event handlers are implemented in oop-dialogDlg.h.
Understanding the Code: oop-dialogDlg.cpp CoopdialogDlg::CoopdialogDlg(CWnd* pParent /*=NULL*/) : CDialog(CoopdialogDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } // Exchange data between the dialog internal structure and the app void CoopdialogDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CoopdialogDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP END_MESSAGE_MAP()
Understanding the Code: oop-dialogDlg.cpp BOOL CoopdialogDlg::OnInitDialog() { CDialog::OnInitDialog(); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } return TRUE; }
void CoopdialogDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } }
Understanding the Code: oop-dialogDlg.cpp void CoopdialogDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; dc.DrawIcon(x, y, m_hIcon); }
}
} // The system calls this function to obtain the cursor to display while the user drags the minimized window. HCURSOR CoopdialogDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); }
Understanding the Code: oop-dialogDlg.cpp class CAboutDlg : public CDialog { // Dialog Data enum { IDD = IDD_ABOUTBOX }; // DDX/DDV support protected: virtual void DoDataExchange(CDataExchange* pDX); // Implementation protected: DECLARE_MESSAGE_MAP() };
Multiple Documents Application with MS Visual Studio 2005 Start->Program Files-> MS Visual Studio 2005-> MS Visual Studio 2005 File->New->Project->Other Languages->MFC->MFC Application Name: oop-md Application Types: Multiple documents (e.g. MS Word) Pick a type and follow the instructions or just pick Finish.
MFC-defined class CWinApp is the parent of all applications. It contains: main event loop event mapper/dispatcher This is an example of Application Framework / Template Method