home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
src
/
mactech
/
volume14_1998
/
14.09.sit
/
14.09
/
BeOSGraphics.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2006-09-22
|
11KB
|
307 lines
/*____________________________________________________________________
File: GraphicsEx.cpp
Copyright: Dan Parks Sydow 1998
Program: Running the GraphicsEx program results in the display
of a single window. At the top of the window are a pair
of overlapping rectangles that together form a single
region. At the bottom of the window is a color control.
Click anywhere on the color control to change the color
that's used to draw the region. The region's color change
does not go into effect until the region is subsequently
clicked on.
Purpose: While this file does include comments throughout, its
main purpose is to provide a vehicle for some of the
several graphics techniques mentioned in my MacTech
magazine article on porting Mac OS graphics code to
BeOS graphics code. Among the graphics issues that this
simple program demonstrates are:
* creating a variable to hold an RGB color
* setting the high color of a view
* creating and making use of a color control
* creating a region
* monitoring the mouse to determine if a mouse button
click lands in a particular area (the above region)
Comments: Normally a BeIDE project is made up of a number of source
code files and header files. For simplicity I've included
all of the project's code in this one file. To make use of
this code, simply create a new BeIDE project (File->New) and
add this file to the project (Project->Add to Project).
______________________________________________________________________*/
/*____________________________________________________________________
Class declarations
So that this code can be easily passed around, posted on the Net, etc.,
I've included it all in this one file. More typically, a BeIDE project
consists of a number of files. Each class declaration appears in one
header file, while the implementation of the member functions of that
class are located together in a separate file.
Class: MyApplication
Every project must define a class derived from the BApplication class.
In main() a single instance of this class is created to serve as the
application object. For this simple project the application class
consists of nothing more than a constructor.
Class: MyWindow
You could create windows from the BWindow class, but such windows
would include only the functionality of the member functions of the
BWindow class. Instead, define a BWindow-derived class for each type
of window your application uses. My window class needs a constructor
and a routine that responds to a user's click in the close button.
Class: MyView
The BView class includes a number of member functions that are
automatically invoked in response to certain events. Draw() gets
called by the system when a view needs to be redrawn. MouseDown()
gets called when the user clicks the mouse button while the cursor
is over the view. To create a powerful view, defined a BView-derived
class and override these functions. My class MyView does that. It
also demonstrates how a view keeps track of its items -- it defines
a data member for each item that might be accessed. For instance,
by declaring a BRegion object as a data member (fRegion), any MyView
member function can make use of the region (assuming that a region
is created (typically in the view's constructor) and assigned to
the data member).
______________________________________________________________________*/
class MyApplication : public BApplication {
public:
MyApplication();
};
class MyWindow : public BWindow {
public:
MyWindow(BRect frame);
virtual bool QuitRequested();
};
class MyView : public BView {
public:
MyView(BRect frame, char *name);
virtual void Draw(BRect updateRect);
virtual void MouseDown(BPoint point);
private:
BRegion *fRegion;
BColorControl *fColorControl;
};
/*____________________________________________________________________
main()
As with any C++ program, main() is where the action begins. Here the
program's one application object is created. The BApplication member
function Run() "kicks" things off -- a call to this routine starts
the program running. Run() doesn't return until the program is to
be terminated (in this example, when the user clicks on the close
button of the program's one window).
______________________________________________________________________*/
main()
{
MyApplication *theApplication;
theApplication = new MyApplication();
theApplication->Run();
return(0);
}
/*____________________________________________________________________
MyApplication()
This is the constructor that's used to create a new application object.
The one argument to the constructor is a signature for the application.
When the program launches a new window appears. That window is created
here in the application's constructor. In a real-world application the
creation of a window would typically occur in response to a menu item
selection (such as File->New). Here a rectangle the size of the window
that's to be created is defined and passed to the window's constructor
(the MyWindow constructor).
______________________________________________________________________*/
MyApplication::MyApplication()
: BApplication("application/x-dps-mywd")
{
BRect theRect;
MyWindow *theWindow;
theRect.Set(20, 30, 450, 250);
theWindow = new MyWindow(theRect);
}
/*____________________________________________________________________
MyWindow()
This is the constructor that's used to create a new window object. The
one argument to the constructor is size of the window to create. The
constructor begins by calling the BWindow constructor. That routine
does most of the work of creating a new window. The body of the MyWindow
constructor offsets the coordinates of the window so that they can be
used for a window-encompassing view. This view, of class type MyView,
is created by invoking the MyView constructor. Once created, the view
is added to the window via a call to the BView function AddChild().
Finally, a call to the BWindow function Show() displays what had been
previously hidden -- the new window.
______________________________________________________________________*/
MyWindow::MyWindow(BRect frame)
: BWindow(frame, "Graphics", B_TITLED_WINDOW, B_NOT_RESIZABLE)
{
MyView *theView;
frame.OffsetTo(B_ORIGIN);
theView = new MyView(frame, "MyView");
AddChild(theView);
Show();
}
/*____________________________________________________________________
QuitRequested()
When the user clicks the mouse button while the cursor is over the
window's close button, the system sends a message to the affected
MyWindow object. The posting of a message places that message in a
message queue. In this case the message goes in the application
object's queue. Once serviced by the application, the window will
close and the program quits.
______________________________________________________________________*/
bool MyWindow::QuitRequested()
{
be_app->PostMessage(B_QUIT_REQUESTED);
return(true);
}
/*____________________________________________________________________
MyView()
A view's constructor determines what the view holds, and thus what
the view looks like. Here the MyView constructor creates a region
from two rectangles. A reference to the new region is stored in the
MyView class data member section. The constructor also creates a
new color control object. To let the system know just where this
new color control is to reside, a call to the BView function AddChild()
is made. Now the color control "belongs" to the view.
______________________________________________________________________*/
MyView::MyView(BRect rect, char *name)
: BView(rect, name, B_FOLLOW_ALL, B_WILL_DRAW)
{
BRect theRect;
fRegion = new BRegion();
theRect.Set(40.0, 20.0, 140.0, 60.0);
fRegion->Include(theRect);
theRect.Set(110.0, 30.0, 300.0, 90.0);
fRegion->Include(theRect);
BPoint leftTop(20.0, 120.0);
color_control_layout matrix = B_CELLS_32x8;
long cellSide = 10;
fColorControl = new BColorControl(leftTop, matrix,
cellSide, "ColorControl");
AddChild(fColorControl);
}
/*____________________________________________________________________
Draw()
When a view needs updating, the system calls that view's Draw() member
function. For any application-defined view class, override Draw() and
implement it such that it draws everything that the view is to display.
This example draws the region to the view. Here's why the MyView class
keeps track of the region in its fRegion data member. The first time
Draw() is called (when the window the view appears in is created), the
region is drawn in the default high color -- black. Sould the program
change the high color of the view (and as you'll see in MouseDown(),
it does), then subsequent calls to Draw() will fill the region using
that color.
______________________________________________________________________*/
void MyView::Draw(BRect)
{
FillRegion(fRegion, B_SOLID_HIGH);
}
/*____________________________________________________________________
MouseDown()
When a mouse button click occurs while the cursor is over a view,
the system calls that view's MouseDown() member function. For an
application-defined view class that is to respond to mouse clicks,
override MouseDown() and implement it such that it does whatever
is appropriate in response to a click. This example checks the
currently selected color in the view's color control and sets the
view's high color to the current control color. Note that when the
user clicks on the color control, this version of MouseDown() *isn't*
called. That's because the color control (the BColorControl object)
is itself a view. If it gets clicked on, then the system calls the
color control view's version of MouseDown(). The BColorControl
version of this routine is responsible for highlighting the selected
color in the control and for setting the RGB values in the control.
Now, if the user instead clicks anywhere else in the view (anywhere
else in the MyView), then that click initiates the system's calling
of this verison of MouseDown(). Here the currently selected color
from the color control is obtained, and that color is used to set
the view's high color. After that, subsequent usage of the Be-defined
constant B_HIGH_COLOR will result in this selected color being used.
After the high color is set, a check is made to see if the cursor was
over the region at the time of the mouse click. If it was, the system
beep is sounded (beep() is a global system function -- one that's
always available for your program's use) and the region is redrawn in
the current high color.
______________________________________________________________________*/
void MyView::MouseDown(BPoint point)
{
rgb_color userColor;
userColor = fColorControl->ValueAsColor();
SetHighColor(userColor);
if (fRegion->Contains(point))
{
beep();
FillRegion(fRegion, B_SOLID_HIGH);
}
}