Event Driven Programming Presentation

advertisement
Semester overview
CSC 4111 Software Engineering Lab
Winter 2010
1
Schedule
•
•
•
•
•
•
•
Lab 1 – syllabus, tools needed
Lab 2 – Technologies in NotePad++
Lab 3 – using Subversion
Lab 4 – example of a software change
Lab 5 – Change request 1
Lab 6 – using Visio
Lab 7 – group meeting. Q/A about projects
Schedule
•
•
•
•
•
•
•
Lab 8 – CR1 due + change request 2
Lab 9 – unit testing
Lab 10 – group meeting. Q/A about project
Lab 11 – CR 2 due + change request 3
Lab 12 – refactoring
Lab 13 – group meeting. Q/A about project
Lab 14 – CR3 due + overview
Technologies in NotePad++
CSC 4111 Software Engineering Lab
Winter 2010
4
Classical Flow of a Program
• The flow is sequential from beginning to end
(program executes top-bottom).
• The user is invoked by the program.
• The user enters input when the program dictates
so.
• The path varies depending on the input and
conditions.
5
Event Driven Development (EDD)
• EDD – a new paradigm
• Hollywood Principle: "Don't call us; we'll call you."
• The user decides the flow of the program, by
performing a particular action
• The action that the user performs is translated to an
event (keystroke, mouse click, command for windows
repaint, etc.)
• The event is handled in the system by event handlers
• Most common use of EDD: graphical user interfaces
(GUIs)
• Windows programs: Event-driven programming
model.
6
The Handlers Pattern
7
Handlers Pattern:
Event:
• a software message that indicates something has
happened, such as a keystroke or mouse click
Dispatcher:
• takes each event and analyzes it to determine its
type
• sends each event to a handler that can handle
events of that type.
• its logic includes an event loop, as it has to process
a stream of input events
Handler:
• A procedure specialized in handling a specific type
of events
8
Sequence of events
1) The application paints several user interface objects
(e.g.: buttons, text areas, menus, etc.)
2) The application waits in a piece of code (event-loop)
for the user to do something
3) The user does something to the objects on the
screen (e.g.: clicks on a button - > the mouse click is
an event)
4) The user interface object updates its appearance on
the screen and calls a pre-arranged function in the
system, which contains the code that implements the
action for the GUI object
9
Win32 Application
• WndProc – dispatcher for each window
• Standard Messages:
–
–
–
–
WM_CREATE,
WM_SIZE,
WM_COMMAND,
WM_MENUSELECT
• resource.h contains the definitions of the new
user messages
– IDM_FILE_NEW
– IDM_FILE_OPEN
– IDM_EDIT_COPY
How to send and receive messages
• ::SendMessage function takes 4 parameters:
– LRESULT SendMessage(
HWND hWnd, UINT Msg, WPARAM wParam, LPAR
AM lParam );
– hWnd – handler to the window (mainwindow, combobox,
dialogbox, checkbox, radiobutton, etc.)
– Msg – the message to send
• Depending on the message the value in the window can read or
written
– wParam, lParam – parameters of the message
• Variables to store the value, value to be stored
• Use SendMessage when you are dealing with dialog
windows related to the menu
Windows application
• Hello world
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
return 0;
}
WinMain – the equivalent of the function main in Console applications
HINSTANCE hInstance - Handle to the programs executable module
(the .exe file in memory)
HINSTANCE hPrevInstance - Always NULL for Win32 programs.
LPSTR lpCmdLine - The command line arguments as a single string.
NOT including the program name.
int nCmdShow - An integer value which may be passed
to ShowWindow(). We'll get to this later.
Task 1 – simple window
•
•
•
•
Create an Empty project in Visual Studio 2008
Create a new cpp file in the project
Add the code from task1.cpp
Compile and run
Create and register your window
• Let’s create a real window
– Step 1: Register the Window Class
– Step 2: Create the Window
– Step 3: The Message Loop
– Step 4: The Window Procedure
Task 2
•
•
•
•
Create an Empty project in Visual Studio 2008
Create a new cpp file in the project
Add the code from task2.cpp
Compile and run
Windows Queue
Windows
Queue
Message loop
• A message is an integer value.
– Look for their definition in the header files
#define IDM_FILE_NEW
40001
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Task 3 – find the message loop in
NotePad++
• Which file contains the message loop in
Notepad++?
Receiving messages
• All the messages are received using the Window
procedure
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
Task 4 - Window Procedure
• The window procedure is called for each message (CALLBACK)
• Each window has its own window procedure
• Task 4: add the following message in the window from task 2
case WM_LBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK |
MB_ICONINFORMATION);
}
• Task 5: Name at least 5 files in Notepad++ where a Window
procedure is defined?
Sending messages
• ::SendMessage function takes 4 parameters:
– LRESULT SendMessage(
HWND hWnd, UINT Msg, WPARAM wParam, LPAR
AM lParam );
– hWnd – handler to the window (mainwindow, combobox,
dialogbox, checkbox, radiobutton, etc.)
– Msg – the message to send
• Depending on the message the value in the window can read or
written
– wParam, lParam – parameters of the message
• Variables to store the value, value to be stored
• Use SendMessage when you are dealing with dialog
windows related to the menu
What parameters to use
• Select the text in a combobox:
– ::SendMessage(hCombo, CB_SETCURSEL,
(WPARAM)-1, 0)
– ::SendMessage(hCombo, CB_SETEDITSEL, 0,
MAKELPARAM(0, -1));
• Task 6: check in MSDN what parameters to
use for the two messages above
How to show a window
• After the window is created, it is not visible
– Use showWindow or display to show a main window
• You can select and work with other windows in the same tim
– use doDialog to show a dialog,
• The window will be displayed on top, no other window can
be selected
• ::ShowWindow(::GetDlgItem(_hSelf,
IDCMARKALL), isEnable?SW_HIDE:SW_SHOW);
• _aboutDlg.doDialog();
How to enable a window
• ::EnableWindow(::GetDlgItem(_hSelf,
IDBOOLOR), FALSE);
• EnableWindows takes 2 parameters
– The first parameter is a reference to a window
– The second parameter is a boolean value
• GetDlgItem returns a reference to the window
(controller) with the given id from the parent
window _hself
– IDBOOLOR – is a radio button created by one of you
– _hself – refers to findReplaceDlg window
What to submit
•
•
•
•
•
Task 1 – Hello World ( 20 points)
Task 2 – Simple Window (20 points )
Task 3 – find the message loop in NotePad++ (20 points)
Task 4: add the following message in the window from task 2 ( 20 points)
Task 5: Name at least 5 files in Notepad++ where a Window procedure is
defined? (10 points)
• Task 6: check in MSDN what parameters to use for the two messages
above (10 points)
Extra credit (20 points)
• Task 7: How many parameters do you have to specify for the message
SCI_FINDCOLUMN? What is the meaning of these parameters? What does
the following function call return?
Submit the lab!
• Complete the tasks by January 27th – 7:30PM
• Send all the answers as a zip file trough
blackboard
• If you missed the class contact the instructor
Extra slides
Winter 2010
27
Scintilla library
• Contains functions which allow the user to
avoid the usage of SendMessage function
– Function “execute” from the class
ScintillaEditView
LRESULT execute(UINT Msg, WPARAM wParam=0, LPARAM
lParam=0) const {
return _pScintillaFunc(_pScintillaPtr, static_cast<int>(Msg),
static_cast<int>(wParam), static_cast<int>(lParam));
};
– The user only need to specify the message
int caretPos = execute(SCI_GETCURRENTPOS);
Demo
• A simple application that uses Scintilla library
Other tips
• Menus, windows, GUI in general are defined
in *.rc files
• Use search in files to grep for string in these
files
• cxx and hpp are extensions of C++ files, they
are equivalent with cpp and header files
• http://www.scintilla.org/ScintillaDoc.html
NotePad++ example – how to find the
maker type
• During concept location I found the message:
SCI_INDICATORVALUEAT
• The change request was to go the next marked
area
• I looked in scintilla.h and I found the message:
SCI_INDICATORVALUEAT
• Question: How to use this message
• Execute(SCI_INDICATORVALUEAT) always returns
the same number (1)
How to find the implementation of a
message in Scintilla project
• Search for SCI_INDICATORVALUEAT in SciLexer
project. Found:
case SCI_INDICATORVALUEAT:
return pdoc->decorations.ValueAt(wParam, lParam);
• Both wParam and lParam are used
– The implementation of the method ValueAt
int DecorationList::ValueAt(int indicator, int position) {
Decoration *deco = DecorationFromIndicator(indicator);
if (deco) {
return deco->rs.ValueAt(position);
}
return 0;
}
Getting the value
int currentposition = _pEditView->execute(SCI_GETCURRENTPOS);
LRESULT style1 = _pEditView->execute(SCI_INDICATORVALUEAT,
SCE_UNIVERSAL_FOUND_STYLE_EXT1, currentposition);
• If the current position is marked with style 1 the value is
of style1 is 1, otherwise it is 0
• Task 7: How many parameters do you have to specify for
the message SCI_FINDCOLUMN? What is the meaning of
these parameters? What does the following function call
return?
(*_ppEditView)->execute(SCI_FINDCOLUMN, i, j)
Download