Home | History | Annotate | Download | only in examples
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/views/examples/example_base.h"
      6 
      7 #include <stdarg.h>
      8 
      9 #include "base/compiler_specific.h"
     10 #include "base/strings/stringprintf.h"
     11 #include "ui/views/view.h"
     12 
     13 namespace views {
     14 namespace examples {
     15 
     16 // Logs the specified string to the status area of the examples window.
     17 // This function can only be called if there is a visible examples window.
     18 void LogStatus(const std::string& status);
     19 
     20 namespace {
     21 
     22 // TODO(oshima): Check if this special container is still necessary.
     23 class ContainerView : public View {
     24  public:
     25   explicit ContainerView(ExampleBase* base)
     26       : example_view_created_(false),
     27         example_base_(base) {
     28   }
     29 
     30  private:
     31   // Overridden from View:
     32   virtual void ViewHierarchyChanged(
     33       const ViewHierarchyChangedDetails& details) OVERRIDE {
     34     View::ViewHierarchyChanged(details);
     35     // We're not using child == this because a Widget may not be
     36     // available when this is added to the hierarchy.
     37     if (details.is_add && GetWidget() && !example_view_created_) {
     38       example_view_created_ = true;
     39       example_base_->CreateExampleView(this);
     40     }
     41   }
     42 
     43   // True if the example view has already been created, or false otherwise.
     44   bool example_view_created_;
     45 
     46   ExampleBase* example_base_;
     47 
     48   DISALLOW_COPY_AND_ASSIGN(ContainerView);
     49 };
     50 
     51 }  // namespace
     52 
     53 ExampleBase::~ExampleBase() {}
     54 
     55 ExampleBase::ExampleBase(const char* title) : example_title_(title) {
     56   container_ = new ContainerView(this);
     57 }
     58 
     59 // Prints a message in the status area, at the bottom of the window.
     60 void ExampleBase::PrintStatus(const char* format, ...) {
     61   va_list ap;
     62   va_start(ap, format);
     63   std::string msg;
     64   base::StringAppendV(&msg, format, ap);
     65   LogStatus(msg);
     66 }
     67 
     68 }  // namespace examples
     69 }  // namespace views
     70