george82 | b3a4d65 | 2005-07-26 10:22:56 +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 | ToolBar::ToolBar() : hwndToolBar(0), tbID(-1) { |
| 24 | INITCOMMONCONTROLSEX icex; |
| 25 | |
| 26 | // Ensure that the common control DLL is loaded |
| 27 | icex.dwSize = sizeof(INITCOMMONCONTROLSEX); |
| 28 | icex.dwICC = ICC_BAR_CLASSES; |
| 29 | InitCommonControlsEx(&icex); |
| 30 | } |
| 31 | |
| 32 | ToolBar::~ToolBar() { |
| 33 | DestroyWindow(getHandle()); |
| 34 | } |
| 35 | |
| 36 | bool ToolBar::create(int _tbID, HWND _parentHwnd, DWORD dwStyle) { |
| 37 | parentHwnd = _parentHwnd; |
| 38 | dwStyle |= WS_CHILD; |
| 39 | |
| 40 | // Create the ToolBar window |
| 41 | hwndToolBar = CreateWindowEx(0, TOOLBARCLASSNAME, 0, dwStyle, |
| 42 | 0, 0, 25, 25, parentHwnd, (HMENU)_tbID, GetModuleHandle(0), 0); |
| 43 | |
| 44 | if (hwndToolBar) { |
| 45 | tbID = _tbID; |
| 46 | |
| 47 | // It's required for backward compatibility |
| 48 | SendMessage(hwndToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0); |
| 49 | } |
| 50 | return (hwndToolBar ? true : false); |
| 51 | }; |
| 52 | |
| 53 | int ToolBar::addBitmap(int nButtons, UINT bitmapID) { |
| 54 | assert(nButtons > 0); |
| 55 | TBADDBITMAP resBitmap; |
| 56 | resBitmap.hInst = GetModuleHandle(0); |
| 57 | resBitmap.nID = bitmapID; |
| 58 | return SendMessage(getHandle(), TB_ADDBITMAP, nButtons, (LPARAM)&resBitmap); |
| 59 | } |
| 60 | |
| 61 | int ToolBar::addSystemBitmap(UINT stdBitmapID) { |
| 62 | TBADDBITMAP resBitmap; |
| 63 | resBitmap.hInst = HINST_COMMCTRL; |
| 64 | resBitmap.nID = stdBitmapID; |
| 65 | return SendMessage(getHandle(), TB_ADDBITMAP, 0, (LPARAM)&resBitmap); |
| 66 | } |
| 67 | |
| 68 | bool ToolBar::setBitmapSize(int width, int height) { |
| 69 | int result = SendMessage(getHandle(), TB_SETBITMAPSIZE, |
| 70 | 0, MAKELONG(width, height)); |
| 71 | return (result ? true : false); |
| 72 | } |
| 73 | |
| 74 | bool ToolBar::addButton(int iBitmap, int idCommand, BYTE state, BYTE style, UINT dwData, int iString) { |
| 75 | TBBUTTON tbb; |
| 76 | tbb.iBitmap = iBitmap; |
| 77 | tbb.idCommand = idCommand; |
| 78 | tbb.fsState = state; |
| 79 | tbb.fsStyle = style; |
| 80 | tbb.dwData = dwData; |
| 81 | tbb.iString = iString; |
| 82 | |
| 83 | int result = SendMessage(getHandle(), TB_ADDBUTTONS, 1, (LPARAM)&tbb); |
| 84 | if (result) { |
| 85 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 86 | } |
| 87 | return (result ? true : false); |
| 88 | } |
| 89 | |
| 90 | bool ToolBar::addNButton(int nButtons, LPTBBUTTON tbb) { |
| 91 | assert(nButtons > 0); |
| 92 | assert(tbb > 0); |
| 93 | int result = SendMessage(getHandle(), TB_ADDBUTTONS, nButtons, (LPARAM)tbb); |
| 94 | if (result) { |
| 95 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 96 | } |
| 97 | return (result ? true : false); |
| 98 | } |
| 99 | |
| 100 | bool ToolBar::deleteButton(int indexButton) { |
| 101 | assert(indexButton >= 0); |
| 102 | int result = SendMessage(getHandle(), TB_DELETEBUTTON, indexButton, 0); |
| 103 | |
| 104 | if (result) { |
| 105 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 106 | } |
| 107 | return (result ? true : false); |
| 108 | } |
| 109 | |
| 110 | bool ToolBar::insertButton(int indexButton, LPTBBUTTON tbb) { |
| 111 | assert(indexButton >= 0); |
| 112 | assert(tbb > 0); |
| 113 | int result = SendMessage(getHandle(), TB_INSERTBUTTON, |
| 114 | indexButton, (LPARAM)tbb); |
| 115 | |
| 116 | if (result) { |
| 117 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 118 | } |
| 119 | return (result ? true : false); |
| 120 | } |
| 121 | |
| 122 | int ToolBar::getButtonInfo(int idButton, TBBUTTONINFO *btnInfo) { |
| 123 | assert(idButton >= 0); |
| 124 | assert(btnInfo > 0); |
| 125 | return SendMessage(getHandle(), TB_GETBUTTONINFO, idButton, (LPARAM)btnInfo); |
| 126 | } |
| 127 | |
| 128 | int ToolBar::getButtonsHeight() { |
| 129 | return HIWORD(SendMessage(getHandle(), TB_GETBUTTONSIZE, 0, 0)); |
| 130 | } |
| 131 | |
| 132 | int ToolBar::getButtonsWidth() { |
| 133 | return LOWORD(SendMessage(getHandle(), TB_GETBUTTONSIZE, 0, 0)); |
| 134 | } |
| 135 | |
| 136 | bool ToolBar::setButtonInfo(int idButton, TBBUTTONINFO* btnInfo) { |
| 137 | assert(idButton >= 0); |
| 138 | assert(btnInfo > 0); |
| 139 | int result = SendMessage(getHandle(), TB_SETBUTTONINFO, |
| 140 | idButton, (LPARAM)(LPTBBUTTONINFO)btnInfo); |
| 141 | return (result ? true : false); |
| 142 | } |
| 143 | |
| 144 | bool ToolBar::checkButton(int idButton, bool check) { |
| 145 | assert(idButton >= 0); |
| 146 | int result = SendMessage(getHandle(), TB_CHECKBUTTON, |
| 147 | idButton, MAKELONG(check, 0)); |
| 148 | return (result ? true : false); |
| 149 | } |
| 150 | |
| 151 | bool ToolBar::enableButton(int idButton, bool enable) { |
| 152 | assert(idButton >= 0); |
| 153 | int result = SendMessage(getHandle(), TB_ENABLEBUTTON, |
| 154 | idButton, MAKELONG(enable, 0)); |
| 155 | return (result ? true : false); |
| 156 | } |
| 157 | |
| 158 | bool ToolBar::pressButton(int idButton, bool press) { |
| 159 | assert(idButton >= 0); |
| 160 | int result = SendMessage(getHandle(), TB_PRESSBUTTON, |
| 161 | idButton, MAKELONG(press, 0)); |
| 162 | return (result ? true : false); |
| 163 | } |
| 164 | |
| 165 | bool ToolBar::getButtonRect(int nIndex, LPRECT buttonRect) { |
| 166 | int result = SendMessage(getHandle(), TB_GETITEMRECT, |
| 167 | nIndex, (LPARAM)buttonRect); |
| 168 | return (result ? true : false); |
| 169 | } |
| 170 | |
| 171 | bool ToolBar::setButtonSize(int width, int height) { |
| 172 | assert(width > 0); |
| 173 | assert(height > 0); |
| 174 | int result = SendMessage(getHandle(), TB_SETBUTTONSIZE, |
| 175 | 0, MAKELONG(width, height)); |
| 176 | if (result) { |
| 177 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 178 | return true; |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | void ToolBar::autoSize() { |
| 184 | DWORD style = SendMessage(getHandle(), TB_GETSTYLE, 0, 0); |
| 185 | if (style & CCS_NORESIZE) { |
| 186 | RECT r, btnRect; |
| 187 | GetClientRect(parentHwnd, &r); |
| 188 | getButtonRect(0, &btnRect); |
| 189 | int height = getButtonsHeight() + btnRect.top * 2 + 2; |
| 190 | SetWindowPos(getHandle(), HWND_TOP, 0, 0, r.right - r.left, height, |
| 191 | SWP_NOMOVE); |
| 192 | } else { |
| 193 | SendMessage(getHandle(), TB_AUTOSIZE, 0, 0); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | int ToolBar::getHeight() { |
| 198 | RECT r; |
| 199 | GetWindowRect(getHandle(), &r); |
| 200 | return r.bottom - r.top; |
| 201 | } |