| 10265 | |
| 10266 | |
| 10267 | void GuiType::AutoSizeTabControl(GuiControlType &aTabControl) |
| 10268 | { |
| 10269 | int autosize = (int)(INT_PTR)GetProp(aTabControl.hwnd, _T("ahk_autosize")); |
| 10270 | if (!autosize) |
| 10271 | return; |
| 10272 | RemoveProp(aTabControl.hwnd, _T("ahk_autosize")); |
| 10273 | |
| 10274 | // Below: MUST NOT initialize to zero, since these are screen coordinates |
| 10275 | // and it is possible to position the GUI prior to this function being called. |
| 10276 | int tab_index = aTabControl.tab_index, right = INT_MIN, bottom = INT_MIN; |
| 10277 | RECT rc; |
| 10278 | |
| 10279 | for (GuiIndexType i = 0; i < mControlCount; ++i) |
| 10280 | { |
| 10281 | if (mControl[i]->tab_control_index != tab_index) |
| 10282 | continue; |
| 10283 | GetWindowRect(mControl[i]->hwnd, &rc); |
| 10284 | if (right < rc.right) |
| 10285 | right = rc.right; |
| 10286 | if (bottom < rc.bottom) |
| 10287 | bottom = rc.bottom; |
| 10288 | } |
| 10289 | |
| 10290 | // Testing shows that -32768 is the minimum screen coordinate, but even if |
| 10291 | // that's not always the case, a right edge of INT_MIN would mean that the |
| 10292 | // controls have 0 width, since window positions are 32-bit at most. |
| 10293 | BOOL at_least_one_control = right != INT_MIN; |
| 10294 | |
| 10295 | RECT tab_rect; |
| 10296 | GetWindowRect(aTabControl.hwnd, &tab_rect); |
| 10297 | if ((autosize & TAB3_AUTOWIDTH) && at_least_one_control) |
| 10298 | tab_rect.right = right + mMarginX + 4; |
| 10299 | if ((autosize & TAB3_AUTOHEIGHT) && at_least_one_control) |
| 10300 | tab_rect.bottom = bottom + mMarginY + 4; |
| 10301 | MapWindowPoints(NULL, mHwnd, (LPPOINT)&tab_rect, 2); // Screen to GUI client. |
| 10302 | int width = tab_rect.right - tab_rect.left |
| 10303 | , height = tab_rect.bottom - tab_rect.top; |
| 10304 | |
| 10305 | DWORD style = GetWindowLong(aTabControl.hwnd, GWL_STYLE); |
| 10306 | |
| 10307 | // The number of "rows" of vertical buttons affect the tab control's width, |
| 10308 | // while horizontal buttons affect its height; so check the appropriate flag: |
| 10309 | bool adjust_for_rows = 0 != (autosize & ((style & TCS_VERTICAL) ? TAB3_AUTOWIDTH : TAB3_AUTOHEIGHT)); |
| 10310 | // For TCS_RIGHT, we need to adjust even when there's only one row before and after: |
| 10311 | int rows_before = (!adjust_for_rows || (style & TCS_RIGHT)) ? 0 : TabCtrl_GetRowCount(aTabControl.hwnd); |
| 10312 | |
| 10313 | // Apply new size. |
| 10314 | MoveWindow(aTabControl.hwnd, tab_rect.left, tab_rect.top, width, height, TRUE); |
| 10315 | |
| 10316 | if (adjust_for_rows) |
| 10317 | { |
| 10318 | int rows_after = TabCtrl_GetRowCount(aTabControl.hwnd); |
| 10319 | if (rows_before != rows_after) |
| 10320 | { |
| 10321 | // Adjust for the newly added tab rows/columns. |
| 10322 | RECT item_rect; |
| 10323 | TabCtrl_GetItemRect(aTabControl.hwnd, 0, &item_rect); |
| 10324 | int offset = (style & TCS_BUTTONS) ? 3 : 0; // The margin between buttons seems to be 3 pixels. |