Home > Products > SophiaFramework UNIVERSE > Tutorial > RSS Reader > - 6 / 9 -

BREW RSS Reader - 6 / 9 -

Draw Screen

This is the main screen for displaying the title and description of RSS items.

Define MainWindow

SFMTYPEDEFCLASS(MainWindow)
class MainWindow : public SFRTitleWindow {
    SFMSEALCOPY(MainWindow)
private:
    SInt16 _displine; // Head of lines to display
    SInt16 _cursor;   // Cursor position
    SInt16 _maxline;  // Number of lines to display
public:
    MainWindow(SInt16 softkeyHeight);
    static Void SetSoftkey(Void);
private:
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent) // Draw Handler
    HANDLER_DECLARE_BOOLEVENT(OnKey)            // Key Handler
};

Draw Handler for MainWindow

HANDLER_IMPLEMENT_VOIDRENDER(MainWindow, OnRenderContent, graphics)
{
    // Get size of screen
    SFXRectangle rectangle(GetContentWorld());
    SFXRGBColor forecolor;
    SFXRGBColor backcolor;
    SIntN i;
    RSSFeedPtr current;

	// Get font height
    SInt16 height = graphics->GetFontHeight();  
    // Get the bottom line of current rectangle
    SInt16 bottom = rectangle.GetBottom();
    // Set rectangle height to that of font 
    rectangle.SetHeight(height); 
             
    // Call parent handler ( clear screen )
    SFRTitleWindow::ContentHandler(graphics);

    // Get current feed from application class 
    current = RssReader::GetCurrentFeed();
    
    rectangle.AddY((6 * height + 15));

    // Set the height of rectangle as a function of font height
    rectangle.SetHeight(((bottom -rectangle.GetTop())/height)* height);
    graphics->FillRectangle(rectangle, SFXRGBColor(255, 255, 255, 0));
    // Set the character color
    graphics->SetForeColor(SFXRGBColor(0, 0, 0, 0));
    if (current != null &&
            current->GetSize() > _displine + _cursor) {
        _currentRect = rectangle;
		if(_currentRect.GetY() >= rectangle.GetY())
		{
			_currentRect.SubY(_moveY);
		}
		_currentRect.AddHeight(500);
        // Draw the description of item that _cursor points to
        graphics->DrawString(
            current->GetItem(_displine + _cursor)->description,
            _currentRect,
            IDF_ALIGN_LEFT | IDF_ALIGN_MIDDLE);
    }
    
    rectangle.SubY((6 * height + 15));
    // Set rectangle height to that of font
	rectangle.SetHeight(height);                        

    // Draw each line 
    for (i = _displine;  i < _displine + _maxline ; ++i) {
    	// If it forms a line with the cursor
        if (i - _displine == _cursor) { 
            // Change color
            forecolor.Set(0xff, 0xff, 0xff, 0x00);
            backcolor.Set(0x66, 0x66, 0xff, 0x00);
        }
        else {
            forecolor.Set(0x00, 0x00, 0x00, 0x00);
            // The color is changed at odd or even lines
            if (i % 2 == 0) { // change color every other line
                backcolor.Set(0xdd, 0xdd, 0xff, 0);
            } else {
                backcolor.Set(0xee, 0xee, 0xff, 0);
            }
        }
        // Draw background rectangle of item title
        graphics->FillRectangle(rectangle, backcolor);
        graphics->SetForeColor(forecolor);
        if (current != null &&
                i < current->GetSize()) {
            if (current->GetItem(i)->title.IsEmpty()) {
                // draw title string of i th item 
                graphics->DrawText("[No Subject]",
                    rectangle,
                    IDF_ALIGN_LEFT | IDF_ALIGN_MIDDLE);
            } else {
            	// If it forms a line with the cursor
                if (i - _displine == _cursor) {
                _currentRect = rectangle;
                _currentRect.SubX(_moveX);
                _currentRect.AddWidth(150);
                if (current->GetItem(i)->alreadyRead) {
                    // Draw title string of i th item 
                    graphics->DrawText(
                        current->GetItem(i)->title,
                        _currentRect,
                        IDF_ALIGN_LEFT | IDF_ALIGN_MIDDLE);
                } else {
                	// Set the font to bold-faced
                	SFXRGBColor fontColor(0x99, 0x00, 0x00, 0x00);
                    graphics->SetFont(AEE_FONT_BOLD); 
                    // Draw title string of i th item
                    graphics->DrawText(
                        current->GetItem(i)->title,
                        _currentRect, fontColor,
                        IDF_ALIGN_LEFT | IDF_ALIGN_MIDDLE);
                    graphics->SetFont(AEE_FONT_NORMAL);
                }
            }
        }
     }
        rectangle.AddY(height); 
        // move down rectangle by its height
    }
    rectangle.AddY(15);
    
    rectangle.SubY(3);
	rectangle.Deflate(0,3);
	graphics->FillRectangle(rectangle, SFXRGBColor(128, 128, 128, 255));
}

Key Handler

HANDLER_IMPLEMENT_BOOLEVENT(MainWindow, OnKey, event)
{
    SFXRectangle rectangle;
    RSSFeedPtr current;

    switch (event.GetP16()) {
        case AVK_DOWN:                   // If down key pressed
            if (_cursor == _maxline - 1) { 
                // if cursor is at the bottom
                ++_displine;             // Move display line down
            } else {
                ++_cursor;               // Move cursor down
            }
            InvalidateContent();         // Redraw window
            _moveY = 0;                  // Reset description
            return true;
        case AVK_UP:                     // If up key pressed
            // If cursor is at the top
            if (_cursor == 0) { 
                // If display line can be moved up 
                if (_displine > 0) { 
                    --_displine;         // Move display line up
                    InvalidateContent(); // Redraw window
                }
            } else {
                --_cursor;               // Move cursor up
                InvalidateContent();     // Redraw window
            }
            return true;                 // If SOFT2 or 9 key pressed 
            _moveY = 0;                  // Reset description
        case AVK_RIGHT:                  // Scroll Horizontally right
            _moveX = _moveX + 5;
            InvalidateContent();
            return true;
        case AVK_LEFT:                   // Scroll Horizontally left
            if(_moveX > 0)
            {
                _moveX = _moveX - 5;
                InvalidateContent();
            }
            return true; 
        case AVK_8:                      // Scroll Vertically down
            _moveY = _moveY + 5;
            InvalidateContent();
            return true;
        case AVK_2:                      // Scroll Vertically up
            if(_moveY > 0)
            {
                _moveY = _moveY - 5;
                InvalidateContent();
            }
            return true;
        case AVK_SOFT2: case AVK_9:
            // Create window for managing feeds
            rectangle.Set(GetBaseBound());
            rectangle.AddLeftTop(SUBSURIPTION_MARGIN, 
                                 SUBSURIPTION_MARGIN);
            ::new SubscriptionWindow(rectangle);
            return true;
        case AVK_SELECT:                 // If SELECT key pressed
            current = RssReader::GetCurrentFeed();
            // Change staus of items from unread to already read
            if (current != null &&
                    current->GetSize() > _displine + _cursor) {
                	current->GetItem(_displine + _cursor)
                      	   -> alreadyRead = true;
                	InvalidateContent();
            }
            return true;
        }
    return false;
}

Go back  1   2   3   4   5   6   7   Apdx1   Apdx2  Next page