Back to index

lightning-sunbird  0.9+nobinonly
nsButton.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <gtk/gtk.h>
00039 
00040 #include "nsGtkEventHandler.h"
00041 
00042 #include "nsButton.h"
00043 #include "nsString.h"
00044 
00045 NS_IMPL_ADDREF_INHERITED(nsButton, nsWidget)
00046 NS_IMPL_RELEASE_INHERITED(nsButton, nsWidget)
00047 NS_IMPL_QUERY_INTERFACE2(nsButton, nsIButton, nsIWidget)
00048 
00049 //-------------------------------------------------------------------------
00050 //
00051 // nsButton constructor
00052 //
00053 //-------------------------------------------------------------------------
00054 nsButton::nsButton() : nsWidget() , nsIButton()
00055 {
00056 }
00057 
00058 //-------------------------------------------------------------------------
00059 //
00060 // Create the native Button widget
00061 //
00062 //-------------------------------------------------------------------------
00063 NS_IMETHODIMP nsButton::CreateNative(GtkObject *parentWindow)
00064 {
00065     if (!GDK_IS_SUPERWIN(parentWindow)) {
00066 #ifdef DEBUG
00067     g_print("Damn, brother.  That's not a superwin.\n");
00068 #endif
00069     return NS_ERROR_FAILURE;
00070   }
00071   
00072   GdkSuperWin *superwin = GDK_SUPERWIN(parentWindow);
00073 
00074   mMozBox = gtk_mozbox_new(superwin->bin_window);
00075 
00076   mWidget = gtk_button_new_with_label("");
00077   gtk_widget_set_name(mWidget, "nsButton");
00078 
00079   // make sure that we put the scrollbar into the mozbox
00080 
00081   gtk_container_add(GTK_CONTAINER(mMozBox), mWidget);
00082 
00083   return NS_OK;
00084 }
00085 
00086 //-------------------------------------------------------------------------
00087 //
00088 // nsButton destructor
00089 //
00090 //-------------------------------------------------------------------------
00091 nsButton::~nsButton()
00092 {
00093 }
00094 
00095 void nsButton::InitCallbacks(char * aName)
00096 {
00097   InstallButtonPressSignal(mWidget);
00098   InstallButtonReleaseSignal(mWidget);
00099 
00100   InstallEnterNotifySignal(mWidget);
00101   InstallLeaveNotifySignal(mWidget);
00102 
00103   // These are needed so that the events will go to us and not our parent.
00104   AddToEventMask(mWidget,
00105                  GDK_BUTTON_PRESS_MASK |
00106                  GDK_BUTTON_RELEASE_MASK |
00107                  GDK_ENTER_NOTIFY_MASK |
00108                  GDK_EXPOSURE_MASK |
00109                  GDK_FOCUS_CHANGE_MASK |
00110                  GDK_KEY_PRESS_MASK |
00111                  GDK_KEY_RELEASE_MASK |
00112                  GDK_LEAVE_NOTIFY_MASK |
00113                  GDK_POINTER_MOTION_MASK);
00114 }
00115 
00116 //-------------------------------------------------------------------------
00117 //
00118 // Set this button label
00119 //
00120 //-------------------------------------------------------------------------
00121 NS_IMETHODIMP nsButton::SetLabel(const nsString& aText)
00122 {
00123   gtk_label_set(GTK_LABEL(GTK_BIN (mWidget)->child),
00124                 NS_LossyConvertUCS2toASCII(aText).get());
00125   return (NS_OK);
00126 }
00127 
00128 //-------------------------------------------------------------------------
00129 //
00130 // Get this button label
00131 //
00132 //-------------------------------------------------------------------------
00133 NS_IMETHODIMP nsButton::GetLabel(nsString& aBuffer)
00134 {
00135   char * text;
00136 
00137   gtk_label_get(GTK_LABEL(GTK_BIN (mWidget)->child), &text);
00138   aBuffer.SetLength(0);
00139   aBuffer.AppendWithConversion(text);
00140 
00141   return (NS_OK);
00142 
00143 }
00144 
00145 //-------------------------------------------------------------------------
00146 //
00147 // set font for button
00148 //
00149 //-------------------------------------------------------------------------
00150 /* virtual */
00151 void nsButton::SetFontNative(GdkFont *aFont)
00152 {
00153   GtkStyle *style = gtk_style_copy(GTK_BIN (mWidget)->child->style);
00154   // gtk_style_copy ups the ref count of the font
00155   gdk_font_unref (style->font);
00156   
00157   style->font = aFont;
00158   gdk_font_ref(style->font);
00159   
00160   gtk_widget_set_style(GTK_BIN (mWidget)->child, style);
00161   
00162   gtk_style_unref(style);
00163 }