
;-----------------------------------------
;   Toolbar.asm -- Toolbar Demonstration
;   based on Murray & Pappas's Toolbar.C
;
;   Translated into assembly 28/1/00 by
;		Ron Thomas
;   Ron_Thom@Compuserve.com	
;-----------------------------------------

.386				; 32 bit when .386 appears before .MODEL
.MODEL FLAT,STDCALL

include windows.inc

include user32.inc
include kernel32.inc
include gdi32.inc
include	comctl32.inc

includelib user32.lib
includelib kernel32.lib
includelib gdi32.lib
includelib comctl32.lib

WinMain		PROTO :DWORD, :DWORD, :DWORD, :SDWORD
DefToolbar	PROTO 

.data

IDTB_BITMAP	EQU	101
IDTB_TOOLBAR	EQU	201

IDM_SMALL	EQU	501
IDM_MEDIUM	EQU	502
IDM_LARGE	EQU	503

IDM_BLACK	EQU	601
IDM_WHITE	EQU	602
IDM_RED		EQU	603
IDM_YELLOW	EQU	604
IDM_GREEN	EQU	605
IDM_BLUE	EQU	606
	
TOTALBUTTONS	EQU	6

wColorValue	db	0,0,0,0,  255,255,255,0,  255,0,0,0,  255,255,0,0,  0,255,0,0,  0,0,255,0
;			black	   white        red       yellow     green      blue

wSize		dw	5*3
			
szMenuName	db	'Alteration', 0		; name of menu in resource
szProgName	db	"ToolBar Demo",0
szTitle		db	"A Toolbar with ToolTips",0
SurroundR	db	"Surround with colour", 0

black		db	"Black",0
white		db	"White",0
red		db	"Red",0
yellow		db	"Yellow",0
green		db	"Green",0
blue		db	"Blue",0
	
.data?

TBButtons	TBBUTTON	TOTALBUTTONS DUP ({})

hInstance HINSTANCE 	?
CommandLine LPSTR 	?

cxClient	dd	?
cyClient	dd	?
hAccel		dd	?
hTBWnd		dd	?
wColor		dw	?

;Macros

include macros.mac
;----------------------------------------------------------------------------
.code
start:
	invoke GetModuleHandle, NULL
	mov    hInstance,eax
	invoke GetCommandLine
        invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
	invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:SDWORD

	LOCAL wc:WNDCLASSEX, msg:MSG, hwnd:HWND

	.IF	!hPrevInst

	    mov   wc.cbSize,SIZEOF WNDCLASSEX
	    mov   wc.lpszClassName,OFFSET szProgName
	    M2M   wc.hInstance,hInstance
	    mov   wc.lpfnWndProc, OFFSET WndProc
	    invoke LoadCursor,NULL,IDC_ARROW
	    mov   wc.hCursor,eax
 	    mov   wc.hIcon,0
 	    mov   wc.hIconSm,0
	    mov   wc.lpszMenuName,offset szMenuName
	    mov   wc.hbrBackground,COLOR_WINDOW+1
	    mov   wc.style, CS_HREDRAW or CS_VREDRAW
	    mov   wc.cbClsExtra,NULL
	    mov   wc.cbWndExtra,NULL
       
            invoke RegisterClassEx, addr wc

	    .IF   !eax
		  return 0
	    .ENDIF

	.ENDIF
			
        invoke CreateWindowEx,NULL,ADDR szProgName,ADDR szTitle,\
           WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, \
	   CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL, hInst,NULL
        mov   hwnd,eax

	invoke LoadAccelerators, hInst, ADDR szMenuName

	.IF	!eax
		return 0
	.ENDIF

	invoke	DefToolbar
	
	invoke	CreateToolbarEx, hwnd, WS_VISIBLE or WS_CHILD or WS_BORDER or \
		TBSTYLE_TOOLTIPS,IDTB_TOOLBAR, TOTALBUTTONS, hInst, IDTB_BITMAP, \
		ADDR TBButtons, TOTALBUTTONS, 0, 0, 16, 16, sizeof TBBUTTON

	mov	hTBWnd,eax
   
        invoke ShowWindow, hwnd, CmdShow	; SW_SHOWNORMAL
        invoke UpdateWindow, hwnd

        .WHILE TRUE
                INVOKE GetMessage, ADDR msg,NULL,0,0
                .BREAK .IF (!eax)

		invoke	TranslateAccelerator, hwnd, hAccel, ADDR msg

		.IF !eax
                    invoke TranslateMessage, ADDR msg
                    invoke DispatchMessage, ADDR msg
		.ENDIF

        .ENDW

        mov     eax,msg.wParam
        ret
WinMain endp

WndProc proc uses ebx, hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

LOCAL ps:PAINTSTRUCT, hmenu:HMENU, hdc:HDC
LOCAL	TTStr:TOOLTIPTEXT

	mov   eax,uMsg

	.IF eax==WM_COMMAND

	    LOWORD  wParam
	  
	    .IF     eax==IDM_SMALL

	    	mov  wSize, 20

	    .ELSEIF eax==IDM_MEDIUM
	
		mov  wSize, 50

	    .ELSEIF eax==IDM_LARGE
 
		mov  wSize, 100

	    .ELSEIF eax==IDM_BLACK  || eax==IDM_WHITE || eax==IDM_RED  || \
		    eax==IDM_YELLOW || eax==IDM_GREEN || eax==IDM_BLUE
	
		invoke  GetMenu, hWnd
		mov	hmenu, eax
		xor	eax,eax
		mov	ax,wColor	
		invoke	CheckMenuItem, hmenu, eax, MF_UNCHECKED
		xor     eax,eax
		LOWORD  wParam
		mov	wColor, ax
		invoke  CheckMenuItem, hmenu, eax, MF_CHECKED

		xor	ebx,ebx
		mov	bx, wColor
		sub	bx, IDM_BLACK				; got index relative to Black
		shl	bx,2					; Convert to ColorVal index

		mov	al, byte ptr wColorValue[bx]		; red   value
		mov	cl, byte ptr wColorValue[bx + 1]	; green	
		mov	dl, byte ptr wColorValue[bx + 2]	; blue

		mov	bl,dl					; Put RGB values 	
		shl	ebx,8					; into COLORREF form 
		add	bl, cl					;
		shl	ebx,8					;
		add	bl,al					;
		and	ebx, 0FFFFFFh				;

		invoke	CreateSolidBrush, ebx			; Get the brush
		invoke	SetClassLong, hWnd, GCL_HBRBACKGROUND, eax

	    .ENDIF

	    invoke InvalidateRect, hWnd, 0, TRUE		; Update window

	.ELSEIF eax==WM_SIZE

		LOWORD	lParam
		mov	cxClient, eax
		HIWORD  lParam
		mov	cyClient, eax

	.ELSEIF	eax==WM_NOTIFY

		mov	eax, lParam
		mov	ebx, eax
		
		.IF TOOLTIPTEXT.hdr.code[ebx]==TTN_NEEDTEXT

		    mov	eax, TOOLTIPTEXT.hdr.idFrom[ebx]
	
		    .IF     eax==IDM_BLACK

			mov  TOOLTIPTEXT.lpszText[ebx], offset black

		    .ELSEIF eax==IDM_WHITE

			mov  TOOLTIPTEXT.lpszText[ebx], offset white
		    
		    .ELSEIF eax==IDM_RED

			mov  TOOLTIPTEXT.lpszText[ebx], offset red

		    .ELSEIF eax==IDM_YELLOW

			mov  TOOLTIPTEXT.lpszText[ebx], offset yellow

		    .ELSEIF eax==IDM_GREEN

			mov  TOOLTIPTEXT.lpszText[ebx], offset green

		    .ELSEIF eax==IDM_BLUE

			mov  TOOLTIPTEXT.lpszText[ebx], offset blue

		    .ENDIF

		.ENDIF
	    	
	.ELSEIF eax==WM_PAINT

		invoke  BeginPaint, hWnd, ADDR ps
		mov	hdc, eax

		invoke	SetMapMode, hdc, MM_ISOTROPIC
		invoke	SetWindowExtEx, hdc, 500, 500, 0
		mov	eax, cyClient
		neg	eax
		invoke  SetViewportExtEx, hdc, cxClient, eax, 0   	  

		mov	eax, cxClient
		shr	eax,1
		mov	ecx, cyClient
		shr	ecx,1

		invoke	SetViewportOrgEx, hdc, eax, ecx, 0

		xor	eax,eax
		mov	ax, wSize
		shl	ax, 1		
		mov	dx, ax		;  wSize*2
		neg	eax		;  -wSize*2
		mov	cx, wSize
		neg	cx
		push	ax

		invoke	Rectangle, hdc, ax, cx, dx, wSize

		pop	ax		; -wSize*2
		mov	ecx,eax		; 
		neg	ecx		; 
		sub	ecx,10		; wSize*2 - 10 
		
		invoke	TextOut, hdc, eax, ecx, ADDR SurroundR, 20 ; 20 = # chars in string
		invoke	ShowWindow, hTBWnd, SW_SHOW
		invoke	UpdateWindow, hTBWnd
		invoke	ValidateRect, hWnd, 0
		invoke	EndPaint, hWnd, ADDR ps
  
        .ELSEIF eax==WM_DESTROY

	      invoke PostQuitMessage,NULL 
	.ELSE
              invoke DefWindowProc,hWnd,uMsg,wParam,lParam
              ret
	.ENDIF

	return 0

WndProc endp

DefToolbar	proc uses ebx

;	Set up the 6 Buttons
;	--------------------

;	Instead of using code to define these buttons we could use initial values
;	for the members when the structure is defined. I leave it like this as possible
;	useful code for when buttons need to be changed dynamically. 
  
	mov	TBButtons.iBitmap,		0
	mov	TBButtons.idCommand,		IDM_BLACK
	mov	TBButtons.fsState, 		TBSTATE_ENABLED
	mov	TBButtons.fsStyle,		TBSTYLE_BUTTON
	mov	TBButtons.dwData,		0
	mov	TBButtons.iString,		0

	mov	ebx, sizeof	TBBUTTON	; Index the 2nd button

	mov	TBButtons[ebx].iBitmap,		0
	mov	TBButtons[ebx].idCommand,	IDM_WHITE
	mov	TBButtons[ebx].fsState, 	TBSTATE_ENABLED
	mov	TBButtons[ebx].fsStyle,		TBSTYLE_BUTTON
	mov	TBButtons[ebx].dwData,		0
	mov	TBButtons[ebx].iString,		0

	add	ebx, sizeof	TBBUTTON	; Index the 3rd button

	mov	TBButtons[ebx].iBitmap,		0
	mov	TBButtons[ebx].idCommand,	IDM_RED
	mov	TBButtons[ebx].fsState, 	TBSTATE_ENABLED
	mov	TBButtons[ebx].fsStyle,		TBSTYLE_BUTTON
	mov	TBButtons[ebx].dwData,		0
	mov	TBButtons[ebx].iString,		0

	add	ebx, sizeof	TBBUTTON

	mov	TBButtons[ebx].iBitmap,		0
	mov	TBButtons[ebx].idCommand,	IDM_YELLOW
	mov	TBButtons[ebx].fsState, 	TBSTATE_ENABLED
	mov	TBButtons[ebx].fsStyle,		TBSTYLE_BUTTON
	mov	TBButtons[ebx].dwData,		0
	mov	TBButtons[ebx].iString,		0

	add	ebx, sizeof	TBBUTTON

	mov	TBButtons[ebx].iBitmap,	0
	mov	TBButtons[ebx].idCommand,	IDM_GREEN
	mov	TBButtons[ebx].fsState, 	TBSTATE_ENABLED
	mov	TBButtons[ebx].fsStyle,		TBSTYLE_BUTTON
	mov	TBButtons[ebx].dwData,		0
	mov	TBButtons[ebx].iString,		0

	add	ebx, sizeof	TBBUTTON

	mov	TBButtons[ebx].iBitmap,		0
	mov	TBButtons[ebx].idCommand,	IDM_BLUE
	mov	TBButtons[ebx].fsState, 	TBSTATE_ENABLED
	mov	TBButtons[ebx].fsStyle,		TBSTYLE_BUTTON
	mov	TBButtons[ebx].dwData,		0
	mov	TBButtons[ebx].iString,		0

	ret
DefToolbar	endp
        end start

