summaryrefslogtreecommitdiff
path: root/navit/gui/win32/win32_gui_notify.c
blob: 222a7a5a972369526188fad9ba4df9013aa37af8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <glib.h>
#include "win32_gui_notify.h"

struct window_data {
    HWND hwnd;
    UINT message;
    void(*func)(struct datawindow_priv *parent, int param1, int param2);
};

struct notify_priv {
    GList *window_list;
    struct datawindow_priv *parent;

};


void win32_gui_notify(struct notify_priv* notify, HWND hwnd, int message_id, void(*func)(struct datawindow_priv *parent,
                      int param1, int param2)) {
    struct window_data *wnd_data = g_new( struct window_data,1);

    wnd_data->hwnd = hwnd;
    wnd_data->message = message_id;
    wnd_data->func = func;

    notify->window_list = g_list_append( notify->window_list, (gpointer) wnd_data );

}

struct notify_priv* win32_gui_notify_new(struct datawindow_priv *parent) {
    struct notify_priv* notify = g_new0(struct notify_priv,1);
    notify->parent = parent;
    return notify;
}

LRESULT CALLBACK message_handler(HWND hwnd, UINT win_message, WPARAM wParam, LPARAM lParam) {
    enum message_id message = INVALID;
    int param1 = -1;
    int param2 = -1;
    HWND hwndDlg = hwnd;

    switch (win_message) {
    case WM_CREATE: {
        message = WINDOW_CREATE;
    }
    break;
    case WM_SIZE: {
        message = WINDOW_SIZE;
        param1 = LOWORD(lParam);
        param2 = HIWORD(lParam);
    }
    break;
    case WM_DESTROY: {
        message = WINDOW_DESTROY;
    }
    break;
    case WM_NOTIFY: {
        hwndDlg = (((LPNMHDR)lParam)->hwndFrom);
        switch (((LPNMHDR)lParam)->code) {
        case NM_DBLCLK: {
            message = DBLCLICK;
#ifdef LPNMITEMACTIVATE
            param1 = ((LPNMITEMACTIVATE)lParam)->iItem;
#endif
        }
        break;
        case NM_CLICK:
            message = CLICK;
            break;
        }
    }
    break;
    case WM_COMMAND: {
        hwndDlg = (HWND)lParam;

        switch (HIWORD(wParam)) {
        case EN_CHANGE: {
            message = CHANGE;
        }
        break;
        case BN_CLICKED: {
            message = BUTTON_CLICK;
        }
        break;
        }
    }
    break;

    default:
        return DefWindowProc(hwnd, win_message, wParam, lParam);
    }

    struct notify_priv* notify_data = (struct notify_priv*)GetWindowLongPtr( hwnd, DWLP_USER );

    if ( message != INVALID && notify_data && notify_data->window_list ) {

        GList* current_element = g_list_first(notify_data->window_list);


        struct window_data* wnd_data = NULL;
        while (current_element != NULL) {
            wnd_data = current_element->data;

            if ( (wnd_data->hwnd == hwndDlg || wnd_data->hwnd == NULL) && message == wnd_data->message) {
                wnd_data->func(notify_data->parent, param1, param2);
            }

            current_element = g_list_next(current_element);
        }
    }
    return FALSE;
}