How do you set the position of a custom painted control on a parent window?

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
modifiedbroadcast
In need of some credit
In need of some credit
Posts: 2
Joined: Mon Aug 29, 2016 7:51 pm

How do you set the position of a custom painted control on a parent window?

Postby modifiedbroadcast » Mon Oct 03, 2016 10:05 pm

I am using a control I found on the wxWidgets component list called wxLed. I have successfully created objects with how the class is at the moment. However, I am unable to actually set the window's position. :?

Here is the source code:

Code: Select all

BEGIN_EVENT_TABLE (wxLed, wxWindow)
    EVT_PAINT (wxLed::OnPaint)
    EVT_LEFT_DOWN(wxLed::mouseDown)
END_EVENT_TABLE ()

wxLed::wxLed(wxWindow * parent, wxWindowID id, wxColour disableColour, wxColour onColour, wxColour offColour,
             const wxPoint & pos, const wxSize & size) : wxWindow()
{
    Create(parent, id, disableColour, onColour, offColour, pos, size);
}

wxLed::wxLed ()
{

}

wxLed::~wxLed ()
{
   delete m_bitmap ;
}

bool wxLed::Create(wxWindow * parent, wxWindowID id, wxColour disableColour, wxColour onColour, wxColour offColour,
                   const wxPoint & pos, const wxSize & size)
{
    if(!wxWindow::Create(parent, id, wxDefaultPosition, wxDefaultSize))
      return false;

    m_bitmap    = NULL;
   m_isEnabled = true ;
   m_isOn      = false;
   m_Disable   = disableColour;
   m_On        = onColour;
   m_Off       = offColour;
   Enable();

   return true;
}

void wxLed::Enable (void)
{
    m_isEnabled = true;
   if( m_isOn)
        SetBitmap (m_On.GetAsString( wxC2S_HTML_SYNTAX)) ;
    else
        SetBitmap (m_Off.GetAsString( wxC2S_HTML_SYNTAX)) ;
}

void wxLed::Disable( void)
{
    m_isEnabled= false;
    SetBitmap( m_Disable.GetAsString( wxC2S_HTML_SYNTAX));
}

void wxLed::Switch(void)
{
    if( m_isEnabled)
    {
        m_isOn = !m_isOn;
        if(m_isOn)
            SetBitmap (m_On.GetAsString( wxC2S_HTML_SYNTAX)) ;
        else
            SetBitmap (m_Off.GetAsString( wxC2S_HTML_SYNTAX)) ;
    }
}

void wxLed::SwitchOn(void)
{
    if( m_isEnabled)
    {
        m_isOn = true;
        SetBitmap (m_On.GetAsString( wxC2S_HTML_SYNTAX)) ;
    }
}

void wxLed::SwitchOff( void)
{
    if( m_isEnabled)
    {
        m_isOn = false;
        SetBitmap (m_Off.GetAsString( wxC2S_HTML_SYNTAX)) ;
    }
}

void wxLed::SetOnColour (wxColour rgb)
{
   m_On = rgb;
    if (m_isEnabled && m_isOn)
        SetBitmap (m_On.GetAsString( wxC2S_HTML_SYNTAX)) ;

}

void wxLed::SetOffColour(wxColour rgb)
{
   m_Off = rgb;
    if (m_isEnabled && !m_isOn)
        SetBitmap (m_Off.GetAsString( wxC2S_HTML_SYNTAX)) ;
}

void wxLed::SetDisableColour(wxColour rgb)
{
   m_Disable = rgb;
    if (!m_isEnabled)
        SetBitmap (m_Disable.GetAsString( wxC2S_HTML_SYNTAX)) ;
}

void wxLed::SetOnOrOff( bool on)
{

    m_isOn = on;
    if(m_isEnabled)
    {
        if( m_isOn)
            SetBitmap (m_On.GetAsString( wxC2S_HTML_SYNTAX)) ;
        else
            SetBitmap (m_Off.GetAsString( wxC2S_HTML_SYNTAX)) ;
    }
}

bool wxLed::IsEnabled( void)
{
    return m_isEnabled;
}

bool wxLed::IsOn(void)
{
    return m_isOn;
}

void wxLed::OnPaint (wxPaintEvent & WXUNUSED (event))
{
   wxPaintDC dc (this) ;
   m_mutex.Lock () ;
   dc.DrawBitmap (* m_bitmap, 0, 0, true) ;
   m_mutex.Unlock () ;
}

#define WX_LED_WIDTH    17
#define WX_LED_HEIGHT   17
#define WX_LED_COLORS    5
#define WX_LED_XPM_COLS    (WX_LED_WIDTH + 1)
#define WX_LED_XPM_LINES   (1 + WX_LED_COLORS + WX_LED_HEIGHT)

bool wxLed::SetBitmap (wxString color)
{
    char ** xpm = NULL ;
    char * xpmData = NULL ;
    xpm = new char * [WX_LED_XPM_LINES] ;
    if (xpm == NULL)
    {
        delete [] xpm ;
        delete [] xpmData ;
        return false;
    }
    xpmData = new char [WX_LED_XPM_LINES * WX_LED_XPM_COLS] ;
    if (xpmData == NULL)
    {
        delete [] xpm ;
        delete [] xpmData ;
        return false;
    }
    for (int i = 0 ; i < WX_LED_XPM_LINES ; i ++)
        xpm [i] = xpmData + i * WX_LED_XPM_COLS ;
    // width height num_colors chars_per_pixel
    sprintf (xpm [0], "%d %d %d 1", WX_LED_WIDTH, WX_LED_HEIGHT, WX_LED_COLORS) ;
    // colors
    strncpy (xpm [1], "  c #006078", WX_LED_XPM_COLS) ;
    strncpy (xpm [2], "- c #C0C0C0", WX_LED_XPM_COLS) ;
    strncpy (xpm [3], "_ c #F8F8F8", WX_LED_XPM_COLS) ;
    strncpy (xpm [4], "* c #FFFFFF", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS], "X c ", WX_LED_XPM_COLS) ;
    strncpy ((xpm [WX_LED_COLORS]) + 4, color.char_str(), 8) ;
    // pixels
    strncpy (xpm [WX_LED_COLORS +  1], "      -----      ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS +  2], "    ---------    ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS +  3], "   -----------   ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS +  4], "  -----XXX----_  ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS +  5], " ----XX**XXX-___ ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS +  6], " ---X***XXXXX___ ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS +  7], "----X**XXXXXX____", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS +  8], "---X**XXXXXXXX___", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS +  9], "---XXXXXXXXXXX___", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS + 10], "---XXXXXXXXXXX___", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS + 11], "----XXXXXXXXX____", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS + 12], " ---XXXXXXXXX___ ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS + 13], " ---_XXXXXXX____ ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS + 14], "  _____XXX_____  ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS + 15], "   ___________   ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS + 16], "    _________    ", WX_LED_XPM_COLS) ;
    strncpy (xpm [WX_LED_COLORS + 17], "      _____      ", WX_LED_XPM_COLS) ;
    m_mutex.Lock () ;
    delete m_bitmap ;
    m_bitmap = new wxBitmap (xpm) ;
    if (m_bitmap == NULL)
    {
        m_mutex.Unlock () ;
        delete [] xpm ;
        delete [] xpmData ;
        return false;
    }
    SetSize (wxSize (m_bitmap->GetWidth (), m_bitmap->GetHeight ())) ;
    m_mutex.Unlock () ;
    Refresh () ;
    delete [] xpm ;
    delete [] xpmData ;
    return true;
}

void wxLed::mouseDown(wxMouseEvent &evt)
{
    if(Pressed)
        Pressed = false;
    else
        Pressed = true;
    SetOnOrOff(Pressed);
    Refresh();
}


The Led is drawn out using an array and strcpy. I have tried setting the position manually and the window dissapears entirely.

Any help would be largely appreciated. :)

Manolo
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 578
Joined: Mon Apr 30, 2012 11:07 pm

Re: How do you set the position of a custom painted control on a parent window?

Postby Manolo » Tue Oct 04, 2016 12:23 am

For what I see, your code is not using wxLed, but you are creating a new class, also called wxLed, with many copied features.

About positioning, if your class derives from wxWindow and you use a wxSizer, both size and position are driven by the sizer. Otherwise, perhaps you need Move(), a function in wxWindow if you derive from it. If no derivation, you must use directly OS-API methods.

The class basically draws a bitmap on the window. You should handle Paint-Event an set the bitmap to the size you wish, maybe the size of the window.

modifiedbroadcast
In need of some credit
In need of some credit
Posts: 2
Joined: Mon Aug 29, 2016 7:51 pm

Re: How do you set the position of a custom painted control on a parent window?

Postby modifiedbroadcast » Wed Oct 05, 2016 4:22 am

Thank you, but I found a more elegant approach using a bitmap panel control.


Return to “Component Writing”

Who is online

Users browsing this forum: No registered users and 1 guest