Rev 124 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | // dialog_comment.cpp |
2 | |||
3 | #include "../common.h" |
||
4 | |||
5 | |||
6 | // dialog template |
||
7 | #define THIS_DIALOG DIALOG_COMMENT |
||
8 | |||
9 | |||
10 | // prototypes of local functions |
||
11 | static void StartThread_ThisDialog (void *thread_parms); |
||
12 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); |
||
13 | |||
14 | |||
15 | void DialogBox_Comment (void) |
||
16 | { |
||
17 | // helper function to fire up the modeless dialog box |
||
18 | |||
19 | _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one |
||
20 | |||
21 | return; // return as soon as the thread is fired up |
||
22 | } |
||
23 | |||
24 | |||
25 | void DialogBox_Comment_Validated (void) |
||
26 | { |
||
27 | // callback function called by the main game thread when the dialog box is validated |
||
28 | |||
29 | // remember this callback is no longer to be called |
||
30 | is_dialogbox_comment_validated = false; |
||
31 | |||
32 | return; // finished |
||
33 | } |
||
34 | |||
35 | |||
36 | static void StartThread_ThisDialog (void *thread_parms) |
||
37 | { |
||
38 | // this function runs in a separate thread, for that's the only way (seemingly) |
||
39 | // to implement a non-modal message box using the Common Controls library. |
||
40 | |||
41 | // display the dialog box |
||
42 | if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1) |
||
43 | is_dialogbox_comment_validated = true; |
||
44 | |||
45 | return; // _endthread() implied |
||
46 | } |
||
47 | |||
48 | |||
49 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) |
||
50 | { |
||
51 | // message handler for the dialog box |
||
52 | |||
53 | unsigned short wParam_hiword; |
||
54 | unsigned short wParam_loword; |
||
55 | int new_size; |
||
56 | |||
57 | // filter out the commonly used message values |
||
58 | wParam_hiword = HIWORD (wParam); |
||
59 | wParam_loword = LOWORD (wParam); |
||
60 | |||
61 | // have we just fired up this window ? |
||
62 | if (message == WM_INITDIALOG) |
||
63 | { |
||
64 | // center the window |
||
65 | CenterWindow (hWnd, hMainWnd); |
||
66 | |||
67 | // set dialog icons (small one for title bar & big one for task manager) |
||
68 | SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN))); |
||
69 | SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN))); |
||
70 | |||
71 | // set window title and control texts |
||
72 | SetWindowText (hWnd, LOCALIZE (L"Comment_Title")); |
||
73 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_COMMENT_QUESTION), LOCALIZE (L"Comment_Question")); |
||
74 | SetWindowText (GetDlgItem (hWnd, BUTTON_OK), LOCALIZE (L"Button_OK")); |
||
75 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_COMMENT_STATUSBAR), LOCALIZE (L"Comment_StatusBar")); |
||
76 | |||
77 | // set the edit box comment, select all text and send focus to it |
||
78 | if ((the_board.move_count > 1) && (the_board.viewed_move >= 1) && (the_board.viewed_move < the_board.move_count) |
||
79 | && (the_board.moves[the_board.viewed_move].comment != NULL) && (the_board.moves[the_board.viewed_move].comment[0] != 0)) |
||
80 | { |
||
81 | SetWindowText (GetDlgItem (hWnd, EDITBOX_COMMENT), the_board.moves[the_board.viewed_move].comment); |
||
82 | SendMessage (GetDlgItem (hWnd, EDITBOX_COMMENT), EM_SETSEL, 0, -1); |
||
83 | } |
||
84 | SetFocus (GetDlgItem (hWnd, EDITBOX_COMMENT)); |
||
85 | |||
86 | // convert the status bar message to a hyperlink |
||
87 | ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_COMMENT_STATUSBAR)); |
||
88 | } |
||
89 | |||
90 | // else did we click the close button on the title bar ? |
||
91 | else if (message == WM_CLOSE) |
||
92 | EndDialog (hWnd, 0); // close the dialog box |
||
93 | |||
94 | // else did we take action on one of the controls ? |
||
95 | else if (message == WM_COMMAND) |
||
96 | { |
||
97 | // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key) |
||
98 | if (wParam_loword == IDCANCEL) |
||
99 | EndDialog (hWnd, 0); // close the dialog box |
||
100 | |||
101 | // else was it the OK button ? |
||
102 | else if (wParam_loword == BUTTON_OK) |
||
103 | { |
||
104 | // grab the edit box comment |
||
105 | if ((the_board.move_count > 1) && (the_board.viewed_move >= 1) && (the_board.viewed_move < the_board.move_count)) |
||
106 | { |
||
107 | // get comment length |
||
108 | new_size = GetWindowTextLength (GetDlgItem (hWnd, EDITBOX_COMMENT)); |
||
109 | |||
110 | // was there text at all ? |
||
111 | if (new_size > 0) |
||
112 | { |
||
113 | // if so, reallocate (add one for the terminator character) and copy comment |
||
114 | the_board.moves[the_board.viewed_move].comment = (wchar_t *) SAFE_realloc (the_board.moves[the_board.viewed_move].comment, the_board.moves[the_board.viewed_move].comment_size, new_size + 1, sizeof (wchar_t), false); |
||
115 | GetWindowText (GetDlgItem (hWnd, EDITBOX_COMMENT), the_board.moves[the_board.viewed_move].comment, new_size + 1); |
||
116 | the_board.moves[the_board.viewed_move].comment_size = new_size + 1; // remember comment size |
||
117 | } |
||
118 | else |
||
119 | { |
||
120 | SAFE_free ((void **) &the_board.moves[the_board.viewed_move].comment); // comment was erased, free the memory space used for it |
||
121 | the_board.moves[the_board.viewed_move].comment_size = 0; // and reset its size to zero |
||
122 | } |
||
123 | } |
||
124 | |||
125 | EndDialog (hWnd, 1); // close the dialog box and return OK |
||
126 | } |
||
127 | |||
128 | // else was it the status bar hyperlink ? |
||
129 | else if (wParam_loword == STATICTEXT_COMMENT_STATUSBAR) |
||
130 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized |
||
131 | } |
||
132 | |||
133 | // call the default dialog message processing function to keep things going |
||
134 | return (false); |
||
135 | } |