Forum: wxRuby Problem centering a label

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alejandro M. (Guest)
on 2009-03-19 23:09
(Received via mailing list)
Hi



I have frame.

Inside i want to put a static text as title, centered.

I use two sizers, one vertical an other horizontal.



But title is not centered, is put to left.



My code is :



require 'wx';



class TarefasNova < Wx::Panel

    def initialize(parent)

        rect = parent.get_client_rect();



        super( parent, 1011, Wx::Point.new( 0, 0 ),  rect.get_size(),
SIMPLE_BORDER, "NovaTarefa" );



        sizer = Wx::BoxSizer.new(Wx::VERTICAL );

        box   = Wx::BoxSizer.new(Wx::HORIZONTAL);



        label = Wx::StaticText.new( self, :label => "CADASTRO DE TAREFAS
(NOVA)", :size => [250,-1] );



        box.add( label, 1, Wx::ALIGN_CENTER_VERTICAL, 5);



        sizer.add_item(box, 0, Wx::ALIGN_CENTER|Wx::ALL, 5);

     end

end



How to make my title centered in the panel?



Alejandro Michelin Salomon



  _____

avast! Antivirus <http://www.avast.com> : Outbound message clean.


Virus Database (VPS): 090319-0, 19/03/2009
Tested on: 19/03/2009 17:32:59
avast! - copyright (c) 1988-2009 ALWIL Software.
Christian S. (Guest)
on 2009-03-20 00:49
Hi Alejandro,

you just need one sizer on your panel. Add one spacer
(sizer.add_stretch_spacer) before and one after your text.

Like so:

    sizer = BoxSizer.new HORIZONTAL
    sizer.add_stretch_spacer
    sizer.add StaticText.new(self, -1, 'asdf'), 0, 1
    sizer.add_stretch_spacer

hth,
Christian.

Alejandro Michelin salomon wrote:
> Hi
>
>
>
> I have frame.
>
> Inside i want to put a static text as title, centered.
>
> I use two sizers, one vertical an other horizontal.
>
>
>
> But title is not centered, is put to left.
>
>
>
> My code is :
>
>
>
> require 'wx';
>
>
>
> class TarefasNova < Wx::Panel
>
>     def initialize(parent)
>
>         rect = parent.get_client_rect();
>
>
>
>         super( parent, 1011, Wx::Point.new( 0, 0 ),  rect.get_size(),
> SIMPLE_BORDER, "NovaTarefa" );
>
>
>
>         sizer = Wx::BoxSizer.new(Wx::VERTICAL );
>
>         box   = Wx::BoxSizer.new(Wx::HORIZONTAL);
>
>
>
>         label = Wx::StaticText.new( self, :label => "CADASTRO DE TAREFAS
> (NOVA)", :size => [250,-1] );
>
>
>
>         box.add( label, 1, Wx::ALIGN_CENTER_VERTICAL, 5);
>
>
>
>         sizer.add_item(box, 0, Wx::ALIGN_CENTER|Wx::ALL, 5);
>
>      end
>
> end
>
>
>
> How to make my title centered in the panel?
>
>
>
> Alejandro Michelin Salomon
>
>
>
>   _____
>
> avast! Antivirus <http://www.avast.com> : Outbound message clean.
>
>
> Virus Database (VPS): 090319-0, 19/03/2009
> Tested on: 19/03/2009 17:32:59
> avast! - copyright (c) 1988-2009 ALWIL Software.
Alejandro M. (Guest)
on 2009-03-20 14:42
(Received via mailing list)
Christian :



I say in the other mail “I have a frame”,  brong  i have a panel.



The panel is inside the main frame.



I change my code to :



class TarefasNova < Wx::Panel

    def initialize(parent)

        rect = parent.get_client_rect();



        super( parent, 1011, Wx::Point.new( 0, 0 ), rect.get_size(),

              SIMPLE_BORDER,

              "NovaTarefa" );



        #sizer = Wx::BoxSizer.new(Wx::VERTICAL)



        box   = Wx::BoxSizer.new(Wx::HORIZONTAL);



        box.add_stretch_spacer();



        box.add Wx::StaticText.new(self, -1, 'asdf'), 0, 1



        #label = Wx::StaticText.new( self, :label => "CADASTRO DE
TAREFAS
(NOVA)", :size => [250,-1] );



        #box.add( label, 1, Wx::ALIGN_CENTER, 5);



        box.add_stretch_spacer();



        #sizer.add_item(box, 0, Wx::ALIGN_CENTER|Wx::ALL, 5);

    end

end



But the text is not centered, the text is in the left.



Exist some diferences between frame and panel to user sizer?



De: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] Em nome de Christian Schmidt
Enviada em: quinta-feira, 19 de março de 2009 19:49
Para: removed_email_address@domain.invalid
Assunto: Re: [wxruby-users] Problem centering a label



Hi Alejandro,

you just need one sizer on your panel. Add one spacer
(sizer.add_stretch_spacer) before and one after your text.

Like so:

    sizer = BoxSizer.new HORIZONTAL
    sizer.add_stretch_spacer
    sizer.add StaticText.new(self, -1, 'asdf'), 0, 1
    sizer.add_stretch_spacer

hth,
Christian.

Alejandro Michelin salomon wrote:
>
>
>         super( parent, 1011, Wx::Point.new( 0, 0 ),  rect.get_size(),
>         label = Wx::StaticText.new( self, :label => "CADASTRO DE TAREFAS
>      end
>
>
>
>   _____
>
> avast! Antivirus <http://www.avast.com> : Outbound message clean.
>
>
> Virus Database (VPS): 090319-0, 19/03/2009
> Tested on: 19/03/2009 17:32:59
> avast! - copyright (c) 1988-2009 ALWIL Software.

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
wxruby-users mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/wxruby-users




  _____

avast! Antivirus <http://www.avast.com> : Inbound message clean.

Virus Database (VPS): 090319-0, 19/03/2009
Tested on: 20/03/2009 07:35:25
avast! - copyright (c) 1988-2009 ALWIL Software.





  _____

avast! Antivirus <http://www.avast.com> : Outbound message clean.


Virus Database (VPS): 090319-0, 19/03/2009
Tested on: 20/03/2009 09:05:48
avast! - copyright (c) 1988-2009 ALWIL Software.
Christian S. (Guest)
on 2009-03-20 16:34
Hi Alejandro,

this one actually works:

class TarefasNova < Wx::Panel
    def initialize(parent)
      super( parent, 1011, {:style => SIMPLE_BORDER, :name
=>"NovaTarefa"} );

      box = Wx::BoxSizer.new(Wx::HORIZONTAL);
      box.add_stretch_spacer();
      box.add Wx::StaticText.new(self, -1, 'asdf'), 0, 1
      box.add_stretch_spacer();

      set_sizer box
    end
end

Watch for the parents' constructor - you needn't set size and position.
Also never forget to call Panel#set_sizer or your sizer won't have any
effect.

hth,
Christian.
Alex F. (Guest)
on 2009-03-20 17:14
(Received via mailing list)
Hi

Christian Schmidt wrote:
>       super( parent, 1011, {:style => SIMPLE_BORDER, :name
> =>"NovaTarefa"} );
>
>       box = Wx::BoxSizer.new(Wx::HORIZONTAL);
>       box.add_stretch_spacer();
>       box.add Wx::StaticText.new(self, -1, 'asdf'), 0, 1
>       box.add_stretch_spacer();
>
>

Simpler, using StaticText ALIGN styles instead of extra spacers

box = Wx::VBoxSizer.new
st = Wx::StaticText.new(self, :label => 'adsf', :style =>
Wx::ALIGN_CENTRE)
box.add st, 0, Wx::GROW

> Watch for the parents' constructor - you needn't set size and position.
>

This is a good point - use sizers to have children adjust to their
parent's size. Also, if a window is the only child of a frame parent
(often the case with Panels inside Frames) then it will automatically
take up all the avaiable space - no sizer needed.

a
Chauk-Mean P. (Guest)
on 2009-03-20 17:55
(Received via mailing list)
Hi Alejandro,

I'm not sure to understand why you need 2 sizers but your main problem
is that you haven't told the panel to use the sizer !

Here is a sample code where there are 3 texts :
- at the top, the text is centered
- at the middle, the text is left aligned
- at the bottom, the text is right aligned

The important line is the one with self.sizer = box.
You should also use wx constants to make your code clearer.

#!/usr/bin/env ruby
# wxRuby2 Sample Code. Copyright (c) 2004-2008 wxRuby development team
# Freely reusable code: see SAMPLES-LICENSE.TXT for details
begin
  require 'rubygems'
rescue LoadError
end
require 'wx'

class MyPanel < Wx::Panel
  def initialize(parent)
    super(parent)
    box = Wx::BoxSizer.new (Wx::VERTICAL)
    box.add_item(Wx::StaticText.new(self, :label => "Centered Title"),
:flag => Wx::ALIGN_CENTER)
    box.add_item(Wx::StaticText.new(self, :label => "Leftside Text"),
:flag => Wx::ALIGN_LEFT)
    box.add_item(Wx::StaticText.new(self, :label => "Rightside Text"),
 :flag => Wx::ALIGN_RIGHT)
    self.sizer = box
  end
end

# This is the minimum code to start a WxRuby app - create a Frame, and
# show it.
Wx::App.run do
  frame = Wx::Frame.new(nil, :title => "Minimal wxRuby App")
  panel = MyPanel.new(frame)
  frame.show
end

Cheers.

Chauk-Mean.
Alejandro M. (Guest)
on 2009-03-20 19:14
(Received via mailing list)
Chauk-Mean. :

Your  code runs ok in a separate file.



If i call MyPanel from my menu to put the panel in my main frame, the
code
does not work.



This code :



begin

  require 'rubygems'

rescue LoadError

end



require 'wx';



class TarefasNova < Wx::Panel

  def initialize(parent)

    super(parent)

    box = Wx::BoxSizer.new (Wx::VERTICAL)

    box.add_item(Wx::StaticText.new(self, :label => "Centered Title"),

:flag => Wx::ALIGN_CENTER)

    box.add_item(Wx::StaticText.new(self, :label => "Leftside Text"),

:flag => Wx::ALIGN_LEFT)

    box.add_item(Wx::StaticText.new(self, :label => "Rightside Text"),

 :flag => Wx::ALIGN_RIGHT)

    self.sizer = box

  end

end



The panel only shows “Righ”.



Does not size to fill frame.



When i put :



        rect = parent.get_client_rect();



        super( parent, 1011, Wx::Point.new( 0, 0 ), rect.get_size(),
SIMPLE_BORDER, "NovaTarefa" );



This makes the panel fill frame.



The frame has menu bar and statusbar.





De: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] Em nome de Chauk-Mean P.
Enviada em: sexta-feira, 20 de março de 2009 10:39
Para: General discussion of wxRuby
Assunto: Re: [wxruby-users] RES: Problem centering a label



Hi Alejandro,

I'm not sure to understand why you need 2 sizers but your main problem
is that you haven't told the panel to use the sizer !

Here is a sample code where there are 3 texts :
- at the top, the text is centered
- at the middle, the text is left aligned
- at the bottom, the text is right aligned

The important line is the one with self.sizer = box.
You should also use wx constants to make your code clearer.

#!/usr/bin/env ruby
# wxRuby2 Sample Code. Copyright (c) 2004-2008 wxRuby development team
# Freely reusable code: see SAMPLES-LICENSE.TXT for details
begin
  require 'rubygems'
rescue LoadError
end
require 'wx'

class MyPanel < Wx::Panel
  def initialize(parent)
    super(parent)
    box = Wx::BoxSizer.new (Wx::VERTICAL)
    box.add_item(Wx::StaticText.new(self, :label => "Centered Title"),
:flag => Wx::ALIGN_CENTER)
    box.add_item(Wx::StaticText.new(self, :label => "Leftside Text"),
:flag => Wx::ALIGN_LEFT)
    box.add_item(Wx::StaticText.new(self, :label => "Rightside Text"),
 :flag => Wx::ALIGN_RIGHT)
    self.sizer = box
  end
end

# This is the minimum code to start a WxRuby app - create a Frame, and
# show it.
Wx::App.run do
  frame = Wx::Frame.new(nil, :title => "Minimal wxRuby App")
  panel = MyPanel.new(frame)
  frame.show
end

Cheers.

Chauk-Mean.
_______________________________________________
wxruby-users mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/wxruby-users




  _____

avast! Antivirus <http://www.avast.com> : Inbound message clean.

Virus Database (VPS): 090319-0, 19/03/2009
Tested on: 20/03/2009 13:13:39
avast! - copyright (c) 1988-2009 ALWIL Software.





  _____

avast! Antivirus <http://www.avast.com> : Outbound message clean.


Virus Database (VPS): 090319-0, 19/03/2009
Tested on: 20/03/2009 13:56:02
avast! - copyright (c) 1988-2009 ALWIL Software.
Alejandro M. (Guest)
on 2009-03-25 16:09
(Received via mailing list)
Hi :



The problem is that, after call to create the child panel, i need to
call
send_size_event().



I find the solution becouse i minize the aplication to see the code in
the
editor and when i maxsimize the aplicattion the panel is ok filling all
the
frame space.



Alejandro



  _____

avast! Antivirus <http://www.avast.com> : Outbound message clean.


Virus Database (VPS): 090324-0, 24/03/2009
Tested on: 25/03/2009 11:01:09
avast! - copyright (c) 1988-2009 ALWIL Software.
This topic is locked and can not be replied to.