Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Nick Glynn 2011
- * Johan Dahlin 2008
- *
- * A quite simple Gtk.Widget subclass which demonstrates how to subclass
- * and do realizing, sizing and drawing. Originally based on widget.py in PyGTK
- * and ported to GTK+ 3
- *
- * valac --pkg gtk+-3.0 ./main.vala
- *
- * http://www.valadoc.org/gtk+-3.0/Gtk.Container.html
- * http://developer.gnome.org/gtk3/3.0/GtkContainer.html#gtk-container-get-resize-mode
- * http://developer.gnome.org/gtkmm-tutorial/3.0/sec-custom-containers.html.en
- * https://github.com/mdamt/blankon-panel
- * https://live.gnome.org/Vala/CustomWidgetSamples
- * http://git.xmms2.org/xmms2/abraca/tree/src/widgets/rating_entry.vala?id=ed5e182c4074f1bff56010658a36a75d95807921
- */
- using Gtk;
- using Cairo;
- public class HVBox : Container {
- private int height = 28;
- private int minimum_width = 0;
- private int natural_width = 0;//store available width
- private int natural_height = 0;
- private int width = 0;
- private Gtk.SizeRequestMode mode = Gtk.SizeRequestMode.HEIGHT_FOR_WIDTH;
- private List<Widget> children;
- private Window HVBParent;
- public HVBox(Window parent) {
- set_has_window (false);
- children = new List<Widget> ();
- HVBParent=parent;
- }
- public static Gdk.Rectangle get_primary_monitor_geometry () {
- Gdk.Rectangle r = {0, 0};
- var screen = Gdk.Screen.get_default ();
- screen.get_monitor_geometry (screen.get_primary_monitor(), out r);
- return r;
- }
- /*3.2 public override void size_allocate (Gtk.Allocation allocation) {
- /*3.0*/ public override void size_allocate (Gdk.Rectangle allocation) {
- /*
- if(allocation.width != this.natural_width){
- this.queue_resize_no_redraw ();
- var minimum_height_1=0;
- this._get_preferred_height_for_width(allocation.width,out minimum_height_1, out this.natural_height);
- HVBParent.set_size_request (-1,this.natural_height);
- stdout.printf("orig size_allocate !!!! natural_height=%d\n",this.natural_height);
- }*/
- //base.size_allocate (allocation);
- stdout.printf("orig size_allocate x=%d y=%d w=%d h=%d\n",allocation.x,allocation.y,allocation.width,allocation.height);
- //Gdk.Rectangle allocation
- var pos_x=allocation.x;
- var orig_pos_x = allocation.x;
- var orig_pos_y = allocation.y;
- var orig_pos_h = allocation.height;
- var orig_pos_w = allocation.width;
- var sum_w=0;
- var sum_h=0;
- var w_count=0;
- var level=0;
- foreach( Widget widget in this.children){
- allocation.height=this.height;
- var natural_width = 0;
- /*3.2 widget.get_preferred_width (out allocation.width, out natural_width);
- /*3.0 */ widget.get_preferred_width (out allocation.width, natural_width);
- if(w_count>0){
- if( (sum_w + allocation.width) > orig_pos_w-1){
- allocation.y += ( w_count > 0 ? allocation.height : 0);
- pos_x = orig_pos_x;
- sum_w = allocation.width;
- level++;
- }else{
- sum_w += allocation.width;
- }
- allocation.y = orig_pos_y+allocation.height * level;
- } else {
- allocation.y = orig_pos_y;
- sum_w += allocation.width;
- }//if w_count
- allocation.x=pos_x;
- widget.size_allocate(allocation);
- pos_x=orig_pos_x+sum_w;
- w_count++;
- }//foreach
- allocation.x = orig_pos_x;
- allocation.y = orig_pos_y;
- allocation.height = allocation.height * (level+1);//(orig_pos_h>sum_h ? orig_pos_h : sum_h);
- allocation.width = orig_pos_w;
- stdout.printf("orig2 size_allocate x=%d y=%d w=%d h=%d\n",allocation.x,allocation.y,allocation.width,allocation.height);
- base.size_allocate (allocation);
- }
- public override void get_preferred_width (out int minimum_width, out int natural_width) {
- var sum_w = 0;
- var max_w = 0;
- foreach( Widget widget in this.children){
- /*3.2 widget.get_preferred_width (out minimum_width, out natural_width);
- /*3.0 */ widget.get_preferred_width (out minimum_width, natural_width);
- max_w=(minimum_width>max_w ? minimum_width : max_w);
- sum_w+=minimum_width;
- }
- this.minimum_width=minimum_width=this.width=max_w;
- natural_width=sum_w;
- stdout.printf("get_preferred_width minimum=%d natural=%d\n",minimum_width,natural_width);
- }
- public override void get_preferred_width_for_height (int height,out int minimum_width, out int natural_width) {
- var sum_w = 0;
- foreach( Widget widget in this.children){
- /*3.2 widget.get_preferred_width (out minimum_width,out natural_width);
- /*3.0 */ widget.get_preferred_width (out minimum_width, natural_width);
- sum_w+=minimum_width;
- }
- if(minimum_width>this.width){
- minimum_width=(int)(sum_w/((int)(height/28)));
- }else{
- minimum_width=this.width;
- }
- natural_width=minimum_width;
- stdout.printf("get_preferred_width_for_height=%d minimum=%d natural=%d\n",height,minimum_width,natural_width);
- }
- public override void get_preferred_height (out int minimum_height, out int natural_height) {
- natural_height=minimum_height=28;
- stdout.printf("get_preferred_height minimum=%d natural=%d\n",minimum_height,natural_height);
- }
- public override void get_preferred_height_for_width (int width,out int minimum_height, out int natural_height) {
- /*workaround for min_height, if actual available width more than self minimum width*/
- if( this.minimum_width!=width ||
- !(this.minimum_width==width && this.natural_width >= width) ){
- this.natural_width=width;
- }
- this._get_preferred_height_for_width(this.natural_width,out minimum_height, out natural_height);
- this.natural_height = natural_height;
- stdout.printf("get_preferred_height_for_width=%d != %d minimum=%d natural=%d\n",width,this.natural_width,minimum_height,natural_height);
- }
- private void _get_preferred_height_for_width (int width,out int minimum_height, out int natural_height) {
- var max_h = 0;
- var allocation = Gtk.Allocation();//don't use new for struct
- allocation.x=0;
- allocation.y=0;
- allocation.height=0;
- allocation.width=width;
- //Gdk.Rectangle allocation
- var pos_x=allocation.x;
- var orig_pos_x = allocation.x;
- var orig_pos_y = allocation.y;
- var orig_pos_h = allocation.height;
- var orig_pos_w = allocation.width;
- var sum_w=0;
- var sum_h=0;
- var w_count=0;
- var level=0;
- foreach( Widget widget in this.children){
- allocation.height=this.height;
- var natural_width = 0;
- /*3.2 widget.get_preferred_width (out allocation.width, out natural_width);*/
- /*3.0 */ widget.get_preferred_width (out allocation.width, natural_width);
- if(w_count>0){
- if( (sum_w + allocation.width) > orig_pos_w-1){
- allocation.y += ( w_count > 0 ? allocation.height : 0);
- pos_x = orig_pos_x;
- sum_w = allocation.width;
- level++;
- }else{
- sum_w += allocation.width;
- //allocation.y=orig_pos_y+sum_h;
- }
- allocation.y = orig_pos_y+allocation.height * level;
- } else {
- allocation.y = orig_pos_y;
- sum_w += allocation.width;
- }//if w_count
- allocation.x=pos_x;//allocation2.width;
- //stdout.printf("size_allocate x=%d y=%d w=%d h=%d\n",allocation.x,allocation.y,allocation.width,allocation.height);
- pos_x=orig_pos_x+sum_w;
- w_count++;
- }//foreach
- allocation.x = orig_pos_x;
- allocation.y = orig_pos_y;
- allocation.height = allocation.height * (level+1);//(orig_pos_h>sum_h ? orig_pos_h : sum_h);
- allocation.width = orig_pos_w;
- //stdout.printf("orig2 size_allocate x=%d y=%d w=%d h=%d\n",allocation.x,allocation.y,allocation.width,allocation.height);
- minimum_height=natural_height=allocation.height;
- }//_get_preferred_height_for_width
- public override SizeRequestMode get_request_mode () {
- //stdout.printf("get_request_mode\n");
- return (this.mode /*| Gtk.SizeRequestMode.WIDTH_FOR_HEIGHT*/);//.CONSTANT_SIZE;//WIDTH_FOR_HEIGHT;//HEIGHT_FOR_WIDTH;
- }
- public override void forall_internal(bool include_internal,Gtk.Callback callback){
- //stdout.printf("forall_internal %d is_internal=%s\n",(int)callback,include_internal.to_string());
- foreach( Widget widget in this.children){
- callback(widget);
- }
- }
- public override void add (Widget widget){
- stdout.printf("add\n");
- widget.set_parent(this);
- if(children.first()!=null)
- children.append(widget);
- else
- children.prepend(widget);
- }
- public override void remove (Widget widget){
- stdout.printf("remove\n");
- children.remove(widget);
- }
- public Widget children_nth (int index){
- stdout.printf("Get_from_index\n");
- return children.nth_data(index);
- }
- public Widget children_last (){
- stdout.printf("children_last\n");
- return children.last().data;
- }
- }
- int main (string[] args) {
- Gtk.init (ref args);
- var win = new Window ();
- //win.set_size_request (20,20);
- win.border_width = 5;
- win.title = "Widget test";
- win.destroy.connect (Gtk.main_quit);
- //win.default_height=20;
- //win.default_width =20;
- win.resizable = true;
- //var frame = new Frame (".");
- //win.add (frame);
- var hvbox = new HVBox(win);
- var my_button = new Button.with_label ("1Click me!_____________");
- my_button.set_has_window (false);
- my_button.show();
- hvbox.add(my_button);
- my_button = new Button.with_label ("2Click me!");
- my_button.set_has_window (false);
- my_button.show();
- hvbox.add(my_button);
- my_button = new Button.with_label ("3Click me!______________________________________________________");
- my_button.set_has_window (false);
- my_button.show();
- hvbox.add(my_button);
- my_button = new Button.with_label ("4Click me!");
- my_button.set_has_window (false);
- my_button.show();
- hvbox.add(my_button);
- my_button = new Button.with_label ("5Click me!");
- my_button.set_has_window (false);
- my_button.show();
- hvbox.add(my_button);
- my_button = new Button.with_label ("6Click me!");
- my_button.set_has_window (false);
- my_button.show();
- hvbox.add(my_button);
- hvbox.set_vexpand(false);
- hvbox.set_hexpand(false);
- var hbox = new VBox(false,0);
- var hbox2 = new VBox(false,0);
- var fmy_button = new Button.with_label ("add");
- fmy_button.set_has_window (false);
- fmy_button.show();
- fmy_button.clicked.connect(()=>{
- var fmy_button2 = new Button.with_label ("Auto button");
- fmy_button2.set_has_window (false);
- fmy_button2.show();
- hvbox.add(fmy_button2);
- });
- fmy_button.set_vexpand(true);
- fmy_button.set_hexpand(true);
- hbox2.pack_start(fmy_button,false,false,0);
- fmy_button = new Button.with_label ("remove");
- fmy_button.set_has_window (false);
- fmy_button.show();
- fmy_button.clicked.connect(()=>{
- Widget w=hvbox.children_last();
- hvbox.remove(w);
- w.destroy();
- });
- hbox2.pack_start(fmy_button,true,true,0);
- fmy_button.set_vexpand(false);
- fmy_button.set_hexpand(false);
- hbox.pack_start(hbox2,true,true,0);
- hbox.pack_start(hvbox,false,false,0);
- win.add (hbox);
- win.show_all ();
- Gtk.main ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement