Listening to the User

There are two ways to find out what the user is doing: By waiting for interface messages to show up in your app, or by polling:

As a general rule, your app should respond to the user's actions by implementing the hook functions that correspond to the interface messages. The polling functions should only be used as a means to get more information from within the hook function implementations.

Interface Messages

Message Hook function Class
B_ZOOM Zoom() BWindow
B_MINIMIZE Minimize() BWindow
B_KEY_DOWN KeyDown() BView
B_KEY_UP KeyUp() BView
B_MOUSE_DOWN MouseDown() BView
B_MOUSE_UP MouseUp() BView
B_MOUSE_MOVED MouseMoved() BView
B_VIEW_MOVED FrameMoved() BView
B_VIEW_RESIZED FrameResized() BView
B_VALUE_CHANGED ValueChanged() BScrollBar
B_WINDOW_ACTIVATED WindowActivated() BWindow and BView
B_QUIT_REQUESTED QuitRequested() BLooper
B_WINDOW_MOVED FrameMoved() BWindow
B_WINDOW_RESIZED FrameResized() BWindow
B_SCREEN_CHANGED ScreenChanged() BWindow
B_WORKSPACE_ACTIVATED WorkspaceActivated() BWindow
B_WORKSPACES_CHANGED WorkspacesChanged() BWindow
B_PULSE Pulse() BView

Eighteen interface messages are currently defined. Two of them command the window to do something in particular:

All other interface messages report events--something that happened, rather than something that the application must do.

The following five messages report user actions on the keyboard and mouse:

The five messages above are all directed at particular views--the view where the cursor is located or where typed input appears. Three others also concern views:

A few messages concern events that affect the window itself:

A few messages report changes to the on-screen environment for a window:

Finally, there's one message that doesn't derive from a user action:

Hook Functions for Interface Messages

Interface messages are generated and delivered to the application as the user acts. Keyboard messages are delivered to the current active window; mouse messages are sent to the window where the cursor is located. The BWindow object handles some of these messages itself, and passes others to the appropriate BView object.

An interface message is dispatched by calling a specific hook function:

B_MOUSE_UP messages don't invoke a hook function. A BView can determine when a mouse button goes up by calling GetMouse() from within its MouseDown() function. As it reports information about the location of the cursor and the state of the mouse buttons, GetMouse() removes mouse messages from the BWindow's message queue, so the same information won't be reported twice.

A BWindow reinterprets a B_QUIT_REQUESTED message, originally defined for the BLooper class in the Application Kit, to mean a user request to close the window. However, it doesn't redeclare the QuitRequested() hook function that it inherits from BLooper.

Tracking the Mouse

When the BWindow receives a keyboard or mouse message, it must decide which view is responsible. This decision is relatively easy for messages reporting mouse events since the cursor points to the affected view:

Tracking the Keyboard

The view that's responsible for handling keyboard events is known as the focus view. The focus view is the view within the active window that's displaying the current selection, or the control that's marked to show that it can be operated from the keyboard. Only one view in the window can be in focus at a time.

You promote a view to focus by calling BView::MakeFocus(), and you ask for the current focus through BWindow::CurrentFocus().

UI Guideline: A view should highlight the current selection only while it's in focus.

Focus View Commands

The table below maps a keyboard action to the hook function it invokes or the message it sends. In each of these cases, the function or message is sent to the current focus view (which may change between a key down and a key up)

User action Function or message
Press a character key BView::KeyDown()
Release a character key BView::KeyUp()
Command+a B_SELECT_ALL
Command+x B_SELECT_CUT

The focus view needn't respond to all of these functions and commands, but a BView that doesn't respond to any of them should never promote itself to focus.

Other Keyboard Commands

There are two other built-in keyboard commands:

User action Message or Function Target

Command+w B_QUIT_REQUESTED The active window.

Command+q B_QUIT_REQUESTED The active app.

Option+Tab Change focus view The active window.

BViews make themselves the focus view (with the MakeFocus() function), but ).

Kinds of Keyboard Messages

B_KEY_UP events are always assigned to the view that's in focus when the user releases the key--even if the previous B_KEY_DOWN message performed a shortcut, forced keyboard navigation, or was assigned to the default button.

Message Protocols

The event messages that the Application Server sends to a BWindow usually have more information in them than is passed to the corresponding hook function. For example, while a B_MOUSE_DOWN message knows where, when, and which mouse button was pressed (among other things), only the "where" information is passed to the MouseDown() function.

You can retrieve the message that initiated a hook function from within the hook function itself by calling BWindow::CurrentMessage() function:

   void MyView::MouseDown(BPoint where)
      BMessage *msg = Window()->CurrentMessage();

The Message Protocols appendix lists the contents of all interface messages.

The User Interface

The Interface Kit provides interface mechanisms that your classes can participate in, if they coordinate with kit-defined code. Two such mechanisms are described below--keyboard navigation and the drag-and-drop delivery of messages.

Keyboard Navigation

Keyboard navigation is a mechanism for allowing users to manipulate views--especially buttons, check boxes, and other control devices--from the keyboard. It gives users the ability to:

The first ability--navigation between views--is implemented by the Interface Kit. The second--navigation within a view--is up to individual applications (although the BControl class helps a little), as are most view-specific aspects of the user interface. The only trick, and it's not a difficult one, is to make the two kinds of navigation work together.

To participate in the navigation mechanism, a class derived from BView needs to coordinate three aspects of its code--setting navigation flags, drawing an indication that the BView is in focus, and responding to keyboard events. The following sections discuss each of these elements.

Setting Navigation Flags

The B_NAVIGABLE flag marks a BView as an eligible target for keyboard navigation. It's one flag in a mask that the BView constructor sets, along with other view attributes. For example:

   MyView::MyView(BRect frame, const char *name, 
                            uint32 resizingMode, uint32 flags)
      : BView(frame, name, resizingMode, flags|B_NAVIGIBLE|B_WILL_DRAW)
       . . .

When the user presses the Tab key, the focus moves from one B_NAVIGIBLE target to the next, working first down and then across the view hierarchy. That is, if a BView has both B_NAVIGIBLE children and B_NAVIGIBLE siblings, the children will be targeted before the siblings.

The flag should be removed from the mask when the view is disabled or cannot become the focus view for any reason, and included again when it's re-enabled. The mask can be altered with the SetFlags() function:

   if ( /* cannot become the focus view */ )
       SetFlags(Flags() & ~B_NAVIGIBLE);
       SetFlags(Flags() | B_NAVIGIBLE);

Most navigible BViews are control devices and derive from the BControl class. All BControls are navigible by default and BControl has a SetEnabled() function that turns the B_NAVIGIBLE flag on and off, so this work is already done for objects that inherit from BControl.

You may also want to set the B_NAVIGIBLE_JUMP flag to permit larger jumps between navigible views. Pressing the Control-Tab combination moves the focus from one group of views to another, where the groups are (hopefully) obvious to the user from their arrangement in the window.

B_NAVIGIBLE_JUMP marks positions in the view hierarchy for these larger jumps. When the user presses Control-Tab, the focus lands on the first B_NAVIGIBLE view at or after the B_NAVIGIBLE_JUMP marker. If a B_NAVIGABLE_JUMP view is not also flagged B_NAVIGABLE, the system searches for the next available B_NAVIGABLE view and jumps to it. The search descends the view hierarchy and moves from one sibling view to another as each branch of the view hierarchy is exhausted. For example, if a B_NAVIGABLE_JUMP parent view is not navigible itself but has navigible children, Control-Tab will land on its first B_NAVIGABLE child.

Unlike B_NAVIGABLE, B_NAVIGABLE_JUMP should not be turned on and off.

Drawing the Focus Indicator

When the user navigates to a view, the BView needs to draw some sort of visual indication that it's the current focus for keyboard actions. Guidelines are forthcoming on what the indication should be. Currently, Be-defined views underline text (for example, a button label) when the view is in focus, or draw a rectangular outline of the view. The underline and outline are drawn in the color returned by keyboard_navigation_color(). Using this color lends consistency to the user interface.

A BView learns that the focus has changed when its MakeFocus() hook function is called. It's up to MakeFocus() to ensure that the focus indicator is drawn or erased, depending on the BView's new status. It's usually simplest for MakeFocus() to call Draw() and have it do the work. For example:

   void MyView::MakeFocus(bool focused)
       if ( focused != IsFocus() ) {
           . . .

The BControl class has a MakeFocus() function that calls Draw() (though it doesn't look exactly like the one above), so if your class derives from BControl, all you need to do is implement Draw(). Draw() can call IsFocus() to test the BView's current status. Here's a rough example:

   void MyView::Draw(BRect updateRect)
       rbg_color navigationColor = keyboard_navigation_color();
       BRect r = Bounds()
       r.InsetBy(2.0, 2.0)
       . . .
       rgb_color c = HighColor();
       if ( IsFocus() ) {
           /* draw the indicator */
       else {
           /* erase the indicator */
       . . .

This example is diagrammatic; it may not show an appropriate way for the BViews in your application to draw. (Note that when MakeFocus() called IsFocus(), it returned the BView's previous status, but when Draw() called it, it returned the updated status.)

Handling Keyboard Actions

Finally, your BView may need to override KeyDown() to handle the keystrokes that are used to operate the view (for view-internal navigation). Always incorporate the inherited version of KeyDown() so that it can take care of navigation between views. For example:

   void MyView::KeyDown(const char *bytes, int32 numBytes)
       switch ( bytes[0] ) {
           case B_ENTER:
           case B_SPACE:
               /* take action */
           case B_UP_ARROW:
           case B_DOWN_ARROW:
           case B_RIGHT_ARROW:
           case B_LEFT_ARROW:
               /* move within the view */
               baseClass::KeyDown(bytes, numBytes);

Again, the BControl class implements a KeyDown() function that invokes the control device when the user presses the space bar or Enter key. If your class derives from BControl and it doesn't have to do any other view-internal navigation, the BControl function may be adequate for your needs.

Drag and Drop

The BView class supports a drag-and-drop user interface. The user can transfer a parcel of information from one place to another by dragging an image from a source view and dropping it on a destination view--perhaps a view in a different window in a different application.

A source BView initiates dragging by calling DragMessage() from within its MouseDown() function. The BView bundles all information relevant to the dragging session into a BMessage object and passes it to DragMessage(). It also passes an image or a rectangle to represent the data package on-screen. For example:

   void MyView::MouseDown(BPoint point)
       . . .
       if ( aRect.Contains(point) ) {
           BMessage message(SOME_WORDS_OF_ENCOURAGEMENT);
           message.AddString("words", theEncouragingWords);
           DragMessage(&message, aRect);
       . . .

The Application Server then takes charge of the BMessage object and animates the image as the user drags it on-screen. As the image moves across the screen, the views it passes over are informed with MouseMoved() function calls. These notifications give views a chance to show the user whether or not they're willing to accept the message being dragged. When the user releases the mouse button, dropping the dragged message, the message is delivered to the BWindow and targeted to the destination BView.

A BView is notified that a message has arrived by a MessageReceived() function call. This is the same function that announces the arrival of other messages. By calling WasDropped(), you can ask the message whether it was dropped on the view or delivered in some other way. If it was dropped, you can find out where by calling DropPoint(). For example:

   void AnotherView::MessageReceived(BMessage *message)
       switch ( Message->what ) {
           . . .
               char *words;
               if ( message->FindString("words", &words) != B_OK )
               if ( message->WasDropped() ) {
                   BPoint where = DropPoint();
                   PleaseInsertTheseWords(words, where);
           . . .

Aside from creating a BMessage object and passing it to DragMessage(), or implementing MouseMoved() and MessageReceived() functions to handle any messages that come its way, there's nothing an application needs to do to support a drag-and-drop user interface. The bulk of the work is done by the Application Server and Interface Kit.

Character Encoding

The BeOS encodes characters using the UTF-8 transformation of Unicode character values. Unicode is a standard encoding scheme for all the major scripts of the world--including, among others, extended Latin, Cyrillic, Greek, Devanagiri, Telugu, Hebrew, Arabic, Tibetan, and the various character sets used by Chinese, Japanese, and Korean. It assigns a unique and unambiguous 16-bit value to each character, making it possible for characters from various languages to co-exist in the same document. Unicode makes it simpler to write language-aware software (though it doesn't solve all the problems). It also makes a wide variety of symbols available to an application, even if it's not concerned with covering more than one language.

Unicode's one disadvantage is that all characters have a width of 16 bits. Although 16 bits are necessary for a universal encoding system and a fixed width for all characters is important for the standard, there are many contexts in which byte-sized characters would be easier to work with and take up less memory (besides being more familiar and backwards compatible with existing code). UTF-8 is designed to address this problem.


UTF-8 stands for "UCS Transformation Format, 8-bit form" (and UCS stands for "Universal Multiple-Octet Character Set," another name for Unicode). UTF-8 transforms 16-bit Unicode values into a variable number of 8-bit units. It takes advantage of the fact that for values equal to or less than 0x007f, the Unicode character set matches the 7-bit ASCII character set--in other words, Unicode adopts the ASCII standard, but encodes each character in 16 bits. UTF-8 strips ASCII values back to 8 bits and uses two or three bytes to encode Unicode values over 0x007f.

The high bit of each UTF-8 byte indicates the role it plays in the encoding:

In addition, the first byte of a multibyte character indicates how many bytes are in the encoding: The number of high bits that are set to 1 (before a bit is 0) is the number of bytes it takes to represent the character. Therefore, the first byte of a multibyte character will always have at least two high bits set. The other bytes in a multibyte encoding have just one high bit set.

To illustrate, a character encoded in one UTF-8 byte will look like this (where a '1' or a '0' indicates a control bit specified by the standard and an 'x' is a bit that contributes to the character value):


A character encoded in two bytes has the following arrangement of bits:

   110xxxxx 10xxxxxx 

And a character encoded in three bytes is laid out as follows:

   1110xxxx 10xxxxxx 10xxxxxx 

Note that any 16-bit value can be encoded in three UTF-8 bytes. However, UTF-8 discards leading zeroes and always uses the fewest possible number of bytes--so it can encode Unicode values less than 0x0080 in a single byte and values less than 0x0800 in two bytes.

In addition to the codings illustrated above, UTF-8 takes four bytes to translate a Unicode surrogate pair--two conjoined 16-bit values that together encode a character that's not part of the standard. Surrogates are extremely rare.

ASCII Compatibility

The UTF-8 encoding scheme has several advantages:

      #define BEGINS_CHAR(byte) ((byte & 0xc0) != 0x80)

      int32 count = 0;
      while ( *p != '\\0' ) {
          if ( BEGINS_CHAR(*p) )

UTF-8 and the BeOS

The BeOS assumes UTF-8 encoding in most cases. For example, a B_KEY_DOWN message reports the character that's mapped to the key the user pressed as a UTF-8 value. That value is then passed as a string to KeyDown() along with the byte count:

virtual void KeyDown(const char *bytes, int32 numBytes);

You can expect the bytes string to always contain at least one byte. And, of course, you can test it for an ASCII value without caring whether it's UTF-8:

   if ( bytes[0] == B_TAB )
       . . .

Similarly, objects that display text in the user interface--such as window titles and button labels--expect to be passed UTF-8 encoded strings, and hand you a UTF-8 string if you ask for the title or label. These objects display text using a system font--either the system plain font (be_plain_font) or the bold font (be_bold_font). The BFont class allows other character encodings, which you may need to use in limited circumstances from time to time, but the system fonts are constrained to UTF-8 (B_UNICODE_UTF8 encoding). The FontPanel preferences application doesn't permit users to change the encoding of a system font.

Unicode and UTF-8 are documented in The Unicode Standard, Version 2.0, published by Addison-Wesley. See that book for complete information on Unicode and for a description of how UTF-8 encodes surrogate pairs.

The Be Book, in lovely HTML, for BeOS Release 4.

Copyright © 1998 Be, Inc. All rights reserved.

Last modified January 4, 1999.