1 /* 2 * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz 3 * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz 4 * Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de> 5 * Copyright 2012, Haiku, Inc. 6 * Distributed under the terms of the MIT License. 7 */ 8 9 #include <Alert.h> 10 #include <Application.h> 11 #include <Button.h> 12 #include <Window.h> 13 14 #include <stdio.h> 15 16 // include this for ALM 17 #include "ALMLayout.h" 18 19 20 class BAlertPolicy : public BALMLayout::BadLayoutPolicy { 21 virtual bool OnBadLayout(BALMLayout* layout, ResultType result, 22 BLayoutContext* context) 23 { 24 BAlert* alert = new BAlert("layout failure", "layout failed!", 25 "sure"); 26 alert->Go(); 27 return true; 28 } 29 }; 30 31 32 class BadLayoutWindow : public BWindow { 33 public: 34 BadLayoutWindow(BRect frame) 35 : 36 BWindow(frame, "ALM Bad Layout", B_TITLED_WINDOW, 37 B_QUIT_ON_WINDOW_CLOSE) 38 { 39 button1 = new BButton("Bad Layout!"); 40 41 // create a new BALMLayout and use it for this window 42 fLayout = new BALMLayout(); 43 SetLayout(fLayout); 44 45 // add an area containing the button 46 // use the borders of the layout as the borders for the area 47 fLayout->AddView(button1, fLayout->Left(), fLayout->Top(), 48 fLayout->Right(), fLayout->Bottom()); 49 button1->SetExplicitMinSize(BSize(50, 50)); 50 button1->SetExplicitMaxSize(BSize(500, 500)); 51 button1->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, 52 B_ALIGN_USE_FULL_HEIGHT)); 53 54 55 BButton* button2 = new BButton("can't fit!"); 56 fLayout->AddView(button2, fLayout->RightOf(button1), fLayout->Top(), 57 fLayout->Right(), fLayout->Bottom()); 58 button2->SetExplicitMinSize(BSize(50, 50)); 59 60 fLayout->SetBadLayoutPolicy(new BAlertPolicy()); 61 // test size limits 62 BSize min = fLayout->MinSize(); 63 BSize max = fLayout->MaxSize(); 64 SetSizeLimits(min.Width(), max.Width(), min.Height(), max.Height()); 65 66 printf("moving to standard BALMLayout BadLayoutPolicy\n"); 67 fLayout->SetBadLayoutPolicy(NULL); 68 } 69 70 private: 71 BALMLayout* fLayout; 72 BButton* button1; 73 }; 74 75 76 class BadLayout : public BApplication { 77 public: 78 BadLayout() 79 : 80 BApplication("application/x-vnd.haiku.BadLayout") 81 { 82 BRect frameRect; 83 frameRect.Set(100, 100, 300, 300); 84 BadLayoutWindow* window = new BadLayoutWindow(frameRect); 85 window->Show(); 86 } 87 }; 88 89 90 int 91 main() 92 { 93 BadLayout app; 94 app.Run(); 95 return 0; 96 } 97 98