Strange colour on the border of radiobox

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
audio_cf
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Feb 15, 2007 4:55 am

Strange colour on the border of radiobox

Post by audio_cf » Thu Feb 15, 2007 5:14 am

I am new to wxWidgets and an inexperienced programer. I am trying to make a small utillity program with a radiobox.
The problem is that the radiobox has a trange colour arround its border. I have liked a picture below.
http://www.cf-tech.com/app/cdr.bmp

Any hints on what I am doing wrong here ?

Thanks /// Carl

Below is the code for the application

Code: Select all

/////////////////////////////////////////////////////////////////////////////
// Name:        CDRDecoder.cpp
// Purpose:     Make a better CS decoder for CDR's
// Author:      Carl-Fredrik Sundstrom
// Modified by:
// Created:     13.02.07
// Copyright:   (c) Carl-Fredrik Sundstrom
// Licence:     Buddy ware
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================


// ============================================================================
// To do
// ============================================================================
//
//	1. Fix the status bar
//
//  2. Fix look af RadioBox
//
//	3. Create and internal FSM
//
//  4. Populate FSM values from my controlls
//  
//  5. Make virtual master class for different CDR decoding and then implement subclasses for AIR/SDP/PPS-IN



// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#if !wxUSE_STATUSBAR
    #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
#endif // wxUSE_STATUSBAR

// for all others, include the necessary headers
#ifndef WX_PRECOMP
    #include "wx/app.h"
    #include "wx/bitmap.h"
    #include "wx/button.h"
    #include "wx/log.h"
    #include "wx/frame.h"
    #include "wx/statusbr.h"
    #include "wx/timer.h"
    #include "wx/checkbox.h"
    #include "wx/statbmp.h"
    #include "wx/menu.h"
    #include "wx/msgdlg.h"
    #include "wx/textdlg.h"
    #include "wx/sizer.h"
    #include "wx/stattext.h"
    #include "wx/bmpbuttn.h"
    #include "wx/dcmemory.h"
    #include "wx/radiobox.h"
    #include "wx/statbox.h"
    #include "wx/textctrl.h"
	#include "wx/string.h"
	#include "wx/colour.h"
	#include "wx/filepicker.h"

#endif

#include "wx/datetime.h"
#include "wx/numdlg.h"
#include "wx/sizer.h"
//#include "widgets.h"
#include "icons/radiobox.xpm"







// define this for the platforms which don't support wxBitmapButton (such as
// Motif), else a wxBitmapButton will be used
#ifdef __WXMOTIF__
//#define USE_MDI_PARENT_FRAME 1

#ifdef USE_MDI_PARENT_FRAME
    #include "wx/mdi.h"
#endif // USE_MDI_PARENT_FRAME
    #define USE_STATIC_BITMAP
#endif

// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------

#ifdef USE_STATIC_BITMAP
    #include "green.xpm"
    #include "red.xpm"
#endif // USE_STATIC_BITMAP

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
    // override base class virtuals
    // ----------------------------

    // this one is called on application startup and is a good place for the app
    // initialization (doing it here and not in the ctor allows to have an error
    // return: if OnInit() returns false, the application terminates)
    virtual bool OnInit();
};


// a radiobox which handles focus set/kill (for testing)
class MyRadioBox : public wxRadioBox
{
public:
    MyRadioBox(wxWindow *parent,
               wxWindowID id,
               const wxString& title = wxEmptyString,
               const wxPoint& pos = wxDefaultPosition,
               const wxSize& size = wxDefaultSize,
               int n = 0, const wxString choices[] = NULL,
               int majorDim = 1,
               long style = wxRA_HORIZONTAL,
               const wxValidator& validator = wxDefaultValidator,
               const wxString& name = wxRadioBoxNameStr)
        : wxRadioBox(parent, id, title, pos, size, n, choices, majorDim,
                     style, validator, name)
    {
    }

protected:
    void OnFocusGot(wxFocusEvent& event)
    {
        wxLogMessage(_T("MyRadioBox::OnFocusGot"));

        event.Skip();
    }

    void OnFocusLost(wxFocusEvent& event)
    {
        wxLogMessage(_T("MyRadioBox::OnFocusLost"));

        event.Skip();
    }

private:
    DECLARE_EVENT_TABLE()
};

// A custom status bar which contains controls, icons &c
class MyStatusBar : public wxStatusBar
{
public:
    MyStatusBar(wxWindow *parent);
    virtual ~MyStatusBar();

    void UpdateClock();

    // event handlers
#if wxUSE_TIMER
    void OnTimer(wxTimerEvent& WXUNUSED(event)) { UpdateClock(); }
#endif
    void OnSize(wxSizeEvent& event);
    void OnToggleClock(wxCommandEvent& event);
    void OnButton(wxCommandEvent& event);

private:
    // toggle the state of the status bar controls
    void DoToggle();

    wxBitmap CreateBitmapForButton(bool on = false);

    enum
    {
        Field_Text,
        Field_Checkbox,
        Field_Bitmap,
        Field_Clock,
        Field_Max
    };

#if wxUSE_TIMER
    wxTimer m_timer;
#endif

#if wxUSE_CHECKBOX
    wxCheckBox *m_checkbox;
#endif
#ifdef USE_STATIC_BITMAP
    wxStaticBitmap *m_statbmp;
#else
    wxBitmapButton *m_statbmp;
#endif

    DECLARE_EVENT_TABLE()
};

// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
    // ctor(s)
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
#ifdef USE_MDI_PARENT_FRAME
class MyFrame : public wxMDIParentFrame
#else
    virtual ~MyFrame();
#endif

    // event handlers (these functions should _not_ be virtual)
    void OnQuit(wxCommandEvent& event);
	void OnRadio( wxCommandEvent &event );


private:
    enum StatBarKind
    {
        StatBar_Default,
        StatBar_Custom,
        StatBar_Max
    } m_statbarKind;
    void OnUpdateSetStatusFields(wxUpdateUIEvent& event);
    void OnUpdateStatusBarToggle(wxUpdateUIEvent& event);
    void OnUpdateSetStyleNormal(wxUpdateUIEvent& event);
    void OnUpdateSetStyleFlat(wxUpdateUIEvent& event);
    void OnUpdateSetStyleRaised(wxUpdateUIEvent& event);
    void OnStatusBarToggle(wxCommandEvent& event);
    void ApplyStyle();

    MyStatusBar *m_statbarCustom;
    wxRadioBox  *m_radio;
	wxPanel		*m_panel;
    wxFilePickerCtrl *m_filePicker;
    wxButton *m_button;
	wxBoxSizer *topsizer;
	wxBoxSizer *horsizer;
	wxTextCtrl *m_text;





    // any class wishing to process wxWidgets events must use this macro
    DECLARE_EVENT_TABLE()
};


/*
// Our about dialog ith its status bar
class MyAboutDialog : public wxDialog
{
public:
    MyAboutDialog(wxWindow *parent);
};
*/
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

// IDs for the controls and the menu commands
enum
{
    // menu items
    StatusBar_Quit = 1,
    StatusBar_SetFields,
    StatusBar_Recreate,
    StatusBar_Toggle,
    StatusBar_Checkbox = 1000,
    StatusBar_SetStyle,
    StatusBar_SetStyleNormal,
    StatusBar_SetStyleFlat,
    StatusBar_SetStyleRaised,
	ID_RADIOBOX
};

static const int BITMAP_SIZE_X = 32;
static const int BITMAP_SIZE_Y = 15;

// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------

// the event tables connect the wxWidgets events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
#ifdef USE_MDI_PARENT_FRAME
BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
#else
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
#endif
//    EVT_UPDATE_UI(StatusBar_SetStyleRaised, MyFrame::OnUpdateSetStyleRaised)
	  EVT_RADIOBOX  (ID_RADIOBOX,             MyFrame::OnRadio)
END_EVENT_TABLE()

BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
    EVT_SIZE(MyStatusBar::OnSize)
#if wxUSE_TIMER
    EVT_TIMER(wxID_ANY, MyStatusBar::OnTimer)
#endif
END_EVENT_TABLE()

// Create a new application object: this macro will allow wxWidgets to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also declares the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
// not wxApp)
IMPLEMENT_APP(MyApp)

// ============================================================================
// implementation
// ============================================================================

// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------

// `Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()


{
    // create the main application window
    MyFrame *frame = new MyFrame(_T("CF CDR Decoder v0.1"),
                                 wxPoint(50, 50), wxSize(450, 340));

    // and show it (the frames, unlike simple controls, are not shown when
    // created initially)

	  // Give it an icon (this is ignored in MDI mode: uses resources)
#ifdef __WXMSW__
  frame->SetIcon(wxIcon(_T("sashtest_icn")));
#endif

	
	frame->Show(true);

	


    // success: wxApp::OnRun() will be called which will enter the main message
    // loop and the application will run. If we returned 'false' here, the
    // application would exit immediately.
    return true;
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
#ifdef USE_MDI_PARENT_FRAME
       : wxMDIParentFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
#else
       : wxFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
#endif
{

    m_statbarCustom = NULL;

   // CreateStatusBar(2);
	m_statbarCustom = new MyStatusBar(this);
	SetStatusBar(m_statbarCustom);

	//(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTAB_TRAVERSAL, const wxString& name = "panel")


	
		this->SetBackgroundColour("wxLIGHT_GREY");
		this->SetForegroundColour("wxLIGHT_GREY");
		this->SetOwnBackgroundColour("wxLIGHT_GREY");


	//static const wxString panelName =  _T("CDR");
	//m_panel = new wxPanel(this, -1,wxDefaultPosition,wxDefaultSize,wxTAB_TRAVERSAL, panelName);

	//m_panel->SetBackgroundColour("wxLIGHT_GREY");


		
	static const wxString choice[] =
    {
        _T("SDP"),
        _T("AIR"),
        _T("PPS-IN/PSL/MSC")
    };
/*
m_button->GetParent()->SetOwnBackgroundColour(wxColor("RED")); // works fine with the frame under my wxToggleButton 
m_button->SetBackgroundColour(wxColor("RED")); // doesn't work at all 
m_button->SetLabel("On"); // need that for my app 
m_button->Refresh();  // did that because manual suggested it 
*/
   m_radio = new wxRadioBox( this, ID_RADIOBOX, _T("Choose CDR type"), wxDefaultPosition /*xPoint(10,10)*/, wxDefaultSize, /*sizeof(choice)*/ 3, choice, 1, wxRA_SPECIFY_COLS );
	//m_radio->SetItemToolTip(nb, _T("tooltip for\n") + choices[nb]);
   m_radio->SetBackgroundColour("wxLIGHT_GREY");
   m_radio->SetForegroundColour("wxLIGHT_GREY");
   m_radio->SetOwnBackgroundColour("wxLIGHT_GREY");

   m_radio->GetParent()->SetOwnBackgroundColour("wxLIGHT_GREY");
   m_radio->GetParent()->SetBackgroundColour("wxLIGHT_GREY");
   
   topsizer = new wxBoxSizer( wxVERTICAL );
	horsizer = new wxBoxSizer( wxHORIZONTAL );
   horsizer->Add(m_radio, 0, wxALL|wxALIGN_TOP|wxALIGN_LEFT, 20);


//   wxFilePickerCtrl(wxWindow *parent, wxWindowID id, const wxString& path = wxEmptyString, const wxString& message = "Select a file", const wxString& wildcard = "*.*", const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxFLP_DEFAULT_STYLE, const wxValidator& validator = wxDefaultValidator, const wxString& name = "filepickerctrl")
    m_filePicker = new wxFilePickerCtrl(this, -1,
                                        wxEmptyString,
                                        wxT("Select a CDR file to decode"), wxT("*"),
                                        wxDefaultPosition, wxDefaultSize,
                                        wxFLP_OPEN |wxFLP_USE_TEXTCTRL |wxFLP_FILE_MUST_EXIST);

  // topsizer->Add(m_filePicker, 0, wxALL|wxALIGN_CENTER |wxALIGN_LEFT|wxSHAPED, 10);


        m_button = new wxButton(this, -1, wxT("Decode"),wxDefaultPosition, wxDefaultSize,0);

					//  topsizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
			horsizer->Add(m_filePicker, 0, wxALIGN_CENTER|wxALL, 5);
			horsizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
		      horsizer->Add(m_button, 0,  wxALIGN_CENTER|wxALL, 5);



  topsizer->Add(horsizer, 0, wxALL|wxALIGN_LEFT |wxSHAPED, 10 ); // no border and centre horizontally

	m_text = new wxTextCtrl( this, -1, "Decoded output", wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE);
  // create text ctrl with minimal size 100x60
  topsizer->Add( m_text,1, wxEXPAND | wxALIGN_CENTER_VERTICAL|wxALL, 10 );







			
			   SetSizer( topsizer );      // use the sizer for layout

  topsizer->SetSizeHints( this );   // set size hints to honour minimum size

    SetStatusText(_T("Welcome to CF's CDR Decoder"));

}

void MyFrame::OnRadio( wxCommandEvent &event )
{

    SetStatusText(_T("ON RADIO"));
}


MyFrame::~MyFrame()
{
    SetStatusBar(NULL);

    //delete m_statbarDefault;
    delete m_statbarCustom;
}






// ----------------------------------------------------------------------------
// MyStatusBar
// ----------------------------------------------------------------------------

#ifdef __VISUALC__
    // 'this' : used in base member initializer list -- so what??
    #pragma warning(disable: 4355)
#endif

MyStatusBar::MyStatusBar(wxWindow *parent)
           : wxStatusBar(parent, wxID_ANY)
#if wxUSE_TIMER
             , m_timer(this)
#endif
#if wxUSE_CHECKBOX
             , m_checkbox(NULL)
#endif
{
    static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 };

    SetFieldsCount(Field_Max);
    SetStatusWidths(Field_Max, widths);


#ifdef USE_STATIC_BITMAP
    m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxIcon(green_xpm));
#else
    m_statbmp = new wxBitmapButton(this, wxID_ANY, CreateBitmapForButton(),
                                   wxDefaultPosition, wxDefaultSize,
                                   wxBU_EXACTFIT);
#endif

#if wxUSE_TIMER
    m_timer.Start(1000);
#endif

    SetMinHeight(BITMAP_SIZE_Y);

    UpdateClock();
}

#ifdef __VISUALC__
    #pragma warning(default: 4355)
#endif

MyStatusBar::~MyStatusBar()
{
#if wxUSE_TIMER
    if ( m_timer.IsRunning() )
    {
        m_timer.Stop();
    }
#endif
}

wxBitmap MyStatusBar::CreateBitmapForButton(bool on)
{
    static const int BMP_BUTTON_SIZE_X = 10;
    static const int BMP_BUTTON_SIZE_Y = 9;

    wxBitmap bitmap(BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y);
    wxMemoryDC dc;
    dc.SelectObject(bitmap);
    dc.SetBrush(on ? *wxGREEN_BRUSH : *wxRED_BRUSH);
    dc.SetBackground(*wxLIGHT_GREY_BRUSH);
    dc.Clear();
    dc.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y);
    dc.SelectObject(wxNullBitmap);

    return bitmap;
}

void MyStatusBar::OnSize(wxSizeEvent& event)
{

    wxRect rect;
    GetFieldRect(Field_Checkbox, rect);


    GetFieldRect(Field_Bitmap, rect);
    wxSize size = m_statbmp->GetSize();

    m_statbmp->Move(rect.x + (rect.width - size.x) / 2,
                    rect.y + (rect.height - size.y) / 2);

    event.Skip();
}




void MyStatusBar::UpdateClock()
{
    SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
}



upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Thu Feb 15, 2007 8:35 am

Hi!
The problem I see with your code that leads to the color problem is that you set the colors explicitly for each control. Although this works (the colors show up correctly here) under some circumstances, you might not need to do this and ease your life :)

I guess you did all that color setting because a wxFrame has a dark grey background by default. This is a standard behavior, by the way. A simple way to circumvent this is to place a wxPanel inside the wxFrame as the top widget. All other controls go on the panel and should adapt the correct background colors. So, simply create a wxPanel with the wxFrame as its parent and reparent all other widgets to have the panel as parent. You don't need to worry about sizing the panel: The first ("top") widget in a frame is always expanded to fit the client area of the frame. Simply set the sizers for the panel.

Hope this helps a bit.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

audio_cf
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Feb 15, 2007 4:55 am

Post by audio_cf » Thu Feb 15, 2007 9:14 am

Thanks for the hint. I added a panel and got everything to look ok.

Now I have another problem!

When I resize the window the panel doesn't change size.

Probably its something simple ?

Best regards /// Carl-Fredrik

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Thu Feb 15, 2007 9:26 am

Hi!
Depends on how you added the panel.

Try something simple first: Create a frame and add a panel. When resizing the frame, so should the panel.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu Feb 15, 2007 9:04 pm

This might just be that you put too many arguments when creating the panel. (happened to me once)

wxPanel* myPanel = new wxPanel(this);

this should be all you need, maybe just add ID if you need to catch events

Post Reply