source: trunk/MFCtooling/replay/WindowFinder.cpp @ 32

Last change on this file since 32 was 32, checked in by sherbold, 13 years ago
File size: 3.2 KB
Line 
1#include "StdAfx.h"
2#include "WindowFinder.h"
3
4#include <algorithm>
5#include <iostream>
6
7BOOL CALLBACK EnumChildren(HWND hwnd, LPARAM lParam) {
8        int resourceId = 0;
9        int windowTextLen = 128;
10        wchar_t * windowText = new wchar_t[128];
11        int classNameLen = 128;
12        wchar_t * className = new wchar_t[128];
13
14        WindowFinder * finder = (WindowFinder*) lParam;
15
16        resourceId = GetDlgCtrlID(hwnd);
17        windowTextLen = GetWindowText(hwnd, windowText, windowTextLen);
18        classNameLen = GetClassName(hwnd, className, classNameLen);
19
20        finder->setEqualityScore(hwnd,resourceId, windowText, className);
21       
22        return TRUE;
23}
24
25WindowFinder::WindowFinder()
26{
27        evalPopup = false;
28        parentHandles = NULL;
29}
30
31WindowFinder::~WindowFinder(void)
32{
33}
34
35HWND WindowFinder::find(WindowData * winData) {
36        currentWindow = winData;
37        maxScore = 0;
38        scores = new std::vector<std::pair<HWND, int>>();
39
40        if( parentHandles==NULL ) {
41                EnumWindows(EnumChildren, (LPARAM) this);
42        } else {
43                if( winData->isModal ) {
44                        evalPopup = true;
45                        EnumWindows(EnumChildren, (LPARAM) this);
46                        evalPopup = false;
47                } else {
48                        for( size_t i=0 ; i<parentHandles->size() ; i++ ) {
49                                EnumChildWindows((*parentHandles)[i], EnumChildren, (LPARAM) this);
50                        }
51                }
52                delete parentHandles;
53        }
54        parentHandles = new std::vector<HWND>();
55        for( size_t i=0 ; i<scores->size() ; i++ ) {
56                if( (*scores)[i].second==maxScore ) {
57                        parentHandles->push_back((*scores)[i].first);
58                }
59        }
60        delete scores;
61       
62        HWND result = NULL;
63
64        if( winData->child!=NULL ) {
65                if( maxScore==0 ) {
66                        std::wcerr << L"Warning: Score is zero." << std::endl;
67                }
68                result = find(winData->child);
69        } else {
70                if( parentHandles->size()==0 ) {
71                        std::wcerr << L"Error: handle not found." << std::endl;
72                        result = NULL;
73                }
74                else {
75                        if( parentHandles->size()!=1 ) {
76                                std::wcerr << L"Warning: more than one fitting window found." << std::endl;
77                        }
78                        if( maxScore==0 ) {
79                                std::wcerr << L"Warning: Score is zero." << std::endl;
80                        }
81                        result = (*parentHandles)[0];
82                        delete parentHandles;
83                }
84        }
85        return result;
86}
87
88
89void WindowFinder::setEqualityScore(HWND hwnd, int resourceId, wchar_t * windowName, wchar_t * className) {
90        int score = 0;
91
92        if( resourceId!=0 && currentWindow->resourceId==resourceId && currentWindow->name.compare(windowName)==0 && currentWindow->className.compare(className)==0 ) {
93                score = 6;
94        }
95        else if( resourceId!=0 && currentWindow->resourceId==resourceId && currentWindow->name.compare(windowName)==0 ) {
96                score = 5;
97        }
98        else if( resourceId!=0 && currentWindow->resourceId==resourceId ) {
99                score = 4;
100        }
101        else if( currentWindow->name.compare(windowName)==0 && currentWindow->className.compare(className)==0 ) {
102                score = 3;
103        }
104        else if( currentWindow->name.compare(windowName)==0 ) {
105                score = 2;
106        }
107        else if( currentWindow->className.compare(className)==0 ) {
108                score = 1;
109        }
110        if( evalPopup ) {
111                HWND parent = GetParent(hwnd);
112                if( std::find(parentHandles->begin(), parentHandles->end(), parent)==parentHandles->end() ) {
113                        score = 0;
114                }
115        }
116
117        scores->push_back(std::make_pair(hwnd, score));
118
119        if( score>maxScore ) {
120                maxScore = score;
121        }
122}
123
124bool WindowFinder::isCurrentPopup() {
125        return evalPopup;
126}
Note: See TracBrowser for help on using the repository browser.