static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { int redraw = 0; switch (message) { case WM_CHAR: { switch (wparam) { case '+': the_scale *= 1.2f; redraw = 1; break; case '-': the_scale /= 1.2f; redraw = 1; break; case 'f': case 'F': { float wscale, hscale; wscale = win_width / (float)the_width; hscale = win_height / (float)the_height; the_scale *= wscale > hscale ? hscale : wscale; redraw = 1; break; } case '1': the_scale = 1.0; redraw = 1; break; case 'q': case 'Q': finish = 1; break; } break; } case WM_PAINT: { HFONT font; BeginPaint(hwnd, &the_output.ps); font = CreateFont(fontsize, 0, 0, 0, 0, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL); SelectObject(the_output.ps.hdc, (HGDIOBJ) font); windows_box(&the_output, 0xff, 0xff, 0xff, 0, 0, win_width, 0, win_height); the_output.max_x = 0; the_output.max_y = 0; output_draw(&the_output.loutput); the_width = the_output.max_x; the_height = the_output.max_y; DeleteObject(font); EndPaint(hwnd, &the_output.ps); break; } case WM_LBUTTONDOWN: state = 1; x = GET_X_LPARAM(lparam); y = GET_Y_LPARAM(lparam); break; case WM_LBUTTONUP: state = 0; break; case WM_MOUSEMOVE: if (!(wparam & MK_LBUTTON)) state = 0; if (state) { int new_x = GET_X_LPARAM(lparam); int new_y = GET_Y_LPARAM(lparam); x_delta -= new_x - x; y_delta -= new_y - y; x = new_x; y = new_y; redraw = 1; } break; case WM_KEYDOWN: switch (wparam) { case VK_ESCAPE: finish = 1; break; case VK_LEFT: x_delta -= win_width/10; redraw = 1; break; case VK_RIGHT: x_delta += win_width/10; redraw = 1; break; case VK_UP: y_delta -= win_height/10; redraw = 1; break; case VK_DOWN: y_delta += win_height/10; redraw = 1; break; case VK_PRIOR: if (control) { x_delta -= win_width; redraw = 1; } else { y_delta -= win_height; redraw = 1; } break; case VK_NEXT: if (control) { x_delta += win_width; redraw = 1; } else { y_delta += win_height; redraw = 1; } break; case VK_HOME: x_delta = 0; y_delta = 0; redraw = 1; break; case VK_END: x_delta = INT_MAX; y_delta = INT_MAX; redraw = 1; break; case VK_CONTROL: control = 1; break; } break; case WM_KEYUP: switch (wparam) { case VK_CONTROL: control = 0; break; } break; case WM_DESTROY: /* only kill the program if closing the actual toplevel, not the fake one */ if (hwnd == the_output.toplevel) PostQuitMessage(0); return 0; case WM_SIZE: { float wscale, hscale; win_width = LOWORD(lparam); win_height = HIWORD(lparam); wscale = win_width / (float)the_width; hscale = win_height / (float)the_height; the_scale *= wscale > hscale ? hscale : wscale; if (the_scale < 1.0f) the_scale = 1.0f; redraw = 1; break; } } if (redraw) { if (x_delta > the_width - win_width) x_delta = the_width - win_width; if (y_delta > the_height - win_height) y_delta = the_height - win_height; if (x_delta < 0) x_delta = 0; if (y_delta < 0) y_delta = 0; fontsize = (unsigned)(the_fontsize * the_scale); gridsize = (unsigned)(the_gridsize * the_scale); RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE); } return DefWindowProc(hwnd, message, wparam, lparam); }
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { int redraw = 0; switch (message) { case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); windows_box(&ps, 0xff, 0xff, 0xff, 0, 0, win_width, 0, win_height); output_draw(&windows_draw_methods, the_logical, the_legend, the_topology, &ps); EndPaint(hwnd, &ps); break; } case WM_LBUTTONDOWN: state = 1; x = GET_X_LPARAM(lparam); y = GET_Y_LPARAM(lparam); break; case WM_LBUTTONUP: state = 0; break; case WM_MOUSEMOVE: if (!(wparam & MK_LBUTTON)) state = 0; if (state) { int new_x = GET_X_LPARAM(lparam); int new_y = GET_Y_LPARAM(lparam); x_delta -= new_x - x; y_delta -= new_y - y; x = new_x; y = new_y; redraw = 1; } break; case WM_KEYDOWN: switch (wparam) { case 'q': case 'Q': case VK_ESCAPE: finish = 1; break; case VK_LEFT: x_delta -= win_width/10; redraw = 1; break; case VK_RIGHT: x_delta += win_width/10; redraw = 1; break; case VK_UP: y_delta -= win_height/10; redraw = 1; break; case VK_DOWN: y_delta += win_height/10; redraw = 1; break; case VK_PRIOR: if (control) { x_delta -= win_width; redraw = 1; } else { y_delta -= win_height; redraw = 1; } break; case VK_NEXT: if (control) { x_delta += win_width; redraw = 1; } else { y_delta += win_height; redraw = 1; } break; case VK_HOME: x_delta = 0; y_delta = 0; redraw = 1; break; case VK_END: x_delta = INT_MAX; y_delta = INT_MAX; redraw = 1; break; case VK_CONTROL: control = 1; break; } break; case WM_KEYUP: switch (wparam) { case VK_CONTROL: control = 0; break; } break; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_SIZE: win_width = LOWORD(lparam); win_height = HIWORD(lparam); redraw = 1; break; } if (redraw) { if (x_delta > the_width - win_width) x_delta = the_width - win_width; if (y_delta > the_height - win_height) y_delta = the_height - win_height; if (x_delta < 0) x_delta = 0; if (y_delta < 0) y_delta = 0; if (win_width > the_width && win_height > the_height) { fontsize = the_fontsize; gridsize = the_gridsize; if (win_width > the_width) { fontsize = the_fontsize * win_width / the_width; gridsize = the_gridsize * win_width / the_width; } if (win_height > the_height) { unsigned int new_fontsize = the_fontsize * win_height / the_height; unsigned int new_gridsize = the_gridsize * win_height / the_height; if (new_fontsize < fontsize) fontsize = new_fontsize; if (new_gridsize < gridsize) gridsize = new_gridsize; } } RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE); } return DefWindowProc(hwnd, message, wparam, lparam); }