1 /* 2 * This file is part of d-handy. 3 * 4 * d-handy is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * d-handy is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with d-handy; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 module handy.SearchBar; 20 21 private import gdk.Event; 22 private import glib.ConstructionException; 23 private import gobject.ObjectG; 24 private import gtk.Bin; 25 private import gtk.BuildableIF; 26 private import gtk.BuildableT; 27 private import gtk.Entry; 28 private import gtk.Widget; 29 private import handy.c.functions; 30 public import handy.c.types; 31 32 33 /** */ 34 public class SearchBar : Bin 35 { 36 /** the main Gtk struct */ 37 protected HdySearchBar* hdySearchBar; 38 39 /** Get the main Gtk struct */ 40 public HdySearchBar* getSearchBarStruct(bool transferOwnership = false) 41 { 42 if (transferOwnership) 43 ownedRef = false; 44 return hdySearchBar; 45 } 46 47 /** the main Gtk struct as a void* */ 48 protected override void* getStruct() 49 { 50 return cast(void*)hdySearchBar; 51 } 52 53 /** 54 * Sets our main struct and passes it to the parent class. 55 */ 56 public this (HdySearchBar* hdySearchBar, bool ownedRef = false) 57 { 58 this.hdySearchBar = hdySearchBar; 59 super(cast(GtkBin*)hdySearchBar, ownedRef); 60 } 61 62 63 /** */ 64 public static GType getType() 65 { 66 return hdy_search_bar_get_type(); 67 } 68 69 /** 70 * Creates a #HdySearchBar. You will need to tell it about 71 * which widget is going to be your text entry using 72 * hdy_search_bar_connect_entry(). 73 * 74 * Returns: a new #HdySearchBar 75 * 76 * Since: 0.0.6 77 * 78 * Throws: ConstructionException GTK+ fails to create the object. 79 */ 80 public this() 81 { 82 auto __p = hdy_search_bar_new(); 83 84 if(__p is null) 85 { 86 throw new ConstructionException("null returned by new"); 87 } 88 89 this(cast(HdySearchBar*) __p); 90 } 91 92 /** 93 * Connects the #GtkEntry widget passed as the one to be used in 94 * this search bar. The entry should be a descendant of the search bar. 95 * This is only required if the entry isn’t the direct child of the 96 * search bar (as in our main example). 97 * 98 * Params: 99 * entry = a #GtkEntry 100 * 101 * Since: 0.0.6 102 */ 103 public void connectEntry(Entry entry) 104 { 105 hdy_search_bar_connect_entry(hdySearchBar, (entry is null) ? null : entry.getEntryStruct()); 106 } 107 108 /** 109 * Returns whether the search mode is on or off. 110 * 111 * Returns: whether search mode is toggled on 112 * 113 * Since: 0.0.6 114 */ 115 public bool getSearchMode() 116 { 117 return hdy_search_bar_get_search_mode(hdySearchBar) != 0; 118 } 119 120 /** 121 * Returns whether the close button is shown. 122 * 123 * Returns: whether the close button is shown 124 * 125 * Since: 0.0.6 126 */ 127 public bool getShowCloseButton() 128 { 129 return hdy_search_bar_get_show_close_button(hdySearchBar) != 0; 130 } 131 132 /** 133 * This function should be called when the top-level 134 * window which contains the search bar received a key event. 135 * 136 * If the key event is handled by the search bar, the bar will 137 * be shown, the entry populated with the entered text and %GDK_EVENT_STOP 138 * will be returned. The caller should ensure that events are 139 * not propagated further. 140 * 141 * If no entry has been connected to the search bar, using 142 * hdy_search_bar_connect_entry(), this function will return 143 * immediately with a warning. 144 * 145 * ## Showing the search bar on key presses 146 * 147 * |[<!-- language="C" --> 148 * static gboolean 149 * on_key_press_event (GtkWidget *widget, 150 * GdkEvent *event, 151 * gpointer user_data) 152 * { 153 * HdySearchBar *bar = HDY_SEARCH_BAR (user_data); 154 * return hdy_search_bar_handle_event (self, event); 155 * } 156 * 157 * static void 158 * create_toplevel (void) 159 * { 160 * GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 161 * GtkWindow *search_bar = hdy_search_bar_new (); 162 * 163 * // Add more widgets to the window... 164 * 165 * g_signal_connect (window, 166 * "key-press-event", 167 * G_CALLBACK (on_key_press_event), 168 * search_bar); 169 * } 170 * ]| 171 * 172 * Params: 173 * event = a #GdkEvent containing key press events 174 * 175 * Returns: %GDK_EVENT_STOP if the key press event resulted 176 * in text being entered in the search entry (and revealing 177 * the search bar if necessary), %GDK_EVENT_PROPAGATE otherwise. 178 * 179 * Since: 0.0.6 180 */ 181 public bool handleEvent(Event event) 182 { 183 return hdy_search_bar_handle_event(hdySearchBar, (event is null) ? null : event.getEventStruct()) != 0; 184 } 185 186 /** 187 * Switches the search mode on or off. 188 * 189 * Params: 190 * searchMode = the new state of the search mode 191 * 192 * Since: 0.0.6 193 */ 194 public void setSearchMode(bool searchMode) 195 { 196 hdy_search_bar_set_search_mode(hdySearchBar, searchMode); 197 } 198 199 /** 200 * Shows or hides the close button. Applications that 201 * already have a “search” toggle button should not show a close 202 * button in their search bar, as it duplicates the role of the 203 * toggle button. 204 * 205 * Params: 206 * visible = whether the close button will be shown or not 207 * 208 * Since: 0.0.6 209 */ 210 public void setShowCloseButton(bool visible) 211 { 212 hdy_search_bar_set_show_close_button(hdySearchBar, visible); 213 } 214 }