📄 slider.c
字号:
/*
*******************************************************************************
* The real-time kernel "rtCell" *
* Copyright 2005 taowentao, allrights reserved. *
* File : Slider.c *
* By : taowentao 2006-09-02 2007-05-20 *
*******************************************************************************
*/
#if !defined(SLIDER_H)
#include "giCell\Wins\include\Slider.h"
#endif
/*
*******************************************************************************
* *
*******************************************************************************
*/
static COLOR SliderColor = GD_GRAY;
/*
*******************************************************************************
* *
*******************************************************************************
*/
static void SliderPaint(VIEW *pView)
{
SLIDER* pSlider;
int x, y, y0, xSize;
long val;
RECT r;
POINT pt;
pSlider = OBJ_FROM_VIEW(pView);
if (pSlider->Align & CTRL_ALIGN_VCENTER)
xSize = pView->viewRect.bottom-pView->viewRect.top+1;
else
xSize = pView->viewRect.right-pView->viewRect.left+1;
xSize -= ((SLOT_XOFF<<1)+SLIDERBOX_W);
val = pSlider->Value-pSlider->Min;
val = (val*xSize+((pSlider->Max-pSlider->Min+1)>>1));
val /= (pSlider->Max-pSlider->Min);
x = (int)val;
if (pSlider->Align & CTRL_ALIGN_VCENTER)
x = (pView->viewRect.bottom-SLOT_XOFF-x-SLIDERBOX_W+1);
else
x += (pView->viewRect.left+SLOT_XOFF);
FillRect(&(pView->viewRect), pSlider->SliderColor);
/* get box position, and draw it */
if (pSlider->Align & CTRL_ALIGN_VCENTER) {
GetOrgFromAlign(SLOT_H, xSize+SLIDERBOX_W, &(pView->viewRect),
CTRL_ALIGN_HCENTER|CTRL_ALIGN_VCENTER, &pt);
r.left = pt.x; r.right = pt.x+SLOT_H-1;
r.top = pt.y; r.bottom = pt.y+xSize+SLIDERBOX_W-1;
DrawDownRect(&r);
r.left = pt.x-BOX_YOFF; r.right = r.left+SLIDERBOX_H-1;
r.top = x; r.bottom = x+SLIDERBOX_W-1;
DrawUpBotton(&r);
y0 = pt.y+(SLIDERBOX_W>>1)-1;
DrawHLine(y0, r.left-2-HLINE_H, r.left-2, GD_BLACK);
DrawHLine(y0, r.right+2, r.right+2+HLINE_H, GD_BLACK);
for (x = 1; x < pSlider->Scales; x++) {
y = y0 + ((x*xSize)/pSlider->Scales);
DrawHLine(y, r.left-2-LINE_H, r.left-2, GD_BLACK);
DrawHLine(y, r.right+2, r.right+2+LINE_H, GD_BLACK);
}
y = y0 + ((x*xSize)/pSlider->Scales);
DrawHLine(y, r.left-2-HLINE_H, r.left-2, GD_BLACK);
DrawHLine(y, r.right+2, r.right+2+HLINE_H, GD_BLACK);
} else {
GetOrgFromAlign(xSize+SLIDERBOX_W, SLOT_H, &(pView->viewRect),
CTRL_ALIGN_HCENTER|CTRL_ALIGN_VCENTER, &pt);
r.left = pt.x; r.right = pt.x+xSize+SLIDERBOX_W-1;
r.top = pt.y; r.bottom = pt.y+SLOT_H-1;
DrawDownRect(&r);
r.left = x; r.right = x+SLIDERBOX_W-1;
r.top = pt.y-BOX_YOFF; r.bottom = r.top+SLIDERBOX_H-1;
DrawUpBotton(&r);
y0 = pt.x+(SLIDERBOX_W>>1)-1;
DrawVLine(y0, r.top-2-HLINE_H, r.top-2, GD_BLACK);
DrawVLine(y0, r.bottom+2, r.bottom+2+HLINE_H, GD_BLACK);
for (x = 1; x < pSlider->Scales; x++) {
y = y0 + ((x*xSize)/pSlider->Scales);
DrawVLine(y, r.top-2-LINE_H, r.top-2, GD_BLACK);
DrawVLine(y, r.bottom+2, r.bottom+2+LINE_H, GD_BLACK);
}
y = y0 + ((x*xSize)/pSlider->Scales);
DrawVLine(y, r.top-2-HLINE_H, r.top-2, GD_BLACK);
DrawVLine(y, r.bottom+2, r.bottom+2+HLINE_H, GD_BLACK);
}
if (pSlider->Status & CTRL_MOUSE_CATCH) {
RECT box = r; box.left +=2; box.top += 2;
box.right -= 2; box.bottom -= 2;
FillRectColor2(&box, GD_WHITE, GD_LIGHTGRAY);
} else {
COLOR clr;
r.left += 2; r.right -= 2;
r.top += 2; r.bottom -= 2;
if (pSlider->Status & CTRL_CAN_DRAG_SLIDER) clr = GD_YELLOW;
else clr = pSlider->SliderColor;
FillRect(&r, clr);
}
if (pSlider->Status & CTRL_FOCUSSED) {
DrawFocusRect(&(pView->viewRect), 0, GD_BLACK);
}
}
static CBOOL _cdecl_ SliderViewProc(VMSG *pMsg)
{
SLIDER* pSlider = OBJ_FROM_VIEW(pMsg->pView);
int xSize, x;
long val;
MOUSE_INF mi;
KEY_INF ki;
RECT r;
POINT pt;
if (pSlider->Align & CTRL_ALIGN_VCENTER)
xSize = pMsg->pView->viewRect.bottom-pMsg->pView->viewRect.top+1;
else
xSize = pMsg->pView->viewRect.right-pMsg->pView->viewRect.left+1;
xSize -= ((SLOT_XOFF<<1)+SLIDERBOX_W);
val = pSlider->Value-pSlider->Min;
val = (val*xSize+((pSlider->Max-pSlider->Min+1)>>1));
val /= (pSlider->Max-pSlider->Min);
x = (int)val;
switch (pMsg->MsgID) {
case MSM_LB_DOWN:
if ((pSlider->Status & CTRL_FOCUSSED)) {
pSlider->Status |= CTRL_MOUSE_ENTER;
_drag_slider:
if (pSlider->Align & CTRL_ALIGN_VCENTER) {
GetOrgFromAlign(SLOT_H, xSize+SLIDERBOX_W, &(pMsg->pView->viewRect),
CTRL_ALIGN_HCENTER|CTRL_ALIGN_VCENTER, &pt);
x = (pMsg->pView->viewRect.bottom-SLOT_XOFF-x-SLIDERBOX_W+1);
r.left = pt.x-BOX_YOFF; r.right = r.left+SLIDERBOX_H-1;
r.top = x; r.bottom = x+SLIDERBOX_W-1;
} else {
GetOrgFromAlign(xSize+SLIDERBOX_W, SLOT_H, &(pMsg->pView->viewRect),
CTRL_ALIGN_HCENTER|CTRL_ALIGN_VCENTER, &pt);
x += (pMsg->pView->viewRect.left+SLOT_XOFF);
r.left = x; r.right = x+SLIDERBOX_W-1;
r.top = pt.y-BOX_YOFF; r.bottom = r.top+SLIDERBOX_H-1;
}
if (IsInRect(pMsg->pt.x, pMsg->pt.y, &r) == true) {
if (pMsg->MsgID == MSM_LB_DOWN) {
pSlider->Status |= CTRL_MOUSE_CATCH;
pSlider->Status |= CTRL_CATCH_MOVE;
} else if (pSlider->Status & CTRL_MOUSE_ENTER) {
pSlider->Status |= CTRL_CAN_DRAG_SLIDER;
} else {
pSlider->Status &= ~CTRL_CAN_DRAG_SLIDER;
}
} else {
pSlider->Status &= ~CTRL_CAN_DRAG_SLIDER;
}
UpdateViewRect(pMsg->pView, &r);
}
return (true);
case MSM_LB_UP:
pSlider->Status &= ~(CTRL_MOUSE_CATCH|CTRL_CATCH_MOVE);
goto _drag_slider;
case MSM_MS_ENTER:
pSlider->Status |= CTRL_MOUSE_ENTER;
SetMouseCursor(ARROW);
goto _drag_slider;
case MSM_MS_LEAVE:
pSlider->Status &= ~(CTRL_MOUSE_ENTER|CTRL_CAN_DRAG_SLIDER);
goto _drag_slider;
case MSM_MS_MOVE:
mi.raw = pMsg->vParam;
if ((pSlider->Status & CTRL_MOUSE_CATCH) && (mi.inf.lbtn != 0)) {
if (pSlider->Status & CTRL_CATCH_MOVE) {
if (pSlider->Align & CTRL_ALIGN_VCENTER) {
GetOrgFromAlign(SLOT_H, xSize+SLIDERBOX_W, &(pMsg->pView->viewRect),
CTRL_ALIGN_HCENTER|CTRL_ALIGN_VCENTER, &pt);
r.left = pt.x-BOX_YOFF; r.right = r.left+SLIDERBOX_H-1;
r.top = pt.y; r.bottom = pt.y+xSize+SLIDERBOX_W-1;
x = pMsg->pt.y;
x = ((pMsg->pView->viewRect.bottom-SLOT_XOFF)-(SLIDERBOX_W>>1)-x);
} else {
GetOrgFromAlign(xSize+SLIDERBOX_W, SLOT_H, &(pMsg->pView->viewRect),
CTRL_ALIGN_HCENTER|CTRL_ALIGN_VCENTER, &pt);
r.left = pt.x; r.right = pt.x+xSize+SLIDERBOX_W-1;
r.top = pt.y-BOX_YOFF; r.bottom = r.top+SLIDERBOX_H-1;
x = pMsg->pt.x;
x -= ((pMsg->pView->viewRect.left+SLOT_XOFF)+(SLIDERBOX_W>>1));
}
if (x < 0) {
x = 0;
} else if (x > xSize) {
x = xSize;
}
val = x;
val = (val*(pSlider->Max-pSlider->Min)+((xSize+1)>>1));
val /= xSize;
val += pSlider->Min;
if (val < pSlider->Min) val = pSlider->Min;
if (val > pSlider->Max) val = pSlider->Max;
pSlider->Value = (int)val;
UpdateViewRect(pMsg->pView, &r);
if (pSlider->pOnSlider != NULL) {
(*(pSlider->pOnSlider))((int)val, pSlider->Min, pSlider->Max);
}
}
} else if (pSlider->Status & CTRL_MOUSE_ENTER) {
goto _drag_slider;
} else {
pSlider->Status &= ~CTRL_CAN_DRAG_SLIDER;
}
return (true);
case KEYM_KEY_DOWN:
ki.raw = pMsg->vParam;
switch (ki.inf.scan) {
case SCANCODE_CURSORBLOCKLEFT:
case SCANCODE_CURSORLEFT:
case SCANCODE_CURSORBLOCKDOWN:
case SCANCODE_CURSORDOWN:
if (pSlider->Value > pSlider->Min) {
int vo = (int)(((CWORD)(pSlider->Max-pSlider->Min))>>4);
vo = vo > 0? vo:1;
pSlider->Value -= vo;
goto _update_slider;
}
return (true);
case SCANCODE_CURSORBLOCKRIGHT:
case SCANCODE_CURSORRIGHT:
case SCANCODE_CURSORBLOCKUP:
case SCANCODE_CURSORUP:
if (pSlider->Value < pSlider->Max) {
int vo = (int)(((CWORD)(pSlider->Max-pSlider->Min))>>4);
vo = vo > 0? vo:1;
pSlider->Value += vo;
_update_slider:
if (pSlider->Value < pSlider->Min) pSlider->Value = pSlider->Min;
if (pSlider->Value > pSlider->Max) pSlider->Value = pSlider->Max;
UpdateView(pMsg->pView);
if (pSlider->pOnSlider != NULL) {
(*(pSlider->pOnSlider))(pSlider->Value, pSlider->Min, pSlider->Max);
}
}
}
return (true);
default:
return CtrlViewProc(pMsg, SliderPaint, &(pSlider->Status));
}
}
void SetSliderValue(SLIDER *pSlider, const int value)
{
if (value >= pSlider->Min && value <= pSlider->Max) {
if (pSlider->Value == value) return;
pSlider->Value = value;
UpdateView(pSlider->pView);
}
}
SLIDER* CreateSlider(int left, int top, int width, int height, VIEW* pParent,
PSLIDER pOnSlider, CWORD Style, int value, int min, int max)
{
VIEW *pView;
SLIDER *pSlider;
if (pParent == NULL)
return (NULL);
pView = CreateControl(left, top, width, height, pParent, 0,
SliderViewProc, sizeof(SLIDER));
if (pView == NULL) return (NULL);
pSlider = OBJ_FROM_VIEW(pView);
pSlider->pView = pView;
pSlider->SliderColor = SliderColor;
pSlider->pOnSlider = pOnSlider;
pSlider->Status = 0;
if (Style & CTRL_ALIGN_VCENTER)
pSlider->Align = CTRL_ALIGN_VCENTER;
else
pSlider->Align = CTRL_ALIGN_HCENTER;
pSlider->Value = value;
pSlider->Max = max;
pSlider->Min = min;
pSlider->Scales = SCALES;
ShowView(pSlider->pView);
return (pSlider);
}
void DeleteSlider(SLIDER *pSlider)
{
if (pSlider == NULL) return;
DeleteView(pSlider->pView);
}
/*
*******************************************************************************
* *
*******************************************************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -