home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
PRG
/
AESample.sit
/
AESample
/
sources
/
custom.wind.c
next >
Wrap
Text File
|
1996-06-22
|
11KB
|
408 lines
/*
*--------------------------------------------------------------
* custom.wind.c
*--------------------------------------------------------------
* I don't think this source is a good sample because I wrote
* this just for fun rather than providing a versatile purposes.
* Hence I named it custom.wind.c.
* In this sample source, button drawings require EmbossDraw.c
* that provides 3D-like lines, frames and texts drawing.
*--------------------------------------------------------------
*/
#include <AppleEvents.h>
#include "sample.h"
#include "sample.dlog.h"
#include "sample.send.h"
#include "sample.drag.h"
#include "custom.wind.h"
#include "EmbossDraw.h"
/*
*--------------------------------------------------------------
* main window resource IDs
*--------------------------------------------------------------
*/
enum customWindowIDs {
rMainWindID = 128,
rMainNameID = 128
};
/*
*--------------------------------------------------------------
* button number constants
*--------------------------------------------------------------
*/
enum customWindowItems {
mainOpenBttn = 0,
mainPrintBttn,
mainSendBttn,
mainAboutBttn,
mainQuitBttn,
kNumOfMainBttns
};
/*
*--------------------------------------------------------------
* button sizes
*--------------------------------------------------------------
*/
enum customWindowSizes{
kBttnHigh = 24,
kBttnWide = 96,
kMainHigh = kBttnHigh * kNumOfMainBttns,
kMainWide = kBttnWide
};
/*
*--------------------------------------------------------------
*mouse status
*--------------------------------------------------------------
*/
enum constMouseStatus {
kMouseReleased,
kMousePressed
};
/*
*--------------------------------------------------------------
* global of the main window
*--------------------------------------------------------------
*/
WindowRef gMainWindow;
/*
*--------------------------------------------------------------
* staic variables only used in this source
*--------------------------------------------------------------
*/
static Rect gMainBttns[kNumOfMainBttns];
static Boolean gBigState;
/*
*--------------------------------------------------------------
* static functions only used in this source
*--------------------------------------------------------------
*/
static void SetTheWindowStandardState(WindowRef, const RectPtr);
static short FindMyButton(Point);
static Boolean TrackMyButton(const short, Point);
static void DrawEmbossedBttn(const short, Boolean);
/*
*--------------------------------------------------------------
* macros
*--------------------------------------------------------------
*/
extern RectPtr GetTheButtonRect(const short item);
#define GetTheButtonRect(item) (&gMainBttns[(item)])
/*
*--------------------------------------------------------------
* SetUpMyMainWindow
*--------------------------------------------------------------
* create my main window, it will appear later
*--------------------------------------------------------------
*/
Boolean SetUpMyMainWindow(void)
{
GrafPtr savePort;
Str63 chicagoName;
short chicagoFNum;
int i;
/* load window resource */
gMainWindow = GetNewCWindow(rMainWindID, kInHeap, kFrontMost);
if (gMainWindow == nil) return (false);
GetPort(&savePort);
SetPortWindowPort(gMainWindow);
/* set window sizes */
SizeWindow(gMainWindow, kMainWide, kMainHigh, false);
gBigState = true;
/* set window font to Chicago at any international Systems */
GetIndString(chicagoName, rFontNameID, iChicagoIndex);
GetFNum(chicagoName, &chicagoFNum);
TextFont(chicagoFNum);
TextSize(12);
/* set button sizes and positions */
for (i = 0; i < kNumOfMainBttns; ++i) {
SetRect(&gMainBttns[i], 0, 0, kBttnWide, kBttnHigh);
OffsetRect(&gMainBttns[i], 0, kBttnHigh * i);
}
SetUpMyDragAndDrop(gMainWindow); /* this is not essential */
SetPort(savePort);
return (true);
}
/*
*--------------------------------------------------------------
* DrawMyMainWindow
*--------------------------------------------------------------
* draw contents of the main window
*--------------------------------------------------------------
*/
void DrawMyMainWindow(void)
{
GrafPtr savePort;
short i;
GetPort(&savePort);
SetPortWindowPort(gMainWindow);
for (i = mainOpenBttn; i < kNumOfMainBttns; ++i) {
DrawEmbossedBttn(i, kMouseReleased);
}
SetPort(savePort);
}
/*
*--------------------------------------------------------------
* ZoomMyMainWindow
*--------------------------------------------------------------
* custom window zooming handler that alters full size and
* one-button size
*--------------------------------------------------------------
*/
void ZoomMyMainWindow(const short zoom)
{
GrafPtr savedPort;
Rect newRect;
Point offsetPt;
GetPort(&savedPort);
SetPortWindowPort(gMainWindow);
if (gBigState) {
/* set window to one button big */
SetRect(&newRect, 0, 0, kMainWide, kBttnHigh);
gBigState = false;
} else {
/* set window to full size */
SetRect(&newRect, 0, 0, kMainWide, kMainHigh);
gBigState = true;
}
/* set the rectangle to global position */
offsetPt.v = 0, offsetPt.h = 0;
LocalToGlobal(&offsetPt);
OffsetRect(&newRect, offsetPt.h, offsetPt.v);
if (zoom == inZoomOut) {
SetTheWindowStandardState(gMainWindow, &newRect);
}
ZoomWindow(gMainWindow, zoom, false);
SetPort(savedPort);
}
/*
*--------------------------------------------------------------
* SetTheWindowStandardState
*--------------------------------------------------------------
* set the window zoom state
*--------------------------------------------------------------
*/
static void SetTheWindowStandardState(WindowRef theWindow, const RectPtr theRect)
{
/* use a new Window Accessor macro in the Windows.h */
SetWindowStandardState(theWindow, theRect);
}
/*
*--------------------------------------------------------------
* DoMyMainWindow
*--------------------------------------------------------------
* handle mouse event of main window
*--------------------------------------------------------------
*/
void DoMyMainWindow(Point mousePt)
{
GrafPtr savePort;
short aButton;
GetPort(&savePort);
SetPortWindowPort(gMainWindow);
GlobalToLocal(&mousePt);
aButton = FindMyButton(mousePt);
if (aButton >= mainOpenBttn && aButton <= mainQuitBttn) {
if (TrackMyButton(aButton, mousePt)) {
switch (aButton) {
case mainOpenBttn: DoOpenFile(); break;
case mainPrintBttn: DoPrintFile(); break;
case mainSendBttn: DoHoldFile(); break;
case mainAboutBttn: DoAboutMe(); break;
case mainQuitBttn: gForever = false; break;
}
}
}
SetPort(savePort);
}
/*
*--------------------------------------------------------------
* FindMyButton
*--------------------------------------------------------------
* find one of the button of main window
*--------------------------------------------------------------
*/
static short FindMyButton(Point thePt)
{
int i;
for (i = mainOpenBttn; i < kNumOfMainBttns; ++i) {
if (PtInRect(thePt, &gMainBttns[i])) {
return (i);
}
}
return (-1);
}
/*
*--------------------------------------------------------------
* TrackMyButton
*--------------------------------------------------------------
* track mouse position in the button rect
*--------------------------------------------------------------
*/
static Boolean TrackMyButton(
const short theItem,
Point thePt)
{
RectPtr aRect = GetTheButtonRect(theItem);
Boolean inTheRect = false;
if (StillDown()) {
do {
GetMouse(&thePt);
if (PtInRect(thePt, aRect)) {
if (inTheRect == false) {
DrawEmbossedBttn(theItem, inTheRect = true);
}
} else {
if (inTheRect) {
DrawEmbossedBttn(theItem, inTheRect = false);
}
}
} while (WaitMyMouseUp());
if (inTheRect) {
DrawEmbossedBttn(theItem, false);
}
}
return (inTheRect);
}
/*
*--------------------------------------------------------------
* DrawEmbossedBttn
*--------------------------------------------------------------
* draw embossed frame of main buttons
* it assumes that the GrafPort has been already set
*--------------------------------------------------------------
*/
static void DrawEmbossedBttn(const short theItem, Boolean isPressed)
{
RectPtr aRect = GetTheButtonRect(theItem);
Str255 itsName;
/* draw the button title */
GetIndString(itsName, rMainNameID, theItem + 1);
EmbossTextBox(&itsName[1], itsName[0], aRect, teCenter, isPressed, false);
/* draw the 3D frame */
if (isPressed) {
DentedRect(aRect);
} else {
RaisedRect(aRect);
}
}
/*
*--------------------------------------------------------------
* IsMouseInTheButtons
*--------------------------------------------------------------
* this routine is an alternative of WaitMouseUp
* it can give more working chance to background tasks
*--------------------------------------------------------------
*/
Boolean IsMouseInTheButtons(Point mousePt)
{
GrafPtr savePort;
RectPtr aRect = GetTheButtonRect(mainOpenBttn);
short currButton;
static short prevButton = -1;
Boolean nowIntheButton;
GetPort(&savePort);
SetPortWindowPort(gMainWindow);
/* check mouse location */
GlobalToLocal(&mousePt);
currButton = FindMyButton(mousePt);
nowIntheButton = ((currButton >= mainOpenBttn) && (currButton <= mainSendBttn));
if (prevButton != currButton) {
if ((prevButton >= mainOpenBttn) && (prevButton <= mainSendBttn)) {
/* draw previous button up */
DrawEmbossedBttn(prevButton, false);
}
if (nowIntheButton) {
/* draw current button down */
DrawEmbossedBttn(currButton, true);
}
prevButton = currButton;
}
SetPort(savePort);
return (nowIntheButton);
}
/*
*--------------------------------------------------------------
* DoReceiveInTheButtons
*--------------------------------------------------------------
*
*--------------------------------------------------------------
*/
OSErr DoReceiveOnTheButtons(const FSSpecPtr theSpec, Point mousePt)
{
GrafPtr savePort;
short aButton;
OSErr result = noErr;
GetPort(&savePort);
SetPortWindowPort(gMainWindow);
GlobalToLocal(&mousePt);
aButton = FindMyButton(mousePt);
switch (aButton) {
case mainOpenBttn:
result = SendSpecToMyself(theSpec, kAEOpenDocuments);
break;
case mainPrintBttn:
result = SendAEToFinder(theSpec, kAEPrintDocuments);
break;
case mainSendBttn:
OpenMyViewDialog(theSpec);
break;
}
SetPort(savePort);
return (result);
}
/*
*--------------------------------------------------------------
* WaitMyMouseUp
*--------------------------------------------------------------
* this routine is an alternative of WaitMouseUp
* it can give more working chance to background tasks
*--------------------------------------------------------------
*/
Boolean WaitMyMouseUp(void)
{
EventRecord anEvent;
if (WaitNextEvent(mUpMask, &anEvent, -1, nil)) {
if (anEvent.what == mouseUp) {
return (false);
}
}
return (true);
}