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 >
C/C++ Source or Header  |  2006-09-22  |  11KB  |  307 lines

  1. /*____________________________________________________________________
  2. File:        GraphicsEx.cpp
  3.     
  4. Copyright:    Dan Parks Sydow 1998
  5.     
  6. Program:    Running the GraphicsEx program results in the display
  7.             of a single window. At the top of the window are a pair
  8.             of overlapping rectangles that together form a single
  9.             region. At the bottom of the window is a color control.
  10.             Click anywhere on the color control to change the color
  11.             that's used to draw the region. The region's color change
  12.             does not go into effect until the region is subsequently
  13.             clicked on.
  14.                 
  15. Purpose:    While this file does include comments throughout, its
  16.             main purpose is to provide a vehicle for some of the
  17.             several graphics techniques mentioned in my MacTech
  18.             magazine article on porting Mac OS graphics code to 
  19.             BeOS graphics code. Among the graphics issues that this
  20.             simple program demonstrates are:
  21.             * creating a variable to hold an RGB color
  22.             * setting the high color of a view
  23.             * creating and making use of a color control
  24.             * creating a region
  25.             * monitoring the mouse to determine if a mouse button
  26.               click lands in a particular area (the above region)
  27.                   
  28. Comments:    Normally a BeIDE project is made up of a number of source
  29.             code files and header files. For simplicity I've included
  30.             all of the project's code in this one file. To make use of
  31.             this code, simply create a new BeIDE project (File->New) and 
  32.             add this file to the project (Project->Add to Project). 
  33. ______________________________________________________________________*/
  34.  
  35.  
  36.  
  37.  
  38. /*____________________________________________________________________
  39.  
  40. Class declarations
  41.  
  42. So that this code can be easily passed around, posted on the Net, etc.,
  43. I've included it all in this one file. More typically, a BeIDE project
  44. consists of a number of files. Each class declaration appears in one 
  45. header file, while the implementation of the member functions of that 
  46. class are located together in a separate file.
  47.  
  48. Class: MyApplication
  49.  
  50. Every project must define a class derived from the BApplication class.
  51. In main() a single instance of this class is created to serve as the
  52. application object. For this simple project the application class
  53. consists of nothing more than a constructor.
  54.  
  55. Class: MyWindow
  56.  
  57. You could create windows from the BWindow class, but such windows
  58. would include only the functionality of the member functions of the
  59. BWindow class. Instead, define a BWindow-derived class for each type
  60. of window your application uses. My window class needs a constructor
  61. and a routine that responds to a user's click in the close button.
  62.  
  63. Class: MyView
  64.  
  65. The BView class includes a number of member functions that are
  66. automatically invoked in response to certain events. Draw() gets 
  67. called by the system when a view needs to be redrawn. MouseDown()
  68. gets called when the user clicks the mouse button while the cursor
  69. is over the view. To create a powerful view, defined a BView-derived
  70. class and override these functions. My class MyView does that. It
  71. also demonstrates how a view keeps track of its items -- it defines
  72. a data member for each item that might be accessed. For instance,
  73. by declaring a BRegion object as a data member (fRegion), any MyView
  74. member function can make use of the region (assuming that a region
  75. is created (typically in the view's constructor) and assigned to 
  76. the data member).
  77. ______________________________________________________________________*/
  78.  
  79. class MyApplication : public BApplication {
  80.  
  81.     public:
  82.                         MyApplication();
  83. };
  84.  
  85.  
  86. class MyWindow : public BWindow {
  87.  
  88.     public:
  89.                         MyWindow(BRect frame); 
  90.         virtual    bool    QuitRequested();
  91. };
  92.  
  93.  
  94. class MyView : public BView {
  95.  
  96.     public:
  97.                         MyView(BRect frame, char *name); 
  98.         virtual    void    Draw(BRect updateRect);
  99.         virtual    void    MouseDown(BPoint point);
  100.         
  101.     private:
  102.         BRegion            *fRegion;
  103.         BColorControl    *fColorControl;
  104. };
  105.  
  106.  
  107.  
  108. /*____________________________________________________________________
  109.  
  110. main()
  111.  
  112. As with any C++ program, main() is where the action begins. Here the
  113. program's one application object is created. The BApplication member
  114. function Run() "kicks" things off -- a call to this routine starts
  115. the program running. Run() doesn't return until the program is to
  116. be terminated (in this example, when the user clicks on the close 
  117. button of the program's one window).
  118. ______________________________________________________________________*/
  119.  
  120. main()
  121. {    
  122.     MyApplication    *theApplication;
  123.  
  124.     theApplication = new MyApplication();
  125.     theApplication->Run();
  126.     
  127.     return(0);
  128. }
  129.  
  130.  
  131. /*____________________________________________________________________
  132.  
  133. MyApplication()
  134.  
  135. This is the constructor that's used to create a new application object.
  136. The one argument to the constructor is a signature for the application.
  137. When the program launches a new window appears. That window is created
  138. here in the application's constructor. In a real-world application the
  139. creation of a window would typically occur in response to a menu item
  140. selection (such as File->New). Here a rectangle the size of the window
  141. that's to be created is defined and passed to the window's constructor 
  142. (the MyWindow constructor).
  143. ______________________________________________________________________*/
  144.  
  145. MyApplication::MyApplication()
  146.                     : BApplication("application/x-dps-mywd")
  147. {
  148.     BRect        theRect;
  149.     MyWindow    *theWindow;
  150.     
  151.     theRect.Set(20, 30, 450, 250);
  152.     theWindow = new MyWindow(theRect);    
  153. }
  154.  
  155.  
  156. /*____________________________________________________________________
  157.  
  158. MyWindow()
  159.  
  160. This is the constructor that's used to create a new window object. The 
  161. one argument to the constructor is size of the window to create. The
  162. constructor begins by calling the BWindow constructor. That routine
  163. does most of the work of creating a new window. The body of the MyWindow
  164. constructor offsets the coordinates of the window so that they can be
  165. used for a window-encompassing view. This view, of class type MyView,
  166. is created by invoking the MyView constructor. Once created, the view
  167. is added to the window via a call to the BView function AddChild().
  168. Finally, a call to the BWindow function Show() displays what had been
  169. previously hidden -- the new window.
  170. ______________________________________________________________________*/
  171.  
  172. MyWindow::MyWindow(BRect frame)
  173.     : BWindow(frame, "Graphics", B_TITLED_WINDOW, B_NOT_RESIZABLE)
  174. {
  175.     MyView  *theView;
  176.     
  177.     frame.OffsetTo(B_ORIGIN);
  178.     theView = new MyView(frame, "MyView");
  179.     AddChild(theView);
  180.  
  181.     Show();
  182. }
  183.  
  184.  
  185. /*____________________________________________________________________
  186.  
  187. QuitRequested()
  188.  
  189. When the user clicks the mouse button while the cursor is over the 
  190. window's close button, the system sends a message to the affected
  191. MyWindow object. The posting of a message places that message in a
  192. message queue. In this case the message goes in the application 
  193. object's queue. Once serviced by the application, the window will 
  194. close and the program quits.
  195. ______________________________________________________________________*/
  196.  
  197. bool MyWindow::QuitRequested()
  198. {
  199.     be_app->PostMessage(B_QUIT_REQUESTED);
  200.     return(true);
  201. }
  202.  
  203.  
  204. /*____________________________________________________________________
  205.  
  206. MyView()
  207.  
  208. A view's constructor determines what the view holds, and thus what
  209. the view looks like. Here the MyView constructor creates a region
  210. from two rectangles. A reference to the new region is stored in the 
  211. MyView class data member section. The constructor also creates a
  212. new color control object. To let the system know just where this 
  213. new color control is to reside, a call to the BView function AddChild()
  214. is made. Now the color control "belongs" to the view.
  215. ______________________________________________________________________*/
  216.  
  217. MyView::MyView(BRect rect, char *name)
  218.     : BView(rect, name, B_FOLLOW_ALL, B_WILL_DRAW)
  219. {
  220.     BRect    theRect;
  221.  
  222.     fRegion = new BRegion();
  223.    
  224.     theRect.Set(40.0, 20.0, 140.0, 60.0);
  225.     fRegion->Include(theRect);
  226.    
  227.     theRect.Set(110.0, 30.0, 300.0, 90.0);
  228.     fRegion->Include(theRect);
  229.     
  230.     
  231.     BPoint                leftTop(20.0, 120.0);
  232.     color_control_layout  matrix = B_CELLS_32x8;
  233.     long                  cellSide = 10;
  234.  
  235.     fColorControl = new BColorControl(leftTop, matrix, 
  236.                         cellSide, "ColorControl");
  237.     AddChild(fColorControl);
  238. }
  239.  
  240.  
  241.  
  242. /*____________________________________________________________________
  243.  
  244. Draw()
  245.  
  246. When a view needs updating, the system calls that view's Draw() member 
  247. function. For any application-defined view class, override Draw() and 
  248. implement it such that it draws everything that the view is to display. 
  249. This example draws the region to the view. Here's why the MyView class 
  250. keeps track of the region in its fRegion data member. The first time 
  251. Draw() is called (when the window the view appears in is created), the 
  252. region is drawn in the default high color -- black. Sould the program 
  253. change the high color of the view (and as you'll see in MouseDown(), 
  254. it does), then subsequent calls to Draw() will fill the region using 
  255. that color.
  256. ______________________________________________________________________*/
  257.  
  258. void MyView::Draw(BRect)
  259. {
  260.     FillRegion(fRegion, B_SOLID_HIGH);
  261. }
  262.  
  263.  
  264. /*____________________________________________________________________
  265.  
  266. MouseDown()
  267.  
  268. When a mouse button click occurs while the cursor is over a view, 
  269. the system calls that view's MouseDown() member function. For an
  270. application-defined view class that is to respond to mouse clicks,
  271. override MouseDown() and implement it such that it does whatever 
  272. is appropriate in response to a click. This example checks the
  273. currently selected color in the view's color control and sets the
  274. view's high color to the current control color. Note that when the
  275. user clicks on the color control, this version of MouseDown() *isn't* 
  276. called. That's because the color control (the BColorControl object) 
  277. is itself a view. If it gets clicked on, then the system calls the
  278. color control view's version of MouseDown(). The BColorControl 
  279. version of this routine is responsible for highlighting the selected
  280. color in the control and for setting the RGB values in the control.
  281. Now, if the user instead clicks anywhere else in the view (anywhere
  282. else in the MyView), then that click initiates the system's calling 
  283. of this verison of MouseDown(). Here the currently selected color 
  284. from the color control is obtained, and that color is used to set
  285. the view's high color. After that, subsequent usage of the Be-defined
  286. constant B_HIGH_COLOR will result in this selected color being used.
  287. After the high color is set, a check is made to see if the cursor was 
  288. over the region at the time of the mouse click. If it was, the system 
  289. beep is sounded (beep() is a global system function -- one that's 
  290. always available for your program's use) and the region is redrawn in 
  291. the current high color.
  292. ______________________________________________________________________*/
  293.  
  294. void MyView::MouseDown(BPoint point)
  295. {
  296.     rgb_color    userColor;
  297.  
  298.     userColor = fColorControl->ValueAsColor();
  299.     SetHighColor(userColor);
  300.  
  301.     if (fRegion->Contains(point))
  302.     {
  303.         beep();
  304.         FillRegion(fRegion, B_SOLID_HIGH);
  305.     }
  306. }
  307.