Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2004 TightVNC Team. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // -=- ToolBar control class. |
| 20 | |
| 21 | #include "ToolBar.h" |
| 22 | |
| 23 | using namespace rfb::win32; |
| 24 | |
| 25 | ToolBar::ToolBar() : hwndToolBar(0), tbID(-1) { |
| 26 | INITCOMMONCONTROLSEX icex; |
| 27 | |
| 28 | // Ensure that the common control DLL is loaded |
| 29 | icex.dwSize = sizeof(INITCOMMONCONTROLSEX); |
| 30 | icex.dwICC = ICC_BAR_CLASSES; |
| 31 | InitCommonControlsEx(&icex); |
| 32 | } |
| 33 | |
| 34 | ToolBar::~ToolBar() { |
| 35 | DestroyWindow(getHandle()); |
| 36 | } |
| 37 | |
| 38 | bool ToolBar::create(int _tbID, HWND _parentHwnd, DWORD dwStyle) { |
| 39 | parentHwnd = _parentHwnd; |
| 40 | dwStyle |= WS_CHILD; |
| 41 | |
| 42 | // Create the ToolBar window |
| 43 | hwndToolBar = CreateWindowEx(0, TOOLBARCLASSNAME, 0, dwStyle, |
| 44 | 0, 0, 25, 25, parentHwnd, (HMENU)_tbID, GetModuleHandle(0), 0); |
| 45 | |
| 46 | if (hwndToolBar) { |
| 47 | tbID = _tbID; |
| 48 | |
| 49 | // It's required for backward compatibility |
| 50 | SendMessage(hwndToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0); |
| 51 | } |
| 52 | return (hwndToolBar ? true : false); |
| 53 | }; |
| 54 | |
| 55 | int ToolBar::addBitmap(int nButtons, UINT bitmapID) { |
| 56 | assert(nButtons > 0); |
| 57 | TBADDBITMAP resBitmap; |
| 58 | resBitmap.hInst = GetModuleHandle(0); |
| 59 | resBitmap.nID = bitmapID; |
| 60 | return SendMessage(getHandle(), TB_ADDBITMAP, nButtons, (LPARAM)&resBitmap); |
| 61 | } |
| 62 | |
| 63 | int ToolBar::addSystemBitmap(UINT stdBitmapID) { |
| 64 | TBADDBITMAP resBitmap; |
| 65 | resBitmap.hInst = HINST_COMMCTRL; |
| 66 | resBitmap.nID = stdBitmapID; |
| 67 | return SendMessage(getHandle(), TB_ADDBITMAP, 0, (LPARAM)&resBitmap); |
| 68 | } |
| 69 | |
| 70 | bool ToolBar::setBitmapSize(int width, int height) { |
| 71 | int result = SendMessage(getHandle(), TB_SETBITMAPSIZE, |
| 72 | 0, MAKELONG(width, height)); |
| 73 | return (result ? true : false); |
| 74 | } |
| 75 | |
| 76 | bool ToolBar::addButton(int iBitmap, int idCommand, BYTE state, BYTE style, UINT dwData, int iString) { |
| 77 | TBBUTTON tbb; |
| 78 | tbb.iBitmap = iBitmap; |
| 79 | tbb.idCommand = idCommand; |
| 80 | tbb.fsState = state; |
| 81 | tbb.fsStyle = style; |
| 82 | tbb.dwData = dwData; |
| 83 | tbb.iString = iString; |
| 84 | |
| 85 | int result = SendMessage(getHandle(), TB_ADDBUTTONS, 1, (LPARAM)&tbb); |
| 86 | if (result) { |
| 87 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 88 | } |
| 89 | return (result ? true : false); |
| 90 | } |
| 91 | |
| 92 | bool ToolBar::addNButton(int nButtons, LPTBBUTTON tbb) { |
| 93 | assert(nButtons > 0); |
| 94 | assert(tbb > 0); |
| 95 | int result = SendMessage(getHandle(), TB_ADDBUTTONS, nButtons, (LPARAM)tbb); |
| 96 | if (result) { |
| 97 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 98 | } |
| 99 | return (result ? true : false); |
| 100 | } |
| 101 | |
| 102 | bool ToolBar::deleteButton(int indexButton) { |
| 103 | assert(indexButton >= 0); |
| 104 | int result = SendMessage(getHandle(), TB_DELETEBUTTON, indexButton, 0); |
| 105 | |
| 106 | if (result) { |
| 107 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 108 | } |
| 109 | return (result ? true : false); |
| 110 | } |
| 111 | |
| 112 | bool ToolBar::insertButton(int indexButton, LPTBBUTTON tbb) { |
| 113 | assert(indexButton >= 0); |
| 114 | assert(tbb > 0); |
| 115 | int result = SendMessage(getHandle(), TB_INSERTBUTTON, |
| 116 | indexButton, (LPARAM)tbb); |
| 117 | |
| 118 | if (result) { |
| 119 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 120 | } |
| 121 | return (result ? true : false); |
| 122 | } |
| 123 | |
| 124 | int ToolBar::getButtonInfo(int idButton, TBBUTTONINFO *btnInfo) { |
| 125 | assert(idButton >= 0); |
| 126 | assert(btnInfo > 0); |
| 127 | return SendMessage(getHandle(), TB_GETBUTTONINFO, idButton, (LPARAM)btnInfo); |
| 128 | } |
| 129 | |
| 130 | int ToolBar::getButtonsHeight() { |
| 131 | return HIWORD(SendMessage(getHandle(), TB_GETBUTTONSIZE, 0, 0)); |
| 132 | } |
| 133 | |
| 134 | int ToolBar::getButtonsWidth() { |
| 135 | return LOWORD(SendMessage(getHandle(), TB_GETBUTTONSIZE, 0, 0)); |
| 136 | } |
| 137 | |
| 138 | bool ToolBar::setButtonInfo(int idButton, TBBUTTONINFO* btnInfo) { |
| 139 | assert(idButton >= 0); |
| 140 | assert(btnInfo > 0); |
| 141 | int result = SendMessage(getHandle(), TB_SETBUTTONINFO, |
| 142 | idButton, (LPARAM)(LPTBBUTTONINFO)btnInfo); |
| 143 | return (result ? true : false); |
| 144 | } |
| 145 | |
| 146 | bool ToolBar::checkButton(int idButton, bool check) { |
| 147 | assert(idButton >= 0); |
| 148 | int result = SendMessage(getHandle(), TB_CHECKBUTTON, |
| 149 | idButton, MAKELONG(check, 0)); |
| 150 | return (result ? true : false); |
| 151 | } |
| 152 | |
| 153 | bool ToolBar::enableButton(int idButton, bool enable) { |
| 154 | assert(idButton >= 0); |
| 155 | int result = SendMessage(getHandle(), TB_ENABLEBUTTON, |
| 156 | idButton, MAKELONG(enable, 0)); |
| 157 | return (result ? true : false); |
| 158 | } |
| 159 | |
| 160 | bool ToolBar::pressButton(int idButton, bool press) { |
| 161 | assert(idButton >= 0); |
| 162 | int result = SendMessage(getHandle(), TB_PRESSBUTTON, |
| 163 | idButton, MAKELONG(press, 0)); |
| 164 | return (result ? true : false); |
| 165 | } |
| 166 | |
| 167 | bool ToolBar::getButtonRect(int nIndex, LPRECT buttonRect) { |
| 168 | int result = SendMessage(getHandle(), TB_GETITEMRECT, |
| 169 | nIndex, (LPARAM)buttonRect); |
| 170 | return (result ? true : false); |
| 171 | } |
| 172 | |
| 173 | bool ToolBar::setButtonSize(int width, int height) { |
| 174 | assert(width > 0); |
| 175 | assert(height > 0); |
| 176 | int result = SendMessage(getHandle(), TB_SETBUTTONSIZE, |
| 177 | 0, MAKELONG(width, height)); |
| 178 | if (result) { |
| 179 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 180 | return true; |
| 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | void ToolBar::autoSize() { |
| 186 | DWORD style = SendMessage(getHandle(), TB_GETSTYLE, 0, 0); |
| 187 | if (style & CCS_NORESIZE) { |
| 188 | RECT r, btnRect; |
| 189 | GetClientRect(parentHwnd, &r); |
| 190 | getButtonRect(0, &btnRect); |
| 191 | int height = getButtonsHeight() + btnRect.top * 2 + 2; |
| 192 | SetWindowPos(getHandle(), HWND_TOP, 0, 0, r.right - r.left, height, |
| 193 | SWP_NOMOVE); |
| 194 | } else { |
| 195 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | int ToolBar::getHeight() { |
| 200 | RECT r; |
| 201 | GetWindowRect(getHandle(), &r); |
| 202 | return r.bottom - r.top; |
| 203 | } |
| 204 | |
| 205 | void ToolBar::show() { |
| 206 | ShowWindow(getHandle(), SW_SHOW); |
| 207 | } |
| 208 | |
| 209 | void ToolBar::hide() { |
| 210 | ShowWindow(getHandle(), SW_HIDE); |
| 211 | } |
| 212 | |
| 213 | bool ToolBar::isVisible() { |
| 214 | DWORD style = GetWindowLong(getHandle(), GWL_STYLE); |
| 215 | return (bool)(style & WS_VISIBLE); |
| 216 | } |