1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/********************************************************************************
* *
* M e n u C h e c k W i d g e t *
* *
*********************************************************************************
* Copyright (C) 2002,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxchar.h"
#include "fxmath.h"
#include "fxkeys.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXAccelTable.h"
#include "FXFont.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXDCWindow.h"
#include "FXApp.h"
#include "FXIcon.h"
#include "FXMenuCommand.h"
#include "FXMenuCheck.h"
/*
Notes:
- FXMenuCheck should flip state when invoked, and send new state along
in ptr in callback.
*/
#define TOPIC_KEYBOARD 1009
#define LEADSPACE 22
#define TRAILSPACE 16
using namespace FX;
/*******************************************************************************/
namespace FX {
// Map
FXDEFMAP(FXMenuCheck) FXMenuCheckMap[]={
FXMAPFUNC(SEL_PAINT,0,FXMenuCheck::onPaint),
FXMAPFUNC(SEL_LEFTBUTTONPRESS,0,FXMenuCheck::onButtonPress),
FXMAPFUNC(SEL_LEFTBUTTONRELEASE,0,FXMenuCheck::onButtonRelease),
FXMAPFUNC(SEL_MIDDLEBUTTONPRESS,0,FXMenuCheck::onButtonPress),
FXMAPFUNC(SEL_MIDDLEBUTTONRELEASE,0,FXMenuCheck::onButtonRelease),
FXMAPFUNC(SEL_RIGHTBUTTONPRESS,0,FXMenuCheck::onButtonPress),
FXMAPFUNC(SEL_RIGHTBUTTONRELEASE,0,FXMenuCheck::onButtonRelease),
FXMAPFUNC(SEL_KEYPRESS,0,FXMenuCheck::onKeyPress),
FXMAPFUNC(SEL_KEYRELEASE,0,FXMenuCheck::onKeyRelease),
FXMAPFUNC(SEL_KEYPRESS,FXWindow::ID_HOTKEY,FXMenuCheck::onHotKeyPress),
FXMAPFUNC(SEL_KEYRELEASE,FXWindow::ID_HOTKEY,FXMenuCheck::onHotKeyRelease),
FXMAPFUNC(SEL_COMMAND,FXWindow::ID_CHECK,FXMenuCheck::onCheck),
FXMAPFUNC(SEL_COMMAND,FXWindow::ID_UNCHECK,FXMenuCheck::onUncheck),
FXMAPFUNC(SEL_COMMAND,FXWindow::ID_UNKNOWN,FXMenuCheck::onUnknown),
FXMAPFUNC(SEL_COMMAND,FXWindow::ID_SETVALUE,FXMenuCheck::onCmdSetValue),
FXMAPFUNC(SEL_COMMAND,FXWindow::ID_SETINTVALUE,FXMenuCheck::onCmdSetIntValue),
FXMAPFUNC(SEL_COMMAND,FXWindow::ID_GETINTVALUE,FXMenuCheck::onCmdGetIntValue),
FXMAPFUNC(SEL_COMMAND,FXWindow::ID_ACCEL,FXMenuCheck::onCmdAccel),
};
// Object implementation
FXIMPLEMENT(FXMenuCheck,FXMenuCommand,FXMenuCheckMap,ARRAYNUMBER(FXMenuCheckMap))
// Command menu item
FXMenuCheck::FXMenuCheck(){
boxColor=0;
check=false;
}
// Command menu item
FXMenuCheck::FXMenuCheck(FXComposite* p,const FXString& text,FXObject* tgt,FXSelector sel,FXuint opts):FXMenuCommand(p,text,nullptr,tgt,sel,opts){
boxColor=getApp()->getBackColor();
check=false;
}
// Get default width
FXint FXMenuCheck::getDefaultWidth(){
FXint tw,aw;
tw=aw=0;
if(!label.empty()) tw=font->getTextWidth(label.text(),label.length());
if(!accel.empty()) aw=font->getTextWidth(accel.text(),accel.length());
if(aw && tw) aw+=5;
return LEADSPACE+tw+aw+TRAILSPACE;
}
// Get default height
FXint FXMenuCheck::getDefaultHeight(){
FXint th=0;
if(!label.empty() || !accel.empty()) th=font->getFontHeight()+5;
return FXMAX(th,20);
}
// Check button
void FXMenuCheck::setCheck(FXuchar s){
if(check!=s){
check=s;
update();
}
}
// Change state to checked
long FXMenuCheck::onCheck(FXObject*,FXSelector,void*){
setCheck(true);
return 1;
}
// Change state to unchecked
long FXMenuCheck::onUncheck(FXObject*,FXSelector,void*){
setCheck(false);
return 1;
}
// Change state to indeterminate
long FXMenuCheck::onUnknown(FXObject*,FXSelector,void*){
setCheck(maybe);
return 1;
}
// Update value from a message
long FXMenuCheck::onCmdSetValue(FXObject*,FXSelector,void* ptr){
setCheck((FXuchar)(FXuval)ptr);
return 1;
}
// Update value from a message
long FXMenuCheck::onCmdSetIntValue(FXObject*,FXSelector,void* ptr){
setCheck((FXuchar)*((FXint*)ptr));
return 1;
}
// Obtain value from text field
long FXMenuCheck::onCmdGetIntValue(FXObject*,FXSelector,void* ptr){
*((FXint*)ptr)=getCheck();
return 1;
}
// Pressed button
long FXMenuCheck::onButtonPress(FXObject*,FXSelector,void*){
if(!isEnabled()) return 0;
return 1;
}
// Released button
long FXMenuCheck::onButtonRelease(FXObject*,FXSelector,void*){
FXbool active=isActive();
if(!isEnabled()) return 0;
getParent()->handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),nullptr);
if(active){
setCheck(!check);
if(target){ target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)(FXuval)check); }
}
return 1;
}
// Keyboard press
long FXMenuCheck::onKeyPress(FXObject*,FXSelector,void* ptr){
FXEvent* event=(FXEvent*)ptr;
if(isEnabled() && !(flags&FLAG_PRESSED)){
FXTRACE((TOPIC_KEYBOARD,"%s::onKeyPress %p keysym=0x%04x state=%04x\n",getClassName(),this,event->code,event->state));
if(event->code==KEY_space || event->code==KEY_KP_Space || event->code==KEY_Return || event->code==KEY_KP_Enter){
flags|=FLAG_PRESSED;
return 1;
}
}
return 0;
}
// Keyboard release
long FXMenuCheck::onKeyRelease(FXObject*,FXSelector,void* ptr){
FXEvent* event=(FXEvent*)ptr;
if(isEnabled() && (flags&FLAG_PRESSED)){
FXTRACE((TOPIC_KEYBOARD,"%s::onKeyRelease %p keysym=0x%04x state=%04x\n",getClassName(),this,event->code,event->state));
if(event->code==KEY_space || event->code==KEY_KP_Space || event->code==KEY_Return || event->code==KEY_KP_Enter){
flags&=~FLAG_PRESSED;
setCheck(!check);
getParent()->handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),nullptr);
if(target) target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)(FXuval)check);
return 1;
}
}
return 0;
}
// Hot key combination pressed
long FXMenuCheck::onHotKeyPress(FXObject*,FXSelector,void* ptr){
FXTRACE((200,"%s::onHotKeyPress %p\n",getClassName(),this));
handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr);
if(isEnabled() && !(flags&FLAG_PRESSED)){
flags|=FLAG_PRESSED;
}
return 1;
}
// Hot key combination released
long FXMenuCheck::onHotKeyRelease(FXObject*,FXSelector,void*){
FXTRACE((200,"%s::onHotKeyRelease %p\n",getClassName(),this));
if(isEnabled() && (flags&FLAG_PRESSED)){
flags&=~FLAG_PRESSED;
setCheck(!check);
getParent()->handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),nullptr);
if(target) target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)(FXuval)check);
}
return 1;
}
// Accelerator activated
long FXMenuCheck::onCmdAccel(FXObject*,FXSelector,void*){
if(isEnabled()){
setCheck(!check);
if(target) target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)(FXuval)check);
return 1;
}
return 0;
}
// Handle repaint
long FXMenuCheck::onPaint(FXObject*,FXSelector,void* ptr){
FXEvent *ev=(FXEvent*)ptr;
FXDCWindow dc(this,ev);
FXint xx,yy;
xx=LEADSPACE;
// Grayed out
if(!isEnabled()){
dc.setForeground(backColor);
dc.fillRectangle(0,0,width,height);
if(!label.empty()){
yy=font->getFontAscent()+(height-font->getFontHeight())/2;
dc.setFont(font);
dc.setForeground(hiliteColor);
dc.drawText(xx+1,yy+1,label);
if(!accel.empty()) dc.drawText(width-TRAILSPACE-font->getTextWidth(accel)+1,yy+1,accel);
if(0<=hotoff) dc.fillRectangle(xx+font->getTextWidth(&label[0],hotoff)+1,yy+2,font->getTextWidth(&label[hotoff],wclen(&label[hotoff])),1);
dc.setForeground(shadowColor);
dc.drawText(xx,yy,label);
if(!accel.empty()) dc.drawText(width-TRAILSPACE-font->getTextWidth(accel),yy,accel);
if(0<=hotoff) dc.fillRectangle(xx+font->getTextWidth(&label[0],hotoff),yy+1,font->getTextWidth(&label[hotoff],wclen(&label[hotoff])),1);
}
}
// Active
else if(isActive()){
dc.setForeground(selbackColor);
dc.fillRectangle(0,0,width,height);
if(!label.empty()){
yy=font->getFontAscent()+(height-font->getFontHeight())/2;
dc.setFont(font);
dc.setForeground(isEnabled() ? seltextColor : shadowColor);
dc.drawText(xx,yy,label);
if(!accel.empty()) dc.drawText(width-TRAILSPACE-font->getTextWidth(accel),yy,accel);
if(0<=hotoff) dc.fillRectangle(xx+font->getTextWidth(&label[0],hotoff),yy+1,font->getTextWidth(&label[hotoff],wclen(&label[hotoff])),1);
}
}
// Normal
else{
dc.setForeground(backColor);
dc.fillRectangle(0,0,width,height);
if(!label.empty()){
yy=font->getFontAscent()+(height-font->getFontHeight())/2;
dc.setFont(font);
dc.setForeground(textColor);
dc.drawText(xx,yy,label);
if(!accel.empty()) dc.drawText(width-TRAILSPACE-font->getTextWidth(accel),yy,accel);
if(0<=hotoff) dc.fillRectangle(xx+font->getTextWidth(&label[0],hotoff),yy+1,font->getTextWidth(&label[hotoff],wclen(&label[hotoff])),1);
}
}
// Draw the box
xx=5;
yy=(height-9)/2;
if(!isEnabled())
dc.setForeground(backColor);
else
dc.setForeground(boxColor);
dc.fillRectangle(xx+1,yy+1,8,8);
dc.setForeground(shadowColor);
dc.drawRectangle(xx,yy,9,9);
// Draw the check
if(check!=false){
FXSegment seg[6];
seg[0].x1=2+xx; seg[0].y1=4+yy; seg[0].x2=4+xx; seg[0].y2=6+yy;
seg[1].x1=2+xx; seg[1].y1=5+yy; seg[1].x2=4+xx; seg[1].y2=7+yy;
seg[2].x1=2+xx; seg[2].y1=6+yy; seg[2].x2=4+xx; seg[2].y2=8+yy;
seg[3].x1=4+xx; seg[3].y1=6+yy; seg[3].x2=8+xx; seg[3].y2=2+yy;
seg[4].x1=4+xx; seg[4].y1=7+yy; seg[4].x2=8+xx; seg[4].y2=3+yy;
seg[5].x1=4+xx; seg[5].y1=8+yy; seg[5].x2=8+xx; seg[5].y2=4+yy;
if(isEnabled()){
if(check==maybe)
dc.setForeground(shadowColor);
else
dc.setForeground(textColor);
}
else{
dc.setForeground(shadowColor);
}
dc.drawLineSegments(seg,6);
}
return 1;
}
// Set box color
void FXMenuCheck::setBoxColor(FXColor clr){
if(clr!=boxColor){
boxColor=clr;
update();
}
}
// Save object to stream
void FXMenuCheck::save(FXStream& store) const {
FXMenuCommand::save(store);
store << check;
store << boxColor;
}
// Load object from stream
void FXMenuCheck::load(FXStream& store){
FXMenuCommand::load(store);
store >> check;
store >> boxColor;
}
}
|