1. Trang chủ
  2. » Công Nghệ Thông Tin

Mac OS X Programming phần 10 potx

30 493 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 151,77 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Pass ShowWindow a reference to the window that it should make visible: WindowRef window; ShowWindow window ; HideWindow: Hiding a Visible Window A window is made visible by calling Show

Trang 1

ShowWindow( window );

DisposeNibReference( nibRef );

RunApplicationEventLoop();

Trang 2

Window Manager

A window is created using the Interface Builder Manager routine

through Window Manager routines The function prototypes for the Window Manager routines are located in the MacWindows.h header file The following sections describe commonly used window-related data types and functions

WindowRef: Referencing a Window

A window is referenced by way of a variable of type WindowRef A WindowRef is a pointer to a window object Your program obtains such a reference to a window at the time the window is created

Creating a Window

A window is created by calling CreateWindowFromNib to unarchive a window

resource stored in a nib file Refer to the "Interface Builder Manager (Nib Files)" section of this appendix for more information

ShowWindow: Showing a Hidden Window

A window created by calling CreateWindowFromNib is initially hidden (invisible) In addition, a window that has been the object of a call to HideWindow will be hidden In either case, call ShowWindow to make the window visible Pass ShowWindow a

reference to the window that it should make visible:

WindowRef window;

ShowWindow( window );

HideWindow: Hiding a Visible Window

A window is made visible by calling ShowWindow To hide (make invisible) a window, call HideWindow Pass HideWindow a reference to the window that it should make invisible:

WindowRef window;

HideWindow( window );

Trang 4

Control Manager

A control is created as an item in a window resource in a nib file Accessing that control from source code

is achieved by using Control Manager routines The function prototypes for the Control Manager routines are located in the Control.h header file The next sections describe the main control-related types and routines

ControlHandle/ControlRef: Referencing a Control

A variable of the type ControlHandle is used to reference a control, such as a radio button Call the Control Manager routine GetControlByID to obtain a handle to a control:

ControlHandle myRadioButtonGroup;

Note that the ControlHandle data type is defined as type ControlRef, so your code can use

variables of these two types interchangeably

ControlID: Specifying a Control

Your project defines a control as an item in a window resource in a nib file That control is given a control

ID, which is the combination of a signature and an ID Together, the values specify one and only one control Your code declares a variable of type ControlID to specify one control Like the control item in the nib resource file, the ControlID variable consists of a signature and an ID

#define kControlSignature 'Xapp'

#define kRadioGroupControlID 101

ControlID myRadioGroupControlID = { kControlSignature,

kRadioGroupControlID };

GetControlByID: Obtaining a Handle to a Control

To obtain a handle to a control, call the GetControlByID routine:

OSStatus GetControlByID( WindowRef inWindow,

const ControlID * inID,

Trang 5

GetControl32BitValue: Obtaining a Control's Value

After calling GetControlByID, your program has a handle to one control Your program can use this handle to access the control Accessing a control often means getting, or setting, the control's value A call

to GetControl32BitValue is made to obtain the value of a control:

SInt32 GetControl32BitValue( ControlRef theControl );

The following snippet obtains the value of a radio group control For this type of control, the value

represents the item number of the radio button that is currently on:

SInt32 controlValue;

controlValue = GetControl32BitValue( myRadioButtonGroup );

After the control's value is obtained, take the appropriate action based on the returned value For instance,

in the case of a radio button group, a switch statement is used to carry out the task at hand:

Trang 6

Menu Manager

A program's menu bar is created using the Interface Builder Manager routine

items is achieved through Menu Manager routines The function prototypes for the Menu Manager routines are located in the Menus.h header file The following sections describe the most commonly used menu-related types and functions

MenuRef: Referencing a Menu

A menu and its items are referenced by way of a variable of type MenuRef A MenuRef is the same as the older MenuHandle data type, so these two types can be used interchangeably

Creating a Menu Bar

A menu bar is created by calling SetMenuBarFromNib to unarchive a menu bar resource stored in a nib file Refer to the "Interface Builder Manager (Nib Files)" section of this

appendix for more information

GetMenuHandle: Accessing a Menu

Before altering the state of a menu or menu item, changing a menu item's characteristics, and performing similar tasks, your program needs a handle or reference to the affected menu Call

MenuRef GetMenuHandle( MenuID menuID );

When editing the project's menu bar in Interface Builder, assign the menu an ID Use this ID in the call to GetMenuHandle:

#define kCalculateMenuID 106

MenuRef gCalculateMenu;

gCalculateMenu = GetMenuHandle( kCalculateMenuID );

Disable/EnableMenuItem: Disabling and Enabling Menus

To disable a menu item, call DisableMenuItem To enable a menu item, call

Trang 7

void DisableMenuItem( MenuRef theMenu,

To disable an entire menu (the menu title and all menu item s in that menu), call

entire menu, call EnableMenuItem with a value of 0 as the number of the item to enable The following code shows both actions:

DisableMenuItem( gCalculateMenu, 0 );

EnableMenuItem( gCalculateMenu, 0 );

FMGetFontFamilyFontName: Obtaining a Reference to a Family of Fonts

To change the font of a menu or menu item, you first need to obtain a reference to the family

of the font to use Note that a font exists as a family in that there are different sizes, and in some cases, different faces, associated with one font.) To get this reference, call

FMFontFamily FMGetFontFamilyFromName( ConstStr255Param

inName );

Pass FMGetFontFamilyName the name of the font of interest (Arial, Geneva, Times, and

so forth), and the routine returns the font family reference associated with this name This reference then is used as an argument to font-altering routines such as SetMenuFont For legacy reasons, some API routines expect a string argument to be formatted as a Pascal string

with \p and enclose the string in quotation marks The following code example returns a reference to the Verdana family of fonts:

Trang 8

FMFontFamily fontFamily;

fontFamily = FMGetFontFamilyFromName( "\pVerdana" );

SetMenuItemFontID: Changing the Font of a Menu Item

OSErr SetMenuItemFontID( MenuRef inMenu,

SInt16 inItem,

SInt16 inFontID );

Pass SetMenuItemFontID a reference (a handle) to the menu that holds the affected menu item Then, pass the item number of the item In this case, the first item in a menu will have an item number of 1, the second item will have an item number of 2, and so forth Finally, pass the font family reference for the font to use The following snippet sets the second menu item

of the File menu to Times:

#define kFileMenuID 101

#define kOpenMenuItem 2

MenuRef fileMenu;

FMFontFamily fontFamily;

fileMenu = GetMenuHandle( kFileMenuID );

fontFamily = FMGetFontFamilyFromName( "\pTimes" );

SetMenuItemFontID( fileMenu, kOpenMenuItem, fontFamily );

SetMenuFont: Changing the Font of an Entire Menu

To change the font of all items in a menu, call SetMenuFont, as shown in the following code:

OSStatus SetMenuFont( MenuRef menu,

SInt16 inFontID,

UInt16 inFontSize );

Pass SetMenuFont a menu reference (handle), a font family reference (obtained from a call

instance, to change the font of all the items in the File menu to 24 point Arial, you'd use the following code:

Trang 9

#define kFileMenuID 2

#define kMenuFontPointSize 24

MenuRef fileMenu;

FMFontFamily fontFamily;

fileMenu = GetMenuHandle( kFileMenuID );

fontFamily = FMGetFontFamilyFromName( "\pArial" );

SetMenuFont( fileMenu, fontFamily, kMenuFontPointSize );

Trang 10

QuickDraw

Drawing to a window involves the Carbon API routines that are grouped in an area called QuickDraw The function prototypes for the QuickDraw routines are located in the

QuickDraw.h header file The next sections describe commonly used QuickDraw routines

SetPortWindowPort: Specifying the Window to Draw To

QuickDraw drawing routines draw to a port, which is a graphics environment capable of maintaining its own set of graphical information Every window has its own port, as does the screen itself Before drawing, call SetPortWindowPort to tell QuickDraw in which port (which window) to draw

void SetPortWindowPort(WindowRef window);

MoveTo and Move: Specifying the Starting Point for Drawing

Before drawing, specify in which window to draw by using SetPortWindowPort, and specify where within that window content area drawing is to take place Call MoveTo to specify a starting location relative to the upper-left corner of the window in which drawing

20 pixels from the left side and 60 pixels from the top of the content area of a window, call

MoveTo( 20, 60 );

The Move routine is similar to MoveTo in that it specifies a starting point for drawing

Move, though, uses the current starting point as its reference

void Move( short h,

short v);

Move moves the starting location a number of pixels horizontally and vertically from the current location Consider this snippet:

Trang 11

MoveTo( 20, 60 );

Move( 10, 40 );

The call to MoveTo sets the drawing starting location 20 pixels from the left side and 60 pixels from the top of the window's content area The call to Move then moves the starting location from that position to a location that is 10 more pixels to the right and 40 more pixels down The result of executing the preceding code snippet is a starting location 30 pixels from the left of the window (20 + 10) and 100 pixels down from the top of the window (60 + 40)

LineTo and Line: Drawing Lines

To draw a line, call the LineTo routine:

void LineTo( short h,

short v);

Specifying the Starting Point for Drawing" section of this appendix) to the specified end point Pass LineTo the horizontal pixel end point relative to the left side of the window to which to draw and the vertical pixel end point relative to the top of that window For

instance, to draw a horizontal line 100 pixels in length, starting at a point 20 pixels from the left and 50 pixels from the top of a window, use this code:

MoveTo( 20, 50 );

LineTo( 120, 50 );

Note in the preceding snippet that the vertical starting point established by MoveTo and the vertical end point established by LineTo are both 50 pixels from the top of the

window Thus, the line is horizontal

To draw a line of a specified length without regard for the starting point, call Line, as shown in the following code:

void Line( short h,

short v);

Line is similar to LineTo in that it draws a line, but Line draws the line without

specifying an end point For instance, a call to Line( 100, 0 ) draws a horizontal line

100 pixels in length, regardless of the location of the current drawing starting point

SetRect: Defining the Boundaries of a Rectangle

Trang 12

A rectangle is an important shape in that it is used to define rectangles, squares, ovals, circles, and round rectangles To define the coordinates of a rectangle, call SetRect, as shown in the following code:

void SetRect( Rect * r,

short left,

short top,

short right,

short bottom );

Pass SetRect a pointer to a Rect variable, along with the four rectangle-defining

coordinates The order is left, top, right, and bottom Each coordinate is specified in pixels, with the top-left corner of the window serving as the origin The following snippet defines

a rectangle 100x50 pixels The rectangle's top-left corner is stationed 30 pixels from the left edge and 70 pixels from the top of the window

Rect theRect;

SetRect( &theRect, 30, 70, 130, 120 );

frame the rectangle, call FrameRect

FrameRect: Framing (Drawing) a Rectangle

After establishing the boundaries of a rectangle using SetRect, frame that rectangle by calling FrameRect, as shown in the following code:

void FrameRect( const Rect * r );

After you pass FrameRect a pointer to a rectangle, FrameRect draws a frame around that rectangle:

Trang 13

Appendix B UNIX and the Terminal

IF YOU'RE MOVING TO MAC OS X from a UNIX background, you might have already

discovered how to use your Macintosh to use UNIX to enter commands, create source code files, and build applications Someone like you might be able to skip this appendix You should be aware, though, that UNIX in Mac OS X does vary in some ways from other UNIX implementations-so you might want to at least skim this appendix regardless of your level of expertise in UNIX

On the other hand, if you're a long-time Macintosh user who is light on UNIX experience, you might never have created or edited a text file in UNIX, and you probably never ran a compiler from the command line You might not even know how to go about using a

command line interface If any of this sounds like you, read this appendix In just a few pages, you'll be moving through directories, writing source code, and compiling that code-all from the UNIX command line

Trang 14

UNIX and the UNIX Shell

Darwin is Apple's name for the lowest level, or foundation, of the Mac OS X A big part of

Darwin is Berkeley Standard Distribution (BSD) It is a popular version of UNIX BSD

provides file system support, network services, multiprocessing support, and other

important operating system services

BSD also supports the shell environment A shell is a command-line interface that provides

a means for a computer user to perform system tasks By typing UNIX commands in the shell, a Mac OS X user can perform a multitude of tasks, including moving, renaming, and copying files, running applications, and compiling source code files

A person new to UNIX might think of the shell as UNIX itself, but that would be a false assumption The shell is an application that enables indirect access to UNIX The word

"indirect" is important here Enabling direct access to the core-level code that makes up an operating system (OS) would be dangerous A user could corrupt OS code and file data To counteract this possibility, the aptly named shell provides a "wrapper" around the OS

kernel In that respect, the shell is similar to the Finder The Finder enables a user to

perform many of the same tasks as does a shell, but it won't let a user directly alter OS code The Finder is a shell It just happens to be a graphics-based shell rather than a text-based command line shell

Most Mac OS X users won't be aware of the UNIX underpinnings of Mac OS X, and they won't be aware of the shell that enables access to the UNIX part of Mac OS X However, many power-users, and many programmers, will know (or will want to know) about UNIX and the shell In Mac OS X, you get to the shell by running the Terminal application You'll find the Terminal in the Utilities folder of the Applications folder You run the Terminal as you do any other Mac application-you double-click its icon

If you're familiar with UNIX command line environments, you might be interested to know that, by default, the Terminal uses a tcsh shell Other shells, including csh and bash, can be used as well However, if shell environments types are meaningless to you, don't worry! To get started with the Terminal, you don't need to know the details of the environment You need only know a few simple UNIX commands!

Trang 15

Entering Commands

Working with a shell is a cyclical process The following three steps are repeated over and over again:

1 Shell presents the user with a prompt

2 User enters a command and presses the Return (or Enter) key

3 Shell executes (carries out) the command

can have more than one Terminal window open at a time if you feel so inclined.) I've left the Finder window in the figure simply to emphasize that in Mac OS X, the Terminal is simply another application and that using the Terminal to work with UNIX doesn't

monopolize your Mac's resources While using the Terminal, you're free to click another application's window and interact with that application

Figure B.1 Entering a command in the Terminal.

A Terminal prompt includes the username you use when you log into your Mac Note that

Ngày đăng: 12/08/2014, 21:20

TỪ KHÓA LIÊN QUAN