📄 trackbar.c
字号:
/*
** $Id: trackbar.c,v 1.33 2004/09/22 06:02:26 weiym Exp $
**
** trackbar.c: the TrackBar Control module.
**
** Copyright (C) 2003, 2004 Feynman Software.
** Copyright (C) 2000 ~ 2002 Wei Yongming.
**
** NOTE: Originally by Zheng Yiran
**
** Create date: 2000/12/02
*/
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
** TODO:
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "minigui.h"
#include "gdi.h"
#include "window.h"
#include "control.h"
#include "cliprect.h"
#include "internals.h"
#include "ctrlclass.h"
#ifdef _CTRL_TRACKBAR
#include "ctrlmisc.h"
#include "trackbar.h"
#ifdef _PHONE_WINDOW_STYLE
static const BITMAP *bmp_hbg, *bmp_hslider;
static const BITMAP *bmp_vbg, *bmp_vslider;
#define WIDTH_HORZ_SLIDER (bmp_hslider->bmWidth)
#define HEIGHT_VERT_SLIDER (bmp_vslider->bmHeight)
#else /* not _PHONE_WINDOW_STYLE */
/* use these definition when drawing trackbar */
#define TB_BORDER 2
#define WIDTH_HORZ_SLIDER 24
#define HEIGHT_HORZ_SLIDER 12
#define WIDTH_VERT_SLIDER 12
#define HEIGHT_VERT_SLIDER 24
#define WIDTH_VERT_RULER 6
#define HEIGHT_HORZ_RULER 6
/* please keep it even for good appearance */
#define LEN_TICK 4
#define GAP_TIP_SLIDER 12
#define GAP_TICK_SLIDER 6
#endif /* not _PHONE_WINDOW_STYLE */
static int TrackBarCtrlProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam);
BOOL RegisterTrackBarControl (void)
{
WNDCLASS WndClass;
#ifdef _PHONE_WINDOW_STYLE
if ((bmp_hbg = GetStockBitmap (STOCKBMP_TRACKBAR_HBG, 0, 0)) == NULL)
return FALSE;
if ((bmp_hslider = GetStockBitmap (STOCKBMP_TRACKBAR_HSLIDER, 0, 0)) == NULL)
return FALSE;
if ((bmp_vbg = GetStockBitmap (STOCKBMP_TRACKBAR_VBG, 0, 0)) == NULL)
return FALSE;
if ((bmp_vslider = GetStockBitmap (STOCKBMP_TRACKBAR_VSLIDER, 0, 0)) == NULL)
return FALSE;
#endif
WndClass.spClassName = "trackbar";
WndClass.dwStyle = WS_NONE;
WndClass.dwExStyle = WS_EX_NONE;
WndClass.hCursor = GetSystemCursor (0);
WndClass.iBkColor = GetWindowElementColor (BKC_CONTROL_DEF);
WndClass.WinProc = TrackBarCtrlProc;
return AddNewControlClass (&WndClass) == ERR_OK;
}
#if 0
void TrackBarControlCleanup (void)
{
return;
}
#endif
#ifdef _PHONE_WINDOW_STYLE
static void GetTrackbarRects (TRACKBARDATA* pData, DWORD dwStyle, const RECT* rc_client, RECT* rc_ruler, RECT* rc_slider)
{
int l, t, w, h;
int pos, min, max;
int sliderx, slidery, sliderw, sliderh;
/* get data of trackbar. */
w = RECTWP (rc_client);
h = RECTHP (rc_client);
pos = pData->nPos;
max = pData->nMax;
min = pData->nMin;
/* get width/height of slider. */
if (dwStyle & TBS_VERTICAL) {
sliderw = bmp_vslider->bmWidth;
sliderh = bmp_vslider->bmHeight;
h -= sliderh;
l = 0; t = sliderh>>1;
}
else {
sliderw = bmp_hslider->bmWidth;
sliderh = bmp_hslider->bmHeight;
w -= sliderw;
l = sliderw>>1; t = 0;
}
/* get the rectangle of the ruler. */
if (rc_ruler) {
SetRect (rc_ruler, l, t, l + w, t + h);
}
/* get the rectangle of the slider according to position. */
if (dwStyle & TBS_VERTICAL) {
sliderx = (w - sliderw)>>1;
slidery = t + (int)((max - pos) * h / (float)(max - min)) - (sliderh>>1);
}
else {
slidery = (h - sliderh)>>1;
sliderx = l + (int)((pos - min) * w / (float)(max - min)) - (sliderw>>1);
}
SetRect (rc_slider, sliderx, slidery, sliderx + sliderw, slidery + sliderh);
}
static void TrackBarOnDraw (HWND hwnd, HDC hdc, TRACKBARDATA* pData, DWORD dwStyle)
{
RECT rc_client, rc_ruler, rc_slider;
GetClientRect (hwnd, &rc_client);
GetTrackbarRects (pData, dwStyle, &rc_client, &rc_ruler, &rc_slider);
/* draw the ruler. */
if (dwStyle & TBS_VERTICAL)
DrawBoxFromBitmap (hdc, &rc_ruler, bmp_vbg, FALSE, FALSE);
else
DrawBoxFromBitmap (hdc, &rc_ruler, bmp_hbg, TRUE, FALSE);
/* draw the slider. */
FillBoxWithBitmap (hdc, rc_slider.left, rc_slider.top, 0, 0,
(dwStyle & TBS_VERTICAL) ? bmp_vslider : bmp_hslider);
}
#else /* not _PHONE_WINDOW_STYLE */
static void GetTrackbarRects (TRACKBARDATA* pData, DWORD dwStyle, const RECT* rc_client, RECT* rc_ruler, RECT* rc_slider)
{
int x, y, w, h;
int pos, min, max;
int sliderx, slidery, sliderw, sliderh;
/* get data of trackbar. */
x = rc_client->left;
y = rc_client->top;
w = RECTWP (rc_client);
h = RECTHP (rc_client);
pos = pData->nPos;
max = pData->nMax;
min = pData->nMin;
if (dwStyle & TBS_BORDER) {
x += TB_BORDER;
y += TB_BORDER;
w -= TB_BORDER << 1;
h -= TB_BORDER << 1;
}
/* get the rectangle of the ruler. */
if (rc_ruler) {
if (dwStyle & TBS_VERTICAL) {
rc_ruler->left = x + ((w - WIDTH_VERT_RULER)>>1);
rc_ruler->top = y + (HEIGHT_VERT_SLIDER >> 1);
rc_ruler->right = x + ((w + WIDTH_VERT_RULER)>>1);
rc_ruler->bottom = y + h - (HEIGHT_VERT_SLIDER >> 1);
}
else {
rc_ruler->left = x + (WIDTH_HORZ_SLIDER >> 1);
rc_ruler->top = y + ((h - HEIGHT_HORZ_RULER)>>1);
rc_ruler->right = x + w - (WIDTH_HORZ_SLIDER >> 1);
rc_ruler->bottom = y + ((h + HEIGHT_HORZ_RULER)>>1);
}
}
/* get width/height of the slider. */
if (dwStyle & TBS_VERTICAL) {
sliderw = WIDTH_VERT_SLIDER;
sliderh = HEIGHT_VERT_SLIDER;
}
else {
sliderw = WIDTH_HORZ_SLIDER;
sliderh = HEIGHT_HORZ_SLIDER;
}
if (dwStyle & TBS_VERTICAL) {
sliderx = x + ((w - sliderw) >> 1);
slidery = y + (HEIGHT_VERT_SLIDER>>1)+ (int)((max - pos) *
(h - HEIGHT_VERT_SLIDER) / (float)(max - min)) - (sliderh>>1);
}
else {
slidery = y + ((h - sliderh) >> 1);
sliderx = x + (WIDTH_HORZ_SLIDER >> 1) + (int)((pos - min) *
(w - WIDTH_HORZ_SLIDER) / (float)(max - min)) - (sliderw>>1);
}
SetRect (rc_slider, sliderx, slidery, sliderx + sliderw, slidery + sliderh);
}
static void TrackBarOnDraw (HWND hwnd, HDC hdc, TRACKBARDATA* pData, DWORD dwStyle)
{
RECT rc_client, rc_ruler, rc_slider;
int x, y, w, h;
int max, min;
int TickFreq, EndTipLen;
int sliderx, slidery, sliderw, sliderh;
int TickStart, TickEnd;
float TickGap, Tick;
char sPos[10];
GetClientRect (hwnd, &rc_client);
GetTrackbarRects (pData, dwStyle, &rc_client, &rc_ruler, &rc_slider);
x = rc_client.left;
y = rc_client.top;
w = RECTW (rc_client);
h = RECTH (rc_client);
if (dwStyle & TBS_BORDER) {
x += TB_BORDER;
y += TB_BORDER;
w -= TB_BORDER << 1;
h -= TB_BORDER << 1;
}
/* get data of trackbar. */
TickFreq = pData->nTickFreq;
/* draw the border according to trackbar style. */
#ifdef _FLAT_WINDOW_STYLE
if (dwStyle & TBS_BORDER) {
DrawFlatControlFrameEx (hdc, hwnd,
rc_client.left, rc_client.top,
rc_client.right, rc_client.bottom,
0, DF_3DBOX_PRESSED | DF_3DBOX_NOTFILL, 0);
}
#else
if (dwStyle & TBS_BORDER) {
Draw3DControlFrameEx (hdc, hwnd,
rc_client.left, rc_client.top,
rc_client.right, rc_client.bottom,
DF_3DBOX_PRESSED | DF_3DBOX_NOTFILL, 0);
}
#endif
/* draw the rulder in middle of trackbar. */
#ifdef _FLAT_WINDOW_STYLE
DrawFlatControlFrameEx (hdc, hwnd,
rc_ruler.left, rc_ruler.top,
rc_ruler.right - 1, rc_ruler.bottom - 1,
0, DF_3DBOX_PRESSED | DF_3DBOX_NOTFILL, 0);
#else
Draw3DControlFrameEx (hdc, hwnd,
rc_ruler.left, rc_ruler.top,
rc_ruler.right - 1, rc_ruler.bottom - 1,
DF_3DBOX_PRESSED | DF_3DBOX_NOTFILL, 0);
#endif
max = pData->nMax;
min = pData->nMin;
sliderx = rc_slider.left;
slidery = rc_slider.top;
sliderw = RECTW(rc_slider);
sliderh = RECTH(rc_slider);
/* draw the tick of trackbar. */
if (!(dwStyle & TBS_NOTICK) && TickFreq < (max - min)) {
SetPenColor (hdc, PIXEL_black);
if (dwStyle & TBS_VERTICAL) {
TickStart = y + (HEIGHT_VERT_SLIDER >> 1);
TickGap = (h - HEIGHT_VERT_SLIDER) / (float)(max - min) * TickFreq;
TickEnd = y + h - (HEIGHT_VERT_SLIDER >> 1);
for (Tick = TickStart; Tick < TickEnd; Tick += TickGap) {
MoveTo (hdc, x + (w>>1) + (sliderw>>1) + GAP_TICK_SLIDER, (int) Tick);
LineTo (hdc, x + (w>>1) + (sliderw>>1) + GAP_TICK_SLIDER + LEN_TICK, (int) Tick);
}
MoveTo (hdc, x + (w>>1) + (sliderw>>1) + GAP_TICK_SLIDER, TickEnd);
LineTo (hdc, x + (w>>1) + (sliderw>>1) + GAP_TICK_SLIDER + LEN_TICK, TickEnd);
} else {
TickStart = x + (WIDTH_HORZ_SLIDER >> 1);
TickGap = (w - WIDTH_HORZ_SLIDER) / (float)(max - min) * TickFreq;
TickEnd = x + w - (WIDTH_HORZ_SLIDER >> 1);
for (Tick = TickStart; Tick < TickEnd; Tick += TickGap) {
MoveTo (hdc, (int)Tick, y + (h>>1) + (sliderh>>1) + GAP_TICK_SLIDER);
LineTo (hdc, (int)Tick, y + (h>>1) + (sliderh>>1) + GAP_TICK_SLIDER + LEN_TICK);
}
MoveTo (hdc, TickEnd, y + (h>>1) + (sliderh>>1) + GAP_TICK_SLIDER);
LineTo (hdc, TickEnd, y + (h>>1) + (sliderh>>1) + GAP_TICK_SLIDER + LEN_TICK);
}
}
/* draw the slider of trackbar according to pos. */
#ifdef _FLAT_WINDOW_STYLE
DrawFlatControlFrameEx (hdc, hwnd,
rc_slider.left, rc_slider.top,
rc_slider.right - 1, rc_slider.bottom - 1, 0,
DF_3DBOX_NORMAL | DF_3DBOX_FILL, GetWindowBkColor (hwnd));
#else
Draw3DControlFrameEx (hdc, hwnd,
rc_slider.left, rc_slider.top,
rc_slider.right - 1, rc_slider.bottom - 1,
DF_3DBOX_NORMAL | DF_3DBOX_FILL, GetWindowBkColor (hwnd));
#endif
SetPenColor (hdc, GetWindowElementColorEx (hwnd, FGC_CONTROL_NORMAL));
if (dwStyle & TBS_VERTICAL) {
MoveTo (hdc, sliderx + (sliderw >> 1) - 3, slidery + (sliderh >> 1));
LineTo (hdc, sliderx + (sliderw >> 1) + 3, slidery + (sliderh >> 1));
MoveTo (hdc, sliderx + (sliderw >> 1) - 2, slidery + (sliderh >> 1) - 2);
LineTo (hdc, sliderx + (sliderw >> 1) + 2, slidery + (sliderh >> 1) - 2);
MoveTo (hdc, sliderx + (sliderw >> 1) - 2, slidery + (sliderh >> 1) + 2);
LineTo (hdc, sliderx + (sliderw >> 1) + 2, slidery + (sliderh >> 1) + 2);
}
else {
MoveTo (hdc, sliderx + (sliderw >> 1), slidery + (sliderh >> 1) - 3);
LineTo (hdc, sliderx + (sliderw >> 1), slidery + (sliderh >> 1) + 3);
MoveTo (hdc, sliderx + (sliderw >> 1) - 2, slidery + (sliderh >> 1) - 2);
LineTo (hdc, sliderx + (sliderw >> 1) - 2, slidery + (sliderh >> 1) + 2);
MoveTo (hdc, sliderx + (sliderw >> 1) + 2, slidery + (sliderh >> 1) - 2);
LineTo (hdc, sliderx + (sliderw >> 1) + 2, slidery + (sliderh >> 1) + 2);
}
/* draw the tip of trackbar. */
if ((dwStyle & TBS_TIP) && !(dwStyle & TBS_VERTICAL)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -