Back to index

lightning-sunbird  0.9+nobinonly
nsCheckButton.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 "nsCheckButton.h"
00039 #include "nsString.h"
00040 
00041 NS_IMPL_ADDREF_INHERITED(nsCheckButton, nsWidget)
00042 NS_IMPL_RELEASE_INHERITED(nsCheckButton, nsWidget)
00043 NS_IMPL_QUERY_INTERFACE2(nsCheckButton, nsICheckButton, nsIWidget)
00044 
00045 //-------------------------------------------------------------------------
00046 //
00047 // nsCheckButton constructor
00048 //
00049 //-------------------------------------------------------------------------
00050 nsCheckButton::nsCheckButton() : nsWidget() , nsICheckButton()
00051 {
00052   mLabel = nsnull;
00053   mCheckButton = nsnull;
00054   mState = PR_FALSE;
00055 }
00056 
00057 //-------------------------------------------------------------------------
00058 //
00059 // nsCheckButton destructor
00060 //
00061 //-------------------------------------------------------------------------
00062 nsCheckButton::~nsCheckButton()
00063 {
00064 }
00065 
00066 void
00067 nsCheckButton::OnDestroySignal(GtkWidget* aGtkWidget)
00068 {
00069   if (aGtkWidget == mCheckButton) {
00070     mCheckButton = nsnull;
00071   }
00072   else if (aGtkWidget == mLabel) {
00073     mLabel = nsnull;
00074   }
00075   else {
00076     nsWidget::OnDestroySignal(aGtkWidget);
00077   }
00078 }
00079 
00080 //-------------------------------------------------------------------------
00081 //
00082 // Create the native CheckButton widget
00083 //
00084 //-------------------------------------------------------------------------
00085 NS_METHOD  nsCheckButton::CreateNative(GtkObject *parentWindow)
00086 {
00087   mWidget = gtk_event_box_new();
00088   mCheckButton = gtk_check_button_new();
00089 
00090   gtk_container_add(GTK_CONTAINER(mWidget), mCheckButton);
00091 
00092   gtk_widget_show(mCheckButton);
00093 
00094   gtk_widget_set_name(mWidget, "nsCheckButton");
00095 
00096   return NS_OK;
00097 }
00098 
00099 void nsCheckButton::InitCallbacks(char * aName)
00100 {
00101   InstallButtonPressSignal(mCheckButton);
00102   InstallButtonReleaseSignal(mCheckButton);
00103 
00104   InstallEnterNotifySignal(mWidget);
00105   InstallLeaveNotifySignal(mWidget);
00106 
00107   // These are needed so that the events will go to us and not our parent.
00108   AddToEventMask(mWidget,
00109                  GDK_BUTTON_PRESS_MASK |
00110                  GDK_BUTTON_RELEASE_MASK |
00111                  GDK_ENTER_NOTIFY_MASK |
00112                  GDK_EXPOSURE_MASK |
00113                  GDK_FOCUS_CHANGE_MASK |
00114                  GDK_KEY_PRESS_MASK |
00115                  GDK_KEY_RELEASE_MASK |
00116                  GDK_LEAVE_NOTIFY_MASK |
00117                  GDK_POINTER_MOTION_MASK);
00118 
00119   // Add in destroy callback
00120   gtk_signal_connect(GTK_OBJECT(mCheckButton),
00121                      "destroy",
00122                      GTK_SIGNAL_FUNC(DestroySignal),
00123                      this);
00124 
00125   InstallSignal((GtkWidget *)mCheckButton,
00126                 (gchar *)"toggled",
00127                 GTK_SIGNAL_FUNC(nsCheckButton::ToggledSignal));
00128 }
00129 
00130 //-------------------------------------------------------------------------
00131 //
00132 // Set this button label
00133 //
00134 //-------------------------------------------------------------------------
00135 NS_METHOD nsCheckButton::SetState(const PRBool aState)
00136 {
00137   mState = aState;
00138 
00139   if (mWidget && mCheckButton) 
00140   {
00141     GtkToggleButton * item = GTK_TOGGLE_BUTTON(mCheckButton);
00142 
00143     item->active = (gboolean) mState;
00144 
00145     gtk_widget_queue_draw(GTK_WIDGET(item));
00146   }
00147 
00148   return NS_OK;
00149 }
00150 
00151 //-------------------------------------------------------------------------
00152 //
00153 // Set this button label
00154 //
00155 //-------------------------------------------------------------------------
00156 NS_METHOD nsCheckButton::GetState(PRBool& aState)
00157 {
00158   aState = mState;
00159     
00160   return NS_OK;
00161 }
00162 
00163 //-------------------------------------------------------------------------
00164 //
00165 // Set this Checkbox label
00166 //
00167 //-------------------------------------------------------------------------
00168 NS_METHOD nsCheckButton::SetLabel(const nsString& aText)
00169 {
00170   if (mWidget) {
00171     NS_LossyConvertUCS2toASCII label(aText);
00172     if (mLabel) {
00173       gtk_label_set(GTK_LABEL(mLabel), label.get());
00174     } else {
00175       mLabel = gtk_label_new(label.get());
00176       gtk_misc_set_alignment (GTK_MISC (mLabel), 0.0, 0.5);
00177       gtk_container_add(GTK_CONTAINER(mCheckButton), mLabel);
00178       gtk_widget_show(mLabel);
00179       gtk_signal_connect(GTK_OBJECT(mLabel),
00180                          "destroy",
00181                          GTK_SIGNAL_FUNC(DestroySignal),
00182                          this);
00183     }
00184   }
00185   return NS_OK;
00186 }
00187 
00188 
00189 //-------------------------------------------------------------------------
00190 //
00191 // Get this button label
00192 //
00193 //-------------------------------------------------------------------------
00194 NS_METHOD nsCheckButton::GetLabel(nsString& aBuffer)
00195 {
00196   aBuffer.SetLength(0);
00197   if (mWidget) {
00198     char * text;
00199     if (mLabel) {
00200       gtk_label_get(GTK_LABEL(mLabel), &text);
00201       aBuffer.AppendWithConversion(text);
00202     }
00203   }
00204   return NS_OK;
00205 }
00206 
00207 /* virtual */ void
00208 nsCheckButton::OnToggledSignal(const gboolean aState)
00209 {
00210   // Untoggle the sonofabitch
00211   if (mWidget && mCheckButton) 
00212   {
00213     GtkToggleButton * item = GTK_TOGGLE_BUTTON(mCheckButton);
00214 
00215     item->active = !item->active;
00216 
00217     gtk_widget_queue_draw(GTK_WIDGET(item));
00218   }
00219 }
00221 
00222 /* static */ gint 
00223 nsCheckButton::ToggledSignal(GtkWidget *      aWidget, 
00224                              gpointer         aData)
00225 {
00226   NS_ASSERTION( nsnull != aWidget, "widget is null");
00227 
00228   nsCheckButton * button = (nsCheckButton *) aData;
00229 
00230   NS_ASSERTION( nsnull != button, "instance pointer is null");
00231 
00232   button->OnToggledSignal(GTK_TOGGLE_BUTTON(aWidget)->active);
00233 
00234   return PR_TRUE;
00235 }
00236 
00238 // SetBackgroundColor for CheckButton
00239 /*virtual*/
00240 void nsCheckButton::SetBackgroundColorNative(GdkColor *aColorNor,
00241                                         GdkColor *aColorBri,
00242                                         GdkColor *aColorDark)
00243 {
00244   // use same style copy as SetFont
00245   GtkStyle *style = gtk_style_copy(GTK_WIDGET (g_list_nth_data(gtk_container_children(GTK_CONTAINER (mWidget)),0))->style);
00246   
00247   style->bg[GTK_STATE_NORMAL]=*aColorNor;
00248   
00249   // Mouse over button
00250   style->bg[GTK_STATE_PRELIGHT]=*aColorBri;
00251 
00252   // Button is down
00253   style->bg[GTK_STATE_ACTIVE]=*aColorDark;
00254 
00255   // other states too? (GTK_STATE_ACTIVE, GTK_STATE_PRELIGHT,
00256   //               GTK_STATE_SELECTED, GTK_STATE_INSENSITIVE)
00257   gtk_widget_set_style(GTK_WIDGET (g_list_nth_data(gtk_container_children(GTK_CONTAINER (mWidget)),0)), style);
00258   // set style for eventbox too
00259   gtk_widget_set_style(mWidget, style);
00260 
00261   gtk_style_unref(style);
00262 }
00263