| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: minifram.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | #ifdef __GNUG__ |
| 11 | #pragma implementation "minifram.h" |
| 12 | #endif |
| 13 | |
| 14 | #include "wx/minifram.h" |
| 15 | |
| 16 | #if wxUSE_MINIFRAME |
| 17 | |
| 18 | #include "wx/dcscreen.h" |
| 19 | |
| 20 | #include "gtk/gtk.h" |
| 21 | #include "wx/gtk/win_gtk.h" |
| 22 | |
| 23 | #include <gdk/gdk.h> |
| 24 | #include <gdk/gdkprivate.h> |
| 25 | #include <gdk/gdkx.h> |
| 26 | |
| 27 | //----------------------------------------------------------------------------- |
| 28 | // idle system |
| 29 | //----------------------------------------------------------------------------- |
| 30 | |
| 31 | extern void wxapp_install_idle_handler(); |
| 32 | extern bool g_isIdle; |
| 33 | |
| 34 | //----------------------------------------------------------------------------- |
| 35 | // data |
| 36 | //----------------------------------------------------------------------------- |
| 37 | |
| 38 | extern bool g_blockEventsOnDrag; |
| 39 | extern bool g_blockEventsOnScroll; |
| 40 | extern GtkWidget *wxGetRootWindow(); |
| 41 | |
| 42 | //----------------------------------------------------------------------------- |
| 43 | // local functions |
| 44 | //----------------------------------------------------------------------------- |
| 45 | |
| 46 | /* draw XOR rectangle when moving mine frame around */ |
| 47 | |
| 48 | static void DrawFrame( GtkWidget *widget, int x, int y, int w, int h ) |
| 49 | { |
| 50 | int org_x = 0; |
| 51 | int org_y = 0; |
| 52 | gdk_window_get_origin( widget->window, &org_x, &org_y ); |
| 53 | x += org_x; |
| 54 | y += org_y; |
| 55 | |
| 56 | GdkGC *gc = gdk_gc_new( GDK_ROOT_PARENT() ); |
| 57 | gdk_gc_set_subwindow( gc, GDK_INCLUDE_INFERIORS ); |
| 58 | gdk_gc_set_function( gc, GDK_INVERT ); |
| 59 | |
| 60 | gdk_draw_rectangle( GDK_ROOT_PARENT(), gc, FALSE, x, y, w, h ); |
| 61 | gdk_gc_unref( gc ); |
| 62 | } |
| 63 | |
| 64 | //----------------------------------------------------------------------------- |
| 65 | // "expose_event" of m_mainWidget |
| 66 | //----------------------------------------------------------------------------- |
| 67 | |
| 68 | static void gtk_window_own_expose_callback( GtkWidget *widget, GdkEventExpose *gdk_event, wxFrame *win ) |
| 69 | { |
| 70 | if (g_isIdle) wxapp_install_idle_handler(); |
| 71 | |
| 72 | if (!win->m_hasVMT) return; |
| 73 | if (gdk_event->count > 0) return; |
| 74 | |
| 75 | GtkPizza *pizza = GTK_PIZZA(widget); |
| 76 | |
| 77 | gtk_draw_shadow( widget->style, |
| 78 | pizza->bin_window, |
| 79 | GTK_STATE_NORMAL, |
| 80 | GTK_SHADOW_OUT, |
| 81 | 0, 0, |
| 82 | win->m_width, win->m_height ); |
| 83 | |
| 84 | if (!win->m_title.IsEmpty() && |
| 85 | ((win->GetWindowStyle() & wxCAPTION) || |
| 86 | (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) || |
| 87 | (win->GetWindowStyle() & wxTINY_CAPTION_VERT))) |
| 88 | { |
| 89 | GdkGC *gc = gdk_gc_new( pizza->bin_window ); |
| 90 | GdkFont *font = wxSMALL_FONT->GetInternalFont(1.0); |
| 91 | |
| 92 | gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] ); |
| 93 | gdk_draw_rectangle( pizza->bin_window, gc, TRUE, |
| 94 | 3, |
| 95 | 3, |
| 96 | win->m_width - 7, |
| 97 | font->ascent + font->descent+1 ); |
| 98 | |
| 99 | gdk_gc_set_foreground( gc, &widget->style->fg[GTK_STATE_SELECTED] ); |
| 100 | gdk_draw_string( pizza->bin_window, font, gc, |
| 101 | 6, |
| 102 | 3+font->ascent, |
| 103 | win->m_title.mb_str() ); |
| 104 | |
| 105 | gdk_gc_unref( gc ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | //----------------------------------------------------------------------------- |
| 110 | // "draw" of m_mainWidget |
| 111 | //----------------------------------------------------------------------------- |
| 112 | |
| 113 | static void gtk_window_own_draw_callback( GtkWidget *widget, GdkRectangle *WXUNUSED(rect), wxFrame *win ) |
| 114 | { |
| 115 | if (g_isIdle) wxapp_install_idle_handler(); |
| 116 | |
| 117 | if (!win->m_hasVMT) return; |
| 118 | |
| 119 | GtkPizza *pizza = GTK_PIZZA(widget); |
| 120 | |
| 121 | gtk_draw_shadow( widget->style, |
| 122 | pizza->bin_window, |
| 123 | GTK_STATE_NORMAL, |
| 124 | GTK_SHADOW_OUT, |
| 125 | 0, 0, |
| 126 | win->m_width, win->m_height ); |
| 127 | |
| 128 | if (!win->m_title.IsEmpty() && |
| 129 | ((win->GetWindowStyle() & wxCAPTION) || |
| 130 | (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) || |
| 131 | (win->GetWindowStyle() & wxTINY_CAPTION_VERT))) |
| 132 | { |
| 133 | GdkGC *gc = gdk_gc_new( pizza->bin_window ); |
| 134 | GdkFont *font = wxSMALL_FONT->GetInternalFont(1.0); |
| 135 | |
| 136 | gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] ); |
| 137 | gdk_draw_rectangle( pizza->bin_window, gc, TRUE, |
| 138 | 3, |
| 139 | 3, |
| 140 | win->m_width - 7, |
| 141 | font->ascent + font->descent+1 ); |
| 142 | |
| 143 | gdk_gc_set_foreground( gc, &widget->style->fg[GTK_STATE_SELECTED] ); |
| 144 | gdk_draw_string( pizza->bin_window, font, gc, |
| 145 | 6, |
| 146 | 3+font->ascent, |
| 147 | win->m_title.mb_str() ); |
| 148 | |
| 149 | gdk_gc_unref( gc ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | //----------------------------------------------------------------------------- |
| 154 | // "button_press_event" of m_mainWidget |
| 155 | //----------------------------------------------------------------------------- |
| 156 | |
| 157 | static gint gtk_window_button_press_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win ) |
| 158 | { |
| 159 | if (g_isIdle) wxapp_install_idle_handler(); |
| 160 | |
| 161 | if (!win->m_hasVMT) return FALSE; |
| 162 | if (g_blockEventsOnDrag) return TRUE; |
| 163 | if (g_blockEventsOnScroll) return TRUE; |
| 164 | |
| 165 | if (win->m_isDragging) return TRUE; |
| 166 | |
| 167 | GtkPizza *pizza = GTK_PIZZA(widget); |
| 168 | if (gdk_event->window != pizza->bin_window) return TRUE; |
| 169 | |
| 170 | GdkFont *font = wxSMALL_FONT->GetInternalFont(1.0); |
| 171 | int height = font->ascent + font->descent+1; |
| 172 | if (gdk_event->y > height) return TRUE; |
| 173 | |
| 174 | gdk_window_raise( win->m_widget->window ); |
| 175 | |
| 176 | gdk_pointer_grab( widget->window, FALSE, |
| 177 | (GdkEventMask) |
| 178 | (GDK_BUTTON_PRESS_MASK | |
| 179 | GDK_BUTTON_RELEASE_MASK | |
| 180 | GDK_POINTER_MOTION_MASK | |
| 181 | GDK_POINTER_MOTION_HINT_MASK | |
| 182 | GDK_BUTTON_MOTION_MASK | |
| 183 | GDK_BUTTON1_MOTION_MASK), |
| 184 | (GdkWindow *) NULL, |
| 185 | (GdkCursor *) NULL, |
| 186 | (unsigned int) GDK_CURRENT_TIME ); |
| 187 | |
| 188 | win->m_diffX = (int)gdk_event->x; |
| 189 | win->m_diffY = (int)gdk_event->y; |
| 190 | DrawFrame( widget, 0, 0, win->m_width, win->m_height ); |
| 191 | win->m_oldX = 0; |
| 192 | win->m_oldY = 0; |
| 193 | |
| 194 | win->m_isDragging = TRUE; |
| 195 | |
| 196 | return TRUE; |
| 197 | } |
| 198 | |
| 199 | //----------------------------------------------------------------------------- |
| 200 | // "button_release_event" of m_mainWidget |
| 201 | //----------------------------------------------------------------------------- |
| 202 | |
| 203 | static gint gtk_window_button_release_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win ) |
| 204 | { |
| 205 | if (g_isIdle) wxapp_install_idle_handler(); |
| 206 | |
| 207 | if (!win->m_hasVMT) return FALSE; |
| 208 | if (g_blockEventsOnDrag) return TRUE; |
| 209 | if (g_blockEventsOnScroll) return TRUE; |
| 210 | |
| 211 | if (!win->m_isDragging) return TRUE; |
| 212 | |
| 213 | win->m_isDragging = FALSE; |
| 214 | |
| 215 | int x = (int)gdk_event->x; |
| 216 | int y = (int)gdk_event->y; |
| 217 | |
| 218 | DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height ); |
| 219 | gdk_pointer_ungrab ( (guint32)GDK_CURRENT_TIME ); |
| 220 | int org_x = 0; |
| 221 | int org_y = 0; |
| 222 | gdk_window_get_origin( widget->window, &org_x, &org_y ); |
| 223 | x += org_x - win->m_diffX; |
| 224 | y += org_y - win->m_diffY; |
| 225 | win->m_x = x; |
| 226 | win->m_y = y; |
| 227 | gtk_widget_set_uposition( win->m_widget, x, y ); |
| 228 | |
| 229 | return TRUE; |
| 230 | } |
| 231 | |
| 232 | //----------------------------------------------------------------------------- |
| 233 | // "motion_notify_event" of m_mainWidget |
| 234 | //----------------------------------------------------------------------------- |
| 235 | |
| 236 | static gint gtk_window_motion_notify_callback( GtkWidget *widget, GdkEventMotion *gdk_event, wxMiniFrame *win ) |
| 237 | { |
| 238 | if (g_isIdle) wxapp_install_idle_handler(); |
| 239 | |
| 240 | if (!win->m_hasVMT) return FALSE; |
| 241 | if (g_blockEventsOnDrag) return TRUE; |
| 242 | if (g_blockEventsOnScroll) return TRUE; |
| 243 | |
| 244 | if (!win->m_isDragging) return TRUE; |
| 245 | |
| 246 | if (gdk_event->is_hint) |
| 247 | { |
| 248 | int x = 0; |
| 249 | int y = 0; |
| 250 | GdkModifierType state; |
| 251 | gdk_window_get_pointer(gdk_event->window, &x, &y, &state); |
| 252 | gdk_event->x = x; |
| 253 | gdk_event->y = y; |
| 254 | gdk_event->state = state; |
| 255 | } |
| 256 | |
| 257 | DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height ); |
| 258 | win->m_oldX = (int)gdk_event->x - win->m_diffX; |
| 259 | win->m_oldY = (int)gdk_event->y - win->m_diffY; |
| 260 | DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height ); |
| 261 | |
| 262 | return TRUE; |
| 263 | } |
| 264 | |
| 265 | //----------------------------------------------------------------------------- |
| 266 | // "clicked" of X system button |
| 267 | //----------------------------------------------------------------------------- |
| 268 | |
| 269 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxMiniFrame *mf ) |
| 270 | { |
| 271 | if (g_isIdle) wxapp_install_idle_handler(); |
| 272 | |
| 273 | mf->Close(); |
| 274 | } |
| 275 | |
| 276 | //----------------------------------------------------------------------------- |
| 277 | // wxMiniFrame |
| 278 | //----------------------------------------------------------------------------- |
| 279 | |
| 280 | static const char *cross_xpm[] = { |
| 281 | /* columns rows colors chars-per-pixel */ |
| 282 | "5 5 16 1", |
| 283 | " c Gray0", |
| 284 | ". c #bf0000", |
| 285 | "X c #00bf00", |
| 286 | "o c #bfbf00", |
| 287 | "O c #0000bf", |
| 288 | "+ c #bf00bf", |
| 289 | "@ c #00bfbf", |
| 290 | "# c None", |
| 291 | "$ c #808080", |
| 292 | "% c Red", |
| 293 | "& c Green", |
| 294 | "* c Yellow", |
| 295 | "= c Blue", |
| 296 | "- c Magenta", |
| 297 | "; c Cyan", |
| 298 | ": c Gray100", |
| 299 | /* pixels */ |
| 300 | " ### ", |
| 301 | "# # #", |
| 302 | "## ##", |
| 303 | "# # #", |
| 304 | " ### ", |
| 305 | }; |
| 306 | |
| 307 | IMPLEMENT_DYNAMIC_CLASS(wxMiniFrame,wxFrame) |
| 308 | |
| 309 | bool wxMiniFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title, |
| 310 | const wxPoint &pos, const wxSize &size, |
| 311 | long style, const wxString &name ) |
| 312 | { |
| 313 | style = style | wxCAPTION; |
| 314 | |
| 315 | if ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT)) |
| 316 | m_miniTitle = 13; |
| 317 | |
| 318 | m_miniEdge = 3; |
| 319 | m_isDragging = FALSE; |
| 320 | m_oldX = -1; |
| 321 | m_oldY = -1; |
| 322 | m_diffX = 0; |
| 323 | m_diffY = 0; |
| 324 | |
| 325 | wxFrame::Create( parent, id, title, pos, size, style, name ); |
| 326 | |
| 327 | if (m_parent && (GTK_IS_WINDOW(m_parent->m_widget))) |
| 328 | { |
| 329 | gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) ); |
| 330 | } |
| 331 | |
| 332 | if ((style & wxSYSTEM_MENU) && |
| 333 | ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT))) |
| 334 | { |
| 335 | GdkBitmap *mask = (GdkBitmap*) NULL; |
| 336 | GdkPixmap *pixmap = gdk_pixmap_create_from_xpm_d |
| 337 | ( |
| 338 | wxGetRootWindow()->window, |
| 339 | &mask, |
| 340 | NULL, |
| 341 | (char **)cross_xpm |
| 342 | ); |
| 343 | |
| 344 | GtkWidget *pw = gtk_pixmap_new( pixmap, mask ); |
| 345 | gdk_bitmap_unref( mask ); |
| 346 | gdk_pixmap_unref( pixmap ); |
| 347 | gtk_widget_show( pw ); |
| 348 | |
| 349 | GtkWidget *close_button = gtk_button_new(); |
| 350 | gtk_container_add( GTK_CONTAINER(close_button), pw ); |
| 351 | |
| 352 | gtk_pizza_put( GTK_PIZZA(m_mainWidget), |
| 353 | close_button, |
| 354 | size.x-16, 4, 11, 11 ); |
| 355 | |
| 356 | gtk_widget_show( close_button ); |
| 357 | |
| 358 | gtk_signal_connect( GTK_OBJECT(close_button), "clicked", |
| 359 | GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this ); |
| 360 | } |
| 361 | |
| 362 | /* these are called when the borders are drawn */ |
| 363 | gtk_signal_connect( GTK_OBJECT(m_mainWidget), "expose_event", |
| 364 | GTK_SIGNAL_FUNC(gtk_window_own_expose_callback), (gpointer)this ); |
| 365 | |
| 366 | gtk_signal_connect( GTK_OBJECT(m_mainWidget), "draw", |
| 367 | GTK_SIGNAL_FUNC(gtk_window_own_draw_callback), (gpointer)this ); |
| 368 | |
| 369 | /* these are required for dragging the mini frame around */ |
| 370 | gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_press_event", |
| 371 | GTK_SIGNAL_FUNC(gtk_window_button_press_callback), (gpointer)this ); |
| 372 | |
| 373 | gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_release_event", |
| 374 | GTK_SIGNAL_FUNC(gtk_window_button_release_callback), (gpointer)this ); |
| 375 | |
| 376 | gtk_signal_connect( GTK_OBJECT(m_mainWidget), "motion_notify_event", |
| 377 | GTK_SIGNAL_FUNC(gtk_window_motion_notify_callback), (gpointer)this ); |
| 378 | |
| 379 | return TRUE; |
| 380 | } |
| 381 | |
| 382 | #endif |