00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdio.h>
00033 #include <assert.h>
00034 #include <windows.h>
00035 #include <richedit.h>
00036
00037 #include "gui.h"
00038 #include "main.h"
00039 #include "error_management.h"
00040
00041 BOOL gui_paint(s_gui_tab_layout *_gui_layout, HDC hdc);
00042 int gui_get_item_w_coord(s_gui_tab_layout *_gui_layout, int *x, int *y);
00043 s_gui_tab_layout_item *gui_is_hwnd(s_gui_tab_layout *_gui_layout, HWND hwnd);
00044 void get_path(char *_path, int size);
00045
00046 void get_path(char *_path, int size)
00047 {
00048 char *_c;
00049
00050 _c = _path + GetModuleFileName(NULL, _path, size);
00051 while(*_c != '\\' && _c > _path)
00052 _c--;
00053 if (_c == _path)
00054 SET_ERROR("Unable to find module path.");
00055
00056 _c[1] = '\0';
00057 }
00058
00059 void gui_set_font(s_gui_tab_layout_item *_gui_item, char *_name, int height, BOOL bolt, BOOL italic, BOOL underlined)
00060 {
00061 HFONT hfont, hfont_old;
00062 LOGFONT lf;
00063
00064 ASSERT(_gui_item);
00065
00066 memset(&lf, 0, sizeof(LOGFONT));
00067 strcpy(lf.lfFaceName, _name);
00068 lf.lfHeight = height;
00069 if (bolt)
00070 lf.lfWeight = FW_BLACK;
00071 lf.lfItalic = italic;
00072 lf.lfUnderline = underlined;
00073
00074 hfont_old = (HFONT) SendMessage(_gui_item->hwnd, WM_GETFONT, 0, 0);
00075 hfont = CreateFontIndirect(&lf);
00076 SendMessage(_gui_item->hwnd, WM_SETFONT, (WPARAM) hfont, MAKELPARAM(TRUE, 0));
00077 if (hfont_old)
00078 DeleteObject(hfont_old);
00079 }
00080
00081
00082 void gui_rescale(s_gui_tab_layout *_gui_layout, float scale_x, float scale_y, BOOL repaint)
00083 {
00084 int i, offset_x, offset_y;
00085 s_gui_tab_layout_item *_gui_item;
00086 HFONT hfont, hfont_old;
00087 LOGFONT lf;
00088 int flags;
00089
00090 ASSERT(_gui_layout);
00091
00092 memset(&lf, 0, sizeof(LOGFONT));
00093 strcpy(lf.lfFaceName, "Arial");
00094
00095 if (repaint)
00096 flags = 0;
00097 else
00098 flags = SWP_NOREDRAW;
00099
00100 if (scale_x < 0 || scale_y < 0)
00101 flags |= SWP_NOMOVE | SWP_NOSIZE;
00102
00103 _gui_layout->scale_x = scale_x;
00104 _gui_layout->scale_y = scale_y;
00105
00106 for(i=0; i<_gui_layout->nb_elements; i++)
00107 {
00108 _gui_item = &_gui_layout->_gui_item[i];
00109 ASSERT(_gui_item);
00110 offset_x = 0;
00111 offset_y = 0;
00112
00113 if (_gui_item->type == GUI_TYPE_COMBOBOX)
00114 offset_y = (20*(scale_y-1))/2;
00115
00116 SetWindowPos(
00117 _gui_item->hwnd,
00118 NULL,
00119 _gui_item->x*scale_x+offset_x,
00120 _gui_item->y*scale_y+offset_y,
00121 _gui_item->width*scale_x,
00122 _gui_item->height*scale_y,
00123 flags | SWP_NOZORDER);
00124
00125 if (_gui_item->type != GUI_TYPE_COMBOBOX && !(_gui_item->style & ES_MULTILINE))
00126 {
00127 lf.lfHeight = _gui_item->height*scale_y-2;
00128 lf.lfWeight = 800;
00129 hfont_old = (HFONT) SendDlgItemMessage(_gui_layout->hwnd, _gui_item->id, WM_GETFONT, 0, 0);
00130 hfont = CreateFontIndirect(&lf);
00131 SendDlgItemMessage(_gui_layout->hwnd, _gui_item->id, WM_SETFONT, (WPARAM) hfont, MAKELPARAM(TRUE, 0));
00132 if (hfont_old)
00133 DeleteObject(hfont_old);
00134 }
00135 }
00136 }
00137
00138 void gui_new_tab_layout(s_gui_tab_layout *_gui_layout, int max_elements)
00139 {
00140 int i;
00141
00142 ASSERT(_gui_layout);
00143
00144 _gui_layout->_gui_item = (s_gui_tab_layout_item *) malloc(sizeof(s_gui_tab_layout_item)*max_elements);
00145 assert(_gui_layout->_gui_item);
00146 memset(_gui_layout->_gui_item, 0, sizeof(s_gui_tab_layout_item)*max_elements);
00147 _gui_layout->hwnd = NULL;
00148 _gui_layout->nb_elements = 0;
00149 for(i=0; i<max_elements; i++)
00150 _gui_layout->_gui_item[i].actif = FALSE;
00151 }
00152
00153 void gui_delete_tab_layout(s_gui_tab_layout *_gui_layout)
00154 {
00155 int i;
00156
00157 ASSERT(_gui_layout);
00158
00159 for(i=0; i<_gui_layout->nb_elements; i++)
00160 DestroyWindow(_gui_layout->_gui_item[i].hwnd);
00161 free(_gui_layout->_gui_item);
00162 }
00163
00164 void gui_create(s_gui_tab_layout *_gui_layout, HWND hwnd)
00165 {
00166 int i;
00167 static int id = 1;
00168 s_gui_tab_layout_item *_gui_item;
00169 HANDLE handle;
00170 char _path[MAX_PATH];
00171
00172 ASSERT(_gui_layout);
00173
00174 _gui_layout->hwnd = hwnd;
00175 _gui_layout->scale_x = 1.0;
00176 _gui_layout->scale_y = 1.0;
00177 for(i=0; i<_gui_layout->nb_elements; i++)
00178 {
00179 _gui_item = &_gui_layout->_gui_item[i];
00180 switch(_gui_item->type)
00181 {
00182 case GUI_TYPE_IMAGE:
00183 _gui_item->style |= WS_CHILD | WS_VISIBLE | SS_BITMAP;
00184 _gui_item->hwnd = CreateWindowEx(
00185 SS_BITMAP,
00186 "STATIC",
00187 _gui_item->_str,
00188 _gui_item->style,
00189 _gui_item->x,
00190 _gui_item->y,
00191 _gui_item->width,
00192 _gui_item->height,
00193 hwnd,
00194 (HMENU) id++,
00195 (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
00196 NULL);
00197 ASSERT(_gui_item->hwnd);
00198 _gui_item->id = GetDlgCtrlID(_gui_item->hwnd);
00199 get_path(_path, MAX_PATH);
00200 strcat(_path, "images\\");
00201 strcat(_path, _gui_item->_str);
00202 handle = (HANDLE) LoadImage(NULL,
00203 _path,
00204 IMAGE_BITMAP,
00205 0,
00206 0,
00207 LR_LOADFROMFILE | LR_DEFAULTSIZE);
00208 ASSERT(handle);
00209 SendMessage(_gui_item->hwnd, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) handle);
00210 break;
00211 case GUI_TYPE_BUTTON:
00212 _gui_item->style |= WS_BORDER | WS_CHILD | WS_VISIBLE | WS_TABSTOP;
00213 _gui_item->hwnd = CreateWindowEx(
00214 0,
00215 "BUTTON",
00216 _gui_item->_str,
00217 _gui_item->style,
00218 _gui_item->x,
00219 _gui_item->y,
00220 _gui_item->width,
00221 _gui_item->height,
00222 hwnd,
00223 (HMENU) id++,
00224 (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
00225 NULL);
00226 ASSERT(_gui_item->hwnd);
00227 _gui_item->id = GetDlgCtrlID(_gui_item->hwnd);
00228 break;
00229 case GUI_TYPE_PAINT:
00230 break;
00231 case GUI_TYPE_STATIC:
00232 _gui_item->style |= WS_CHILD | WS_VISIBLE;
00233 _gui_item->hwnd = CreateWindowEx(
00234 0,
00235 "STATIC",
00236 _gui_item->_str,
00237 _gui_item->style,
00238 _gui_item->x,
00239 _gui_item->y,
00240 _gui_item->width,
00241 _gui_item->height,
00242 hwnd,
00243 (HMENU) id++,
00244 (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
00245 NULL);
00246 ASSERT(_gui_item->hwnd);
00247 _gui_item->id = GetDlgCtrlID(_gui_item->hwnd);
00248 break;
00249 case GUI_TYPE_EDIT:
00250 _gui_item->style |= WS_CHILD | WS_VISIBLE | WS_TABSTOP;
00251 _gui_item->hwnd = CreateWindowEx(
00252 0,
00253 "EDIT",
00254 _gui_item->_str,
00255 _gui_item->style | ES_MULTILINE,
00256 _gui_item->x,
00257 _gui_item->y,
00258 _gui_item->width,
00259 _gui_item->height,
00260 hwnd,
00261 (HMENU) id++,
00262 (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
00263 NULL);
00264 ASSERT(_gui_item->hwnd);
00265 _gui_item->id = GetDlgCtrlID(_gui_item->hwnd);
00266 break;
00267 case GUI_TYPE_COMBOBOX:
00268 _gui_item->style |= WS_VSCROLL | WS_CHILD | WS_TABSTOP | WS_VISIBLE | CBS_SORT | CBS_DROPDOWN;
00269 _gui_item->hwnd = CreateWindowEx(
00270 0,
00271 "COMBOBOX",
00272 _gui_item->_str,
00273 _gui_item->style,
00274 _gui_item->x,
00275 _gui_item->y,
00276 _gui_item->width,
00277 _gui_item->height,
00278 hwnd,
00279 (HMENU) id++,
00280 (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
00281 NULL);
00282 ASSERT(_gui_item->hwnd);
00283 _gui_item->id = GetDlgCtrlID(_gui_item->hwnd);
00284 break;
00285 case GUI_TYPE_RICHEDIT:
00286 _gui_item->style |= WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER | ES_MULTILINE | WS_HSCROLL | ES_AUTOVSCROLL | ES_NOHIDESEL;
00287 ASSERT(LoadLibrary("RICHED32.DLL"));
00288 _gui_item->hwnd = CreateWindowEx(
00289 WS_EX_CLIENTEDGE,
00290 RICHEDIT_CLASS,
00291 _gui_item->_str,
00292 _gui_item->style,
00293 _gui_item->x,
00294 _gui_item->y,
00295 _gui_item->width,
00296 _gui_item->height,
00297 hwnd,
00298 (HMENU) id++,
00299 (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
00300 NULL);
00301 ASSERT(_gui_item->hwnd);
00302 _gui_item->id = GetDlgCtrlID(_gui_item->hwnd);
00303 break;
00304 }
00305 _gui_item->actif = TRUE;
00306 }
00307 }
00308
00309 void gui_move(s_gui_tab_layout *_gui_layout, int x, int y)
00310 {
00311 int i, inf_x, inf_y;
00312 int dx, dy;
00313 s_gui_tab_layout_item *_gui_item;
00314
00315 ASSERT(_gui_layout);
00316
00317 inf_x = 0xFFFFFF;
00318 inf_y = 0xFFFFFF;
00319 for(i=0; i<_gui_layout->nb_elements; i++)
00320 {
00321 _gui_item = &_gui_layout->_gui_item[i];
00322 inf_x = (inf_x < _gui_item->x)?inf_x:_gui_item->x;
00323 inf_y = (inf_y < _gui_item->y)?inf_y:_gui_item->y;
00324 }
00325
00326 for(i=0; i<_gui_layout->nb_elements; i++)
00327 {
00328 _gui_item = &_gui_layout->_gui_item[i];
00329 _gui_item->x = _gui_item->x-inf_x + x;
00330 _gui_item->y = _gui_item->y-inf_y + y;
00331 SetWindowPos(
00332 _gui_item->hwnd,
00333 NULL,
00334 _gui_item->x,
00335 _gui_item->y,
00336 0,
00337 0,
00338 SWP_NOZORDER | SWP_NOSIZE);
00339 }
00340 }
00341
00342 void gui_visibility(s_gui_tab_layout *_gui_layout, BOOL visible)
00343 {
00344 int i;
00345 UINT uFlags;
00346
00347 ASSERT(_gui_layout);
00348
00349 uFlags = SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE;
00350 if (visible)
00351 uFlags |= SWP_SHOWWINDOW;
00352 else
00353 uFlags |= SWP_HIDEWINDOW;
00354
00355 for(i=0; i<_gui_layout->nb_elements; i++)
00356 {
00357 if (visible)
00358 _gui_layout->_gui_item[i].actif = TRUE;
00359 else
00360 _gui_layout->_gui_item[i].actif = FALSE;
00361 SetWindowPos(_gui_layout->_gui_item[i].hwnd, NULL, 0, 0, 0, 0, uFlags);
00362 }
00363 }
00364
00365 void gui_visibility_item(s_gui_tab_layout_item *_gui_item, BOOL visible)
00366 {
00367 UINT uFlags;
00368
00369 ASSERT(_gui_item);
00370
00371 uFlags = SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE;
00372 if (visible)
00373 uFlags |= SWP_SHOWWINDOW;
00374 else
00375 uFlags |= SWP_HIDEWINDOW;
00376
00377 if (visible)
00378 _gui_item->actif = TRUE;
00379 else
00380 _gui_item->actif = FALSE;
00381 SetWindowPos(_gui_item->hwnd, NULL, 0, 0, 0, 0, uFlags);
00382 }
00383
00384 int gui_get_item_w_coord(s_gui_tab_layout *_gui_layout, int *x, int *y)
00385 {
00386 int i;
00387 int new_x, new_y;
00388 s_gui_tab_layout_item *_gui_item;
00389
00390 ASSERT(_gui_layout);
00391
00392 for(i=0; i<_gui_layout->nb_elements; i++)
00393 {
00394 _gui_item = &_gui_layout->_gui_item[i];
00395 ASSERT(_gui_item);
00396 new_x = *x - _gui_item->x;
00397 new_y = *y - _gui_item->y;
00398 if (new_x < 0 || new_y < 0)
00399 continue;
00400 if (new_x < _gui_item->width && new_y < _gui_item->height)
00401 {
00402 *x = new_x;
00403 *y = new_y;
00404 return i;
00405 }
00406 }
00407
00408 return -1;
00409 }
00410
00411 s_gui_tab_layout_item *gui_is_hwnd(s_gui_tab_layout *_gui_layout, HWND hwnd)
00412 {
00413 int i;
00414
00415 ASSERT(_gui_layout);
00416
00417 for(i=0; i<_gui_layout->nb_elements; i++)
00418 if (_gui_layout->_gui_item[i].hwnd == hwnd)
00419 return &_gui_layout->_gui_item[i];
00420
00421 return NULL;
00422 }
00423
00424 LRESULT CALLBACK gui_proc(s_gui_tab_layout *_gui_layout, HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00425 {
00426 static HBRUSH hbrush = NULL;
00427 LRESULT result;
00428 HDC hdc;
00429 PAINTSTRUCT ps;
00430 int item_id, msg_code;
00431 int i, x, y;
00432 s_gui_tab_layout_item *_gui_item;
00433 COLORREF bg_color, fg_color;
00434 char _temp[256];
00435
00436 result = 0;
00437 switch(message)
00438 {
00439 case WM_DESTROY:
00440 ASSERT(_gui_layout);
00441 gui_delete_tab_layout(_gui_layout);
00442 result = 1;
00443 break;
00444 case WM_PAINT:
00445 ASSERT(_gui_layout);
00446 hdc = BeginPaint(hwnd, &ps);
00447 result = gui_paint(_gui_layout, hdc);
00448 EndPaint(hwnd, &ps);
00449 break;
00450 case WM_MOUSEMOVE:
00451 case WM_LBUTTONDOWN:
00452 case WM_LBUTTONUP:
00453 case WM_LBUTTONDBLCLK:
00454 case WM_RBUTTONDOWN:
00455 case WM_RBUTTONUP:
00456 case WM_RBUTTONDBLCLK:
00457 ASSERT(_gui_layout);
00458 x = LOWORD(lParam);
00459 y = HIWORD(lParam);
00460 i = gui_get_item_w_coord(_gui_layout, &x, &y);
00461 if (i == -1)
00462 return 0;
00463 ASSERT(i >= 0 && i < _gui_layout->nb_elements);
00464 _gui_item = &_gui_layout->_gui_item[i];
00465 ASSERT(_gui_item);
00466 lParam = x + (y << 16);
00467 if (_gui_item->_f_proc)
00468 result = _gui_item->_f_proc(_gui_item->hwnd, message, wParam, lParam);
00469 break;
00470 case WM_CTLCOLORSTATIC:
00471 case WM_CTLCOLOREDIT:
00472 case WM_CTLCOLORSCROLLBAR:
00473 case WM_CTLCOLORLISTBOX:
00474 case WM_CTLCOLORBTN:
00475 ASSERT(_gui_layout);
00476 if (!(_gui_item = gui_is_hwnd(_gui_layout, (HWND) lParam)))
00477 break;
00478 hdc = (HDC) wParam;
00479 ASSERT(hdc);
00480 switch(_gui_item->type)
00481 {
00482 case GUI_TYPE_STATIC:
00483 bg_color = (_gui_item->background_color)?_gui_item->background_color:GUI_BACKGROUND_COLOR_STATIC;
00484 fg_color = (_gui_item->foreground_color)?_gui_item->foreground_color:GUI_FOREGROUND_COLOR_STATIC;
00485 break;
00486 case GUI_TYPE_RICHEDIT:
00487 case GUI_TYPE_EDIT:
00488 bg_color = (_gui_item->background_color)?_gui_item->background_color:GUI_BACKGROUND_COLOR_EDIT;
00489 fg_color = (_gui_item->foreground_color)?_gui_item->foreground_color:GUI_FOREGROUND_COLOR_EDIT;
00490 break;
00491 case GUI_TYPE_COMBOBOX:
00492 bg_color = (_gui_item->background_color)?_gui_item->background_color:GUI_BACKGROUND_COLOR_COMBOBOX;
00493 fg_color = (_gui_item->foreground_color)?_gui_item->foreground_color:GUI_FOREGROUND_COLOR_COMBOBOX;
00494 break;
00495 }
00496
00497 SetBkColor(hdc, bg_color);
00498 SetTextColor(hdc, fg_color);
00499 if (hbrush)
00500 DeleteObject(hbrush);
00501 hbrush = CreateSolidBrush(bg_color);
00502 ASSERT(hbrush);
00503 SelectObject(hdc, hbrush);
00504 result = (LRESULT) hbrush;
00505 break;
00506 case WM_COMMAND:
00507 ASSERT(_gui_layout);
00508 msg_code = (wParam >> 16) & 0xFFFF;
00509 item_id = wParam & 0xFFFF;
00510
00511 i = _gui_layout->nb_elements;
00512 _gui_item = NULL;
00513 while(i--)
00514 {
00515 _gui_item = &_gui_layout->_gui_item[i];
00516 ASSERT(_gui_item);
00517 if (_gui_item->id == item_id)
00518 break;
00519 }
00520 if (!_gui_item)
00521 {
00522 result = (LRESULT) 0;
00523 break;
00524 }
00525
00526 if (_gui_item->id == item_id)
00527 {
00528 if (_gui_item->_f_proc)
00529 result = _gui_item->_f_proc(_gui_item->hwnd, msg_code, wParam, lParam);
00530 else
00531 result = (LRESULT) 0;
00532 }
00533 else
00534 result = (LRESULT) 0;
00535 break;
00536 default:
00537 result = (LRESULT) 0;
00538 }
00539
00540 return result;
00541 }
00542
00543 BOOL gui_paint(s_gui_tab_layout *_gui_layout, HDC hdc)
00544 {
00545 HDC hdc_buffer;
00546 HBITMAP hbmp;
00547 int w, h;
00548 int i;
00549 RECT rect;
00550 s_gui_tab_layout_item *_gui_item;
00551
00552 GetUpdateRect(_gui_layout->hwnd, &rect, FALSE);
00553 for(i=0; i<_gui_layout->nb_elements; i++)
00554 {
00555 _gui_item = &_gui_layout->_gui_item[i];
00556 if (_gui_item->type == GUI_TYPE_PAINT)
00557 {
00558 w = _gui_item->width;
00559 h = _gui_item->height;
00560
00561 hbmp = CreateCompatibleBitmap(hdc, w, h);
00562 hdc_buffer = CreateCompatibleDC(hdc);
00563 SelectObject(hdc_buffer, hbmp);
00564
00565 _gui_item->_f_proc(_gui_item->hwnd, WM_PAINT, (WPARAM) hdc_buffer, (LPARAM) 0);
00566
00567 StretchBlt(hdc,
00568 _gui_item->x*_gui_layout->scale_x,
00569 _gui_item->y*_gui_layout->scale_y,
00570 w*_gui_layout->scale_x,
00571 h*_gui_layout->scale_y,
00572 hdc_buffer,
00573 0,
00574 0,
00575 w,
00576 h,
00577 SRCCOPY);
00578 DeleteObject(hbmp);
00579 DeleteDC(hdc_buffer);
00580 }
00581 }
00582 }