📄 propsheet.c
字号:
/*
** $Id: propsheet.c,v 1.37 2004/08/12 02:55:12 weiym Exp $
**
** propsheet.c: the Property Sheet (Tab) control.
**
** Copyright (C) 2003 Feynman Software.
** Copyright (C) 2001 ~ 2002 Wei Yongming and others.
**
** NOTE: Originally by Wang Jian and Jiang Jun.
**
** Create date: 2001/11/20
*/
/*
** 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_PROPSHEET
#include "propsheet.h"
#include "ctrlmisc.h"
int GUIAPI DefaultPageProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
HWND hCurFocus;
switch (message) {
case MSG_DLG_GETDEFID:
{
HWND hDef;
hDef = GetDlgDefPushButton (hWnd);
if (hDef)
return GetDlgCtrlID (hDef);
return 0;
}
case MSG_DESTROY:
DestroyAllControls (hWnd);
break;
case MSG_DLG_SETDEFID:
{
HWND hOldDef;
HWND hNewDef;
hNewDef = GetDlgItem (hWnd, wParam);
if (SendMessage (hNewDef, MSG_GETDLGCODE, 0, 0L) & DLGC_PUSHBUTTON) {
hOldDef = GetDlgDefPushButton (hWnd);
if (hOldDef) {
ExcludeWindowStyle (hOldDef, BS_DEFPUSHBUTTON);
InvalidateRect (hOldDef, NULL, TRUE);
}
IncludeWindowStyle (hNewDef, BS_DEFPUSHBUTTON);
InvalidateRect (hNewDef, NULL, TRUE);
return (int)hOldDef;
}
break;
}
case MSG_KEYDOWN:
if ((hCurFocus = GetFocusChild (hWnd))) {
if (SendMessage (hCurFocus, MSG_GETDLGCODE, 0, 0L)
& DLGC_WANTALLKEYS)
break;
}
switch (LOWORD (wParam)) {
case SCANCODE_ENTER:
{
HWND hDef;
if (SendMessage (hCurFocus, MSG_GETDLGCODE, 0, 0L) & DLGC_PUSHBUTTON)
break;
hDef = GetDlgDefPushButton (hWnd);
if (hDef) {
SendMessage (hWnd, MSG_COMMAND, GetDlgCtrlID (hDef), 0L);
return 0;
}
break;
}
case SCANCODE_ESCAPE:
SendMessage (hWnd, MSG_COMMAND, IDCANCEL, 0L);
return 0;
case SCANCODE_TAB:
{
HWND hNewFocus;
if (hCurFocus) {
if (SendMessage (hCurFocus, MSG_GETDLGCODE, 0, 0L)
& DLGC_WANTTAB)
break;
}
if (lParam & KS_SHIFT)
hNewFocus = GetNextDlgTabItem (hWnd, hCurFocus, TRUE);
else
hNewFocus = GetNextDlgTabItem (hWnd, hCurFocus, FALSE);
if (hNewFocus != hCurFocus) {
SetFocus (hNewFocus);
// SendMessage (hWnd, MSG_DLG_SETDEFID, GetDlgCtrlID (hNewFocus), 0L);
}
return 0;
}
case SCANCODE_CURSORBLOCKDOWN:
case SCANCODE_CURSORBLOCKRIGHT:
case SCANCODE_CURSORBLOCKUP:
case SCANCODE_CURSORBLOCKLEFT:
{
HWND hNewFocus;
if (hCurFocus) {
if (SendMessage (hCurFocus, MSG_GETDLGCODE, 0, 0L)
& DLGC_WANTARROWS)
break;
}
if (LOWORD (wParam) == SCANCODE_CURSORBLOCKDOWN
|| LOWORD (wParam) == SCANCODE_CURSORBLOCKRIGHT)
hNewFocus = GetNextDlgGroupItem (hWnd, hCurFocus, FALSE);
else
hNewFocus = GetNextDlgGroupItem (hWnd, hCurFocus, TRUE);
if (hNewFocus != hCurFocus) {
if (SendMessage (hCurFocus, MSG_GETDLGCODE, 0, 0L)
& DLGC_STATIC)
return 0;
SetFocus (hNewFocus);
// SendMessage (hWnd, MSG_DLG_SETDEFID, GetDlgCtrlID (hNewFocus), 0L);
if (SendMessage (hNewFocus, MSG_GETDLGCODE, 0, 0L)
& DLGC_RADIOBUTTON) {
SendMessage (hNewFocus, BM_CLICK, 0, 0L);
ExcludeWindowStyle (hCurFocus, WS_TABSTOP);
IncludeWindowStyle (hNewFocus, WS_TABSTOP);
}
}
return 0;
}
break;
}
break;
}
return DefaultControlProc (hWnd, message, wParam, lParam);
}
static int PropSheetCtrlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam);
BOOL RegisterPropSheetControl (void)
{
WNDCLASS WndClass;
WndClass.spClassName = CTRL_PROPSHEET;
WndClass.dwStyle = WS_NONE;
WndClass.dwExStyle = WS_EX_NONE;
WndClass.hCursor = GetSystemCursor (0);
WndClass.iBkColor = GetWindowElementColor (BKC_CONTROL_DEF);
WndClass.WinProc = PropSheetCtrlProc;
return AddNewControlClass (&WndClass) == ERR_OK;
}
/**************************internal functions******************************/
#define _ICON_OFFSET 2
#define _GAP_ICON_TEXT 2
#define _MIN_TAB_WIDTH 10
/* destroy a page */
static void destroy_page (PROPPAGE * page)
{
FreeFixStr (page->title);
DestroyWindow (page->hwnd);
}
/* change page title */
static int set_page_title (PROPPAGE * page, char *ptr)
{
int len = 0;
if (page->title) {
FreeFixStr (page->title);
}
if (ptr) {
len = strlen (ptr);
if (!(page->title = FixStrAlloc (len)))
return PS_ERR;
strcpy (page->title, ptr);
}
else {
page->title = NULL;
}
return PS_OKAY;
}
/* create a new page */
static BOOL create_page (HWND hwnd, PPROPSHEETDATA pData, PROPPAGE* page, PDLGTEMPLATE pDlgTemplate, WNDPROC proc)
{
int i, len;
PCTRLDATA pCtrlData;
HWND hCtrl;
RECT rcPage;
if (!pDlgTemplate->controls)
return FALSE;
GetClientRect (hwnd, &rcPage);
page->hwnd = CreateWindowEx (CTRL_STATIC, "", SS_LEFT, WS_EX_NONE,
IDC_STATIC,
2, pData->head_rc.bottom + 2,
rcPage.right - 4, rcPage.bottom - 4 - pData->head_rc.bottom,
hwnd, pDlgTemplate->dwAddData);
if (page->hwnd == HWND_INVALID)
return FALSE;
len = strlen (pDlgTemplate->caption);
page->title = FixStrAlloc (len);
if (page->title)
strcpy (page->title, pDlgTemplate->caption);
page->proc = proc;
page->icon = pDlgTemplate->hIcon;
for (i = 0; i < pDlgTemplate->controlnr; i++) {
pCtrlData = pDlgTemplate->controls + i;
hCtrl = CreateWindowEx (pCtrlData->class_name,
pCtrlData->caption,
pCtrlData->dwStyle | WS_CHILD,
pCtrlData->dwExStyle,
pCtrlData->id,
pCtrlData->x, pCtrlData->y,
pCtrlData->w, pCtrlData->h,
page->hwnd,
pCtrlData->dwAddData);
if (hCtrl == HWND_INVALID) {
DestroyWindow (page->hwnd);
if (page->title)
FreeFixStr (page->title);
return FALSE;
}
}
SetWindowCallbackProc (page->hwnd, page->proc);
SendMessage (page->hwnd, MSG_INITPAGE, 0, pDlgTemplate->dwAddData);
return TRUE;
}
static void show_hide_page (PPROPPAGE page, int show_cmd)
{
HWND focus;
ShowWindow (page->hwnd, show_cmd);
focus = GetNextDlgTabItem (page->hwnd, (HWND)0, 0);
if (SendMessage (page->hwnd, MSG_SHOWPAGE, focus, show_cmd) && show_cmd == SW_SHOW) {
if (focus)
SetFocus (focus);
}
}
/* recalculate widths of the tabs after a page added or removed. */
static void recalc_tab_widths (HWND hwnd, PPROPSHEETDATA pData, DWORD dwStyle)
{
PPROPPAGE page;
if (pData->page_count == 0)
return;
if (dwStyle & PSS_COMPACTTAB) {
HDC hdc;
SIZE ext;
int total_width = 0;
hdc = GetClientDC (hwnd);
page = pData->head;
while (page) {
page->width = 0;
if (page->title) {
GetTextExtent (hdc, page->title, -1, &ext);
page->width = ext.cx + 4;
}
if (page->icon)
page->width += GetMainWinMetrics (MWM_ICONX);
if (page->width < _MIN_TAB_WIDTH)
page->width = _MIN_TAB_WIDTH;
total_width += page->width;
page = page->next;
}
ReleaseDC (hdc);
while (total_width > pData->head_rc.right) {
int new_width = 0;
page = pData->head;
while (page) {
page->width -= 2;
if (page->width < _MIN_TAB_WIDTH)
page->width = _MIN_TAB_WIDTH;
new_width += page->width;
page = page->next;
}
total_width = new_width;
}
pData->head_width = total_width;
}
else {
int width;
width = pData->head_rc.right * 8 / (pData->page_count * 10);
if (width < _MIN_TAB_WIDTH)
width = pData->head_rc.right / pData->page_count;
page = pData->head;
while (page) {
page->width = width;
page = page->next;
}
pData->head_width = width * pData->page_count;
}
}
/* resize children after the sheet resized. */
static void resize_children (PPROPSHEETDATA pData, const RECT* rcPage)
{
PPROPPAGE page;
page = pData->head;
while (page) {
MoveWindow (page->hwnd,
0, pData->head_rc.bottom,
rcPage->right,
rcPage->bottom - pData->head_rc.bottom,
page == pData->active);
page = page->next;
}
}
/* return the page from index. */
static PPROPPAGE get_page (PPROPSHEETDATA pData, int index)
{
int i = 0;
PPROPPAGE page;
page = pData->head;
while (page) {
if (i == index)
return page;
i++;
page = page->next;
}
return NULL;
}
/* append a new page */
static int append_page (PPROPSHEETDATA pData, PPROPPAGE new_page)
{
int i = 0;
PPROPPAGE page;
page = pData->head;
while (page && page->next) {
i++;
page = page->next;
}
new_page->next = NULL;
if (page) {
i++;
page->next = new_page;
}
else {
pData->head = new_page;
}
pData->page_count++;
return i;
}
/* remove a page */
static void remove_page (PPROPSHEETDATA pData, PPROPPAGE rm_page)
{
PPROPPAGE page, prev;
prev = NULL;
page = pData->head;
while (page) {
if (page == rm_page) {
if (prev) {
prev->next = rm_page->next;
}
else {
pData->head = rm_page->next;
}
pData->page_count--;
break;
}
prev = page;
page = page->next;
}
}
/* window procedure of the property sheet. */
static int PropSheetCtrlProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam)
{
PCONTROL pCtrl;
PPROPSHEETDATA pData;
pCtrl = Control (hwnd);
pData = (PROPSHEETDATA *) pCtrl->dwAddData2;
switch (message) {
case MSG_CREATE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -